From 9074047eb0dd04f6fcc136d7ed571e31c8d77a06 Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 20 Mar 2022 09:47:12 +0100 Subject: [PATCH] Refactoring: schedule exact alarm --- .../java/eu/faircode/email/EntityAccount.java | 2 +- .../faircode/email/FragmentDialogStill.java | 16 ++--- .../eu/faircode/email/FragmentMessages.java | 11 +-- .../eu/faircode/email/FragmentOptions.java | 5 +- .../email/FragmentOptionsSynchronize.java | 3 +- .../java/eu/faircode/email/FragmentSetup.java | 38 +++------- .../main/java/eu/faircode/email/Helper.java | 18 ++--- .../eu/faircode/email/ServiceSynchronize.java | 3 - app/src/main/res/layout/dialog_setup.xml | 21 +----- app/src/main/res/layout/fragment_setup.xml | 72 ++++--------------- app/src/main/res/values/strings.xml | 1 - 11 files changed, 46 insertions(+), 144 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/EntityAccount.java b/app/src/main/java/eu/faircode/email/EntityAccount.java index b4ff399254..6f7458671d 100644 --- a/app/src/main/java/eu/faircode/email/EntityAccount.java +++ b/app/src/main/java/eu/faircode/email/EntityAccount.java @@ -188,7 +188,7 @@ public class EntityAccount extends EntityOrder implements Serializable { } boolean isExempted(Context context) { - return (!Helper.isOptimizing12(context) && this.poll_exempted); + return this.poll_exempted; } String getProtocol() { diff --git a/app/src/main/java/eu/faircode/email/FragmentDialogStill.java b/app/src/main/java/eu/faircode/email/FragmentDialogStill.java index 2bb561998f..9eb58518b4 100644 --- a/app/src/main/java/eu/faircode/email/FragmentDialogStill.java +++ b/app/src/main/java/eu/faircode/email/FragmentDialogStill.java @@ -47,8 +47,7 @@ public class FragmentDialogStill extends FragmentDialogBase { final Context context = getContext(); View dview = LayoutInflater.from(context).inflate(R.layout.dialog_setup, null); TextView tvDozeDevice = dview.findViewById(R.id.tvDozeDevice); - TextView tvDozeAndroid = dview.findViewById(R.id.tvDozeAndroid); - TextView tvInexact = dview.findViewById(R.id.tvInexact); + TextView tvDozeAndroid12 = dview.findViewById(R.id.tvDozeAndroid12); CheckBox cbNotAgain = dview.findViewById(R.id.cbNotAgain); Group grp2 = dview.findViewById(R.id.grp2); Group grp3 = dview.findViewById(R.id.grp3); @@ -70,17 +69,14 @@ public class FragmentDialogStill extends FragmentDialogBase { }); boolean hasPermissions = Helper.hasPermission(context, Manifest.permission.READ_CONTACTS); - Boolean isIgnoring = Helper.isIgnoringOptimizations(context); - boolean isKilling = Helper.isKilling() && !(isIgnoring == null || isIgnoring); - boolean isOptimizing = Helper.isOptimizing12(context); - boolean canSchedule = AlarmManagerCompatEx.canScheduleExactAlarms(context); + boolean isIgnoring = !Boolean.FALSE.equals(Helper.isIgnoringOptimizations(context)); + boolean canScheduleExact = AlarmManagerCompatEx.canScheduleExactAlarms(getContext()); - tvDozeDevice.setVisibility(isKilling && !isOptimizing ? View.VISIBLE : View.GONE); - tvDozeAndroid.setVisibility(isOptimizing ? View.VISIBLE : View.GONE); - tvInexact.setVisibility(canSchedule ? View.GONE : View.VISIBLE); + tvDozeDevice.setVisibility(Helper.isKilling() && !isIgnoring ? View.VISIBLE : View.GONE); + tvDozeAndroid12.setVisibility(!canScheduleExact && !isIgnoring ? View.VISIBLE : View.GONE); grp2.setVisibility(hasPermissions ? View.GONE : View.VISIBLE); - grp3.setVisibility(isIgnoring == null || isIgnoring ? View.GONE : View.VISIBLE); + grp3.setVisibility(isIgnoring ? View.GONE : View.VISIBLE); AlertDialog.Builder builder = new AlertDialog.Builder(context) .setView(dview) diff --git a/app/src/main/java/eu/faircode/email/FragmentMessages.java b/app/src/main/java/eu/faircode/email/FragmentMessages.java index 445a9cfaa6..08d631d97d 100644 --- a/app/src/main/java/eu/faircode/email/FragmentMessages.java +++ b/app/src/main/java/eu/faircode/email/FragmentMessages.java @@ -4461,14 +4461,15 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. if (!setup_reminder) return false; - boolean isOptimizing = Helper.isOptimizing12(context); - boolean canSchedule = AlarmManagerCompatEx.canScheduleExactAlarms(context); - - if (!isOptimizing && canSchedule) + if (!Helper.isAndroid12()) + return false; + if (!Boolean.FALSE.equals(Helper.isIgnoringOptimizations(context))) + return false; + if (AlarmManagerCompatEx.canScheduleExactAlarms(context)) return false; final Snackbar snackbar = Snackbar.make(view, - canSchedule ? R.string.title_setup_doze_12 : R.string.title_setup_alarm_12, + R.string.title_setup_alarm_12, Snackbar.LENGTH_INDEFINITE) .setGestureInsetBottomIgnored(true); snackbar.setAction(R.string.title_fix, new View.OnClickListener() { diff --git a/app/src/main/java/eu/faircode/email/FragmentOptions.java b/app/src/main/java/eu/faircode/email/FragmentOptions.java index 3030bff0de..5478082892 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptions.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptions.java @@ -484,10 +484,9 @@ public class FragmentOptions extends FragmentBase { boolean setup_reminder = prefs.getBoolean("setup_reminder", true); boolean hasPermissions = hasPermission(Manifest.permission.READ_CONTACTS); - Boolean isIgnoring = Helper.isIgnoringOptimizations(getContext()); + boolean isIgnoring = !Boolean.FALSE.equals(Helper.isIgnoringOptimizations(getContext())); - if (!setup_reminder || - (hasPermissions && (isIgnoring == null || isIgnoring))) + if (!setup_reminder || (hasPermissions && isIgnoring)) super.finish(); else { FragmentDialogStill fragment = new FragmentDialogStill(); diff --git a/app/src/main/java/eu/faircode/email/FragmentOptionsSynchronize.java b/app/src/main/java/eu/faircode/email/FragmentOptionsSynchronize.java index 7db5fec1c6..ea4b24f71d 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptionsSynchronize.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptionsSynchronize.java @@ -634,8 +634,7 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr } private void bindTo(EntityAccount account) { - cbExempted.setEnabled(!Helper.isOptimizing12(context) && - !account.ondemand && account.protocol == EntityAccount.TYPE_IMAP); + cbExempted.setEnabled(!account.ondemand && account.protocol == EntityAccount.TYPE_IMAP); cbExempted.setChecked(account.poll_exempted); cbExempted.setText(account.name); } diff --git a/app/src/main/java/eu/faircode/email/FragmentSetup.java b/app/src/main/java/eu/faircode/email/FragmentSetup.java index 4f0866f751..4e105f57b3 100644 --- a/app/src/main/java/eu/faircode/email/FragmentSetup.java +++ b/app/src/main/java/eu/faircode/email/FragmentSetup.java @@ -103,7 +103,6 @@ public class FragmentSetup extends FragmentBase { private TextView tvDoze12; private ImageButton ibDoze; - private Button btnInexactAlarms; private Button btnBackgroundRestricted; private Button btnDataSaver; private TextView tvStamina; @@ -116,7 +115,6 @@ public class FragmentSetup extends FragmentBase { private Button btnMore; private Button btnSupport; - private Group grpInexactAlarms; private Group grpBackgroundRestricted; private Group grpDataSaver; private Group grpSupport; @@ -175,7 +173,6 @@ public class FragmentSetup extends FragmentBase { tvDoze12 = view.findViewById(R.id.tvDoze12); ibDoze = view.findViewById(R.id.ibDoze); - btnInexactAlarms = view.findViewById(R.id.btnInexactAlarms); btnBackgroundRestricted = view.findViewById(R.id.btnBackgroundRestricted); btnDataSaver = view.findViewById(R.id.btnDataSaver); tvStamina = view.findViewById(R.id.tvStamina); @@ -188,7 +185,6 @@ public class FragmentSetup extends FragmentBase { btnMore = view.findViewById(R.id.btnMore); btnSupport = view.findViewById(R.id.btnSupport); - grpInexactAlarms = view.findViewById(R.id.grpInexactAlarms); grpBackgroundRestricted = view.findViewById(R.id.grpBackgroundRestricted); grpDataSaver = view.findViewById(R.id.grpDataSaver); grpSupport = view.findViewById(R.id.grpSupport); @@ -534,20 +530,6 @@ public class FragmentSetup extends FragmentBase { PackageManager pm = getContext().getPackageManager(); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { - final Intent settings = new Intent( - Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM, - Uri.parse("package:" + BuildConfig.APPLICATION_ID)); - - btnInexactAlarms.setEnabled(settings.resolveActivity(pm) != null); // system whitelisted - btnInexactAlarms.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - startActivity(settings); - } - }); - } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { final Intent settings = new Intent( Settings.ACTION_APPLICATION_DETAILS_SETTINGS, @@ -639,7 +621,6 @@ public class FragmentSetup extends FragmentBase { btnInbox.setEnabled(false); - grpInexactAlarms.setVisibility(View.GONE); grpBackgroundRestricted.setVisibility(View.GONE); grpDataSaver.setVisibility(View.GONE); tvStamina.setVisibility(View.GONE); @@ -745,7 +726,8 @@ public class FragmentSetup extends FragmentBase { } // Doze - Boolean ignoring = Helper.isIgnoringOptimizations(getContext()); + boolean isIgnoring = !Boolean.FALSE.equals(Helper.isIgnoringOptimizations(getContext())); + boolean canScheduleExact = AlarmManagerCompatEx.canScheduleExactAlarms(getContext()); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) btnDoze.setEnabled(false); @@ -755,19 +737,15 @@ public class FragmentSetup extends FragmentBase { if (intent.resolveActivity(pm) == null) btnDoze.setEnabled(false); else - btnDoze.setEnabled((ignoring != null && !ignoring) || BuildConfig.DEBUG); + btnDoze.setEnabled(!isIgnoring || BuildConfig.DEBUG); } - boolean done = (ignoring == null || ignoring || Helper.isArc()); - tvDozeDone.setText(done ? R.string.title_setup_done : R.string.title_setup_to_do); - tvDozeDone.setTextColor(done ? textColorPrimary : colorWarning); - tvDozeDone.setTypeface(null, done ? Typeface.NORMAL : Typeface.BOLD); - tvDozeDone.setCompoundDrawablesWithIntrinsicBounds(done ? check : null, null, null, null); - tvDoze12.setVisibility(Helper.isOptimizing12(getContext()) ? View.VISIBLE : View.GONE); + tvDozeDone.setText(isIgnoring ? R.string.title_setup_done : R.string.title_setup_to_do); + tvDozeDone.setTextColor(isIgnoring ? textColorPrimary : colorWarning); + tvDozeDone.setTypeface(null, isIgnoring ? Typeface.NORMAL : Typeface.BOLD); + tvDozeDone.setCompoundDrawablesWithIntrinsicBounds(isIgnoring ? check : null, null, null, null); - grpInexactAlarms.setVisibility( - !AlarmManagerCompatEx.canScheduleExactAlarms(getContext()) - ? View.VISIBLE : View.GONE); + tvDoze12.setVisibility(!canScheduleExact && !isIgnoring ? View.VISIBLE : View.GONE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { ActivityManager am = diff --git a/app/src/main/java/eu/faircode/email/Helper.java b/app/src/main/java/eu/faircode/email/Helper.java index a294175ce0..8d6a128e30 100644 --- a/app/src/main/java/eu/faircode/email/Helper.java +++ b/app/src/main/java/eu/faircode/email/Helper.java @@ -450,6 +450,9 @@ public class Helper { } static Boolean isIgnoringOptimizations(Context context) { + if (isArc()) + return true; + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return null; @@ -460,14 +463,6 @@ public class Helper { return pm.isIgnoringBatteryOptimizations(BuildConfig.APPLICATION_ID); } - static boolean isOptimizing12(Context context) { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S || true) - return false; - - Boolean ignoring = Helper.isIgnoringOptimizations(context); - return (ignoring != null && !ignoring); - } - static Integer getBatteryLevel(Context context) { try { BatteryManager bm = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE); @@ -1112,12 +1107,11 @@ public class Helper { // Vivo isRealme() || isBlackview() || - isSony() || - BuildConfig.DEBUG); + isSony()); } - static boolean isDozeRequired() { - return (Build.VERSION.SDK_INT > Build.VERSION_CODES.R && false); + static boolean isAndroid12() { + return (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S); } static String getUiModeType(Context context) { diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index bdff80d1d2..f211c9c3f6 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -2879,9 +2879,6 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences } static int getPollInterval(Context context) { - if (Helper.isOptimizing12(context)) - return (BuildConfig.DEBUG ? 2 : 15); - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); return prefs.getInt("poll_interval", 0); // minutes } diff --git a/app/src/main/res/layout/dialog_setup.xml b/app/src/main/res/layout/dialog_setup.xml index 1c7d520ee7..79b0053425 100644 --- a/app/src/main/res/layout/dialog_setup.xml +++ b/app/src/main/res/layout/dialog_setup.xml @@ -105,22 +105,7 @@ app:layout_constraintTop_toBottomOf="@id/tvDoze" /> - - + app:layout_constraintTop_toBottomOf="@id/tvDozeDevice" /> + app:layout_constraintTop_toBottomOf="@id/tvDozeAndroid12" /> - - - - - - + app:layout_constraintTop_toBottomOf="@id/btnDoze" /> + app:layout_constraintTop_toBottomOf="@id/tvDoze12" /> -