session-android/app/src/main/java/org/thoughtcrime/securesms/jobs/TrimThreadJob.java

85 lines
2.5 KiB
Java
Raw Normal View History

/**
* 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 androidx.annotation.NonNull;
2021-05-13 02:31:06 +02:00
import org.session.libsession.messaging.utilities.Data;
2021-01-15 05:36:30 +01:00
import org.session.libsession.utilities.TextSecurePreferences;
import org.session.libsignal.utilities.Log;
import org.thoughtcrime.securesms.dependencies.DatabaseComponent;
import org.thoughtcrime.securesms.jobmanager.Job;
2019-03-28 16:56:35 +01:00
public class TrimThreadJob extends BaseJob {
2019-03-28 16:56:35 +01:00
public static final String KEY = "TrimThreadJob";
private static final String TAG = TrimThreadJob.class.getSimpleName();
private static final String KEY_THREAD_ID = "thread_id";
private long threadId;
2019-03-28 16:56:35 +01:00
public TrimThreadJob(long threadId) {
this(new Job.Parameters.Builder().setQueue("TrimThreadJob").build(), threadId);
}
2019-03-28 16:56:35 +01:00
private TrimThreadJob(@NonNull Job.Parameters parameters, long threadId) {
super(parameters);
this.threadId = threadId;
}
@Override
public @NonNull
Data serialize() {
2019-03-28 16:56:35 +01:00
return new Data.Builder().putLong(KEY_THREAD_ID, threadId).build();
}
@Override
2019-03-28 16:56:35 +01:00
public @NonNull String getFactoryKey() {
return KEY;
}
@Override
public void onRun() {
boolean trimmingEnabled = TextSecurePreferences.isThreadLengthTrimmingEnabled(context);
int threadLengthLimit = TextSecurePreferences.getThreadTrimLength(context);
if (!trimmingEnabled)
return;
DatabaseComponent.get(context).threadDatabase().trimThread(threadId, threadLengthLimit);
}
@Override
public boolean onShouldRetry(@NonNull Exception exception) {
return false;
}
@Override
public void onCanceled() {
Log.w(TAG, "Canceling trim attempt: " + threadId);
}
2019-03-28 16:56:35 +01:00
public static final class Factory implements Job.Factory<TrimThreadJob> {
@Override
public @NonNull TrimThreadJob create(@NonNull Parameters parameters, @NonNull Data data) {
return new TrimThreadJob(parameters, data.getLong(KEY_THREAD_ID));
}
}
}