From 231cc99cb6316831147dc0eaacf677b214634ae6 Mon Sep 17 00:00:00 2001 From: androidacy-user Date: Tue, 25 Apr 2023 17:08:47 -0400 Subject: [PATCH] (chore) code cleanup Signed-off-by: androidacy-user --- app/src/main/java/com/fox2code/mmm/MainActivity.java | 1 + .../main/java/com/fox2code/mmm/SetupActivity.java | 1 + .../fox2code/mmm/androidacy/AndroidacyActivity.java | 12 ++++++++++++ .../java/com/fox2code/mmm/manager/ModuleManager.java | 2 ++ .../com/fox2code/mmm/repo/CustomRepoManager.java | 1 + .../main/java/com/fox2code/mmm/repo/RepoData.java | 5 +++++ .../main/java/com/fox2code/mmm/repo/RepoUpdater.java | 1 + .../com/fox2code/mmm/settings/SettingsActivity.java | 1 + .../java/com/fox2code/mmm/utils/io/net/Http.java | 6 +----- 9 files changed, 25 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/fox2code/mmm/MainActivity.java b/app/src/main/java/com/fox2code/mmm/MainActivity.java index f6f0938..429cff5 100644 --- a/app/src/main/java/com/fox2code/mmm/MainActivity.java +++ b/app/src/main/java/com/fox2code/mmm/MainActivity.java @@ -119,6 +119,7 @@ public class MainActivity extends FoxActivity implements SwipeRefreshLayout.OnRe enabledRepos.setLength(enabledRepos.length() - 1); } TrackHelper.track().event("enabled_repos", enabledRepos.toString()).with(MainApplication.getINSTANCE().getTracker()); + realm.close(); // log all shared preferences that are present if (!Iof) { Timber.w("You may be running an untrusted build."); diff --git a/app/src/main/java/com/fox2code/mmm/SetupActivity.java b/app/src/main/java/com/fox2code/mmm/SetupActivity.java index 00b1e78..3ea66bb 100644 --- a/app/src/main/java/com/fox2code/mmm/SetupActivity.java +++ b/app/src/main/java/com/fox2code/mmm/SetupActivity.java @@ -352,6 +352,7 @@ public class SetupActivity extends FoxActivity implements LanguageActivity { Timber.d("Created magisk_alt_repo"); } realm.commitTransaction(); + realm.close(); realmDatabasesCreated = true; Timber.d("Realm transaction finished"); long endTime = System.currentTimeMillis(); diff --git a/app/src/main/java/com/fox2code/mmm/androidacy/AndroidacyActivity.java b/app/src/main/java/com/fox2code/mmm/androidacy/AndroidacyActivity.java index 5807695..9fb8164 100644 --- a/app/src/main/java/com/fox2code/mmm/androidacy/AndroidacyActivity.java +++ b/app/src/main/java/com/fox2code/mmm/androidacy/AndroidacyActivity.java @@ -434,4 +434,16 @@ public final class AndroidacyActivity extends FoxActivity { this.downloadMode = false; return FileProvider.getUriForFile(this, this.getPackageName() + ".file-provider", this.moduleFile); } + + @Override + protected void onDestroy() { + super.onDestroy(); + if (webView != null) { + SwipeRefreshLayout parent = (SwipeRefreshLayout) webView.getParent(); + parent.removeView(webView); + webView.removeAllViews(); + webView.destroy(); // fix memory leak + } + Timber.i("onDestroy for %s", this); + } } diff --git a/app/src/main/java/com/fox2code/mmm/manager/ModuleManager.java b/app/src/main/java/com/fox2code/mmm/manager/ModuleManager.java index 1f9071e..61f8dc9 100644 --- a/app/src/main/java/com/fox2code/mmm/manager/ModuleManager.java +++ b/app/src/main/java/com/fox2code/mmm/manager/ModuleManager.java @@ -117,6 +117,8 @@ public final class ModuleManager extends SyncManager { moduleInfos.put(module, moduleInfo); realm.close(); break; + } else { + realm.close(); } } } diff --git a/app/src/main/java/com/fox2code/mmm/repo/CustomRepoManager.java b/app/src/main/java/com/fox2code/mmm/repo/CustomRepoManager.java index 1c219ca..f1d9e60 100644 --- a/app/src/main/java/com/fox2code/mmm/repo/CustomRepoManager.java +++ b/app/src/main/java/com/fox2code/mmm/repo/CustomRepoManager.java @@ -151,6 +151,7 @@ public class CustomRepoManager { // Set the enabled state to true customRepoData.setEnabled(true); customRepoData.updateEnabledState(); + realm.close(); return customRepoData; } diff --git a/app/src/main/java/com/fox2code/mmm/repo/RepoData.java b/app/src/main/java/com/fox2code/mmm/repo/RepoData.java index 3441d8d..e837a86 100644 --- a/app/src/main/java/com/fox2code/mmm/repo/RepoData.java +++ b/app/src/main/java/com/fox2code/mmm/repo/RepoData.java @@ -344,6 +344,7 @@ public class RepoData extends XRepo { } catch (Exception e) { Timber.e(e, "Error while updating enabled state for repo %s", this.id); } + realm2.close(); this.enabled = (!this.forceHide) && dbEnabled; } @@ -408,12 +409,16 @@ public class RepoData extends XRepo { long diff = currentTime - lastUpdate; long diffMinutes = diff / (60 * 1000) % 60; Timber.d("Repo " + this.id + " updated: " + diffMinutes + " minutes ago"); + realm.close(); return diffMinutes > (BuildConfig.DEBUG ? 15 : 20); } else { Timber.d("Repo " + this.id + " should update could not find repo in database"); Timber.d("This is probably an error, please report this to the developer"); + realm.close(); return true; } + } else { + realm.close(); } return true; } diff --git a/app/src/main/java/com/fox2code/mmm/repo/RepoUpdater.java b/app/src/main/java/com/fox2code/mmm/repo/RepoUpdater.java index 3f50436..d39be64 100644 --- a/app/src/main/java/com/fox2code/mmm/repo/RepoUpdater.java +++ b/app/src/main/java/com/fox2code/mmm/repo/RepoUpdater.java @@ -356,6 +356,7 @@ public class RepoUpdater { Timber.w("Failed to update lastUpdate for repo %s", this.repoData.id); } }); + realm2.close(); } else { success.set(true); // assume we're reading from cache. this may be unsafe but it's better than nothing } diff --git a/app/src/main/java/com/fox2code/mmm/settings/SettingsActivity.java b/app/src/main/java/com/fox2code/mmm/settings/SettingsActivity.java index e17095e..0121417 100644 --- a/app/src/main/java/com/fox2code/mmm/settings/SettingsActivity.java +++ b/app/src/main/java/com/fox2code/mmm/settings/SettingsActivity.java @@ -1416,6 +1416,7 @@ public class SettingsActivity extends FoxActivity implements LanguageActivity { findPreference(preferenceName + "_donate").setVisible(false); } } + realm.close(); } else { Timber.d("Hiding preference " + preferenceName + " because it's data is null"); hideRepoData(preferenceName); diff --git a/app/src/main/java/com/fox2code/mmm/utils/io/net/Http.java b/app/src/main/java/com/fox2code/mmm/utils/io/net/Http.java index 5cf2405..6d7343a 100644 --- a/app/src/main/java/com/fox2code/mmm/utils/io/net/Http.java +++ b/app/src/main/java/com/fox2code/mmm/utils/io/net/Http.java @@ -331,8 +331,6 @@ public enum Http { @SuppressWarnings("resource") private static Object doHttpPostRaw(String url, String data, boolean allowCache) throws IOException { - if (BuildConfig.DEBUG) - Timber.i("POST " + url + " " + data); Response response; response = (allowCache ? getHttpClientWithCache() : getHttpClient()).newCall(new Request.Builder().url(url).post(JsonRequestBody.from(data)).header("Content-Type", "application/json").build()).execute(); if (response.isRedirect()) { @@ -340,7 +338,7 @@ public enum Http { } // 200/204 == success, 304 == cache valid if (response.code() != 200 && response.code() != 204 && (response.code() != 304 || !allowCache)) { - if (BuildConfig.DEBUG) + if (BuildConfig.DEBUG_HTTP) Timber.e("Failed to fetch " + url + ", code: " + response.code() + ", body: " + response.body().string()); checkNeedCaptchaAndroidacy(url, response.code()); throw new HttpException(response.code()); @@ -356,8 +354,6 @@ public enum Http { } public static byte[] doHttpGet(String url, ProgressListener progressListener) throws IOException { - if (BuildConfig.DEBUG) - Timber.i("GET %s", url.split("\\?")[0]); Response response = getHttpClient().newCall(new Request.Builder().url(url).get().build()).execute(); if (response.code() != 200 && response.code() != 204) { Timber.e("Failed to fetch " + url + ", code: " + response.code());