From 2021ef6806f0b627886501a2da5a9d53b4ec07b4 Mon Sep 17 00:00:00 2001 From: Catfriend1 Date: Sun, 7 Oct 2018 18:28:46 +0200 Subject: [PATCH] Disable wakelock experimental option on Android 6+ (#77) --- .../activities/SettingsActivity.java | 6 +++++ .../service/SyncthingRunnable.java | 27 ++++++++++--------- app/src/main/res/values-de/strings.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- app/src/main/res/xml/app_settings.xml | 13 ++++----- 5 files changed, 29 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java b/app/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java index 00851729..704a1831 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java @@ -231,6 +231,12 @@ public class SettingsActivity extends SyncthingActivity { mRunOnMeteredWifi.setEnabled(mRunOnWifi.isChecked()); mWifiSsidWhitelist.setEnabled(mRunOnWifi.isChecked()); + /* Experimental options */ + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + /* Wakelocks are only valid on Android 5 or lower. */ + mUseWakelock.setEnabled(false); + mUseWakelock.setChecked(false); + } mCategorySyncthingOptions = findPreference("category_syncthing_options"); setPreferenceCategoryChangeListener(mCategorySyncthingOptions, this::onSyncthingPreferenceChange); diff --git a/app/src/main/java/com/nutomic/syncthingandroid/service/SyncthingRunnable.java b/app/src/main/java/com/nutomic/syncthingandroid/service/SyncthingRunnable.java index dccd08bf..37ed0398 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/service/SyncthingRunnable.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/service/SyncthingRunnable.java @@ -4,6 +4,7 @@ import android.annotation.SuppressLint; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.os.Build; import android.os.Environment; import android.os.PowerManager; import android.os.SystemClock; @@ -133,13 +134,20 @@ public class SyncthingRunnable implements Runnable { } catch (IOException | InterruptedException e) { Log.w(TAG, "Failed to chmod Syncthing", e); } - // Loop Syncthing + + /** + * Potential fix for #498, keep the CPU running while native binary is running. + * Only valid on Android 5 or lower. + */ + PowerManager pm; + PowerManager.WakeLock wakeLock = null; + Boolean useWakeLock = mPreferences.getBoolean(Constants.PREF_USE_WAKE_LOCK, false); + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M && useWakeLock) { + pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); + wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); + } + Process process = null; - // Potential fix for #498, keep the CPU running while native binary is running - PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); - PowerManager.WakeLock wakeLock = useWakeLock() - ? pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG) - : null; try { if (wakeLock != null) { wakeLock.acquire(); @@ -263,13 +271,6 @@ public class SyncthingRunnable implements Runnable { } } - /** - * Returns true if the experimental setting for using wake locks has been enabled in settings. - */ - private boolean useWakeLock() { - return mPreferences.getBoolean(Constants.PREF_USE_WAKE_LOCK, false); - } - /** * Look for running libsyncthing.so processes and return an array * containing the PIDs of found instances. diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 777e69ab..7a246254 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -434,7 +434,7 @@ Bitte melden Sie auftretende Probleme via GitHub. Prozessor wach halten während Syncthing läuft. - Nutze dieses Einstellung, wenn du unerwartete Verbindungsabbrüche hast, während du im Batteriebetrieb arbeitest. Das wird zu einem erhöhten Energieverbrauch führen. + Nur für Android 5 oder niedriger. Nutze diese Einstellung, wenn du unerwartete Verbindungsabbrüche hast, während du im Batteriebetrieb arbeitest. Das wird zu einem erhöhten Energieverbrauch führen. Tor benutzen diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 916567f8..ba9e2886 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -438,7 +438,7 @@ Please report any problems you encounter via Github. Keep the CPU awake while Syncthing is running - Use this setting if you experience unexpected disconnects while operating on battery. This will result in increased battery consumption. + Only for Android 5 or lower. Use this setting if you experience unexpected disconnects while operating on battery. This will result in increased battery consumption. Use Tor diff --git a/app/src/main/res/xml/app_settings.xml b/app/src/main/res/xml/app_settings.xml index 2694caa4..e7444f03 100644 --- a/app/src/main/res/xml/app_settings.xml +++ b/app/src/main/res/xml/app_settings.xml @@ -242,12 +242,6 @@ android:title="@string/category_experimental" android:key="category_experimental"> - - + + +