Skip to content
This repository was archived by the owner on May 4, 2023. It is now read-only.

Commit 0339dd7

Browse files
Proper implementation for black theme
Signed-off-by: androidacy-user <opensource@androidacy.com>
1 parent ff1cb4f commit 0339dd7

File tree

6 files changed

+81
-134
lines changed

6 files changed

+81
-134
lines changed

app/src/main/java/com/fox2code/mmm/MainApplication.java

Lines changed: 73 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -47,37 +47,33 @@
4747
import io.noties.prism4j.Prism4j;
4848
import io.noties.prism4j.annotations.PrismBundle;
4949

50-
@PrismBundle(
51-
includeAll = true,
52-
grammarLocatorClassName = ".Prism4jGrammarLocator"
53-
)
54-
public class MainApplication extends FoxApplication
55-
implements androidx.work.Configuration.Provider {
50+
@PrismBundle(includeAll = true, grammarLocatorClassName = ".Prism4jGrammarLocator")
51+
public class MainApplication extends FoxApplication implements androidx.work.Configuration.Provider {
5652
private static final String TAG = "MainApplication";
5753
private static final String timeFormatString = "dd MMM yyyy"; // Example: 13 july 2001
58-
private static Locale timeFormatLocale =
59-
Resources.getSystem().getConfiguration().locale;
60-
private static SimpleDateFormat timeFormat =
61-
new SimpleDateFormat(timeFormatString, timeFormatLocale);
6254
private static final Shell.Builder shellBuilder;
6355
private static final long secret;
6456
@SuppressLint("RestrictedApi") // Use FoxProcess wrapper helper.
6557
private static final boolean wrapped = !FoxProcessExt.isRootLoader();
58+
private static Locale timeFormatLocale = Resources.getSystem().getConfiguration().locale;
59+
private static SimpleDateFormat timeFormat = new SimpleDateFormat(timeFormatString, timeFormatLocale);
6660
private static SharedPreferences bootSharedPreferences;
6761
private static String relPackageName = BuildConfig.APPLICATION_ID;
6862
private static MainApplication INSTANCE;
6963
private static boolean firstBoot;
70-
// Provides the Context for the base application
71-
public Context FoxApplication = this;
7264

7365
static {
74-
Shell.setDefaultBuilder(shellBuilder = Shell.Builder.create()
75-
.setFlags(Shell.FLAG_REDIRECT_STDERR)
76-
.setTimeout(10).setInitializers(InstallerInitializer.class)
77-
);
66+
Shell.setDefaultBuilder(shellBuilder = Shell.Builder.create().setFlags(Shell.FLAG_REDIRECT_STDERR).setTimeout(10).setInitializers(InstallerInitializer.class));
7867
secret = new Random().nextLong();
7968
}
8069

70+
// Provides the Context for the base application
71+
public Context FoxApplication = this;
72+
@StyleRes
73+
private int managerThemeResId = R.style.Theme_MagiskModuleManager;
74+
private FoxThemeWrapper markwonThemeContext;
75+
private Markwon markwon;
76+
8177
public MainApplication() {
8278
if (INSTANCE != null && INSTANCE != this)
8379
throw new IllegalStateException("Duplicate application instance!");
@@ -90,10 +86,8 @@ public static Shell build(String... command) {
9086

9187
public static void addSecret(Intent intent) {
9288
ComponentName componentName = intent.getComponent();
93-
String packageName = componentName != null ?
94-
componentName.getPackageName() : intent.getPackage();
95-
if (!BuildConfig.APPLICATION_ID.equalsIgnoreCase(packageName) &&
96-
!relPackageName.equals(packageName)) {
89+
String packageName = componentName != null ? componentName.getPackageName() : intent.getPackage();
90+
if (!BuildConfig.APPLICATION_ID.equalsIgnoreCase(packageName) && !relPackageName.equals(packageName)) {
9791
// Code safeguard, we should never reach here.
9892
throw new IllegalArgumentException("Can't add secret to outbound Intent");
9993
}
@@ -139,38 +133,31 @@ public static boolean isDohEnabled() {
139133
}
140134

141135
public static boolean isMonetEnabled() {
142-
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.S &&
143-
getSharedPreferences().getBoolean("pref_enable_monet", true);
136+
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && getSharedPreferences().getBoolean("pref_enable_monet", true);
144137
}
145138

146139
public static boolean isBlurEnabled() {
147-
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
148-
getSharedPreferences().getBoolean("pref_enable_blur", false);
140+
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && getSharedPreferences().getBoolean("pref_enable_blur", false);
149141
}
150142

151143
public static boolean isDeveloper() {
152-
return BuildConfig.DEBUG ||
153-
getSharedPreferences().getBoolean("developer", false);
144+
return BuildConfig.DEBUG || getSharedPreferences().getBoolean("developer", false);
154145
}
155146

156147
public static boolean isDisableLowQualityModuleFilter() {
157-
return getSharedPreferences().getBoolean("pref_disable_low_quality_module_filter",
158-
false) && isDeveloper();
148+
return getSharedPreferences().getBoolean("pref_disable_low_quality_module_filter", false) && isDeveloper();
159149
}
160150

161151
public static boolean isUsingMagiskCommand() {
162-
return InstallerInitializer.peekMagiskVersion() >= Constants.MAGISK_VER_CODE_INSTALL_COMMAND
163-
&& getSharedPreferences().getBoolean("pref_use_magisk_install_command", false)
164-
&& isDeveloper();
152+
return InstallerInitializer.peekMagiskVersion() >= Constants.MAGISK_VER_CODE_INSTALL_COMMAND && getSharedPreferences().getBoolean("pref_use_magisk_install_command", false) && isDeveloper();
165153
}
166154

167155
public static boolean isBackgroundUpdateCheckEnabled() {
168156
return !wrapped && getSharedPreferences().getBoolean("pref_background_update_check", true);
169157
}
170158

171159
public static boolean isAndroidacyTestMode() {
172-
return isDeveloper() &&
173-
getSharedPreferences().getBoolean("pref_androidacy_test_mode", false);
160+
return isDeveloper() && getSharedPreferences().getBoolean("pref_androidacy_test_mode", false);
174161
}
175162

176163
public static boolean isFirstBoot() {
@@ -186,8 +173,7 @@ public static void setHasGottenRootAccess(boolean bool) {
186173
}
187174

188175
public static boolean isCrashReportingEnabled() {
189-
return getSharedPreferences().getBoolean("pref_crash_reporting",
190-
BuildConfig.DEFAULT_ENABLE_CRASH_REPORTING && !BuildConfig.DEBUG);
176+
return getSharedPreferences().getBoolean("pref_crash_reporting", BuildConfig.DEFAULT_ENABLE_CRASH_REPORTING && !BuildConfig.DEBUG);
191177
}
192178

193179
public static SharedPreferences getBootSharedPreferences() {
@@ -203,28 +189,17 @@ public static String formatTime(long timeStamp) {
203189
return timeFormat.format(new Date(timeStamp));
204190
}
205191

206-
@StyleRes
207-
private int managerThemeResId = R.style.Theme_MagiskModuleManager;
208-
private FoxThemeWrapper markwonThemeContext;
209-
private Markwon markwon;
210-
211192
public static boolean isNotificationPermissionGranted() {
212193
return NotificationManagerCompat.from(INSTANCE).areNotificationsEnabled();
213194
}
214195

215196
public Markwon getMarkwon() {
216-
if (this.markwon != null)
217-
return this.markwon;
197+
if (this.markwon != null) return this.markwon;
218198
FoxThemeWrapper contextThemeWrapper = this.markwonThemeContext;
219199
if (contextThemeWrapper == null) {
220-
contextThemeWrapper = this.markwonThemeContext =
221-
new FoxThemeWrapper(this, this.managerThemeResId);
200+
contextThemeWrapper = this.markwonThemeContext = new FoxThemeWrapper(this, this.managerThemeResId);
222201
}
223-
Markwon markwon = Markwon.builder(contextThemeWrapper).usePlugin(HtmlPlugin.create())
224-
.usePlugin(SyntaxHighlightPlugin.create(
225-
new Prism4j(new Prism4jGrammarLocator()), new Prism4jSwitchTheme()))
226-
.usePlugin(ImagesPlugin.create().addSchemeHandler(
227-
OkHttpNetworkSchemeHandler.create(Http.getHttpClientWithCache()))).build();
202+
Markwon markwon = Markwon.builder(contextThemeWrapper).usePlugin(HtmlPlugin.create()).usePlugin(SyntaxHighlightPlugin.create(new Prism4j(new Prism4jGrammarLocator()), new Prism4jSwitchTheme())).usePlugin(ImagesPlugin.create().addSchemeHandler(OkHttpNetworkSchemeHandler.create(Http.getHttpClientWithCache()))).build();
228203
return this.markwon = markwon;
229204
}
230205

@@ -238,43 +213,6 @@ public androidx.work.Configuration getWorkManagerConfiguration() {
238213
return new androidx.work.Configuration.Builder().build();
239214
}
240215

241-
private class Prism4jSwitchTheme implements Prism4jTheme {
242-
private final Prism4jTheme light = new Prism4jThemeDefault(Color.TRANSPARENT);
243-
private final Prism4jTheme dark = new Prism4jThemeDarkula(Color.TRANSPARENT);
244-
// Black theme
245-
private final Prism4jTheme black = new Prism4jThemeDefault(Color.BLACK);
246-
247-
private Prism4jTheme getTheme() {
248-
// isLightTheme() means light, isDarkTheme() means dark, and isBlackTheme() means black
249-
return isLightTheme() ? light : isDarkTheme() ? dark : black;
250-
}
251-
252-
@Override
253-
public int background() {
254-
return this.getTheme().background();
255-
}
256-
257-
@Override
258-
public int textColor() {
259-
return this.getTheme().textColor();
260-
}
261-
262-
@Override
263-
public void apply(@NonNull String language, @NonNull Prism4j.Syntax syntax,
264-
@NonNull SpannableStringBuilder builder, int start, int end) {
265-
this.getTheme().apply(language, syntax, builder, start, end);
266-
}
267-
}
268-
269-
@SuppressLint("NonConstantResourceId")
270-
public void setManagerThemeResId(@StyleRes int resId) {
271-
this.managerThemeResId = resId;
272-
if (this.markwonThemeContext != null) {
273-
this.markwonThemeContext.setTheme(resId);
274-
}
275-
this.markwon = null;
276-
}
277-
278216
public void updateTheme() {
279217
@StyleRes int themeResId;
280218
String theme;
@@ -283,27 +221,22 @@ public void updateTheme() {
283221
default:
284222
Log.w("MainApplication", "Unknown theme id: " + theme);
285223
case "system":
286-
themeResId = monet ?
287-
R.style.Theme_MagiskModuleManager_Monet :
288-
R.style.Theme_MagiskModuleManager;
224+
themeResId = monet ? R.style.Theme_MagiskModuleManager_Monet : R.style.Theme_MagiskModuleManager;
289225
break;
290226
case "dark":
291-
themeResId = monet ?
292-
R.style.Theme_MagiskModuleManager_Monet_Dark :
293-
R.style.Theme_MagiskModuleManager_Dark;
227+
themeResId = monet ? R.style.Theme_MagiskModuleManager_Monet_Dark : R.style.Theme_MagiskModuleManager_Dark;
228+
break;
229+
case "black":
230+
themeResId = monet ? R.style.Theme_MagiskModuleManager_Monet_Black : R.style.Theme_MagiskModuleManager_Black;
294231
break;
295232
case "light":
296-
themeResId = monet ?
297-
R.style.Theme_MagiskModuleManager_Monet_Light :
298-
R.style.Theme_MagiskModuleManager_Light;
233+
themeResId = monet ? R.style.Theme_MagiskModuleManager_Monet_Light : R.style.Theme_MagiskModuleManager_Light;
299234
break;
300235
}
301236
// Handle force black theme
302237
if (theme.equals("dark") && isForceBlackThemeEnabled()) {
303238
// just black background
304-
themeResId = monet ?
305-
R.style.Theme_MagiskModuleManager_Monet_Black :
306-
R.style.Theme_MagiskModuleManager_Black;
239+
themeResId = monet ? R.style.Theme_MagiskModuleManager_Monet_Black : R.style.Theme_MagiskModuleManager_Black;
307240
}
308241
this.setManagerThemeResId(themeResId);
309242
}
@@ -317,14 +250,21 @@ public int getManagerThemeResId() {
317250
return managerThemeResId;
318251
}
319252

253+
@SuppressLint("NonConstantResourceId")
254+
public void setManagerThemeResId(@StyleRes int resId) {
255+
this.managerThemeResId = resId;
256+
if (this.markwonThemeContext != null) {
257+
this.markwonThemeContext.setTheme(resId);
258+
}
259+
this.markwon = null;
260+
}
261+
320262
@SuppressLint("NonConstantResourceId")
321263
public boolean isLightTheme() {
322264
switch (this.managerThemeResId) {
323265
case R.style.Theme_MagiskModuleManager:
324266
case R.style.Theme_MagiskModuleManager_Monet:
325-
return (this.getResources().getConfiguration().uiMode
326-
& Configuration.UI_MODE_NIGHT_MASK)
327-
!= Configuration.UI_MODE_NIGHT_YES;
267+
return (this.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) != Configuration.UI_MODE_NIGHT_YES;
328268
case R.style.Theme_MagiskModuleManager_Monet_Light:
329269
case R.style.Theme_MagiskModuleManager_Light:
330270
return true;
@@ -353,14 +293,12 @@ public void onCreate() {
353293
}*/
354294
SharedPreferences sharedPreferences = MainApplication.getSharedPreferences();
355295
// We are only one process so it's ok to do this
356-
SharedPreferences bootPrefs = MainApplication.bootSharedPreferences =
357-
this.getSharedPreferences("mmm_boot", MODE_PRIVATE);
296+
SharedPreferences bootPrefs = MainApplication.bootSharedPreferences = this.getSharedPreferences("mmm_boot", MODE_PRIVATE);
358297
long lastBoot = System.currentTimeMillis() - SystemClock.elapsedRealtime();
359298
long lastBootPrefs = bootPrefs.getLong("last_boot", 0);
360299
if (lastBootPrefs == 0 || Math.abs(lastBoot - lastBootPrefs) > 100) {
361300
boolean firstBoot = sharedPreferences.getBoolean("first_boot", true);
362-
bootPrefs.edit().clear().putLong("last_boot", lastBoot)
363-
.putBoolean("first_boot", firstBoot).apply();
301+
bootPrefs.edit().clear().putLong("last_boot", lastBoot).putBoolean("first_boot", firstBoot).apply();
364302
if (firstBoot) {
365303
sharedPreferences.edit().putBoolean("first_boot", false).apply();
366304
}
@@ -374,12 +312,10 @@ public void onCreate() {
374312
// Update SSL Ciphers if update is possible
375313
GMSProviderInstaller.installIfNeeded(this);
376314
// Update emoji config
377-
FontRequestEmojiCompatConfig fontRequestEmojiCompatConfig =
378-
DefaultEmojiCompatConfig.create(this);
315+
FontRequestEmojiCompatConfig fontRequestEmojiCompatConfig = DefaultEmojiCompatConfig.create(this);
379316
if (fontRequestEmojiCompatConfig != null) {
380317
fontRequestEmojiCompatConfig.setReplaceAll(true);
381-
fontRequestEmojiCompatConfig
382-
.setMetadataLoadStrategy(EmojiCompat.LOAD_STRATEGY_MANUAL);
318+
fontRequestEmojiCompatConfig.setMetadataLoadStrategy(EmojiCompat.LOAD_STRATEGY_MANUAL);
383319
EmojiCompat emojiCompat = EmojiCompat.init(fontRequestEmojiCompatConfig);
384320
new Thread(() -> {
385321
Log.d("MainApplication", "Loading emoji compat...");
@@ -408,9 +344,35 @@ public void onConfigurationChanged(@NonNull Configuration newConfig) {
408344
Locale newTimeFormatLocale = newConfig.locale;
409345
if (timeFormatLocale != newTimeFormatLocale) {
410346
timeFormatLocale = newTimeFormatLocale;
411-
timeFormat = new SimpleDateFormat(
412-
timeFormatString, timeFormatLocale);
347+
timeFormat = new SimpleDateFormat(timeFormatString, timeFormatLocale);
413348
}
414349
super.onConfigurationChanged(newConfig);
415350
}
351+
352+
private class Prism4jSwitchTheme implements Prism4jTheme {
353+
private final Prism4jTheme light = new Prism4jThemeDefault(Color.TRANSPARENT);
354+
private final Prism4jTheme dark = new Prism4jThemeDarkula(Color.TRANSPARENT);
355+
// Black theme
356+
private final Prism4jTheme black = new Prism4jThemeDefault(Color.BLACK);
357+
358+
private Prism4jTheme getTheme() {
359+
// isLightTheme() means light, isDarkTheme() means dark, and isBlackTheme() means black
360+
return isLightTheme() ? light : isDarkTheme() ? dark : black;
361+
}
362+
363+
@Override
364+
public int background() {
365+
return this.getTheme().background();
366+
}
367+
368+
@Override
369+
public int textColor() {
370+
return this.getTheme().textColor();
371+
}
372+
373+
@Override
374+
public void apply(@NonNull String language, @NonNull Prism4j.Syntax syntax, @NonNull SpannableStringBuilder builder, int start, int end) {
375+
this.getTheme().apply(language, syntax, builder, start, end);
376+
}
377+
}
416378
}

app/src/main/java/com/fox2code/mmm/settings/SettingsActivity.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -137,20 +137,6 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
137137
}, 1);
138138
return true;
139139
});
140-
// Force black theme - depends on themePreference being system or dark
141-
SwitchPreferenceCompat forceBlackPreference = findPreference("pref_force_black_theme");
142-
// Set invisible if theme is not dark or system
143-
forceBlackPreference.setVisible(themePreference.getValue().equals("dark") ||
144-
themePreference.getValue().equals("system"));
145-
forceBlackPreference.setOnPreferenceChangeListener((preference, newValue) -> {
146-
devModeStep = 0;
147-
UiThreadHandler.handler.postDelayed(() -> {
148-
MainApplication.getINSTANCE().updateTheme();
149-
FoxActivity.getFoxActivity(this).setThemeRecreate(
150-
MainApplication.getINSTANCE().getManagerThemeResId());
151-
}, 1);
152-
return true;
153-
});
154140
// Crash reporting
155141
TwoStatePreference crashReportingPreference = findPreference("pref_crash_reporting");
156142
if (!SentryMain.IS_SENTRY_INSTALLED) crashReportingPreference.setVisible(false);

app/src/main/res/values-v31/themes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
<item name="cardBackgroundColor">@color/black</item>
3131
<item name="backgroundColor">@color/black</item>
3232
<item name="colorSurface">@color/black</item>
33+
<!-- Darker variants of the colors -->
34+
<item name="colorPrimaryVariant">@color/system_accent2_900</item>
35+
<item name="colorSecondaryVariant">@color/system_accent2_900</item>
3336
</style>
3437

3538
<style name="Widget.Material3.Chip.Choice.Dark" parent="Widget.Material3.Chip.Assist">

app/src/main/res/values/arrays.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
<string-array name="theme_values" translatable="false">
44
<item>system</item>
55
<item>dark</item>
6+
<item>black</item>
67
<item>light</item>
78
</string-array>
89

910
<string-array name="theme_values_names">
1011
<item>System</item>
1112
<item>Dark</item>
13+
<item>AMOLED Black</item>
1214
<item>Light</item>
1315
</string-array>
1416
<string-array name="permission_notification_dont_ask_again">Don't prompt again</string-array>

app/src/main/res/values/themes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@
9191
<item name="cardBackgroundColor">@color/black</item>
9292
<item name="backgroundColor">@color/black</item>
9393
<item name="colorSurface">@color/black</item>
94+
<!-- Darker variants of the colors -->
95+
<item name="colorPrimaryVariant">@color/system_accent2_900</item>
96+
<item name="colorSecondaryVariant">@color/system_accent2_900</item>
9497
</style>
9598

9699
<style name="Widget.Material.Chip.Choice.Dark" parent="Widget.MaterialComponents.Chip.Action">

app/src/main/res/xml/root_preferences.xml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,6 @@
5656
app:singleLineTitle="false"
5757
app:title="@string/theme_pref" />
5858

59-
<!-- Force black theme -->
60-
<SwitchPreferenceCompat
61-
app:defaultValue="false"
62-
app:icon="@drawable/ic_baseline_palette_24"
63-
app:key="pref_force_black_theme"
64-
app:singleLineTitle="false"
65-
app:summary="@string/force_black_theme_desc"
66-
app:title="@string/force_black_theme_pref" />
67-
6859
<Preference
6960
app:icon="@drawable/ic_baseline_language_24"
7061
app:key="pref_language_selector"

0 commit comments

Comments
 (0)