tweak dataDir func

Signed-off-by: androidacy-user <opensource@androidacy.com>
pull/27/head
androidacy-user 2 years ago
parent b95559cd9c
commit c26c17e7ae

@ -412,17 +412,38 @@ public class MainApplication extends FoxApplication implements androidx.work.Con
// getDataDir wrapper with optional path parameter // getDataDir wrapper with optional path parameter
public File getDataDirWithPath(String path) { public File getDataDirWithPath(String path) {
File dataDir = this.getDataDir(); File dataDir = this.getDataDir();
// for path with / somewhere in the middle, its a subdirectory
if (path != null) { if (path != null) {
dataDir = new File(dataDir, path); if (path.startsWith("/"))
} path = path.substring(1);
// create the directory if it doesn't exist if (path.endsWith("/"))
if (!dataDir.exists()) { path = path.substring(0, path.length() - 1);
if (!dataDir.mkdirs()) { if (path.contains("/")) {
if (BuildConfig.DEBUG) String[] dirs = path.split("/");
Timber.w("Failed to create directory %s", dataDir); for (String dir : dirs) {
dataDir = new File(dataDir, dir);
// make sure the directory exists
if (!dataDir.exists()) {
if (!dataDir.mkdirs()) {
if (BuildConfig.DEBUG)
Timber.w("Failed to create directory %s", dataDir);
}
}
}
} else {
dataDir = new File(dataDir, path);
// create the directory if it doesn't exist
if (!dataDir.exists()) {
if (!dataDir.mkdirs()) {
if (BuildConfig.DEBUG)
Timber.w("Failed to create directory %s", dataDir);
}
}
} }
return dataDir;
} else {
throw new IllegalArgumentException("Path cannot be null");
} }
return dataDir;
} }
public void clearAppData() { public void clearAppData() {

@ -353,4 +353,27 @@ public class RepoData extends XRepo {
public final boolean isForceHide() { public final boolean isForceHide() {
return this.forceHide; return this.forceHide;
} }
// should update (lastUpdate > 15 minutes)
public boolean shouldUpdate() {
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);
ReposList repo = realm2.where(ReposList.class).equalTo("id", this.id).findFirst();
// Make sure ModuleListCache for repoId is not null
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder().name("ModuleListCache.realm").allowQueriesOnUiThread(true).allowWritesOnUiThread(true).directory(MainApplication.getINSTANCE().getDataDirWithPath("realms/" + this.id)).schemaVersion(1).build();
Realm realm = Realm.getInstance(realmConfiguration);
ModuleListCache moduleListCache = realm.where(ModuleListCache.class).equalTo("repoId", this.id).findFirst();
if (repo != null) {
if (repo.getLastUpdate() != 0 && moduleListCache != null) {
long lastUpdate = repo.getLastUpdate();
long currentTime = System.currentTimeMillis();
long diff = currentTime - lastUpdate;
long diffMinutes = diff / (60 * 1000) % 60;
return diffMinutes > 15;
} else {
return true;
}
}
return true;
}
} }

@ -1,7 +1,9 @@
package com.fox2code.mmm.repo; package com.fox2code.mmm.repo;
import com.fox2code.mmm.MainApplication;
import com.fox2code.mmm.utils.io.Http; import com.fox2code.mmm.utils.io.Http;
import com.fox2code.mmm.utils.realm.ModuleListCache; import com.fox2code.mmm.utils.realm.ModuleListCache;
import com.fox2code.mmm.utils.realm.ReposList;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
@ -12,9 +14,11 @@ import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Objects;
import io.realm.Realm; import io.realm.Realm;
import io.realm.RealmConfiguration; import io.realm.RealmConfiguration;
import io.realm.RealmResults;
import timber.log.Timber; import timber.log.Timber;
public class RepoUpdater { public class RepoUpdater {
@ -40,6 +44,32 @@ public class RepoUpdater {
this.toApply = Collections.emptySet(); this.toApply = Collections.emptySet();
return 0; return 0;
} }
// if we shouldn't update, get the values from the ModuleListCache realm
if (!this.repoData.shouldUpdate()) {
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()
.name("ModuleListCache.realm")
.schemaVersion(1)
.modules(new ModuleListCache())
.build();
Realm realm = Realm.getInstance(realmConfiguration);
RealmResults<ModuleListCache> results = realm.where(ModuleListCache.class).equalTo("repoId", this.repoData.id).findAll();
// reposlist realm
RealmConfiguration realmConfiguration2 = new RealmConfiguration.Builder()
.name("ReposList.realm")
.schemaVersion(1)
.modules(new ReposList())
.build();
Realm realm2 = Realm.getInstance(realmConfiguration2);
ReposList reposList = realm2.where(ReposList.class).equalTo("id", this.repoData.id).findFirst();
this.toUpdate = Collections.emptyList();
this.toApply = new HashSet<>();
for (ModuleListCache moduleListCache : results) {
RepoData repoData = RepoManager.getINSTANCE().get(Objects.requireNonNull(reposList).getUrl());
this.toApply.add(new RepoModule(repoData, moduleListCache.getId()));
}
this.toApply = new HashSet<>(this.toUpdate);
return this.toUpdate.size();
}
try { try {
if (!this.repoData.prepare()) { if (!this.repoData.prepare()) {
this.indexRaw = null; this.indexRaw = null;
@ -83,7 +113,7 @@ public class RepoUpdater {
// iterate over modules, using this.supportedProperties as a template to attempt to get each property from the module. everything that is not null is added to the module // iterate over modules, using this.supportedProperties as a template to attempt to get each property from the module. everything that is not null is added to the module
// use realm to insert to // use realm to insert to
// props avail: // props avail:
File cacheRoot = this.repoData.cacheRoot; File cacheRoot = MainApplication.getINSTANCE().getDataDirWithPath("realms/repos" + this.repoData.id);
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder().name("ModuleListCache.realm").schemaVersion(1).deleteRealmIfMigrationNeeded().allowWritesOnUiThread(true).allowQueriesOnUiThread(true).directory(cacheRoot).build(); RealmConfiguration realmConfiguration = new RealmConfiguration.Builder().name("ModuleListCache.realm").schemaVersion(1).deleteRealmIfMigrationNeeded().allowWritesOnUiThread(true).allowQueriesOnUiThread(true).directory(cacheRoot).build();
// array with module info default values // array with module info default values
// supported properties for a module // supported properties for a module
@ -119,13 +149,24 @@ public class RepoUpdater {
// get modules from "modules" key. This is a JSONArray so we need to convert it to a JSONObject // get modules from "modules" key. This is a JSONArray so we need to convert it to a JSONObject
modulesArray = modules.getJSONArray("modules"); modulesArray = modules.getJSONArray("modules");
} }
Realm realm = Realm.getInstance(realmConfiguration);
// drop old data
realm.beginTransaction();
realm.where(ModuleListCache.class).equalTo("repoId", this.repoData.id).findAll().deleteAllFromRealm();
realm.commitTransaction();
// iterate over modules. pls dont hate me for this, its ugly but it works // iterate over modules. pls dont hate me for this, its ugly but it works
for (int n = 0; n < modulesArray.length(); n++) { for (int n = 0; n < modulesArray.length(); n++) {
// get module // get module
JSONObject module = modulesArray.getJSONObject(n); JSONObject module = modulesArray.getJSONObject(n);
try { try {
// get module id // get module id
String id = module.getString("id"); // if codename is present, prefer that over id
String id;
if (module.has("codename") && !module.getString("codename").equals("")) {
id = module.getString("codename");
} else {
id = module.getString("id");
}
// get module name // get module name
String name = module.getString("name"); String name = module.getString("name");
// get module version // get module version
@ -216,62 +257,56 @@ public class RepoUpdater {
// then insert to realm // then insert to realm
// then commit // then commit
// then close // then close
Realm realm = Realm.getInstance(realmConfiguration);
if (realm.isInTransaction()) { if (realm.isInTransaction()) {
realm.cancelTransaction(); realm.cancelTransaction();
} }
realm.executeTransaction(r -> { // create a realm object and insert or update it
// create the object // add everything to the realm object
// if it already exists, it will be updated realm.beginTransaction();
// create a new module ModuleListCache moduleListCache = realm.createObject(ModuleListCache.class, id);
ModuleListCache moduleListCache = r.createObject(ModuleListCache.class, id); moduleListCache.setName(name);
// set module name moduleListCache.setVersion(version);
moduleListCache.setName(name); moduleListCache.setVersionCode(versionCode);
// set module version moduleListCache.setAuthor(author);
moduleListCache.setVersion(version); moduleListCache.setDescription(description);
// set module version code moduleListCache.setMinApi(minApiInt);
moduleListCache.setVersionCode(versionCode); moduleListCache.setMaxApi(maxApiInt);
// set module author moduleListCache.setMinMagisk(minMagiskInt);
moduleListCache.setAuthor(author); moduleListCache.setNeedRamdisk(needRamdisk);
// set module description moduleListCache.setSupport(support);
moduleListCache.setDescription(description); moduleListCache.setDonate(donate);
// set module min api moduleListCache.setConfig(config);
moduleListCache.setMinApi(minApiInt); moduleListCache.setChangeBoot(changeBoot);
// set module max api moduleListCache.setMmtReborn(mmtReborn);
moduleListCache.setMaxApi(maxApiInt); moduleListCache.setRepoId(repoId);
// set module min magisk moduleListCache.setInstalled(installed);
moduleListCache.setMinMagisk(minMagiskInt); moduleListCache.setInstalledVersionCode(installedVersionCode);
// set module need ramdisk realm.copyToRealmOrUpdate(moduleListCache);
moduleListCache.setNeedRamdisk(needRamdisk); realm.commitTransaction();
// set module support
moduleListCache.setSupport(support);
// set module donate
moduleListCache.setDonate(donate);
// set module config
moduleListCache.setConfig(config);
// set module change boot
moduleListCache.setChangeBoot(changeBoot);
// set module mmt reborn
moduleListCache.setMmtReborn(mmtReborn);
// set module repo id
moduleListCache.setRepoId(repoId);
// set module installed
moduleListCache.setInstalled(installed);
// set module installed version code
moduleListCache.setInstalledVersionCode(installedVersionCode);
});
realm.close();
} catch ( } catch (
Exception e) { Exception e) {
e.printStackTrace(); e.printStackTrace();
Timber.w("Failed to get module info from module " + module + " in repo " + this.repoData.id + " with error " + e.getMessage()); Timber.w("Failed to get module info from module " + module + " in repo " + this.repoData.id + " with error " + e.getMessage());
} }
} }
realm.close();
} catch ( } catch (
Exception e) { Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
this.indexRaw = null; this.indexRaw = null;
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);
if (realm2.isInTransaction()) {
realm2.cancelTransaction();
}
// set lastUpdate
realm2.executeTransaction(r -> {
ReposList repoListCache = r.where(ReposList.class).equalTo("id", this.repoData.id).findFirst();
if (repoListCache != null) {
repoListCache.setLastUpdate((int) System.currentTimeMillis());
}
});
} }
this.toUpdate = null; this.toUpdate = null;
this.toApply = null; this.toApply = null;

Loading…
Cancel
Save