2626import android .widget .Toast ;
2727
2828import androidx .annotation .NonNull ;
29+ import androidx .annotation .RequiresApi ;
30+ import androidx .appcompat .app .AlertDialog ;
2931import androidx .appcompat .widget .SearchView ;
3032import androidx .cardview .widget .CardView ;
3133import androidx .core .app .NotificationManagerCompat ;
5153import com .fox2code .mmm .utils .Http ;
5254import com .fox2code .mmm .utils .IntentHelper ;
5355import com .google .android .material .dialog .MaterialAlertDialogBuilder ;
56+ import com .google .android .material .materialswitch .MaterialSwitch ;
5457import com .google .android .material .progressindicator .LinearProgressIndicator ;
5558
5659import org .chromium .net .ExperimentalCronetEngine ;
6164import java .io .IOException ;
6265import java .net .HttpURLConnection ;
6366import java .net .URL ;
67+ import java .util .Objects ;
6468
6569import eightbitlab .com .blurview .BlurView ;
6670
@@ -168,7 +172,6 @@ public void onPathReceived(String path) {
168172 moduleViewListBuilder .addNotification (NotificationType .MAGISK_OUTDATED );
169173 if (!MainApplication .isShowcaseMode ())
170174 moduleViewListBuilder .addNotification (NotificationType .INSTALL_FROM_STORAGE );
171- ensurePermissions ();
172175 ModuleManager .getINSTANCE ().scan ();
173176 ModuleManager .getINSTANCE ().runAfterScan (moduleViewListBuilder ::appendInstalledModules );
174177 this .commonNext ();
@@ -195,6 +198,22 @@ public void commonNext() {
195198 // Fix insets not being accounted for correctly
196199 updateScreenInsets (getResources ().getConfiguration ());
197200 });
201+
202+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
203+ showSetupBox ();
204+
205+ // Wait for pref_first_launch to be false
206+ SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences (MainActivity .this );
207+ while (prefs .getBoolean ("pref_first_launch" , true )) {
208+ try {
209+ //noinspection BusyWait
210+ Thread .sleep (100 );
211+ } catch (InterruptedException e ) {
212+ e .printStackTrace ();
213+ }
214+ }
215+ }
216+ ensurePermissions ();
198217 Log .i (TAG , "Scanning for modules!" );
199218 if (BuildConfig .DEBUG ) Log .d ("NoodleDebug" , "Initialize Update" );
200219 final int max = ModuleManager .getINSTANCE ().getUpdatableModuleCount ();
@@ -260,6 +279,16 @@ public void commonNext() {
260279 String lastEventId = preferences .getString ("lastEventId" , "" );
261280 if (BuildConfig .DEBUG ) Log .d ("NoodleDebug" , "Last Event ID: " + lastEventId );
262281 if (!lastEventId .equals ("" )) {
282+ try {
283+ ExperimentalCronetEngine cronetEngine = new ExperimentalCronetEngine .Builder (this ).build ();
284+ CronetURLStreamHandlerFactory cronetURLStreamHandlerFactory = new CronetURLStreamHandlerFactory (cronetEngine );
285+ URL .setURLStreamHandlerFactory (cronetURLStreamHandlerFactory );
286+ } catch (Exception e ) {
287+ if (BuildConfig .DEBUG ) {
288+ Log .w (TAG , "Failed to setup cronet HTTPURLConnection factory" , e );
289+ Log .w (TAG , "This might mean the factory is already set" );
290+ }
291+ }
263292 // Three edit texts for the user to enter their email, name and a description of the issue
264293 EditText email = new EditText (this );
265294 email .setHint (R .string .email );
@@ -320,9 +349,7 @@ public void commonNext() {
320349 if (connection .getResponseCode () == 200 ) {
321350 runOnUiThread (() -> Toast .makeText (this , R .string .sentry_dialogue_success , Toast .LENGTH_LONG ).show ());
322351 } else {
323- runOnUiThread (() -> Toast .makeText (this ,
324- R .string .sentry_dialogue_failed_toast ,
325- Toast .LENGTH_LONG ).show ());
352+ runOnUiThread (() -> Toast .makeText (this , R .string .sentry_dialogue_failed_toast , Toast .LENGTH_LONG ).show ());
326353 }
327354 } catch (IOException | JSONException ignored ) {
328355 // Show a toast if the user feedback could not be submitted
@@ -619,4 +646,53 @@ private void ensurePermissions() {
619646 }
620647 }
621648 }
649+
650+ // Method to show a setup box on first launch
651+ @ RequiresApi (api = Build .VERSION_CODES .M )
652+ @ SuppressLint ({"InflateParams" , "RestrictedApi" , "UnspecifiedImmutableFlag" , "ApplySharedPref" })
653+ private void showSetupBox () {
654+ // Check if this is the first launch
655+ if (PreferenceManager .getDefaultSharedPreferences (this ).getBoolean ("pref_first_launch" , true )) {
656+ MainApplication .getBootSharedPreferences ().edit ().putBoolean ("mm_first_scan" , false ).commit ();
657+ // Show setup box
658+ runOnUiThread (() -> {
659+ MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder (this );
660+ builder .setCancelable (false );
661+ builder .setTitle (R .string .setup_title );
662+ builder .setView (getLayoutInflater ().inflate (R .layout .setup_box , null ));
663+ // For now, we'll just have the positive button save the preferences and dismiss the dialog
664+ builder .setPositiveButton (R .string .setup_button , (dialog , which ) -> {
665+ // Set the preferences
666+ SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences (this );
667+ prefs .edit ().putBoolean ("pref_background_update_check" ,
668+ ((MaterialSwitch ) Objects .requireNonNull (((AlertDialog ) dialog ).findViewById (R .id .setup_background_update_check ))).isChecked ()).commit ();
669+ prefs .edit ().putBoolean ("pref_crash_reporting" , ((MaterialSwitch ) Objects .requireNonNull (((AlertDialog ) dialog ).findViewById (R .id .setup_crash_reporting ))).isChecked ()).commit ();
670+ prefs .edit ().putBoolean ("pref_androidacy_repo_enabled" , ((MaterialSwitch ) Objects .requireNonNull (((AlertDialog ) dialog ).findViewById (R .id .setup_androidacy_repo ))).isChecked ()).commit ();
671+ prefs .edit ().putBoolean ("pref_magisk_alt_repo_enabled" , ((MaterialSwitch ) Objects .requireNonNull (((AlertDialog ) dialog ).findViewById (R .id .setup_magisk_alt_repo ))).isChecked ()).commit ();
672+ if (BuildConfig .DEBUG ) {
673+ Log .d ("MainActivity" , String .format ("Background update check: %s, Crash reporting: %s, Androidacy repo: %s, Magisk alt repo: %s" ,
674+ prefs .getBoolean ("pref_background_update_check" , false ),
675+ prefs .getBoolean ("pref_crash_reporting" , false ),
676+ prefs .getBoolean ("pref_androidacy_repo_enabled" , false ),
677+ prefs .getBoolean ("pref_magisk_alt_repo_enabled" , false )));
678+ }
679+ // Set pref_first_launch to false
680+ PreferenceManager .getDefaultSharedPreferences (this ).edit ().putBoolean ("pref_first_launch" ,
681+ false ).commit ();
682+ // Restart the app
683+ Intent intent = new Intent (this , MainActivity .class );
684+ intent .addFlags (Intent .FLAG_ACTIVITY_CLEAR_TOP );
685+ startActivity (intent );
686+ finish ();
687+ });
688+ builder .setNegativeButton (R .string .setup_button_skip , (dialog , which ) -> {
689+ // Set pref_first_launch to false
690+ PreferenceManager .getDefaultSharedPreferences (this ).edit ().putBoolean ("pref_first_launch" ,
691+ false ).commit ();
692+ dialog .dismiss ();
693+ });
694+ builder .show ();
695+ });
696+ }
697+ }
622698}
0 commit comments