11package com .fox2code .mmm .background ;
22
3+ import android .Manifest ;
34import android .app .PendingIntent ;
45import android .content .Context ;
56import android .content .Intent ;
6- import android .os . Build ;
7+ import android .content . pm . PackageManager ;
78
89import androidx .annotation .NonNull ;
10+ import androidx .core .app .ActivityCompat ;
911import androidx .core .app .NotificationChannelCompat ;
1012import androidx .core .app .NotificationCompat ;
1113import androidx .core .app .NotificationManagerCompat ;
3133import java .util .concurrent .TimeUnit ;
3234
3335public class BackgroundUpdateChecker extends Worker {
34- private static boolean easterEggActive = false ;
35- static final Object lock = new Object (); // Avoid concurrency issues
3636 public static final String NOTIFICATION_CHANNEL_ID = "background_update" ;
3737 public static final int NOTIFICATION_ID = 1 ;
38+ static final Object lock = new Object (); // Avoid concurrency issues
39+ private static boolean easterEggActive = false ;
3840
39- public BackgroundUpdateChecker (@ NonNull Context context ,
40- @ NonNull WorkerParameters workerParams ) {
41+ public BackgroundUpdateChecker (@ NonNull Context context , @ NonNull WorkerParameters workerParams ) {
4142 super (context , workerParams );
4243 }
4344
44- @ NonNull
45- @ Override
46- public Result doWork () {
47- if (!NotificationManagerCompat .from (this .getApplicationContext ()).areNotificationsEnabled ()
48- || !MainApplication .isBackgroundUpdateCheckEnabled ()) return Result .success ();
49- synchronized (lock ) {
50- doCheck (this .getApplicationContext ());
51- }
52- return Result .success ();
53- }
54-
5545 static void doCheck (Context context ) {
5646 Thread .currentThread ().setPriority (2 );
5747 ModuleManager .getINSTANCE ().scanAsync ();
5848 RepoManager .getINSTANCE ().update (null );
5949 ModuleManager .getINSTANCE ().runAfterScan (() -> {
6050 int moduleUpdateCount = 0 ;
61- HashMap <String , RepoModule > repoModules =
62- RepoManager .getINSTANCE ().getModules ();
63- for (LocalModuleInfo localModuleInfo :
64- ModuleManager .getINSTANCE ().getModules ().values ()) {
65- if ("twrp-keep" .equals (localModuleInfo .id )) continue ;
51+ HashMap <String , RepoModule > repoModules = RepoManager .getINSTANCE ().getModules ();
52+ for (LocalModuleInfo localModuleInfo : ModuleManager .getINSTANCE ().getModules ().values ()) {
53+ if ("twrp-keep" .equals (localModuleInfo .id ))
54+ continue ;
6655 RepoModule repoModule = repoModules .get (localModuleInfo .id );
6756 localModuleInfo .checkModuleUpdate ();
68- if (localModuleInfo .updateVersionCode > localModuleInfo .versionCode &&
69- !PropUtils .isNullString (localModuleInfo .updateVersion )) {
57+ if (localModuleInfo .updateVersionCode > localModuleInfo .versionCode && !PropUtils .isNullString (localModuleInfo .updateVersion )) {
7058 moduleUpdateCount ++;
71- } else if (repoModule != null &&
72- repoModule .moduleInfo .versionCode > localModuleInfo .versionCode &&
73- !PropUtils .isNullString (repoModule .moduleInfo .version )) {
59+ } else if (repoModule != null && repoModule .moduleInfo .versionCode > localModuleInfo .versionCode && !PropUtils .isNullString (repoModule .moduleInfo .version )) {
7460 moduleUpdateCount ++;
7561 }
7662 }
@@ -81,43 +67,39 @@ static void doCheck(Context context) {
8167 }
8268
8369 public static void postNotification (Context context , int updateCount ) {
84- if (!easterEggActive ) easterEggActive = new Random ().nextInt (100 ) <= updateCount ;
85- NotificationCompat .Builder builder = new NotificationCompat .Builder (
86- context , NOTIFICATION_CHANNEL_ID )
87- .setContentTitle (context .getString (easterEggActive ?
88- R .string .notification_update_title_easter_egg :
89- R .string .notification_update_title )
90- .replace ("%i" , String .valueOf (updateCount )))
91- .setContentText (context .getString (R .string .notification_update_subtitle ))
92- .setSmallIcon (R .drawable .ic_baseline_extension_24 )
93- .setPriority (NotificationCompat .PRIORITY_HIGH )
94- .setContentIntent (PendingIntent .getActivity (context , 0 ,
95- new Intent (context , MainActivity .class ).setFlags (
96- Intent .FLAG_ACTIVITY_NEW_TASK | Intent .FLAG_ACTIVITY_CLEAR_TASK ),
97- Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ?
98- PendingIntent .FLAG_IMMUTABLE : 0 )).setAutoCancel (true );
70+ if (!easterEggActive )
71+ easterEggActive = new Random ().nextInt (100 ) <= updateCount ;
72+ NotificationCompat .Builder builder = new NotificationCompat .Builder (context , NOTIFICATION_CHANNEL_ID ).setContentTitle (context .getString (easterEggActive ? R .string .notification_update_title_easter_egg : R .string .notification_update_title ).replace ("%i" , String .valueOf (updateCount ))).setContentText (context .getString (R .string .notification_update_subtitle )).setSmallIcon (R .drawable .ic_baseline_extension_24 ).setPriority (NotificationCompat .PRIORITY_HIGH ).setContentIntent (PendingIntent .getActivity (context , 0 , new Intent (context , MainActivity .class ).setFlags (Intent .FLAG_ACTIVITY_NEW_TASK | Intent .FLAG_ACTIVITY_CLEAR_TASK ), PendingIntent .FLAG_IMMUTABLE )).setAutoCancel (true );
73+ if (ActivityCompat .checkSelfPermission (MainApplication .getINSTANCE (), Manifest .permission .POST_NOTIFICATIONS ) != PackageManager .PERMISSION_GRANTED ) {
74+ return ;
75+ }
9976 NotificationManagerCompat .from (context ).notify (NOTIFICATION_ID , builder .build ());
10077 }
10178
10279 public static void onMainActivityCreate (Context context ) {
103- NotificationManagerCompat notificationManagerCompat =
104- NotificationManagerCompat .from (context );
105- notificationManagerCompat .createNotificationChannel (
106- new NotificationChannelCompat .Builder (NOTIFICATION_CHANNEL_ID ,
107- NotificationManagerCompat .IMPORTANCE_HIGH ).setShowBadge (true )
108- .setName (context .getString (R .string .notification_update_pref )).build ());
80+ // Refuse to run if first_launch pref is not false
81+ if (MainApplication .getSharedPreferences ().getBoolean ("first_launch" , true ))
82+ return ;
83+ NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat .from (context );
84+ notificationManagerCompat .createNotificationChannel (new NotificationChannelCompat .Builder (NOTIFICATION_CHANNEL_ID , NotificationManagerCompat .IMPORTANCE_HIGH ).setShowBadge (true ).setName (context .getString (R .string .notification_update_pref )).build ());
10985 notificationManagerCompat .cancel (BackgroundUpdateChecker .NOTIFICATION_ID );
11086 BackgroundUpdateChecker .easterEggActive = false ;
111- WorkManager .getInstance (context ).enqueueUniquePeriodicWork ("background_checker" ,
112- ExistingPeriodicWorkPolicy .REPLACE , new PeriodicWorkRequest .Builder (
113- BackgroundUpdateChecker .class , 6 , TimeUnit .HOURS )
114- .setConstraints (new Constraints .Builder ().setRequiresBatteryNotLow (true )
115- .setRequiredNetworkType (NetworkType .UNMETERED ).build ()).build ());
87+ WorkManager .getInstance (context ).enqueueUniquePeriodicWork ("background_checker" , ExistingPeriodicWorkPolicy .REPLACE , new PeriodicWorkRequest .Builder (BackgroundUpdateChecker .class , 6 , TimeUnit .HOURS ).setConstraints (new Constraints .Builder ().setRequiresBatteryNotLow (true ).setRequiredNetworkType (NetworkType .UNMETERED ).build ()).build ());
11688 }
11789
11890 public static void onMainActivityResume (Context context ) {
119- NotificationManagerCompat .from (context ).cancel (
120- BackgroundUpdateChecker .NOTIFICATION_ID );
91+ NotificationManagerCompat .from (context ).cancel (BackgroundUpdateChecker .NOTIFICATION_ID );
12192 BackgroundUpdateChecker .easterEggActive = false ;
12293 }
94+
95+ @ NonNull
96+ @ Override
97+ public Result doWork () {
98+ if (!NotificationManagerCompat .from (this .getApplicationContext ()).areNotificationsEnabled () || !MainApplication .isBackgroundUpdateCheckEnabled ())
99+ return Result .success ();
100+ synchronized (lock ) {
101+ doCheck (this .getApplicationContext ());
102+ }
103+ return Result .success ();
104+ }
123105}
0 commit comments