diff --git a/src/org/thoughtcrime/securesms/database/MmsDatabase.java b/src/org/thoughtcrime/securesms/database/MmsDatabase.java index 5f48b8f709..96af50ca82 100644 --- a/src/org/thoughtcrime/securesms/database/MmsDatabase.java +++ b/src/org/thoughtcrime/securesms/database/MmsDatabase.java @@ -29,6 +29,7 @@ import android.util.Pair; import com.google.i18n.phonenumbers.PhoneNumberUtil; +import org.thoughtcrime.securesms.ApplicationContext; import org.thoughtcrime.securesms.R; import org.thoughtcrime.securesms.crypto.MasterCipher; import org.thoughtcrime.securesms.crypto.MasterSecret; @@ -36,6 +37,7 @@ import org.thoughtcrime.securesms.database.model.DisplayRecord; import org.thoughtcrime.securesms.database.model.MediaMmsMessageRecord; import org.thoughtcrime.securesms.database.model.MessageRecord; import org.thoughtcrime.securesms.database.model.NotificationMmsMessageRecord; +import org.thoughtcrime.securesms.jobs.TrimThreadJob; import org.thoughtcrime.securesms.mms.IncomingMediaMessage; import org.thoughtcrime.securesms.mms.OutgoingGroupMediaMessage; import org.thoughtcrime.securesms.mms.OutgoingMediaMessage; @@ -52,6 +54,7 @@ import org.thoughtcrime.securesms.util.ListenableFutureTask; import org.thoughtcrime.securesms.util.TextSecurePreferences; import org.thoughtcrime.securesms.util.Trimmer; import org.thoughtcrime.securesms.util.Util; +import org.whispersystems.jobqueue.JobManager; import org.whispersystems.libaxolotl.InvalidMessageException; import org.whispersystems.libaxolotl.util.guava.Optional; import org.whispersystems.textsecure.api.util.InvalidNumberException; @@ -154,8 +157,11 @@ public class MmsDatabase extends Database implements MmsSmsColumns { private static final Map> slideCache = Collections.synchronizedMap(new LRUCache>(20)); + private final JobManager jobManager; + public MmsDatabase(Context context, SQLiteOpenHelper databaseHelper) { super(context, databaseHelper); + this.jobManager = ApplicationContext.getInstance(context).getJobManager(); } public int getMessageCountForThread(long threadId) { @@ -566,9 +572,9 @@ public class MmsDatabase extends Database implements MmsSmsColumns { DatabaseFactory.getThreadDatabase(context).update(threadId); notifyConversationListeners(threadId); - Trimmer.trimThread(context, threadId); + jobManager.add(new TrimThreadJob(context, threadId)); - return new Pair(messageId, threadId); + return new Pair<>(messageId, threadId); } public Pair insertMessageInbox(MasterSecret masterSecret, @@ -644,7 +650,7 @@ public class MmsDatabase extends Database implements MmsSmsColumns { DatabaseFactory.getThreadDatabase(context).setUnread(threadId); } - Trimmer.trimThread(context, threadId); + jobManager.add(new TrimThreadJob(context, threadId)); } public long insertMessageOutbox(MasterSecret masterSecret, OutgoingMediaMessage message, @@ -690,7 +696,7 @@ public class MmsDatabase extends Database implements MmsSmsColumns { long messageId = insertMediaMessage(masterSecret, sendRequest.getPduHeaders(), sendRequest.getBody(), contentValues); - Trimmer.trimThread(context, threadId); + jobManager.add(new TrimThreadJob(context, threadId)); return messageId; } diff --git a/src/org/thoughtcrime/securesms/database/SmsDatabase.java b/src/org/thoughtcrime/securesms/database/SmsDatabase.java index ed1617940d..c5bc39c837 100644 --- a/src/org/thoughtcrime/securesms/database/SmsDatabase.java +++ b/src/org/thoughtcrime/securesms/database/SmsDatabase.java @@ -27,8 +27,10 @@ import android.text.TextUtils; import android.util.Log; import android.util.Pair; +import org.thoughtcrime.securesms.ApplicationContext; import org.thoughtcrime.securesms.database.model.DisplayRecord; import org.thoughtcrime.securesms.database.model.SmsMessageRecord; +import org.thoughtcrime.securesms.jobs.TrimThreadJob; import org.thoughtcrime.securesms.recipients.Recipient; import org.thoughtcrime.securesms.recipients.RecipientFactory; import org.thoughtcrime.securesms.recipients.RecipientFormattingException; @@ -37,7 +39,7 @@ import org.thoughtcrime.securesms.sms.IncomingGroupMessage; import org.thoughtcrime.securesms.sms.IncomingKeyExchangeMessage; import org.thoughtcrime.securesms.sms.IncomingTextMessage; import org.thoughtcrime.securesms.sms.OutgoingTextMessage; -import org.thoughtcrime.securesms.util.Trimmer; +import org.whispersystems.jobqueue.JobManager; import org.whispersystems.textsecure.api.util.InvalidNumberException; import java.util.Set; @@ -86,8 +88,11 @@ public class SmsDatabase extends Database implements MmsSmsColumns { REPLY_PATH_PRESENT, SUBJECT, BODY, SERVICE_CENTER, RECEIPT_COUNT }; + private final JobManager jobManager; + public SmsDatabase(Context context, SQLiteOpenHelper databaseHelper) { super(context, databaseHelper); + this.jobManager = ApplicationContext.getInstance(context).getJobManager(); } private void updateTypeBitmask(long id, long maskOff, long maskOn) { @@ -329,7 +334,8 @@ public class SmsDatabase extends Database implements MmsSmsColumns { DatabaseFactory.getThreadDatabase(context).update(record.getThreadId()); notifyConversationListeners(record.getThreadId()); - Trimmer.trimThread(context, record.getThreadId()); + + jobManager.add(new TrimThreadJob(context, record.getThreadId())); reader.close(); return new Pair<>(newMessageId, record.getThreadId()); @@ -417,9 +423,9 @@ public class SmsDatabase extends Database implements MmsSmsColumns { DatabaseFactory.getThreadDatabase(context).update(threadId); notifyConversationListeners(threadId); - Trimmer.trimThread(context, threadId); + jobManager.add(new TrimThreadJob(context, threadId)); - return new Pair(messageId, threadId); + return new Pair<>(messageId, threadId); } public Pair insertMessageInbox(IncomingTextMessage message) { @@ -450,7 +456,7 @@ public class SmsDatabase extends Database implements MmsSmsColumns { DatabaseFactory.getThreadDatabase(context).update(threadId); notifyConversationListeners(threadId); - Trimmer.trimThread(context, threadId); + jobManager.add(new TrimThreadJob(context, threadId)); return messageId; } diff --git a/src/org/thoughtcrime/securesms/jobs/TrimThreadJob.java b/src/org/thoughtcrime/securesms/jobs/TrimThreadJob.java new file mode 100644 index 0000000000..754e58cb52 --- /dev/null +++ b/src/org/thoughtcrime/securesms/jobs/TrimThreadJob.java @@ -0,0 +1,65 @@ +/** + * Copyright (C) 2014 Open Whisper Systems + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.thoughtcrime.securesms.jobs; + +import android.content.Context; +import android.util.Log; + +import org.thoughtcrime.securesms.database.DatabaseFactory; +import org.thoughtcrime.securesms.util.TextSecurePreferences; +import org.whispersystems.jobqueue.Job; +import org.whispersystems.jobqueue.JobParameters; + +public class TrimThreadJob extends Job { + + private static final String TAG = TrimThreadJob.class.getSimpleName(); + + private final Context context; + private final long threadId; + + public TrimThreadJob(Context context, long threadId) { + super(JobParameters.newBuilder().withGroupId(TrimThreadJob.class.getSimpleName()).create()); + this.context = context; + this.threadId = threadId; + } + + @Override + public void onAdded() { + + } + + @Override + public void onRun() { + boolean trimmingEnabled = TextSecurePreferences.isThreadLengthTrimmingEnabled(context); + int threadLengthLimit = TextSecurePreferences.getThreadTrimLength(context); + + if (!trimmingEnabled) + return; + + DatabaseFactory.getThreadDatabase(context).trimThread(threadId, threadLengthLimit); + } + + @Override + public boolean onShouldRetry(Exception exception) { + return false; + } + + @Override + public void onCanceled() { + Log.w(TAG, "Canceling trim attempt: " + threadId); + } +} diff --git a/src/org/thoughtcrime/securesms/util/Trimmer.java b/src/org/thoughtcrime/securesms/util/Trimmer.java index 235b75d8c0..4df39bd6fc 100644 --- a/src/org/thoughtcrime/securesms/util/Trimmer.java +++ b/src/org/thoughtcrime/securesms/util/Trimmer.java @@ -14,22 +14,6 @@ public class Trimmer { new TrimmingProgressTask(context).execute(threadLengthLimit); } - public static void trimThread(final Context context, final long threadId) { - boolean trimmingEnabled = TextSecurePreferences.isThreadLengthTrimmingEnabled(context); - final int threadLengthLimit = TextSecurePreferences.getThreadTrimLength(context); - - if (!trimmingEnabled) - return; - - new AsyncTask() { - @Override - protected Void doInBackground(Void... params) { - DatabaseFactory.getThreadDatabase(context).trimThread(threadId, threadLengthLimit); - return null; - } - }.execute(); - } - private static class TrimmingProgressTask extends AsyncTask implements ThreadDatabase.ProgressListener { private ProgressDialog progressDialog; private Context context;