1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-30 08:11:17 +00:00

Merge pull request #547 from capi/wakelock

Experimental setting to keep a partial wakelock
This commit is contained in:
Felix Ableitner 2016-02-18 23:32:32 +01:00
commit f335afe735
6 changed files with 44 additions and 0 deletions

View file

@ -10,6 +10,7 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_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.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application <application
android:allowBackup="false" android:allowBackup="false"

View file

@ -58,6 +58,7 @@ public class SettingsFragment extends PreferenceFragment
private CheckBoxPreference mSyncOnlyWifi; private CheckBoxPreference mSyncOnlyWifi;
private WifiSsidPreference mSyncOnlyOnSSIDs; private WifiSsidPreference mSyncOnlyOnSSIDs;
private CheckBoxPreference mUseRoot; private CheckBoxPreference mUseRoot;
private CheckBoxPreference mKeepWakelock;
private PreferenceScreen mOptionsScreen; private PreferenceScreen mOptionsScreen;
private PreferenceScreen mGuiScreen; private PreferenceScreen mGuiScreen;
private SyncthingService mSyncthingService; private SyncthingService mSyncthingService;
@ -134,6 +135,7 @@ public class SettingsFragment extends PreferenceFragment
mSyncOnlyOnSSIDs = (WifiSsidPreference) findPreference(SyncthingService.PREF_SYNC_ONLY_WIFI_SSIDS); mSyncOnlyOnSSIDs = (WifiSsidPreference) findPreference(SyncthingService.PREF_SYNC_ONLY_WIFI_SSIDS);
mSyncOnlyOnSSIDs.setDefaultValue(new TreeSet<String>()); // default to empty list mSyncOnlyOnSSIDs.setDefaultValue(new TreeSet<String>()); // default to empty list
mUseRoot = (CheckBoxPreference) findPreference(SyncthingService.PREF_USE_ROOT); mUseRoot = (CheckBoxPreference) findPreference(SyncthingService.PREF_USE_ROOT);
mKeepWakelock = (CheckBoxPreference) findPreference(SyncthingService.PREF_USE_WAKE_LOCK);
Preference appVersion = screen.findPreference(APP_VERSION_KEY); Preference appVersion = screen.findPreference(APP_VERSION_KEY);
mOptionsScreen = (PreferenceScreen) screen.findPreference(SYNCTHING_OPTIONS_KEY); mOptionsScreen = (PreferenceScreen) screen.findPreference(SYNCTHING_OPTIONS_KEY);
mGuiScreen = (PreferenceScreen) screen.findPreference(SYNCTHING_GUI_KEY); mGuiScreen = (PreferenceScreen) screen.findPreference(SYNCTHING_GUI_KEY);
@ -153,6 +155,7 @@ public class SettingsFragment extends PreferenceFragment
mSyncOnlyWifi.setOnPreferenceChangeListener(this); mSyncOnlyWifi.setOnPreferenceChangeListener(this);
mSyncOnlyOnSSIDs.setOnPreferenceChangeListener(this); mSyncOnlyOnSSIDs.setOnPreferenceChangeListener(this);
mUseRoot.setOnPreferenceClickListener(this); mUseRoot.setOnPreferenceClickListener(this);
mKeepWakelock.setOnPreferenceClickListener(this);
screen.findPreference(EXPORT_CONFIG).setOnPreferenceClickListener(this); screen.findPreference(EXPORT_CONFIG).setOnPreferenceClickListener(this);
screen.findPreference(IMPORT_CONFIG).setOnPreferenceClickListener(this); screen.findPreference(IMPORT_CONFIG).setOnPreferenceClickListener(this);
screen.findPreference(SYNCTHING_RESET).setOnPreferenceClickListener(this); screen.findPreference(SYNCTHING_RESET).setOnPreferenceClickListener(this);
@ -358,6 +361,9 @@ public class SettingsFragment extends PreferenceFragment
mSyncthingService.getApi().requireRestart(getActivity()); mSyncthingService.getApi().requireRestart(getActivity());
} }
return true; return true;
case SyncthingService.PREF_USE_WAKE_LOCK:
mSyncthingService.getApi().requireRestart(getActivity());
return true;
case EXPORT_CONFIG: case EXPORT_CONFIG:
new AlertDialog.Builder(getActivity()) new AlertDialog.Builder(getActivity())
.setMessage(R.string.dialog_confirm_export) .setMessage(R.string.dialog_confirm_export)

View file

@ -4,6 +4,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Environment; import android.os.Environment;
import android.os.PowerManager;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
@ -95,7 +96,14 @@ public class SyncthingRunnable implements Runnable {
} }
// Loop Syncthing // Loop Syncthing
Process process = null; 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 { try {
if (wakeLock != null)
wakeLock.acquire();
// Loop to handle Syncthing restarts (these always have an error code of 3). // Loop to handle Syncthing restarts (these always have an error code of 3).
do { do {
ProcessBuilder pb = (useRoot()) ProcessBuilder pb = (useRoot())
@ -132,6 +140,8 @@ public class SyncthingRunnable implements Runnable {
} catch (IOException | InterruptedException e) { } catch (IOException | InterruptedException e) {
Log.e(TAG, "Failed to execute syncthing binary or read output", e); Log.e(TAG, "Failed to execute syncthing binary or read output", e);
} finally { } finally {
if (wakeLock != null)
wakeLock.release();
if (process != null) if (process != null)
process.destroy(); process.destroy();
if (ret != 0) { if (ret != 0) {
@ -149,6 +159,14 @@ public class SyncthingRunnable implements Runnable {
return sp.getBoolean(SyncthingService.PREF_USE_ROOT, false) && Shell.SU.available(); 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. * Look for a running libsyncthing.so process and nice its IO.
*/ */

View file

@ -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_SYNC_ONLY_CHARGING = "sync_only_charging";
public static final String PREF_USE_ROOT = "use_root"; public static final String PREF_USE_ROOT = "use_root";
private static final String PREF_NOTIFICATION_TYPE = "notification_type"; 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; private static final int NOTIFICATION_ACTIVE = 1;

View file

@ -296,6 +296,12 @@ Please report any problems you encounter via Github.</string>
<string name="export_config">Export Configuration</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 --> <!-- Toast shown after config was successfully exported -->
<string name="config_export_successful">Config was exported to %1$s</string> <string name="config_export_successful">Config was exported to %1$s</string>

View file

@ -147,6 +147,18 @@
android:title="@string/streset_title" android:title="@string/streset_title"
android:singleLine="true" /> 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>
<PreferenceCategory <PreferenceCategory