From ff6ea4af09e1c64dc6f8c139f49cbfda18562610 Mon Sep 17 00:00:00 2001 From: M66B Date: Tue, 4 Sep 2018 18:06:22 +0000 Subject: [PATCH] Setup doze / data saver --- .idea/caches/build_file_checksums.ser | Bin 535 -> 535 bytes app/build.gradle | 2 +- .../java/eu/faircode/email/FragmentSetup.java | 87 +++++++++++++++--- app/src/main/res/layout/fragment_setup.xml | 80 ++++++++++------ app/src/main/res/values/strings.xml | 4 + 5 files changed, 134 insertions(+), 39 deletions(-) diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index ec827900667b06651b552d81400d0fc750e524d7..d63bfde85a21db8c78cf4cc5a218f367e8ff3a1a 100644 GIT binary patch delta 33 rcmV++0N($X1eXMmm;|MJzZ|igY5@_pPwd=f(fzuTwKibUq|{A#1FaCu delta 33 rcmV++0N($X1eXMmm;|KyEcmgUY5@_arO`<5;dzxjkT(%TE>5p_1?CSd diff --git a/app/build.gradle b/app/build.gradle index 3ce1c572..445bd7ba 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -25,7 +25,7 @@ android { proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } debug { - debuggable = false // Workaround profiler bug + debuggable = true minifyEnabled = true useProguard = true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' diff --git a/app/src/main/java/eu/faircode/email/FragmentSetup.java b/app/src/main/java/eu/faircode/email/FragmentSetup.java index 4c2590b7..ae389afa 100644 --- a/app/src/main/java/eu/faircode/email/FragmentSetup.java +++ b/app/src/main/java/eu/faircode/email/FragmentSetup.java @@ -20,18 +20,26 @@ package eu.faircode.email; */ import android.Manifest; +import android.annotation.TargetApi; import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; +import android.net.ConnectivityManager; +import android.net.Uri; +import android.os.Build; import android.os.Bundle; +import android.os.PowerManager; import android.preference.PreferenceManager; +import android.provider.Settings; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; -import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; @@ -39,22 +47,26 @@ import java.util.List; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.appcompat.app.AlertDialog; import androidx.core.content.ContextCompat; import androidx.fragment.app.FragmentTransaction; import androidx.lifecycle.Observer; public class FragmentSetup extends FragmentEx { private Button btnAccount; - private ProgressBar pbAccount; private TextView tvAccountDone; private Button btnIdentity; - private ProgressBar pbIdentity; private TextView tvIdentityDone; private Button btnPermissions; private TextView tvPermissionsDone; + private Button btnDoze; + private TextView tvDozeDone; + + private Button btnData; + private CheckBox cbDarkTheme; private Button btnOptions; @@ -72,16 +84,19 @@ public class FragmentSetup extends FragmentEx { // Get controls btnAccount = view.findViewById(R.id.btnAccount); - pbAccount = view.findViewById(R.id.pbAccount); tvAccountDone = view.findViewById(R.id.tvAccountDone); btnIdentity = view.findViewById(R.id.btnIdentity); - pbIdentity = view.findViewById(R.id.pbIdentity); tvIdentityDone = view.findViewById(R.id.tvIdentityDone); btnPermissions = view.findViewById(R.id.btnPermissions); tvPermissionsDone = view.findViewById(R.id.tvPermissionsDone); + btnDoze = view.findViewById(R.id.btnDoze); + tvDozeDone = view.findViewById(R.id.tvDozeDone); + + btnData = view.findViewById(R.id.btnData); + cbDarkTheme = view.findViewById(R.id.cbDarkTheme); btnOptions = view.findViewById(R.id.btnOptions); @@ -113,6 +128,39 @@ public class FragmentSetup extends FragmentEx { } }); + btnDoze.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + new AlertDialog.Builder(getContext()) + .setMessage(R.string.title_setup_doze_instructions) + .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + try { + startActivity(new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)); + } catch (Throwable ex) { + Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex)); + } + } + }) + .create() + .show(); + } + }); + + btnData.setOnClickListener(new View.OnClickListener() { + @Override + @TargetApi(Build.VERSION_CODES.N) + public void onClick(View v) { + try { + startActivity(new Intent(Settings.ACTION_IGNORE_BACKGROUND_DATA_RESTRICTIONS_SETTINGS, + Uri.parse("package:" + BuildConfig.APPLICATION_ID))); + } catch (Throwable ex) { + Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex)); + } + } + }); + final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); String theme = prefs.getString("theme", "light"); @@ -141,12 +189,13 @@ public class FragmentSetup extends FragmentEx { // Initialize - pbAccount.setVisibility(View.GONE); - pbIdentity.setVisibility(View.GONE); - tvAccountDone.setText(R.string.title_setup_to_do); + tvAccountDone.setText(null); btnIdentity.setEnabled(false); - tvIdentityDone.setText(R.string.title_setup_to_do); - tvPermissionsDone.setText(R.string.title_setup_to_do); + tvIdentityDone.setText(null); + tvPermissionsDone.setText(null); + btnDoze.setEnabled(false); + tvDozeDone.setText(null); + btnData.setVisibility(View.GONE); int[] grantResults = new int[permissions.length]; for (int i = 0; i < permissions.length; i++) @@ -198,8 +247,8 @@ public class FragmentSetup extends FragmentEx { db.account().liveAccounts(true).observe(getViewLifecycleOwner(), new Observer>() { @Override public void onChanged(@Nullable List accounts) { - tvAccountDone.setText(accounts != null && accounts.size() > 0 ? R.string.title_setup_done : R.string.title_setup_to_do); btnIdentity.setEnabled(accounts != null && accounts.size() > 0); + tvAccountDone.setText(accounts != null && accounts.size() > 0 ? R.string.title_setup_done : R.string.title_setup_to_do); } }); @@ -211,6 +260,22 @@ public class FragmentSetup extends FragmentEx { }); } + @Override + public void onResume() { + super.onResume(); + + PowerManager pm = getContext().getSystemService(PowerManager.class); + boolean ignoring = pm.isIgnoringBatteryOptimizations(BuildConfig.APPLICATION_ID); + btnDoze.setEnabled(!ignoring); + tvDozeDone.setText(ignoring ? R.string.title_setup_done : R.string.title_setup_to_do); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + ConnectivityManager cm = getContext().getSystemService(ConnectivityManager.class); + boolean saving = (cm.getRestrictBackgroundStatus() == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED); + btnData.setVisibility(saving ? View.VISIBLE : View.GONE); + } + } + @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { boolean has = (grantResults.length > 0); diff --git a/app/src/main/res/layout/fragment_setup.xml b/app/src/main/res/layout/fragment_setup.xml index fb375747..b6d2d3ad 100644 --- a/app/src/main/res/layout/fragment_setup.xml +++ b/app/src/main/res/layout/fragment_setup.xml @@ -23,17 +23,6 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> - - - - + + +