Support for auto-deleting old messages beyond a certain conversation thread length.

This commit is contained in:
Moxie Marlinspike 2013-01-09 21:06:56 -08:00
parent a185750bb7
commit fe43ef65ab
7 changed files with 319 additions and 16 deletions

View File

@ -39,6 +39,11 @@
<string name="ApplicationPreferenceActivity_you_need_to_have_entered_your_passphrase_before_importing_keys">You need to have entered your passphrase before importing keys...</string>
<string name="ApplicationPreferenceActivity_you_need_to_have_entered_your_passphrase_before_managing_keys">You need to have entered your passphrase before managing keys...</string>
<string name="ApplicationPreferenceActivity_you_havent_set_a_passphrase_yet">You haven\'t set a passphrase yet!</string>
<string name="ApplicationPreferencesActivity_conversation_length_limit">Conversation length limit</string>
<string name="ApplicationPreferencesActivity_messages_per_conversation">messages per conversation</string>
<string name="ApplicationPreferencesActivity_delete_all_old_messages_now">Delete all old messages now?</string>
<string name="ApplicationPreferencesActivity_are_you_sure_you_would_like_to_immediately_trim_all_conversation_threads_to_the_s_most_recent_messages">Are you sure you would like to immediately trim all conversation threads to the %s most recent messages?</string>
<string name="ApplicationPreferencesActivity_delete">Delete</string>
<!-- AttachmentTypeSelectorAdapter -->
@ -377,6 +382,17 @@
<string name="preferences__mmsc_url_required">MMSC URL (Required)</string>
<string name="preferences__mms_proxy_host_optional">MMS Proxy Host (Optional)</string>
<string name="preferences__mms_proxy_port_optional">MMS Proxy Port (Optional)</string>
<string name="preferences__delivery_reports">Delivery Reports</string>
<string name="preferences__request_a_delivery_report_for_each_sms_message_you_send">Request a delivery report for each SMS message you send</string>
<string name="preferences__request_a_delivery_report_for_each_mms_message_you_send">Request a delivery report for each MMS message you send</string>
<string name="preferences__mms_delivery_reports">MMS delivery reports</string>
<string name="preferences__sms_delivery_reports">SMS delivery reports</string>
<string name="preferences__automatically_delete_older_messages_once_a_conversation_thread_exceeds_a_specified_length">Automatically delete older messages once a conversation thread exceeds a specified length</string>
<string name="preferences__delete_old_messages">Delete old messages</string>
<string name="preferences__storage">Storage</string>
<string name="preferences__conversation_length_limit">Conversation length limit</string>
<string name="preferences__trim_all_threads_now">Trim all threads now</string>
<string name="preferences__scan_through_all_conversation_threads_and_enforce_conversation_length_limits">Scan through all conversation threads and enforce conversation length limits</string>
<!-- **************************************** -->

View File

@ -14,17 +14,36 @@
</PreferenceCategory>
<PreferenceCategory android:title="Delivery Reports">
<PreferenceCategory android:title="@string/preferences__delivery_reports">
<CheckBoxPreference android:defaultValue="false"
android:key="pref_delivery_report_sms"
android:summary="Request a delivery report for each SMS message you send"
android:title="SMS delivery reports" />
android:summary="@string/preferences__request_a_delivery_report_for_each_sms_message_you_send"
android:title="@string/preferences__sms_delivery_reports" />
<CheckBoxPreference android:defaultValue="false"
android:key="pref_delivery_report_mms"
android:summary="Request a delivery report for each MMS message you send"
android:summary="@string/preferences__request_a_delivery_report_for_each_mms_message_you_send"
android:enabled="false"
android:title="MMS delivery reports" />
android:title="@string/preferences__mms_delivery_reports" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/preferences__storage">
<CheckBoxPreference android:defaultValue="false"
android:key="pref_trim_threads"
android:summary="@string/preferences__automatically_delete_older_messages_once_a_conversation_thread_exceeds_a_specified_length"
android:title="@string/preferences__delete_old_messages" />
<EditTextPreference android:defaultValue="500"
android:key="pref_trim_length"
android:title="@string/preferences__conversation_length_limit"
android:inputType="number"
android:dependency="pref_trim_threads" />
<Preference android:key="pref_trim_now"
android:title="@string/preferences__trim_all_threads_now"
android:summary="@string/preferences__scan_through_all_conversation_threads_and_enforce_conversation_length_limits"
android:dependency="pref_trim_threads" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/preferences__input_settings">

View File

@ -17,6 +17,8 @@
package org.thoughtcrime.securesms;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
@ -25,6 +27,7 @@ import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.util.Log;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockPreferenceActivity;
@ -38,6 +41,7 @@ import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.service.KeyCachingService;
import org.thoughtcrime.securesms.util.Dialogs;
import org.thoughtcrime.securesms.util.MemoryCleaner;
import org.thoughtcrime.securesms.util.Trimmer;
import java.util.List;
@ -83,6 +87,10 @@ public class ApplicationPreferencesActivity extends SherlockPreferenceActivity {
public static final String SMS_DELIVERY_REPORT_PREF = "pref_delivery_report_sms";
public static final String MMS_DELIVERY_REPORT_PREF = "pref_delivery_report_mms";
public static final String THREAD_TRIM_ENABLED = "pref_trim_threads";
public static final String THREAD_TRIM_LENGTH = "pref_trim_length";
public static final String THREAD_TRIM_NOW = "pref_trim_now";
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
@ -104,6 +112,10 @@ public class ApplicationPreferencesActivity extends SherlockPreferenceActivity {
.setOnPreferenceClickListener(new ManageIdentitiesClickListener());
this.findPreference(CHANGE_PASSPHRASE_PREF)
.setOnPreferenceClickListener(new ChangePassphraseClickListener());
this.findPreference(THREAD_TRIM_NOW)
.setOnPreferenceClickListener(new TrimNowClickListener());
this.findPreference(THREAD_TRIM_LENGTH)
.setOnPreferenceChangeListener(new TrimLengthValidationListener());
}
@Override
@ -159,20 +171,16 @@ public class ApplicationPreferencesActivity extends SherlockPreferenceActivity {
preference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference pref, Object newValue) {
preference.setSummary(newValue == null ? "Not set" : (String)newValue);
preference.setSummary(newValue == null ? "Not set" : ((String)newValue));
return true;
}
});
}
private void initializeEditTextSummaries() {
final EditTextPreference mmscUrlPreference = (EditTextPreference)this.findPreference(MMSC_HOST_PREF);
final EditTextPreference mmsProxyHostPreference = (EditTextPreference)this.findPreference(MMSC_PROXY_HOST_PREF);
final EditTextPreference mmsProxyPortPreference = (EditTextPreference)this.findPreference(MMSC_PROXY_PORT_PREF);
initializeEditTextSummary(mmscUrlPreference);
initializeEditTextSummary(mmsProxyHostPreference);
initializeEditTextSummary(mmsProxyPortPreference);
initializeEditTextSummary((EditTextPreference)this.findPreference(MMSC_HOST_PREF));
initializeEditTextSummary((EditTextPreference)this.findPreference(MMSC_PROXY_HOST_PREF));
initializeEditTextSummary((EditTextPreference)this.findPreference(MMSC_PROXY_PORT_PREF));
}
private void initializeIdentitySelection() {
@ -330,4 +338,56 @@ public class ApplicationPreferencesActivity extends SherlockPreferenceActivity {
}
}
private class TrimNowClickListener implements Preference.OnPreferenceClickListener {
@Override
public boolean onPreferenceClick(Preference preference) {
final int threadLengthLimit = Integer.parseInt(PreferenceManager.getDefaultSharedPreferences(ApplicationPreferencesActivity.this)
.getString(THREAD_TRIM_LENGTH, "500"));
AlertDialog.Builder builder = new AlertDialog.Builder(ApplicationPreferencesActivity.this);
builder.setTitle(R.string.ApplicationPreferencesActivity_delete_all_old_messages_now);
builder.setMessage(String.format(getString(R.string.ApplicationPreferencesActivity_are_you_sure_you_would_like_to_immediately_trim_all_conversation_threads_to_the_s_most_recent_messages),
threadLengthLimit));
builder.setPositiveButton(R.string.ApplicationPreferencesActivity_delete,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Trimmer.trimAllThreads(ApplicationPreferencesActivity.this, threadLengthLimit);
}
});
builder.setNegativeButton(android.R.string.cancel, null);
builder.show();
return true;
}
}
private class TrimLengthValidationListener implements Preference.OnPreferenceChangeListener {
public TrimLengthValidationListener() {
EditTextPreference preference = (EditTextPreference)findPreference(THREAD_TRIM_LENGTH);
preference.setSummary(preference.getText() + " " + getString(R.string.ApplicationPreferencesActivity_messages_per_conversation));
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (newValue == null || ((String)newValue).trim().length() == 0) {
return false;
}
try {
Integer.parseInt((String)newValue);
} catch (NumberFormatException nfe) {
Log.w("ApplicationPreferencesActivity", nfe);
return false;
}
preference.setSummary((String)newValue + " " +
getString(R.string.ApplicationPreferencesActivity_messages_per_conversation));
return true;
}
}
}

View File

@ -28,6 +28,7 @@ import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.recipients.RecipientFactory;
import org.thoughtcrime.securesms.recipients.RecipientFormattingException;
import org.thoughtcrime.securesms.recipients.Recipients;
import org.thoughtcrime.securesms.util.Trimmer;
import ws.com.google.android.mms.InvalidHeaderValueException;
import ws.com.google.android.mms.MmsException;
@ -343,6 +344,7 @@ public class MmsDatabase extends Database {
notifyConversationListeners(threadId);
DatabaseFactory.getThreadDatabase(context).update(threadId);
DatabaseFactory.getThreadDatabase(context).setUnread(threadId);
Trimmer.trimThread(context, threadId);
return messageId;
} catch (RecipientFormattingException rfe) {
@ -364,6 +366,8 @@ public class MmsDatabase extends Database {
long messageId = insertMediaMessage(sendRequest, contentValues);
DatabaseFactory.getThreadDatabase(context).setRead(threadId);
Trimmer.trimThread(context, threadId);
return messageId;
}
@ -427,6 +431,35 @@ public class MmsDatabase extends Database {
}
}
/*package*/void deleteMessagesInThreadBeforeDate(long threadId, long date) {
date = date / 1000;
Cursor cursor = null;
try {
SQLiteDatabase db = databaseHelper.getReadableDatabase();
String where = THREAD_ID + " = ? AND (CASE " + MESSAGE_BOX;
for (int outgoingType : Types.OUTGOING_MAILBOX_TYPES) {
where += " WHEN " + outgoingType + " THEN " + DATE_SENT + " < " + date;
}
where += (" ELSE " + DATE_RECEIVED + " < " + date + " END)");
Log.w("MmsDatabase", "Executing trim query: " + where);
cursor = db.query(TABLE_NAME, new String[] {ID}, where, new String[] {threadId+""}, null, null, null);
while (cursor != null && cursor.moveToNext()) {
Log.w("MmsDatabase", "Trimming: " + cursor.getLong(0));
delete(cursor.getLong(0));
}
} finally {
if (cursor != null)
cursor.close();
}
}
public void deleteAllThreads() {
DatabaseFactory.getPartDatabase(context).deleteAllParts();
DatabaseFactory.getMmsAddressDatabase(context).deleteAllAddresses();
@ -553,12 +586,23 @@ public class MmsDatabase extends Database {
public static final int DOWNLOAD_SOFT_FAILURE = 4;
public static final int DOWNLOAD_HARD_FAILURE = 5;
public static final int[] OUTGOING_MAILBOX_TYPES = {Types.MESSAGE_BOX_OUTBOX,
Types.MESSAGE_BOX_SENT,
Types.MESSAGE_BOX_SECURE_OUTBOX,
Types.MESSAGE_BOX_SENT_FAILED,
Types.MESSAGE_BOX_SECURE_SENT};
public static boolean isSecureMmsBox(long mailbox) {
return mailbox == Types.MESSAGE_BOX_SECURE_OUTBOX || mailbox == Types.MESSAGE_BOX_SECURE_SENT || mailbox == Types.MESSAGE_BOX_SECURE_INBOX;
}
public static boolean isOutgoingMmsBox(long mailbox) {
return mailbox == Types.MESSAGE_BOX_OUTBOX || mailbox == Types.MESSAGE_BOX_SENT || mailbox == Types.MESSAGE_BOX_SECURE_OUTBOX || mailbox == Types.MESSAGE_BOX_SENT_FAILED || mailbox == Types.MESSAGE_BOX_SECURE_SENT;
for (int outgoingMailboxType : OUTGOING_MAILBOX_TYPES) {
if (mailbox == outgoingMailboxType)
return true;
}
return false;
}
public static boolean isPendingMmsBox(long mailbox) {

View File

@ -27,6 +27,7 @@ import android.util.Log;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.Recipients;
import org.thoughtcrime.securesms.util.Trimmer;
import java.util.ArrayList;
import java.util.List;
@ -112,6 +113,8 @@ public class SmsDatabase extends Database {
DatabaseFactory.getThreadDatabase(context).setUnread(threadId);
DatabaseFactory.getThreadDatabase(context).update(threadId);
notifyConversationListeners(threadId);
Trimmer.trimThread(context, threadId);
return messageId;
}
@ -235,6 +238,8 @@ public class SmsDatabase extends Database {
DatabaseFactory.getThreadDatabase(context).setRead(threadId);
DatabaseFactory.getThreadDatabase(context).update(threadId);
notifyConversationListeners(threadId);
Trimmer.trimThread(context, threadId);
return messageId;
}
@ -276,6 +281,18 @@ public class SmsDatabase extends Database {
db.delete(TABLE_NAME, THREAD_ID + " = ?", new String[] {threadId+""});
}
/*package*/void deleteMessagesInThreadBeforeDate(long threadId, long date) {
SQLiteDatabase db = databaseHelper.getWritableDatabase();
String where = THREAD_ID + " = ? AND (CASE " + TYPE;
for (int outgoingType : Types.OUTGOING_MESSAGE_TYPES) {
where += " WHEN " + outgoingType + " THEN " + DATE_SENT + " < " + date;
}
where += (" ELSE " + DATE_RECEIVED + " < " + date + " END)");
db.delete(TABLE_NAME, where, new String[] {threadId+""});
}
/*package*/ void deleteThreads(Set<Long> threadIds) {
SQLiteDatabase db = databaseHelper.getWritableDatabase();
@ -348,12 +365,21 @@ public class SmsDatabase extends Database {
public static final int DECRYPT_IN_PROGRESS_TYPE = 47; // Messages are in the process of being asymmetricaly decrypted.
public static final int NO_SESSION_TYPE = 48; // Messages were received with async encryption but there is no session yet.
public static final int[] OUTGOING_MESSAGE_TYPES = {SENT_TYPE, SENT_PENDING, ENCRYPTING_TYPE,
ENCRYPTED_OUTBOX_TYPE, SECURE_SENT_TYPE,
FAILED_TYPE};
public static boolean isFailedMessageType(long type) {
return type == FAILED_TYPE;
}
public static boolean isOutgoingMessageType(long type) {
return type == SENT_TYPE || type == SENT_PENDING || type == ENCRYPTING_TYPE || type == ENCRYPTED_OUTBOX_TYPE || type == SECURE_SENT_TYPE || type == FAILED_TYPE;
for (int outgoingType : OUTGOING_MESSAGE_TYPES) {
if (type == outgoingType)
return true;
}
return false;
}
public static boolean isPendingMessageType(long type) {

View File

@ -21,6 +21,7 @@ import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.Recipients;
@ -143,6 +144,56 @@ public class ThreadDatabase extends Database {
notifyConversationListListeners();
}
public void trimAllThreads(int length, ProgressListener listener) {
Cursor cursor = null;
int threadCount = 0;
int complete = 0;
try {
cursor = this.getConversationList();
if (cursor != null)
threadCount = cursor.getCount();
while (cursor != null && cursor.moveToNext()) {
long threadId = cursor.getLong(cursor.getColumnIndexOrThrow(ID));
trimThread(threadId, length);
listener.onProgress(++complete, threadCount);
}
} finally {
if (cursor != null)
cursor.close();
}
}
public void trimThread(long threadId, int length) {
Log.w("ThreadDatabase", "Trimming thread: " + threadId + " to: " + length);
Cursor cursor = null;
try {
cursor = DatabaseFactory.getMmsSmsDatabase(context).getConversation(threadId);
if (cursor != null && cursor.getCount() > length) {
Log.w("ThreadDatabase", "Cursor count is greater than length!");
cursor.moveToPosition(cursor.getCount() - length);
long lastTweetDate = cursor.getLong(cursor.getColumnIndexOrThrow(MmsSmsDatabase.DATE_RECEIVED));
Log.w("ThreadDatabase", "Cut off tweet date: " + lastTweetDate);
DatabaseFactory.getSmsDatabase(context).deleteMessagesInThreadBeforeDate(threadId, lastTweetDate);
DatabaseFactory.getMmsDatabase(context).deleteMessagesInThreadBeforeDate(threadId, lastTweetDate);
update(threadId);
notifyConversationListeners(threadId);
}
} finally {
if (cursor != null)
cursor.close();
}
}
public void setRead(long threadId) {
ContentValues contentValues = new ContentValues(1);
contentValues.put(READ, 1);
@ -290,4 +341,8 @@ public class ThreadDatabase extends Database {
notifyConversationListListeners();
}
public static interface ProgressListener {
public void onProgress(int complete, int total);
}
}

View File

@ -0,0 +1,83 @@
package org.thoughtcrime.securesms.util;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.widget.Toast;
import org.thoughtcrime.securesms.ApplicationPreferencesActivity;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.ThreadDatabase;
public class Trimmer {
public static void trimAllThreads(Context context, int threadLengthLimit) {
new TrimmingProgressTask(context).execute(threadLengthLimit);
}
public static void trimThread(final Context context, final long threadId) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
boolean trimmingEnabled = preferences.getBoolean(ApplicationPreferencesActivity.THREAD_TRIM_ENABLED, false);
final int threadLengthLimit = Integer.parseInt(preferences.getString(ApplicationPreferencesActivity.THREAD_TRIM_LENGTH, "500"));
if (!trimmingEnabled)
return;
new Thread() {
@Override
public void run() {
DatabaseFactory.getThreadDatabase(context).trimThread(threadId, threadLengthLimit);
}
}.start();
}
private static class TrimmingProgressTask extends AsyncTask<Integer, Integer, Void> implements ThreadDatabase.ProgressListener {
private ProgressDialog progressDialog;
private Context context;
public TrimmingProgressTask(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(context);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(false);
progressDialog.setTitle("Deleting...");
progressDialog.setMessage("Deleting old messages...");
progressDialog.setMax(100);
progressDialog.show();
}
@Override
protected Void doInBackground(Integer... params) {
DatabaseFactory.getThreadDatabase(context).trimAllThreads(params[0], this);
return null;
}
@Override
protected void onProgressUpdate(Integer... progress) {
double count = progress[1];
double index = progress[0];
progressDialog.setProgress((int)Math.round((index / count) * 100.0));
}
@Override
protected void onPostExecute(Void result) {
progressDialog.dismiss();
Toast.makeText(context,
"Old messages successfully deleted!",
Toast.LENGTH_LONG).show();
}
@Override
public void onProgress(int complete, int total) {
this.publishProgress(complete, total);
}
}
}