From e3578c59dd7cb65c3d78a1bd219d05577472a0b6 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 2 Jul 2021 23:16:18 +0800 Subject: Save light, dark and amoled Material You themes to database. Add 'Apply Material You' option. Test MotionLayout. --- .../activities/MotionTestActivity.java | 41 ++++++++ .../WallpaperChangeReceiver.java | 15 ++- .../customviews/ClickableMotionLayout.java | 39 ++++++++ .../services/MaterialYouService.java | 5 +- .../settings/ThemePreferenceFragment.java | 32 ++++++- .../infinityforreddit/utils/MaterialYouUtils.java | 104 ++++++++++++++------- .../utils/SharedPreferencesUtils.java | 1 + 7 files changed, 195 insertions(+), 42 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/activities/MotionTestActivity.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/customviews/ClickableMotionLayout.java (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MotionTestActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MotionTestActivity.java new file mode 100644 index 00000000..ba8d0681 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MotionTestActivity.java @@ -0,0 +1,41 @@ +package ml.docilealligator.infinityforreddit.activities; + +import android.os.Bundle; +import android.util.Log; + +import androidx.appcompat.app.AppCompatActivity; +import androidx.constraintlayout.motion.widget.MotionLayout; + +import ml.docilealligator.infinityforreddit.R; + +public class MotionTestActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_motion_test); + + MotionLayout motionLayout = findViewById(R.id.motion_layout); + motionLayout.addTransitionListener(new MotionLayout.TransitionListener() { + @Override + public void onTransitionStarted(MotionLayout motionLayout, int i, int i1) { + Log.i("asdfasdf", "start " + (i == R.id.start) + " " + (i1 == R.id.end)); + } + + @Override + public void onTransitionChange(MotionLayout motionLayout, int i, int i1, float v) { + Log.i("asdfasdf", "start " + (i == R.id.start) + " " + (i1 == R.id.end) + " " + v); + } + + @Override + public void onTransitionCompleted(MotionLayout motionLayout, int i) { + Log.i("asdfasdf", "complete " + (i == R.id.start)); + } + + @Override + public void onTransitionTrigger(MotionLayout motionLayout, int i, boolean b, float v) { + Log.i("asdfasdf", "trigger " + (i == R.id.start) + " " + v + " " + b); + } + }); + } +} \ No newline at end of file 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 8028cdfc..ae9392f7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/broadcastreceivers/WallpaperChangeReceiver.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/broadcastreceivers/WallpaperChangeReceiver.java @@ -3,13 +3,24 @@ 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) { - Intent materialYouIntent = new Intent(context, MaterialYouService.class); - context.startService(materialYouIntent); + if (mSharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_MATERIAL_YOU, false)) { + Intent materialYouIntent = new Intent(context, MaterialYouService.class); + context.startService(materialYouIntent); + } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/ClickableMotionLayout.java b/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/ClickableMotionLayout.java new file mode 100644 index 00000000..8c0f47ff --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/ClickableMotionLayout.java @@ -0,0 +1,39 @@ +package ml.docilealligator.infinityforreddit.customviews; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.MotionEvent; +import android.view.ViewConfiguration; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.constraintlayout.motion.widget.MotionLayout; + +public class ClickableMotionLayout extends MotionLayout { + private long mStartTime = 0; + + public ClickableMotionLayout(@NonNull Context context) { + super(context); + } + + public ClickableMotionLayout(@NonNull Context context, @Nullable AttributeSet attrs) { + super(context, attrs); + } + + public ClickableMotionLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + @Override + public boolean onInterceptTouchEvent(MotionEvent event) { + if ( event.getAction() == MotionEvent.ACTION_DOWN ) { + mStartTime = event.getEventTime(); + } else if ( event.getAction() == MotionEvent.ACTION_UP ) { + if ( event.getEventTime() - mStartTime <= ViewConfiguration.getTapTimeout() ) { + return false; + } + } + + return super.onInterceptTouchEvent(event); + } +} 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 ec4448ff..221b980b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/services/MaterialYouService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/services/MaterialYouService.java @@ -40,7 +40,8 @@ public class MaterialYouService extends IntentService { @Override protected void onHandleIntent(Intent intent) { ((Infinity) getApplication()).getAppComponent().inject(this); - MaterialYouUtils.changeTheme(this, executor, new Handler(), customThemeWrapper, - lightThemeSharedPreferences, darkThemeSharedPreferences, amoledThemeSharedPreferences); + 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 c4d03d48..2e94f166 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/ThemePreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/ThemePreferenceFragment.java @@ -50,6 +50,9 @@ public class ThemePreferenceFragment extends PreferenceFragmentCompat { private AppCompatActivity activity; @Inject + @Named("default") + SharedPreferences sharedPreferences; + @Inject @Named("light_theme") SharedPreferences lightThemeSharedPreferences; @Inject @@ -79,6 +82,7 @@ public class ThemePreferenceFragment extends PreferenceFragmentCompat { Preference customizeAmoledThemePreference = findPreference(SharedPreferencesUtils.CUSTOMIZE_AMOLED_THEME); Preference selectAndCustomizeThemePreference = findPreference(SharedPreferencesUtils.MANAGE_THEMES); SwitchPreference enableMaterialYouSwitchPreference = findPreference(SharedPreferencesUtils.ENABLE_MATERIAL_YOU); + Preference applyMaterialYouPreference = findPreference(SharedPreferencesUtils.APPLY_MATERIAL_YOU); boolean systemDefault = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q; if (themePreference != null && amoledDarkSwitch != null) { @@ -169,15 +173,33 @@ public class ThemePreferenceFragment extends PreferenceFragmentCompat { }); } - if (enableMaterialYouSwitchPreference != null) { - Handler handler = new Handler(); + if (enableMaterialYouSwitchPreference != null && applyMaterialYouPreference != null) { + applyMaterialYouPreference.setVisible( + sharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_MATERIAL_YOU, false)); + enableMaterialYouSwitchPreference.setOnPreferenceChangeListener((preference, newValue) -> { - if (true) { - MaterialYouUtils.changeTheme(activity, executor, new Handler(), customThemeWrapper, - lightThemeSharedPreferences, darkThemeSharedPreferences, amoledThemeSharedPreferences); + if ((Boolean) newValue) { + MaterialYouUtils.changeTheme(activity, executor, new Handler(), + redditDataRoomDatabase, customThemeWrapper, + lightThemeSharedPreferences, darkThemeSharedPreferences, + amoledThemeSharedPreferences); + applyMaterialYouPreference.setVisible(true); + } else { + applyMaterialYouPreference.setVisible(false); } 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; + } + }); } customThemeViewModel = new ViewModelProvider(this, 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 e74d7e53..37e92aa1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/MaterialYouUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/MaterialYouUtils.java @@ -14,11 +14,33 @@ import org.greenrobot.eventbus.EventBus; import java.util.concurrent.Executor; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.customtheme.CustomTheme; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.events.RecreateActivityEvent; public class MaterialYouUtils { + public interface CheckThemeNameListener { + void themeNotExists(); + void themeExists(); + } + + public static void checkThemeName(Executor executor, Handler handler, + RedditDataRoomDatabase redditDataRoomDatabase, + CheckThemeNameListener checkThemeNameListener) { + executor.execute(() -> { + if (redditDataRoomDatabase.customThemeDao().getCustomTheme("Material You") != null + || redditDataRoomDatabase.customThemeDao().getCustomTheme("Material You Dark") != null + || redditDataRoomDatabase.customThemeDao().getCustomTheme("Material You Amoled") != null) { + handler.post(checkThemeNameListener::themeExists); + } else { + handler.post(checkThemeNameListener::themeNotExists); + } + }); + } + public static void changeTheme(Context context, Executor executor, Handler handler, + RedditDataRoomDatabase redditDataRoomDatabase, CustomThemeWrapper customThemeWrapper, SharedPreferences lightThemeSharedPreferences, SharedPreferences darkThemeSharedPreferences, @@ -29,6 +51,10 @@ public class MaterialYouUtils { WallpaperColors wallpaperColors = wallpaperManager.getWallpaperColors(WallpaperManager.FLAG_SYSTEM); if (wallpaperColors != null) { + CustomTheme lightTheme = CustomThemeWrapper.getIndigo(context); + CustomTheme darkTheme = CustomThemeWrapper.getIndigoDark(context); + CustomTheme amoledTheme = CustomThemeWrapper.getIndigoAmoled(context); + int colorPrimaryInt = shiftColorTo255(wallpaperColors.getPrimaryColor().toArgb(), 0.4); int colorPrimaryDarkInt = shiftColorTo0(colorPrimaryInt, 0.3); int backgroundColor = shiftColorTo255(colorPrimaryInt, 0.6); @@ -39,39 +65,51 @@ public class MaterialYouUtils { 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.COMMENT_BACKGROUND_COLOR, cardViewBackgroundColor) - .putInt(CustomThemeSharedPreferencesUtils.AWARDED_COMMENT_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(colorPrimaryInt) == 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(); + lightTheme.colorPrimary = colorPrimaryInt; + lightTheme.colorPrimaryDark = colorPrimaryDarkInt; + lightTheme.colorAccent = colorAccentInt; + lightTheme.colorPrimaryLightTheme = colorPrimaryInt; + lightTheme.backgroundColor = backgroundColor; + lightTheme.cardViewBackgroundColor = cardViewBackgroundColor; + lightTheme.commentBackgroundColor = cardViewBackgroundColor; + lightTheme.awardedCommentBackgroundColor = cardViewBackgroundColor; + lightTheme.bottomAppBarBackgroundColor = colorPrimaryInt; + lightTheme.navBarColor = colorPrimaryInt; + lightTheme.primaryTextColor = backgroundColorAppropriateTextColor; + lightTheme.bottomAppBarIconColor = colorPrimaryAppropriateTextColor; + lightTheme.primaryIconColor = backgroundColorAppropriateTextColor; + lightTheme.fabIconColor = colorPrimaryAppropriateTextColor; + lightTheme.toolbarPrimaryTextAndIconColor = colorPrimaryAppropriateTextColor; + lightTheme.toolbarSecondaryTextColor = colorPrimaryAppropriateTextColor; + lightTheme.tabLayoutWithCollapsedCollapsingToolbarTabIndicator = colorPrimaryAppropriateTextColor; + lightTheme.tabLayoutWithCollapsedCollapsingToolbarTextColor = colorPrimaryAppropriateTextColor; + lightTheme.tabLayoutWithCollapsedCollapsingToolbarTabBackground = colorPrimaryInt; + lightTheme.tabLayoutWithExpandedCollapsingToolbarTabBackground = colorPrimaryInt; + lightTheme.tabLayoutWithExpandedCollapsingToolbarTabIndicator = colorPrimaryAppropriateTextColor; + lightTheme.tabLayoutWithExpandedCollapsingToolbarTextColor = colorPrimaryAppropriateTextColor; + lightTheme.circularProgressBarBackground = colorPrimaryInt; + lightTheme.isLightStatusBar = getAppropriateTextColor(colorPrimaryInt) == Color.toArgb(Color.BLACK); + lightTheme.name = "Material You"; + + darkTheme.colorAccent = colorPrimaryInt; + darkTheme.colorPrimaryLightTheme = colorPrimaryInt; + darkTheme.name = "Material You Dark"; + + amoledTheme.colorAccent = colorPrimaryInt; + amoledTheme.colorPrimaryLightTheme = colorPrimaryInt; + amoledTheme.name = "Material You Amoled"; + + redditDataRoomDatabase.customThemeDao().unsetLightTheme(); + redditDataRoomDatabase.customThemeDao().unsetDarkTheme(); + redditDataRoomDatabase.customThemeDao().unsetAmoledTheme(); + + redditDataRoomDatabase.customThemeDao().insert(lightTheme); + redditDataRoomDatabase.customThemeDao().insert(darkTheme); + redditDataRoomDatabase.customThemeDao().insert(amoledTheme); + + CustomThemeSharedPreferencesUtils.insertThemeToSharedPreferences(lightTheme, lightThemeSharedPreferences); + CustomThemeSharedPreferencesUtils.insertThemeToSharedPreferences(darkTheme, darkThemeSharedPreferences); + CustomThemeSharedPreferencesUtils.insertThemeToSharedPreferences(amoledTheme, amoledThemeSharedPreferences); handler.post(() -> EventBus.getDefault().post(new RecreateActivityEvent())); } 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 5eecceb1..82343e4f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java @@ -181,6 +181,7 @@ public class SharedPreferencesUtils { 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 APPLY_MATERIAL_YOU = "apply_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