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

Commit 2573313

Browse files
code cleanup
Signed-off-by: androidacy-user <opensource@androidacy.com>
1 parent 5d941ae commit 2573313

File tree

14 files changed

+237
-218
lines changed

14 files changed

+237
-218
lines changed

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

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class AppUpdateManager {
4141
private String latestPreRelease;
4242
private long lastChecked;
4343
private boolean preReleaseNewer;
44-
private boolean lastCheckSuccess;
44+
4545
private AppUpdateManager() {
4646
this.compatFile = new File(MainApplication.getINSTANCE().getFilesDir(), "compat.txt");
4747
this.latestRelease = MainApplication.getBootSharedPreferences().getString("updater_latest_release", BuildConfig.VERSION_NAME);
@@ -51,7 +51,8 @@ private AppUpdateManager() {
5151
if (this.compatFile.isFile()) {
5252
try {
5353
this.parseCompatibilityFlags(new FileInputStream(this.compatFile));
54-
} catch (IOException e) {
54+
} catch (
55+
IOException e) {
5556
e.printStackTrace();
5657
}
5758
}
@@ -73,118 +74,134 @@ public static boolean shouldForceHide(String repoId) {
7374

7475
// Return true if should show a notification
7576
public boolean checkUpdate(boolean force) {
76-
if (!BuildConfig.ENABLE_AUTO_UPDATER) return false;
77-
if (!force && this.peekShouldUpdate()) return true;
77+
if (!BuildConfig.ENABLE_AUTO_UPDATER)
78+
return false;
79+
if (!force && this.peekShouldUpdate())
80+
return true;
7881
long lastChecked = this.lastChecked;
7982
if (lastChecked != 0 &&
8083
// Avoid spam calls by putting a 60 seconds timer
8184
lastChecked < System.currentTimeMillis() - 60000L)
8285
return force && this.peekShouldUpdate();
8386
synchronized (this.updateLock) {
84-
if (lastChecked != this.lastChecked) return this.peekShouldUpdate();
87+
if (lastChecked != this.lastChecked)
88+
return this.peekShouldUpdate();
8589
boolean preReleaseNewer = true;
8690
try {
8791
JSONArray releases = new JSONArray(new String(Http.doHttpGet(RELEASES_API_URL, false), StandardCharsets.UTF_8));
8892
String latestRelease = null, latestPreRelease = null;
8993
for (int i = 0; i < releases.length(); i++) {
9094
JSONObject release = releases.getJSONObject(i);
9195
// Skip invalid entries
92-
if (release.getBoolean("draft")) continue;
96+
if (release.getBoolean("draft"))
97+
continue;
9398
boolean preRelease = release.getBoolean("prerelease");
9499
String version = release.getString("tag_name");
95-
if (version.startsWith("v")) version = version.substring(1);
100+
if (version.startsWith("v"))
101+
version = version.substring(1);
96102
if (preRelease) {
97-
if (latestPreRelease == null) latestPreRelease = version;
103+
if (latestPreRelease == null)
104+
latestPreRelease = version;
98105
} else if (latestRelease == null) {
99106
latestRelease = version;
100-
if (latestPreRelease == null) preReleaseNewer = false;
107+
if (latestPreRelease == null)
108+
preReleaseNewer = false;
101109
}
102110
if (latestRelease != null && latestPreRelease != null) {
103111
break; // We read everything we needed to read.
104112
}
105113
}
106-
if (latestRelease != null) this.latestRelease = latestRelease;
114+
if (latestRelease != null)
115+
this.latestRelease = latestRelease;
107116
if (latestPreRelease != null) {
108117
this.latestPreRelease = latestPreRelease;
109118
this.preReleaseNewer = preReleaseNewer;
110119
} else if (!preReleaseNewer) {
111120
this.latestPreRelease = "";
112121
this.preReleaseNewer = false;
113122
}
114-
if (BuildConfig.DEBUG) Log.i(TAG, "Latest release: " + latestRelease);
115-
if (BuildConfig.DEBUG) Log.i(TAG, "Latest pre-release: " + latestPreRelease);
116-
if (BuildConfig.DEBUG) Log.i(TAG, "Latest pre-release newer: " + preReleaseNewer);
123+
if (BuildConfig.DEBUG)
124+
Log.d(TAG, "Latest release: " + latestRelease);
125+
if (BuildConfig.DEBUG)
126+
Log.d(TAG, "Latest pre-release: " + latestPreRelease);
127+
if (BuildConfig.DEBUG)
128+
Log.d(TAG, "Latest pre-release newer: " + preReleaseNewer);
117129
this.lastChecked = System.currentTimeMillis();
118-
this.lastCheckSuccess = true;
119-
} catch (Exception ioe) {
120-
this.lastCheckSuccess = false;
130+
} catch (
131+
Exception ioe) {
121132
Log.e("AppUpdateManager", "Failed to check releases", ioe);
122133
}
123134
}
124135
return this.peekShouldUpdate();
125136
}
126137

127138
public void checkUpdateCompat() {
128-
if (BuildConfig.DEBUG) Log.i(TAG, "Checking compatibility flags");
139+
if (BuildConfig.DEBUG)
140+
Log.d(TAG, "Checking compatibility flags");
129141
if (this.compatFile.exists()) {
130142
long lastUpdate = this.compatFile.lastModified();
131143
if (lastUpdate <= System.currentTimeMillis() && lastUpdate + 600_000L > System.currentTimeMillis()) {
132144
return; // Skip update
133145
}
134146
}
135147
try {
136-
if (BuildConfig.DEBUG) Log.i(TAG, "Downloading compatibility flags");
148+
if (BuildConfig.DEBUG)
149+
Log.d(TAG, "Downloading compatibility flags");
137150
JSONObject object = new JSONObject(new String(Http.doHttpGet(COMPAT_API_URL, false), StandardCharsets.UTF_8));
138151
if (object.isNull("body")) {
139-
if (BuildConfig.DEBUG) Log.i(TAG, "Compatibility flags not found");
152+
if (BuildConfig.DEBUG)
153+
Log.d(TAG, "Compatibility flags not found");
140154
compatDataId.clear();
141155
Files.write(compatFile, new byte[0]);
142156
return;
143157
}
144-
if (BuildConfig.DEBUG) Log.i(TAG, "Parsing compatibility flags");
158+
if (BuildConfig.DEBUG)
159+
Log.d(TAG, "Parsing compatibility flags");
145160
byte[] rawData = object.getString("body").getBytes(StandardCharsets.UTF_8);
146161
this.parseCompatibilityFlags(new ByteArrayInputStream(rawData));
147162
Files.write(compatFile, rawData);
148-
if (!BuildConfig.ENABLE_AUTO_UPDATER) this.lastCheckSuccess = true;
149-
if (BuildConfig.DEBUG) Log.i(TAG, "Compatibility flags update finishing");
163+
if (BuildConfig.DEBUG)
164+
Log.d(TAG, "Compatibility flags update finishing");
150165
return;
151-
} catch (Exception e) {
152-
if (!BuildConfig.ENABLE_AUTO_UPDATER) this.lastCheckSuccess = false;
166+
} catch (
167+
Exception e) {
153168
Log.e("AppUpdateManager", "Failed to update compat list", e);
154169
}
155-
if (BuildConfig.DEBUG) Log.i(TAG, "Compatibility flags updated");
170+
if (BuildConfig.DEBUG)
171+
Log.d(TAG, "Compatibility flags updated");
156172
}
157173

158174
public boolean peekShouldUpdate() {
159-
if (!BuildConfig.ENABLE_AUTO_UPDATER) return false;
175+
if (!BuildConfig.ENABLE_AUTO_UPDATER)
176+
return false;
160177
// Convert both BuildConfig.VERSION_NAME and latestRelease to int
161178
int currentVersion = 0, latestVersion = 0;
162179
try {
163180
currentVersion = Integer.parseInt(BuildConfig.VERSION_NAME.replace(".", ""));
164181
latestVersion = Integer.parseInt(this.latestRelease.replace(".", ""));
165-
} catch (NumberFormatException ignored) {
182+
} catch (
183+
NumberFormatException ignored) {
166184
}
167185
return currentVersion < latestVersion || (this.preReleaseNewer && currentVersion < Integer.parseInt(this.latestPreRelease.replace(".", "")));
168186
}
169187

170188
public boolean peekHasUpdate() {
171-
if (!BuildConfig.ENABLE_AUTO_UPDATER) return false;
189+
if (!BuildConfig.ENABLE_AUTO_UPDATER)
190+
return false;
172191
return !BuildConfig.VERSION_NAME.equals(this.preReleaseNewer ? this.latestPreRelease : this.latestRelease);
173192
}
174193

175-
public boolean isLastCheckSuccess() {
176-
return lastCheckSuccess;
177-
}
178-
179194
private void parseCompatibilityFlags(InputStream inputStream) throws IOException {
180195
compatDataId.clear();
181196
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
182197
String line;
183198
while ((line = bufferedReader.readLine()) != null) {
184199
line = line.trim();
185-
if (line.isEmpty() || line.startsWith("#")) continue;
200+
if (line.isEmpty() || line.startsWith("#"))
201+
continue;
186202
int i = line.indexOf('/');
187-
if (i == -1) continue;
203+
if (i == -1)
204+
continue;
188205
int value = 0;
189206
for (String arg : line.substring(i + 1).split(",")) {
190207
switch (arg) {

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,13 @@ public void onFailure(int error) {
214214

215215
public void commonNext() {
216216
if (BuildConfig.DEBUG) {
217-
Log.i(TAG, "Common next");
217+
Log.d(TAG, "Common next");
218218
moduleViewListBuilder.addNotification(NotificationType.DEBUG);
219219
}
220220
updateScreenInsets(); // Fix an edge case
221221
if (waitInitialSetupFinished()) {
222222
if (BuildConfig.DEBUG) {
223-
Log.i(TAG, "Initial setup not finished, waiting...");
223+
Log.d(TAG, "Initial setup not finished, waiting...");
224224
}
225225
return;
226226
}
@@ -239,7 +239,7 @@ public void commonNext() {
239239

240240
// On every preferences change, log the change if debug is enabled
241241
if (BuildConfig.DEBUG) {
242-
Log.i("PrefsListener", "onCreate: Preferences: " + MainApplication.getSharedPreferences().getAll());
242+
Log.d("PrefsListener", "onCreate: Preferences: " + MainApplication.getSharedPreferences().getAll());
243243
// Log all preferences changes
244244
MainApplication.getSharedPreferences().registerOnSharedPreferenceChangeListener((prefs, key) -> Log.i("PrefsListener", "onSharedPreferenceChanged: " + key + " = " + prefs.getAll().get(key)));
245245
}
@@ -377,7 +377,7 @@ public void commonNext() {
377377
connection.connect();
378378
// For debug builds, log the response code and response body
379379
if (BuildConfig.DEBUG) {
380-
Log.i("NoodleDebug", "Response Code: " + connection.getResponseCode());
380+
Log.d("NoodleDebug", "Response Code: " + connection.getResponseCode());
381381
}
382382
// Check if the request was successful
383383
if (connection.getResponseCode() == 200) {
@@ -767,7 +767,7 @@ private void checkShowInitialSetup() {
767767
((MaterialSwitch) Objects.requireNonNull(view.findViewById(R.id.setup_magisk_alt_repo))).setChecked(BuildConfig.ENABLED_REPOS.contains("magisk_alt_repo"));
768768
// On debug builds, log when a switch is toggled
769769
if (BuildConfig.DEBUG) {
770-
((MaterialSwitch) Objects.requireNonNull(view.findViewById(R.id.setup_background_update_check))).setOnCheckedChangeListener((buttonView, isChecked) -> Log.i("SetupWizard", "Background Update Check: " + isChecked));
770+
Log.d("SetupWizard", "Background Update Check: " + ((MaterialSwitch) Objects.requireNonNull(view.findViewById(R.id.setup_background_update_check))).isChecked());
771771
((MaterialSwitch) Objects.requireNonNull(view.findViewById(R.id.setup_crash_reporting))).setOnCheckedChangeListener((buttonView, isChecked) -> Log.i("SetupWizard", "Crash Reporting: " + isChecked));
772772
((MaterialSwitch) Objects.requireNonNull(view.findViewById(R.id.setup_androidacy_repo))).setOnCheckedChangeListener((buttonView, isChecked) -> Log.i("SetupWizard", "Androidacy Repo: " + isChecked));
773773
((MaterialSwitch) Objects.requireNonNull(view.findViewById(R.id.setup_magisk_alt_repo))).setOnCheckedChangeListener((buttonView, isChecked) -> Log.i("SetupWizard", "Magisk Alt Repo: " + isChecked));
@@ -793,7 +793,7 @@ private void checkShowInitialSetup() {
793793
case "system":
794794
popupMenu.getMenu().findItem(R.id.theme_system).setChecked(true);
795795
break;
796-
// Black and transparent_light
796+
// Black and transparent_light
797797
case "black":
798798
popupMenu.getMenu().findItem(R.id.theme_black).setChecked(true);
799799
break;
@@ -818,8 +818,7 @@ private void checkShowInitialSetup() {
818818
// Set the theme
819819
UiThreadHandler.handler.postDelayed(() -> {
820820
MainApplication.getINSTANCE().updateTheme();
821-
FoxActivity.getFoxActivity(this).setThemeRecreate(
822-
MainApplication.getINSTANCE().getManagerThemeResId());
821+
FoxActivity.getFoxActivity(this).setThemeRecreate(MainApplication.getINSTANCE().getManagerThemeResId());
823822
}, 1);
824823
return true;
825824
});
@@ -856,12 +855,13 @@ private void checkShowInitialSetup() {
856855
// Sleep for 1 second to allow the user to see the changes
857856
try {
858857
Thread.sleep(500);
859-
} catch (InterruptedException e) {
858+
} catch (
859+
InterruptedException e) {
860860
e.printStackTrace();
861861
}
862862
// Log the changes if debug
863863
if (BuildConfig.DEBUG) {
864-
Log.i("SetupWizard", "Background update check: " + prefs.getBoolean("pref_background_update_check", false));
864+
Log.d("SetupWizard", "Background update check: " + prefs.getBoolean("pref_background_update_check", false));
865865
Log.i("SetupWizard", "Crash reporting: " + prefs.getBoolean("pref_crash_reporting", false));
866866
Log.i("SetupWizard", "Magisk Alt Repo: " + prefs.getBoolean("pref_magisk_alt_repo_enabled", false));
867867
Log.i("SetupWizard", "Androidacy Repo: " + prefs.getBoolean("pref_androidacy_repo_enabled", false));

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import android.content.ComponentName;
55
import android.content.Intent;
66
import android.content.SharedPreferences;
7+
import android.content.pm.PackageManager;
8+
import android.content.pm.Signature;
79
import android.content.res.Configuration;
810
import android.content.res.Resources;
911
import android.graphics.Color;
@@ -29,6 +31,7 @@
2931
import com.fox2code.mmm.utils.Http;
3032
import com.fox2code.mmm.utils.SentryMain;
3133
import com.fox2code.rosettax.LanguageSwitcher;
34+
import com.google.common.hash.Hashing;
3235
import com.topjohnwu.superuser.Shell;
3336

3437
import java.io.File;
@@ -57,6 +60,7 @@ public class MainApplication extends FoxApplication implements androidx.work.Con
5760
@SuppressLint("RestrictedApi")
5861
// Use FoxProcess wrapper helper.
5962
private static final boolean wrapped = !FoxProcessExt.isRootLoader();
63+
public static boolean isOfficial = false;
6064
private static Locale timeFormatLocale = Resources.getSystem().getConfiguration().locale;
6165
private static SimpleDateFormat timeFormat = new SimpleDateFormat(timeFormatString, timeFormatLocale);
6266
private static SharedPreferences bootSharedPreferences;
@@ -283,6 +287,19 @@ public void onCreate() {
283287
if (BuildConfig.DEBUG) {
284288
Log.d("MainApplication", "Starting FoxMMM version " + BuildConfig.VERSION_NAME + " (" + BuildConfig.VERSION_CODE + "), commit " + BuildConfig.COMMIT_HASH);
285289
}
290+
// Determine if this is an official build based on the signature
291+
try {
292+
// Get the signature of the key used to sign the app
293+
@SuppressLint("PackageManagerGetSignatures") Signature[] signatures = this.getPackageManager().getPackageInfo(this.getPackageName(), PackageManager.GET_SIGNATURES).signatures;
294+
String officialSignatureHash = "7bec7c4462f4aac616612d9f56a023ee3046e83afa956463b5fab547fd0a0be6";
295+
String ourSignatureHash = Hashing.sha256().hashBytes(signatures[0].toByteArray()).toString();
296+
isOfficial = ourSignatureHash.equals(officialSignatureHash);
297+
} catch (
298+
PackageManager.NameNotFoundException ignored) {
299+
}
300+
if (!isOfficial) {
301+
Log.w("MainApplication", "This is not an official build of FoxMMM. This warning can be safely ignored if this is expected, otherwise you may be running an untrusted build.");
302+
}
286303
SharedPreferences sharedPreferences = MainApplication.getSharedPreferences();
287304
// We are only one process so it's ok to do this
288305
SharedPreferences bootPrefs = MainApplication.bootSharedPreferences = this.getSharedPreferences("mmm_boot", MODE_PRIVATE);

0 commit comments

Comments
 (0)