Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Periodic alarm to check for messages.
- Loading branch information
1 parent
29d66f2
commit 98cb6b4
Showing
14 changed files
with
349 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...c/main/java/org/thoughtcrime/securesms/messageprocessingalarm/MessageProcessReceiver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package org.thoughtcrime.securesms.messageprocessingalarm; | ||
|
||
import android.annotation.SuppressLint; | ||
import android.app.AlarmManager; | ||
import android.app.PendingIntent; | ||
import android.content.BroadcastReceiver; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.os.SystemClock; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import org.signal.core.util.concurrent.SignalExecutors; | ||
import org.signal.core.util.logging.Log; | ||
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies; | ||
import org.thoughtcrime.securesms.jobmanager.JobTracker; | ||
import org.thoughtcrime.securesms.jobs.PushNotificationReceiveJob; | ||
import org.thoughtcrime.securesms.util.FeatureFlags; | ||
import org.whispersystems.libsignal.util.guava.Optional; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* On received message, runs a job to poll for messages. | ||
*/ | ||
public final class MessageProcessReceiver extends BroadcastReceiver { | ||
|
||
private static final String TAG = Log.tag(MessageProcessReceiver.class); | ||
|
||
private static final long FIRST_RUN_DELAY = TimeUnit.MINUTES.toMillis(3); | ||
private static final long FOREGROUND_DELAY = TimeUnit.SECONDS.toMillis(2); | ||
private static final long JOB_TIMEOUT = TimeUnit.SECONDS.toMillis(5); | ||
|
||
public static final String BROADCAST_ACTION = "org.thoughtcrime.securesms.action.PROCESS_MESSAGES"; | ||
|
||
@Override | ||
@SuppressLint("StaticFieldLeak") | ||
public void onReceive(@NonNull Context context, @NonNull Intent intent) { | ||
Log.i(TAG, String.format("onReceive(%s)", intent.getAction())); | ||
|
||
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) { | ||
Log.i(TAG, "Starting Alarm because of boot receiver"); | ||
startOrUpdateAlarm(context); | ||
} else if (BROADCAST_ACTION.equals(intent.getAction())) { | ||
PendingResult pendingResult = goAsync(); | ||
|
||
SignalExecutors.BOUNDED.submit(() -> { | ||
Log.i(TAG, "Running PushNotificationReceiveJob"); | ||
|
||
Optional<JobTracker.JobState> jobState = ApplicationDependencies.getJobManager() | ||
.runSynchronously(PushNotificationReceiveJob.withDelayedForegroundService(FOREGROUND_DELAY), JOB_TIMEOUT); | ||
|
||
Log.i(TAG, "PushNotificationReceiveJob ended: " + (jobState.isPresent() ? jobState.get().toString() : "Job did not complete")); | ||
|
||
pendingResult.finish(); | ||
}); | ||
} | ||
} | ||
|
||
public static void startOrUpdateAlarm(@NonNull Context context) { | ||
Intent alarmIntent = new Intent(context, MessageProcessReceiver.class); | ||
|
||
alarmIntent.setAction(MessageProcessReceiver.BROADCAST_ACTION); | ||
|
||
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 123, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT); | ||
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); | ||
|
||
long interval = FeatureFlags.getBackgroundMessageProcessDelay(); | ||
|
||
if (interval < 0) { | ||
alarmManager.cancel(pendingIntent); | ||
Log.i(TAG, "Alarm cancelled"); | ||
} else { | ||
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, | ||
SystemClock.elapsedRealtime() + FIRST_RUN_DELAY, | ||
interval, | ||
pendingIntent); | ||
Log.i(TAG, "Alarm scheduled to repeat at interval " + interval); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,8 +8,11 @@ | |
|
||
import org.signal.core.util.logging.Log; | ||
import org.thoughtcrime.securesms.ApplicationContext; | ||
import org.thoughtcrime.securesms.R; | ||
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies; | ||
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint; | ||
import org.thoughtcrime.securesms.service.DelayedNotificationController; | ||
import org.thoughtcrime.securesms.service.GenericForegroundService; | ||
import org.thoughtcrime.securesms.util.PowerManagerCompat; | ||
import org.thoughtcrime.securesms.util.ServiceUtil; | ||
import org.thoughtcrime.securesms.util.TextSecurePreferences; | ||
|
@@ -31,11 +34,21 @@ public class BackgroundMessageRetriever { | |
|
||
private static final long NORMAL_TIMEOUT = TimeUnit.SECONDS.toMillis(10); | ||
|
||
public static final long DO_NOT_SHOW_IN_FOREGROUND = DelayedNotificationController.DO_NOT_SHOW; | ||
|
||
/** | ||
* @return False if the retrieval failed and should be rescheduled, otherwise true. | ||
*/ | ||
@WorkerThread | ||
public boolean retrieveMessages(@NonNull Context context, MessageRetrievalStrategy... strategies) { | ||
return retrieveMessages(context, DO_NOT_SHOW_IN_FOREGROUND, strategies); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
greyson-signal
Contributor
|
||
} | ||
|
||
/** | ||
* @return False if the retrieval failed and should be rescheduled, otherwise true. | ||
*/ | ||
@WorkerThread | ||
public boolean retrieveMessages(@NonNull Context context, long showNotificationAfterMs, MessageRetrievalStrategy... strategies) { | ||
if (shouldIgnoreFetch(context)) { | ||
Log.i(TAG, "Skipping retrieval -- app is in the foreground."); | ||
return true; | ||
|
@@ -47,27 +60,29 @@ public boolean retrieveMessages(@NonNull Context context, MessageRetrievalStrate | |
} | ||
|
||
synchronized (this) { | ||
PowerManager.WakeLock wakeLock = null; | ||
try (DelayedNotificationController controller = GenericForegroundService.startForegroundTaskDelayed(context, context.getString(R.string.BackgroundMessageRetriever_checking_for_messages), showNotificationAfterMs)) { | ||
PowerManager.WakeLock wakeLock = null; | ||
|
||
try { | ||
wakeLock = WakeLockUtil.acquire(context, PowerManager.PARTIAL_WAKE_LOCK, TimeUnit.SECONDS.toMillis(60), WAKE_LOCK_TAG); | ||
try { | ||
wakeLock = WakeLockUtil.acquire(context, PowerManager.PARTIAL_WAKE_LOCK, TimeUnit.SECONDS.toMillis(60), WAKE_LOCK_TAG); | ||
|
||
TextSecurePreferences.setNeedsMessagePull(context, true); | ||
TextSecurePreferences.setNeedsMessagePull(context, true); | ||
|
||
long startTime = System.currentTimeMillis(); | ||
PowerManager powerManager = ServiceUtil.getPowerManager(context); | ||
boolean doze = PowerManagerCompat.isDeviceIdleMode(powerManager); | ||
boolean network = new NetworkConstraint.Factory(ApplicationContext.getInstance(context)).create().isMet(); | ||
long startTime = System.currentTimeMillis(); | ||
PowerManager powerManager = ServiceUtil.getPowerManager(context); | ||
boolean doze = PowerManagerCompat.isDeviceIdleMode(powerManager); | ||
boolean network = new NetworkConstraint.Factory(ApplicationContext.getInstance(context)).create().isMet(); | ||
|
||
if (doze || !network) { | ||
Log.w(TAG, "We may be operating in a constrained environment. Doze: " + doze + " Network: " + network); | ||
} | ||
if (doze || !network) { | ||
Log.w(TAG, "We may be operating in a constrained environment. Doze: " + doze + " Network: " + network); | ||
} | ||
|
||
Log.i(TAG, "Performing normal message fetch."); | ||
return executeBackgroundRetrieval(context, startTime, strategies); | ||
} finally { | ||
WakeLockUtil.release(wakeLock, WAKE_LOCK_TAG); | ||
ACTIVE_LOCK.release(); | ||
Log.i(TAG, "Performing normal message fetch."); | ||
return executeBackgroundRetrieval(context, startTime, strategies); | ||
} finally { | ||
WakeLockUtil.release(wakeLock, WAKE_LOCK_TAG); | ||
ACTIVE_LOCK.release(); | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
@cody-signal Hi! I'm not Android programmer, but it seems that
DO_NOT_SHOW_IN_FOREGROUND
value doesn't work as expected in some cases - https://www.reddit.com/r/signal/comments/lo6ub8/checking_messages/Of course maybe I interpreted wrong
foreground
meaning :) but I've found here theBackgroundMessageRetriever_checking_for_messages
and suspect that it is connected.