This commit is contained in:
Katharine Chui
2022-02-03 03:34:20 +08:00
parent 9289d3cf0b
commit edf953cdad
12 changed files with 589 additions and 38 deletions

View File

@@ -19,10 +19,9 @@ public class CatimaAppCompatActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// XXX on the splash screen activity, aka the main activity, this has to be executed after applying dynamic colors, not before
// so running this only on non main for now
if (!this.getClass().getSimpleName().equals(MainActivity.class.getSimpleName())) {
Utils.patchOledDarkTheme(this);
// XXX splash screen activity has to do this after installing splash screen before view inflate
if(!this.getClass().getSimpleName().equals(MainActivity.class.getSimpleName())) {
Utils.patchColors(this);
}
}
@@ -39,9 +38,6 @@ public class CatimaAppCompatActivity extends AppCompatActivity {
getWindow().setStatusBarColor(Utils.isDarkModeEnabled(this) ? Color.TRANSPARENT : Color.argb(127, 0, 0, 0));
}
// XXX android 9 and below has a nasty rendering bug if the theme was patched earlier
// the splash screen activity needs the fix regardless to solve a dynamic color api issue
if (!this.getClass().getSimpleName().equals(MainActivity.class.getSimpleName())) {
Utils.postPatchOledDarkTheme(this);
}
Utils.postPatchColors(this);
}
}

View File

@@ -16,6 +16,5 @@ public class LoyaltyCardLockerApplication extends Application {
Settings settings = new Settings(this);
AppCompatDelegate.setDefaultNightMode(settings.getTheme());
DynamicColors.applyToActivitiesIfAvailable(this);
}
}

View File

@@ -180,20 +180,10 @@ public class MainActivity extends CatimaAppCompatActivity implements LoyaltyCard
protected void onCreate(Bundle inputSavedInstanceState) {
super.onCreate(inputSavedInstanceState);
SplashScreen.installSplashScreen(this);
// onPreCreate can't tell this activity uses a material theme due to splash screen, force color application here
DynamicColors.applyIfAvailable(this);
Utils.patchOledDarkTheme(this);
setTitle(R.string.app_name);
// XXX color patching has to be done again after setting splash screen
Utils.patchColors(this);
setContentView(R.layout.main_activity);
// XXX more dynamic color fixing due to splash screen
// without this the background color will get stuck with the old color before dynamic color
TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(android.R.attr.colorBackground, typedValue, true);
findViewById(android.R.id.content).setBackgroundColor(typedValue.data);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

View File

@@ -17,6 +17,7 @@ import android.util.Log;
import android.util.TypedValue;
import android.widget.Toast;
import com.google.android.material.color.DynamicColors;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
@@ -464,21 +465,64 @@ public class Utils {
}
// replace colors in the current theme
// use before views are inflated, after dynamic color
public static void patchOledDarkTheme(AppCompatActivity activity) {
if (isDarkModeEnabled(activity) && new Settings(activity).getOledDark()) {
activity.getTheme().applyStyle(R.style.DarkBackground, true);
public static void patchColors(AppCompatActivity activity) {
Settings settings = new Settings(activity);
String color = settings.getColor();
final String system = activity.getResources().getString(R.string.settings_key_system_theme);
final String red = activity.getResources().getString(R.string.settings_key_red_theme);
final String pink = activity.getResources().getString(R.string.settings_key_pink_theme);
final String magenta = activity.getResources().getString(R.string.settings_key_magenta_theme);
final String violet = activity.getResources().getString(R.string.settings_key_violet_theme);
final String blue = activity.getResources().getString(R.string.settings_key_blue_theme);
final String sky_blue = activity.getResources().getString(R.string.settings_key_sky_blue_theme);
final String green = activity.getResources().getString(R.string.settings_key_green_theme);
final String brown = activity.getResources().getString(R.string.settings_key_brown_theme);
// do nothing if color is catima
// final String catima = activity.getResources().getString(R.string.settings_key_catima_theme);
Resources.Theme theme = activity.getTheme();
if (color.equals(system)) {
DynamicColors.applyIfAvailable(activity);
} else {
if (color.equals(red)) {
theme.applyStyle(R.style.red, true);
}
if (color.equals(pink)) {
theme.applyStyle(R.style.pink, true);
}
if (color.equals(magenta)) {
theme.applyStyle(R.style.magenta, true);
}
if (color.equals(violet)) {
theme.applyStyle(R.style.violet, true);
}
if (color.equals(blue)) {
theme.applyStyle(R.style.blue, true);
}
if (color.equals(sky_blue)) {
theme.applyStyle(R.style.skyblue, true);
}
if (color.equals(green)) {
theme.applyStyle(R.style.green, true);
}
if (color.equals(brown)) {
theme.applyStyle(R.style.brown, true);
}
}
if (isDarkModeEnabled(activity) && settings.getOledDark()) {
theme.applyStyle(R.style.DarkBackground, true);
}
}
// XXX android 9 and below has issues with patched theme where the background becomes a
// rendering mess
// use after views are inflated
public static void postPatchOledDarkTheme(AppCompatActivity activity) {
if (isDarkModeEnabled(activity) && new Settings(activity).getOledDark()) {
TypedValue typedValue = new TypedValue();
activity.getTheme().resolveAttribute(android.R.attr.colorBackground, typedValue, true);
activity.findViewById(android.R.id.content).setBackgroundColor(typedValue.data);
}
public static void postPatchColors(AppCompatActivity activity) {
TypedValue typedValue = new TypedValue();
activity.getTheme().resolveAttribute(android.R.attr.colorBackground, typedValue, true);
activity.findViewById(android.R.id.content).setBackgroundColor(typedValue.data);
}
}

View File

@@ -107,4 +107,6 @@ public class Settings {
public boolean getOledDark() {
return getBoolean(R.string.settings_key_oled_dark, false);
}
public String getColor() { return getString(R.string.setting_key_theme_color, mContext.getResources().getString(R.string.settings_key_system_theme)); }
}

View File

@@ -140,6 +140,13 @@ public class SettingsActivity extends CatimaAppCompatActivity {
refreshActivity(true);
return true;
});
Preference colorPreference = findPreference(getResources().getString(R.string.setting_key_theme_color));
assert colorPreference != null;
colorPreference.setOnPreferenceChangeListener((preference, o) -> {
refreshActivity(true);
return true;
});
}
private void refreshActivity(boolean reloadMain) {