Browse Source

Setup doze / data saver

main
M66B 6 years ago
parent
commit
ff6ea4af09
5 changed files with 134 additions and 39 deletions
  1. BIN
      .idea/caches/build_file_checksums.ser
  2. +1
    -1
      app/build.gradle
  3. +76
    -11
      app/src/main/java/eu/faircode/email/FragmentSetup.java
  4. +53
    -27
      app/src/main/res/layout/fragment_setup.xml
  5. +4
    -0
      app/src/main/res/values/strings.xml

BIN
.idea/caches/build_file_checksums.ser View File


+ 1
- 1
app/build.gradle View File

@ -25,7 +25,7 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
} }
debug { debug {
debuggable = false // Workaround profiler bug
debuggable = true
minifyEnabled = true minifyEnabled = true
useProguard = true useProguard = true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'


+ 76
- 11
app/src/main/java/eu/faircode/email/FragmentSetup.java View File

@ -20,18 +20,26 @@ package eu.faircode.email;
*/ */
import android.Manifest; import android.Manifest;
import android.annotation.TargetApi;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.PowerManager;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.provider.Settings;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Button; import android.widget.Button;
import android.widget.CheckBox; import android.widget.CheckBox;
import android.widget.CompoundButton; import android.widget.CompoundButton;
import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
@ -39,22 +47,26 @@ import java.util.List;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentTransaction; import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Observer; import androidx.lifecycle.Observer;
public class FragmentSetup extends FragmentEx { public class FragmentSetup extends FragmentEx {
private Button btnAccount; private Button btnAccount;
private ProgressBar pbAccount;
private TextView tvAccountDone; private TextView tvAccountDone;
private Button btnIdentity; private Button btnIdentity;
private ProgressBar pbIdentity;
private TextView tvIdentityDone; private TextView tvIdentityDone;
private Button btnPermissions; private Button btnPermissions;
private TextView tvPermissionsDone; private TextView tvPermissionsDone;
private Button btnDoze;
private TextView tvDozeDone;
private Button btnData;
private CheckBox cbDarkTheme; private CheckBox cbDarkTheme;
private Button btnOptions; private Button btnOptions;
@ -72,16 +84,19 @@ public class FragmentSetup extends FragmentEx {
// Get controls // Get controls
btnAccount = view.findViewById(R.id.btnAccount); btnAccount = view.findViewById(R.id.btnAccount);
pbAccount = view.findViewById(R.id.pbAccount);
tvAccountDone = view.findViewById(R.id.tvAccountDone); tvAccountDone = view.findViewById(R.id.tvAccountDone);
btnIdentity = view.findViewById(R.id.btnIdentity); btnIdentity = view.findViewById(R.id.btnIdentity);
pbIdentity = view.findViewById(R.id.pbIdentity);
tvIdentityDone = view.findViewById(R.id.tvIdentityDone); tvIdentityDone = view.findViewById(R.id.tvIdentityDone);
btnPermissions = view.findViewById(R.id.btnPermissions); btnPermissions = view.findViewById(R.id.btnPermissions);
tvPermissionsDone = view.findViewById(R.id.tvPermissionsDone); 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); cbDarkTheme = view.findViewById(R.id.cbDarkTheme);
btnOptions = view.findViewById(R.id.btnOptions); 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()); final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
String theme = prefs.getString("theme", "light"); String theme = prefs.getString("theme", "light");
@ -141,12 +189,13 @@ public class FragmentSetup extends FragmentEx {
// Initialize // Initialize
pbAccount.setVisibility(View.GONE);
pbIdentity.setVisibility(View.GONE);
tvAccountDone.setText(R.string.title_setup_to_do);
tvAccountDone.setText(null);
btnIdentity.setEnabled(false); 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]; int[] grantResults = new int[permissions.length];
for (int i = 0; i < permissions.length; i++) 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<List<EntityAccount>>() { db.account().liveAccounts(true).observe(getViewLifecycleOwner(), new Observer<List<EntityAccount>>() {
@Override @Override
public void onChanged(@Nullable List<EntityAccount> accounts) { public void onChanged(@Nullable List<EntityAccount> 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); 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 @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
boolean has = (grantResults.length > 0); boolean has = (grantResults.length > 0);


+ 53
- 27
app/src/main/res/layout/fragment_setup.xml View File

@ -23,17 +23,6 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="@+id/pbAccount"
style="@style/Base.Widget.AppCompat.ProgressBar"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginStart="12dp"
android:indeterminate="true"
app:layout_constraintBottom_toBottomOf="@id/btnAccount"
app:layout_constraintStart_toEndOf="@id/btnAccount"
app:layout_constraintTop_toTopOf="@id/btnAccount" />
<TextView <TextView
android:id="@+id/tvAccount" android:id="@+id/tvAccount"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -61,23 +50,12 @@
android:id="@+id/btnIdentity" android:id="@+id/btnIdentity"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginTop="18dp"
android:text="@string/title_setup_identity" android:text="@string/title_setup_identity"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvAccountDone" /> app:layout_constraintTop_toBottomOf="@id/tvAccountDone" />
<ProgressBar
android:id="@+id/pbIdentity"
style="@style/Base.Widget.AppCompat.ProgressBar"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginStart="12dp"
android:indeterminate="true"
app:layout_constraintBottom_toBottomOf="@id/btnIdentity"
app:layout_constraintStart_toEndOf="@id/btnIdentity"
app:layout_constraintTop_toTopOf="@id/btnIdentity" />
<TextView <TextView
android:id="@+id/tvIdentity" android:id="@+id/tvIdentity"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -105,7 +83,7 @@
android:id="@+id/btnPermissions" android:id="@+id/btnPermissions"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginTop="18dp"
android:text="@string/title_setup_permissions" android:text="@string/title_setup_permissions"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
@ -132,22 +110,70 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvPermissions" /> app:layout_constraintTop_toBottomOf="@id/tvPermissions" />
<!-- doze -->
<Button
android:id="@+id/btnDoze"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="@string/title_setup_doze"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvPermissionsDone" />
<TextView
android:id="@+id/tvDoze"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title_setup_doze_remark"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/btnDoze" />
<TextView
android:id="@+id/tvDozeDone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title_setup_to_do"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="?android:attr/textColorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvDoze" />
<!-- data saver -->
<Button
android:id="@+id/btnData"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:text="@string/title_setup_data"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvDozeDone" />
<CheckBox <CheckBox
android:id="@+id/cbDarkTheme" android:id="@+id/cbDarkTheme"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginTop="18dp"
android:text="@string/title_setup_dark_theme" android:text="@string/title_setup_dark_theme"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvPermissionsDone" />
app:layout_constraintTop_toBottomOf="@id/btnData" />
<Button <Button
android:id="@+id/btnOptions" android:id="@+id/btnOptions"
style="?android:attr/buttonStyleSmall" style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginTop="18dp"
android:minHeight="0dp" android:minHeight="0dp"
android:minWidth="0dp" android:minWidth="0dp"
android:text="@string/title_advanced" android:text="@string/title_advanced"


+ 4
- 0
app/src/main/res/values/strings.xml View File

@ -59,6 +59,10 @@
<string name="title_setup_account_remark">To receive email</string> <string name="title_setup_account_remark">To receive email</string>
<string name="title_setup_identity">Manage identities</string> <string name="title_setup_identity">Manage identities</string>
<string name="title_setup_identity_remark">To send email</string> <string name="title_setup_identity_remark">To send email</string>
<string name="title_setup_doze">Disable battery optimizations</string>
<string name="title_setup_doze_remark">To continuously receive email (optional)</string>
<string name="title_setup_doze_instructions">In the next dialog, select \"All apps\" at the top, select this app and select and confirm \"Don\'t optimize\"</string>
<string name="title_setup_data">Disable data saving</string>
<string name="title_setup_permissions">Grant permissions</string> <string name="title_setup_permissions">Grant permissions</string>
<string name="title_setup_permissions_remark">To autocomplete addresses (optional)</string> <string name="title_setup_permissions_remark">To autocomplete addresses (optional)</string>
<string name="title_setup_to_do">To do</string> <string name="title_setup_to_do">To do</string>


Loading…
Cancel
Save