11package com .fox2code .mmm .repo ;
22
3- import android .annotation .SuppressLint ;
43import android .content .SharedPreferences ;
5- import android .util .Log ;
6-
7- import androidx .annotation .NonNull ;
84
95import com .fox2code .mmm .MainApplication ;
106import com .fox2code .mmm .R ;
117import com .fox2code .mmm .manager .ModuleInfo ;
128import com .fox2code .mmm .utils .Files ;
13- import com .fox2code .mmm .utils .Http ;
149import com .fox2code .mmm .utils .PropUtils ;
1510
1611import org .json .JSONArray ;
2015import java .io .File ;
2116import java .io .IOException ;
2217import java .nio .charset .StandardCharsets ;
23- import java .text .ParseException ;
24- import java .text .SimpleDateFormat ;
2518import java .util .ArrayList ;
26- import java .util .Collections ;
2719import java .util .HashMap ;
2820import java .util .Iterator ;
2921import java .util .List ;
30- import java .util .Map ;
31- import java .util .Objects ;
3222
3323public class RepoData {
3424 private static final String TAG = "RepoData" ;
@@ -38,59 +28,24 @@ public class RepoData {
3828 public final File cacheRoot ;
3929 public final SharedPreferences cachedPreferences ;
4030 public final File metaDataCache ;
41- public final boolean special ;
4231 public final HashMap <String , RepoModule > moduleHashMap ;
4332 public long lastUpdate ;
4433 public String name ;
4534 private boolean enabled ; // Cache for speed
46- private final Map <String , SpecialData > specialData ;
47- private long specialLastUpdate ;
4835
4936 protected RepoData (String url , File cacheRoot , SharedPreferences cachedPreferences ) {
50- this (url , cacheRoot , cachedPreferences , false );
51- }
52-
53- RepoData (String url , File cacheRoot , SharedPreferences cachedPreferences ,boolean special ) {
5437 this .url = url ;
5538 this .id = RepoManager .internalIdOfUrl (url );
5639 this .cacheRoot = cacheRoot ;
5740 this .cachedPreferences = cachedPreferences ;
5841 this .metaDataCache = new File (cacheRoot , "modules.json" );
59- this .special = special ;
6042 this .moduleHashMap = new HashMap <>();
6143 this .name = this .url ; // Set url as default name
6244 this .enabled = MainApplication .getSharedPreferences ()
6345 .getBoolean ("pref_" + this .id + "_enabled" , true );
64- this .specialData = special ? new HashMap <>() : Collections .emptyMap ();
6546 if (!this .cacheRoot .isDirectory ()) {
6647 this .cacheRoot .mkdirs ();
6748 } else {
68- if (special ) { // Special times need to be loaded before populate
69- File metaDataCacheSpecial = new File (cacheRoot , "modules_times.json" );
70- if (metaDataCacheSpecial .exists ()) {
71- try {
72- JSONArray jsonArray = new JSONArray (new String (
73- Files .read (this .metaDataCache ), StandardCharsets .UTF_8 ));
74- for (int i = 0 ; i < jsonArray .length (); i ++) {
75- JSONObject jsonObject = jsonArray .getJSONObject (i );
76- this .specialData .put (
77- jsonObject .getString ("name" ), new SpecialData (
78- Objects .requireNonNull (ISO_OFFSET_DATE_TIME .parse (
79- jsonObject .getString (
80- "pushed_at" ))).getTime (),
81- jsonObject .optInt ("stargazers_count" )));
82- Log .d (TAG , "Got " +
83- jsonObject .getString ("name" ) + " from local storage!" );
84- }
85- this .specialLastUpdate = metaDataCacheSpecial .lastModified ();
86- if (this .specialLastUpdate > System .currentTimeMillis ()) {
87- this .specialLastUpdate = 0 ; // Don't allow time travel
88- }
89- } catch (Exception e ) {
90- metaDataCacheSpecial .delete ();
91- }
92- }
93- }
9449 if (this .metaDataCache .exists ()) {
9550 this .lastUpdate = metaDataCache .lastModified ();
9651 if (this .lastUpdate > System .currentTimeMillis ()) {
@@ -132,19 +87,12 @@ protected List<RepoModule> populate(JSONObject jsonObject) throws JSONException
13287 String moduleId = module .getString ("id" );
13388 // Deny remote modules ids shorter than 3 chars long or that start with a digit
13489 if (moduleId .length () < 3 || Character .isDigit (moduleId .charAt (0 ))) continue ;
135- SpecialData moduleSpecialData = this .specialData .get (moduleId );
13690 long moduleLastUpdate = module .getLong ("last_update" );
13791 String moduleNotesUrl = module .getString ("notes_url" );
13892 String modulePropsUrl = module .getString ("prop_url" );
13993 String moduleZipUrl = module .getString ("zip_url" );
14094 String moduleChecksum = module .optString ("checksum" );
14195 String moduleStars = module .optString ("stars" );
142- if (moduleSpecialData != null ) { // Fix last update time
143- moduleLastUpdate = Math .max (moduleLastUpdate , moduleSpecialData .time );
144- moduleNotesUrl = Http .updateLink (moduleNotesUrl );
145- modulePropsUrl = Http .updateLink (modulePropsUrl );
146- moduleZipUrl = Http .updateLink (moduleZipUrl );
147- }
14896 RepoModule repoModule = this .moduleHashMap .get (moduleId );
14997 if (repoModule == null ) {
15098 repoModule = new RepoModule (this , moduleId );
@@ -163,10 +111,7 @@ protected List<RepoModule> populate(JSONObject jsonObject) throws JSONException
163111 repoModule .propUrl = modulePropsUrl ;
164112 repoModule .zipUrl = moduleZipUrl ;
165113 repoModule .checksum = moduleChecksum ;
166- if (moduleSpecialData != null ) {
167- repoModule .qualityValue = moduleSpecialData .stars ;
168- repoModule .qualityText = R .string .module_stars ;
169- } else if (!moduleStars .isEmpty ()) {
114+ if (!moduleStars .isEmpty ()) {
170115 try {
171116 repoModule .qualityValue = Integer .parseInt (moduleStars );
172117 repoModule .qualityText = R .string .module_stars ;
@@ -204,12 +149,6 @@ public boolean tryLoadMetadata(RepoModule repoModule) {
204149 if (moduleInfo .version == null ) {
205150 moduleInfo .version = "v" + moduleInfo .versionCode ;
206151 }
207- SpecialData moduleSpecialData =
208- this .specialData .get (repoModule .id );
209- if (moduleSpecialData != null ) {
210- repoModule .qualityValue = moduleSpecialData .stars ;
211- repoModule .qualityText = R .string .module_stars ;
212- }
213152 return true ;
214153 } catch (Exception ignored ) {
215154 file .delete ();
@@ -219,41 +158,6 @@ public boolean tryLoadMetadata(RepoModule repoModule) {
219158 return false ;
220159 }
221160
222- @ SuppressLint ("SimpleDateFormat" )
223- private static final SimpleDateFormat ISO_OFFSET_DATE_TIME =
224- new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss'Z'" );
225-
226- public void updateSpecialTimes (boolean force ) throws IOException , JSONException {
227- if (!this .special ) return ;
228- synchronized (this .populateLock ) {
229- if (this .specialLastUpdate == 0L || (force && (this .specialData .isEmpty () ||
230- this .specialLastUpdate < System .currentTimeMillis () - 60000L ))) {
231- File metaDataCacheSpecial = new File (cacheRoot , "modules_times.json" );
232- this .specialData .clear ();
233- try {
234- // Requesting only 32 most recently pushed repos
235- byte [] data = Http .doHttpGet (
236- "https://api.github.com/users/Magisk-Modules-Repo/" +
237- "repos?sort=pushed&per_page=32" , false );
238- JSONArray jsonArray = new JSONArray (new String (data , StandardCharsets .UTF_8 ));
239- for (int i = 0 ;i < jsonArray .length ();i ++) {
240- JSONObject jsonObject = jsonArray .optJSONObject (i );
241- this .specialData .put (
242- jsonObject .getString ("name" ), new SpecialData (
243- Objects .requireNonNull (ISO_OFFSET_DATE_TIME .parse (
244- jsonObject .getString (
245- "pushed_at" ))).getTime (),
246- jsonObject .optInt ("stargazers_count" )));
247- }
248- Files .write (metaDataCacheSpecial , data );
249- this .specialLastUpdate = System .currentTimeMillis ();
250- } catch (ParseException e ) {
251- throw new IOException (e );
252- }
253- }
254- }
255- }
256-
257161 public String getNameOrFallback (String fallback ) {
258162 return this .name == null ||
259163 this .name .equals (this .url ) ?
@@ -274,22 +178,4 @@ public void updateEnabledState() {
274178 this .enabled = MainApplication .getSharedPreferences ()
275179 .getBoolean ("pref_" + this .id + "_enabled" , true );
276180 }
277-
278- private static class SpecialData {
279- SpecialData (long time , int stars ) {
280- this .time = time ; this .stars = stars ;
281- }
282-
283- final long time ;
284- final int stars ;
285-
286- @ NonNull
287- @ Override
288- public String toString () {
289- return "SpecialData{" +
290- "time=" + time +
291- ", stars=" + stars +
292- '}' ;
293- }
294- }
295181}
0 commit comments