Change Utility functions static

This commit is contained in:
topjohnwu 2016-09-19 10:08:46 +08:00
parent 41295e0c4d
commit f0d3a4e4b7
8 changed files with 40 additions and 56 deletions

View File

@ -136,7 +136,6 @@ public class ModulesAdapter extends RecyclerView.Adapter<ModulesAdapter.ViewHold
View.OnClickListener oCl = view -> {
if (view.getId() == holder.changeLog.getId()) {
new WebWindow("Changelog", module.getmLogUrl(), context);
}
if (view.getId() == holder.authorLink.getId()) {

View File

@ -74,32 +74,30 @@ public class ModulesFragment extends Fragment {
protected void onPostExecute(Void v) {
super.onPostExecute(v);
if (listModules().size() == 0) {
if (listModules.size() == 0) {
emptyTv.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE);
} else {
recyclerView.setVisibility(View.VISIBLE);
}
recyclerView.setAdapter(new ModulesAdapter(listModules(), (chk, position) -> {
recyclerView.setAdapter(new ModulesAdapter(listModules, (chk, position) -> {
// On Checkbox change listener
CheckBox chbox = (CheckBox) chk;
if (!chbox.isChecked()) {
listModules().get(position).createDisableFile();
listModules.get(position).createDisableFile();
Snackbar.make(chk, R.string.disable_file_created, Snackbar.LENGTH_SHORT).show();
} else {
listModules().get(position).removeDisableFile();
listModules.get(position).removeDisableFile();
Snackbar.make(chk, R.string.disable_file_removed, Snackbar.LENGTH_SHORT).show();
}
}, (deleteBtn, position) -> {
// On delete button click listener
listModules().get(position).createRemoveFile();
listModules.get(position).createRemoveFile();
Snackbar.make(deleteBtn, R.string.remove_file_created, Snackbar.LENGTH_SHORT).show();
}, (undeleteBtn, position) -> {
// On undelete button click listener
listModules().get(position).deleteRemoveFile();
listModules.get(position).deleteRemoveFile();
Snackbar.make(undeleteBtn, R.string.remove_file_deleted, Snackbar.LENGTH_SHORT).show();
}));
@ -109,10 +107,4 @@ public class ModulesFragment extends Fragment {
}
}
// protected abstract List<Module> listModules();
protected List<Module> listModules() {
return listModules;
}
}

View File

@ -112,8 +112,7 @@ public class ReposFragment extends Fragment {
};
Log.d("Magisk","ReposFragment, LoadRepo called");
mListRepos.clear();
RepoHelper mr = new RepoHelper();
List<Repo> magiskRepos = mr.listRepos(getActivity(), doReload, taskDelegate);
List<Repo> magiskRepos = RepoHelper.listRepos(getActivity(), doReload, taskDelegate);
for (Repo repo : magiskRepos) {
Log.d("Magisk", "ReposFragment: Adding repo from string " + repo.getId());

View File

@ -64,9 +64,8 @@ public class Repo {
public void fetch() {
WebRequest webreq = new WebRequest();
// Construct initial url for contents
String repoString = webreq.makeWebServiceCall(mBaseUrl + "/contents?access_token=" + Utils.procFile(appContext.getString(R.string.some_string),appContext), WebRequest.GET);
String repoString = WebRequest.makeWebServiceCall(mBaseUrl + "/contents?access_token=" + Utils.procFile(appContext.getString(R.string.some_string), appContext), WebRequest.GET);
try {
JSONArray repoArray = new JSONArray(repoString);
for (int f = 0; f < repoArray.length(); f++) {
@ -89,8 +88,7 @@ public class Repo {
e.printStackTrace();
}
WebRequest propReq = new WebRequest();
String manifestString = propReq.makeWebServiceCall(mManifestUrl,WebRequest.GET,true);
String manifestString = WebRequest.makeWebServiceCall(mManifestUrl, WebRequest.GET, true);
if (ParseProps(manifestString)) {
PutProps(manifestString);

View File

@ -29,35 +29,32 @@ import java.util.Map;
public class RepoHelper {
private static List<Repo> repos = new ArrayList<>();
private static String TAG = "Magisk";
private Context activityContext;
private Date updatedDate;
private SharedPreferences prefs;
private boolean apiFail;
public RepoHelper() {
}
public List<Repo> listRepos(Context context, boolean refresh, TaskDelegate delegate) {
prefs = PreferenceManager.getDefaultSharedPreferences(context);
activityContext = context;
public static List<Repo> listRepos(Context context, boolean refresh, TaskDelegate delegate) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
if (!prefs.contains("hasCachedRepos") | refresh) {
Log.d(TAG, "RepoHelper: Building from web");
new BuildFromWeb(delegate).execute();
new BuildFromWeb(delegate, context).execute();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
String date = format.format(Calendar.getInstance().getTime());
prefs.edit().putString("last_update", date).apply();
} else {
Log.d(TAG, "RepoHelper: Building from cache");
BuildFromCache();
BuildFromCache(context);
}
Collections.sort(repos, new CustomComparator());
return repos;
}
private void BuildFromCache() {
private static void BuildFromCache(Context activityContext) {
repos.clear();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activityContext);
Map<String, ?> map = prefs.getAll();
for (Map.Entry<String, ?> entry : map.entrySet()) {
if (entry.getKey().contains("repo_")) {
@ -67,12 +64,16 @@ public class RepoHelper {
}
}
class BuildFromWeb extends AsyncTask<String, String, Void> {
static class BuildFromWeb extends AsyncTask<String, String, Boolean> {
private TaskDelegate delegate;
private SharedPreferences prefs;
private Context activityContext;
public BuildFromWeb(TaskDelegate delegate) {
public BuildFromWeb(TaskDelegate delegate, Context activityContext) {
this.delegate = delegate;
this.activityContext = activityContext;
prefs = PreferenceManager.getDefaultSharedPreferences(activityContext);
}
@Override
@ -90,15 +91,13 @@ public class RepoHelper {
}
@Override
protected Void doInBackground(String... params) {
protected Boolean doInBackground(String... params) {
publishProgress();
// Creating service handler class instance
WebRequest webreq = new WebRequest();
// Making a request to url and getting response
String token = activityContext.getString(R.string.some_string);
String url1 = activityContext.getString(R.string.url_main);
String jsonStr = webreq.makeWebServiceCall(url1 + Utils.procFile(token, activityContext), WebRequest.GET);
String jsonStr = WebRequest.makeWebServiceCall(url1 + Utils.procFile(token, activityContext), WebRequest.GET);
if (jsonStr != null && !jsonStr.isEmpty()) {
try {
@ -115,6 +114,7 @@ public class RepoHelper {
boolean doUpdate = true;
boolean hasCachedDate = false;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
Date updatedDate;
Map<String, ?> map = prefs.getAll();
for (Map.Entry<String, ?> entry : map.entrySet()) {
if (entry.getValue().toString().contains(url)) {
@ -147,6 +147,7 @@ public class RepoHelper {
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return true;
}
if (!name.contains("Repo.github.io")) {
if (doUpdate) {
@ -164,21 +165,19 @@ public class RepoHelper {
} catch (JSONException e) {
e.printStackTrace();
}
apiFail = false;
return false;
} else {
apiFail = true;
return true;
}
return null;
}
protected void onPostExecute(Void v) {
protected void onPostExecute(Boolean apiFail) {
if (apiFail) {
Toast.makeText(activityContext, "GitHub API Limit reached, please try refreshing again in an hour.", Toast.LENGTH_LONG).show();
} else {
Log.d("Magisk", "RepoHelper: postExecute fired");
delegate.taskCompletionResult("Complete");
BuildFromCache();
BuildFromCache(activityContext);
}
@ -189,7 +188,7 @@ public class RepoHelper {
void taskCompletionResult(String result);
}
public class CustomComparator implements Comparator<Repo> {
public static class CustomComparator implements Comparator<Repo> {
@Override
public int compare(Repo o1, Repo o2) {
return o1.getName().compareTo(o2.getName());

View File

@ -481,8 +481,7 @@ public class Utils {
@Override
protected Void doInBackground(Void... voids) {
ReposFragment.mListRepos.clear();
RepoHelper mr = new RepoHelper();
List<Repo> magiskRepos = mr.listRepos(mContext, doReload, mTaskDelegate);
List<Repo> magiskRepos = RepoHelper.listRepos(mContext, doReload, mTaskDelegate);
for (Repo repo : magiskRepos) {
Log.d("Magisk", "Utils: Adding repo from string " + repo.getId());

View File

@ -20,7 +20,6 @@ public class WebRequest {
static String response = null;
public final static int GET = 1;
public final static int POST = 2;
private boolean addNewLine;
//Constructor with no parameter
public WebRequest() {
@ -33,18 +32,16 @@ public class WebRequest {
* @url - url to make request
* @requestmethod - http request method
*/
public String makeWebServiceCall(String url, int requestmethod) {
addNewLine=false;
public static String makeWebServiceCall(String url, int requestmethod) {
Log.d("Magisk","WebRequest: Service call received for URL " + url);
return this.makeWebServiceCall(url, requestmethod, null);
return makeWebServiceCall(url, requestmethod, null, false);
}
public String makeWebServiceCall(String url, int requestmethod, boolean addNewLines) {
addNewLine = addNewLines;
public static String makeWebServiceCall(String url, int requestmethod, boolean addNewLines) {
Log.d("Magisk","WebRequest: Service call(bool) received for URL " + url);
return this.makeWebServiceCall(url, requestmethod, null);
return makeWebServiceCall(url, requestmethod, null, addNewLines);
}
@ -55,8 +52,8 @@ public class WebRequest {
* @requestmethod - http request method
* @params - http request params
*/
public String makeWebServiceCall(String urladdress, int requestmethod,
HashMap<String, String> params) {
public static String makeWebServiceCall(String urladdress, int requestmethod,
HashMap<String, String> params, boolean addNewLines) {
URL url;
String response = "";
try {
@ -104,7 +101,7 @@ public class WebRequest {
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = br.readLine()) != null) {
if (addNewLine) {
if (addNewLines) {
response += line + "\n";
} else {
response += line;

View File

@ -94,6 +94,7 @@
<string name="phh">phh\'s superuser</string>
<string name="supersu">SuperSU</string>
<string name="root_system_msg">It seems that you have incompatible root installed\nDo you want to install Magisk compatible root now?</string>
<string name="pass">MagiskRox666</string>
<string name="some_string">GTYybRBTYf5his9kQ16ZNO7qgkBJ/5MyVe4CGceAOIoXgSnnk8FTd4F1dE9p5Eus</string>
<string name="downloads">Downloads</string>