Switch to polling if battery optimizations enabled on Android 12+

pull/206/head
M66B 3 years ago
parent 957301bf43
commit 54daa402e4

@ -170,7 +170,16 @@ public class EntityAccount extends EntityOrder implements Serializable {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean enabled = prefs.getBoolean("enabled", true); boolean enabled = prefs.getBoolean("enabled", true);
int pollInterval = ServiceSynchronize.getPollInterval(context); int pollInterval = ServiceSynchronize.getPollInterval(context);
return (!enabled || this.ondemand || (pollInterval > 0 && !this.poll_exempted)); return (!enabled || this.ondemand || (pollInterval > 0 && !isExempted(context)));
}
boolean isExempted(Context context) {
if (Helper.isTarget(context, Build.VERSION_CODES.R)) {
Boolean ignoring = Helper.isIgnoringOptimizations(context);
if (ignoring != null && !ignoring)
return false;
}
return this.poll_exempted;
} }
String getProtocol() { String getProtocol() {

@ -23,6 +23,7 @@ import android.app.Dialog;
import android.app.TimePickerDialog; import android.app.TimePickerDialog;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.text.format.DateFormat; import android.text.format.DateFormat;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -449,7 +450,7 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr
swEnabled.setChecked(prefs.getBoolean("enabled", true)); swEnabled.setChecked(prefs.getBoolean("enabled", true));
swOptimize.setChecked(prefs.getBoolean("auto_optimize", false)); swOptimize.setChecked(prefs.getBoolean("auto_optimize", false));
int pollInterval = ServiceSynchronize.getPollInterval(getContext()); int pollInterval = prefs.getInt("poll_interval", 0);
int[] pollIntervalValues = getResources().getIntArray(R.array.pollIntervalValues); int[] pollIntervalValues = getResources().getIntArray(R.array.pollIntervalValues);
for (int pos = 0; pos < pollIntervalValues.length; pos++) for (int pos = 0; pos < pollIntervalValues.length; pos++)
if (pollIntervalValues[pos] == pollInterval) { if (pollIntervalValues[pos] == pollInterval) {

@ -34,6 +34,7 @@ import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo; import android.content.pm.ResolveInfo;
@ -432,6 +433,22 @@ public class Helper {
return pm.isIgnoringBatteryOptimizations(BuildConfig.APPLICATION_ID); return pm.isIgnoringBatteryOptimizations(BuildConfig.APPLICATION_ID);
} }
private static Integer targetSdk = null;
static boolean isTarget(Context context, int sdk) {
if (targetSdk == null)
try {
PackageManager pm = context.getPackageManager();
ApplicationInfo ai = pm.getApplicationInfo(BuildConfig.APPLICATION_ID, 0);
targetSdk = ai.targetSdkVersion;
} catch (Throwable ex) {
Log.e(ex);
targetSdk = Build.VERSION.SDK_INT;
}
return (targetSdk >= sdk);
}
static Integer getBatteryLevel(Context context) { static Integer getBatteryLevel(Context context) {
try { try {
BatteryManager bm = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE); BatteryManager bm = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);

@ -2145,7 +2145,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
int pollInterval = getPollInterval(this); int pollInterval = getPollInterval(this);
long now = new Date().getTime(); long now = new Date().getTime();
long delayed = now - account.last_connected - account.poll_interval * 60 * 1000L; long delayed = now - account.last_connected - account.poll_interval * 60 * 1000L;
long maxDelayed = (pollInterval > 0 && !account.poll_exempted long maxDelayed = (pollInterval > 0 && !account.isExempted(this)
? pollInterval * ACCOUNT_ERROR_AFTER_POLL : ACCOUNT_ERROR_AFTER) * 60 * 1000L; ? pollInterval * ACCOUNT_ERROR_AFTER_POLL : ACCOUNT_ERROR_AFTER) * 60 * 1000L;
if (delayed > maxDelayed && if (delayed > maxDelayed &&
state.getBackoff() >= CONNECT_BACKOFF_ALARM_START * 60) { state.getBackoff() >= CONNECT_BACKOFF_ALARM_START * 60) {
@ -2408,7 +2408,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
db.endTransaction(); db.endTransaction();
} }
prefs.edit().putInt("poll_interval", OPTIMIZE_POLL_INTERVAL).apply(); prefs.edit().putInt("poll_interval", OPTIMIZE_POLL_INTERVAL).apply();
} else if (pollInterval <= 60 && account.poll_exempted) { } else if (pollInterval <= 60 && account.isExempted(this)) {
db.account().setAccountPollExempted(account.id, false); db.account().setAccountPollExempted(account.id, false);
eval(this, "Optimize=" + reason); eval(this, "Optimize=" + reason);
} }
@ -2609,7 +2609,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
List<TupleAccountNetworkState> result = new ArrayList<>(); List<TupleAccountNetworkState> result = new ArrayList<>();
for (TupleAccountState accountState : accountStates) for (TupleAccountState accountState : accountStates)
result.add(new TupleAccountNetworkState( result.add(new TupleAccountNetworkState(
enabled && (pollInterval == 0 || accountState.poll_exempted) && scheduled, enabled && (pollInterval == 0 || accountState.isExempted(ServiceSynchronize.this)) && scheduled,
command, command,
networkState, networkState,
accountState)); accountState));
@ -2721,14 +2721,14 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
} }
static int getPollInterval(Context context) { static int getPollInterval(Context context) {
if (Helper.isTarget(context, Build.VERSION_CODES.R)) {
Boolean ignoring = Helper.isIgnoringOptimizations(context);
if (ignoring != null && !ignoring)
return 15;
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
int poll_interval = prefs.getInt("poll_interval", 0); // minutes return prefs.getInt("poll_interval", 0); // minutes
//if (Build.VERSION.SDK_INT > Build.VERSION_CODES.R) {
// Boolean ignoring = Helper.isIgnoringOptimizations(context);
// if (ignoring != null && !ignoring)
// poll_interval = 15;
//}
return poll_interval;
} }
static long[] getSchedule(Context context) { static long[] getSchedule(Context context) {

Loading…
Cancel
Save