Do thread trimming as JobManager job rather than AsyncTask.

// FREEBIE
This commit is contained in:
Moxie Marlinspike 2014-11-24 22:50:32 -08:00
parent 3439657bba
commit d72a3ec669
4 changed files with 86 additions and 25 deletions

View File

@ -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<Long, SoftReference<SlideDeck>> slideCache =
Collections.synchronizedMap(new LRUCache<Long, SoftReference<SlideDeck>>(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<Long, Long>(messageId, threadId);
return new Pair<>(messageId, threadId);
}
public Pair<Long, Long> 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;
}

View File

@ -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<Long, Long>(messageId, threadId);
return new Pair<>(messageId, threadId);
}
public Pair<Long, Long> 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;
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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);
}
}

View File

@ -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<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
DatabaseFactory.getThreadDatabase(context).trimThread(threadId, threadLengthLimit);
return null;
}
}.execute();
}
private static class TrimmingProgressTask extends AsyncTask<Integer, Integer, Void> implements ThreadDatabase.ProgressListener {
private ProgressDialog progressDialog;
private Context context;