Theme improvements

This commit is contained in:
NekoInverter committed 2020-03-23 16:41:14 +08:00
1 parent a9b7df3284
commit d0d9b77f5e
65 files changed
+458 -561

No files matched your search

@@ -17,6 +17,8 @@
package com.hippo.ehviewer.ui;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.view.WindowManager;
@@ -32,9 +34,59 @@ import com.hippo.ehviewer.R;
import com.hippo.ehviewer.Settings;
import java.util.Locale;
import java.util.Objects;
public abstract class EhActivity extends AppCompatActivity {
private static final String THEME_DEFAULT = "DEFAULT";
private static final String THEME_BLACK = "BLACK";
private String mTheme;
public static boolean isBlackNightTheme() {
return Settings.getBoolean("black_dark_theme", false);
}
public static String getTheme(Context context) {
if (isBlackNightTheme()
&& isNightMode(context.getResources().getConfiguration()))
return THEME_BLACK;
return THEME_DEFAULT;
}
public static boolean isNightMode(Configuration configuration) {
return (configuration.uiMode & Configuration.UI_MODE_NIGHT_YES) > 0;
}
@Override
protected void onApplyThemeResource(Resources.Theme theme, int resid, boolean first) {
// apply real style and our custom style
if (getParent() == null) {
theme.applyStyle(resid, true);
} else {
try {
theme.setTo(getParent().getTheme());
} catch (Exception e) {
// Empty
}
theme.applyStyle(resid, false);
}
theme.applyStyle(getThemeStyleRes(this), true);
// only pass theme style to super, so styled theme will not be overwritten
super.onApplyThemeResource(theme, R.style.ThemeOverlay, first);
}
@StyleRes
public int getThemeStyleRes(Context context) {
switch (getTheme(context)) {
case THEME_BLACK:
return R.style.ThemeOverlay_Black;
case THEME_DEFAULT:
default:
return R.style.ThemeOverlay;
}
}
@StyleRes
protected abstract int getThemeResId(int theme);
@@ -49,6 +101,7 @@ public abstract class EhActivity extends AppCompatActivity {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Settings.getApplyNavBarThemeColor()) {
getWindow().setNavigationBarColor(AttrResources.getAttrColor(this, R.attr.colorPrimaryDark));
}
mTheme = getTheme(this);
}
@Override
@@ -67,6 +120,9 @@ public abstract class EhActivity extends AppCompatActivity {
} else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
}
if (!Objects.equals(mTheme, getTheme(this))) {
recreate();
}
}
@Override