diff --git a/src/main/java/com/nutomic/syncthingandroid/fragments/SettingsFragment.java b/src/main/java/com/nutomic/syncthingandroid/fragments/SettingsFragment.java index 40594973..fb5002fd 100644 --- a/src/main/java/com/nutomic/syncthingandroid/fragments/SettingsFragment.java +++ b/src/main/java/com/nutomic/syncthingandroid/fragments/SettingsFragment.java @@ -43,23 +43,15 @@ public class SettingsFragment extends PreferenceFragment private static final String IMPORT_CONFIG = "import_config"; private static final String STTRACE = "sttrace"; private static final String SYNCTHING_RESET = "streset"; - private static final String SYNCTHING_VERSION_KEY = "syncthing_version"; - - private static final String APP_VERSION_KEY = "app_version"; + private static final String APP_VERSION_KEY = "app_version"; private CheckBoxPreference mAlwaysRunInBackground; - private CheckBoxPreference mSyncOnlyCharging; - private CheckBoxPreference mSyncOnlyWifi; - private Preference mUseRoot; - private PreferenceScreen mOptionsScreen; - private PreferenceScreen mGuiScreen; - private SyncthingService mSyncthingService; @Override @@ -224,6 +216,8 @@ public class SettingsFragment extends PreferenceFragment pref.setSummary((String) o); } + boolean requireRestart = false; + if (preference.equals(mSyncOnlyCharging) || preference.equals(mSyncOnlyWifi)) { mSyncthingService.updateState(); } else if (preference.equals(mAlwaysRunInBackground)) { @@ -232,6 +226,10 @@ public class SettingsFragment extends PreferenceFragment : R.string.always_run_in_background_disabled); mSyncOnlyCharging.setEnabled((Boolean) o); mSyncOnlyWifi.setEnabled((Boolean) o); + } else if (preference.equals(mUseRoot)) { + if (!(Boolean) o) + new Thread(new ChownFilesRunnable()).start(); + requireRestart = true; } else if (preference.getKey().equals(DEVICE_NAME_KEY)) { RestApi.Device old = mSyncthingService.getApi().getLocalDevice(); RestApi.Device updated = new RestApi.Device(); @@ -254,7 +252,6 @@ public class SettingsFragment extends PreferenceFragment RestApi.TYPE_GUI, preference.getKey(), o, false, getActivity()); } - boolean requireRestart = false; // Avoid any code injection. int error = 0; @@ -281,16 +278,9 @@ public class SettingsFragment extends PreferenceFragment return false; } - if (preference.getKey().equals(SyncthingService.PREF_USE_ROOT)) { - if (!(Boolean) o) - new Thread(new ChownFilesRunnable()).start(); - requireRestart = true; - } - if (requireRestart) mSyncthingService.getApi().requireRestart(getActivity()); - return true; } diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java index 62206cce..1aa86e2c 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java @@ -40,7 +40,8 @@ import java.util.LinkedList; /** * Holds the native syncthing instance and provides an API to access it. */ -public class SyncthingService extends Service { +public class SyncthingService extends Service implements + SharedPreferences.OnSharedPreferenceChangeListener { private static final String TAG = "SyncthingService"; @@ -87,12 +88,10 @@ public class SyncthingService extends Service { public static final String BINARY_NAME = "lib/libsyncthing.so"; public static final String PREF_ALWAYS_RUN_IN_BACKGROUND = "always_run_in_background"; - - public static final String PREF_SYNC_ONLY_WIFI = "sync_only_wifi"; - - public static final String PREF_SYNC_ONLY_CHARGING = "sync_only_charging"; - - public static final String PREF_USE_ROOT = "use_root"; + public static final String PREF_SYNC_ONLY_WIFI = "sync_only_wifi"; + public static final String PREF_SYNC_ONLY_CHARGING = "sync_only_charging"; + public static final String PREF_USE_ROOT = "use_root"; + private static final String PREF_PERSISTENT_NOTIFICATION = "persistent_notification"; private static final int NOTIFICATION_ACTIVE = 1; @@ -180,6 +179,7 @@ public class SyncthingService extends Service { * called. */ public void updateState() { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); boolean shouldRun; if (!alwaysRunInBackground(this)) { // Always run, ignoring wifi/charging state. @@ -187,7 +187,6 @@ public class SyncthingService extends Service { } else { // Check wifi/charging state against preferences and start if ok. - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); boolean prefStopMobileData = prefs.getBoolean(PREF_SYNC_ONLY_WIFI, false); boolean prefStopNotCharging = prefs.getBoolean(PREF_SYNC_ONLY_CHARGING, false); @@ -195,8 +194,6 @@ public class SyncthingService extends Service { (mDeviceStateHolder.isWifiConnected() || !prefStopMobileData); } - NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); - // Start syncthing. if (shouldRun) { if (mCurrentState == State.ACTIVE || mCurrentState == State.STARTING) { @@ -216,15 +213,7 @@ public class SyncthingService extends Service { new PollWebGuiAvailableTaskImpl(getFilesDir() + "/" + HTTPS_CERT_FILE).execute(mConfig.getWebGuiUrl()); mRunnable = new SyncthingRunnable(this, SyncthingRunnable.Command.main); new Thread(mRunnable).start(); - Notification n = new NotificationCompat.Builder(this) - .setContentTitle(getString(R.string.syncthing_active)) - .setSmallIcon(R.drawable.ic_stat_notify) - .setOngoing(true) - .setPriority(NotificationCompat.PRIORITY_MIN) - .setContentIntent(PendingIntent.getActivity(this, 0, - new Intent(this, MainActivity.class), 0)) - .build(); - nm.notify(NOTIFICATION_ACTIVE, n); + updateNotification(); } // Stop syncthing. else { @@ -239,6 +228,35 @@ public class SyncthingService extends Service { onApiChange(); } + /** + * Shows or hides the persistent notification based on running state and + * {@link #PREF_PERSISTENT_NOTIFICATION}. + */ + private void updateNotification() { + SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); + NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); + if ((mCurrentState == State.ACTIVE || mCurrentState == State.STARTING) && + sp.getBoolean(PREF_PERSISTENT_NOTIFICATION, true)) { + Notification n = new NotificationCompat.Builder(this) + .setContentTitle(getString(R.string.syncthing_active)) + .setSmallIcon(R.drawable.ic_stat_notify) + .setOngoing(true) + .setPriority(NotificationCompat.PRIORITY_MIN) + .setContentIntent(PendingIntent.getActivity(this, 0, + new Intent(this, MainActivity.class), 0)) + .build(); + nm.notify(NOTIFICATION_ACTIVE, n); + } else { + nm.cancel(NOTIFICATION_ACTIVE); + } + } + + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + if (key.equals(PREF_PERSISTENT_NOTIFICATION)) + updateNotification(); + } + /** * Starts the native binary. */ @@ -267,6 +285,7 @@ public class SyncthingService extends Service { mDeviceStateHolder = new DeviceStateHolder(SyncthingService.this); registerReceiver(mDeviceStateHolder, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); new StartupTask(sp.getString("gui_user",""), sp.getString("gui_password","")).execute(); + sp.registerOnSharedPreferenceChangeListener(this); } /** @@ -330,6 +349,8 @@ public class SyncthingService extends Service { super.onDestroy(); Log.i(TAG, "Shutting down service"); shutdown(); + SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); + sp.unregisterOnSharedPreferenceChangeListener(this); } private void shutdown() { diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index 6ed0c734..ec8621f7 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -229,9 +229,13 @@ Please report any problems you encounter via Github. Select any folder on the device for syncing - Sync as root + Sync as Root - Run syncthing as superuser + Run Syncthing as Superuser + + Persistent Notification + + Show Notification while Syncthing is running Syncthing diff --git a/src/main/res/xml/app_settings.xml b/src/main/res/xml/app_settings.xml index 1742eb68..3436195c 100644 --- a/src/main/res/xml/app_settings.xml +++ b/src/main/res/xml/app_settings.xml @@ -32,6 +32,12 @@ android:defaultValue="false" android:enabled="false" /> + +