From bef43617362239ca671351df7ee70b18f5055eda Mon Sep 17 00:00:00 2001 From: d8ahazard Date: Fri, 2 Sep 2016 14:50:54 -0500 Subject: [PATCH] That's important too... --- .../topjohnwu/magisk/module/RepoAdapter.java | 83 +++++++++++-------- 1 file changed, 50 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/module/RepoAdapter.java b/app/src/main/java/com/topjohnwu/magisk/module/RepoAdapter.java index 97ebf2810..a18d77b13 100644 --- a/app/src/main/java/com/topjohnwu/magisk/module/RepoAdapter.java +++ b/app/src/main/java/com/topjohnwu/magisk/module/RepoAdapter.java @@ -1,12 +1,11 @@ package com.topjohnwu.magisk.module; -import android.app.ListActivity; import android.content.Context; import android.content.SharedPreferences; import android.os.AsyncTask; -import android.os.Bundle; import android.preference.PreferenceManager; import android.util.Log; +import android.widget.Toast; import com.topjohnwu.magisk.utils.WebRequest; @@ -14,9 +13,10 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; -import java.net.MalformedURLException; -import java.net.URL; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.Map; @@ -27,42 +27,48 @@ public class RepoAdapter { private static final String TAG_ID = "id"; private static final String TAG_NAME = "name"; private Context activityContext; + private Date updatedDate, currentDate; - - public List listRepos(Context context) { + public List listRepos(Context context, boolean refresh) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - if (!prefs.contains("hasCachedRepos")) { + if (!prefs.contains("hasCachedRepos") | refresh) { activityContext = context; - MyAsyncTask asynchTask = new MyAsyncTask(); - asynchTask.execute(); + new MyAsyncTask().execute(); List out = null; } else { Log.d("Magisk", "Building from cache"); - Map map = prefs.getAll(); - for (Map.Entry entry : map.entrySet()) - { + Map map = prefs.getAll(); + repos.clear(); + for (Map.Entry entry : map.entrySet()) { if (entry.getKey().contains("module_")) { - String repoString = entry.getValue().toString(); + String repoString = entry.getValue().toString().replace(""", "\""); JSONArray repoArray = null; try { repoArray = new JSONArray(repoString); - repos.clear(); - for (int f = 0; f < repoArray.length(); f++) { - JSONObject jsonobject = repoArray.getJSONObject(f); - String name = jsonobject.getString("name"); - String moduleName, moduleDescription, zipUrl; - moduleName = jsonobject.getString("moduleName"); - moduleDescription = jsonobject.getString("moduleDescription"); - zipUrl = jsonobject.getString("zipUrl"); - repos.add(new Repo(moduleName,moduleDescription,zipUrl)); - } + for (int f = 0; f < repoArray.length(); f++) { + JSONObject jsonobject = repoArray.getJSONObject(f); + String name = entry.getKey().replace("module_", ""); + name = name.replace(" ", ""); + String moduleName, moduleDescription, zipUrl; + moduleName = jsonobject.getString("moduleName"); + moduleDescription = jsonobject.getString("moduleDescription"); + zipUrl = jsonobject.getString("zipUrl"); + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); + try { + updatedDate = format.parse(jsonobject.getString("lastUpdate")); + } catch (ParseException e) { + e.printStackTrace(); + } + repos.add(new Repo(name, moduleDescription, zipUrl, updatedDate, activityContext)); + + } } catch (JSONException e) { e.printStackTrace(); } } - System.out.println(entry.getKey() + "/" + entry.getValue()); + } } @@ -72,26 +78,31 @@ public class RepoAdapter { } - class MyAsyncTask extends AsyncTask { @Override protected void onPreExecute() { super.onPreExecute(); + } + + @Override + protected void onProgressUpdate(String... values) { + super.onProgressUpdate(values); + Toast.makeText(activityContext, "Refreshing online modules", Toast.LENGTH_SHORT).show(); } @Override protected Void doInBackground(String... params) { - Log.d("Magisk", "doInBackground running"); + publishProgress(); // Creating service handler class instance WebRequest webreq = new WebRequest(); // Making a request to url and getting response String jsonStr = webreq.makeWebServiceCall(url, WebRequest.GET); + Log.d("Magisk", "doInBackground Running, String: " + jsonStr + " Url: " + url); - Log.d("Response: ", "> " + jsonStr); try { repos.clear(); @@ -100,11 +111,18 @@ public class RepoAdapter { JSONObject jsonobject = jsonArray.getJSONObject(i); String name = jsonobject.getString("name"); String url = jsonobject.getString("url"); - if (!name.contains("Repo.github.io")) { - repos.add(new Repo(name, url, activityContext)); - } - for (int f = 0; f < repos.size(); f++) { - repos.get(f).fetch(); + String lastUpdate = jsonobject.getString("updated_at"); + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); + try { + updatedDate = format.parse(lastUpdate); + + } catch (ParseException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + if (!name.contains("Repo.github.io")) { + repos.add(new Repo(name, url, updatedDate, activityContext)); } } } catch (JSONException e) { @@ -126,5 +144,4 @@ public class RepoAdapter { } - }