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

Commit 66cb0b1

Browse files
Security fixes and optimizations
- All known hosts now have a hardcoded trusted root ca, because we're not just going to trust that the user has a-ok certs installed - Remove some unused code - Properly fix a couple NetworkOnMainThread errors Signed-off-by: androidacy-user <opensource@androidacy.com>
1 parent 86c46de commit 66cb0b1

File tree

14 files changed

+135
-249
lines changed

14 files changed

+135
-249
lines changed

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
import com.fox2code.mmm.utils.ExternalHelper;
5151
import com.fox2code.mmm.utils.Http;
5252
import com.fox2code.mmm.utils.IntentHelper;
53-
import com.fox2code.mmm.utils.NoodleDebug;
5453
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
5554
import com.google.android.material.progressindicator.LinearProgressIndicator;
5655

@@ -71,7 +70,6 @@
7170
public class MainActivity extends FoxActivity implements SwipeRefreshLayout.OnRefreshListener, SearchView.OnQueryTextListener, SearchView.OnCloseListener, OverScrollManager.OverScrollHelper {
7271
private static final String TAG = "MainActivity";
7372
private static final int PRECISION = 10000;
74-
public static boolean noodleDebugState = BuildConfig.DEBUG;
7573
public final ModuleViewListBuilder moduleViewListBuilder;
7674
public LinearProgressIndicator progressIndicator;
7775
private ModuleViewAdapter moduleViewAdapter;
@@ -87,7 +85,6 @@ public class MainActivity extends FoxActivity implements SwipeRefreshLayout.OnRe
8785
private RecyclerView moduleList;
8886
private CardView searchCard;
8987
private SearchView searchView;
90-
private NoodleDebug noodleDebug;
9188
private boolean initMode;
9289

9390
public MainActivity() {
@@ -104,7 +101,8 @@ protected void onResume() {
104101
@Override
105102
protected void onCreate(Bundle savedInstanceState) {
106103
this.initMode = true;
107-
noodleDebugState = MainApplication.isDeveloper();
104+
// Ensure HTTP Cache directories are created
105+
Http.ensureCacheDirs(this);
108106
BackgroundUpdateChecker.onMainActivityCreate(this);
109107
super.onCreate(savedInstanceState);
110108
this.setActionBarExtraMenuButton(R.drawable.ic_baseline_settings_24, v -> {
@@ -132,7 +130,6 @@ protected void onCreate(Bundle savedInstanceState) {
132130
this.moduleList = findViewById(R.id.module_list);
133131
this.searchCard = findViewById(R.id.search_card);
134132
this.searchView = findViewById(R.id.search_bar);
135-
this.noodleDebug = new NoodleDebug(this, R.id.noodle_debug);
136133
this.moduleViewAdapter = new ModuleViewAdapter();
137134
this.moduleList.setAdapter(this.moduleViewAdapter);
138135
this.moduleList.setLayoutManager(new LinearLayoutManager(this));
@@ -290,6 +287,7 @@ public void commonNext() {
290287
dialog.cancel();
291288
}
292289
preferences.edit().remove("lastEventId").apply();
290+
preferences.edit().putString("lastExitReason", "").apply();
293291
// Prevent strict mode violation
294292
new Thread(() -> {
295293
try {
@@ -439,8 +437,6 @@ public void onPathReceived(String path) {
439437
@Override
440438
public void onFailure(int error) {
441439
moduleViewListBuilder.addNotification(InstallerInitializer.getErrorNotification());
442-
noodleDebug.setEnabled(noodleDebugState);
443-
noodleDebug.bind();
444440
this.commonNext();
445441
}
446442

app/src/main/java/com/fox2code/mmm/androidacy/AndroidacyActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
258258
break;
259259
}
260260
}
261-
return super.onConsoleMessage(consoleMessage);
261+
return true;
262262
}
263263

264264
@Override

app/src/main/java/com/fox2code/mmm/background/BackgroundBootListener.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ public class BackgroundBootListener extends BroadcastReceiver {
1313
public void onReceive(Context context, Intent intent) {
1414
if (!BOOT_COMPLETED.equals(intent.getAction())) return;
1515
synchronized (BackgroundUpdateChecker.lock) {
16-
BackgroundUpdateChecker.onMainActivityCreate(context);
17-
if (MainApplication.isBackgroundUpdateCheckEnabled()) {
18-
BackgroundUpdateChecker.doCheck(context);
19-
}
16+
new Thread(() -> {
17+
BackgroundUpdateChecker.onMainActivityCreate(context);
18+
if (MainApplication.isBackgroundUpdateCheckEnabled()) {
19+
BackgroundUpdateChecker.doCheck(context);
20+
}
21+
}).start();
2022
}
2123
}
2224
}

app/src/main/java/com/fox2code/mmm/background/BackgroundUpdateChecker.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import android.content.Context;
55
import android.content.Intent;
66
import android.os.Build;
7-
import android.os.StrictMode;
87

98
import androidx.annotation.NonNull;
109
import androidx.core.app.NotificationChannelCompat;
@@ -54,10 +53,7 @@ public Result doWork() {
5453
}
5554

5655
static void doCheck(Context context) {
57-
// This is actually not recommended but it's the only way to do it
58-
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
59-
StrictMode.setThreadPolicy(policy);
60-
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
56+
Thread.currentThread().setPriority(2);
6157
ModuleManager.getINSTANCE().scanAsync();
6258
RepoManager.getINSTANCE().update(null);
6359
ModuleManager.getINSTANCE().runAfterScan(() -> {

app/src/main/java/com/fox2code/mmm/manager/ModuleManager.java

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import com.fox2code.mmm.BuildConfig;
99
import com.fox2code.mmm.MainApplication;
1010
import com.fox2code.mmm.installer.InstallerInitializer;
11-
import com.fox2code.mmm.utils.Http;
12-
import com.fox2code.mmm.utils.NoodleDebug;
1311
import com.fox2code.mmm.utils.PropUtils;
1412
import com.fox2code.mmm.utils.SyncManager;
1513
import com.topjohnwu.superuser.Shell;
@@ -24,33 +22,34 @@
2422
import java.util.Iterator;
2523

2624
public final class ModuleManager extends SyncManager {
27-
private static final String TAG = "ModuleManager";
28-
2925
// New method is not really effective, this flag force app to use old method
3026
public static final boolean FORCE_NEED_FALLBACK = true;
27+
private static final String TAG = "ModuleManager";
3128
private static final int FLAG_MM_INVALID = ModuleInfo.FLAG_METADATA_INVALID;
3229
private static final int FLAG_MM_UNPROCESSED = ModuleInfo.FLAG_CUSTOM_INTERNAL;
3330
private static final int FLAGS_KEEP_INIT = FLAG_MM_UNPROCESSED |
3431
ModuleInfo.FLAGS_MODULE_ACTIVE | ModuleInfo.FLAG_MODULE_UPDATING_ONLY;
3532
private static final int FLAGS_RESET_UPDATE = FLAG_MM_INVALID | FLAG_MM_UNPROCESSED;
33+
private static final ModuleManager INSTANCE = new ModuleManager();
3634
private final HashMap<String, LocalModuleInfo> moduleInfos;
3735
private final SharedPreferences bootPrefs;
3836
private int updatableModuleCount = 0;
3937

40-
private static final ModuleManager INSTANCE = new ModuleManager();
38+
private ModuleManager() {
39+
this.moduleInfos = new HashMap<>();
40+
this.bootPrefs = MainApplication.getBootSharedPreferences();
41+
}
4142

4243
public static ModuleManager getINSTANCE() {
4344
return INSTANCE;
4445
}
4546

46-
private ModuleManager() {
47-
this.moduleInfos = new HashMap<>();
48-
this.bootPrefs = MainApplication.getBootSharedPreferences();
47+
public static boolean isModuleActive(String moduleId) {
48+
ModuleInfo moduleInfo = ModuleManager.getINSTANCE().getModules().get(moduleId);
49+
return moduleInfo != null && (moduleInfo.flags & ModuleInfo.FLAGS_MODULE_ACTIVE) != 0;
4950
}
5051

5152
protected void scanInternal(@NonNull UpdateListener updateListener) {
52-
NoodleDebug noodleDebug = NoodleDebug.getNoodleDebug();
53-
noodleDebug.push("Initialize scan");
5453
boolean firstScan = this.bootPrefs.getBoolean("mm_first_scan", true);
5554
SharedPreferences.Editor editor = firstScan ? this.bootPrefs.edit() : null;
5655
for (ModuleInfo v : this.moduleInfos.values()) {
@@ -73,7 +72,6 @@ protected void scanInternal(@NonNull UpdateListener updateListener) {
7372
}
7473
if (BuildConfig.DEBUG) Log.d("NoodleDebug", "Scan");
7574
if (modules != null) {
76-
noodleDebug.push("");
7775
for (String module : modules) {
7876
if (!new SuFile("/data/adb/modules/" + module).isDirectory())
7977
continue; // Ignore non directory files inside modules folder
@@ -123,7 +121,6 @@ protected void scanInternal(@NonNull UpdateListener updateListener) {
123121
if (BuildConfig.DEBUG) Log.d("NoodleDebug", "Scan update");
124122
String[] modules_update = new SuFile("/data/adb/modules_update").list();
125123
if (modules_update != null) {
126-
noodleDebug.push("");
127124
for (String module : modules_update) {
128125
if (!new SuFile("/data/adb/modules_update/" + module).isDirectory())
129126
continue; // Ignore non directory files inside modules folder
@@ -148,7 +145,6 @@ protected void scanInternal(@NonNull UpdateListener updateListener) {
148145
this.updatableModuleCount = 0;
149146
Iterator<LocalModuleInfo> moduleInfoIterator =
150147
this.moduleInfos.values().iterator();
151-
noodleDebug.push("");
152148
while (moduleInfoIterator.hasNext()) {
153149
LocalModuleInfo moduleInfo = moduleInfoIterator.next();
154150
if (BuildConfig.DEBUG) Log.d("NoodleDebug", moduleInfo.id);
@@ -232,7 +228,7 @@ public boolean masterClear(ModuleInfo moduleInfo) {
232228
try { // Check for module that declare having file outside their own folder.
233229
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
234230
SuFileInputStream.open("/data/adb/modules/." + moduleInfo.id + "-files"),
235-
StandardCharsets.UTF_8))) {
231+
StandardCharsets.UTF_8))) {
236232
String line;
237233
while ((line = bufferedReader.readLine()) != null) {
238234
line = line.trim().replace(' ', '.');
@@ -245,16 +241,12 @@ public boolean masterClear(ModuleInfo moduleInfo) {
245241
Shell.cmd("rm -rf \"" + line + "\"").exec();
246242
}
247243
}
248-
} catch (IOException ignored) {}
244+
} catch (IOException ignored) {
245+
}
249246
Shell.cmd("rm -rf /data/adb/modules/" + escapedId + "/").exec();
250247
Shell.cmd("rm -f /data/adb/modules/." + escapedId + "-files").exec();
251248
Shell.cmd("rm -rf /data/adb/modules_update/" + escapedId + "/").exec();
252249
moduleInfo.flags = ModuleInfo.FLAG_METADATA_INVALID;
253250
return true;
254251
}
255-
256-
public static boolean isModuleActive(String moduleId) {
257-
ModuleInfo moduleInfo = ModuleManager.getINSTANCE().getModules().get(moduleId);
258-
return moduleInfo != null && (moduleInfo.flags & ModuleInfo.FLAGS_MODULE_ACTIVE) != 0;
259-
}
260252
}

app/src/main/java/com/fox2code/mmm/settings/SettingsActivity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ public void onCreatePreferencesAndroidacy() {
537537
// Use MaterialAlertDialogBuilder
538538
new MaterialAlertDialogBuilder(this.requireContext())
539539
.setTitle(R.string.warning)
540+
.setCancelable(false)
540541
.setMessage(R.string.androidacy_test_mode_warning)
541542
.setPositiveButton(android.R.string.ok, (dialog, which) -> {
542543
// User clicked OK button
@@ -575,6 +576,7 @@ public void onCreatePreferencesAndroidacy() {
575576
// Show dialog to restart app with ok button
576577
new MaterialAlertDialogBuilder(this.requireContext())
577578
.setTitle(R.string.warning)
579+
.setCancelable(false)
578580
.setMessage(R.string.androidacy_test_mode_disable_warning)
579581
.setNeutralButton(android.R.string.ok, (dialog, which) -> {
580582
// User clicked OK button
@@ -609,6 +611,7 @@ public void onCreatePreferencesAndroidacy() {
609611
androidacyRepoEnabled.setOnPreferenceClickListener(preference -> {
610612
new MaterialAlertDialogBuilder(this.requireContext())
611613
.setTitle(R.string.androidacy_repo_disabled)
614+
.setCancelable(false)
612615
.setMessage(R.string.androidacy_repo_disabled_message)
613616
.setPositiveButton(R.string.download_full_app, (dialog, which) -> {
614617
// User clicked OK button. Open GitHub releases page
@@ -664,6 +667,7 @@ public void onCreatePreferencesAndroidacy() {
664667
// Show dialog to restart app with ok button
665668
new MaterialAlertDialogBuilder(this.requireContext())
666669
.setTitle(R.string.restart)
670+
.setCancelable(false)
667671
.setMessage(R.string.api_key_restart)
668672
.setNeutralButton(android.R.string.ok, (dialog, which) -> {
669673
// User clicked OK button
@@ -721,6 +725,7 @@ public void onCreatePreferencesAndroidacy() {
721725
// Show dialog to restart app with ok button
722726
new MaterialAlertDialogBuilder(this.requireContext())
723727
.setTitle(R.string.restart)
728+
.setCancelable(false)
724729
.setMessage(R.string.api_key_restart)
725730
.setNeutralButton(android.R.string.ok, (dialog, which) -> {
726731
// User clicked OK button

app/src/main/java/com/fox2code/mmm/utils/Http.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import androidx.annotation.Nullable;
1616

1717
import com.fox2code.mmm.BuildConfig;
18+
import com.fox2code.mmm.MainActivity;
1819
import com.fox2code.mmm.MainApplication;
1920
import com.fox2code.mmm.androidacy.AndroidacyUtil;
2021
import com.fox2code.mmm.installer.InstallerInitializer;
@@ -341,6 +342,29 @@ public static boolean hasWebView() {
341342
return hasWebView;
342343
}
343344

345+
public static void ensureCacheDirs(MainActivity mainActivity) {
346+
File cacheDir = mainActivity.getCacheDir();
347+
File cacheDir2 = new File(cacheDir, "HTTP Cache");
348+
if (!cacheDir2.exists()) {
349+
if (!cacheDir2.mkdirs()) {
350+
Log.e(TAG, "Failed to create cache dir");
351+
}
352+
}
353+
// Ensure js and wasm cache dirs
354+
File jsCacheDir = new File(cacheDir2, "js");
355+
if (!jsCacheDir.exists()) {
356+
if (!jsCacheDir.mkdirs()) {
357+
Log.e(TAG, "Failed to create js cache dir");
358+
}
359+
}
360+
File wasmCacheDir = new File(cacheDir2, "wasm");
361+
if (!wasmCacheDir.exists()) {
362+
if (!wasmCacheDir.mkdirs()) {
363+
Log.e(TAG, "Failed to create wasm cache dir");
364+
}
365+
}
366+
}
367+
344368
public interface ProgressListener {
345369
void onUpdate(int downloaded, int total, boolean done);
346370
}

0 commit comments

Comments
 (0)