Skip to content

Commit

Permalink
Simplify repo update logic
Browse files Browse the repository at this point in the history
  • Loading branch information
topjohnwu committed Aug 1, 2018
1 parent 27e0d16 commit 6acb950
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 49 deletions.
2 changes: 1 addition & 1 deletion app/src/full/java/com/topjohnwu/magisk/Const.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static class Url {
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 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 ZIP_URL = "https://github.com/Magisk-Modules-Repo/%s/archive/master.zip";
public static final String DONATION_URL = "https://www.paypal.me/topjohnwu";
Expand Down
67 changes: 19 additions & 48 deletions app/src/full/java/com/topjohnwu/magisk/asyncs/UpdateRepos.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.database.Cursor;
import android.os.AsyncTask;
import android.text.TextUtils;

import com.topjohnwu.magisk.Const;
import com.topjohnwu.magisk.Data;
Expand All @@ -22,12 +21,9 @@
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
Expand All @@ -38,10 +34,6 @@

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 CORE_POOL_SIZE = Math.max(2, CPU_COUNT - 1);
private static final DateFormat dateFormat;
Expand All @@ -52,14 +44,11 @@ public class UpdateRepos {
}

private MagiskManager mm;
private List<String> etags, newEtags;
private Set<String> cached;
private ExecutorService threadPool;

public UpdateRepos() {
mm = Data.MM();
threadPool = Executors.newFixedThreadPool(CORE_POOL_SIZE);
newEtags = new LinkedList<>();
}

private void waitTasks() {
Expand Down Expand Up @@ -104,51 +93,37 @@ private boolean loadJSON(String jsonString) throws JSONException, ParseException
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<>();
if (mode == CHECK_ETAG && page < etags.size())
header.put(Const.Key.IF_NONE_MATCH, etags.get(page));
if (page == 0)
header.put(Const.Key.IF_NONE_MATCH, mm.prefs.getString(Const.Key.ETAG_KEY, ""));
String url = Utils.fmt(Const.Url.REPO_URL, page + 1);

try {
HttpURLConnection conn = WebService.request(url, header);
if (conn.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
// Current page is not updated, check the next page
return loadPage(page + 1, CHECK_ETAG);
}
// No updates
if (conn.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED)
return false;
// Current page is the last page
if (!loadJSON(WebService.getString(conn)))
return mode != CHECK_ETAG;
return true;
} catch (Exception e) {
e.printStackTrace();
// Should not happen, but if exception occurs, page load fails
return false;
}

/* If one page is updated, we force update all pages */

// Update ETAG
String etag = header.get(Const.Key.ETAG_KEY);
etag = etag.substring(etag.indexOf('\"'), etag.lastIndexOf('\"') + 1);
if (mode == LOAD_PREV) {
// We are loading a previous page, push the new tag to the front
newEtags.add(0, etag);
} else {
newEtags.add(etag);
if (page == 0) {
String etag = header.get(Const.Key.ETAG_KEY);
etag = etag.substring(etag.indexOf('\"'), etag.lastIndexOf('\"') + 1);
mm.prefs.edit().putString(Const.Key.ETAG_KEY, etag).apply();
}

String links = header.get(Const.Key.LINK_KEY);
if (links != null) {
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;
return links == null || !links.contains("next") || loadPage(page + 1);
}

private void fullReload() {
Expand All @@ -171,17 +146,13 @@ private void fullReload() {
public void exec(boolean force) {
Topic.reset(Topic.REPO_LOAD_DONE);
AsyncTask.THREAD_POOL_EXECUTOR.execute(() -> {
etags = Arrays.asList(mm.prefs.getString(Const.Key.ETAG_KEY, "").split(","));
cached = mm.repoDB.getRepoIDSet();
threadPool = Executors.newFixedThreadPool(CORE_POOL_SIZE);

if (loadPage(0, CHECK_ETAG)) {
if (loadPage(0)) {
waitTasks();

// The leftover cached means they are removed from online repo
mm.repoDB.removeRepo(cached);

// Update ETag
mm.prefs.edit().putString(Const.Key.ETAG_KEY, TextUtils.join(",", newEtags)).apply();
} else if (force) {
fullReload();
}
Expand Down

0 comments on commit 6acb950

Please sign in to comment.