Already ask the user to disable doze on the first start

This commit is contained in:
Hocuri
2020-07-29 11:06:24 +02:00
committed by B. Petersen
parent 7d0ac9f501
commit 954ca9b2d1
2 changed files with 23 additions and 3 deletions
@@ -160,6 +160,7 @@ public class ConversationListFragment extends Fragment
super.onResume();
updateReminders();
DozeReminder.maybeAskDirectly(getActivity());
list.getAdapter().notifyDataSetChanged();
}
@@ -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,11 +20,11 @@ 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 {
private static final String ALREADY_ASKED_DIRECTLY = "DOZE_ALREADY_ASKED_DIRECTLY";
public static boolean isEligible(Context context) {
if(context==null) {
@@ -40,6 +39,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, DozeReminder.ALREADY_ASKED_DIRECTLY, false)) {
return false;
}
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
if(pm.isIgnoringBatteryOptimizations(context.getPackageName())) {
return false;
@@ -101,4 +105,19 @@ public class DozeReminder {
context.startActivity(intent);
}
}
public static void maybeAskDirectly(Context context) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M
&& !Prefs.getBooleanPreference(context, DozeReminder.ALREADY_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.setBooleanPreference(context, DozeReminder.ALREADY_ASKED_DIRECTLY, true);
// ALREADY_ASKED_DIRECTLY is also used above in isEligible(). As long as ALREADY_ASKED_DIRECTLY is false, isEligible() will return false
// and no device message will be added.
}
}