1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-12-27 13:21:30 +00:00

Moved shouldRun check into DeviceStateHolder.

This commit is contained in:
Felix Ableitner 2016-08-02 19:12:50 +02:00
parent 45894bacb8
commit 8cb9105197
2 changed files with 55 additions and 49 deletions

View file

@ -4,10 +4,16 @@ import android.annotation.TargetApi;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.BatteryManager;
import android.preference.PreferenceManager;
import android.util.Log;
import java.util.HashSet;
import java.util.Set;
/**
* Holds information about the current wifi and charging state of the device.
@ -17,6 +23,8 @@ import android.os.BatteryManager;
*/
public class DeviceStateHolder extends BroadcastReceiver {
private static final String TAG = "DeviceStateHolder";
/**
* Intent extra containing a boolean saying whether wifi is connected or not.
*/
@ -91,7 +99,52 @@ public class DeviceStateHolder extends BroadcastReceiver {
}
}
public String getWifiSsid() {
private String getWifiSsid() {
return mWifiSsid;
}
public boolean shouldRun() {
if (SyncthingService.alwaysRunInBackground(mContext)) {
// Always run, ignoring wifi/charging state.
return true;
}
else {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
// Check wifi/charging state against preferences and start if ok.
boolean prefStopMobileData = prefs.getBoolean(SyncthingService.PREF_SYNC_ONLY_WIFI, false);
boolean prefStopNotCharging = prefs.getBoolean(SyncthingService.PREF_SYNC_ONLY_CHARGING, false);
return (isCharging() || !prefStopNotCharging) &&
(!prefStopMobileData || isAllowedWifiConnected());
}
}
private boolean isAllowedWifiConnected() {
boolean wifiConnected = isWifiConnected();
if (wifiConnected) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
Set<String> ssids = sp.getStringSet(SyncthingService.PREF_SYNC_ONLY_WIFI_SSIDS, new HashSet<String>());
if (ssids.isEmpty()) {
Log.d(TAG, "All SSIDs allowed for syncing");
return true;
} else {
String ssid = getWifiSsid();
if (ssid != null) {
if (ssids.contains(ssid)) {
Log.d(TAG, "SSID [" + ssid + "] found in whitelist: " + ssids);
return true;
}
Log.i(TAG, "SSID [" + ssid + "] not whitelisted: " + ssids);
return false;
} else {
// Don't know the SSID (yet) (should not happen?!), so not allowing
Log.w(TAG, "SSID unknown (yet), cannot check SSID whitelist. Disallowing sync.");
return false;
}
}
}
Log.d(TAG, "Wifi not connected");
return false;
}
}

View file

@ -1,12 +1,9 @@
package com.nutomic.syncthingandroid.syncthing;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
@ -22,7 +19,6 @@ import android.widget.Toast;
import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.activities.MainActivity;
import com.nutomic.syncthingandroid.activities.SettingsActivity;
import com.nutomic.syncthingandroid.util.ConfigXml;
import com.nutomic.syncthingandroid.util.FolderObserver;
import com.nutomic.syncthingandroid.util.PRNGFixes;
@ -194,23 +190,8 @@ public class SyncthingService extends Service implements
* called.
*/
public void updateState() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean shouldRun;
if (!alwaysRunInBackground(this)) {
// Always run, ignoring wifi/charging state.
shouldRun = true;
}
else {
// Check wifi/charging state against preferences and start if ok.
boolean prefStopMobileData = prefs.getBoolean(PREF_SYNC_ONLY_WIFI, false);
boolean prefStopNotCharging = prefs.getBoolean(PREF_SYNC_ONLY_CHARGING, false);
shouldRun = (mDeviceStateHolder.isCharging() || !prefStopNotCharging) &&
(!prefStopMobileData || isAllowedWifiConnected());
}
// Start syncthing.
if (shouldRun) {
if (mDeviceStateHolder.shouldRun()) {
if (mCurrentState == State.ACTIVE || mCurrentState == State.STARTING) {
mStopScheduled = false;
return;
@ -257,34 +238,6 @@ public class SyncthingService extends Service implements
onApiChange();
}
private boolean isAllowedWifiConnected() {
boolean wifiConnected = mDeviceStateHolder.isWifiConnected();
if (wifiConnected) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Set<String> ssids = sp.getStringSet(PREF_SYNC_ONLY_WIFI_SSIDS, new HashSet<String>());
if (ssids.isEmpty()) {
Log.d(TAG, "All SSIDs allowed for syncing");
return true;
} else {
String ssid = mDeviceStateHolder.getWifiSsid();
if (ssid != null) {
if (ssids.contains(ssid)) {
Log.d(TAG, "SSID [" + ssid + "] found in whitelist: " + ssids);
return true;
}
Log.i(TAG, "SSID [" + ssid + "] not whitelisted: " + ssids);
return false;
} else {
// Don't know the SSID (yet) (should not happen?!), so not allowing
Log.w(TAG, "SSID unknown (yet), cannot check SSID whitelist. Disallowing sync.");
return false;
}
}
}
Log.d(TAG, "Wifi not connected");
return false;
}
/**
* Shows or hides the persistent notification based on running state and
* {@link #PREF_NOTIFICATION_TYPE}.