Skip to content

Commit

Permalink
Fix crash when running shortcut update job on older APIs.
Browse files Browse the repository at this point in the history
  • Loading branch information
cody-signal committed Feb 11, 2021
1 parent b58b0fd commit dc6045c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
@@ -1,12 +1,15 @@
package org.thoughtcrime.securesms.jobs;

import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import org.signal.core.util.logging.Log;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.ThreadDatabase;
import org.thoughtcrime.securesms.database.model.ThreadRecord;
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
import org.thoughtcrime.securesms.jobmanager.Data;
import org.thoughtcrime.securesms.jobmanager.Job;
import org.thoughtcrime.securesms.recipients.Recipient;
Expand All @@ -18,6 +21,8 @@
import java.util.List;
import java.util.concurrent.TimeUnit;

import static org.thoughtcrime.securesms.util.ConversationUtil.CONVERSATION_SUPPORT_VERSION;

/**
* On some devices, interacting with the ShortcutManager can take a very long time (several seconds).
* So, we interact with it in a job instead, and keep it in one queue so it can't starve the other
Expand All @@ -29,7 +34,13 @@ public class ConversationShortcutUpdateJob extends BaseJob {

public static final String KEY = "ConversationShortcutUpdateJob";

public ConversationShortcutUpdateJob() {
public static void enqueue() {
if (Build.VERSION.SDK_INT >= CONVERSATION_SUPPORT_VERSION) {
ApplicationDependencies.getJobManager().add(new ConversationShortcutUpdateJob());
}
}

private ConversationShortcutUpdateJob() {
this(new Parameters.Builder()
.setQueue("ConversationShortcutUpdateJob")
.setLifespan(TimeUnit.MINUTES.toMillis(15))
Expand All @@ -52,7 +63,7 @@ private ConversationShortcutUpdateJob(@NonNull Parameters parameters) {
}

@Override
@RequiresApi(ConversationUtil.CONVERSATION_SUPPORT_VERSION)
@RequiresApi(CONVERSATION_SUPPORT_VERSION)
protected void onRun() throws Exception {
if (TextSecurePreferences.isScreenLockEnabled(context)) {
Log.i(TAG, "Screen lock enabled. Clearing shortcuts.");
Expand Down
Expand Up @@ -57,6 +57,7 @@
import org.thoughtcrime.securesms.service.KeyCachingService;
import org.thoughtcrime.securesms.storage.StorageSyncHelper;
import org.thoughtcrime.securesms.util.CommunicationActions;
import org.thoughtcrime.securesms.util.ConversationUtil;
import org.thoughtcrime.securesms.util.FeatureFlags;
import org.thoughtcrime.securesms.util.ServiceUtil;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
Expand Down Expand Up @@ -241,7 +242,7 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
intent.setAction(KeyCachingService.LOCK_TOGGLED_EVENT);
getContext().startService(intent);

ApplicationDependencies.getJobManager().add(new ConversationShortcutUpdateJob());
ConversationUtil.refreshRecipientShortcuts();

return true;
}
Expand Down
Expand Up @@ -58,9 +58,7 @@ private ConversationUtil() {}
* Enqueues a job to update the list of shortcuts.
*/
public static void refreshRecipientShortcuts() {
if (Build.VERSION.SDK_INT >= CONVERSATION_SUPPORT_VERSION) {
ApplicationDependencies.getJobManager().add(new ConversationShortcutUpdateJob());
}
ConversationShortcutUpdateJob.enqueue();
}

/**
Expand Down

0 comments on commit dc6045c

Please sign in to comment.