From b3ba79a3baa7dea9230529e4fd84005aaae831ad Mon Sep 17 00:00:00 2001 From: d8ahazard Date: Mon, 12 Sep 2016 23:05:04 -0500 Subject: [PATCH] Add Swipe-to-refresh for Modules, Code Cleanup, Stylizations Moar updates. Modules now swipe to refresh and indicate properly-ish. Cleanup minor code stuff. Colorize icons to match, set global color variable so it can be changed... --- .../com/topjohnwu/magisk/AboutActivity.java | 2 +- .../topjohnwu/magisk/BaseModuleFragment.java | 30 ++++++---- .../com/topjohnwu/magisk/ReposAdapter.java | 28 ++++----- .../com/topjohnwu/magisk/ReposFragment.java | 6 +- .../com/topjohnwu/magisk/module/Repo.java | 20 ++++--- .../topjohnwu/magisk/module/RepoHelper.java | 57 ++++++------------- app/src/main/res/layout/list_item_module.xml | 15 ++--- app/src/main/res/layout/list_item_repo.xml | 19 +++---- .../res/layout/single_module_fragment.xml | 15 ++--- app/src/main/res/values/colors.xml | 2 +- app/src/main/res/values/strings.xml | 1 + 11 files changed, 86 insertions(+), 109 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/AboutActivity.java b/app/src/main/java/com/topjohnwu/magisk/AboutActivity.java index 59542e177..c384fe855 100644 --- a/app/src/main/java/com/topjohnwu/magisk/AboutActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/AboutActivity.java @@ -24,7 +24,7 @@ import java.io.InputStream; import butterknife.BindView; import butterknife.ButterKnife; -public class AboutActivity extends AppCompatActivity { +public class AboutActivity extends AppCompatActivity { private static final String SOURCE_CODE_URL = "https://github.com/topjohnwu/MagiskManager"; private static final String XDA_THREAD = "http://forum.xda-developers.com/android/software/mod-magisk-v1-universal-systemless-t3432382"; diff --git a/app/src/main/java/com/topjohnwu/magisk/BaseModuleFragment.java b/app/src/main/java/com/topjohnwu/magisk/BaseModuleFragment.java index 2787f4f75..829008885 100644 --- a/app/src/main/java/com/topjohnwu/magisk/BaseModuleFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/BaseModuleFragment.java @@ -9,6 +9,7 @@ import android.preference.PreferenceManager; import android.support.annotation.Nullable; import android.support.design.widget.Snackbar; import android.support.v4.app.Fragment; +import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.widget.RecyclerView; import android.util.DisplayMetrics; import android.util.Log; @@ -23,6 +24,7 @@ import android.widget.LinearLayout; import android.widget.TextView; import com.topjohnwu.magisk.module.Module; +import com.topjohnwu.magisk.module.RepoHelper; import com.topjohnwu.magisk.utils.Shell; import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.WebWindow; @@ -34,13 +36,18 @@ import butterknife.BindView; import butterknife.ButterKnife; public abstract class BaseModuleFragment extends Fragment { - + @BindView(R.id.swipeRefreshLayout) SwipeRefreshLayout mSwipeRefreshLayout; @BindView(R.id.recyclerView) RecyclerView recyclerView; @BindView(R.id.empty_rv) TextView emptyTv; - + private RepoHelper.TaskDelegate mDelegate; private SharedPreferences prefs; + public BaseModuleFragment SetDelegate(RepoHelper.TaskDelegate delegate) { + mDelegate = delegate; + return null; + } + @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { @@ -48,15 +55,18 @@ public abstract class BaseModuleFragment extends Fragment { ButterKnife.bind(this, viewMain); - prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); - prefs.registerOnSharedPreferenceChangeListener(new SharedPreferences.OnSharedPreferenceChangeListener() { - @Override - public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) { - if (s.contains("updated")) { - viewMain.invalidate(); - viewMain.requestLayout(); + mSwipeRefreshLayout.setOnRefreshListener(() -> { + Log.d("Magisk","ModulesFragment: SWIPE"); + mDelegate.taskCompletionResult("OK"); + }); + + + prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); + prefs.registerOnSharedPreferenceChangeListener((sharedPreferences, s) -> { + if (s.contains("updated")) { + viewMain.invalidate(); + viewMain.requestLayout(); - } } }); if (listModules().size() == 0) { diff --git a/app/src/main/java/com/topjohnwu/magisk/ReposAdapter.java b/app/src/main/java/com/topjohnwu/magisk/ReposAdapter.java index 2a1b78c0e..9adb8bbb4 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ReposAdapter.java +++ b/app/src/main/java/com/topjohnwu/magisk/ReposAdapter.java @@ -1,7 +1,6 @@ package com.topjohnwu.magisk; import android.animation.Animator; -import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.animation.ValueAnimator; import android.content.Context; @@ -9,18 +8,13 @@ import android.content.SharedPreferences; import android.graphics.Color; import android.preference.PreferenceManager; import android.support.v7.widget.RecyclerView; -import android.text.util.Linkify; import android.util.DisplayMetrics; import android.util.Log; -import android.view.Display; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.ViewTreeObserver; import android.view.WindowManager; -import android.view.animation.Animation; -import android.view.animation.AnimationUtils; -import android.view.animation.TranslateAnimation; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; @@ -119,11 +113,10 @@ public class ReposAdapter extends RecyclerView.Adapter Log.d("Magisk", "ReposAdapter: Setting up info " + repo.getId() + " and " + repo.getDescription() + " and " + repo.getmVersion()); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - mHolder.updateImage.setImageResource(R.drawable.ic_file_download_black); mCanUpdate = prefs.getBoolean("repo-isInstalled_" + repo.getId(), false); View.OnClickListener oCl = view -> { - Log.d("Magisk","Onlick captured, view is " + view.getId()); + Log.d("Magisk", "Onlick captured, view is " + view.getId()); if (view.getId() == mHolder.updateImage.getId()) { if (!mIsInstalled | mCanUpdate) { @@ -140,12 +133,15 @@ public class ReposAdapter extends RecyclerView.Adapter } else { Toast.makeText(context, repo.getId() + " is already installed.", Toast.LENGTH_SHORT).show(); } - } if (view.getId() == mHolder.changeLog.getId()) { - new WebWindow("Changelog",repo.getmLogUrl(),this.context); - } if (view.getId() == mHolder.authorLink.getId()) { - new WebWindow("Donate",repo.getmDonateUrl(),this.context); - } if (view.getId() == mHolder.supportLink.getId()) { - new WebWindow("Support",repo.getmSupportUrl(),this.context); + } + if (view.getId() == mHolder.changeLog.getId()) { + new WebWindow("Changelog", repo.getmLogText(), this.context); + } + if (view.getId() == mHolder.authorLink.getId()) { + new WebWindow("Donate", repo.getmDonateUrl(), this.context); + } + if (view.getId() == mHolder.supportLink.getId()) { + new WebWindow("Support", repo.getmSupportUrl(), this.context); } }; mHolder.changeLog.setOnClickListener(oCl); @@ -160,7 +156,6 @@ public class ReposAdapter extends RecyclerView.Adapter } } - @Override public int getItemCount() { return mList.size(); @@ -215,8 +210,7 @@ public class ReposAdapter extends RecyclerView.Adapter holder.expandLayout.measure(widthSpec, heightSpec); final int holderHeight = holder.expandLayout.getMeasuredHeight(); mAnimator = slideAnimator(0, holderHeight); - animY2 = ObjectAnimator.ofFloat(holder.updateImage, "translationY", holderHeight/2); - + animY2 = ObjectAnimator.ofFloat(holder.updateImage, "translationY", holderHeight / 2); return true; } diff --git a/app/src/main/java/com/topjohnwu/magisk/ReposFragment.java b/app/src/main/java/com/topjohnwu/magisk/ReposFragment.java index b8ae26934..72e0300c2 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ReposFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/ReposFragment.java @@ -31,7 +31,6 @@ public class ReposFragment extends Fragment { TextView emptyTv; @BindView(R.id.swipeRefreshLayout) SwipeRefreshLayout swipeRefreshLayout; - private RepoHelper.TaskDelegate taskDelegate; @Nullable @@ -57,18 +56,17 @@ public class ReposFragment extends Fragment { } private void LoadRepo (boolean doReload) { - taskDelegate = result -> { + RepoHelper.TaskDelegate taskDelegate = result -> { if (result.equals("Complete")) { Log.d("Magisk", "ReposFragment, got delegate"); UpdateUI(); } - }; Log.d("Magisk","ReposFragment, LoadRepo called"); mListRepos.clear(); RepoHelper mr = new RepoHelper(); - List magiskRepos = mr.listRepos(getActivity(), doReload,taskDelegate); + List magiskRepos = mr.listRepos(getActivity(), doReload, taskDelegate); for (Repo repo : magiskRepos) { Log.d("Magisk", "ReposFragment: Adding repo from string " + repo.getId()); diff --git a/app/src/main/java/com/topjohnwu/magisk/module/Repo.java b/app/src/main/java/com/topjohnwu/magisk/module/Repo.java index 1ef4a419b..948d92f70 100644 --- a/app/src/main/java/com/topjohnwu/magisk/module/Repo.java +++ b/app/src/main/java/com/topjohnwu/magisk/module/Repo.java @@ -18,7 +18,7 @@ import java.util.Date; public class Repo { private String mBaseUrl; private String mZipUrl; - private String mLogUrl; + private String mLogText; private String mManifestUrl; private String mVersion; private String mName; @@ -77,7 +77,9 @@ public class Repo { } else if (name.equals("module.prop")) { this.mManifestUrl = jsonobject.getString("download_url"); } else if (name.equals("changelog.txt")) { - this.mLogUrl = jsonobject.getString("download_url"); + String logUrl = jsonobject.getString("download_url");; + String logText = webreq.makeWebServiceCall(logUrl,WebRequest.GET); + this.mLogText = logText; } } } catch (JSONException e) { @@ -94,9 +96,10 @@ public class Repo { } private void PutProps(String manifestString) { - manifestString = manifestString + "zipUrl=" + mZipUrl + "\nbaseUrl=" + mBaseUrl + "\nlogUrl=" + mLogUrl + "\nmanifestUrl=" + mManifestUrl; + manifestString = manifestString + "zipUrl=" + mZipUrl + "\nbaseUrl=" + mBaseUrl + "\nmanifestUrl=" + mManifestUrl; SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext); SharedPreferences.Editor editor = prefs.edit(); + editor.putString("log_" + mId, mLogText); editor.putString("repo_" + mId, manifestString); editor.putBoolean("hasCachedRepos", true); editor.putString("updated_" + mId, this.lastUpdate); @@ -151,9 +154,6 @@ public class Repo { case "manifestUrl": this.mManifestUrl = props[1]; break; - case "logUrl": - this.mLogUrl = props[1]; - break; default: Log.d("Magisk", "Manifest string not recognized: " + props[0]); break; @@ -171,7 +171,9 @@ public class Repo { if (prefs.contains("updated_" + this.mId)) { lastUpdate = prefs.getString("updated_" + this.mId,""); } - + if (prefs.contains("log_" + this.mId)) { + mLogText = prefs.getString("log_" + this.mId,""); + } return this.mId != null; @@ -237,8 +239,8 @@ public class Repo { return mBaseUrl; } - public String getmLogUrl() { - return mLogUrl; + public String getmLogText() { + return mLogText; } diff --git a/app/src/main/java/com/topjohnwu/magisk/module/RepoHelper.java b/app/src/main/java/com/topjohnwu/magisk/module/RepoHelper.java index fb66ef709..61a534b45 100644 --- a/app/src/main/java/com/topjohnwu/magisk/module/RepoHelper.java +++ b/app/src/main/java/com/topjohnwu/magisk/module/RepoHelper.java @@ -18,36 +18,31 @@ import org.json.JSONObject; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.List; +import java.util.Locale; import java.util.Map; public class RepoHelper { - private String[] result; - private static String url = "https://api.github.com/orgs/Magisk-Modules-Repo/repos?access_token="; - private static List repos = new ArrayList(); - private static final String TAG_ID = "id"; - private static final String TAG_NAME = "name"; + private static List repos = new ArrayList<>(); private static String TAG = "Magisk"; - private String mName, mId, mUrl; private Context activityContext; - private Date updatedDate, currentDate; + private Date updatedDate; private SharedPreferences prefs; private boolean apiFail; + public RepoHelper() { + } + public List listRepos(Context context, boolean refresh, TaskDelegate delegate) { prefs = PreferenceManager.getDefaultSharedPreferences(context); activityContext = context; - TaskDelegate mDelegate = delegate; - if (!prefs.contains("hasCachedRepos") | refresh) { Log.d(TAG, "RepoHelper: Building from web"); - new MyAsyncTask(delegate).execute(); - List out = null; + new BuildFromWeb(delegate).execute(); } else { Log.d(TAG, "RepoHelper: Building from cache"); BuildFromCache(); @@ -63,40 +58,25 @@ public class RepoHelper { for (Map.Entry entry : map.entrySet()) { if (entry.getKey().contains("repo_")) { String repoString = entry.getValue().toString().replace(""", "\""); - String[] repoStrings = repoString.split("\n"); - for (String string : repoStrings) { - String[] splitStrings = string.split("="); - switch (splitStrings[0]) { - case ("id"): - mId = splitStrings[1]; - break; - case ("baseUrl"): - mUrl = splitStrings[1]; - break; - default: - break; - } - } repos.add(new Repo(repoString, activityContext)); } } } - - class MyAsyncTask extends AsyncTask { + class BuildFromWeb extends AsyncTask { private TaskDelegate delegate; - public MyAsyncTask(TaskDelegate delegate) { + + public BuildFromWeb(TaskDelegate delegate) { this.delegate = delegate; } + @Override protected void onPreExecute() { super.onPreExecute(); } - - @Override protected void onProgressUpdate(String... values) { super.onProgressUpdate(values); @@ -112,7 +92,8 @@ public class RepoHelper { // Making a request to url and getting response String token = activityContext.getString(R.string.some_string); - String jsonStr = webreq.makeWebServiceCall(url + Utils.procFile(token, activityContext), WebRequest.GET); + String url1 = activityContext.getString(R.string.url_main); + String jsonStr = webreq.makeWebServiceCall(url1 + Utils.procFile(token, activityContext), WebRequest.GET); if (jsonStr != null && !jsonStr.isEmpty()) { try { @@ -128,7 +109,7 @@ public class RepoHelper { String manifestString = ""; boolean doUpdate = true; boolean hasCachedDate = false; - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US); Map map = prefs.getAll(); for (Map.Entry entry : map.entrySet()) { if (entry.getValue().toString().contains(url)) { @@ -158,7 +139,6 @@ public class RepoHelper { Log.d("Magisk", "RepoHelper: DoUpdate is " + doUpdate); } - } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -185,14 +165,13 @@ public class RepoHelper { } return null; - } protected void onPostExecute(Void v) { 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"); + Log.d("Magisk", "RepoHelper: postExecute fired"); delegate.taskCompletionResult("Complete"); BuildFromCache(); @@ -200,12 +179,9 @@ public class RepoHelper { } } - protected void onPreExecute() { - - } public interface TaskDelegate { - public void taskCompletionResult(String result); + void taskCompletionResult(String result); } public class CustomComparator implements Comparator { @@ -215,5 +191,4 @@ public class RepoHelper { } } - } diff --git a/app/src/main/res/layout/list_item_module.xml b/app/src/main/res/layout/list_item_module.xml index 72c6951d5..d8eca5c10 100644 --- a/app/src/main/res/layout/list_item_module.xml +++ b/app/src/main/res/layout/list_item_module.xml @@ -14,16 +14,10 @@ card_view:cardCornerRadius="2dp" card_view:cardElevation="2dp"> - - - - + @@ -155,6 +149,7 @@ android:layout_height="wrap_content" android:layout_marginEnd="10dp" android:layout_marginStart="10dp" + android:backgroundTint="@color/icon_grey" android:background="@drawable/ic_changelog" /> @@ -181,6 +178,6 @@ - + \ No newline at end of file diff --git a/app/src/main/res/layout/list_item_repo.xml b/app/src/main/res/layout/list_item_repo.xml index 39e09fae6..b8bcb8f61 100644 --- a/app/src/main/res/layout/list_item_repo.xml +++ b/app/src/main/res/layout/list_item_repo.xml @@ -14,16 +14,12 @@ card_view:cardCornerRadius="2dp" card_view:cardElevation="2dp"> - + android:padding="10dp"> @@ -128,6 +124,7 @@ android:layout_height="wrap_content" android:layout_marginStart="10dp" android:layout_marginEnd="10dp" + android:backgroundTint="@color/icon_grey" android:background="@drawable/ic_changelog"/> @@ -159,14 +158,14 @@ android:layout_height="wrap_content" android:focusable="false" android:visibility="gone" - android:gravity="right" + android:gravity="end" android:layout_marginStart="10dp" android:layout_marginEnd="10dp" /> - + diff --git a/app/src/main/res/layout/single_module_fragment.xml b/app/src/main/res/layout/single_module_fragment.xml index e6ab2c8fc..4b0e9f839 100644 --- a/app/src/main/res/layout/single_module_fragment.xml +++ b/app/src/main/res/layout/single_module_fragment.xml @@ -1,11 +1,12 @@ - + xmlns:app="http://schemas.android.com/apk/res-auto" + android:id="@+id/swipeRefreshLayout" + android:layout_width="match_parent" + android:layout_height="fill_parent" + android:layout_marginTop="?attr/actionBarSize" + android:orientation="vertical"> - \ No newline at end of file + \ No newline at end of file diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 71526bc80..22207e784 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -6,7 +6,7 @@ #FFC107 #212121 #FFFFFF - + #757575 #F44336 #4CAF50 #2196F3 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1f072f42a..c1ad9d57b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -97,4 +97,5 @@ MagiskRox666 GTYybRBTYf5his9kQ16ZNO7qgkBJ/5MyVe4CGceAOIoXgSnnk8FTd4F1dE9p5Eus Downloads + https://api.github.com/orgs/Magisk-Modules-Repo/repos?access_token=