some bugfixes

Signed-off-by: androidacy-user <opensource@androidacy.com>
pull/285/head
androidacy-user 1 year ago
parent e6f9fbe1cb
commit 1f38a197de

@ -286,11 +286,21 @@ public final class AndroidacyRepoData extends RepoData {
@Override @Override
protected List<RepoModule> populate(JSONObject jsonObject) throws JSONException, NoSuchAlgorithmException { protected List<RepoModule> populate(JSONObject jsonObject) throws JSONException, NoSuchAlgorithmException {
Timber.d("AndroidacyRepoData populate start"); Timber.d("AndroidacyRepoData populate start");
if (!jsonObject.getString("status").equals("success"))
throw new JSONException("Response is not a success!");
String name = jsonObject.optString("name", "Androidacy Modules Repo"); String name = jsonObject.optString("name", "Androidacy Modules Repo");
String nameForModules = name.endsWith(" (Official)") ? name.substring(0, name.length() - 11) : name; String nameForModules = name.endsWith(" (Official)") ? name.substring(0, name.length() - 11) : name;
JSONArray jsonArray = jsonObject.getJSONArray("data"); JSONArray jsonArray;
try {
jsonArray = jsonObject.getJSONArray("data");
} catch (JSONException e) {
// probably using modules key since it's cached
try {
jsonArray = jsonObject.getJSONArray("modules");
} catch (JSONException e2) {
// we should never get here, bail out
Timber.e(e2, "Failed to parse modules");
return null;
}
}
for (RepoModule repoModule : this.moduleHashMap.values()) { for (RepoModule repoModule : this.moduleHashMap.values()) {
repoModule.processed = false; repoModule.processed = false;
} }
@ -299,9 +309,20 @@ public final class AndroidacyRepoData extends RepoData {
long lastLastUpdate = 0; long lastLastUpdate = 0;
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
jsonObject = jsonArray.getJSONObject(i); jsonObject = jsonArray.getJSONObject(i);
String moduleId = jsonObject.getString("codename"); String moduleId;
try {
moduleId = jsonObject.getString("codename");
} catch (JSONException e) {
Timber.e("Module %s has no codename or json %s is invalid", jsonObject.optString("codename", "Unknown"), jsonObject.toString());
continue;
}
// Normally, we'd validate the module id here, but we don't need to because the server does it for us // Normally, we'd validate the module id here, but we don't need to because the server does it for us
long lastUpdate = jsonObject.getLong("updated_at") * 1000; long lastUpdate;
try {
lastUpdate = jsonObject.getLong("updated_at") * 1000;
} catch (JSONException e) {
lastUpdate = jsonObject.getLong("lastUpdate") * 1000;
}
lastLastUpdate = Math.max(lastLastUpdate, lastUpdate); lastLastUpdate = Math.max(lastLastUpdate, lastUpdate);
RepoModule repoModule = this.moduleHashMap.get(moduleId); RepoModule repoModule = this.moduleHashMap.get(moduleId);
if (repoModule == null) { if (repoModule == null) {

@ -57,12 +57,14 @@ public class RepoUpdater {
this.toUpdate = Collections.emptyList(); this.toUpdate = Collections.emptyList();
this.toApply = new HashSet<>(); this.toApply = new HashSet<>();
for (ModuleListCache moduleListCache : results) { for (ModuleListCache moduleListCache : results) {
this.toApply.add(new RepoModule(this.repoData, moduleListCache.getId(), moduleListCache.getName(), moduleListCache.getDescription(), moduleListCache.getAuthor(), moduleListCache.getDonate(), moduleListCache.getConfig(), moduleListCache.getSupport(), moduleListCache.getVersion(), moduleListCache.getVersionCode())); this.toApply.add(new RepoModule(this.repoData, moduleListCache.getCodename(), moduleListCache.getName(), moduleListCache.getDescription(), moduleListCache.getAuthor(), moduleListCache.getDonate(), moduleListCache.getConfig(), moduleListCache.getSupport(), moduleListCache.getVersion(), moduleListCache.getVersionCode()));
} }
Timber.d("Fetched %d modules from cache for %s, from %s records", this.toApply.size(), this.repoData.id, results.size()); Timber.d("Fetched %d modules from cache for %s, from %s records", this.toApply.size(), this.repoData.id, results.size());
// apply the toApply list to the toUpdate list // apply the toApply list to the toUpdate list
try { try {
this.toUpdate = this.repoData.populate(new JSONObject(new String(results.asJSON().getBytes(), StandardCharsets.UTF_8))); JSONObject jsonObject = new JSONObject();
jsonObject.put("modules", new JSONArray(results.asJSON()));
this.toUpdate = this.repoData.populate(jsonObject);
} catch (Exception e) { } catch (Exception e) {
Timber.e(e); Timber.e(e);
} }
@ -251,6 +253,15 @@ public class RepoUpdater {
} else { } else {
mmtReborn = false; mmtReborn = false;
} }
// try to get updated_at or lastUpdate value for lastUpdate
int lastUpdate;
if (module.has("updated_at")) {
lastUpdate = module.getInt("updated_at");
} else if (module.has("lastUpdate")) {
lastUpdate = module.getInt("lastUpdate");
} else {
lastUpdate = 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
@ -296,6 +307,7 @@ public class RepoUpdater {
moduleListCache.setInstalled(installed); moduleListCache.setInstalled(installed);
moduleListCache.setInstalledVersionCode(installedVersionCode); moduleListCache.setInstalledVersionCode(installedVersionCode);
moduleListCache.setSafe(safe); moduleListCache.setSafe(safe);
moduleListCache.setLastUpdate(lastUpdate);
realm.copyToRealmOrUpdate(moduleListCache); realm.copyToRealmOrUpdate(moduleListCache);
realm.commitTransaction(); realm.commitTransaction();
} catch ( } catch (

@ -1,5 +1,6 @@
package com.fox2code.mmm.utils.realm; package com.fox2code.mmm.utils.realm;
import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -15,7 +16,7 @@ public class ModuleListCache extends RealmObject {
// for compatibility, only id is required // for compatibility, only id is required
@PrimaryKey @PrimaryKey
@Required @Required
private String id; private String codename;
private String name; private String name;
private String version; private String version;
private int versionCode; private int versionCode;
@ -37,8 +38,8 @@ public class ModuleListCache extends RealmObject {
// androidacy specific, may be added by other repos // androidacy specific, may be added by other repos
private boolean safe; private boolean safe;
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) { 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) {
this.id = id; this.codename = codename;
this.name = name; this.name = name;
this.version = version; this.version = version;
this.versionCode = versionCode; this.versionCode = versionCode;
@ -70,7 +71,7 @@ public class ModuleListCache extends RealmObject {
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
for (ModuleListCache module : modules) { for (ModuleListCache module : modules) {
try { try {
jsonObject.put(module.getId(), module.toJson()); jsonObject.put(module.getCodename(), module.toJson());
} catch ( } catch (
JSONException e) { JSONException e) {
Timber.e(e); Timber.e(e);
@ -216,12 +217,12 @@ public class ModuleListCache extends RealmObject {
this.installedVersionCode = installedVersionCode; this.installedVersionCode = installedVersionCode;
} }
public String getId() { public String getCodename() {
return id; return codename;
} }
public void setId(String id) { public void setCodename(String codename) {
this.id = id; this.codename = codename;
} }
public int getLastUpdate() { public int getLastUpdate() {
@ -273,4 +274,27 @@ public class ModuleListCache extends RealmObject {
realm.close(); realm.close();
return modules; return modules;
} }
// same as above but returns a json object
public JSONObject getModulesAsJson(String repoId) {
Realm realm = Realm.getDefaultInstance();
RealmResults<ModuleListCache> modules = realm.where(ModuleListCache.class).equalTo("repoId", repoId).findAll();
JSONObject jsonObject = new JSONObject();
// everything goes under top level "modules" key
try {
jsonObject.put("modules", new JSONArray());
} catch (JSONException ignored) {
// we should never get here
}
for (ModuleListCache module : modules) {
try {
jsonObject.getJSONArray("modules").put(module.toJson());
} catch (
JSONException e) {
Timber.e(e);
}
}
realm.close();
return jsonObject;
}
} }

Loading…
Cancel
Save