159 lines
5.8 KiB
Java
Raw Normal View History

2017-01-25 04:27:05 +08:00
package com.topjohnwu.magisk.module;
2016-09-29 01:42:25 +08:00
import android.app.Activity;
2016-09-29 01:42:25 +08:00
import android.content.Context;
import android.content.SharedPreferences;
2017-01-25 16:45:55 +08:00
import android.widget.Toast;
2016-09-29 01:42:25 +08:00
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
2017-02-07 02:01:32 +08:00
import com.topjohnwu.magisk.MagiskManager;
2017-01-25 13:16:50 +08:00
import com.topjohnwu.magisk.R;
2017-02-12 19:49:46 +08:00
import com.topjohnwu.magisk.asyncs.LoadRepos;
2017-01-25 04:27:05 +08:00
import com.topjohnwu.magisk.utils.Logger;
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.magisk.utils.ValueSortedMap;
import com.topjohnwu.magisk.utils.WebService;
2016-09-29 01:42:25 +08:00
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
2016-11-13 03:07:16 +08:00
import java.util.HashMap;
2016-09-29 01:42:25 +08:00
import java.util.Locale;
import java.util.Map;
2016-09-29 01:42:25 +08:00
public class ModuleHelper {
2017-01-25 04:27:05 +08:00
private static final String MAGISK_PATH = "/magisk";
2016-09-29 01:42:25 +08:00
2017-01-25 04:27:05 +08:00
private static final int GSON_DB_VER = 1;
private static final String ETAG_KEY = "ETag";
private static final String VERSION_KEY = "version";
private static final String REPO_KEY = "repomap";
private static final String FILE_KEY = "RepoMap";
2016-09-29 01:42:25 +08:00
2017-02-07 02:01:32 +08:00
public static void createModuleMap(MagiskManager magiskManager) {
2016-09-29 01:42:25 +08:00
Logger.dev("ModuleHelper: Loading modules");
2017-02-07 02:01:32 +08:00
magiskManager.moduleMap = new ValueSortedMap<>();
2016-09-29 01:42:25 +08:00
for (String path : Utils.getModList(MAGISK_PATH)) {
Logger.dev("ModuleHelper: Adding modules from " + path);
2016-11-09 00:46:26 +08:00
Module module;
try {
module = new Module(path);
2017-02-07 02:01:32 +08:00
magiskManager.moduleMap.put(module.getId(), module);
2016-11-09 00:46:26 +08:00
} catch (BaseModule.CacheModException ignored) {}
2016-09-29 01:42:25 +08:00
}
2017-01-25 04:27:05 +08:00
Logger.dev("ModuleHelper: Data load done");
2016-09-29 01:42:25 +08:00
}
2017-02-07 02:01:32 +08:00
public static void createRepoMap(MagiskManager magiskManager) {
2016-09-29 01:42:25 +08:00
Logger.dev("ModuleHelper: Loading repos");
2017-02-07 02:01:32 +08:00
SharedPreferences prefs = magiskManager.prefs;
2017-02-07 02:01:32 +08:00
magiskManager.repoMap = new ValueSortedMap<>();
2016-09-29 01:42:25 +08:00
Gson gson = new Gson();
2016-11-13 03:07:16 +08:00
String jsonString;
int cachedVersion = prefs.getInt(VERSION_KEY, 0);
2017-01-25 04:27:05 +08:00
if (cachedVersion != GSON_DB_VER) {
2016-11-13 03:07:16 +08:00
// Ignore incompatible cached database
jsonString = null;
} else {
jsonString = prefs.getString(REPO_KEY, null);
}
2016-09-29 01:42:25 +08:00
Map<String, Repo> cached = null;
2016-09-29 01:42:25 +08:00
if (jsonString != null) {
cached = gson.fromJson(jsonString, new TypeToken<ValueSortedMap<String, Repo>>(){}.getType());
2016-09-29 01:42:25 +08:00
}
if (cached == null) {
2016-11-13 03:07:16 +08:00
cached = new ValueSortedMap<>();
2016-09-29 01:42:25 +08:00
}
// Get cached ETag to add in the request header
String etag = prefs.getString(ETAG_KEY, "");
Map<String, String> header = new HashMap<>();
header.put("If-None-Match", etag);
2016-11-13 03:07:16 +08:00
// Making a request to main URL for repo info
jsonString = WebService.request(
2017-02-07 02:01:32 +08:00
magiskManager.getString(R.string.url_main), WebService.GET, null, header, false);
if (!jsonString.isEmpty()) {
2016-09-29 01:42:25 +08:00
try {
2016-11-13 03:07:16 +08:00
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
2016-09-29 01:42:25 +08:00
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);
2016-11-09 00:46:26 +08:00
try {
if (repo == null) {
Logger.dev("ModuleHelper: Create new repo " + id);
2017-02-07 02:01:32 +08:00
repo = new Repo(magiskManager, name, updatedDate);
2016-11-09 00:46:26 +08:00
} else {
Logger.dev("ModuleHelper: Update cached repo " + id);
2016-11-09 00:46:26 +08:00
repo.update(updatedDate);
}
if (repo.getId() != null) {
2017-02-07 02:01:32 +08:00
magiskManager.repoMap.put(id, repo);
2016-11-09 00:46:26 +08:00
}
} catch (BaseModule.CacheModException ignored) {}
2016-09-29 01:42:25 +08:00
}
} catch (JSONException e) {
e.printStackTrace();
}
2016-11-13 03:07:16 +08:00
} else {
// Use cached if no internet or no updates
Logger.dev("ModuleHelper: No updates, use cached");
2017-02-07 02:01:32 +08:00
magiskManager.repoMap.putAll(cached);
2016-09-29 01:42:25 +08:00
}
2016-11-13 03:07:16 +08:00
prefs.edit()
2017-01-25 04:27:05 +08:00
.putInt(VERSION_KEY, GSON_DB_VER)
2017-02-07 02:01:32 +08:00
.putString(REPO_KEY, gson.toJson(magiskManager.repoMap))
.putString(ETAG_KEY, etag)
2016-11-13 03:07:16 +08:00
.apply();
2016-09-29 01:42:25 +08:00
Logger.dev("ModuleHelper: Repo load done");
}
public static void clearRepoCache(Activity activity) {
MagiskManager magiskManager = Utils.getMagiskManager(activity);
SharedPreferences repoMap = activity.getSharedPreferences(FILE_KEY, Context.MODE_PRIVATE);
2017-01-25 16:45:55 +08:00
repoMap.edit()
.remove(ETAG_KEY)
.remove(VERSION_KEY)
.apply();
2017-02-07 02:01:32 +08:00
magiskManager.repoLoadDone.isTriggered = false;
2017-02-12 19:49:46 +08:00
new LoadRepos(activity).exec();
Toast.makeText(activity, R.string.repo_cache_cleared, Toast.LENGTH_SHORT).show();
2017-01-25 16:45:55 +08:00
}
2016-09-29 01:42:25 +08:00
}