fix cache issues + increase performance

cache is feature complete now, custom repos adding is still having leaks

Signed-off-by: androidacy-user <opensource@androidacy.com>
pull/27/head
androidacy-user 3 years ago
parent 1f38a197de
commit 6d0dec6ead

@ -352,6 +352,9 @@ public final class AndroidacyRepoData extends RepoData {
repoModule.notesUrl = this.injectToken(repoModule.notesUrl); repoModule.notesUrl = this.injectToken(repoModule.notesUrl);
repoModule.qualityText = R.string.module_downloads; repoModule.qualityText = R.string.module_downloads;
repoModule.qualityValue = jsonObject.optInt("downloads", 0); repoModule.qualityValue = jsonObject.optInt("downloads", 0);
if (repoModule.qualityValue == 0) {
repoModule.qualityValue = jsonObject.optInt("stats", 0);
}
String checksum = jsonObject.optString("checksum", ""); String checksum = jsonObject.optString("checksum", "");
repoModule.checksum = checksum.isEmpty() ? null : checksum; repoModule.checksum = checksum.isEmpty() ? null : checksum;
ModuleInfo moduleInfo = repoModule.moduleInfo; ModuleInfo moduleInfo = repoModule.moduleInfo;

@ -165,12 +165,6 @@ public class InstallerActivity extends FoxActivity {
throw new SecurityException("Module cache is not in cache dir!"); throw new SecurityException("Module cache is not in cache dir!");
File moduleCache = this.toDelete = urlMode ? File moduleCache = this.toDelete = urlMode ?
new File(this.moduleCache, "module.zip") : new File(finalTarget); new File(this.moduleCache, "module.zip") : new File(finalTarget);
try {
if (!moduleCache.getCanonicalPath().startsWith(MainApplication.getINSTANCE().getCacheDir().getAbsolutePath()))
throw new SecurityException("Module cache is not in cache dir!");
} catch (
IOException ignored) {
}
if (urlMode && moduleCache.exists() && !moduleCache.delete() && if (urlMode && moduleCache.exists() && !moduleCache.delete() &&
!new SuFile(moduleCache.getAbsolutePath()).delete()) !new SuFile(moduleCache.getAbsolutePath()).delete())
Timber.e("Failed to delete module cache"); Timber.e("Failed to delete module cache");

@ -40,13 +40,12 @@ public class RepoData extends XRepo {
public final String id; public final String id;
public final File cacheRoot; public final File cacheRoot;
public final SharedPreferences cachedPreferences; public final SharedPreferences cachedPreferences;
public JSONObject metaDataCache;
public final HashMap<String, RepoModule> moduleHashMap; public final HashMap<String, RepoModule> moduleHashMap;
public final JSONObject supportedProperties = new JSONObject();
private final Object populateLock = new Object(); private final Object populateLock = new Object();
public JSONObject metaDataCache;
public long lastUpdate; public long lastUpdate;
public String name, website, support, donate, submitModule; public String name, website, support, donate, submitModule;
public final JSONObject supportedProperties = new JSONObject();
protected String defaultName, defaultWebsite, defaultSupport, defaultDonate, defaultSubmitModule; protected String defaultName, defaultWebsite, defaultSupport, defaultDonate, defaultSubmitModule;
// array with module info default values // array with module info default values
@ -189,6 +188,10 @@ public class RepoData extends XRepo {
String moduleChecksum = module.optString("checksum"); String moduleChecksum = module.optString("checksum");
String moduleStars = module.optString("stars"); String moduleStars = module.optString("stars");
String moduleDownloads = module.optString("downloads"); String moduleDownloads = module.optString("downloads");
// if downloads is mull or empty, try to get it from the stats field
if (moduleDownloads.isEmpty() && module.has("stats")) {
moduleDownloads = module.optString("stats");
}
RepoModule repoModule = this.moduleHashMap.get(moduleId); RepoModule repoModule = this.moduleHashMap.get(moduleId);
if (repoModule == null) { if (repoModule == null) {
repoModule = new RepoModule(this, moduleId); repoModule = new RepoModule(this, moduleId);
@ -212,15 +215,13 @@ public class RepoData extends XRepo {
try { try {
repoModule.qualityValue = Integer.parseInt(moduleStars); repoModule.qualityValue = Integer.parseInt(moduleStars);
repoModule.qualityText = R.string.module_stars; repoModule.qualityText = R.string.module_stars;
} catch ( } catch (NumberFormatException ignored) {
NumberFormatException ignored) {
} }
} else if (!moduleDownloads.isEmpty()) { } else if (!moduleDownloads.isEmpty()) {
try { try {
repoModule.qualityValue = Integer.parseInt(moduleDownloads); repoModule.qualityValue = Integer.parseInt(moduleDownloads);
repoModule.qualityText = R.string.module_downloads; repoModule.qualityText = R.string.module_downloads;
} catch ( } catch (NumberFormatException ignored) {
NumberFormatException ignored) {
} }
} }
} }
@ -269,8 +270,7 @@ public class RepoData extends XRepo {
moduleInfo.version = "v" + moduleInfo.versionCode; moduleInfo.version = "v" + moduleInfo.versionCode;
} }
return true; return true;
} catch ( } catch (Exception ignored) {
Exception ignored) {
boolean delete = file.delete(); boolean delete = file.delete();
if (!delete) { if (!delete) {
throw new RuntimeException("Failed to delete invalid metadata file"); throw new RuntimeException("Failed to delete invalid metadata file");
@ -312,7 +312,15 @@ public class RepoData extends XRepo {
// reposlist realm // reposlist realm
RealmConfiguration realmConfiguration2 = new RealmConfiguration.Builder().name("ReposList.realm").allowQueriesOnUiThread(true).allowWritesOnUiThread(true).directory(MainApplication.getINSTANCE().getDataDirWithPath("realms")).schemaVersion(1).build(); RealmConfiguration realmConfiguration2 = new RealmConfiguration.Builder().name("ReposList.realm").allowQueriesOnUiThread(true).allowWritesOnUiThread(true).directory(MainApplication.getINSTANCE().getDataDirWithPath("realms")).schemaVersion(1).build();
Realm realm2 = Realm.getInstance(realmConfiguration2); Realm realm2 = Realm.getInstance(realmConfiguration2);
this.enabled = (!this.forceHide) && Objects.requireNonNull(realm2.where(ReposList.class).equalTo("id", this.id).findFirst()).isEnabled(); boolean dbEnabled;
try {
dbEnabled = Objects.requireNonNull(realm2.where(ReposList.class).equalTo("id", this.id).findFirst()).isEnabled();
} catch (NullPointerException e) {
Timber.e(e, "Error while updating enabled state");
// for now, throw an exception
throw e;
}
this.enabled = (!this.forceHide) && dbEnabled;
} }
public String getUrl() throws NoSuchAlgorithmException { public String getUrl() throws NoSuchAlgorithmException {
@ -327,37 +335,30 @@ public class RepoData extends XRepo {
@NonNull @NonNull
@Override @Override
public String getName() { public String getName() {
if (isNonNull(this.name)) if (isNonNull(this.name)) return this.name;
return this.name; if (this.defaultName != null) return this.defaultName;
if (this.defaultName != null)
return this.defaultName;
return this.url; return this.url;
} }
@NonNull @NonNull
public String getWebsite() { public String getWebsite() {
if (isNonNull(this.website)) if (isNonNull(this.website)) return this.website;
return this.website; if (this.defaultWebsite != null) return this.defaultWebsite;
if (this.defaultWebsite != null)
return this.defaultWebsite;
return this.url; return this.url;
} }
public String getSupport() { public String getSupport() {
if (isNonNull(this.support)) if (isNonNull(this.support)) return this.support;
return this.support;
return this.defaultSupport; return this.defaultSupport;
} }
public String getDonate() { public String getDonate() {
if (isNonNull(this.donate)) if (isNonNull(this.donate)) return this.donate;
return this.donate;
return this.defaultDonate; return this.defaultDonate;
} }
public String getSubmitModule() { public String getSubmitModule() {
if (isNonNull(this.submitModule)) if (isNonNull(this.submitModule)) return this.submitModule;
return this.submitModule;
return this.defaultSubmitModule; return this.defaultSubmitModule;
} }

@ -128,27 +128,20 @@ public final class RepoManager extends SyncManager {
} }
public static String internalIdOfUrl(String url) { public static String internalIdOfUrl(String url) {
switch (url) { return switch (url) {
case MAGISK_ALT_REPO: case MAGISK_ALT_REPO, MAGISK_ALT_REPO_JSDELIVR -> "magisk_alt_repo";
case MAGISK_ALT_REPO_JSDELIVR: case ANDROIDACY_MAGISK_REPO_ENDPOINT, ANDROIDACY_TEST_MAGISK_REPO_ENDPOINT ->
return "magisk_alt_repo"; "androidacy_repo";
case ANDROIDACY_MAGISK_REPO_ENDPOINT: default -> "repo_" + Hashes.hashSha256(url.getBytes(StandardCharsets.UTF_8));
case ANDROIDACY_TEST_MAGISK_REPO_ENDPOINT: };
return "androidacy_repo";
default:
return "repo_" + Hashes.hashSha256(url.getBytes(StandardCharsets.UTF_8));
}
} }
static boolean isBuiltInRepo(String repo) { static boolean isBuiltInRepo(String repo) {
switch (repo) { return switch (repo) {
case RepoManager.ANDROIDACY_MAGISK_REPO_ENDPOINT: case RepoManager.ANDROIDACY_MAGISK_REPO_ENDPOINT, RepoManager.ANDROIDACY_TEST_MAGISK_REPO_ENDPOINT, RepoManager.MAGISK_ALT_REPO, RepoManager.MAGISK_ALT_REPO_JSDELIVR ->
case RepoManager.ANDROIDACY_TEST_MAGISK_REPO_ENDPOINT: true;
case RepoManager.MAGISK_ALT_REPO: default -> false;
case RepoManager.MAGISK_ALT_REPO_JSDELIVR: };
return true;
}
return false;
} }
/** /**
@ -408,10 +401,7 @@ public final class RepoManager extends SyncManager {
} }
} }
switch (url) { switch (url) {
case MAGISK_REPO: case MAGISK_REPO, MAGISK_REPO_MANAGER -> repoData.defaultWebsite = MAGISK_REPO_HOMEPAGE;
case MAGISK_REPO_MANAGER: {
repoData.defaultWebsite = MAGISK_REPO_HOMEPAGE;
}
} }
this.repoData.put(url, repoData); this.repoData.put(url, repoData);
if (this.initialized) { if (this.initialized) {

@ -262,6 +262,15 @@ public class RepoUpdater {
} else { } else {
lastUpdate = 0; lastUpdate = 0;
} }
// now downloads or stars
int downloads;
if (module.has("downloads")) {
downloads = module.getInt("downloads");
} else if (module.has("stars")) {
downloads = module.getInt("stars");
} else {
downloads = 0;
}
// get module repo id // get module repo id
String repoId = this.repoData.id; String repoId = this.repoData.id;
// get module installed // get module installed
@ -308,6 +317,7 @@ public class RepoUpdater {
moduleListCache.setInstalledVersionCode(installedVersionCode); moduleListCache.setInstalledVersionCode(installedVersionCode);
moduleListCache.setSafe(safe); moduleListCache.setSafe(safe);
moduleListCache.setLastUpdate(lastUpdate); moduleListCache.setLastUpdate(lastUpdate);
moduleListCache.setStats(downloads);
realm.copyToRealmOrUpdate(moduleListCache); realm.copyToRealmOrUpdate(moduleListCache);
realm.commitTransaction(); realm.commitTransaction();
} catch ( } catch (

@ -37,8 +37,9 @@ public class ModuleListCache extends RealmObject {
private int lastUpdate; private int lastUpdate;
// androidacy specific, may be added by other repos // androidacy specific, may be added by other repos
private boolean safe; private boolean safe;
private int stats;
public ModuleListCache(String codename, String name, String version, int versionCode, String author, String description, int minApi, int maxApi, int minMagisk, boolean needRamdisk, String support, String donate, String config, boolean changeBoot, boolean mmtReborn, String repoId, boolean installed, int installedVersionCode, int lastUpdate) { public ModuleListCache(String codename, String name, String version, int versionCode, String author, String description, int minApi, int maxApi, int minMagisk, boolean needRamdisk, String support, String donate, String config, boolean changeBoot, boolean mmtReborn, String repoId, boolean installed, int installedVersionCode, int lastUpdate, int stats) {
this.codename = codename; this.codename = codename;
this.name = name; this.name = name;
this.version = version; this.version = version;
@ -59,6 +60,7 @@ public class ModuleListCache extends RealmObject {
this.installedVersionCode = installedVersionCode; this.installedVersionCode = installedVersionCode;
this.lastUpdate = lastUpdate; this.lastUpdate = lastUpdate;
this.safe = false; this.safe = false;
this.stats = stats;
} }
public ModuleListCache() { public ModuleListCache() {
@ -241,6 +243,14 @@ public class ModuleListCache extends RealmObject {
this.safe = safe; this.safe = safe;
} }
public int getStats() {
return stats;
}
public void setStats(int stats) {
this.stats = stats;
}
private JSONObject toJson() { private JSONObject toJson() {
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
try { try {
@ -261,6 +271,9 @@ public class ModuleListCache extends RealmObject {
jsonObject.put("repoId", repoId); jsonObject.put("repoId", repoId);
jsonObject.put("installed", installed); jsonObject.put("installed", installed);
jsonObject.put("installedVersionCode", installedVersionCode); jsonObject.put("installedVersionCode", installedVersionCode);
jsonObject.put("lastUpdate", lastUpdate);
jsonObject.put("safe", safe);
jsonObject.put("stats", stats);
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }

Loading…
Cancel
Save