1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-12-23 11:21:29 +00:00

Added option to toggle persistent notification (fixes #366).

This commit is contained in:
Felix Ableitner 2015-05-19 00:15:09 +02:00
parent c496a5325b
commit 530c156a9a
4 changed files with 59 additions and 38 deletions

View file

@ -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;
}

View file

@ -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() {

View file

@ -229,9 +229,13 @@ Please report any problems you encounter via Github.</string>
<string name="advanced_folder_picker_summary">Select any folder on the device for syncing</string>
<string name="use_root_title">Sync as root</string>
<string name="use_root_title">Sync as Root</string>
<string name="use_root_summary">Run syncthing as superuser</string>
<string name="use_root_summary">Run Syncthing as Superuser</string>
<string name="persistent_notification_title">Persistent Notification</string>
<string name="persistent_notification_summary">Show Notification while Syncthing is running</string>
<string name="category_syncthing">Syncthing</string>

View file

@ -32,6 +32,12 @@
android:defaultValue="false"
android:enabled="false" />
<CheckBoxPreference
android:key="persistent_notification"
android:title="@string/persistent_notification_title"
android:summary="@string/persistent_notification_summary"
android:defaultValue="true" />
</PreferenceCategory>
<PreferenceCategory