2014-11-24 22:50:32 -08:00
|
|
|
/**
|
|
|
|
* 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;
|
2018-08-09 10:15:43 -04:00
|
|
|
import android.support.annotation.NonNull;
|
2014-11-24 22:50:32 -08:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
2018-06-18 12:27:04 -07:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.JobParameters;
|
2018-08-09 10:15:43 -04:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.SafeData;
|
2018-08-01 11:09:24 -04:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2014-11-24 22:50:32 -08:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
import androidx.work.Data;
|
2018-11-27 12:34:42 -08:00
|
|
|
import androidx.work.WorkerParameters;
|
2018-08-09 10:15:43 -04:00
|
|
|
|
|
|
|
public class TrimThreadJob extends ContextJob {
|
2014-11-24 22:50:32 -08:00
|
|
|
|
|
|
|
private static final String TAG = TrimThreadJob.class.getSimpleName();
|
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
private static final String KEY_THREAD_ID = "thread_id";
|
|
|
|
|
|
|
|
private long threadId;
|
|
|
|
|
2018-11-27 12:34:42 -08:00
|
|
|
public TrimThreadJob(@NonNull Context context, @NonNull WorkerParameters workerParameters) {
|
|
|
|
super(context, workerParameters);
|
2018-08-09 10:15:43 -04:00
|
|
|
}
|
2014-11-24 22:50:32 -08:00
|
|
|
|
|
|
|
public TrimThreadJob(Context context, long threadId) {
|
2018-08-09 10:15:43 -04:00
|
|
|
super(context, JobParameters.newBuilder().withGroupId(TrimThreadJob.class.getSimpleName()).create());
|
2014-11-24 22:50:32 -08:00
|
|
|
this.context = context;
|
|
|
|
this.threadId = threadId;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-08-09 10:15:43 -04:00
|
|
|
protected void initialize(@NonNull SafeData data) {
|
|
|
|
threadId = data.getLong(KEY_THREAD_ID);
|
|
|
|
}
|
2014-11-24 22:50:32 -08:00
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
@Override
|
|
|
|
protected @NonNull Data serialize(@NonNull Data.Builder dataBuilder) {
|
|
|
|
return dataBuilder.putLong(KEY_THREAD_ID, threadId).build();
|
2014-11-24 22:50:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
|
|
|
}
|
|
|
|
}
|