From 30bce5b798750230b050eb564eddd5570655b184 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sun, 4 Jul 2021 08:44:31 +0800 Subject: Fix app crashes when changing a wallpaper. Fix Apply Material You option not working. --- .../broadcastreceivers/WallpaperChangeReceiver.java | 14 ++------------ .../infinityforreddit/services/MaterialYouService.java | 12 +++++++++--- .../settings/ThemePreferenceFragment.java | 15 ++++++--------- 3 files changed, 17 insertions(+), 24 deletions(-) (limited to 'app/src') 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 ae9392f7..f994b1a9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/broadcastreceivers/WallpaperChangeReceiver.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/broadcastreceivers/WallpaperChangeReceiver.java @@ -3,24 +3,14 @@ package ml.docilealligator.infinityforreddit.broadcastreceivers; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; -import android.content.SharedPreferences; - -import javax.inject.Inject; -import javax.inject.Named; import ml.docilealligator.infinityforreddit.services.MaterialYouService; -import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; public class WallpaperChangeReceiver extends BroadcastReceiver { - @Inject - @Named("default") - SharedPreferences mSharedPreferences; @Override public void onReceive(Context context, Intent intent) { - if (mSharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_MATERIAL_YOU, false)) { - Intent materialYouIntent = new Intent(context, MaterialYouService.class); - context.startService(materialYouIntent); - } + Intent materialYouIntent = new Intent(context, MaterialYouService.class); + context.startService(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 221b980b..7fc764f8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/services/MaterialYouService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/services/MaterialYouService.java @@ -14,9 +14,13 @@ import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.utils.MaterialYouUtils; +import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; public class MaterialYouService extends IntentService { + @Inject + @Named("default") + SharedPreferences mSharedPreferences; @Inject @Named("light_theme") SharedPreferences lightThemeSharedPreferences; @@ -40,8 +44,10 @@ public class MaterialYouService extends IntentService { @Override protected void onHandleIntent(Intent intent) { ((Infinity) getApplication()).getAppComponent().inject(this); - MaterialYouUtils.changeTheme(this, executor, new Handler(), redditDataRoomDatabase, - customThemeWrapper, lightThemeSharedPreferences, darkThemeSharedPreferences, - amoledThemeSharedPreferences); + if (mSharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_MATERIAL_YOU, false)) { + MaterialYouUtils.changeTheme(this, executor, new Handler(), redditDataRoomDatabase, + customThemeWrapper, lightThemeSharedPreferences, darkThemeSharedPreferences, + amoledThemeSharedPreferences); + } } } \ 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 2e94f166..9dafc175 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/ThemePreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/ThemePreferenceFragment.java @@ -190,15 +190,12 @@ public class ThemePreferenceFragment extends PreferenceFragmentCompat { return true; }); - applyMaterialYouPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - MaterialYouUtils.changeTheme(activity, executor, new Handler(), - redditDataRoomDatabase, customThemeWrapper, - lightThemeSharedPreferences, darkThemeSharedPreferences, - amoledThemeSharedPreferences); - return true; - } + applyMaterialYouPreference.setOnPreferenceClickListener(preference -> { + MaterialYouUtils.changeTheme(activity, executor, new Handler(), + redditDataRoomDatabase, customThemeWrapper, + lightThemeSharedPreferences, darkThemeSharedPreferences, + amoledThemeSharedPreferences); + return true; }); } -- cgit v1.2.3