4747import io .noties .prism4j .Prism4j ;
4848import 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}
0 commit comments