Massive repo refactor

This commit is contained in:
topjohnwu
2016-09-29 01:42:25 +08:00
parent 0acc5e33b3
commit bb7ff27d04
17 changed files with 445 additions and 429 deletions
@@ -10,15 +10,11 @@ import android.os.Environment;
import android.preference.PreferenceManager;
import android.provider.OpenableColumns;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
import com.topjohnwu.magisk.ModulesFragment;
import com.topjohnwu.magisk.MagiskFragment;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.ReposFragment;
import com.topjohnwu.magisk.module.Module;
import com.topjohnwu.magisk.receivers.DownloadReceiver;
import org.json.JSONException;
import org.json.JSONObject;
@@ -30,11 +26,12 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collections;
import java.util.List;
public class Async {
public static final String UPDATE_JSON = "https://raw.githubusercontent.com/topjohnwu/MagiskManager/updates/magisk_update.json";
public static class constructEnv extends AsyncTask<Void, Void, Void> {
Context mContext;
@@ -77,21 +74,21 @@ public class Async {
@Override
protected Void doInBackground(Void... voids) {
String jsonStr = WebRequest.makeWebServiceCall(Utils.UPDATE_JSON, WebRequest.GET);
String jsonStr = WebRequest.makeWebServiceCall(UPDATE_JSON, WebRequest.GET);
try {
JSONObject json = new JSONObject(jsonStr);
JSONObject magisk = json.getJSONObject("magisk");
JSONObject app = json.getJSONObject("app");
Utils.remoteMagiskVersion = magisk.getInt("versionCode");
Utils.magiskLink = magisk.getString("link");
Utils.magiskChangelog = magisk.getString("changelog");
MagiskFragment.remoteMagiskVersion = magisk.getInt("versionCode");
MagiskFragment.magiskLink = magisk.getString("link");
MagiskFragment.magiskChangelog = magisk.getString("changelog");
Utils.remoteAppVersion = app.getString("version");
Utils.remoteAppVersionCode = app.getInt("versionCode");
Utils.appLink = app.getString("link");
Utils.appChangelog = app.getString("changelog");
MagiskFragment.remoteAppVersion = app.getString("version");
MagiskFragment.remoteAppVersionCode = app.getInt("versionCode");
MagiskFragment.appLink = app.getString("link");
MagiskFragment.appChangelog = app.getString("changelog");
} catch (JSONException ignored) {
Logger.dev("JSON error!");
@@ -116,28 +113,7 @@ public class Async {
@Override
protected Void doInBackground(Void... voids) {
ModulesFragment.listModules.clear();
Logger.dev("Loading modules");
List<String> magisk = Utils.getModList(Utils.MAGISK_PATH);
List<String> magiskCache = Utils.getModList(Utils.MAGISK_CACHE_PATH);
for (String mod : magisk) {
Logger.dev("Adding modules from " + mod);
ModulesFragment.listModules.add(new Module(mod));
}
for (String mod : magiskCache) {
Logger.dev("Adding cache modules from " + mod);
Module cacheMod = new Module(mod);
// Prevent people forgot to change module.prop
cacheMod.setCache();
ModulesFragment.listModules.add(cacheMod);
}
Collections.sort(ModulesFragment.listModules, new Utils.ModuleComparator());
Logger.dev("Module load done");
ModuleHelper.createModuleMap();
return null;
}
@@ -158,10 +134,7 @@ public class Async {
@Override
protected Void doInBackground(Void... voids) {
ReposFragment.mListRepos.clear();
RepoHelper.createRepoMap(mContext);
RepoHelper.checkUpdate();
ReposFragment.mListRepos = RepoHelper.getSortedList();
ModuleHelper.createRepoMap(mContext);
return null;
}
@@ -0,0 +1,141 @@
package com.topjohnwu.magisk.utils;
import android.content.Context;
import android.content.SharedPreferences;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.module.Module;
import com.topjohnwu.magisk.module.Repo;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TreeMap;
public class ModuleHelper {
public static final String MAGISK_PATH = "/magisk";
public static final String MAGISK_CACHE_PATH = "/cache/magisk";
private static final String file_key = "RepoMap";
private static final String key = "repomap";
private static TreeMap<String, Repo> repoMap = new TreeMap<>(new ModuleComparator());
private static TreeMap<String, Module> moduleMap = new TreeMap<>(new ModuleComparator());
public static void createModuleMap() {
Logger.dev("ModuleHelper: Loading modules");
moduleMap.clear();
for (String path : Utils.getModList(MAGISK_PATH)) {
Logger.dev("ModuleHelper: Adding modules from " + path);
Module module = new Module(path);
moduleMap.put(module.getId(), module);
}
for (String path : Utils.getModList(MAGISK_CACHE_PATH)) {
Logger.dev("ModuleHelper: Adding cache modules from " + path);
Module cacheMod = new Module(path);
// Force set to cache
cacheMod.setCache();
moduleMap.put(cacheMod.getId(), cacheMod);
}
Logger.dev("ModuleHelper: Module load done");
}
public static void createRepoMap(Context context) {
Logger.dev("ModuleHelper: Loading repos");
repoMap.clear();
Gson gson = new Gson();
SharedPreferences prefs = context.getSharedPreferences(file_key, Context.MODE_PRIVATE);
String jsonString = prefs.getString(key, null);
TreeMap<String, Repo> cached = null;
if (jsonString != null) {
cached = gson.fromJson(jsonString, new TypeToken< TreeMap<String, Repo> >(){}.getType());
}
if (cached == null) {
cached = new TreeMap<>(new ModuleComparator());
}
// Making a request to url and getting response
String jsonStr = WebRequest.makeWebServiceCall(context.getString(R.string.url_main) + Utils.getToken(), WebRequest.GET);
if (jsonStr != null && !jsonStr.isEmpty()) {
try {
JSONArray jsonArray = new JSONArray(jsonStr);
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);
if (repo == null) {
Logger.dev("ModuleHelper: Create new repo " + id);
repoMap.put(id, new Repo(context, name, updatedDate));
} else {
Logger.dev("ModuleHelper: Cached repo " + id);
repo.update(updatedDate);
repoMap.put(id, repo);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
prefs.edit().putString(key, gson.toJson(repoMap)).apply();
}
Logger.dev("ModuleHelper: Repo load done");
}
public static void getModuleList(List<Module> moduleList) {
moduleList.clear();
moduleList.addAll(moduleMap.values());
}
public static void getRepoLists(List<Repo> update, List<Repo> installed, List<Repo> others) {
update.clear();
installed.clear();
others.clear();
for (Repo repo : repoMap.values()) {
Module module = moduleMap.get(repo.getId());
if (module != null) {
if (repo.getVersionCode() > module.getVersionCode()) {
update.add(repo);
} else {
installed.add(repo);
}
} else {
others.add(repo);
}
}
}
public static class ModuleComparator implements Comparator<String> {
@Override
public int compare(String o1, String o2) {
return o1.toLowerCase().compareTo(o2.toLowerCase());
}
}
}
@@ -1,96 +0,0 @@
package com.topjohnwu.magisk.utils;
import android.content.Context;
import android.content.SharedPreferences;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.topjohnwu.magisk.ModulesFragment;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.module.Module;
import com.topjohnwu.magisk.module.Repo;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
public class RepoHelper {
private static List<Repo> repos = new ArrayList<>();
private static String TAG = "Magisk";
private static final String file_key = "RepoMap";
private static final String key = "repomap";
public static HashMap<String, Repo> repoMap;
public static void createRepoMap(Context context) {
Gson gson = new Gson();
SharedPreferences prefs = context.getSharedPreferences(file_key, Context.MODE_PRIVATE);
String jsonString = prefs.getString(key, null);
if (jsonString != null) {
repoMap = gson.fromJson(jsonString, new TypeToken< HashMap<String, Repo> >(){}.getType());
}
if (repoMap == null) {
repoMap = new HashMap<>();
}
// Making a request to url and getting response
String jsonStr = WebRequest.makeWebServiceCall(context.getString(R.string.url_main) + Utils.getToken(), WebRequest.GET);
if (jsonStr != null && !jsonStr.isEmpty()) {
try {
JSONArray jsonArray = new JSONArray(jsonStr);
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("updated_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 = repoMap.get(id);
if (repo == null) {
repoMap.put(id, new Repo(context, name, updatedDate));
} else {
repo.update(updatedDate);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
jsonString = gson.toJson(repoMap);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(key, jsonString);
editor.apply();
}
}
public static void checkUpdate() {
for (Module module : ModulesFragment.listModules) {
module.checkUpdate();
}
}
public static List<Repo> getSortedList() {
ArrayList<Repo> list = new ArrayList<>(repoMap.values());
Collections.sort(list, new Utils.ModuleComparator());
return list;
}
public interface TaskDelegate {
void taskCompletionResult(String result);
}
}
@@ -21,6 +21,7 @@ import android.view.View;
import android.widget.Toast;
import com.kcoppock.broadcasttilesupport.BroadcastTileIntentBuilder;
import com.topjohnwu.magisk.MagiskFragment;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.module.BaseModule;
import com.topjohnwu.magisk.receivers.DownloadReceiver;
@@ -49,23 +50,17 @@ import javax.crypto.spec.DESKeySpec;
public class Utils {
public static int magiskVersion, remoteMagiskVersion = -1, remoteAppVersionCode = -1;
public static String magiskLink, magiskChangelog, appLink, appChangelog, remoteAppVersion;
private static final String TAG = "Magisk";
public static final String MAGISK_PATH = "/magisk";
public static final String MAGISK_CACHE_PATH = "/cache/magisk";
public static final String UPDATE_JSON = "https://raw.githubusercontent.com/topjohnwu/MagiskManager/updates/magisk_update.json";
private static final String cryptoPass = "MagiskRox666";
private static final String secret = "GTYybRBTYf5his9kQ16ZNO7qgkBJ/5MyVe4CGceAOIoXgSnnk8FTd4F1dE9p5Eus";
public static void init(Context context) {
List<String> ret = Shell.sh("getprop magisk.version");
if (ret.get(0).isEmpty()) {
magiskVersion = -1;
MagiskFragment.magiskVersion = -1;
} else {
magiskVersion = Integer.parseInt(ret.get(0));
MagiskFragment.magiskVersion = Integer.parseInt(ret.get(0));
}
String toolPath = context.getApplicationInfo().dataDir + "/busybox";
Shell.su("PATH=$PATH:" + toolPath);
@@ -111,7 +106,7 @@ public class Utils {
}
public static void toggleRoot(Boolean b, Context context) {
if (Utils.magiskVersion != -1) {
if (MagiskFragment.magiskVersion != -1) {
if (b) {
Shell.su("ln -s $(getprop magisk.supath) /magisk/.core/bin", "setprop magisk.root 1");
} else {
@@ -126,7 +121,7 @@ public class Utils {
public static void toggleAutoRoot(Boolean b, Context context) {
Logger.dev("Utils: toggleAutocalled for " + b );
if (Utils.magiskVersion != -1) {
if (MagiskFragment.magiskVersion != -1) {
if (!Utils.hasServicePermission(context)) {
Intent intent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
Toast.makeText(context, "Please enable Magisk in accessibility for auto-toggle work.", Toast.LENGTH_LONG).show();
@@ -412,10 +407,4 @@ public class Utils {
}
public static class ModuleComparator implements Comparator<BaseModule> {
@Override
public int compare(BaseModule o1, BaseModule o2) {
return o1.getName().compareToIgnoreCase(o2.getName());
}
}
}
@@ -21,6 +21,8 @@ public WebWindow(String title, String url, Context context) {
}
alert.setTitle(title);
Logger.dev("WebView: URL = " + url);
WebView wv = new WebView(context);
wv.loadUrl(url);
wv.setWebViewClient(new WebViewClient() {