diff options
6 files changed, 72 insertions, 15 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/broadcastreceivers/WallpaperChangeReceiver.java b/app/src/main/java/ml/docilealligator/infinityforreddit/broadcastreceivers/WallpaperChangeReceiver.java index f994b1a9..5a596308 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/broadcastreceivers/WallpaperChangeReceiver.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/broadcastreceivers/WallpaperChangeReceiver.java @@ -4,6 +4,8 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import androidx.core.content.ContextCompat; + import ml.docilealligator.infinityforreddit.services.MaterialYouService; public class WallpaperChangeReceiver extends BroadcastReceiver { @@ -11,6 +13,6 @@ public class WallpaperChangeReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent materialYouIntent = new Intent(context, MaterialYouService.class); - context.startService(materialYouIntent); + ContextCompat.startForegroundService(context, materialYouIntent); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/services/MaterialYouService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/services/MaterialYouService.java index 7fc764f8..583686fa 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/services/MaterialYouService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/services/MaterialYouService.java @@ -1,9 +1,18 @@ package ml.docilealligator.infinityforreddit.services; -import android.app.IntentService; +import android.app.Notification; +import android.app.NotificationChannel; +import android.app.NotificationManager; +import android.app.Service; import android.content.Intent; import android.content.SharedPreferences; +import android.os.Build; import android.os.Handler; +import android.os.IBinder; + +import androidx.annotation.Nullable; +import androidx.core.app.NotificationCompat; +import androidx.core.app.NotificationManagerCompat; import java.util.concurrent.Executor; @@ -11,12 +20,14 @@ import javax.inject.Inject; import javax.inject.Named; import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.utils.MaterialYouUtils; +import ml.docilealligator.infinityforreddit.utils.NotificationUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; -public class MaterialYouService extends IntentService { +public class MaterialYouService extends Service { @Inject @Named("default") @@ -38,16 +49,44 @@ public class MaterialYouService extends IntentService { Executor executor; public MaterialYouService() { - super("MaterialYouService"); + } + + @Nullable + @Override + public IBinder onBind(Intent intent) { + return null; } @Override - protected void onHandleIntent(Intent intent) { + public int onStartCommand(Intent intent, int flags, int startId) { ((Infinity) getApplication()).getAppComponent().inject(this); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationChannel serviceChannel = new NotificationChannel( + NotificationUtils.CHANNEL_ID_MATERIAL_YOU, + NotificationUtils.CHANNEL_MATERIAL_YOU, + NotificationManager.IMPORTANCE_LOW + ); + + NotificationManagerCompat manager = NotificationManagerCompat.from(this); + manager.createNotificationChannel(serviceChannel); + } + Notification notification = new NotificationCompat.Builder(this, NotificationUtils.CHANNEL_ID_MATERIAL_YOU) + .setContentTitle(getString(R.string.material_you_notification_title)) + .setContentText(getString(R.string.please_wait)) + .setSmallIcon(R.drawable.ic_notification) + .setColor(customThemeWrapper.getColorPrimaryLightTheme()) + .build(); + startForeground(NotificationUtils.MATERIAL_YOU_NOTIFICATION_ID, notification); + if (mSharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_MATERIAL_YOU, false)) { MaterialYouUtils.changeTheme(this, executor, new Handler(), redditDataRoomDatabase, customThemeWrapper, lightThemeSharedPreferences, darkThemeSharedPreferences, - amoledThemeSharedPreferences); + amoledThemeSharedPreferences, () -> { + stopForeground(true); + stopSelf(); + }); } + + return START_NOT_STICKY; } }
\ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/ThemePreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/ThemePreferenceFragment.java index 9dafc175..0e0a8c7d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/ThemePreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/ThemePreferenceFragment.java @@ -1,5 +1,10 @@ package ml.docilealligator.infinityforreddit.settings; +import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY; +import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM; +import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO; +import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES; + import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; @@ -38,11 +43,6 @@ import ml.docilealligator.infinityforreddit.utils.CustomThemeSharedPreferencesUt import ml.docilealligator.infinityforreddit.utils.MaterialYouUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; -import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY; -import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM; -import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO; -import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES; - /** * A simple {@link Fragment} subclass. */ @@ -182,7 +182,7 @@ public class ThemePreferenceFragment extends PreferenceFragmentCompat { MaterialYouUtils.changeTheme(activity, executor, new Handler(), redditDataRoomDatabase, customThemeWrapper, lightThemeSharedPreferences, darkThemeSharedPreferences, - amoledThemeSharedPreferences); + amoledThemeSharedPreferences, null); applyMaterialYouPreference.setVisible(true); } else { applyMaterialYouPreference.setVisible(false); @@ -194,7 +194,7 @@ public class ThemePreferenceFragment extends PreferenceFragmentCompat { MaterialYouUtils.changeTheme(activity, executor, new Handler(), redditDataRoomDatabase, customThemeWrapper, lightThemeSharedPreferences, darkThemeSharedPreferences, - amoledThemeSharedPreferences); + amoledThemeSharedPreferences, null); return true; }); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/MaterialYouUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/MaterialYouUtils.java index 44ff8f9f..d470e1d0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/MaterialYouUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/MaterialYouUtils.java @@ -9,6 +9,7 @@ import android.os.Build; import android.os.Handler; import androidx.annotation.ColorInt; +import androidx.annotation.Nullable; import org.greenrobot.eventbus.EventBus; @@ -44,7 +45,8 @@ public class MaterialYouUtils { CustomThemeWrapper customThemeWrapper, SharedPreferences lightThemeSharedPreferences, SharedPreferences darkThemeSharedPreferences, - SharedPreferences amoledThemeSharedPreferences) { + SharedPreferences amoledThemeSharedPreferences, + @Nullable MaterialYouListener materialYouListener) { executor.execute(() -> { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { WallpaperManager wallpaperManager = WallpaperManager.getInstance(context); @@ -116,7 +118,12 @@ public class MaterialYouUtils { CustomThemeSharedPreferencesUtils.insertThemeToSharedPreferences(darkTheme, darkThemeSharedPreferences); CustomThemeSharedPreferencesUtils.insertThemeToSharedPreferences(amoledTheme, amoledThemeSharedPreferences); - handler.post(() -> EventBus.getDefault().post(new RecreateActivityEvent())); + handler.post(() -> { + if (materialYouListener != null) { + materialYouListener.applied(); + } + EventBus.getDefault().post(new RecreateActivityEvent()); + }); } } }); @@ -141,4 +148,8 @@ public class MaterialYouUtils { double luminance = 1 - (0.299 * Color.red(color) + 0.587 * Color.green(color) + 0.114 * Color.blue(color)) / 255; return luminance < 0.5 ? Color.BLACK : Color.WHITE; } + + public interface MaterialYouListener { + void applied(); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/NotificationUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/NotificationUtils.java index 3c3255de..5552271f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/NotificationUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/NotificationUtils.java @@ -22,11 +22,14 @@ public class NotificationUtils { public static final String CHANNEL_DOWNLOAD_IMAGE = "Download Image"; public static final String CHANNEL_ID_DOWNLOAD_GIF = "download_gif"; public static final String CHANNEL_DOWNLOAD_GIF = "Download Gif"; + public static final String CHANNEL_ID_MATERIAL_YOU = "material_you"; + public static final String CHANNEL_MATERIAL_YOU = "Material You"; public static final int SUBMIT_POST_SERVICE_NOTIFICATION_ID = 10000; public static final int DOWNLOAD_REDDIT_VIDEO_NOTIFICATION_ID = 20000; public static final int DOWNLOAD_VIDEO_NOTIFICATION_ID = 30000; public static final int DOWNLOAD_IMAGE_NOTIFICATION_ID = 40000; public static final int DOWNLOAD_GIF_NOTIFICATION_ID = 50000; + public static final int MATERIAL_YOU_NOTIFICATION_ID = 60000; private static final int SUMMARY_BASE_ID_UNREAD_MESSAGE = 0; private static final int NOTIFICATION_BASE_ID_UNREAD_MESSAGE = 1; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 30b81fd4..847457e2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1162,4 +1162,6 @@ <string name="error_loading_wiki">Error loading wiki</string> <string name="no_wiki">This subreddit has no wiki page</string> + <string name="material_you_notification_title">Applying Material You</string> + </resources> |