Sort module/repo by name

This commit is contained in:
topjohnwu 2016-11-13 03:07:16 +08:00
parent f69facc842
commit 3e259021d0
3 changed files with 82 additions and 28 deletions

View File

@ -1,11 +1,13 @@
package com.topjohnwu.magisk.module; package com.topjohnwu.magisk.module;
import android.support.annotation.NonNull;
import com.topjohnwu.magisk.utils.Logger; import com.topjohnwu.magisk.utils.Logger;
import java.util.List; import java.util.List;
public abstract class BaseModule { public abstract class BaseModule implements Comparable<BaseModule> {
protected String mId, mName, mVersion, mAuthor, mDescription, mSupportUrl, mDonateUrl; protected String mId, mName, mVersion, mAuthor, mDescription, mSupportUrl, mDonateUrl;
protected boolean mIsCacheModule = false; protected boolean mIsCacheModule = false;
@ -98,4 +100,9 @@ public abstract class BaseModule {
Logger.dev("Cache mods are no longer supported! id: " + id); Logger.dev("Cache mods are no longer supported! id: " + id);
} }
} }
@Override
public int compareTo(@NonNull BaseModule o) {
return this.getName().toLowerCase().compareTo(o.getName().toLowerCase());
}
} }

View File

@ -9,20 +9,19 @@ import com.topjohnwu.magisk.utils.WebRequest;
import java.util.Date; import java.util.Date;
public class Repo extends BaseModule { public class Repo extends BaseModule {
protected String repoName, mLogUrl, mManifestUrl, mZipUrl; private String mLogUrl, mManifestUrl, mZipUrl;
protected Date mLastUpdate; private Date mLastUpdate;
public Repo(Context context, String name, Date lastUpdate) throws CacheModException { public Repo(Context context, String name, Date lastUpdate) throws CacheModException {
repoName = name;
mLastUpdate = lastUpdate; mLastUpdate = lastUpdate;
mLogUrl = context.getString(R.string.file_url, repoName, "changelog.txt"); mLogUrl = context.getString(R.string.file_url, name, "changelog.txt");
mManifestUrl = context.getString(R.string.file_url, repoName, "module.prop"); mManifestUrl = context.getString(R.string.file_url, name, "module.prop");
mZipUrl = context.getString(R.string.zip_url, repoName); mZipUrl = context.getString(R.string.zip_url, name);
update(); update();
} }
public void update() throws CacheModException { public void update() throws CacheModException {
Logger.dev("Repo: Re-fetch prop " + mId); Logger.dev("Repo: Re-fetch prop");
String props = WebRequest.makeWebServiceCall(mManifestUrl, WebRequest.GET, true); String props = WebRequest.makeWebServiceCall(mManifestUrl, WebRequest.GET, true);
String lines[] = props.split("\\n"); String lines[] = props.split("\\n");
parseProps(lines); parseProps(lines);

View File

@ -2,6 +2,7 @@ package com.topjohnwu.magisk.utils;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
@ -16,19 +17,24 @@ import org.json.JSONObject;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Comparator; import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.TreeMap; import java.util.Map;
public class ModuleHelper { public class ModuleHelper {
public static final String MAGISK_PATH = "/magisk"; private static final String MAGISK_PATH = "/magisk";
private static final String FILE_KEY = "RepoMap";
private static final String REPO_KEY = "repomap";
private static final String VERSION_KEY = "version";
private static final int DATABASE_VER = 1;
private static final String file_key = "RepoMap"; private static ValueSortedMap<String, Repo> repoMap = new ValueSortedMap<>();
private static final String key = "repomap"; private static ValueSortedMap<String, Module> moduleMap = new ValueSortedMap<>();
private static TreeMap<String, Repo> repoMap = new TreeMap<>(new ModuleComparator());
private static TreeMap<String, Module> moduleMap = new TreeMap<>(new ModuleComparator());
public static void createModuleMap() { public static void createModuleMap() {
@ -54,24 +60,34 @@ public class ModuleHelper {
repoMap.clear(); repoMap.clear();
Gson gson = new Gson(); Gson gson = new Gson();
SharedPreferences prefs = context.getSharedPreferences(file_key, Context.MODE_PRIVATE); SharedPreferences prefs = context.getSharedPreferences(FILE_KEY, Context.MODE_PRIVATE);
String jsonString = prefs.getString(key, null); String jsonString;
TreeMap<String, Repo> cached = null; int cachedVersion = prefs.getInt(VERSION_KEY, 0);
if (cachedVersion != DATABASE_VER) {
// Ignore incompatible cached database
jsonString = null;
} else {
jsonString = prefs.getString(REPO_KEY, null);
}
ValueSortedMap<String, Repo> cached = null;
if (jsonString != null) { if (jsonString != null) {
cached = gson.fromJson(jsonString, new TypeToken< TreeMap<String, Repo> >(){}.getType()); cached = gson.fromJson(jsonString, new TypeToken< ValueSortedMap<String, Repo> >(){}.getType());
} }
if (cached == null) { if (cached == null) {
cached = new TreeMap<>(new ModuleComparator()); cached = new ValueSortedMap<>();
} }
// Making a request to url and getting response // Making a request to url and getting response
String jsonStr = WebRequest.makeWebServiceCall(context.getString(R.string.url_main) + Utils.getToken(), WebRequest.GET); jsonString = WebRequest.makeWebServiceCall(context.getString(R.string.url_main) + Utils.getToken(), WebRequest.GET);
if (jsonStr != null && !jsonStr.isEmpty()) {
if (jsonString != null && !jsonString.isEmpty()) {
// Have internet access
try { try {
JSONArray jsonArray = new JSONArray(jsonStr); JSONArray jsonArray = new JSONArray(jsonString);
for (int i = 0; i < jsonArray.length(); i++) { for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonobject = jsonArray.getJSONObject(i); JSONObject jsonobject = jsonArray.getJSONObject(i);
String id = jsonobject.getString("description"); String id = jsonobject.getString("description");
@ -101,10 +117,16 @@ public class ModuleHelper {
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
} else {
prefs.edit().putString(key, gson.toJson(repoMap)).apply(); // Use cached if no internet
repoMap.putAll(cached);
} }
prefs.edit()
.putInt(VERSION_KEY, DATABASE_VER)
.putString(REPO_KEY, gson.toJson(repoMap))
.apply();
Logger.dev("ModuleHelper: Repo load done"); Logger.dev("ModuleHelper: Repo load done");
} }
@ -131,10 +153,36 @@ public class ModuleHelper {
} }
} }
public static class ModuleComparator implements Comparator<String> { private static class ValueSortedMap<K, V extends Comparable > extends HashMap<K, V> {
private List<V> sorted = new ArrayList<>();
@NonNull
@Override @Override
public int compare(String o1, String o2) { public Collection<V> values() {
return o1.toLowerCase().compareTo(o2.toLowerCase()); if (sorted.isEmpty()) {
sorted.addAll(super.values());
Collections.sort(sorted);
}
return sorted;
}
@Override
public V put(K key, V value) {
sorted.clear();
return super.put(key, value);
}
@Override
public void putAll(Map<? extends K, ? extends V> m) {
sorted.clear();
super.putAll(m);
}
@Override
public V remove(Object key) {
sorted.clear();
return super.remove(key);
} }
} }
} }