From 817f050bcd03901cebd62e20fa86b8be86f16283 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 30 Mar 2017 06:52:18 +0800 Subject: [PATCH] Say goodbye to old modules --- .../topjohnwu/magisk/asyncs/LoadRepos.java | 25 ++++++++----------- .../magisk/database/RepoDatabaseHelper.java | 16 ++++++++++-- .../topjohnwu/magisk/module/BaseModule.java | 11 +++++++- .../com/topjohnwu/magisk/module/Repo.java | 1 + 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/asyncs/LoadRepos.java b/app/src/main/java/com/topjohnwu/magisk/asyncs/LoadRepos.java index bd7e2e574..d15721269 100644 --- a/app/src/main/java/com/topjohnwu/magisk/asyncs/LoadRepos.java +++ b/app/src/main/java/com/topjohnwu/magisk/asyncs/LoadRepos.java @@ -57,12 +57,10 @@ public class LoadRepos extends ParallelTask { String etag = prefs.getString(ETAG_KEY, ""); header.put("If-None-Match", etag); - magiskManager.repoMap = new ValueSortedMap<>(); - // Make a request to main URL for repo info String jsonString = WebService.request(REPO_URL, WebService.GET, null, header, false); - ValueSortedMap cached = dbHelper.getRepoMap(); + ValueSortedMap cached = dbHelper.getRepoMap(false), fetched = new ValueSortedMap<>(); if (!TextUtils.isEmpty(jsonString)) { try { @@ -97,27 +95,24 @@ public class LoadRepos extends ParallelTask { repo.update(updatedDate); } if (repo.getId() != null) { - magiskManager.repoMap.put(id, repo); + fetched.put(id, repo); } } catch (BaseModule.CacheModException ignored) {} + + // Update the database + dbHelper.addRepoMap(fetched); + // The leftover cached are those removed remote, cleanup db + dbHelper.removeRepo(cached); + // Update ETag + prefs.edit().putString(ETAG_KEY, etag).apply(); } } catch (JSONException e) { e.printStackTrace(); } - } else { - // Use cached if no internet or no updates - Logger.dev("LoadRepos: No updates, use cached"); - magiskManager.repoMap.putAll(cached); - cached.clear(); } - // Update the database - dbHelper.addRepoMap(magiskManager.repoMap); - // The leftover cached are those removed remote, cleanup db - dbHelper.removeRepo(cached); - // Update ETag - prefs.edit().putString(ETAG_KEY, etag).apply(); + magiskManager.repoMap = dbHelper.getRepoMap(); Logger.dev("LoadRepos: Repo load done"); return null; diff --git a/app/src/main/java/com/topjohnwu/magisk/database/RepoDatabaseHelper.java b/app/src/main/java/com/topjohnwu/magisk/database/RepoDatabaseHelper.java index 625d42a68..d30d88be6 100644 --- a/app/src/main/java/com/topjohnwu/magisk/database/RepoDatabaseHelper.java +++ b/app/src/main/java/com/topjohnwu/magisk/database/RepoDatabaseHelper.java @@ -13,7 +13,7 @@ import java.util.Collection; public class RepoDatabaseHelper extends SQLiteOpenHelper { - private static final int DATABASE_VER = 1; + private static final int DATABASE_VER = 2; private static final String TABLE_NAME = "repos"; public RepoDatabaseHelper(Context context) { @@ -33,8 +33,12 @@ public class RepoDatabaseHelper extends SQLiteOpenHelper { "(id TEXT, name TEXT, version TEXT, versionCode INT, " + "author TEXT, description TEXT, repo_name TEXT, last_update INT, " + "PRIMARY KEY(id))"); + oldVersion++; + } + if (oldVersion == 1) { + db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD template INT"); + oldVersion++; } - // No upgrades yet } public void addRepoMap(ValueSortedMap map) { @@ -62,6 +66,10 @@ public class RepoDatabaseHelper extends SQLiteOpenHelper { } public ValueSortedMap getRepoMap() { + return getRepoMap(true); + } + + public ValueSortedMap getRepoMap(boolean filtered) { ValueSortedMap ret = new ValueSortedMap<>(); SQLiteDatabase db = getReadableDatabase(); Repo repo; @@ -69,6 +77,10 @@ public class RepoDatabaseHelper extends SQLiteOpenHelper { while (c.moveToNext()) { repo = new Repo(c); Logger.dev("Load from cache: " + repo.getId()); + if (repo.getTemplateVersion() < 3 && filtered) { + Logger.dev("Outdated repo: " + repo.getId()); + continue; + } ret.put(repo.getId(), repo); } } diff --git a/app/src/main/java/com/topjohnwu/magisk/module/BaseModule.java b/app/src/main/java/com/topjohnwu/magisk/module/BaseModule.java index 7b3216697..3463fbb04 100644 --- a/app/src/main/java/com/topjohnwu/magisk/module/BaseModule.java +++ b/app/src/main/java/com/topjohnwu/magisk/module/BaseModule.java @@ -11,7 +11,7 @@ import java.util.List; public abstract class BaseModule implements Comparable { private String mId, mName, mVersion, mAuthor, mDescription; - private int mVersionCode = 0; + private int mVersionCode = 0, templateVersion = 0; protected BaseModule() {} @@ -22,6 +22,7 @@ public abstract class BaseModule implements Comparable { mVersionCode = c.getInt(c.getColumnIndex("versionCode")); mAuthor = c.getString(c.getColumnIndex("author")); mDescription = c.getString(c.getColumnIndex("description")); + templateVersion = c.getInt(c.getColumnIndex("template")); } protected void parseProps(List props) throws CacheModException { parseProps(props.toArray(new String[props.size()])); } @@ -57,6 +58,10 @@ public abstract class BaseModule implements Comparable { case "description": mDescription = prop[1]; break; + case "template": + try { + templateVersion = Integer.parseInt(prop[1]); + } catch (NumberFormatException ignored) {} case "cacheModule": if (Boolean.parseBoolean(prop[1])) throw new CacheModException(mId); @@ -99,6 +104,10 @@ public abstract class BaseModule implements Comparable { return mVersionCode; } + public int getTemplateVersion() { + return templateVersion; + } + public static class CacheModException extends Exception { public CacheModException(String id) { Logger.error("Cache mods are no longer supported! id: " + id); diff --git a/app/src/main/java/com/topjohnwu/magisk/module/Repo.java b/app/src/main/java/com/topjohnwu/magisk/module/Repo.java index d758b7984..48ec896f1 100644 --- a/app/src/main/java/com/topjohnwu/magisk/module/Repo.java +++ b/app/src/main/java/com/topjohnwu/magisk/module/Repo.java @@ -53,6 +53,7 @@ public class Repo extends BaseModule { values.put("description", getDescription()); values.put("repo_name", repoName); values.put("last_update", mLastUpdate.getTime()); + values.put("template", getTemplateVersion()); return values; }