mirror of
https://github.com/syncthing/syncthing-android.git
synced 2024-12-02 01:01:17 +00:00
Fix phone plugged to charger detection (#27)
* Fix phone plugged to charger detection * Add detection for wireless charging Correct API level from 21 to 17 * Fix typo in strings.xml
This commit is contained in:
parent
f50b933278
commit
e6af3ce3d2
2 changed files with 33 additions and 13 deletions
|
@ -41,8 +41,8 @@ public class RunConditionMonitor {
|
|||
|
||||
private static final String TAG = "RunConditionMonitor";
|
||||
|
||||
private static final String POWER_SOURCE_AC_BATTERY = "ac_and_battery_power";
|
||||
private static final String POWER_SOURCE_AC = "ac_power";
|
||||
private static final String POWER_SOURCE_CHARGER_BATTERY = "ac_and_battery_power";
|
||||
private static final String POWER_SOURCE_CHARGER = "ac_power";
|
||||
private static final String POWER_SOURCE_BATTERY = "battery_power";
|
||||
|
||||
private @Nullable Object mSyncStatusObserverHandle = null;
|
||||
|
@ -173,27 +173,27 @@ public class RunConditionMonitor {
|
|||
Set<String> whitelistedWifiSsids = mPreferences.getStringSet(Constants.PREF_WIFI_SSID_WHITELIST, new HashSet<>());
|
||||
boolean prefWifiWhitelistEnabled = !whitelistedWifiSsids.isEmpty();
|
||||
boolean prefRunInFlightMode = mPreferences.getBoolean(Constants.PREF_RUN_IN_FLIGHT_MODE, false);
|
||||
String prefPowerSource = mPreferences.getString(Constants.PREF_POWER_SOURCE, POWER_SOURCE_AC_BATTERY);
|
||||
String prefPowerSource = mPreferences.getString(Constants.PREF_POWER_SOURCE, POWER_SOURCE_CHARGER_BATTERY);
|
||||
boolean prefRespectPowerSaving = mPreferences.getBoolean(Constants.PREF_RESPECT_BATTERY_SAVING, true);
|
||||
boolean prefRespectMasterSync = mPreferences.getBoolean(Constants.PREF_RESPECT_MASTER_SYNC, false);
|
||||
|
||||
// PREF_POWER_SOURCE
|
||||
switch (prefPowerSource) {
|
||||
case POWER_SOURCE_AC:
|
||||
if (!isOnAcPower()) {
|
||||
Log.v(TAG, "decideShouldRun: POWER_SOURCE_AC && !isOnAcPower");
|
||||
mRunDecisionExplanation = res.getString(R.string.reason_not_on_ac_power);
|
||||
case POWER_SOURCE_CHARGER:
|
||||
if (!isCharging()) {
|
||||
Log.v(TAG, "decideShouldRun: POWER_SOURCE_AC && !isCharging");
|
||||
mRunDecisionExplanation = res.getString(R.string.reason_not_charging);
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case POWER_SOURCE_BATTERY:
|
||||
if (isOnAcPower()) {
|
||||
Log.v(TAG, "decideShouldRun: POWER_SOURCE_BATTERY && isOnAcPower");
|
||||
if (isCharging()) {
|
||||
Log.v(TAG, "decideShouldRun: POWER_SOURCE_BATTERY && isCharging");
|
||||
mRunDecisionExplanation = res.getString(R.string.reason_not_on_battery_power);
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case POWER_SOURCE_AC_BATTERY:
|
||||
case POWER_SOURCE_CHARGER_BATTERY:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
@ -299,13 +299,33 @@ public class RunConditionMonitor {
|
|||
/**
|
||||
* Functions for run condition information retrieval.
|
||||
*/
|
||||
private boolean isOnAcPower() {
|
||||
private boolean isCharging() {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
|
||||
// API level < 21
|
||||
return isCharging_API16();
|
||||
} else {
|
||||
// API level >= 21
|
||||
return isCharging_API17();
|
||||
}
|
||||
}
|
||||
|
||||
@TargetApi(16)
|
||||
private boolean isCharging_API16() {
|
||||
Intent batteryIntent = mContext.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
||||
int status = batteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
|
||||
return status == BatteryManager.BATTERY_STATUS_CHARGING ||
|
||||
status == BatteryManager.BATTERY_STATUS_FULL;
|
||||
}
|
||||
|
||||
@TargetApi(17)
|
||||
private boolean isCharging_API17() {
|
||||
Intent intent = mContext.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
||||
int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
|
||||
return plugged == BatteryManager.BATTERY_PLUGGED_AC ||
|
||||
plugged == BatteryManager.BATTERY_PLUGGED_USB ||
|
||||
plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
|
||||
}
|
||||
|
||||
@TargetApi(21)
|
||||
private boolean isPowerSaving() {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
|
||||
|
|
|
@ -124,7 +124,7 @@ Please report any problems you encounter via Github.</string>
|
|||
<!-- Indicates that the device is paused and does not sync -->
|
||||
<string name="device_paused">Paused</string>
|
||||
|
||||
!-- Indicates that the device connection is unknown -->
|
||||
<!-- Indicates that the device connection is unknown -->
|
||||
<string name="device_state_unknown">Unknown</string>
|
||||
|
||||
<!-- Title for current download rate -->
|
||||
|
@ -620,7 +620,7 @@ Please report any problems you encounter via Github.</string>
|
|||
<!-- RunConditionMonitor -->
|
||||
|
||||
<!-- Explanations why syncthing is running or not running -->
|
||||
<string name="reason_not_on_ac_power">Phone is not running on AC power.</string>
|
||||
<string name="reason_not_charging">Phone is not charging.</string>
|
||||
<string name="reason_not_on_battery_power">Phone is not running on battery power.</string>
|
||||
<string name="reason_not_while_power_saving">Syncthing is not running as the phone is currently power saving.</string>
|
||||
<string name="reason_not_while_auto_sync_data_disabled">Syncthing is not running as Android currently has \'Auto-sync data\' disabled.</string>
|
||||
|
|
Loading…
Reference in a new issue