|
|
|
@ -1,16 +1,11 @@
|
|
|
|
|
package com.fox2code.mmm.repo;
|
|
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint;
|
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
|
|
|
|
|
import com.fox2code.mmm.MainApplication;
|
|
|
|
|
import com.fox2code.mmm.R;
|
|
|
|
|
import com.fox2code.mmm.manager.ModuleInfo;
|
|
|
|
|
import com.fox2code.mmm.utils.Files;
|
|
|
|
|
import com.fox2code.mmm.utils.Http;
|
|
|
|
|
import com.fox2code.mmm.utils.PropUtils;
|
|
|
|
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
@ -20,15 +15,10 @@ import org.json.JSONObject;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.text.ParseException;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
|
|
|
|
|
public class RepoData {
|
|
|
|
|
private static final String TAG = "RepoData";
|
|
|
|
@ -38,59 +28,24 @@ public class RepoData {
|
|
|
|
|
public final File cacheRoot;
|
|
|
|
|
public final SharedPreferences cachedPreferences;
|
|
|
|
|
public final File metaDataCache;
|
|
|
|
|
public final boolean special;
|
|
|
|
|
public final HashMap<String, RepoModule> moduleHashMap;
|
|
|
|
|
public long lastUpdate;
|
|
|
|
|
public String name;
|
|
|
|
|
private boolean enabled; // Cache for speed
|
|
|
|
|
private final Map<String, SpecialData> specialData;
|
|
|
|
|
private long specialLastUpdate;
|
|
|
|
|
|
|
|
|
|
protected RepoData(String url, File cacheRoot, SharedPreferences cachedPreferences) {
|
|
|
|
|
this(url, cacheRoot, cachedPreferences, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RepoData(String url, File cacheRoot, SharedPreferences cachedPreferences,boolean special) {
|
|
|
|
|
this.url = url;
|
|
|
|
|
this.id = RepoManager.internalIdOfUrl(url);
|
|
|
|
|
this.cacheRoot = cacheRoot;
|
|
|
|
|
this.cachedPreferences = cachedPreferences;
|
|
|
|
|
this.metaDataCache = new File(cacheRoot, "modules.json");
|
|
|
|
|
this.special = special;
|
|
|
|
|
this.moduleHashMap = new HashMap<>();
|
|
|
|
|
this.name = this.url; // Set url as default name
|
|
|
|
|
this.enabled = MainApplication.getSharedPreferences()
|
|
|
|
|
.getBoolean("pref_" + this.id + "_enabled", true);
|
|
|
|
|
this.specialData = special ? new HashMap<>() : Collections.emptyMap();
|
|
|
|
|
if (!this.cacheRoot.isDirectory()) {
|
|
|
|
|
this.cacheRoot.mkdirs();
|
|
|
|
|
} else {
|
|
|
|
|
if (special) { // Special times need to be loaded before populate
|
|
|
|
|
File metaDataCacheSpecial = new File(cacheRoot, "modules_times.json");
|
|
|
|
|
if (metaDataCacheSpecial.exists()) {
|
|
|
|
|
try {
|
|
|
|
|
JSONArray jsonArray = new JSONArray(new String(
|
|
|
|
|
Files.read(this.metaDataCache), StandardCharsets.UTF_8));
|
|
|
|
|
for (int i = 0; i < jsonArray.length(); i++) {
|
|
|
|
|
JSONObject jsonObject = jsonArray.getJSONObject(i);
|
|
|
|
|
this.specialData.put(
|
|
|
|
|
jsonObject.getString("name"), new SpecialData(
|
|
|
|
|
Objects.requireNonNull(ISO_OFFSET_DATE_TIME.parse(
|
|
|
|
|
jsonObject.getString(
|
|
|
|
|
"pushed_at"))).getTime(),
|
|
|
|
|
jsonObject.optInt("stargazers_count")));
|
|
|
|
|
Log.d(TAG, "Got " +
|
|
|
|
|
jsonObject.getString("name") + " from local storage!");
|
|
|
|
|
}
|
|
|
|
|
this.specialLastUpdate = metaDataCacheSpecial.lastModified();
|
|
|
|
|
if (this.specialLastUpdate > System.currentTimeMillis()) {
|
|
|
|
|
this.specialLastUpdate = 0; // Don't allow time travel
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
metaDataCacheSpecial.delete();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (this.metaDataCache.exists()) {
|
|
|
|
|
this.lastUpdate = metaDataCache.lastModified();
|
|
|
|
|
if (this.lastUpdate > System.currentTimeMillis()) {
|
|
|
|
@ -132,19 +87,12 @@ public class RepoData {
|
|
|
|
|
String moduleId = module.getString("id");
|
|
|
|
|
// Deny remote modules ids shorter than 3 chars long or that start with a digit
|
|
|
|
|
if (moduleId.length() < 3 || Character.isDigit(moduleId.charAt(0))) continue;
|
|
|
|
|
SpecialData moduleSpecialData = this.specialData.get(moduleId);
|
|
|
|
|
long moduleLastUpdate = module.getLong("last_update");
|
|
|
|
|
String moduleNotesUrl = module.getString("notes_url");
|
|
|
|
|
String modulePropsUrl = module.getString("prop_url");
|
|
|
|
|
String moduleZipUrl = module.getString("zip_url");
|
|
|
|
|
String moduleChecksum = module.optString("checksum");
|
|
|
|
|
String moduleStars = module.optString("stars");
|
|
|
|
|
if (moduleSpecialData != null) { // Fix last update time
|
|
|
|
|
moduleLastUpdate = Math.max(moduleLastUpdate, moduleSpecialData.time);
|
|
|
|
|
moduleNotesUrl = Http.updateLink(moduleNotesUrl);
|
|
|
|
|
modulePropsUrl = Http.updateLink(modulePropsUrl);
|
|
|
|
|
moduleZipUrl = Http.updateLink(moduleZipUrl);
|
|
|
|
|
}
|
|
|
|
|
RepoModule repoModule = this.moduleHashMap.get(moduleId);
|
|
|
|
|
if (repoModule == null) {
|
|
|
|
|
repoModule = new RepoModule(this, moduleId);
|
|
|
|
@ -163,10 +111,7 @@ public class RepoData {
|
|
|
|
|
repoModule.propUrl = modulePropsUrl;
|
|
|
|
|
repoModule.zipUrl = moduleZipUrl;
|
|
|
|
|
repoModule.checksum = moduleChecksum;
|
|
|
|
|
if (moduleSpecialData != null) {
|
|
|
|
|
repoModule.qualityValue = moduleSpecialData.stars;
|
|
|
|
|
repoModule.qualityText = R.string.module_stars;
|
|
|
|
|
} else if (!moduleStars.isEmpty()) {
|
|
|
|
|
if (!moduleStars.isEmpty()) {
|
|
|
|
|
try {
|
|
|
|
|
repoModule.qualityValue = Integer.parseInt(moduleStars);
|
|
|
|
|
repoModule.qualityText = R.string.module_stars;
|
|
|
|
@ -204,12 +149,6 @@ public class RepoData {
|
|
|
|
|
if (moduleInfo.version == null) {
|
|
|
|
|
moduleInfo.version = "v" + moduleInfo.versionCode;
|
|
|
|
|
}
|
|
|
|
|
SpecialData moduleSpecialData =
|
|
|
|
|
this.specialData.get(repoModule.id);
|
|
|
|
|
if (moduleSpecialData != null) {
|
|
|
|
|
repoModule.qualityValue = moduleSpecialData.stars;
|
|
|
|
|
repoModule.qualityText = R.string.module_stars;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
|
file.delete();
|
|
|
|
@ -219,41 +158,6 @@ public class RepoData {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@SuppressLint("SimpleDateFormat")
|
|
|
|
|
private static final SimpleDateFormat ISO_OFFSET_DATE_TIME =
|
|
|
|
|
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
|
|
|
|
|
|
|
|
|
|
public void updateSpecialTimes(boolean force) throws IOException, JSONException {
|
|
|
|
|
if (!this.special) return;
|
|
|
|
|
synchronized (this.populateLock) {
|
|
|
|
|
if (this.specialLastUpdate == 0L || (force && (this.specialData.isEmpty() ||
|
|
|
|
|
this.specialLastUpdate < System.currentTimeMillis() - 60000L))) {
|
|
|
|
|
File metaDataCacheSpecial = new File(cacheRoot, "modules_times.json");
|
|
|
|
|
this.specialData.clear();
|
|
|
|
|
try {
|
|
|
|
|
// Requesting only 32 most recently pushed repos
|
|
|
|
|
byte[] data = Http.doHttpGet(
|
|
|
|
|
"https://api.github.com/users/Magisk-Modules-Repo/" +
|
|
|
|
|
"repos?sort=pushed&per_page=32", false);
|
|
|
|
|
JSONArray jsonArray = new JSONArray(new String(data, StandardCharsets.UTF_8));
|
|
|
|
|
for (int i = 0;i < jsonArray.length();i++) {
|
|
|
|
|
JSONObject jsonObject = jsonArray.optJSONObject(i);
|
|
|
|
|
this.specialData.put(
|
|
|
|
|
jsonObject.getString("name"), new SpecialData(
|
|
|
|
|
Objects.requireNonNull(ISO_OFFSET_DATE_TIME.parse(
|
|
|
|
|
jsonObject.getString(
|
|
|
|
|
"pushed_at"))).getTime(),
|
|
|
|
|
jsonObject.optInt("stargazers_count")));
|
|
|
|
|
}
|
|
|
|
|
Files.write(metaDataCacheSpecial, data);
|
|
|
|
|
this.specialLastUpdate = System.currentTimeMillis();
|
|
|
|
|
} catch (ParseException e) {
|
|
|
|
|
throw new IOException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getNameOrFallback(String fallback) {
|
|
|
|
|
return this.name == null ||
|
|
|
|
|
this.name.equals(this.url) ?
|
|
|
|
@ -274,22 +178,4 @@ public class RepoData {
|
|
|
|
|
this.enabled = MainApplication.getSharedPreferences()
|
|
|
|
|
.getBoolean("pref_" + this.id + "_enabled", true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static class SpecialData {
|
|
|
|
|
SpecialData(long time, int stars) {
|
|
|
|
|
this.time = time; this.stars = stars;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final long time;
|
|
|
|
|
final int stars;
|
|
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
|
@Override
|
|
|
|
|
public String toString() {
|
|
|
|
|
return "SpecialData{" +
|
|
|
|
|
"time=" + time +
|
|
|
|
|
", stars=" + stars +
|
|
|
|
|
'}';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|