2017-02-12 19:49:46 +08:00
|
|
|
package com.topjohnwu.magisk.asyncs;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
2017-02-12 20:53:41 +08:00
|
|
|
import android.content.SharedPreferences;
|
2017-02-12 23:26:30 +08:00
|
|
|
import android.text.TextUtils;
|
2017-02-12 19:49:46 +08:00
|
|
|
|
2017-02-12 23:26:30 +08:00
|
|
|
import com.topjohnwu.magisk.database.RepoDatabaseHelper;
|
2017-02-12 20:53:41 +08:00
|
|
|
import com.topjohnwu.magisk.module.BaseModule;
|
|
|
|
import com.topjohnwu.magisk.module.Repo;
|
|
|
|
import com.topjohnwu.magisk.utils.Logger;
|
|
|
|
import com.topjohnwu.magisk.utils.ValueSortedMap;
|
|
|
|
import com.topjohnwu.magisk.utils.WebService;
|
|
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2017-02-12 23:26:30 +08:00
|
|
|
import java.io.File;
|
2017-02-12 20:53:41 +08:00
|
|
|
import java.text.ParseException;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.Map;
|
2017-02-12 19:49:46 +08:00
|
|
|
|
|
|
|
public class LoadRepos extends ParallelTask<Void, Void, Void> {
|
|
|
|
|
2017-02-12 20:53:41 +08:00
|
|
|
public static final String ETAG_KEY = "ETag";
|
2017-02-12 23:26:30 +08:00
|
|
|
|
|
|
|
private static final String REPO_URL = "https://api.github.com/orgs/Magisk-Modules-Repo/repos";
|
|
|
|
|
|
|
|
private String prefsPath;
|
2017-02-12 20:53:41 +08:00
|
|
|
|
2017-02-12 19:49:46 +08:00
|
|
|
public LoadRepos(Activity context) {
|
|
|
|
super(context);
|
2017-02-12 23:26:30 +08:00
|
|
|
prefsPath = context.getApplicationInfo().dataDir + "/shared_prefs";
|
2017-02-12 19:49:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Void doInBackground(Void... voids) {
|
2017-02-12 20:53:41 +08:00
|
|
|
Logger.dev("LoadRepos: Loading repos");
|
|
|
|
|
|
|
|
SharedPreferences prefs = magiskManager.prefs;
|
|
|
|
|
2017-02-12 23:26:30 +08:00
|
|
|
// Legacy data cleanup
|
|
|
|
new File(prefsPath, "RepoMap.xml").delete();
|
|
|
|
prefs.edit().remove("version").remove("repomap").apply();
|
2017-02-12 20:53:41 +08:00
|
|
|
|
2017-02-12 23:26:30 +08:00
|
|
|
Map<String, String> header = new HashMap<>();
|
|
|
|
// Get cached ETag to add in the request header
|
|
|
|
String etag = prefs.getString(ETAG_KEY, "");
|
2017-02-12 20:53:41 +08:00
|
|
|
|
2017-02-12 23:26:30 +08:00
|
|
|
// Add header only if db exists
|
|
|
|
if (magiskManager.getDatabasePath("repo.db").exists())
|
|
|
|
header.put("If-None-Match", etag);
|
2017-02-12 20:53:41 +08:00
|
|
|
|
2017-02-12 23:26:30 +08:00
|
|
|
magiskManager.repoMap = new ValueSortedMap<>();
|
2017-02-12 20:53:41 +08:00
|
|
|
|
2017-02-12 23:26:30 +08:00
|
|
|
// Make a request to main URL for repo info
|
|
|
|
String jsonString = WebService.request(REPO_URL, WebService.GET, null, header, false);
|
2017-02-12 20:53:41 +08:00
|
|
|
|
2017-02-12 23:26:30 +08:00
|
|
|
RepoDatabaseHelper dbHelper = new RepoDatabaseHelper(magiskManager);
|
|
|
|
ValueSortedMap<String, Repo> cached = dbHelper.getRepoMap();
|
2017-02-12 20:53:41 +08:00
|
|
|
|
2017-02-12 23:26:30 +08:00
|
|
|
if (!TextUtils.isEmpty(jsonString)) {
|
2017-02-12 20:53:41 +08:00
|
|
|
try {
|
|
|
|
JSONArray jsonArray = new JSONArray(jsonString);
|
|
|
|
// If it gets to this point, the response is valid, update ETag
|
|
|
|
etag = WebService.getLastResponseHeader().get(ETAG_KEY).get(0);
|
|
|
|
// Maybe bug in Android build tools, sometimes the ETag has crap in it...
|
|
|
|
etag = etag.substring(etag.indexOf('\"'), etag.lastIndexOf('\"') + 1);
|
|
|
|
|
|
|
|
// Update repo info
|
|
|
|
for (int i = 0; i < jsonArray.length(); i++) {
|
|
|
|
JSONObject jsonobject = jsonArray.getJSONObject(i);
|
|
|
|
String id = jsonobject.getString("description");
|
|
|
|
String name = jsonobject.getString("name");
|
|
|
|
String lastUpdate = jsonobject.getString("pushed_at");
|
|
|
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
|
|
|
|
Date updatedDate;
|
|
|
|
try {
|
|
|
|
updatedDate = format.parse(lastUpdate);
|
|
|
|
} catch (ParseException e) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Repo repo = cached.get(id);
|
|
|
|
try {
|
|
|
|
if (repo == null) {
|
|
|
|
Logger.dev("LoadRepos: Create new repo " + id);
|
2017-02-12 23:26:30 +08:00
|
|
|
repo = new Repo(name, updatedDate);
|
2017-02-12 20:53:41 +08:00
|
|
|
} else {
|
2017-02-12 23:26:30 +08:00
|
|
|
// Popout from cached
|
|
|
|
cached.remove(id);
|
2017-02-12 20:53:41 +08:00
|
|
|
Logger.dev("LoadRepos: Update cached repo " + id);
|
|
|
|
repo.update(updatedDate);
|
|
|
|
}
|
2017-02-12 23:26:30 +08:00
|
|
|
if (repo.getId() != null)
|
2017-02-12 20:53:41 +08:00
|
|
|
magiskManager.repoMap.put(id, repo);
|
|
|
|
} catch (BaseModule.CacheModException ignored) {}
|
|
|
|
}
|
2017-02-12 23:26:30 +08:00
|
|
|
|
2017-02-12 20:53:41 +08:00
|
|
|
} 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);
|
2017-02-12 23:26:30 +08:00
|
|
|
cached.clear();
|
2017-02-12 20:53:41 +08:00
|
|
|
}
|
|
|
|
|
2017-02-12 23:26:30 +08:00
|
|
|
// 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();
|
2017-02-12 20:53:41 +08:00
|
|
|
|
|
|
|
Logger.dev("LoadRepos: Repo load done");
|
2017-02-12 19:49:46 +08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Void v) {
|
|
|
|
magiskManager.repoLoadDone.trigger();
|
|
|
|
}
|
|
|
|
}
|