You've already forked Magisk
mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-09-06 06:36:58 +00:00
Update UI callback with prefs listener
This commit is contained in:
@@ -1,40 +1,31 @@
|
||||
package com.topjohnwu.magisk.utils;
|
||||
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.SharedPreferences;
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Environment;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.renderscript.ScriptGroup;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.ipaulpro.afilechooser.FileInfo;
|
||||
import com.ipaulpro.afilechooser.utils.FileUtils;
|
||||
import com.topjohnwu.magisk.ModulesFragment;
|
||||
import com.topjohnwu.magisk.R;
|
||||
import com.topjohnwu.magisk.ReposFragment;
|
||||
import com.topjohnwu.magisk.module.Module;
|
||||
import com.topjohnwu.magisk.module.RepoHelper;
|
||||
import com.topjohnwu.magisk.receivers.DownloadReceiver;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@@ -157,6 +148,11 @@ public class Async {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Void v) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
|
||||
prefs.edit().putBoolean("module_done", true).apply();
|
||||
}
|
||||
}
|
||||
|
||||
public static class LoadRepos extends AsyncTask<Void, Void, Void> {
|
||||
@@ -176,11 +172,16 @@ public class Async {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Void v) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
|
||||
prefs.edit().putBoolean("repo_done", true).apply();
|
||||
}
|
||||
}
|
||||
|
||||
public static class FlashZIP extends AsyncTask<Void, Void, Boolean> {
|
||||
|
||||
private String mPath, mName;
|
||||
private String mName;
|
||||
private Uri mUri;
|
||||
private ProgressDialog progress;
|
||||
private File mFile, sdFile;
|
||||
@@ -282,7 +283,7 @@ public class Async {
|
||||
if (sdFile == null) {
|
||||
Toast.makeText(mContext, mContext.getString(R.string.install_error), Toast.LENGTH_LONG).show();
|
||||
} else {
|
||||
Toast.makeText(mContext, mContext.getString(R.string.manual_install, mPath), Toast.LENGTH_LONG).show();
|
||||
Toast.makeText(mContext, mContext.getString(R.string.manual_install, mFile.getAbsolutePath()), Toast.LENGTH_LONG).show();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
package com.topjohnwu.magisk.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.topjohnwu.magisk.ModulesFragment;
|
||||
import com.topjohnwu.magisk.R;
|
||||
import com.topjohnwu.magisk.module.Module;
|
||||
import com.topjohnwu.magisk.module.Repo;
|
||||
import com.topjohnwu.magisk.utils.Utils;
|
||||
import com.topjohnwu.magisk.utils.WebRequest;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class RepoHelper {
|
||||
private static List<Repo> repos = new ArrayList<>();
|
||||
private static String TAG = "Magisk";
|
||||
|
||||
private static final String file_key = "RepoMap";
|
||||
private static final String key = "repomap";
|
||||
public static HashMap<String, Repo> repoMap;
|
||||
|
||||
public static void createRepoMap(Context context) {
|
||||
Gson gson = new Gson();
|
||||
SharedPreferences prefs = context.getSharedPreferences(file_key, Context.MODE_PRIVATE);
|
||||
String jsonString = prefs.getString(key, null);
|
||||
if (jsonString != null) {
|
||||
repoMap = gson.fromJson(jsonString, new TypeToken< HashMap<String, Repo> >(){}.getType());
|
||||
}
|
||||
|
||||
if (repoMap == null) {
|
||||
repoMap = new HashMap<>();
|
||||
}
|
||||
|
||||
// Making a request to url and getting response
|
||||
String token = context.getString(R.string.some_string);
|
||||
String url1 = context.getString(R.string.url_main);
|
||||
String jsonStr = WebRequest.makeWebServiceCall(url1 + Utils.procFile(token, context), WebRequest.GET);
|
||||
if (jsonStr != null && !jsonStr.isEmpty()) {
|
||||
try {
|
||||
JSONArray jsonArray = new JSONArray(jsonStr);
|
||||
for (int i = 0; i < jsonArray.length(); i++) {
|
||||
JSONObject jsonobject = jsonArray.getJSONObject(i);
|
||||
String id = jsonobject.getString("description");
|
||||
String name = jsonobject.getString("name");
|
||||
String lastUpdate = jsonobject.getString("updated_at");
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
|
||||
Date updatedDate;
|
||||
try {
|
||||
updatedDate = format.parse(lastUpdate);
|
||||
} catch (ParseException e) {
|
||||
continue;
|
||||
}
|
||||
Repo repo = repoMap.get(id);
|
||||
if (repo == null) {
|
||||
repoMap.put(id, new Repo(context, name, updatedDate));
|
||||
} else {
|
||||
repo.update(updatedDate);
|
||||
}
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
jsonString = gson.toJson(repoMap);
|
||||
SharedPreferences.Editor editor = prefs.edit();
|
||||
editor.putString(key, jsonString);
|
||||
editor.apply();
|
||||
}
|
||||
}
|
||||
|
||||
public static void checkUpdate() {
|
||||
for (Module module : ModulesFragment.listModules) {
|
||||
module.checkUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
public static List<Repo> getSortedList() {
|
||||
ArrayList<Repo> list = new ArrayList<>(repoMap.values());
|
||||
Collections.sort(list, new Utils.ModuleComparator());
|
||||
return list;
|
||||
}
|
||||
|
||||
public interface TaskDelegate {
|
||||
void taskCompletionResult(String result);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user