mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-22 01:40:07 +01:00
Prepare the websocket keepalive for API 31.
This commit is contained in:
@@ -2,10 +2,12 @@ package org.thoughtcrime.securesms.util;
|
||||
|
||||
import android.app.AlarmManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.app.Service;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.os.Build;
|
||||
import android.os.SystemClock;
|
||||
|
||||
import androidx.core.app.AlarmManagerCompat;
|
||||
@@ -18,13 +20,12 @@ import org.whispersystems.signalservice.api.util.SleepTimer;
|
||||
import java.util.concurrent.ConcurrentSkipListSet;
|
||||
|
||||
/**
|
||||
* A sleep timer that is based on elapsed realtime, so
|
||||
* that it works properly, even in low-power sleep modes.
|
||||
*
|
||||
* A sleep timer that is based on elapsed realtime, so that it works properly, even in low-power sleep modes.
|
||||
*/
|
||||
public class AlarmSleepTimer implements SleepTimer {
|
||||
private static final String TAG = Log.tag(AlarmSleepTimer.class);
|
||||
private static ConcurrentSkipListSet<Integer> actionIdList = new ConcurrentSkipListSet<>();
|
||||
|
||||
private static final ConcurrentSkipListSet<Integer> actionIdList = new ConcurrentSkipListSet<>();
|
||||
|
||||
private final Context context;
|
||||
|
||||
@@ -33,23 +34,25 @@ public class AlarmSleepTimer implements SleepTimer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sleep(long millis) {
|
||||
final AlarmReceiver alarmReceiver = new AlarmSleepTimer.AlarmReceiver();
|
||||
int actionId = 0;
|
||||
public void sleep(long sleepDuration) {
|
||||
AlarmReceiver alarmReceiver = new AlarmSleepTimer.AlarmReceiver();
|
||||
int actionId = 0;
|
||||
|
||||
while (!actionIdList.add(actionId)){
|
||||
actionId++;
|
||||
}
|
||||
|
||||
try {
|
||||
context.registerReceiver(alarmReceiver,
|
||||
new IntentFilter(AlarmReceiver.WAKE_UP_THREAD_ACTION + "." + actionId));
|
||||
String actionName = buildActionName(actionId);
|
||||
context.registerReceiver(alarmReceiver, new IntentFilter(actionName));
|
||||
|
||||
final long startTime = System.currentTimeMillis();
|
||||
alarmReceiver.setAlarm(millis, AlarmReceiver.WAKE_UP_THREAD_ACTION + "." + actionId);
|
||||
long startTime = System.currentTimeMillis();
|
||||
alarmReceiver.setAlarm(sleepDuration, actionName);
|
||||
|
||||
while (System.currentTimeMillis() - startTime < millis) {
|
||||
while (System.currentTimeMillis() - startTime < sleepDuration) {
|
||||
try {
|
||||
synchronized (this) {
|
||||
wait(millis - System.currentTimeMillis() + startTime);
|
||||
wait(sleepDuration - (System.currentTimeMillis() - startTime));
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
Log.w(TAG, e);
|
||||
@@ -58,25 +61,35 @@ public class AlarmSleepTimer implements SleepTimer {
|
||||
context.unregisterReceiver(alarmReceiver);
|
||||
} catch(Exception e) {
|
||||
Log.w(TAG, "Exception during sleep ...",e);
|
||||
}finally {
|
||||
} finally {
|
||||
actionIdList.remove(actionId);
|
||||
}
|
||||
}
|
||||
|
||||
private static String buildActionName(int actionId) {
|
||||
return AlarmReceiver.WAKE_UP_THREAD_ACTION + "." + actionId;
|
||||
}
|
||||
|
||||
private class AlarmReceiver extends BroadcastReceiver {
|
||||
private static final String WAKE_UP_THREAD_ACTION = "org.thoughtcrime.securesms.util.AlarmSleepTimer.AlarmReceiver.WAKE_UP_THREAD";
|
||||
|
||||
private void setAlarm(long millis, String action) {
|
||||
final Intent intent = new Intent(action);
|
||||
final PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntentFlags.mutable());
|
||||
final AlarmManager alarmManager = ContextCompat.getSystemService(context, AlarmManager.class);
|
||||
final AlarmManager alarmManager = ServiceUtil.getAlarmManager(context);
|
||||
|
||||
Log.w(TAG, "Setting alarm to wake up in " + millis + "ms.");
|
||||
|
||||
AlarmManagerCompat.setExactAndAllowWhileIdle(alarmManager,
|
||||
AlarmManager.ELAPSED_REALTIME_WAKEUP,
|
||||
SystemClock.elapsedRealtime() + millis,
|
||||
pendingIntent);
|
||||
if (Build.VERSION.SDK_INT < 31 || alarmManager.canScheduleExactAlarms()) {
|
||||
Log.d(TAG, "Setting an exact alarm to wake up in " + millis + "ms.");
|
||||
AlarmManagerCompat.setExactAndAllowWhileIdle(alarmManager,
|
||||
AlarmManager.ELAPSED_REALTIME_WAKEUP,
|
||||
SystemClock.elapsedRealtime() + millis,
|
||||
pendingIntent);
|
||||
} else {
|
||||
Log.w(TAG, "Setting an inexact alarm to wake up in " + millis + "ms. CanScheduleAlarms: " + alarmManager.canScheduleExactAlarms());
|
||||
alarmManager.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
|
||||
SystemClock.elapsedRealtime() + millis,
|
||||
pendingIntent);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user