That fuck shit the fascists are using
at master 92 lines 3.7 kB view raw
1package org.tm.archive.messageprocessingalarm; 2 3import android.annotation.SuppressLint; 4import android.app.AlarmManager; 5import android.app.PendingIntent; 6import android.content.BroadcastReceiver; 7import android.content.Context; 8import android.content.Intent; 9import android.os.Handler; 10import android.os.Looper; 11import android.os.SystemClock; 12 13import androidx.annotation.NonNull; 14 15import org.signal.core.util.PendingIntentFlags; 16import org.signal.core.util.concurrent.SignalExecutors; 17import org.signal.core.util.logging.Log; 18import org.tm.archive.dependencies.ApplicationDependencies; 19import org.tm.archive.jobmanager.JobTracker; 20import org.tm.archive.jobs.MessageFetchJob; 21import org.tm.archive.util.FeatureFlags; 22 23import java.util.Locale; 24import java.util.Optional; 25 26/** 27 * A receiver that is scheduled via alarm to run at a remotely-configurable interval to fetch messages from the service. 28 * Basically an extra reliability thing that helps poke the system to fetch messages more often. 29 */ 30public final class RoutineMessageFetchReceiver extends BroadcastReceiver { 31 32 private static final String TAG = Log.tag(RoutineMessageFetchReceiver.class); 33 34 public static final String BROADCAST_ACTION = "org.tm.archive.action.PROCESS_MESSAGES"; 35 36 @Override 37 @SuppressLint("StaticFieldLeak") 38 public void onReceive(@NonNull Context context, @NonNull Intent intent) { 39 Log.i(TAG, String.format("onReceive(%s)", intent.getAction())); 40 41 if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) { 42 Log.i(TAG, "Starting Alarm because of boot receiver"); 43 startOrUpdateAlarm(context); 44 } else if (BROADCAST_ACTION.equals(intent.getAction())) { 45 46 if (ApplicationDependencies.getAppForegroundObserver().isForegrounded()) { 47 Log.i(TAG, "App is foregrounded"); 48 return; 49 } 50 51 long foregroundDelayMs = FeatureFlags.getBackgroundMessageProcessForegroundDelay(); 52 long jobTimeout = foregroundDelayMs + 200; 53 54 Log.i(TAG, String.format(Locale.US, "Starting PushNotificationReceiveJob asynchronously with %d delay before foreground shown", foregroundDelayMs)); 55 56 PendingResult pendingResult = goAsync(); 57 58 new Handler(Looper.getMainLooper()).postDelayed(pendingResult::finish, jobTimeout); 59 60 SignalExecutors.BOUNDED.submit(() -> { 61 Log.i(TAG, "Running PushNotificationReceiveJob"); 62 63 Optional<JobTracker.JobState> jobState = ApplicationDependencies.getJobManager() 64 .runSynchronously(new MessageFetchJob(), jobTimeout); 65 66 Log.i(TAG, "PushNotificationReceiveJob ended: " + (jobState.isPresent() ? jobState.get().toString() : "Job did not complete")); 67 }); 68 } 69 } 70 71 public static void startOrUpdateAlarm(@NonNull Context context) { 72 Intent alarmIntent = new Intent(context, RoutineMessageFetchReceiver.class); 73 74 alarmIntent.setAction(RoutineMessageFetchReceiver.BROADCAST_ACTION); 75 76 PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 123, alarmIntent, PendingIntentFlags.updateCurrent()); 77 AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 78 79 long interval = FeatureFlags.getBackgroundMessageProcessInterval(); 80 81 if (interval < 0) { 82 alarmManager.cancel(pendingIntent); 83 Log.i(TAG, "Alarm cancelled"); 84 } else { 85 alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 86 SystemClock.elapsedRealtime() + interval, 87 interval, 88 pendingIntent); 89 Log.i(TAG, "Alarm scheduled to repeat at interval " + interval); 90 } 91 } 92}