Simplify repo update logic

This commit is contained in:
topjohnwu 2018-08-02 01:52:03 +08:00
parent 27e0d1641a
commit 6acb950990
2 changed files with 20 additions and 49 deletions

View File

@ -77,7 +77,7 @@ public class Const {
public static final String STABLE_URL = "https://raw.githubusercontent.com/topjohnwu/magisk_files/master/stable.json"; public static final String STABLE_URL = "https://raw.githubusercontent.com/topjohnwu/magisk_files/master/stable.json";
public static final String BETA_URL = "https://raw.githubusercontent.com/topjohnwu/magisk_files/master/beta.json"; public static final String BETA_URL = "https://raw.githubusercontent.com/topjohnwu/magisk_files/master/beta.json";
public static final String SNET_URL = "https://github.com/topjohnwu/magisk_files/raw/a300521162587da23e45010797bfd8c9a03594f6/snet.apk"; public static final String SNET_URL = "https://github.com/topjohnwu/magisk_files/raw/a300521162587da23e45010797bfd8c9a03594f6/snet.apk";
public static final String REPO_URL = "https://api.github.com/users/Magisk-Modules-Repo/repos?per_page=100&page=%d"; public static final String REPO_URL = "https://api.github.com/users/Magisk-Modules-Repo/repos?per_page=100&sort=pushed&page=%d";
public static final String FILE_URL = "https://raw.githubusercontent.com/Magisk-Modules-Repo/%s/master/%s"; public static final String FILE_URL = "https://raw.githubusercontent.com/Magisk-Modules-Repo/%s/master/%s";
public static final String ZIP_URL = "https://github.com/Magisk-Modules-Repo/%s/archive/master.zip"; public static final String ZIP_URL = "https://github.com/Magisk-Modules-Repo/%s/archive/master.zip";
public static final String DONATION_URL = "https://www.paypal.me/topjohnwu"; public static final String DONATION_URL = "https://www.paypal.me/topjohnwu";

View File

@ -2,7 +2,6 @@ package com.topjohnwu.magisk.asyncs;
import android.database.Cursor; import android.database.Cursor;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.text.TextUtils;
import com.topjohnwu.magisk.Const; import com.topjohnwu.magisk.Const;
import com.topjohnwu.magisk.Data; import com.topjohnwu.magisk.Data;
@ -22,12 +21,9 @@ import java.net.HttpURLConnection;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -38,10 +34,6 @@ import java.util.concurrent.TimeUnit;
public class UpdateRepos { public class UpdateRepos {
private static final int CHECK_ETAG = 0;
private static final int LOAD_NEXT = 1;
private static final int LOAD_PREV = 2;
private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors(); private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
private static final int CORE_POOL_SIZE = Math.max(2, CPU_COUNT - 1); private static final int CORE_POOL_SIZE = Math.max(2, CPU_COUNT - 1);
private static final DateFormat dateFormat; private static final DateFormat dateFormat;
@ -52,14 +44,11 @@ public class UpdateRepos {
} }
private MagiskManager mm; private MagiskManager mm;
private List<String> etags, newEtags;
private Set<String> cached; private Set<String> cached;
private ExecutorService threadPool; private ExecutorService threadPool;
public UpdateRepos() { public UpdateRepos() {
mm = Data.MM(); mm = Data.MM();
threadPool = Executors.newFixedThreadPool(CORE_POOL_SIZE);
newEtags = new LinkedList<>();
} }
private void waitTasks() { private void waitTasks() {
@ -104,51 +93,37 @@ public class UpdateRepos {
return true; return true;
} }
private boolean loadPage(int page, int mode) { /* We sort repos by last push, which means that we only need to check whether the
* first page is updated to determine whether the online repo database is changed
*/
private boolean loadPage(int page) {
Map<String, String> header = new HashMap<>(); Map<String, String> header = new HashMap<>();
if (mode == CHECK_ETAG && page < etags.size()) if (page == 0)
header.put(Const.Key.IF_NONE_MATCH, etags.get(page)); header.put(Const.Key.IF_NONE_MATCH, mm.prefs.getString(Const.Key.ETAG_KEY, ""));
String url = Utils.fmt(Const.Url.REPO_URL, page + 1); String url = Utils.fmt(Const.Url.REPO_URL, page + 1);
try { try {
HttpURLConnection conn = WebService.request(url, header); HttpURLConnection conn = WebService.request(url, header);
if (conn.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) { // No updates
// Current page is not updated, check the next page if (conn.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED)
return loadPage(page + 1, CHECK_ETAG); return false;
} // Current page is the last page
if (!loadJSON(WebService.getString(conn))) if (!loadJSON(WebService.getString(conn)))
return mode != CHECK_ETAG; return true;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); // Should not happen, but if exception occurs, page load fails
return false; return false;
} }
/* If one page is updated, we force update all pages */
// Update ETAG // Update ETAG
String etag = header.get(Const.Key.ETAG_KEY); if (page == 0) {
etag = etag.substring(etag.indexOf('\"'), etag.lastIndexOf('\"') + 1); String etag = header.get(Const.Key.ETAG_KEY);
if (mode == LOAD_PREV) { etag = etag.substring(etag.indexOf('\"'), etag.lastIndexOf('\"') + 1);
// We are loading a previous page, push the new tag to the front mm.prefs.edit().putString(Const.Key.ETAG_KEY, etag).apply();
newEtags.add(0, etag);
} else {
newEtags.add(etag);
} }
String links = header.get(Const.Key.LINK_KEY); String links = header.get(Const.Key.LINK_KEY);
if (links != null) { return links == null || !links.contains("next") || loadPage(page + 1);
for (String s : links.split(", ")) {
if (mode != LOAD_PREV && s.contains("next")) {
// Force load all next pages
loadPage(page + 1, LOAD_NEXT);
}
if (mode != LOAD_NEXT && s.contains("prev")) {
// Back propagation
loadPage(page - 1, LOAD_PREV);
}
}
}
return true;
} }
private void fullReload() { private void fullReload() {
@ -171,17 +146,13 @@ public class UpdateRepos {
public void exec(boolean force) { public void exec(boolean force) {
Topic.reset(Topic.REPO_LOAD_DONE); Topic.reset(Topic.REPO_LOAD_DONE);
AsyncTask.THREAD_POOL_EXECUTOR.execute(() -> { AsyncTask.THREAD_POOL_EXECUTOR.execute(() -> {
etags = Arrays.asList(mm.prefs.getString(Const.Key.ETAG_KEY, "").split(","));
cached = mm.repoDB.getRepoIDSet(); cached = mm.repoDB.getRepoIDSet();
threadPool = Executors.newFixedThreadPool(CORE_POOL_SIZE);
if (loadPage(0, CHECK_ETAG)) { if (loadPage(0)) {
waitTasks(); waitTasks();
// The leftover cached means they are removed from online repo // The leftover cached means they are removed from online repo
mm.repoDB.removeRepo(cached); mm.repoDB.removeRepo(cached);
// Update ETag
mm.prefs.edit().putString(Const.Key.ETAG_KEY, TextUtils.join(",", newEtags)).apply();
} else if (force) { } else if (force) {
fullReload(); fullReload();
} }