migrate more to using realm

it might crash but hey at least it builds right

Signed-off-by: androidacy-user <opensource@androidacy.com>
pull/27/head
androidacy-user 3 years ago
parent 2a45502d91
commit 21da75c3fc

@ -4,10 +4,12 @@ import android.annotation.SuppressLint;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.net.Uri; import android.net.Uri;
import android.os.Build;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import com.fox2code.mmm.BuildConfig; import com.fox2code.mmm.BuildConfig;
import com.fox2code.mmm.MainApplication; import com.fox2code.mmm.MainApplication;
@ -19,6 +21,7 @@ import com.fox2code.mmm.repo.RepoModule;
import com.fox2code.mmm.utils.io.Http; import com.fox2code.mmm.utils.io.Http;
import com.fox2code.mmm.utils.io.HttpException; import com.fox2code.mmm.utils.io.HttpException;
import com.fox2code.mmm.utils.io.PropUtils; import com.fox2code.mmm.utils.io.PropUtils;
import com.fox2code.mmm.utils.realm.ReposList;
import com.google.android.material.dialog.MaterialAlertDialogBuilder; import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.topjohnwu.superuser.Shell; import com.topjohnwu.superuser.Shell;
@ -37,15 +40,15 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import io.realm.Realm;
import io.realm.RealmConfiguration;
import okhttp3.HttpUrl; import okhttp3.HttpUrl;
@SuppressWarnings("KotlinInternalInJava") @SuppressWarnings("KotlinInternalInJava")
public final class AndroidacyRepoData extends RepoData { public final class AndroidacyRepoData extends RepoData {
public static String token = MainApplication.getINSTANCE().getSharedPreferences("androidacy", 0).getString("pref_androidacy_api_token", null);
public SharedPreferences cachedPreferences = MainApplication.getINSTANCE().getSharedPreferences("androidacy", 0);
private static final String TAG = "AndroidacyRepoData"; private static final String TAG = "AndroidacyRepoData";
public static String token = MainApplication.getINSTANCE().getSharedPreferences("androidacy", 0).getString("pref_androidacy_api_token", null);
static { static {
HttpUrl.Builder OK_HTTP_URL_BUILDER = new HttpUrl.Builder().scheme("https"); HttpUrl.Builder OK_HTTP_URL_BUILDER = new HttpUrl.Builder().scheme("https");
@ -56,27 +59,18 @@ public final class AndroidacyRepoData extends RepoData {
@SuppressWarnings("unused") @SuppressWarnings("unused")
public final String ClientID = BuildConfig.ANDROIDACY_CLIENT_ID; public final String ClientID = BuildConfig.ANDROIDACY_CLIENT_ID;
private final boolean testMode; private final boolean testMode;
private final String host; private final String host;
public SharedPreferences cachedPreferences = MainApplication.getINSTANCE().getSharedPreferences("androidacy", 0);
public String memberLevel; public String memberLevel;
// Avoid spamming requests to Androidacy // Avoid spamming requests to Androidacy
private long androidacyBlockade = 0; private long androidacyBlockade = 0;
public AndroidacyRepoData(File cacheRoot, SharedPreferences cachedPreferences, boolean testMode) { public AndroidacyRepoData(File cacheRoot, SharedPreferences cachedPreferences, boolean testMode) {
super(testMode ? RepoManager.ANDROIDACY_TEST_MAGISK_REPO_ENDPOINT : RepoManager.ANDROIDACY_MAGISK_REPO_ENDPOINT, cacheRoot, cachedPreferences); super(testMode ? RepoManager.ANDROIDACY_TEST_MAGISK_REPO_ENDPOINT : RepoManager.ANDROIDACY_MAGISK_REPO_ENDPOINT, cacheRoot, cachedPreferences);
// make sure the modules.json exists RealmConfiguration realmConfiguration = new RealmConfiguration.Builder().name("ModuleListCache.realm").allowWritesOnUiThread(true).allowWritesOnUiThread(true).directory(cacheRoot).build();
File modulesJson = new File(cacheRoot, "modules.json"); Realm.setDefaultConfiguration(realmConfiguration);
if (!modulesJson.exists()) { Realm.getInstance(realmConfiguration);
try {
if (!modulesJson.createNewFile()) {
throw new IOException("Failed to create modules.json");
}
} catch (
IOException e) {
e.printStackTrace();
}
}
this.defaultName = "Androidacy Modules Repo"; this.defaultName = "Androidacy Modules Repo";
this.defaultWebsite = RepoManager.ANDROIDACY_MAGISK_REPO_HOMEPAGE; this.defaultWebsite = RepoManager.ANDROIDACY_MAGISK_REPO_HOMEPAGE;
this.defaultSupport = "https://t.me/androidacy_discussions"; this.defaultSupport = "https://t.me/androidacy_discussions";
@ -86,6 +80,7 @@ public final class AndroidacyRepoData extends RepoData {
this.testMode = testMode; this.testMode = testMode;
} }
@RequiresApi(api = Build.VERSION_CODES.N)
public static AndroidacyRepoData getInstance() { public static AndroidacyRepoData getInstance() {
return RepoManager.getINSTANCE().getAndroidacyRepoData(); return RepoManager.getINSTANCE().getAndroidacyRepoData();
} }
@ -187,6 +182,23 @@ public final class AndroidacyRepoData extends RepoData {
@SuppressLint("RestrictedApi") @SuppressLint("RestrictedApi")
@Override @Override
protected boolean prepare() throws NoSuchAlgorithmException { protected boolean prepare() throws NoSuchAlgorithmException {
// insert metadata into database
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder().name("ReposListCache.realm").allowWritesOnUiThread(true).allowWritesOnUiThread(true).directory(cacheRoot).build();
Realm realm = Realm.getInstance(realmConfiguration);
realm.beginTransaction();
ReposList repo = realm.where(ReposList.class).equalTo("id", this.id).findFirst();
if (repo == null) {
repo = realm.createObject(ReposList.class, this.id);
}
repo.setName(this.defaultName);
repo.setWebsite(this.defaultWebsite);
repo.setSupport(this.defaultSupport);
repo.setDonate(this.defaultDonate);
repo.setSubmitModule(this.defaultSubmitModule);
repo.setLastUpdate(0);
// close realm
realm.commitTransaction();
realm.close();
// If ANDROIDACY_CLIENT_ID is not set or is empty, disable this repo and return // If ANDROIDACY_CLIENT_ID is not set or is empty, disable this repo and return
if (Objects.equals(BuildConfig.ANDROIDACY_CLIENT_ID, "")) { if (Objects.equals(BuildConfig.ANDROIDACY_CLIENT_ID, "")) {
SharedPreferences.Editor editor = MainApplication.getSharedPreferences().edit(); SharedPreferences.Editor editor = MainApplication.getSharedPreferences().edit();

@ -15,6 +15,8 @@ import com.fox2code.mmm.XRepo;
import com.fox2code.mmm.manager.ModuleInfo; import com.fox2code.mmm.manager.ModuleInfo;
import com.fox2code.mmm.utils.io.Files; import com.fox2code.mmm.utils.io.Files;
import com.fox2code.mmm.utils.io.PropUtils; import com.fox2code.mmm.utils.io.PropUtils;
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.JSONException; import org.json.JSONException;
@ -22,62 +24,111 @@ import org.json.JSONObject;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import io.realm.Realm;
import io.realm.RealmConfiguration;
public class RepoData extends XRepo { public class RepoData extends XRepo {
public final String url; public final String url;
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 final File metaDataCache; public JSONObject metaDataCache;
public final HashMap<String, RepoModule> moduleHashMap; public final HashMap<String, RepoModule> moduleHashMap;
private final Object populateLock = new Object(); private final Object populateLock = new Object();
public long lastUpdate; public long lastUpdate;
public String name, website, support, donate, submitModule; public String name, website, support, donate, submitModule;
public JSONObject supportedProperties = new JSONObject();
protected String defaultName, defaultWebsite, defaultSupport, defaultDonate, defaultSubmitModule; protected String defaultName, defaultWebsite, defaultSupport, defaultDonate, defaultSubmitModule;
// array with module info default values
// supported properties for a module
//id=<string>
//name=<string>
//version=<string>
//versionCode=<int>
//author=<string>
//description=<string>
//minApi=<int>
//maxApi=<int>
//minMagisk=<int>
//needRamdisk=<boolean>
//support=<url>
//donate=<url>
//config=<package>
//changeBoot=<boolean>
//mmtReborn=<boolean>
// extra properties only useful for the database
//repoId=<string>
//installed=<boolean>
//installedVersionCode=<int> (only if installed)
private boolean forceHide, enabled; // Cache for speed private boolean forceHide, enabled; // Cache for speed
public RepoData(String url, File cacheRoot, SharedPreferences cachedPreferences) { public RepoData(String url, File cacheRoot, SharedPreferences cachedPreferences) {
// setup supportedProperties
try {
supportedProperties.put("id", "");
supportedProperties.put("name", "");
supportedProperties.put("version", "");
supportedProperties.put("versionCode", "");
supportedProperties.put("author", "");
supportedProperties.put("description", "");
supportedProperties.put("minApi", "");
supportedProperties.put("maxApi", "");
supportedProperties.put("minMagisk", "");
supportedProperties.put("needRamdisk", "");
supportedProperties.put("support", "");
supportedProperties.put("donate", "");
supportedProperties.put("config", "");
supportedProperties.put("changeBoot", "");
supportedProperties.put("mmtReborn", "");
supportedProperties.put("repoId", "");
supportedProperties.put("installed", "");
supportedProperties.put("installedVersionCode", "");
} catch (JSONException e) {
e.printStackTrace();
}
this.url = url; this.url = url;
this.id = RepoManager.internalIdOfUrl(url); this.id = RepoManager.internalIdOfUrl(url);
this.cacheRoot = cacheRoot; this.cacheRoot = cacheRoot;
this.cachedPreferences = cachedPreferences; this.cachedPreferences = cachedPreferences;
this.metaDataCache = new File(cacheRoot, "modules.json"); // metadata cache is a realm database from ModuleListCache
this.metaDataCache = null;
this.moduleHashMap = new HashMap<>(); this.moduleHashMap = new HashMap<>();
this.defaultName = url; // Set url as default name this.defaultName = url; // Set url as default name
this.forceHide = AppUpdateManager.shouldForceHide(this.id); this.forceHide = AppUpdateManager.shouldForceHide(this.id);
this.enabled = (!this.forceHide) && MainApplication.getSharedPreferences().getBoolean("pref_" + this.getPreferenceId() + "_enabled", true); this.enabled = (!this.forceHide) && MainApplication.getSharedPreferences().getBoolean("pref_" + this.getPreferenceId() + "_enabled", true);
this.defaultWebsite = "https://" + Uri.parse(url).getHost() + "/"; this.defaultWebsite = "https://" + Uri.parse(url).getHost() + "/";
if (!this.cacheRoot.isDirectory()) { // open realm database
boolean mkdirs = this.cacheRoot.mkdirs(); // load metadata from realm database
if (!mkdirs) { if (this.enabled) {
throw new RuntimeException("Failed to create cache directory"); RealmConfiguration realmConfiguration = new RealmConfiguration.Builder().name("ModuleListCache.realm").allowQueriesOnUiThread(true).allowWritesOnUiThread(true).directory(cacheRoot).build();
} // load metadata from realm database
} else { Realm.getInstance(realmConfiguration);
if (this.metaDataCache.exists()) { this.metaDataCache = ModuleListCache.getRepoModulesAsJson(this.id);
this.lastUpdate = metaDataCache.lastModified(); // load repo metadata from ReposList unless it's a built-in repo
if (this.lastUpdate > System.currentTimeMillis()) { if (RepoManager.isBuiltInRepo(this.id)) {
this.lastUpdate = 0; // Don't allow time travel this.name = this.defaultName;
} this.website = this.defaultWebsite;
try { this.support = this.defaultSupport;
List<RepoModule> modules = this.populate(new JSONObject(new String(Files.read(this.metaDataCache), StandardCharsets.UTF_8))); this.donate = this.defaultDonate;
for (RepoModule repoModule : modules) { this.submitModule = this.defaultSubmitModule;
if (!this.tryLoadMetadata(repoModule)) { } else {
repoModule.moduleInfo.flags &= ~ModuleInfo.FLAG_METADATA_INVALID; // get everything from ReposList realm database
} RealmConfiguration realmConfiguration2 = new RealmConfiguration.Builder().name("ReposList.realm").allowQueriesOnUiThread(true).allowWritesOnUiThread(true).directory(cacheRoot).build();
} // load metadata from realm database
} catch ( Realm.getInstance(realmConfiguration2);
Exception e) { this.name = ReposList.getRepo(this.id).getName();
boolean delete = this.metaDataCache.delete(); this.website = ReposList.getRepo(this.id).getWebsite();
if (!delete) { this.support = ReposList.getRepo(this.id).getSupport();
throw new RuntimeException("Failed to delete invalid cache file"); this.donate = ReposList.getRepo(this.id).getDonate();
} this.submitModule = ReposList.getRepo(this.id).getSubmitModule();
}
} }
} }
} }
@ -225,7 +276,8 @@ public class RepoData extends XRepo {
@Override @Override
public void setEnabled(boolean enabled) { public void setEnabled(boolean enabled) {
this.enabled = enabled && !this.forceHide; this.enabled = enabled && !this.forceHide;
if (BuildConfig.DEBUG) { Log.d("RepoData", "Repo " + this.id + " enabled: " + this.enabled + " (forced: " + this.forceHide + ") with preferenceID: " + this.getPreferenceId()); if (BuildConfig.DEBUG) {
Log.d("RepoData", "Repo " + this.id + " enabled: " + this.enabled + " (forced: " + this.forceHide + ") with preferenceID: " + this.getPreferenceId());
} }
MainApplication.getSharedPreferences().edit().putBoolean("pref_" + this.getPreferenceId() + "_enabled", enabled).apply(); MainApplication.getSharedPreferences().edit().putBoolean("pref_" + this.getPreferenceId() + "_enabled", enabled).apply();
} }
@ -236,7 +288,8 @@ public class RepoData extends XRepo {
return; return;
} }
this.forceHide = AppUpdateManager.shouldForceHide(this.id); this.forceHide = AppUpdateManager.shouldForceHide(this.id);
if (BuildConfig.DEBUG) { Log.d("RepoData", "Repo " + this.id + " update enabled: " + this.enabled + " (forced: " + this.forceHide + ") with preferenceID: " + this.getPreferenceId()); if (BuildConfig.DEBUG) {
Log.d("RepoData", "Repo " + this.id + " update enabled: " + this.enabled + " (forced: " + this.forceHide + ") with preferenceID: " + this.getPreferenceId());
} }
this.enabled = (!this.forceHide) && MainApplication.getSharedPreferences().getBoolean("pref_" + this.getPreferenceId() + "_enabled", true); this.enabled = (!this.forceHide) && MainApplication.getSharedPreferences().getBoolean("pref_" + this.getPreferenceId() + "_enabled", true);
} }

@ -4,12 +4,14 @@ import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Build;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import com.fox2code.mmm.BuildConfig; import com.fox2code.mmm.BuildConfig;
import com.fox2code.mmm.MainActivity; import com.fox2code.mmm.MainActivity;
@ -63,6 +65,7 @@ public final class RepoManager extends SyncManager {
private boolean initialized; private boolean initialized;
private boolean repoLastSuccess; private boolean repoLastSuccess;
@RequiresApi(api = Build.VERSION_CODES.N)
private RepoManager(MainApplication mainApplication) { private RepoManager(MainApplication mainApplication) {
INSTANCE = this; // Set early fox XHooks INSTANCE = this; // Set early fox XHooks
this.initialized = false; this.initialized = false;
@ -89,6 +92,7 @@ public final class RepoManager extends SyncManager {
this.initialized = true; this.initialized = true;
} }
@RequiresApi(api = Build.VERSION_CODES.N)
public static RepoManager getINSTANCE() { public static RepoManager getINSTANCE() {
if (INSTANCE == null || !INSTANCE.initialized) { if (INSTANCE == null || !INSTANCE.initialized) {
synchronized (lock) { synchronized (lock) {
@ -106,6 +110,7 @@ public final class RepoManager extends SyncManager {
return INSTANCE; return INSTANCE;
} }
@RequiresApi(api = Build.VERSION_CODES.N)
public static RepoManager getINSTANCE_UNSAFE() { public static RepoManager getINSTANCE_UNSAFE() {
if (INSTANCE == null) { if (INSTANCE == null) {
synchronized (lock) { synchronized (lock) {
@ -156,6 +161,7 @@ public final class RepoManager extends SyncManager {
return INSTANCE != null && INSTANCE.androidacyRepoData != null && INSTANCE.androidacyRepoData.isEnabled(); return INSTANCE != null && INSTANCE.androidacyRepoData != null && INSTANCE.androidacyRepoData.isEnabled();
} }
@RequiresApi(api = Build.VERSION_CODES.N)
@SuppressWarnings("StatementWithEmptyBody") @SuppressWarnings("StatementWithEmptyBody")
private void populateDefaultCache(RepoData repoData) { private void populateDefaultCache(RepoData repoData) {
for (RepoModule repoModule : repoData.moduleHashMap.values()) { for (RepoModule repoModule : repoData.moduleHashMap.values()) {
@ -185,10 +191,12 @@ public final class RepoManager extends SyncManager {
return this.repoData.get(url); return this.repoData.get(url);
} }
@RequiresApi(api = Build.VERSION_CODES.N)
public RepoData addOrGet(String url) { public RepoData addOrGet(String url) {
return this.addOrGet(url, null); return this.addOrGet(url, null);
} }
@RequiresApi(api = Build.VERSION_CODES.N)
public RepoData addOrGet(String url, String fallBackName) { public RepoData addOrGet(String url, String fallBackName) {
if (MAGISK_ALT_REPO_JSDELIVR.equals(url)) if (MAGISK_ALT_REPO_JSDELIVR.equals(url))
url = MAGISK_ALT_REPO; url = MAGISK_ALT_REPO;
@ -211,6 +219,8 @@ public final class RepoManager extends SyncManager {
return repoData; return repoData;
} }
@SuppressWarnings("StatementWithEmptyBody")
@RequiresApi(api = Build.VERSION_CODES.N)
@SuppressLint("StringFormatInvalid") @SuppressLint("StringFormatInvalid")
protected void scanInternal(@NonNull UpdateListener updateListener) { protected void scanInternal(@NonNull UpdateListener updateListener) {
// Refuse to start if first_launch is not false in shared preferences // Refuse to start if first_launch is not false in shared preferences
@ -378,9 +388,10 @@ public final class RepoManager extends SyncManager {
return this.hasInternet; return this.hasInternet;
} }
@RequiresApi(api = Build.VERSION_CODES.N)
private RepoData addRepoData(String url, String fallBackName) { private RepoData addRepoData(String url, String fallBackName) {
String id = internalIdOfUrl(url); String id = internalIdOfUrl(url);
File cacheRoot = new File(this.mainApplication.getCacheDir(), id); File cacheRoot = new File(this.mainApplication.getDataDir(), id);
SharedPreferences sharedPreferences = this.mainApplication.getSharedPreferences("mmm_" + id, Context.MODE_PRIVATE); SharedPreferences sharedPreferences = this.mainApplication.getSharedPreferences("mmm_" + id, Context.MODE_PRIVATE);
RepoData repoData = id.startsWith("repo_") ? new CustomRepoData(url, cacheRoot, sharedPreferences) : new RepoData(url, cacheRoot, sharedPreferences); RepoData repoData = id.startsWith("repo_") ? new CustomRepoData(url, cacheRoot, sharedPreferences) : new RepoData(url, cacheRoot, sharedPreferences);
if (fallBackName != null && !fallBackName.isEmpty()) { if (fallBackName != null && !fallBackName.isEmpty()) {
@ -404,8 +415,10 @@ public final class RepoManager extends SyncManager {
return repoData; return repoData;
} }
@RequiresApi(api = Build.VERSION_CODES.N)
private AndroidacyRepoData addAndroidacyRepoData() { private AndroidacyRepoData addAndroidacyRepoData() {
File cacheRoot = new File(this.mainApplication.getCacheDir(), "androidacy_repo"); // cache dir is actually under app data
File cacheRoot = new File(this.mainApplication.getDataDir(), "androidacy_repo");
SharedPreferences sharedPreferences = this.mainApplication.getSharedPreferences("mmm_androidacy_repo", Context.MODE_PRIVATE); SharedPreferences sharedPreferences = this.mainApplication.getSharedPreferences("mmm_androidacy_repo", Context.MODE_PRIVATE);
AndroidacyRepoData repoData = new AndroidacyRepoData(cacheRoot, sharedPreferences, MainApplication.isAndroidacyTestMode()); AndroidacyRepoData repoData = new AndroidacyRepoData(cacheRoot, sharedPreferences, MainApplication.isAndroidacyTestMode());
this.repoData.put(ANDROIDACY_MAGISK_REPO_ENDPOINT, repoData); this.repoData.put(ANDROIDACY_MAGISK_REPO_ENDPOINT, repoData);

@ -1,20 +1,25 @@
package com.fox2code.mmm.repo; package com.fox2code.mmm.repo;
import android.os.Build;
import android.util.Log; import android.util.Log;
import com.fox2code.mmm.BuildConfig; import androidx.annotation.NonNull;
import com.fox2code.mmm.utils.io.Files; import androidx.annotation.RequiresApi;
import com.fox2code.mmm.utils.io.Http; import com.fox2code.mmm.utils.io.Http;
import com.fox2code.mmm.utils.realm.ModuleListCache;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Collection; 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 io.realm.Realm;
import io.realm.RealmConfiguration;
public class RepoUpdater { public class RepoUpdater {
private static final String TAG = "RepoUpdater"; private static final String TAG = "RepoUpdater";
public final RepoData repoData; public final RepoData repoData;
@ -26,6 +31,7 @@ public class RepoUpdater {
this.repoData = repoData; this.repoData = repoData;
} }
@RequiresApi(api = Build.VERSION_CODES.N)
public int fetchIndex() { public int fetchIndex() {
if (!RepoManager.getINSTANCE().hasConnectivity()) { if (!RepoManager.getINSTANCE().hasConnectivity()) {
this.indexRaw = null; this.indexRaw = null;
@ -79,12 +85,134 @@ public class RepoUpdater {
} }
if (this.indexRaw != null) { if (this.indexRaw != null) {
try { try {
Files.write(this.repoData.metaDataCache, this.indexRaw); // 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
if (BuildConfig.DEBUG) { // use realm to insert to
Log.d(TAG, "Wrote index of " + this.repoData.id); // props avail:
//
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder().name("ModuleListCache.realm").schemaVersion(1).deleteRealmIfMigrationNeeded().build();
// array with module info default values
// supported properties for a module
//id=<string>
//name=<string>
//version=<string>
//versionCode=<int>
//author=<string>
//description=<string>
//minApi=<int>
//maxApi=<int>
//minMagisk=<int>
//needRamdisk=<boolean>
//support=<url>
//donate=<url>
//config=<package>
//changeBoot=<boolean>
//mmtReborn=<boolean>
// extra properties only useful for the database
//repoId=<string>
//installed=<boolean>
//installedVersionCode=<int> (only if installed)
//
// all except first six can be null
for (JSONObject module : new JSONObject[]{new JSONObject(new String(this.indexRaw, StandardCharsets.UTF_8))}) {
// get module id
String id = module.getString("id");
// get module name
String name = module.getString("name");
// get module version
String version = module.getString("version");
// get module version code
int versionCode = module.getInt("versionCode");
// get module author
String author = module.getString("author");
// get module description
String description = module.getString("description");
// get module min api
int minApi = module.getInt("minApi");
// get module max api
int maxApi = module.getInt("maxApi");
// get module min magisk
int minMagisk = module.getInt("minMagisk");
// get module need ramdisk
boolean needRamdisk = module.getBoolean("needRamdisk");
// get module support
String support = module.getString("support");
// get module donate
String donate = module.getString("donate");
// get module config
String config = module.getString("config");
// get module change boot
boolean changeBoot = module.getBoolean("changeBoot");
// get module mmt reborn
boolean mmtReborn = module.getBoolean("mmtReborn");
// get module repo id
String repoId = this.repoData.id;
// get module installed
boolean installed = false;
// get module installed version code
int installedVersionCode = 0;
// insert module to realm
// first create a collection of all the properties
// then insert to realm
// then commit
// then close
Realm.getInstanceAsync(realmConfiguration, new Realm.Callback() {
@Override
public void onSuccess(@NonNull Realm realm) {
realm.executeTransactionAsync(r -> {
// create a new module
ModuleListCache moduleListCache = r.createObject(ModuleListCache.class);
// set module id
moduleListCache.setId(id);
// set module name
moduleListCache.setName(name);
// set module version
moduleListCache.setVersion(version);
// set module version code
moduleListCache.setVersionCode(versionCode);
// set module author
moduleListCache.setAuthor(author);
// set module description
moduleListCache.setDescription(description);
// set module min api
moduleListCache.setMinApi(minApi);
// set module max api
moduleListCache.setMaxApi(maxApi);
// set module min magisk
moduleListCache.setMinMagisk(minMagisk);
// set module need ramdisk
moduleListCache.setNeedRamdisk(needRamdisk);
// 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);
}, () -> {
// Transaction was a success.
Log.d(TAG, "onSuccess: Transaction was a success.");
// close realm
realm.close();
}, error -> {
// Transaction failed and was automatically canceled.
Log.e(TAG, "onError: Transaction failed and was automatically canceled.", error);
// close realm
realm.close();
});
}
});
} }
} catch ( } catch (
IOException e) { Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
this.indexRaw = null; this.indexRaw = null;
@ -93,4 +221,5 @@ public class RepoUpdater {
this.toApply = null; this.toApply = null;
return success; return success;
} }
} }

@ -11,27 +11,6 @@ import io.realm.annotations.Required;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class ModuleListCache extends RealmObject { public class ModuleListCache extends RealmObject {
// supported properties for a module
//id=<string>
//name=<string>
//version=<string>
//versionCode=<int>
//author=<string>
//description=<string>
//minApi=<int>
//maxApi=<int>
//minMagisk=<int>
//needRamdisk=<boolean>
//support=<url>
//donate=<url>
//config=<package>
//changeBoot=<boolean>
//mmtReborn=<boolean>
// extra properties only useful for the database
//repoId=<string>
//installed=<boolean>
//installedVersionCode=<int> (only if installed)
// for compatibility, only id is required // for compatibility, only id is required
@PrimaryKey @PrimaryKey
@Required @Required
@ -53,8 +32,9 @@ public class ModuleListCache extends RealmObject {
private String repoId; private String repoId;
private boolean installed; private boolean installed;
private int installedVersionCode; private int installedVersionCode;
private int lastUpdate;
public ModuleListCache(String id, 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) { public ModuleListCache(String id, 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) {
this.id = id; this.id = id;
this.name = name; this.name = name;
this.version = version; this.version = version;
@ -73,6 +53,7 @@ public class ModuleListCache extends RealmObject {
this.repoId = repoId; this.repoId = repoId;
this.installed = installed; this.installed = installed;
this.installedVersionCode = installedVersionCode; this.installedVersionCode = installedVersionCode;
this.lastUpdate = lastUpdate;
} }
public ModuleListCache() { public ModuleListCache() {
@ -239,6 +220,14 @@ public class ModuleListCache extends RealmObject {
this.id = id; this.id = id;
} }
public int getLastUpdate() {
return lastUpdate;
}
public void setLastUpdate(int lastUpdate) {
this.lastUpdate = lastUpdate;
}
private JSONObject toJson() { private JSONObject toJson() {
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
try { try {
@ -264,4 +253,12 @@ public class ModuleListCache extends RealmObject {
} }
return jsonObject; return jsonObject;
} }
public RealmResults<ModuleListCache> getModules() {
// return all modules matching the repo id
Realm realm = Realm.getDefaultInstance();
RealmResults<ModuleListCache> modules = realm.where(ModuleListCache.class).equalTo("repoId", repoId).findAll();
realm.close();
return modules;
}
} }

@ -1,5 +1,6 @@
package com.fox2code.mmm.utils.realm; package com.fox2code.mmm.utils.realm;
import io.realm.Realm;
import io.realm.RealmObject; import io.realm.RealmObject;
import io.realm.annotations.Required; import io.realm.annotations.Required;
@ -15,6 +16,10 @@ public class ReposList extends RealmObject {
private boolean enabled; private boolean enabled;
private String donate; private String donate;
private String support; private String support;
private String submitModule;
private int lastUpdate;
private String website;
private String name;
public ReposList(String id, String url, boolean enabled, String donate, String support) { public ReposList(String id, String url, boolean enabled, String donate, String support) {
this.id = id; this.id = id;
@ -22,6 +27,8 @@ public class ReposList extends RealmObject {
this.enabled = enabled; this.enabled = enabled;
this.donate = donate; this.donate = donate;
this.support = support; this.support = support;
this.submitModule = null;
this.lastUpdate = 0;
} }
public ReposList() { public ReposList() {
@ -66,4 +73,44 @@ public class ReposList extends RealmObject {
public void setSupport(String support) { public void setSupport(String support) {
this.support = support; this.support = support;
} }
// get metadata for a repo
public static ReposList getRepo(String id) {
Realm realm = Realm.getDefaultInstance();
ReposList repo = realm.where(ReposList.class).equalTo("id", id).findFirst();
realm.close();
return repo;
}
public String getSubmitModule() {
return submitModule;
}
public void setSubmitModule(String submitModule) {
this.submitModule = submitModule;
}
public int getLastUpdate() {
return lastUpdate;
}
public void setLastUpdate(int lastUpdate) {
this.lastUpdate = lastUpdate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
} }

Loading…
Cancel
Save