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

Commit 9a79029

Browse files
Work on setup experience
Signed-off-by: androidacy-user <opensource@androidacy.com>
1 parent 18d07d8 commit 9a79029

File tree

8 files changed

+211
-145
lines changed

8 files changed

+211
-145
lines changed

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

Lines changed: 49 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import android.widget.Toast;
2727

2828
import androidx.annotation.NonNull;
29-
import androidx.appcompat.app.AlertDialog;
29+
import androidx.appcompat.app.ActionBar;
3030
import androidx.appcompat.widget.SearchView;
3131
import androidx.cardview.widget.CardView;
3232
import androidx.core.app.ActivityCompat;
@@ -52,6 +52,7 @@
5252
import com.fox2code.mmm.utils.ExternalHelper;
5353
import com.fox2code.mmm.utils.Http;
5454
import com.fox2code.mmm.utils.IntentHelper;
55+
import com.google.android.material.button.MaterialButton;
5556
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
5657
import com.google.android.material.materialswitch.MaterialSwitch;
5758
import com.google.android.material.progressindicator.LinearProgressIndicator;
@@ -128,10 +129,12 @@ protected void onCreate(Bundle savedInstanceState) {
128129
}
129130
BackgroundUpdateChecker.onMainActivityCreate(this);
130131
super.onCreate(savedInstanceState);
131-
this.setActionBarExtraMenuButton(R.drawable.ic_baseline_settings_24, v -> {
132-
IntentHelper.startActivity(this, SettingsActivity.class);
133-
return true;
134-
}, R.string.pref_category_settings);
132+
if (!MainApplication.getSharedPreferences().getBoolean("first_run", true)) {
133+
this.setActionBarExtraMenuButton(R.drawable.ic_baseline_settings_24, v -> {
134+
IntentHelper.startActivity(this, SettingsActivity.class);
135+
return true;
136+
}, R.string.pref_category_settings);
137+
}
135138
setContentView(R.layout.activity_main);
136139
this.setTitle(R.string.app_name);
137140
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
@@ -740,53 +743,47 @@ private void checkShowInitialSetup() {
740743
Log.i("SetupWizard", "First launch: " + firstLaunch);
741744
if (firstLaunch) {
742745
doSetupNowRunning = true;
743-
// Show setup box
744-
runOnUiThread(() -> {
745-
MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(this);
746-
builder.setCancelable(false);
747-
builder.setTitle(R.string.setup_title);
748-
// Create a view from R.xml.setup_box
749-
View view = getLayoutInflater().inflate(R.layout.setup_box, null);
750-
builder.setView(view);
751-
// For sdk >= 31, use MaterialSwitch instead of MaterialSwitch
752-
// For now, we'll just have the positive button save the preferences and dismiss the dialog
753-
builder.setPositiveButton(R.string.setup_button, (dialog, which) -> {
754-
// Set the preferences and pref_first_launch to false
755-
prefs.edit().putBoolean("first_launch", false).putBoolean("pref_background_update_check", ((MaterialSwitch) Objects.requireNonNull(((AlertDialog) dialog).findViewById(R.id.setup_background_update_check))).isChecked()).putBoolean("pref_crash_reporting", ((MaterialSwitch) Objects.requireNonNull(((AlertDialog) dialog).findViewById(R.id.setup_crash_reporting))).isChecked()).putBoolean("pref_androidacy_repo_enabled", ((MaterialSwitch) Objects.requireNonNull(((AlertDialog) dialog).findViewById(R.id.setup_androidacy_repo))).isChecked()).putBoolean("pref_magisk_alt_repo_enabled", ((MaterialSwitch) Objects.requireNonNull(((AlertDialog) dialog).findViewById(R.id.setup_magisk_alt_repo))).isChecked()).commit();
756-
// For debug builds, log the preferences
757-
if (BuildConfig.DEBUG) {
758-
Log.i("SetupWizard", "First launch: " + prefs.getBoolean("first_launch", true));
759-
Log.i("SetupWizard", "Background update check: " + prefs.getBoolean("pref_background_update_check", false));
760-
Log.i("SetupWizard", "Crash reporting: " + prefs.getBoolean("pref_crash_reporting", false));
761-
Log.i("SetupWizard", "Androidacy repo: " + prefs.getBoolean("pref_androidacy_repo_enabled", false));
762-
Log.i("SetupWizard", "Magisk alt repo: " + prefs.getBoolean("pref_magisk_alt_repo_enabled", false));
763-
}
764-
dialog.dismiss();
765-
// Sleep for 100ms. Who knows, it might fix it?
766-
try {
767-
Thread.sleep(750);
768-
} catch (InterruptedException e) {
769-
e.printStackTrace();
770-
}
771-
doSetupRestarting = true;
772-
// Restart the app
773-
Intent intent = new Intent(this, MainActivity.class);
774-
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
775-
finish();
776-
startActivity(intent);
777-
});
778-
builder.setNegativeButton(R.string.setup_button_skip, (dialog, which) -> {
779-
MainApplication.getSharedPreferences().edit().putBoolean("first_launch", false).commit();
780-
dialog.dismiss();
781-
ensurePermissions();
782-
});
783-
builder.show();
784-
// Set the switches appropriately
785-
((MaterialSwitch) Objects.requireNonNull(view.findViewById(R.id.setup_background_update_check))).setChecked(BuildConfig.ENABLE_AUTO_UPDATER);
786-
((MaterialSwitch) Objects.requireNonNull(view.findViewById(R.id.setup_crash_reporting))).setChecked(BuildConfig.DEFAULT_ENABLE_CRASH_REPORTING);
787-
// Repos are a little harder, as the enabled_repos build config is an arraylist
788-
((MaterialSwitch) Objects.requireNonNull(view.findViewById(R.id.setup_androidacy_repo))).setChecked(BuildConfig.ENABLED_REPOS.contains("androidacy_repo"));
789-
((MaterialSwitch) Objects.requireNonNull(view.findViewById(R.id.setup_magisk_alt_repo))).setChecked(BuildConfig.ENABLED_REPOS.contains("magisk_alt_repo"));
746+
// Show setup box. Put the setup_box in the main activity layout
747+
View view = getLayoutInflater().inflate(R.layout.setup_box, null);
748+
// Make the setup_box linear layout the sole child of the root_container constraint layout
749+
setContentView(view);
750+
// Handle action bar. Set it to setup_title and make it visible
751+
ActionBar actionBar = getSupportActionBar();
752+
if (actionBar != null) {
753+
actionBar.setTitle(R.string.app_name);
754+
// Set solid color background
755+
actionBar.show();
756+
}
757+
((MaterialSwitch) Objects.requireNonNull(view.findViewById(R.id.setup_background_update_check))).setChecked(BuildConfig.ENABLE_AUTO_UPDATER);
758+
((MaterialSwitch) Objects.requireNonNull(view.findViewById(R.id.setup_crash_reporting))).setChecked(BuildConfig.DEFAULT_ENABLE_CRASH_REPORTING);
759+
// Repos are a little harder, as the enabled_repos build config is an arraylist
760+
((MaterialSwitch) Objects.requireNonNull(view.findViewById(R.id.setup_androidacy_repo))).setChecked(BuildConfig.ENABLED_REPOS.contains("androidacy_repo"));
761+
((MaterialSwitch) Objects.requireNonNull(view.findViewById(R.id.setup_magisk_alt_repo))).setChecked(BuildConfig.ENABLED_REPOS.contains("magisk_alt_repo"));
762+
// Set up the buttons
763+
// Cancel button
764+
MaterialButton cancelButton = view.findViewById(R.id.setup_cancel);
765+
cancelButton.setText(R.string.cancel);
766+
cancelButton.setOnClickListener(v -> {
767+
// Set first launch to false and finish the activity
768+
prefs.edit().putBoolean("first_launch", false).commit();
769+
finish();
770+
});
771+
// Setup button
772+
MaterialButton setupButton = view.findViewById(R.id.setup_continue);
773+
setupButton.setText(R.string.setup_button);
774+
setupButton.setOnClickListener(v -> {
775+
// Set first launch to false
776+
prefs.edit().putBoolean("first_launch", false).commit();
777+
// Set the background update check pref
778+
prefs.edit().putBoolean("pref_background_update_check", ((MaterialSwitch) Objects.requireNonNull(view.findViewById(R.id.setup_background_update_check))).isChecked()).commit();
779+
// Set the crash reporting pref
780+
prefs.edit().putBoolean("pref_crash_reporting", ((MaterialSwitch) Objects.requireNonNull(view.findViewById(R.id.setup_crash_reporting))).isChecked()).commit();
781+
// Set the repos
782+
// first pref_magisk_alt_repo_enabled then pref_androidacy_repo_enabled
783+
prefs.edit().putBoolean("pref_magisk_alt_repo_enabled", ((MaterialSwitch) Objects.requireNonNull(view.findViewById(R.id.setup_magisk_alt_repo))).isChecked()).commit();
784+
prefs.edit().putBoolean("pref_androidacy_repo_enabled", ((MaterialSwitch) Objects.requireNonNull(view.findViewById(R.id.setup_androidacy_repo))).isChecked()).commit();
785+
// Finish the activity
786+
finish();
790787
});
791788
} else {
792789
ensurePermissions();
Lines changed: 36 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.fox2code.mmm.background;
22

3+
import android.Manifest;
34
import android.app.PendingIntent;
45
import android.content.Context;
56
import android.content.Intent;
6-
import android.os.Build;
7+
import android.content.pm.PackageManager;
78

89
import androidx.annotation.NonNull;
10+
import androidx.core.app.ActivityCompat;
911
import androidx.core.app.NotificationChannelCompat;
1012
import androidx.core.app.NotificationCompat;
1113
import androidx.core.app.NotificationManagerCompat;
@@ -31,46 +33,30 @@
3133
import java.util.concurrent.TimeUnit;
3234

3335
public class BackgroundUpdateChecker extends Worker {
34-
private static boolean easterEggActive = false;
35-
static final Object lock = new Object(); // Avoid concurrency issues
3636
public static final String NOTIFICATION_CHANNEL_ID = "background_update";
3737
public static final int NOTIFICATION_ID = 1;
38+
static final Object lock = new Object(); // Avoid concurrency issues
39+
private static boolean easterEggActive = false;
3840

39-
public BackgroundUpdateChecker(@NonNull Context context,
40-
@NonNull WorkerParameters workerParams) {
41+
public BackgroundUpdateChecker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
4142
super(context, workerParams);
4243
}
4344

44-
@NonNull
45-
@Override
46-
public Result doWork() {
47-
if (!NotificationManagerCompat.from(this.getApplicationContext()).areNotificationsEnabled()
48-
|| !MainApplication.isBackgroundUpdateCheckEnabled()) return Result.success();
49-
synchronized (lock) {
50-
doCheck(this.getApplicationContext());
51-
}
52-
return Result.success();
53-
}
54-
5545
static void doCheck(Context context) {
5646
Thread.currentThread().setPriority(2);
5747
ModuleManager.getINSTANCE().scanAsync();
5848
RepoManager.getINSTANCE().update(null);
5949
ModuleManager.getINSTANCE().runAfterScan(() -> {
6050
int moduleUpdateCount = 0;
61-
HashMap<String, RepoModule> repoModules =
62-
RepoManager.getINSTANCE().getModules();
63-
for (LocalModuleInfo localModuleInfo :
64-
ModuleManager.getINSTANCE().getModules().values()) {
65-
if ("twrp-keep".equals(localModuleInfo.id)) continue;
51+
HashMap<String, RepoModule> repoModules = RepoManager.getINSTANCE().getModules();
52+
for (LocalModuleInfo localModuleInfo : ModuleManager.getINSTANCE().getModules().values()) {
53+
if ("twrp-keep".equals(localModuleInfo.id))
54+
continue;
6655
RepoModule repoModule = repoModules.get(localModuleInfo.id);
6756
localModuleInfo.checkModuleUpdate();
68-
if (localModuleInfo.updateVersionCode > localModuleInfo.versionCode &&
69-
!PropUtils.isNullString(localModuleInfo.updateVersion)) {
57+
if (localModuleInfo.updateVersionCode > localModuleInfo.versionCode && !PropUtils.isNullString(localModuleInfo.updateVersion)) {
7058
moduleUpdateCount++;
71-
} else if (repoModule != null &&
72-
repoModule.moduleInfo.versionCode > localModuleInfo.versionCode &&
73-
!PropUtils.isNullString(repoModule.moduleInfo.version)) {
59+
} else if (repoModule != null && repoModule.moduleInfo.versionCode > localModuleInfo.versionCode && !PropUtils.isNullString(repoModule.moduleInfo.version)) {
7460
moduleUpdateCount++;
7561
}
7662
}
@@ -81,43 +67,39 @@ static void doCheck(Context context) {
8167
}
8268

8369
public static void postNotification(Context context, int updateCount) {
84-
if (!easterEggActive) easterEggActive = new Random().nextInt(100) <= updateCount;
85-
NotificationCompat.Builder builder = new NotificationCompat.Builder(
86-
context, NOTIFICATION_CHANNEL_ID)
87-
.setContentTitle(context.getString(easterEggActive ?
88-
R.string.notification_update_title_easter_egg :
89-
R.string.notification_update_title)
90-
.replace("%i", String.valueOf(updateCount)))
91-
.setContentText(context.getString(R.string.notification_update_subtitle))
92-
.setSmallIcon(R.drawable.ic_baseline_extension_24)
93-
.setPriority(NotificationCompat.PRIORITY_HIGH)
94-
.setContentIntent(PendingIntent.getActivity(context, 0,
95-
new Intent(context, MainActivity.class).setFlags(
96-
Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK),
97-
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ?
98-
PendingIntent.FLAG_IMMUTABLE : 0)).setAutoCancel(true);
70+
if (!easterEggActive)
71+
easterEggActive = new Random().nextInt(100) <= updateCount;
72+
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID).setContentTitle(context.getString(easterEggActive ? R.string.notification_update_title_easter_egg : R.string.notification_update_title).replace("%i", String.valueOf(updateCount))).setContentText(context.getString(R.string.notification_update_subtitle)).setSmallIcon(R.drawable.ic_baseline_extension_24).setPriority(NotificationCompat.PRIORITY_HIGH).setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK), PendingIntent.FLAG_IMMUTABLE)).setAutoCancel(true);
73+
if (ActivityCompat.checkSelfPermission(MainApplication.getINSTANCE(), Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
74+
return;
75+
}
9976
NotificationManagerCompat.from(context).notify(NOTIFICATION_ID, builder.build());
10077
}
10178

10279
public static void onMainActivityCreate(Context context) {
103-
NotificationManagerCompat notificationManagerCompat =
104-
NotificationManagerCompat.from(context);
105-
notificationManagerCompat.createNotificationChannel(
106-
new NotificationChannelCompat.Builder(NOTIFICATION_CHANNEL_ID,
107-
NotificationManagerCompat.IMPORTANCE_HIGH).setShowBadge(true)
108-
.setName(context.getString(R.string.notification_update_pref)).build());
80+
// Refuse to run if first_launch pref is not false
81+
if (MainApplication.getSharedPreferences().getBoolean("first_launch", true))
82+
return;
83+
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
84+
notificationManagerCompat.createNotificationChannel(new NotificationChannelCompat.Builder(NOTIFICATION_CHANNEL_ID, NotificationManagerCompat.IMPORTANCE_HIGH).setShowBadge(true).setName(context.getString(R.string.notification_update_pref)).build());
10985
notificationManagerCompat.cancel(BackgroundUpdateChecker.NOTIFICATION_ID);
11086
BackgroundUpdateChecker.easterEggActive = false;
111-
WorkManager.getInstance(context).enqueueUniquePeriodicWork("background_checker",
112-
ExistingPeriodicWorkPolicy.REPLACE, new PeriodicWorkRequest.Builder(
113-
BackgroundUpdateChecker.class, 6, TimeUnit.HOURS)
114-
.setConstraints(new Constraints.Builder().setRequiresBatteryNotLow(true)
115-
.setRequiredNetworkType(NetworkType.UNMETERED).build()).build());
87+
WorkManager.getInstance(context).enqueueUniquePeriodicWork("background_checker", ExistingPeriodicWorkPolicy.REPLACE, new PeriodicWorkRequest.Builder(BackgroundUpdateChecker.class, 6, TimeUnit.HOURS).setConstraints(new Constraints.Builder().setRequiresBatteryNotLow(true).setRequiredNetworkType(NetworkType.UNMETERED).build()).build());
11688
}
11789

11890
public static void onMainActivityResume(Context context) {
119-
NotificationManagerCompat.from(context).cancel(
120-
BackgroundUpdateChecker.NOTIFICATION_ID);
91+
NotificationManagerCompat.from(context).cancel(BackgroundUpdateChecker.NOTIFICATION_ID);
12192
BackgroundUpdateChecker.easterEggActive = false;
12293
}
94+
95+
@NonNull
96+
@Override
97+
public Result doWork() {
98+
if (!NotificationManagerCompat.from(this.getApplicationContext()).areNotificationsEnabled() || !MainApplication.isBackgroundUpdateCheckEnabled())
99+
return Result.success();
100+
synchronized (lock) {
101+
doCheck(this.getApplicationContext());
102+
}
103+
return Result.success();
104+
}
123105
}

app/src/main/java/com/fox2code/mmm/repo/RepoManager.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import java.net.HttpURLConnection;
3131
import java.net.URL;
3232
import java.nio.charset.StandardCharsets;
33-
import java.security.NoSuchAlgorithmException;
3433
import java.util.Collection;
3534
import java.util.HashMap;
3635
import java.util.LinkedHashMap;
@@ -245,13 +244,6 @@ protected void scanInternal(@NonNull UpdateListener updateListener) {
245244
if (!repoUpdaters[i].repoData.isEnabled()) {
246245
if (BuildConfig.DEBUG) Log.i("RepoManager",
247246
"Skipping disabled repo: " + repoUpdaters[i].repoData.getName());
248-
// Remove the repo from the list
249-
try {
250-
this.repoData.remove(repoUpdaters[i].repoData.getUrl());
251-
} catch (
252-
NoSuchAlgorithmException e) {
253-
e.printStackTrace();
254-
}
255247
continue;
256248
}
257249
List<RepoModule> repoModules = repoUpdaters[i].toUpdate();

app/src/main/java/com/fox2code/mmm/repo/RepoUpdater.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,6 @@ public int fetchIndex() {
4141
return 0;
4242
}
4343
this.indexRaw = Http.doHttpGet(this.repoData.getUrl(), false);
44-
// Ensure it's a valid json and response code is 200
45-
/*if (Arrays.hashCode(this.indexRaw) == 0) {
46-
this.indexRaw = null;
47-
this.toUpdate = Collections.emptyList();
48-
this.toApply = this.repoData.moduleHashMap.values();
49-
return 0;
50-
}*/
5144
this.toUpdate = this.repoData.populate(new JSONObject(
5245
new String(this.indexRaw, StandardCharsets.UTF_8)));
5346
// Since we reuse instances this should work

0 commit comments

Comments
 (0)