Merge pull request #1505 from deltachat/betterdoze

Already ask the user to disable doze on the first start.
This commit is contained in:
bjoern
2020-09-06 22:36:29 +02:00
committed by GitHub
3 changed files with 28 additions and 4 deletions
@@ -238,6 +238,7 @@ public class ConversationListFragment extends Fragment
@Override
protected void onPostExecute(Optional<? extends Reminder> reminder) {
DozeReminder.maybeAskDirectly(getActivity());
if (reminder.isPresent() && getActivity() != null && !isRemoving()) {
reminderView.showReminder(reminder.get());
} else if (!reminder.isPresent()) {
@@ -10,8 +10,7 @@ import android.net.Uri;
import android.os.Build;
import android.os.PowerManager;
import android.provider.Settings;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;
@@ -21,12 +20,10 @@ import com.b44t.messenger.DcMsg;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.connect.DcHelper;
import org.thoughtcrime.securesms.util.IntentUtils;
import org.thoughtcrime.securesms.util.Prefs;
@SuppressLint("BatteryLife")
public class DozeReminder {
public static boolean isEligible(Context context) {
if(context==null) {
return false;
@@ -40,6 +37,11 @@ public class DozeReminder {
return false;
}
// If we did never ask directly, we do not want to add a device message yet. First we want to try to ask directly.
if (!Prefs.getBooleanPreference(context, Prefs.DOZE_ASKED_DIRECTLY, false)) {
return false;
}
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
if(pm.isIgnoringBatteryOptimizations(context.getPackageName())) {
return false;
@@ -101,4 +103,24 @@ public class DozeReminder {
context.startActivity(intent);
}
}
public static void maybeAskDirectly(Context context) {
try {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M
&& !Prefs.getBooleanPreference(context, Prefs.DOZE_ASKED_DIRECTLY, false)
&& ContextCompat.checkSelfPermission(context, Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) == PackageManager.PERMISSION_GRANTED
&& !((PowerManager) context.getSystemService(Context.POWER_SERVICE)).isIgnoringBatteryOptimizations(context.getPackageName())) {
Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS,
Uri.parse("package:" + context.getPackageName()));
context.startActivity(intent);
}
// Prefs.DOZE_ASKED_DIRECTLY is also used above in isEligible().
// As long as Prefs.DOZE_ASKED_DIRECTLY is false, isEligible() will return false
// and no device message will be added.
Prefs.setBooleanPreference(context, Prefs.DOZE_ASKED_DIRECTLY, true);
} catch(Exception e) {
e.printStackTrace();
}
}
}
@@ -49,6 +49,7 @@ public class Prefs {
public static final String SCREEN_SECURITY_PREF = "pref_screen_security";
private static final String ENTER_SENDS_PREF = "pref_enter_sends";
private static final String PROMPTED_DOZE_MSG_ID_PREF = "pref_prompted_doze_msg_id";
public static final String DOZE_ASKED_DIRECTLY = "pref_doze_asked_directly";
private static final String IN_THREAD_NOTIFICATION_PREF = "pref_key_inthread_notifications";
public static final String MESSAGE_BODY_TEXT_SIZE_PREF = "pref_message_body_text_size";
public static final String EPHEMERAL_MESSAGES_PREF = "pref_ephemeral_messages";