diff --git a/app/src/main/java/com/fox2code/mmm/androidacy/AndroidacyRepoData.java b/app/src/main/java/com/fox2code/mmm/androidacy/AndroidacyRepoData.java index 87601e0..bb5c1b4 100644 --- a/app/src/main/java/com/fox2code/mmm/androidacy/AndroidacyRepoData.java +++ b/app/src/main/java/com/fox2code/mmm/androidacy/AndroidacyRepoData.java @@ -286,11 +286,21 @@ public final class AndroidacyRepoData extends RepoData { @Override protected List populate(JSONObject jsonObject) throws JSONException, NoSuchAlgorithmException { 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 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()) { repoModule.processed = false; } @@ -299,9 +309,20 @@ public final class AndroidacyRepoData extends RepoData { long lastLastUpdate = 0; for (int i = 0; i < len; 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 - 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); RepoModule repoModule = this.moduleHashMap.get(moduleId); if (repoModule == null) { 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 5b05f01..8a2d4f5 100644 --- a/app/src/main/java/com/fox2code/mmm/repo/RepoUpdater.java +++ b/app/src/main/java/com/fox2code/mmm/repo/RepoUpdater.java @@ -57,12 +57,14 @@ public class RepoUpdater { this.toUpdate = Collections.emptyList(); this.toApply = new HashSet<>(); 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()); // apply the toApply list to the toUpdate list 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) { Timber.e(e); } @@ -251,6 +253,15 @@ public class RepoUpdater { } else { 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 String repoId = this.repoData.id; // get module installed @@ -296,6 +307,7 @@ public class RepoUpdater { moduleListCache.setInstalled(installed); moduleListCache.setInstalledVersionCode(installedVersionCode); moduleListCache.setSafe(safe); + moduleListCache.setLastUpdate(lastUpdate); realm.copyToRealmOrUpdate(moduleListCache); realm.commitTransaction(); } catch ( diff --git a/app/src/main/java/com/fox2code/mmm/utils/realm/ModuleListCache.java b/app/src/main/java/com/fox2code/mmm/utils/realm/ModuleListCache.java index 4d4075f..579aa2f 100644 --- a/app/src/main/java/com/fox2code/mmm/utils/realm/ModuleListCache.java +++ b/app/src/main/java/com/fox2code/mmm/utils/realm/ModuleListCache.java @@ -1,5 +1,6 @@ package com.fox2code.mmm.utils.realm; +import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -15,7 +16,7 @@ public class ModuleListCache extends RealmObject { // for compatibility, only id is required @PrimaryKey @Required - private String id; + private String codename; private String name; private String version; private int versionCode; @@ -37,8 +38,8 @@ public class ModuleListCache extends RealmObject { // androidacy specific, may be added by other repos 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) { - this.id = id; + 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.codename = codename; this.name = name; this.version = version; this.versionCode = versionCode; @@ -70,7 +71,7 @@ public class ModuleListCache extends RealmObject { JSONObject jsonObject = new JSONObject(); for (ModuleListCache module : modules) { try { - jsonObject.put(module.getId(), module.toJson()); + jsonObject.put(module.getCodename(), module.toJson()); } catch ( JSONException e) { Timber.e(e); @@ -216,12 +217,12 @@ public class ModuleListCache extends RealmObject { this.installedVersionCode = installedVersionCode; } - public String getId() { - return id; + public String getCodename() { + return codename; } - public void setId(String id) { - this.id = id; + public void setCodename(String codename) { + this.codename = codename; } public int getLastUpdate() { @@ -273,4 +274,27 @@ public class ModuleListCache extends RealmObject { realm.close(); return modules; } + + // same as above but returns a json object + public JSONObject getModulesAsJson(String repoId) { + Realm realm = Realm.getDefaultInstance(); + RealmResults 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; + } }