From e4dae20a3a64ef9cf41a5bb7a138813bfc1d8280 Mon Sep 17 00:00:00 2001 From: Catfriend1 Date: Sun, 27 Jan 2019 20:01:18 +0100 Subject: [PATCH] Fix leftover SyncthingNative instance after update in root mode (fixes #261) (#284) * Reformat code * Fix leftover SyncthingNative instance after update in root mode (fixes #261) --- .../receiver/BootReceiver.java | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/nutomic/syncthingandroid/receiver/BootReceiver.java b/app/src/main/java/com/nutomic/syncthingandroid/receiver/BootReceiver.java index 897395c6..8c38bc39 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/receiver/BootReceiver.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/receiver/BootReceiver.java @@ -6,20 +6,41 @@ import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; import android.preference.PreferenceManager; +import android.util.Log; import com.nutomic.syncthingandroid.service.Constants; +import com.nutomic.syncthingandroid.service.SyncthingRunnable; import com.nutomic.syncthingandroid.service.SyncthingService; +import eu.chainfire.libsuperuser.Shell; + public class BootReceiver extends BroadcastReceiver { + private static final String TAG = "BootReceiver"; + @Override public void onReceive(Context context, Intent intent) { - if (!intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED) && - !intent.getAction().equals(Intent.ACTION_MY_PACKAGE_REPLACED)) + Boolean bootCompleted = intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED); + Boolean packageReplaced = intent.getAction().equals(Intent.ACTION_MY_PACKAGE_REPLACED); + if (!bootCompleted && !packageReplaced) { return; + } - if (!startServiceOnBoot(context)) + if (packageReplaced) { + if (getPrefUseRoot(context) && Shell.SU.available()) { + /** + * In Root mode, there will be a SyncthingNative process left running after app update. + * See https://github.com/Catfriend1/syncthing-android/issues/261 + */ + Log.d(TAG, "ACTION_MY_PACKAGE_REPLACED: Killing leftover SyncthingNative instance if present ..."); + new SyncthingRunnable(context, SyncthingRunnable.Command.main).killSyncthing(); + } + } + + // Check if we should (re)start now. + if (!getPrefStartServiceOnBoot(context)) { return; + } startServiceCompat(context); } @@ -39,8 +60,13 @@ public class BootReceiver extends BroadcastReceiver { } } - private static boolean startServiceOnBoot(Context context) { + private static boolean getPrefStartServiceOnBoot(Context context) { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); return sp.getBoolean(Constants.PREF_START_SERVICE_ON_BOOT, false); } + + private static boolean getPrefUseRoot(Context context) { + SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); + return sp.getBoolean(Constants.PREF_USE_ROOT, false); + } }