|
| 1 | +package com.fox2code.mmm.background; |
| 2 | + |
| 3 | +import android.app.PendingIntent; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.Intent; |
| 6 | +import android.os.Build; |
| 7 | + |
| 8 | +import androidx.annotation.NonNull; |
| 9 | +import androidx.core.app.NotificationChannelCompat; |
| 10 | +import androidx.core.app.NotificationCompat; |
| 11 | +import androidx.core.app.NotificationManagerCompat; |
| 12 | +import androidx.work.Constraints; |
| 13 | +import androidx.work.ExistingPeriodicWorkPolicy; |
| 14 | +import androidx.work.NetworkType; |
| 15 | +import androidx.work.PeriodicWorkRequest; |
| 16 | +import androidx.work.WorkManager; |
| 17 | +import androidx.work.Worker; |
| 18 | +import androidx.work.WorkerParameters; |
| 19 | + |
| 20 | +import com.fox2code.mmm.MainActivity; |
| 21 | +import com.fox2code.mmm.MainApplication; |
| 22 | +import com.fox2code.mmm.R; |
| 23 | +import com.fox2code.mmm.manager.LocalModuleInfo; |
| 24 | +import com.fox2code.mmm.manager.ModuleManager; |
| 25 | +import com.fox2code.mmm.repo.RepoManager; |
| 26 | +import com.fox2code.mmm.repo.RepoModule; |
| 27 | + |
| 28 | +import java.util.Random; |
| 29 | +import java.util.concurrent.TimeUnit; |
| 30 | + |
| 31 | +public class BackgroundUpdateChecker extends Worker { |
| 32 | + private static boolean easterEggActive = false; |
| 33 | + public static final String NOTIFICATION_CHANNEL_ID = "background_update"; |
| 34 | + public static final int NOTIFICATION_ID = 1; |
| 35 | + |
| 36 | + public BackgroundUpdateChecker(@NonNull Context context, |
| 37 | + @NonNull WorkerParameters workerParams) { |
| 38 | + super(context, workerParams); |
| 39 | + } |
| 40 | + |
| 41 | + @NonNull |
| 42 | + @Override |
| 43 | + public Result doWork() { |
| 44 | + if (!NotificationManagerCompat.from(this.getApplicationContext()).areNotificationsEnabled() |
| 45 | + || !MainApplication.isBackgroundUpdateCheckEnabled()) return Result.success(); |
| 46 | + |
| 47 | + doCheck(this.getApplicationContext()); |
| 48 | + |
| 49 | + return Result.success(); |
| 50 | + } |
| 51 | + |
| 52 | + static void doCheck(Context context) { |
| 53 | + Thread.currentThread().setPriority(Thread.MIN_PRIORITY); |
| 54 | + RepoManager.getINSTANCE().update(null); |
| 55 | + ModuleManager.getINSTANCE().scan(); |
| 56 | + ModuleManager.getINSTANCE().scan(); |
| 57 | + int moduleUpdateCount = 0; |
| 58 | + for (LocalModuleInfo localModuleInfo : |
| 59 | + ModuleManager.getINSTANCE().getModules().values()) { |
| 60 | + RepoModule repoModule = RepoManager.getINSTANCE() |
| 61 | + .getModules().get(localModuleInfo.id); |
| 62 | + localModuleInfo.checkModuleUpdate(); |
| 63 | + if (localModuleInfo.updateVersionCode > localModuleInfo.versionCode) { |
| 64 | + moduleUpdateCount++; |
| 65 | + } else if (repoModule != null && |
| 66 | + repoModule.moduleInfo.versionCode > localModuleInfo.versionCode) { |
| 67 | + moduleUpdateCount++; |
| 68 | + } |
| 69 | + } |
| 70 | + if (moduleUpdateCount != 0) { |
| 71 | + postNotification(context, moduleUpdateCount); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public static void postNotification(Context context, int updateCount) { |
| 76 | + if (!easterEggActive) easterEggActive = new Random().nextInt(100) <= updateCount; |
| 77 | + NotificationCompat.Builder builder = new NotificationCompat.Builder( |
| 78 | + context, NOTIFICATION_CHANNEL_ID) |
| 79 | + .setContentTitle(context.getString(easterEggActive ? |
| 80 | + R.string.notification_update_title_easter_egg : |
| 81 | + R.string.notification_update_title) |
| 82 | + .replace("%i", String.valueOf(updateCount))) |
| 83 | + .setContentText(context.getString(R.string.notification_update_subtitle)) |
| 84 | + .setSmallIcon(R.drawable.ic_baseline_extension_24) |
| 85 | + .setPriority(NotificationCompat.PRIORITY_HIGH) |
| 86 | + .setContentIntent(PendingIntent.getActivity(context, 0, |
| 87 | + new Intent(context, MainActivity.class).setFlags( |
| 88 | + Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK), |
| 89 | + Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? |
| 90 | + PendingIntent.FLAG_IMMUTABLE : 0)).setAutoCancel(true); |
| 91 | + NotificationManagerCompat.from(context).notify(NOTIFICATION_ID, builder.build()); |
| 92 | + } |
| 93 | + |
| 94 | + public static void onMainActivityCreate(Context context) { |
| 95 | + NotificationManagerCompat notificationManagerCompat = |
| 96 | + NotificationManagerCompat.from(context); |
| 97 | + notificationManagerCompat.createNotificationChannel( |
| 98 | + new NotificationChannelCompat.Builder(NOTIFICATION_CHANNEL_ID, |
| 99 | + NotificationManagerCompat.IMPORTANCE_HIGH).setShowBadge(true) |
| 100 | + .setName(context.getString(R.string.notification_update_pref)).build()); |
| 101 | + notificationManagerCompat.cancel(BackgroundUpdateChecker.NOTIFICATION_ID); |
| 102 | + BackgroundUpdateChecker.easterEggActive = false; |
| 103 | + WorkManager.getInstance(context).enqueueUniquePeriodicWork("background_checker", |
| 104 | + ExistingPeriodicWorkPolicy.REPLACE, new PeriodicWorkRequest.Builder( |
| 105 | + BackgroundUpdateChecker.class, 6, TimeUnit.HOURS) |
| 106 | + .setConstraints(new Constraints.Builder().setRequiresBatteryNotLow(true) |
| 107 | + .setRequiredNetworkType(NetworkType.UNMETERED).build()).build()); |
| 108 | + } |
| 109 | + |
| 110 | + public static void onMainActivityResume(Context context) { |
| 111 | + NotificationManagerCompat.from(context).cancel( |
| 112 | + BackgroundUpdateChecker.NOTIFICATION_ID); |
| 113 | + BackgroundUpdateChecker.easterEggActive = false; |
| 114 | + } |
| 115 | +} |
0 commit comments