package org.thoughtcrime.securesms.stickers; import android.content.Context; import android.database.Cursor; import android.support.annotation.NonNull; import org.thoughtcrime.securesms.ApplicationContext; import org.thoughtcrime.securesms.database.AttachmentDatabase; import org.thoughtcrime.securesms.database.DatabaseFactory; import org.thoughtcrime.securesms.database.StickerDatabase; import org.thoughtcrime.securesms.database.StickerDatabase.StickerPackRecordReader; import org.thoughtcrime.securesms.database.model.StickerPackRecord; import org.thoughtcrime.securesms.jobmanager.JobManager; import org.thoughtcrime.securesms.jobs.MultiDeviceStickerPackOperationJob; import org.thoughtcrime.securesms.jobs.StickerPackDownloadJob; import org.thoughtcrime.securesms.util.TextSecurePreferences; import org.thoughtcrime.securesms.util.concurrent.SignalExecutors; import java.util.ArrayList; import java.util.List; final class StickerManagementRepository { private final Context context; private final StickerDatabase stickerDatabase; private final AttachmentDatabase attachmentDatabase; StickerManagementRepository(@NonNull Context context) { this.context = context.getApplicationContext(); this.stickerDatabase = DatabaseFactory.getStickerDatabase(context); this.attachmentDatabase = DatabaseFactory.getAttachmentDatabase(context); } void deleteOrphanedStickerPacks() { SignalExecutors.SERIAL.execute(stickerDatabase::deleteOrphanedPacks); } void fetchUnretrievedReferencePacks() { SignalExecutors.SERIAL.execute(() -> { JobManager jobManager = ApplicationContext.getInstance(context).getJobManager(); try (Cursor cursor = attachmentDatabase.getUnavailableStickerPacks()) { while (cursor != null && cursor.moveToNext()) { String packId = cursor.getString(cursor.getColumnIndexOrThrow(AttachmentDatabase.STICKER_PACK_ID)); String packKey = cursor.getString(cursor.getColumnIndexOrThrow(AttachmentDatabase.STICKER_PACK_KEY)); jobManager.add(new StickerPackDownloadJob(packId, packKey, true)); } } }); } void getStickerPacks(@NonNull Callback callback) { SignalExecutors.SERIAL.execute(() -> { List installedPacks = new ArrayList<>(); List availablePacks = new ArrayList<>(); List blessedPacks = new ArrayList<>(); try (StickerPackRecordReader reader = new StickerPackRecordReader(stickerDatabase.getAllStickerPacks())) { StickerPackRecord record; while ((record = reader.getNext()) != null) { if (record.isInstalled()) { installedPacks.add(record); } else if (BlessedPacks.contains(record.getPackId())) { blessedPacks.add(record); } else { availablePacks.add(record); } } } callback.onComplete(new PackResult(installedPacks, availablePacks, blessedPacks)); }); } void uninstallStickerPack(@NonNull String packId, @NonNull String packKey) { SignalExecutors.SERIAL.execute(() -> { stickerDatabase.uninstallPack(packId); if (TextSecurePreferences.isMultiDevice(context)) { ApplicationContext.getInstance(context) .getJobManager() .add(new MultiDeviceStickerPackOperationJob(packId, packKey, MultiDeviceStickerPackOperationJob.Type.REMOVE)); } }); } void installStickerPack(@NonNull String packId, @NonNull String packKey) { SignalExecutors.SERIAL.execute(() -> { JobManager jobManager = ApplicationContext.getInstance(context).getJobManager(); if (stickerDatabase.isPackAvailableAsReference(packId)) { stickerDatabase.markPackAsInstalled(packId); } jobManager.add(new StickerPackDownloadJob(packId, packKey, false)); if (TextSecurePreferences.isMultiDevice(context)) { jobManager.add(new MultiDeviceStickerPackOperationJob(packId, packKey, MultiDeviceStickerPackOperationJob.Type.INSTALL)); } }); } static class PackResult { private final List installedPacks; private final List availablePacks; private final List blessedPacks; PackResult(@NonNull List installedPacks, @NonNull List availablePacks, @NonNull List blessedPacks) { this.installedPacks = installedPacks; this.availablePacks = availablePacks; this.blessedPacks = blessedPacks; } @NonNull List getInstalledPacks() { return installedPacks; } @NonNull List getAvailablePacks() { return availablePacks; } @NonNull List getBlessedPacks() { return blessedPacks; } } interface Callback { void onComplete(T result); } }