mirror of
https://github.com/syncthing/syncthing-android.git
synced 2024-12-27 13:21:30 +00:00
Merge pull request #547 from capi/wakelock
Experimental setting to keep a partial wakelock
This commit is contained in:
commit
f335afe735
6 changed files with 44 additions and 0 deletions
|
@ -10,6 +10,7 @@
|
|||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
|
||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
||||
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||
|
||||
<application
|
||||
android:allowBackup="false"
|
||||
|
|
|
@ -58,6 +58,7 @@ public class SettingsFragment extends PreferenceFragment
|
|||
private CheckBoxPreference mSyncOnlyWifi;
|
||||
private WifiSsidPreference mSyncOnlyOnSSIDs;
|
||||
private CheckBoxPreference mUseRoot;
|
||||
private CheckBoxPreference mKeepWakelock;
|
||||
private PreferenceScreen mOptionsScreen;
|
||||
private PreferenceScreen mGuiScreen;
|
||||
private SyncthingService mSyncthingService;
|
||||
|
@ -134,6 +135,7 @@ public class SettingsFragment extends PreferenceFragment
|
|||
mSyncOnlyOnSSIDs = (WifiSsidPreference) findPreference(SyncthingService.PREF_SYNC_ONLY_WIFI_SSIDS);
|
||||
mSyncOnlyOnSSIDs.setDefaultValue(new TreeSet<String>()); // default to empty list
|
||||
mUseRoot = (CheckBoxPreference) findPreference(SyncthingService.PREF_USE_ROOT);
|
||||
mKeepWakelock = (CheckBoxPreference) findPreference(SyncthingService.PREF_USE_WAKE_LOCK);
|
||||
Preference appVersion = screen.findPreference(APP_VERSION_KEY);
|
||||
mOptionsScreen = (PreferenceScreen) screen.findPreference(SYNCTHING_OPTIONS_KEY);
|
||||
mGuiScreen = (PreferenceScreen) screen.findPreference(SYNCTHING_GUI_KEY);
|
||||
|
@ -153,6 +155,7 @@ public class SettingsFragment extends PreferenceFragment
|
|||
mSyncOnlyWifi.setOnPreferenceChangeListener(this);
|
||||
mSyncOnlyOnSSIDs.setOnPreferenceChangeListener(this);
|
||||
mUseRoot.setOnPreferenceClickListener(this);
|
||||
mKeepWakelock.setOnPreferenceClickListener(this);
|
||||
screen.findPreference(EXPORT_CONFIG).setOnPreferenceClickListener(this);
|
||||
screen.findPreference(IMPORT_CONFIG).setOnPreferenceClickListener(this);
|
||||
screen.findPreference(SYNCTHING_RESET).setOnPreferenceClickListener(this);
|
||||
|
@ -358,6 +361,9 @@ public class SettingsFragment extends PreferenceFragment
|
|||
mSyncthingService.getApi().requireRestart(getActivity());
|
||||
}
|
||||
return true;
|
||||
case SyncthingService.PREF_USE_WAKE_LOCK:
|
||||
mSyncthingService.getApi().requireRestart(getActivity());
|
||||
return true;
|
||||
case EXPORT_CONFIG:
|
||||
new AlertDialog.Builder(getActivity())
|
||||
.setMessage(R.string.dialog_confirm_export)
|
||||
|
|
|
@ -4,6 +4,7 @@ import android.content.Context;
|
|||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Environment;
|
||||
import android.os.PowerManager;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
@ -95,7 +96,14 @@ public class SyncthingRunnable implements Runnable {
|
|||
}
|
||||
// Loop Syncthing
|
||||
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();
|
||||
// Loop to handle Syncthing restarts (these always have an error code of 3).
|
||||
do {
|
||||
ProcessBuilder pb = (useRoot())
|
||||
|
@ -132,6 +140,8 @@ public class SyncthingRunnable implements Runnable {
|
|||
} catch (IOException | InterruptedException e) {
|
||||
Log.e(TAG, "Failed to execute syncthing binary or read output", e);
|
||||
} finally {
|
||||
if (wakeLock != null)
|
||||
wakeLock.release();
|
||||
if (process != null)
|
||||
process.destroy();
|
||||
if (ret != 0) {
|
||||
|
@ -149,6 +159,14 @@ public class SyncthingRunnable implements Runnable {
|
|||
return sp.getBoolean(SyncthingService.PREF_USE_ROOT, false) && Shell.SU.available();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the experimental setting for using wake locks has been enabled in settings.
|
||||
*/
|
||||
private boolean useWakeLock() {
|
||||
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
|
||||
return sp.getBoolean(SyncthingService.PREF_USE_WAKE_LOCK, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Look for a running libsyncthing.so process and nice its IO.
|
||||
*/
|
||||
|
|
|
@ -94,6 +94,7 @@ public class SyncthingService extends Service implements
|
|||
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_NOTIFICATION_TYPE = "notification_type";
|
||||
public static final String PREF_USE_WAKE_LOCK = "wakelock_while_binary_running";
|
||||
|
||||
private static final int NOTIFICATION_ACTIVE = 1;
|
||||
|
||||
|
|
|
@ -296,6 +296,12 @@ Please report any problems you encounter via Github.</string>
|
|||
|
||||
<string name="export_config">Export Configuration</string>
|
||||
|
||||
<string name="experimental_settings">Experimental</string>
|
||||
|
||||
<string name="keep_wakelock_while_binary_running">Keep the CPU awake while Syncthing is running</string>
|
||||
|
||||
<string name="keep_wakelock_while_binary_running_summary">Use this setting if you experience unexpected disconnects while operating on battery. This will result in increased battery consumption.</string>
|
||||
|
||||
<!-- Toast shown after config was successfully exported -->
|
||||
<string name="config_export_successful">Config was exported to %1$s</string>
|
||||
|
||||
|
|
|
@ -147,6 +147,18 @@
|
|||
android:title="@string/streset_title"
|
||||
android:singleLine="true" />
|
||||
|
||||
<PreferenceScreen
|
||||
android:key="syncthing_experimental"
|
||||
android:title="@string/experimental_settings">
|
||||
|
||||
<CheckBoxPreference
|
||||
android:key="wakelock_while_binary_running"
|
||||
android:title="@string/keep_wakelock_while_binary_running"
|
||||
android:summary="@string/keep_wakelock_while_binary_running_summary"
|
||||
android:defaultValue="false" />
|
||||
|
||||
</PreferenceScreen>
|
||||
|
||||
</PreferenceCategory>
|
||||
|
||||
<PreferenceCategory
|
||||
|
|
Loading…
Reference in a new issue