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

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

Separate calls for read and unread messages

parent 09273072
Loading
Loading
Loading
Loading
+11 −22
Original line number Diff line number Diff line
@@ -44,42 +44,29 @@ public class DeleteOldMessagesService extends IntentService {
    private void deleteOldUnreadMessages(Context context) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, -Integer.parseInt(QKPreferences.getString(QKPreference.AUTO_DELETE_UNREAD)));

        Cursor cursor = null;
        String selection = SmsHelper.COLUMN_DATE + "<=?";

        try {
            cursor = context.getContentResolver().query(
                    SmsHelper.SMS_CONTENT_PROVIDER,
                    new String[]{SmsHelper.COLUMN_ID, SmsHelper.COLUMN_DATE},
                    selection,
                    new String[]{String.valueOf(calendar.getTimeInMillis())},
                    null);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        int count = deleteOldMessages(context, SmsHelper.UNREAD_SELECTION, calendar.getTimeInMillis());
        Log.i(TAG, "Deleted unread messages: " + count);
    }

    private void deleteOldReadMessages(Context context) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, -Integer.parseInt(QKPreferences.getString(QKPreference.AUTO_DELETE_READ)));
        int count = deleteOldMessages(context, SmsHelper.READ_SELECTION + "=" + SmsHelper.READ, calendar.getTimeInMillis());
        Log.i(TAG, "Deleted read messages: " + count);
    }

    private int deleteOldMessages(Context context, String selection, long before) {
        Cursor cursor = null;
        String selection = SmsHelper.COLUMN_DATE + "<=?";
        selection += " AND " + SmsHelper.COLUMN_DATE + "<=?";

        try {
            cursor = context.getContentResolver().query(
                    SmsHelper.SMS_CONTENT_PROVIDER,
                    new String[]{SmsHelper.COLUMN_ID, SmsHelper.COLUMN_DATE},
                    selection,
                    new String[]{String.valueOf(calendar.getTimeInMillis())},
                    new String[]{String.valueOf(before)},
                    null);

            return cursor.getCount();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
@@ -87,6 +74,8 @@ public class DeleteOldMessagesService extends IntentService {
                cursor.close();
            }
        }

        return 0;
    }

    public static void setupAutoDeleteAlarm(Context context) {
+1 −0
Original line number Diff line number Diff line
@@ -86,6 +86,7 @@ public class SmsHelper {
    public static final String COLUMN_BODY = "body";
    public static final String COLUMN_SEEN = "seen";

    public static final String READ_SELECTION = COLUMN_READ + " = " + READ;
    public static final String UNREAD_SELECTION = COLUMN_READ + " = " + UNREAD;
    public static final String UNSEEN_SELECTION = COLUMN_SEEN + " = " + UNREAD;
    public static final String FAILED_SELECTION = COLUMN_TYPE + " = " + Message.FAILED;
+13 −1
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ import com.moez.QKSMS.common.utils.KeyboardUtils;
import com.moez.QKSMS.common.utils.PackageUtils;
import com.moez.QKSMS.enums.QKPreference;
import com.moez.QKSMS.receiver.NightModeAutoReceiver;
import com.moez.QKSMS.service.DeleteOldMessagesService;
import com.moez.QKSMS.transaction.EndlessJabber;
import com.moez.QKSMS.transaction.NotificationManager;
import com.moez.QKSMS.transaction.SmsHelper;
@@ -106,6 +107,7 @@ public class SettingsFragment extends PreferenceFragment implements Preference.O
    public static final String DELIVERY_REPORTS = "pref_key_delivery";
    public static final String DELIVERY_TOAST = "pref_key_delivery_toast";
    public static final String DELIVERY_VIBRATE = "pref_key_delivery_vibrate";
    public static final String DELETE_OLD_MESSAGES = "pref_key_delete_old_messages";
    public static final String DELETE_UNREAD_MESSAGES = "pref_key_delete_old_unread_messages";
    public static final String DELETE_READ_MESSAGES = "pref_key_delete_old_read_messages";
    public static final String YAPPY = "pref_key_endlessjabber";
@@ -453,12 +455,22 @@ public class SettingsFragment extends PreferenceFragment implements Preference.O
            case NIGHT_START:
                updateAlarmManager(mContext, true);
                break;

            case DELETE_OLD_MESSAGES:
                DeleteOldMessagesService.setupAutoDeleteAlarm(mContext);
                if ((Boolean) newValue) {
                    mContext.makeToast(R.string.toast_deleting_old_messages);
                }
                break;

            case DELETE_UNREAD_MESSAGES:
                preference.setSummary(mContext.getString(R.string.pref_delete_old_messages_unread_summary, newValue));
                break;

            case DELETE_READ_MESSAGES:
                preference.setSummary(mContext.getString(R.string.pref_delete_old_messages_read_summary, newValue));
                break;

            case YAPPY:
                if ((Boolean) newValue) {
                    try {
+1 −0
Original line number Diff line number Diff line
@@ -641,6 +641,7 @@
    <string name="toast_endlessjabber_not_installed">EndlessJabber is not installed, opening Google Play</string>
    <string name="toast_changelog_error">Couldn\'t load the changelog. Please try again later</string>
    <string name="toast_conversation_error">Couldn\'t open conversation</string>
    <string name="toast_deleting_old_messages">Your old messages will be deleted shortly</string>

    <!--Toast shown to thank user for donating-->
    <string name="thanks_1">Thank you for donating! You\'re my new best friend.</string>
+0 −2
Original line number Diff line number Diff line
@@ -59,7 +59,6 @@
            android:widgetLayout="@layout/view_switch" />
        <EditTextPreference
            android:defaultValue="7"
            android:dependency="pref_key_delete_old_messages"
            android:inputType="number"
            android:key="pref_key_delete_old_unread_messages"
            android:layout="@layout/list_item_preference"
@@ -67,7 +66,6 @@
            android:title="@string/pref_delete_old_messages_unread" />
        <EditTextPreference
            android:defaultValue="7"
            android:dependency="pref_key_delete_old_messages"
            android:inputType="number"
            android:key="pref_key_delete_old_read_messages"
            android:layout="@layout/list_item_preference"