Set up alarm to run every day and delete old messages

This commit is contained in:
Moez Bhatti
2016-06-27 23:17:06 -04:00
parent 32b79c4cbf
commit ace3962f21
11 changed files with 121 additions and 36 deletions
@@ -36,6 +36,7 @@ import com.android.volley.toolbox.Volley;
import com.moez.QKSMS.common.AnalyticsManager;
import com.moez.QKSMS.common.LifecycleHandler;
import com.moez.QKSMS.common.LiveViewManager;
import com.moez.QKSMS.common.QKPreferences;
import com.moez.QKSMS.common.google.DraftCache;
import com.moez.QKSMS.common.google.PduLoaderManager;
import com.moez.QKSMS.common.google.ThumbnailManager;
@@ -102,6 +103,7 @@ public class QKSMSAppBase extends MultiDexApplication {
LayoutManager.init(this);
NotificationManager.init(this);
LiveViewManager.init(this);
QKPreferences.init(this);
activePendingMessages();
}
@@ -0,0 +1,47 @@
package com.moez.QKSMS.common;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import com.moez.QKSMS.enums.QKPreference;
public abstract class QKPreferences {
private static SharedPreferences sPrefs;
public static void init(Context context) {
sPrefs = PreferenceManager.getDefaultSharedPreferences(context);
}
public static boolean getBoolean(QKPreference preference) {
return sPrefs.getBoolean(preference.getKey(), (boolean) preference.getDefaultValue());
}
public static void setBoolean(QKPreference preference, boolean newValue) {
sPrefs.edit().putBoolean(preference.getKey(), newValue).apply();
}
public static int getInt(QKPreference preference) {
return sPrefs.getInt(preference.getKey(), (int) preference.getDefaultValue());
}
public static void setInt(QKPreference preference, int newValue) {
sPrefs.edit().putInt(preference.getKey(), newValue).apply();
}
public static long getLong(QKPreference preference) {
return sPrefs.getLong(preference.getKey(), (int) preference.getDefaultValue());
}
public static void setLong(QKPreference preference, long newValue) {
sPrefs.edit().putLong(preference.getKey(), newValue).apply();
}
public static String getString(QKPreference preference) {
return sPrefs.getString(preference.getKey(), (String) preference.getDefaultValue());
}
public static void setString(QKPreference preference, String newValue) {
sPrefs.edit().putString(preference.getKey(), newValue).apply();
}
}
@@ -43,9 +43,9 @@ public enum QKPreference {
DELIVERY_TOAST("pref_key_delivery_toast", true),
DELIVERY_VIBRATE("pref_key_delivery_vibrate", true),
DELETE_OLD_MESSAGES("pref_key_delete_old_messages", false),
DELETE_UNREAD_MESSAGES("pref_key_delete_old_unread_messages", "7"), // This type of preference only accepts strings
DELETE_READ_MESSAGES("pref_key_delete_old_read_messages", "7"),
AUTO_DELETE("pref_key_delete_old_messages", false),
AUTO_DELETE_UNREAD("pref_key_delete_old_unread_messages", "7"), // This type of preference only accepts strings
AUTO_DELETE_READ("pref_key_delete_old_read_messages", "7"),
AUTO_EMOJI("pref_key_auto_emoji", false),
TEXT_FORMATTING("pref_key_markdown_enabled", false),
@@ -100,7 +100,8 @@ public enum QKPreference {
CONVERSATION_THEME("conversation_theme"),
// Storage
COMPOSE_DRAFT("compose_draft", "");
COMPOSE_DRAFT("compose_draft", ""),
LAST_AUTO_DELETE_CHECK("last_auto_delete_check", 0);
private String mKey;
private Object mDefaultValue;
@@ -0,0 +1,50 @@
package com.moez.QKSMS.receiver;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.moez.QKSMS.common.QKPreferences;
import com.moez.QKSMS.enums.QKPreference;
import java.util.Calendar;
import static android.content.Context.ALARM_SERVICE;
public class AutoDeleteReceiver extends BroadcastReceiver {
private static final String TAG = "AutoDeleteService";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "onReceive");
Calendar last = Calendar.getInstance();
last.setTimeInMillis(QKPreferences.getLong(QKPreference.LAST_AUTO_DELETE_CHECK));
Calendar current = Calendar.getInstance();
// Continue if the auto delete setting is enabled, and we haven't done a purge today
if (QKPreferences.getBoolean(QKPreference.AUTO_DELETE) && (last.getTimeInMillis() == 0 ||
current.get(Calendar.DAY_OF_YEAR) != last.get(Calendar.DAY_OF_YEAR) ||
current.get(Calendar.YEAR) != last.get(Calendar.YEAR))) {
Log.i(TAG, "Ready to delete old messages");
QKPreferences.setLong(QKPreference.LAST_AUTO_DELETE_CHECK, System.currentTimeMillis());
} else {
Log.i(TAG, "Not going to delete old messages");
}
}
public static void setupAutoDeleteAlarm(Context context) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 3); // We want this service to run when the phone is not likely being used
Intent intent = new Intent(context, AutoDeleteReceiver.class);
PendingIntent pIntent = PendingIntent.getBroadcast(context, 9237, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager) context.getSystemService(ALARM_SERVICE);
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pIntent);
}
}
@@ -15,5 +15,7 @@ public class BootReceiver extends BroadcastReceiver {
NotificationManager.create(context);
SettingsFragment.updateAlarmManager(context, PreferenceManager.getDefaultSharedPreferences(context).getBoolean(SettingsFragment.NIGHT_AUTO, false));
AutoDeleteReceiver.setupAutoDeleteAlarm(context);
}
}
@@ -30,6 +30,7 @@ import com.moez.QKSMS.common.google.DraftCache;
import com.moez.QKSMS.common.utils.MessageUtils;
import com.moez.QKSMS.data.Conversation;
import com.moez.QKSMS.enums.QKPreference;
import com.moez.QKSMS.receiver.AutoDeleteReceiver;
import com.moez.QKSMS.receiver.IconColorReceiver;
import com.moez.QKSMS.transaction.NotificationManager;
import com.moez.QKSMS.transaction.SmsHelper;
@@ -100,6 +101,8 @@ public class MainActivity extends QKActivity {
//Adds a small/non intrusive snackbar that asks the user to rate the app
SnackEngage.from(this).withSnack(new QKRateSnack().withDuration(BaseSnack.DURATION_LONG))
.build().engageWhenAppropriate();
AutoDeleteReceiver.setupAutoDeleteAlarm(this);
}
/**
@@ -27,10 +27,10 @@ import com.moez.QKSMS.R;
import com.moez.QKSMS.common.DialogHelper;
import com.moez.QKSMS.common.DonationManager;
import com.moez.QKSMS.common.LiveViewManager;
import com.moez.QKSMS.common.QKPreferences;
import com.moez.QKSMS.common.utils.ColorUtils;
import com.moez.QKSMS.enums.QKPreference;
import com.moez.QKSMS.ui.ThemeManager;
import com.moez.QKSMS.ui.search.SearchFragment;
import com.moez.QKSMS.ui.settings.SettingsActivity;
import com.moez.QKSMS.ui.view.QKTextView;
@@ -62,12 +62,12 @@ public abstract class QKActivity extends AppCompatActivity {
mProgressDialog.setCancelable(false);
LiveViewManager.registerView(QKPreference.TINTED_STATUS, this, key -> {
mStatusTintEnabled = getBoolean(QKPreference.TINTED_STATUS) &&
mStatusTintEnabled = QKPreferences.getBoolean(QKPreference.TINTED_STATUS) &&
Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
});
LiveViewManager.registerView(QKPreference.TINTED_NAV, this, key -> {
mNavigationTintEnabled = getBoolean(QKPreference.TINTED_NAV) &&
mNavigationTintEnabled = QKPreferences.getBoolean(QKPreference.TINTED_NAV) &&
Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
});
}
@@ -296,28 +296,4 @@ public abstract class QKActivity extends AppCompatActivity {
public RequestQueue getRequestQueue() {
return ((QKSMSApp) getApplication()).getRequestQueue();
}
public boolean getBoolean(QKPreference preference) {
return getPrefs().getBoolean(preference.getKey(), (boolean) preference.getDefaultValue());
}
public void setBoolean(QKPreference preference, boolean newValue) {
getPrefs().edit().putBoolean(preference.getKey(), newValue).apply();
}
public int getInt(QKPreference preference) {
return getPrefs().getInt(preference.getKey(), (int) preference.getDefaultValue());
}
public void setInt(QKPreference preference, int newValue) {
getPrefs().edit().putInt(preference.getKey(), newValue).apply();
}
public String getString(QKPreference preference) {
return getPrefs().getString(preference.getKey(), (String) preference.getDefaultValue());
}
public void setString(QKPreference preference, String newValue) {
getPrefs().edit().putString(preference.getKey(), newValue).apply();
}
}
@@ -8,6 +8,7 @@ import com.moez.QKSMS.R;
import com.moez.QKSMS.common.ConversationPrefsHelper;
import com.moez.QKSMS.common.FontManager;
import com.moez.QKSMS.common.LiveViewManager;
import com.moez.QKSMS.common.QKPreferences;
import com.moez.QKSMS.common.emoji.EmojiRegistry;
import com.moez.QKSMS.common.utils.DateFormatter;
import com.moez.QKSMS.data.Contact;
@@ -105,7 +106,7 @@ public class ConversationListAdapter extends RecyclerCursorAdapter<ConversationL
}
LiveViewManager.registerView(QKPreference.HIDE_AVATAR_CONVERSATIONS, this, key ->
holder.mAvatarView.setVisibility(mContext.getBoolean(QKPreference.HIDE_AVATAR_CONVERSATIONS) ? View.GONE : View.VISIBLE));
holder.mAvatarView.setVisibility(QKPreferences.getBoolean(QKPreference.HIDE_AVATAR_CONVERSATIONS) ? View.GONE : View.VISIBLE));
// Date
holder.dateView.setText(DateFormatter.getConversationTimestamp(mContext, conversation.getDate()));
@@ -46,6 +46,7 @@ import com.moez.QKSMS.common.CIELChEvaluator;
import com.moez.QKSMS.common.ConversationPrefsHelper;
import com.moez.QKSMS.common.DialogHelper;
import com.moez.QKSMS.common.LiveViewManager;
import com.moez.QKSMS.common.QKPreferences;
import com.moez.QKSMS.common.utils.KeyboardUtils;
import com.moez.QKSMS.common.utils.MessageUtils;
import com.moez.QKSMS.common.vcard.ContactOperations;
@@ -188,7 +189,7 @@ public class MessageListFragment extends QKFragment implements ActivityLauncher,
mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
mProxSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
if (mContext.getBoolean(QKPreference.PROXIMITY_SENSOR)) {
if (QKPreferences.getBoolean(QKPreference.PROXIMITY_SENSOR)) {
mSensorManager.registerListener(this, mProxSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@@ -247,7 +248,7 @@ public class MessageListFragment extends QKFragment implements ActivityLauncher,
public void onResume() {
super.onResume();
if (mContext.getBoolean(QKPreference.PROXIMITY_SENSOR)) {
if (QKPreferences.getBoolean(QKPreference.PROXIMITY_SENSOR)) {
mSensorManager.registerListener(this, mProxSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@@ -35,6 +35,7 @@ import com.moez.QKSMS.common.DialogHelper;
import com.moez.QKSMS.common.DonationManager;
import com.moez.QKSMS.common.ListviewHelper;
import com.moez.QKSMS.common.LiveViewManager;
import com.moez.QKSMS.common.QKPreferences;
import com.moez.QKSMS.common.utils.DateFormatter;
import com.moez.QKSMS.common.utils.KeyboardUtils;
import com.moez.QKSMS.common.utils.PackageUtils;
@@ -288,12 +289,12 @@ public class SettingsFragment extends PreferenceFragment implements Preference.O
EditTextPreference deleteUnread = (EditTextPreference) findPreference(DELETE_UNREAD_MESSAGES);
if (deleteUnread != null) {
deleteUnread.setSummary(mContext.getString(R.string.pref_delete_old_messages_unread_summary, mContext.getString(QKPreference.DELETE_UNREAD_MESSAGES)));
deleteUnread.setSummary(mContext.getString(R.string.pref_delete_old_messages_unread_summary, QKPreferences.getString(QKPreference.AUTO_DELETE_UNREAD)));
}
EditTextPreference deleteRead = (EditTextPreference) findPreference(DELETE_READ_MESSAGES);
if (deleteRead != null) {
deleteRead.setSummary(mContext.getString(R.string.pref_delete_old_messages_read_summary, mContext.getString(QKPreference.DELETE_READ_MESSAGES)));
deleteRead.setSummary(mContext.getString(R.string.pref_delete_old_messages_read_summary, QKPreferences.getString(QKPreference.AUTO_DELETE_READ)));
}
Preference day_start = findPreference(DAY_START);