Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ace3962f authored by Moez Bhatti's avatar Moez Bhatti
Browse files

Set up alarm to run every day and delete old messages

parent 32b79c4c
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -450,6 +450,7 @@
                <action android:name="com.mariussoft.endlessjabber.action.extend" />
            </intent-filter>
        </receiver>
        <receiver android:name=".receiver.AutoDeleteReceiver" />

        <service android:name="com.mariussoft.endlessjabber.sdk.EndlessJabberWakefulService" />

+2 −0
Original line number Diff line number Diff line
@@ -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();
    }
+47 −0
Original line number Diff line number Diff line
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();
    }
}
 No newline at end of file
+5 −4
Original line number Diff line number Diff line
@@ -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;
+50 −0
Original line number Diff line number Diff line
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);
    }
}
Loading