That's important too...

This commit is contained in:
d8ahazard 2016-09-02 14:50:54 -05:00
parent aa991b62f4
commit bef4361736

View File

@ -1,12 +1,11 @@
package com.topjohnwu.magisk.module; package com.topjohnwu.magisk.module;
import android.app.ListActivity;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import android.widget.Toast;
import com.topjohnwu.magisk.utils.WebRequest; import com.topjohnwu.magisk.utils.WebRequest;
@ -14,9 +13,10 @@ import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.net.MalformedURLException; import java.text.ParseException;
import java.net.URL; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -27,42 +27,48 @@ public class RepoAdapter {
private static final String TAG_ID = "id"; private static final String TAG_ID = "id";
private static final String TAG_NAME = "name"; private static final String TAG_NAME = "name";
private Context activityContext; private Context activityContext;
private Date updatedDate, currentDate;
public List<Repo> listRepos(Context context, boolean refresh) {
public List<Repo> listRepos(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
if (!prefs.contains("hasCachedRepos")) { if (!prefs.contains("hasCachedRepos") | refresh) {
activityContext = context; activityContext = context;
MyAsyncTask asynchTask = new MyAsyncTask(); new MyAsyncTask().execute();
asynchTask.execute();
List<String> out = null; List<String> out = null;
} else { } else {
Log.d("Magisk", "Building from cache"); Log.d("Magisk", "Building from cache");
Map<String,?> map = prefs.getAll(); Map<String, ?> map = prefs.getAll();
for (Map.Entry<String,?> entry : map.entrySet()) repos.clear();
{ for (Map.Entry<String, ?> entry : map.entrySet()) {
if (entry.getKey().contains("module_")) { if (entry.getKey().contains("module_")) {
String repoString = entry.getValue().toString(); String repoString = entry.getValue().toString().replace("&quot;", "\"");
JSONArray repoArray = null; JSONArray repoArray = null;
try { try {
repoArray = new JSONArray(repoString); 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) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
System.out.println(entry.getKey() + "/" + entry.getValue());
} }
} }
@ -72,26 +78,31 @@ public class RepoAdapter {
} }
class MyAsyncTask extends AsyncTask<String, String, Void> { class MyAsyncTask extends AsyncTask<String, String, Void> {
@Override @Override
protected void onPreExecute() { protected void onPreExecute() {
super.onPreExecute(); super.onPreExecute();
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
Toast.makeText(activityContext, "Refreshing online modules", Toast.LENGTH_SHORT).show();
} }
@Override @Override
protected Void doInBackground(String... params) { protected Void doInBackground(String... params) {
Log.d("Magisk", "doInBackground running"); publishProgress();
// Creating service handler class instance // Creating service handler class instance
WebRequest webreq = new WebRequest(); WebRequest webreq = new WebRequest();
// Making a request to url and getting response // Making a request to url and getting response
String jsonStr = webreq.makeWebServiceCall(url, WebRequest.GET); String jsonStr = webreq.makeWebServiceCall(url, WebRequest.GET);
Log.d("Magisk", "doInBackground Running, String: " + jsonStr + " Url: " + url);
Log.d("Response: ", "> " + jsonStr);
try { try {
repos.clear(); repos.clear();
@ -100,11 +111,18 @@ public class RepoAdapter {
JSONObject jsonobject = jsonArray.getJSONObject(i); JSONObject jsonobject = jsonArray.getJSONObject(i);
String name = jsonobject.getString("name"); String name = jsonobject.getString("name");
String url = jsonobject.getString("url"); String url = jsonobject.getString("url");
if (!name.contains("Repo.github.io")) { String lastUpdate = jsonobject.getString("updated_at");
repos.add(new Repo(name, url, activityContext)); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
} try {
for (int f = 0; f < repos.size(); f++) { updatedDate = format.parse(lastUpdate);
repos.get(f).fetch();
} 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) { } catch (JSONException e) {
@ -126,5 +144,4 @@ public class RepoAdapter {
} }
} }