mirror of
https://github.com/ArcaneChat/android.git
synced 2026-07-03 14:05:24 +02:00
Merge pull request #1162 from deltachat/doze-device-message
move doze reminder to device-chat
This commit is contained in:
@@ -575,6 +575,7 @@
|
||||
<string name="perm_explain_access_to_location_denied">To attach a location, go to the app settings, select \"Permissions\", and enable \"Location\".</string>
|
||||
<string name="perm_enable_bg_reminder_title">Tap here to receive messages while Delta Chat is in the background.</string>
|
||||
<string name="perm_enable_bg_reminder_text">Delta Chat uses few resources and takes care not to drain your battery.</string>
|
||||
<string name="perm_enable_bg_already_done">You already allowed Delta Chat to receive messages in the background.\n\nIf messages still do not arrive in background, please also check your system settings.</string>
|
||||
|
||||
<!-- dc_str_* resources -->
|
||||
<string name="encrypted_message">Encrypted message</string>
|
||||
|
||||
@@ -3,7 +3,8 @@ package com.b44t.messenger;
|
||||
public class DcContact {
|
||||
|
||||
public final static int DC_CONTACT_ID_SELF = 1;
|
||||
public final static int DC_CONTACT_ID_DEVICE = 2;
|
||||
public final static int DC_CONTACT_ID_INFO = 2;
|
||||
public final static int DC_CONTACT_ID_DEVICE = 5;
|
||||
public final static int DC_CONTACT_ID_LAST_SPECIAL = 9;
|
||||
public final static int DC_CONTACT_ID_NEW_CONTACT = -1; // used by the UI, not valid to the core
|
||||
public final static int DC_CONTACT_ID_NEW_GROUP = -2; // - " -
|
||||
|
||||
@@ -59,6 +59,7 @@ import com.b44t.messenger.DcEventCenter;
|
||||
import com.b44t.messenger.DcMsg;
|
||||
|
||||
import org.thoughtcrime.securesms.ConversationAdapter.ItemClickListener;
|
||||
import org.thoughtcrime.securesms.components.reminder.DozeReminder;
|
||||
import org.thoughtcrime.securesms.connect.ApplicationDcContext;
|
||||
import org.thoughtcrime.securesms.connect.DcHelper;
|
||||
import org.thoughtcrime.securesms.database.Address;
|
||||
@@ -706,6 +707,9 @@ public class ConversationFragment extends Fragment
|
||||
else if(messageRecord.isSetupMessage()) {
|
||||
querySetupCode(messageRecord,null);
|
||||
}
|
||||
else if(DozeReminder.isDozeReminderMsg(getContext(), messageRecord)) {
|
||||
DozeReminder.dozeReminderTapped(getContext());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -184,19 +184,18 @@ public class ConversationListFragment extends Fragment
|
||||
@Override
|
||||
protected Optional<? extends Reminder> doInBackground(Context... params) {
|
||||
final Context context = params[0];
|
||||
// if (ExpiredBuildReminder.isEligible()) {
|
||||
// return Optional.of(new ExpiredBuildReminder(context));
|
||||
// } else if (OutdatedBuildReminder.isEligible()) {
|
||||
// return Optional.of(new OutdatedBuildReminder(context));
|
||||
// } else
|
||||
try {
|
||||
if (DozeReminder.isEligible(context)) {
|
||||
return Optional.of(new DozeReminder(context));
|
||||
DozeReminder.addDozeReminderDeviceMsg(context);
|
||||
return Optional.absent();
|
||||
} else if (OutdatedReminder.isEligible(context)) {
|
||||
return Optional.of(new OutdatedReminder(context));
|
||||
}
|
||||
else {
|
||||
return Optional.absent();
|
||||
}
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return Optional.absent();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -12,36 +12,20 @@ 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;
|
||||
|
||||
import com.b44t.messenger.DcContact;
|
||||
import com.b44t.messenger.DcContext;
|
||||
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 extends Reminder {
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.M)
|
||||
public DozeReminder(@NonNull final Context context) {
|
||||
super(context.getString(R.string.perm_enable_bg_reminder_title),
|
||||
context.getString(R.string.perm_enable_bg_reminder_text));
|
||||
|
||||
setOkListener(v -> {
|
||||
if(ContextCompat.checkSelfPermission(context, Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)==PackageManager.PERMISSION_GRANTED) {
|
||||
Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS,
|
||||
Uri.parse("package:" + context.getPackageName()));
|
||||
context.startActivity(intent);
|
||||
}
|
||||
else {
|
||||
Intent intent = new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
|
||||
context.startActivity(intent);
|
||||
}
|
||||
});
|
||||
|
||||
setDismissListener(v -> {
|
||||
Prefs. setPromptedOptimizeDoze(context, true);
|
||||
});
|
||||
}
|
||||
public class DozeReminder {
|
||||
|
||||
public static boolean isEligible(Context context) {
|
||||
if(context==null) {
|
||||
@@ -52,7 +36,7 @@ public class DozeReminder extends Reminder {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(Prefs.hasPromptedOptimizeDoze(context)) {
|
||||
if(Prefs.getPrompteDozeMsgId(context)!=0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -75,4 +59,46 @@ public class DozeReminder extends Reminder {
|
||||
|
||||
return true; // yip, asking for disabling battery optimisations makes sense
|
||||
}
|
||||
|
||||
public static void addDozeReminderDeviceMsg(Context context) {
|
||||
DcContext dcContext = DcHelper.getContext(context);
|
||||
DcMsg msg = new DcMsg(dcContext, DcMsg.DC_MSG_TEXT);
|
||||
msg.setText(context.getString(R.string.perm_enable_bg_reminder_title)+"\n\n"
|
||||
+context.getString(R.string.perm_enable_bg_reminder_text));
|
||||
int msgId = dcContext.addDeviceMsg("android.doze-reminder", msg);
|
||||
if(msgId!=0) {
|
||||
Prefs.setPromptedDozeMsgId(context, msgId);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isDozeReminderMsg(Context context, DcMsg msg) {
|
||||
return msg != null
|
||||
&& msg.getFromId() == DcContact.DC_CONTACT_ID_DEVICE
|
||||
&& msg.getId() == Prefs.getPrompteDozeMsgId(context);
|
||||
}
|
||||
|
||||
public static void dozeReminderTapped(Context context) {
|
||||
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
|
||||
return;
|
||||
}
|
||||
|
||||
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
|
||||
if(pm.isIgnoringBatteryOptimizations(context.getPackageName())) {
|
||||
new AlertDialog.Builder(context)
|
||||
.setMessage(R.string.perm_enable_bg_already_done)
|
||||
.setPositiveButton(android.R.string.ok, null)
|
||||
.show();
|
||||
return;
|
||||
}
|
||||
|
||||
if(ContextCompat.checkSelfPermission(context, Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)==PackageManager.PERMISSION_GRANTED) {
|
||||
Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS,
|
||||
Uri.parse("package:" + context.getPackageName()));
|
||||
context.startActivity(intent);
|
||||
}
|
||||
else {
|
||||
Intent intent = new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
|
||||
context.startActivity(intent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ public class Prefs {
|
||||
public static final String SCREEN_LOCK_TIMEOUT_PREF = "pref_timeout_passphrase";
|
||||
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_OPTIMIZE_DOZE_PREF = "pref_prompted_optimize_doze";
|
||||
private static final String PROMPTED_DOZE_MSG_ID_PREF = "pref_prompted_doze_msg_id";
|
||||
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";
|
||||
|
||||
@@ -160,12 +160,12 @@ public class Prefs {
|
||||
setStringPreference(context, LANGUAGE_PREF, language);
|
||||
}
|
||||
|
||||
public static void setPromptedOptimizeDoze(Context context, boolean value) {
|
||||
setBooleanPreference(context, PROMPTED_OPTIMIZE_DOZE_PREF, value);
|
||||
public static void setPromptedDozeMsgId(Context context, int msg_id) {
|
||||
setIntegerPrefrence(context, PROMPTED_DOZE_MSG_ID_PREF, msg_id);
|
||||
}
|
||||
|
||||
public static boolean hasPromptedOptimizeDoze(Context context) {
|
||||
return getBooleanPreference(context, PROMPTED_OPTIMIZE_DOZE_PREF, false);
|
||||
public static int getPrompteDozeMsgId(Context context) {
|
||||
return getIntegerPreference(context, PROMPTED_DOZE_MSG_ID_PREF, 0);
|
||||
}
|
||||
|
||||
public static boolean isNotificationsEnabled(Context context) {
|
||||
|
||||
Reference in New Issue
Block a user