66import android .content .SharedPreferences ;
77import android .content .res .Configuration ;
88import android .content .res .Resources ;
9+ import android .graphics .Color ;
910import android .os .SystemClock ;
11+ import android .text .SpannableStringBuilder ;
1012
1113import androidx .annotation .NonNull ;
1214import androidx .annotation .StyleRes ;
2729import io .noties .markwon .html .HtmlPlugin ;
2830import io .noties .markwon .image .ImagesPlugin ;
2931import io .noties .markwon .image .network .OkHttpNetworkSchemeHandler ;
32+ import io .noties .markwon .syntax .Prism4jTheme ;
33+ import io .noties .markwon .syntax .Prism4jThemeDarkula ;
34+ import io .noties .markwon .syntax .Prism4jThemeDefault ;
35+ import io .noties .markwon .syntax .SyntaxHighlightPlugin ;
36+ import io .noties .prism4j .Prism4j ;
37+ import io .noties .prism4j .annotations .PrismBundle ;
3038
39+ @ PrismBundle (
40+ includeAll = true ,
41+ grammarLocatorClassName = ".Prism4jGrammarLocator"
42+ )
3143public class MainApplication extends Application implements CompatActivity .ApplicationCallbacks {
3244 private static final String timeFormatString = "dd MMM yyyy" ; // Example: 13 july 2001
3345 private static Locale timeFormatLocale =
@@ -38,6 +50,7 @@ public class MainApplication extends Application implements CompatActivity.Appli
3850 private static final int secret ;
3951 private static SharedPreferences bootSharedPreferences ;
4052 private static MainApplication INSTANCE ;
53+ private static boolean firstBoot ;
4154
4255 static {
4356 Shell .setDefaultBuilder (shellBuilder = Shell .Builder .create ()
@@ -75,6 +88,32 @@ public static boolean isForceDarkTerminal() {
7588 return getSharedPreferences ().getBoolean ("pref_force_dark_terminal" , false );
7689 }
7790
91+ public static boolean isDeveloper () {
92+ return BuildConfig .DEBUG ||
93+ getSharedPreferences ().getBoolean ("developer" , false );
94+ }
95+
96+ public static boolean isUsingMagiskCommand () {
97+ return InstallerInitializer .peekMagiskVersion () >= Constants .MAGISK_VER_CODE_INSTALL_COMMAND
98+ && getSharedPreferences ().getBoolean ("pref_use_magisk_install_command" , false )
99+ && isDeveloper ();
100+ }
101+
102+ public static boolean isFirstBoot () {
103+ return firstBoot ;
104+ }
105+
106+ public static void notifyBootListenerCompleted () {
107+ if (MainApplication .bootSharedPreferences != null ) {
108+ MainApplication .bootSharedPreferences .edit ()
109+ .putBoolean ("first_boot" , false ).apply ();
110+ } else if (MainApplication .INSTANCE != null ) {
111+ MainApplication .getSharedPreferences ().edit ()
112+ .putBoolean ("first_boot" , false ).apply ();
113+ }
114+ firstBoot = false ;
115+ }
116+
78117 public static boolean hasGottenRootAccess () {
79118 return getSharedPreferences ().getBoolean ("has_root_access" , false );
80119 }
@@ -104,18 +143,48 @@ public static String formatTime(long timeStamp) {
104143 public Markwon getMarkwon () {
105144 if (this .markwon != null )
106145 return this .markwon ;
107- ContextThemeWrapper contextThemeWrapper = this .markwonThemeContext =
108- new ContextThemeWrapper (this , this .managerThemeResId );
146+ ContextThemeWrapper contextThemeWrapper = this .markwonThemeContext ;
147+ if (contextThemeWrapper == null )
148+ contextThemeWrapper = this .markwonThemeContext =
149+ new ContextThemeWrapper (this , this .managerThemeResId );
109150 Markwon markwon = Markwon .builder (contextThemeWrapper ).usePlugin (HtmlPlugin .create ())
151+ .usePlugin (SyntaxHighlightPlugin .create (
152+ new Prism4j (new Prism4jGrammarLocator ()), new Prism4jSwitchTheme ()))
110153 .usePlugin (ImagesPlugin .create ().addSchemeHandler (
111154 OkHttpNetworkSchemeHandler .create (Http .getHttpclientWithCache ()))).build ();
112155 return this .markwon = markwon ;
113156 }
114157
158+ private class Prism4jSwitchTheme implements Prism4jTheme {
159+ private final Prism4jTheme light = new Prism4jThemeDefault (Color .TRANSPARENT );
160+ private final Prism4jTheme dark = new Prism4jThemeDarkula (Color .TRANSPARENT );
161+
162+ private Prism4jTheme getTheme () {
163+ return isLightTheme () ? this .light : this .dark ;
164+ }
165+
166+ @ Override
167+ public int background () {
168+ return this .getTheme ().background ();
169+ }
170+
171+ @ Override
172+ public int textColor () {
173+ return this .getTheme ().textColor ();
174+ }
175+
176+ @ Override
177+ public void apply (@ NonNull String language , @ NonNull Prism4j .Syntax syntax ,
178+ @ NonNull SpannableStringBuilder builder , int start , int end ) {
179+ this .getTheme ().apply (language , syntax , builder , start , end );
180+ }
181+ }
182+
115183 public void setManagerThemeResId (@ StyleRes int resId ) {
116184 this .managerThemeResId = resId ;
117185 if (this .markwonThemeContext != null )
118186 this .markwonThemeContext .setTheme (resId );
187+ this .markwon = null ;
119188 }
120189
121190 @ StyleRes
@@ -144,13 +213,22 @@ public boolean isLightTheme() {
144213 public void onCreate () {
145214 INSTANCE = this ;
146215 super .onCreate ();
216+ SharedPreferences sharedPreferences = MainApplication .getSharedPreferences ();
147217 // We are only one process so it's ok to do this
148218 SharedPreferences bootPrefs = MainApplication .bootSharedPreferences =
149219 this .getSharedPreferences ("mmm_boot" , MODE_PRIVATE );
150220 long lastBoot = System .currentTimeMillis () - SystemClock .elapsedRealtime ();
151221 long lastBootPrefs = bootPrefs .getLong ("last_boot" , 0 );
152222 if (lastBootPrefs == 0 || Math .abs (lastBoot - lastBootPrefs ) > 100 ) {
153- bootPrefs .edit ().clear ().putLong ("last_boot" , lastBoot ).apply ();
223+ boolean firstBoot = sharedPreferences .getBoolean ("first_boot" , true );
224+ bootPrefs .edit ().clear ().putLong ("last_boot" , lastBoot )
225+ .putBoolean ("first_boot" , firstBoot ).apply ();
226+ if (firstBoot ) {
227+ sharedPreferences .edit ().putBoolean ("first_boot" , false ).apply ();
228+ }
229+ MainApplication .firstBoot = firstBoot ;
230+ } else {
231+ MainApplication .firstBoot = bootPrefs .getBoolean ("first_boot" , false );
154232 }
155233 @ StyleRes int themeResId ;
156234 switch (getSharedPreferences ().getString ("pref_theme" , "system" )) {
0 commit comments