From dfe2ca80814f14532e8253f807ee1a05c1734607 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Wed, 16 Jun 2021 17:24:17 +0800 Subject: Material You. --- .../adapters/PostRecyclerViewAdapter.java | 3 +- .../settings/ThemePreferenceFragment.java | 113 ++++++++++++++++++++- .../utils/SharedPreferencesUtils.java | 1 + 3 files changed, 113 insertions(+), 4 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java index 5142704b..408a6617 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java @@ -30,6 +30,7 @@ import androidx.constraintlayout.widget.Barrier; import androidx.constraintlayout.widget.ConstraintLayout; import androidx.constraintlayout.widget.ConstraintSet; import androidx.core.graphics.drawable.DrawableCompat; +import androidx.paging.PagedListAdapter; import androidx.paging.PagingDataAdapter; import androidx.recyclerview.widget.DiffUtil; import androidx.recyclerview.widget.ItemTouchHelper; @@ -104,7 +105,7 @@ import retrofit2.Retrofit; * Created by alex on 2/25/18. */ -public class PostRecyclerViewAdapter extends PagingDataAdapter implements CacheManager { +public class PostRecyclerViewAdapter extends PagedListAdapter implements CacheManager { private static final int VIEW_TYPE_POST_CARD_VIDEO_AUTOPLAY_TYPE = 1; private static final int VIEW_TYPE_POST_CARD_VIDEO_AND_GIF_PREVIEW_TYPE = 2; private static final int VIEW_TYPE_POST_CARD_IMAGE_AND_GIF_AUTOPLAY_TYPE = 3; 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 fba59d25..292d7bcf 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/ThemePreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/ThemePreferenceFragment.java @@ -1,11 +1,17 @@ package ml.docilealligator.infinityforreddit.settings; +import android.app.WallpaperColors; +import android.app.WallpaperManager; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.content.res.Configuration; +import android.graphics.Color; import android.os.Build; import android.os.Bundle; +import android.os.Handler; +import androidx.annotation.ColorInt; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatDelegate; @@ -19,16 +25,19 @@ import androidx.preference.SwitchPreference; import org.greenrobot.eventbus.EventBus; +import java.util.concurrent.Executor; + 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.activities.CustomThemeListingActivity; import ml.docilealligator.infinityforreddit.activities.CustomizeThemeActivity; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeViewModel; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.events.RecreateActivityEvent; -import ml.docilealligator.infinityforreddit.Infinity; -import ml.docilealligator.infinityforreddit.R; -import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.utils.CustomThemeSharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; @@ -44,9 +53,20 @@ public class ThemePreferenceFragment extends PreferenceFragmentCompat { private AppCompatActivity activity; @Inject + @Named("light_theme") + SharedPreferences lightThemeSharedPreferences; + @Inject + @Named("dark_theme") + SharedPreferences darkThemeSharedPreferences; + @Inject + @Named("amoled_theme") + SharedPreferences amoledThemeSharedPreferences; + @Inject RedditDataRoomDatabase redditDataRoomDatabase; @Inject CustomThemeWrapper customThemeWrapper; + @Inject + Executor executor; public CustomThemeViewModel customThemeViewModel; @Override @@ -61,6 +81,7 @@ public class ThemePreferenceFragment extends PreferenceFragmentCompat { Preference customizeDarkThemePreference = findPreference(SharedPreferencesUtils.CUSTOMIZE_DARK_THEME); Preference customizeAmoledThemePreference = findPreference(SharedPreferencesUtils.CUSTOMIZE_AMOLED_THEME); Preference selectAndCustomizeThemePreference = findPreference(SharedPreferencesUtils.MANAGE_THEMES); + SwitchPreference enableMaterialYouSwitchPreference = findPreference(SharedPreferencesUtils.ENABLE_MATERIAL_YOU); boolean systemDefault = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q; if (themePreference != null && amoledDarkSwitch != null) { @@ -151,6 +172,70 @@ public class ThemePreferenceFragment extends PreferenceFragmentCompat { }); } + if (enableMaterialYouSwitchPreference != null) { + Handler handler = new Handler(); + enableMaterialYouSwitchPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + if (true) { + executor.execute(() -> { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { + WallpaperManager wallpaperManager = WallpaperManager.getInstance(activity); + WallpaperColors wallpaperColors = wallpaperManager.getWallpaperColors(WallpaperManager.FLAG_SYSTEM); + + if (wallpaperColors != null) { + int colorPrimaryInt = shiftColorTo255(wallpaperColors.getPrimaryColor().toArgb(), 0.4); + int colorPrimaryDarkInt = shiftColorTo0(colorPrimaryInt, 0.4); + int backgroundColor = shiftColorTo255(colorPrimaryInt, 0.6); + int cardViewBackgroundColor = shiftColorTo255(colorPrimaryInt, 0.9); + Color colorAccent = wallpaperColors.getSecondaryColor(); + int colorAccentInt = shiftColorTo255(colorAccent == null ? customThemeWrapper.getColorAccent() : colorAccent.toArgb(), 0.4); + + int colorPrimaryAppropriateTextColor = getAppropriateTextColor(colorPrimaryInt); + int backgroundColorAppropriateTextColor = getAppropriateTextColor(backgroundColor); + + lightThemeSharedPreferences.edit().putInt(CustomThemeSharedPreferencesUtils.COLOR_PRIMARY, colorPrimaryInt) + .putInt(CustomThemeSharedPreferencesUtils.COLOR_PRIMARY_DARK, colorPrimaryDarkInt) + .putInt(CustomThemeSharedPreferencesUtils.COLOR_ACCENT, colorAccentInt) + .putInt(CustomThemeSharedPreferencesUtils.COLOR_PRIMARY_LIGHT_THEME, colorPrimaryInt) + .putInt(CustomThemeSharedPreferencesUtils.BACKGROUND_COLOR, backgroundColor) + .putInt(CustomThemeSharedPreferencesUtils.CARD_VIEW_BACKGROUND_COLOR, cardViewBackgroundColor) + .putInt(CustomThemeSharedPreferencesUtils.BOTTOM_APP_BAR_BACKGROUND_COLOR, colorPrimaryInt) + .putInt(CustomThemeSharedPreferencesUtils.NAV_BAR_COLOR, colorPrimaryInt) + .putInt(CustomThemeSharedPreferencesUtils.PRIMARY_TEXT_COLOR, backgroundColorAppropriateTextColor) + .putInt(CustomThemeSharedPreferencesUtils.BOTTOM_APP_BAR_ICON_COLOR, colorPrimaryAppropriateTextColor) + .putInt(CustomThemeSharedPreferencesUtils.PRIMARY_ICON_COLOR, backgroundColorAppropriateTextColor) + .putInt(CustomThemeSharedPreferencesUtils.FAB_ICON_COLOR, colorPrimaryAppropriateTextColor) + .putInt(CustomThemeSharedPreferencesUtils.TOOLBAR_PRIMARY_TEXT_AND_ICON_COLOR, colorPrimaryAppropriateTextColor) + .putInt(CustomThemeSharedPreferencesUtils.TOOLBAR_SECONDARY_TEXT_COLOR, colorPrimaryAppropriateTextColor) + .putInt(CustomThemeSharedPreferencesUtils.TAB_LAYOUT_WITH_COLLAPSED_COLLAPSING_TOOLBAR_TAB_INDICATOR, colorPrimaryAppropriateTextColor) + .putInt(CustomThemeSharedPreferencesUtils.TAB_LAYOUT_WITH_COLLAPSED_COLLAPSING_TOOLBAR_TEXT_COLOR, colorPrimaryAppropriateTextColor) + .putInt(CustomThemeSharedPreferencesUtils.TAB_LAYOUT_WITH_COLLAPSED_COLLAPSING_TOOLBAR_TAB_BACKGROUND, colorPrimaryInt) + .putInt(CustomThemeSharedPreferencesUtils.TAB_LAYOUT_WITH_EXPANDED_COLLAPSING_TOOLBAR_TAB_BACKGROUND, colorPrimaryInt) + .putInt(CustomThemeSharedPreferencesUtils.TAB_LAYOUT_WITH_EXPANDED_COLLAPSING_TOOLBAR_TAB_INDICATOR, colorPrimaryAppropriateTextColor) + .putInt(CustomThemeSharedPreferencesUtils.TAB_LAYOUT_WITH_EXPANDED_COLLAPSING_TOOLBAR_TEXT_COLOR, colorPrimaryAppropriateTextColor) + .putInt(CustomThemeSharedPreferencesUtils.CIRCULAR_PROGRESS_BAR_BACKGROUND, colorPrimaryInt) + .putBoolean(CustomThemeSharedPreferencesUtils.LIGHT_STATUS_BAR, getAppropriateTextColor(colorPrimaryDarkInt) == Color.toArgb(Color.BLACK)) + .apply(); + darkThemeSharedPreferences.edit() + .putInt(CustomThemeSharedPreferencesUtils.COLOR_ACCENT, colorPrimaryInt) + .putInt(CustomThemeSharedPreferencesUtils.COLOR_PRIMARY_LIGHT_THEME, colorPrimaryInt) + .apply(); + amoledThemeSharedPreferences.edit() + .putInt(CustomThemeSharedPreferencesUtils.COLOR_ACCENT, colorPrimaryInt) + .putInt(CustomThemeSharedPreferencesUtils.COLOR_PRIMARY_LIGHT_THEME, colorPrimaryInt) + .apply(); + + handler.post(() -> EventBus.getDefault().post(new RecreateActivityEvent())); + } + } + }); + } + return true; + } + }); + } + customThemeViewModel = new ViewModelProvider(this, new CustomThemeViewModel.Factory(redditDataRoomDatabase)) .get(CustomThemeViewModel.class); @@ -186,6 +271,28 @@ public class ThemePreferenceFragment extends PreferenceFragmentCompat { }); } + private int shiftColorTo255(int color, double ratio) { + int offset = (int) (Math.min(Math.min(255 - Color.red(color), 255 - Color.green(color)), 255 - Color.blue(color)) * ratio); + return Color.argb(Color.alpha(color), Color.red(color) + offset, + Color.green(color) + offset, + Color.blue(color) + offset); + } + + private int shiftColorTo0(int color, double ratio) { + int offset = (int) (Math.min(Math.min(Color.red(color), Color.green(color)), Color.blue(color)) * ratio); + return Color.argb(Color.alpha(color), Color.red(color) - offset, + Color.green(color) - offset, + Color.blue(color) - offset); + + } + + @ColorInt + public int getAppropriateTextColor(@ColorInt int color) { + // Counting the perceptive luminance - human eye favors green color... + 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; + } + @Override public void onAttach(@NonNull Context context) { super.onAttach(context); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java index 2795138d..5eecceb1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java @@ -180,6 +180,7 @@ public class SharedPreferencesUtils { public static final String NUMBER_OF_COLUMNS_IN_POST_FEED_LANDSCAPE_CARD_LAYOUT_2 = "number_of_columns_in_post_feed_landscape_card_layout_2"; public static final String DISABLE_NSFW_FOREVER = "disable_nsfw_forever"; public static final String SHOW_ONLY_ONE_COMMENT_LEVEL_INDICATOR = "show_only_one_comment_level_indicator"; + public static final String ENABLE_MATERIAL_YOU = "enable_material_you"; public static final String DEFAULT_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit_preferences"; public static final String MAIN_PAGE_TABS_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.main_page_tabs"; -- cgit v1.2.3 From 18ecb626056de308bed0eb79a88968b617663005 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Wed, 16 Jun 2021 18:26:32 +0800 Subject: Listen to wallpaper change and change theme. --- .../infinityforreddit/AppComponent.java | 3 + .../infinityforreddit/Infinity.java | 4 + .../WallpaperChangeReceiver.java | 21 +++++ .../services/MaterialYouService.java | 46 ++++++++++ .../settings/ThemePreferenceFragment.java | 90 ++---------------- .../infinityforreddit/utils/MaterialYouUtils.java | 101 +++++++++++++++++++++ 6 files changed, 181 insertions(+), 84 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/broadcastreceivers/WallpaperChangeReceiver.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/services/MaterialYouService.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/utils/MaterialYouUtils.java (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index 922642f2..939858c7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -73,6 +73,7 @@ import ml.docilealligator.infinityforreddit.fragments.ViewRedditGalleryImageOrGi import ml.docilealligator.infinityforreddit.fragments.ViewRedditGalleryVideoFragment; import ml.docilealligator.infinityforreddit.services.DownloadMediaService; import ml.docilealligator.infinityforreddit.services.DownloadRedditVideoService; +import ml.docilealligator.infinityforreddit.services.MaterialYouService; import ml.docilealligator.infinityforreddit.services.SubmitPostService; import ml.docilealligator.infinityforreddit.settings.AdvancedPreferenceFragment; import ml.docilealligator.infinityforreddit.settings.CrashReportsFragment; @@ -266,4 +267,6 @@ public interface AppComponent { void inject(AnonymousSubscriptionsActivity anonymousSubscriptionsActivity); void inject(LockScreenActivity lockScreenActivity); + + void inject(MaterialYouService materialYouService); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Infinity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Infinity.java index 1514b745..1ea45d67 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Infinity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Infinity.java @@ -1,6 +1,7 @@ package ml.docilealligator.infinityforreddit; import android.app.Application; +import android.content.Intent; import android.content.IntentFilter; import android.net.ConnectivityManager; import android.os.Bundle; @@ -16,6 +17,7 @@ import com.livefront.bridge.SavedStateHandler; import org.greenrobot.eventbus.EventBus; import ml.docilealligator.infinityforreddit.broadcastreceivers.NetworkWifiStatusReceiver; +import ml.docilealligator.infinityforreddit.broadcastreceivers.WallpaperChangeReceiver; import ml.docilealligator.infinityforreddit.events.ChangeNetworkStatusEvent; import ml.docilealligator.infinityforreddit.utils.Utils; @@ -87,6 +89,8 @@ public class Infinity extends Application implements LifecycleObserver { mNetworkWifiStatusReceiver = new NetworkWifiStatusReceiver(() -> EventBus.getDefault().post(new ChangeNetworkStatusEvent(Utils.getConnectedNetwork(getApplicationContext())))); registerReceiver(mNetworkWifiStatusReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); + + registerReceiver(new WallpaperChangeReceiver(), new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED)); } // @OnLifecycleEvent(Lifecycle.Event.ON_START) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/broadcastreceivers/WallpaperChangeReceiver.java b/app/src/main/java/ml/docilealligator/infinityforreddit/broadcastreceivers/WallpaperChangeReceiver.java new file mode 100644 index 00000000..b2e64fb9 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/broadcastreceivers/WallpaperChangeReceiver.java @@ -0,0 +1,21 @@ +package ml.docilealligator.infinityforreddit.broadcastreceivers; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.util.Log; + +import java.util.Objects; + +import ml.docilealligator.infinityforreddit.services.MaterialYouService; + +public class WallpaperChangeReceiver extends BroadcastReceiver { + @Override + public void onReceive(Context context, Intent intent) { + Log.i("asfasdf", "s " + intent.getAction()); + if (Objects.equals(intent.getAction(), "android.intent.action.WALLPAPER_CHANGED")) { + 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 new file mode 100644 index 00000000..ec4448ff --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/services/MaterialYouService.java @@ -0,0 +1,46 @@ +package ml.docilealligator.infinityforreddit.services; + +import android.app.IntentService; +import android.content.Intent; +import android.content.SharedPreferences; +import android.os.Handler; + +import java.util.concurrent.Executor; + +import javax.inject.Inject; +import javax.inject.Named; + +import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.utils.MaterialYouUtils; + +public class MaterialYouService extends IntentService { + + @Inject + @Named("light_theme") + SharedPreferences lightThemeSharedPreferences; + @Inject + @Named("dark_theme") + SharedPreferences darkThemeSharedPreferences; + @Inject + @Named("amoled_theme") + SharedPreferences amoledThemeSharedPreferences; + @Inject + RedditDataRoomDatabase redditDataRoomDatabase; + @Inject + CustomThemeWrapper customThemeWrapper; + @Inject + Executor executor; + + public MaterialYouService() { + super("MaterialYouService"); + } + + @Override + protected void onHandleIntent(Intent intent) { + ((Infinity) getApplication()).getAppComponent().inject(this); + MaterialYouUtils.changeTheme(this, executor, new Handler(), 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 292d7bcf..c4d03d48 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/ThemePreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/ThemePreferenceFragment.java @@ -1,17 +1,13 @@ package ml.docilealligator.infinityforreddit.settings; -import android.app.WallpaperColors; -import android.app.WallpaperManager; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; -import android.graphics.Color; import android.os.Build; import android.os.Bundle; import android.os.Handler; -import androidx.annotation.ColorInt; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatDelegate; @@ -39,6 +35,7 @@ import ml.docilealligator.infinityforreddit.customtheme.CustomThemeViewModel; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.events.RecreateActivityEvent; import ml.docilealligator.infinityforreddit.utils.CustomThemeSharedPreferencesUtils; +import ml.docilealligator.infinityforreddit.utils.MaterialYouUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY; @@ -174,65 +171,12 @@ public class ThemePreferenceFragment extends PreferenceFragmentCompat { if (enableMaterialYouSwitchPreference != null) { Handler handler = new Handler(); - enableMaterialYouSwitchPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - if (true) { - executor.execute(() -> { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { - WallpaperManager wallpaperManager = WallpaperManager.getInstance(activity); - WallpaperColors wallpaperColors = wallpaperManager.getWallpaperColors(WallpaperManager.FLAG_SYSTEM); - - if (wallpaperColors != null) { - int colorPrimaryInt = shiftColorTo255(wallpaperColors.getPrimaryColor().toArgb(), 0.4); - int colorPrimaryDarkInt = shiftColorTo0(colorPrimaryInt, 0.4); - int backgroundColor = shiftColorTo255(colorPrimaryInt, 0.6); - int cardViewBackgroundColor = shiftColorTo255(colorPrimaryInt, 0.9); - Color colorAccent = wallpaperColors.getSecondaryColor(); - int colorAccentInt = shiftColorTo255(colorAccent == null ? customThemeWrapper.getColorAccent() : colorAccent.toArgb(), 0.4); - - int colorPrimaryAppropriateTextColor = getAppropriateTextColor(colorPrimaryInt); - int backgroundColorAppropriateTextColor = getAppropriateTextColor(backgroundColor); - - lightThemeSharedPreferences.edit().putInt(CustomThemeSharedPreferencesUtils.COLOR_PRIMARY, colorPrimaryInt) - .putInt(CustomThemeSharedPreferencesUtils.COLOR_PRIMARY_DARK, colorPrimaryDarkInt) - .putInt(CustomThemeSharedPreferencesUtils.COLOR_ACCENT, colorAccentInt) - .putInt(CustomThemeSharedPreferencesUtils.COLOR_PRIMARY_LIGHT_THEME, colorPrimaryInt) - .putInt(CustomThemeSharedPreferencesUtils.BACKGROUND_COLOR, backgroundColor) - .putInt(CustomThemeSharedPreferencesUtils.CARD_VIEW_BACKGROUND_COLOR, cardViewBackgroundColor) - .putInt(CustomThemeSharedPreferencesUtils.BOTTOM_APP_BAR_BACKGROUND_COLOR, colorPrimaryInt) - .putInt(CustomThemeSharedPreferencesUtils.NAV_BAR_COLOR, colorPrimaryInt) - .putInt(CustomThemeSharedPreferencesUtils.PRIMARY_TEXT_COLOR, backgroundColorAppropriateTextColor) - .putInt(CustomThemeSharedPreferencesUtils.BOTTOM_APP_BAR_ICON_COLOR, colorPrimaryAppropriateTextColor) - .putInt(CustomThemeSharedPreferencesUtils.PRIMARY_ICON_COLOR, backgroundColorAppropriateTextColor) - .putInt(CustomThemeSharedPreferencesUtils.FAB_ICON_COLOR, colorPrimaryAppropriateTextColor) - .putInt(CustomThemeSharedPreferencesUtils.TOOLBAR_PRIMARY_TEXT_AND_ICON_COLOR, colorPrimaryAppropriateTextColor) - .putInt(CustomThemeSharedPreferencesUtils.TOOLBAR_SECONDARY_TEXT_COLOR, colorPrimaryAppropriateTextColor) - .putInt(CustomThemeSharedPreferencesUtils.TAB_LAYOUT_WITH_COLLAPSED_COLLAPSING_TOOLBAR_TAB_INDICATOR, colorPrimaryAppropriateTextColor) - .putInt(CustomThemeSharedPreferencesUtils.TAB_LAYOUT_WITH_COLLAPSED_COLLAPSING_TOOLBAR_TEXT_COLOR, colorPrimaryAppropriateTextColor) - .putInt(CustomThemeSharedPreferencesUtils.TAB_LAYOUT_WITH_COLLAPSED_COLLAPSING_TOOLBAR_TAB_BACKGROUND, colorPrimaryInt) - .putInt(CustomThemeSharedPreferencesUtils.TAB_LAYOUT_WITH_EXPANDED_COLLAPSING_TOOLBAR_TAB_BACKGROUND, colorPrimaryInt) - .putInt(CustomThemeSharedPreferencesUtils.TAB_LAYOUT_WITH_EXPANDED_COLLAPSING_TOOLBAR_TAB_INDICATOR, colorPrimaryAppropriateTextColor) - .putInt(CustomThemeSharedPreferencesUtils.TAB_LAYOUT_WITH_EXPANDED_COLLAPSING_TOOLBAR_TEXT_COLOR, colorPrimaryAppropriateTextColor) - .putInt(CustomThemeSharedPreferencesUtils.CIRCULAR_PROGRESS_BAR_BACKGROUND, colorPrimaryInt) - .putBoolean(CustomThemeSharedPreferencesUtils.LIGHT_STATUS_BAR, getAppropriateTextColor(colorPrimaryDarkInt) == Color.toArgb(Color.BLACK)) - .apply(); - darkThemeSharedPreferences.edit() - .putInt(CustomThemeSharedPreferencesUtils.COLOR_ACCENT, colorPrimaryInt) - .putInt(CustomThemeSharedPreferencesUtils.COLOR_PRIMARY_LIGHT_THEME, colorPrimaryInt) - .apply(); - amoledThemeSharedPreferences.edit() - .putInt(CustomThemeSharedPreferencesUtils.COLOR_ACCENT, colorPrimaryInt) - .putInt(CustomThemeSharedPreferencesUtils.COLOR_PRIMARY_LIGHT_THEME, colorPrimaryInt) - .apply(); - - handler.post(() -> EventBus.getDefault().post(new RecreateActivityEvent())); - } - } - }); - } - return true; + enableMaterialYouSwitchPreference.setOnPreferenceChangeListener((preference, newValue) -> { + if (true) { + MaterialYouUtils.changeTheme(activity, executor, new Handler(), customThemeWrapper, + lightThemeSharedPreferences, darkThemeSharedPreferences, amoledThemeSharedPreferences); } + return true; }); } @@ -271,28 +215,6 @@ public class ThemePreferenceFragment extends PreferenceFragmentCompat { }); } - private int shiftColorTo255(int color, double ratio) { - int offset = (int) (Math.min(Math.min(255 - Color.red(color), 255 - Color.green(color)), 255 - Color.blue(color)) * ratio); - return Color.argb(Color.alpha(color), Color.red(color) + offset, - Color.green(color) + offset, - Color.blue(color) + offset); - } - - private int shiftColorTo0(int color, double ratio) { - int offset = (int) (Math.min(Math.min(Color.red(color), Color.green(color)), Color.blue(color)) * ratio); - return Color.argb(Color.alpha(color), Color.red(color) - offset, - Color.green(color) - offset, - Color.blue(color) - offset); - - } - - @ColorInt - public int getAppropriateTextColor(@ColorInt int color) { - // Counting the perceptive luminance - human eye favors green color... - 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; - } - @Override public void onAttach(@NonNull Context context) { super.onAttach(context); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/MaterialYouUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/MaterialYouUtils.java new file mode 100644 index 00000000..51199bea --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/MaterialYouUtils.java @@ -0,0 +1,101 @@ +package ml.docilealligator.infinityforreddit.utils; + +import android.app.WallpaperColors; +import android.app.WallpaperManager; +import android.content.Context; +import android.content.SharedPreferences; +import android.graphics.Color; +import android.os.Build; +import android.os.Handler; + +import androidx.annotation.ColorInt; + +import org.greenrobot.eventbus.EventBus; + +import java.util.concurrent.Executor; + +import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.events.RecreateActivityEvent; + +public class MaterialYouUtils { + public static void changeTheme(Context context, Executor executor, Handler handler, + CustomThemeWrapper customThemeWrapper, + SharedPreferences lightThemeSharedPreferences, + SharedPreferences darkThemeSharedPreferences, + SharedPreferences amoledThemeSharedPreferences) { + executor.execute(() -> { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { + WallpaperManager wallpaperManager = WallpaperManager.getInstance(context); + WallpaperColors wallpaperColors = wallpaperManager.getWallpaperColors(WallpaperManager.FLAG_SYSTEM); + + if (wallpaperColors != null) { + int colorPrimaryInt = shiftColorTo255(wallpaperColors.getPrimaryColor().toArgb(), 0.4); + int colorPrimaryDarkInt = shiftColorTo0(colorPrimaryInt, 0.4); + int backgroundColor = shiftColorTo255(colorPrimaryInt, 0.6); + int cardViewBackgroundColor = shiftColorTo255(colorPrimaryInt, 0.9); + Color colorAccent = wallpaperColors.getSecondaryColor(); + int colorAccentInt = shiftColorTo255(colorAccent == null ? customThemeWrapper.getColorAccent() : colorAccent.toArgb(), 0.4); + + int colorPrimaryAppropriateTextColor = getAppropriateTextColor(colorPrimaryInt); + int backgroundColorAppropriateTextColor = getAppropriateTextColor(backgroundColor); + + lightThemeSharedPreferences.edit().putInt(CustomThemeSharedPreferencesUtils.COLOR_PRIMARY, colorPrimaryInt) + .putInt(CustomThemeSharedPreferencesUtils.COLOR_PRIMARY_DARK, colorPrimaryDarkInt) + .putInt(CustomThemeSharedPreferencesUtils.COLOR_ACCENT, colorAccentInt) + .putInt(CustomThemeSharedPreferencesUtils.COLOR_PRIMARY_LIGHT_THEME, colorPrimaryInt) + .putInt(CustomThemeSharedPreferencesUtils.BACKGROUND_COLOR, backgroundColor) + .putInt(CustomThemeSharedPreferencesUtils.CARD_VIEW_BACKGROUND_COLOR, cardViewBackgroundColor) + .putInt(CustomThemeSharedPreferencesUtils.BOTTOM_APP_BAR_BACKGROUND_COLOR, colorPrimaryInt) + .putInt(CustomThemeSharedPreferencesUtils.NAV_BAR_COLOR, colorPrimaryInt) + .putInt(CustomThemeSharedPreferencesUtils.PRIMARY_TEXT_COLOR, backgroundColorAppropriateTextColor) + .putInt(CustomThemeSharedPreferencesUtils.BOTTOM_APP_BAR_ICON_COLOR, colorPrimaryAppropriateTextColor) + .putInt(CustomThemeSharedPreferencesUtils.PRIMARY_ICON_COLOR, backgroundColorAppropriateTextColor) + .putInt(CustomThemeSharedPreferencesUtils.FAB_ICON_COLOR, colorPrimaryAppropriateTextColor) + .putInt(CustomThemeSharedPreferencesUtils.TOOLBAR_PRIMARY_TEXT_AND_ICON_COLOR, colorPrimaryAppropriateTextColor) + .putInt(CustomThemeSharedPreferencesUtils.TOOLBAR_SECONDARY_TEXT_COLOR, colorPrimaryAppropriateTextColor) + .putInt(CustomThemeSharedPreferencesUtils.TAB_LAYOUT_WITH_COLLAPSED_COLLAPSING_TOOLBAR_TAB_INDICATOR, colorPrimaryAppropriateTextColor) + .putInt(CustomThemeSharedPreferencesUtils.TAB_LAYOUT_WITH_COLLAPSED_COLLAPSING_TOOLBAR_TEXT_COLOR, colorPrimaryAppropriateTextColor) + .putInt(CustomThemeSharedPreferencesUtils.TAB_LAYOUT_WITH_COLLAPSED_COLLAPSING_TOOLBAR_TAB_BACKGROUND, colorPrimaryInt) + .putInt(CustomThemeSharedPreferencesUtils.TAB_LAYOUT_WITH_EXPANDED_COLLAPSING_TOOLBAR_TAB_BACKGROUND, colorPrimaryInt) + .putInt(CustomThemeSharedPreferencesUtils.TAB_LAYOUT_WITH_EXPANDED_COLLAPSING_TOOLBAR_TAB_INDICATOR, colorPrimaryAppropriateTextColor) + .putInt(CustomThemeSharedPreferencesUtils.TAB_LAYOUT_WITH_EXPANDED_COLLAPSING_TOOLBAR_TEXT_COLOR, colorPrimaryAppropriateTextColor) + .putInt(CustomThemeSharedPreferencesUtils.CIRCULAR_PROGRESS_BAR_BACKGROUND, colorPrimaryInt) + .putBoolean(CustomThemeSharedPreferencesUtils.LIGHT_STATUS_BAR, getAppropriateTextColor(colorPrimaryDarkInt) == Color.toArgb(Color.BLACK)) + .apply(); + darkThemeSharedPreferences.edit() + .putInt(CustomThemeSharedPreferencesUtils.COLOR_ACCENT, colorPrimaryInt) + .putInt(CustomThemeSharedPreferencesUtils.COLOR_PRIMARY_LIGHT_THEME, colorPrimaryInt) + .apply(); + amoledThemeSharedPreferences.edit() + .putInt(CustomThemeSharedPreferencesUtils.COLOR_ACCENT, colorPrimaryInt) + .putInt(CustomThemeSharedPreferencesUtils.COLOR_PRIMARY_LIGHT_THEME, colorPrimaryInt) + .apply(); + + handler.post(() -> EventBus.getDefault().post(new RecreateActivityEvent())); + } + } + }); + } + + private static int shiftColorTo255(int color, double ratio) { + int offset = (int) (Math.min(Math.min(255 - Color.red(color), 255 - Color.green(color)), 255 - Color.blue(color)) * ratio); + return Color.argb(Color.alpha(color), Color.red(color) + offset, + Color.green(color) + offset, + Color.blue(color) + offset); + } + + private static int shiftColorTo0(int color, double ratio) { + int offset = (int) (Math.min(Math.min(Color.red(color), Color.green(color)), Color.blue(color)) * ratio); + return Color.argb(Color.alpha(color), Color.red(color) - offset, + Color.green(color) - offset, + Color.blue(color) - offset); + + } + + @ColorInt + public static int getAppropriateTextColor(@ColorInt int color) { + // Counting the perceptive luminance - human eye favors green color... + 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; + } +} -- cgit v1.2.3 From 92a1c1d1aecc6814a49f490f97fdbb03530bba3a Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 17 Jun 2021 12:35:37 +0800 Subject: Fix Material You themeing issue. --- .../broadcastreceivers/WallpaperChangeReceiver.java | 10 ++-------- .../infinityforreddit/utils/MaterialYouUtils.java | 1 + 2 files changed, 3 insertions(+), 8 deletions(-) (limited to 'app/src/main/java') 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 b2e64fb9..8028cdfc 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/broadcastreceivers/WallpaperChangeReceiver.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/broadcastreceivers/WallpaperChangeReceiver.java @@ -3,19 +3,13 @@ package ml.docilealligator.infinityforreddit.broadcastreceivers; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; -import android.util.Log; - -import java.util.Objects; import ml.docilealligator.infinityforreddit.services.MaterialYouService; public class WallpaperChangeReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { - Log.i("asfasdf", "s " + intent.getAction()); - if (Objects.equals(intent.getAction(), "android.intent.action.WALLPAPER_CHANGED")) { - 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/utils/MaterialYouUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/MaterialYouUtils.java index 51199bea..a9cfc226 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/MaterialYouUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/MaterialYouUtils.java @@ -45,6 +45,7 @@ public class MaterialYouUtils { .putInt(CustomThemeSharedPreferencesUtils.COLOR_PRIMARY_LIGHT_THEME, colorPrimaryInt) .putInt(CustomThemeSharedPreferencesUtils.BACKGROUND_COLOR, backgroundColor) .putInt(CustomThemeSharedPreferencesUtils.CARD_VIEW_BACKGROUND_COLOR, cardViewBackgroundColor) + .putInt(CustomThemeSharedPreferencesUtils.COMMENT_BACKGROUND_COLOR, cardViewBackgroundColor) .putInt(CustomThemeSharedPreferencesUtils.BOTTOM_APP_BAR_BACKGROUND_COLOR, colorPrimaryInt) .putInt(CustomThemeSharedPreferencesUtils.NAV_BAR_COLOR, colorPrimaryInt) .putInt(CustomThemeSharedPreferencesUtils.PRIMARY_TEXT_COLOR, backgroundColorAppropriateTextColor) -- cgit v1.2.3