Commit 41295e0c authored by topjohnwu's avatar topjohnwu

Refactor modules fragment

parent 1e09ccb4
This diff is collapsed.
package com.topjohnwu.magisk; package com.topjohnwu.magisk;
import android.app.Activity; import android.content.SharedPreferences;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.provider.MediaStore; import android.preference.PreferenceManager;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager; import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.app.FragmentPagerAdapter; import android.support.v7.widget.RecyclerView;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ProgressBar; import android.widget.CheckBox;
import android.widget.Toast; import android.widget.TextView;
import com.ipaulpro.afilechooser.FileInfo;
import com.ipaulpro.afilechooser.utils.FileUtils;
import com.topjohnwu.magisk.module.Module; import com.topjohnwu.magisk.module.Module;
import com.topjohnwu.magisk.module.RepoHelper;
import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.Utils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List; import java.util.List;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
public class ModulesFragment extends Fragment { public class ModulesFragment extends Fragment {
@BindView(R.id.swipeRefreshLayout) SwipeRefreshLayout mSwipeRefreshLayout;
@BindView(R.id.recyclerView) RecyclerView recyclerView;
@BindView(R.id.empty_rv) TextView emptyTv;
private static final int FETCH_ZIP_CODE = 2; private SharedPreferences prefs;
public static List<Module> listModules = new ArrayList<>(); public static List<Module> listModules = new ArrayList<>();
public static List<Module> listModulesCache = new ArrayList<>();
@BindView(R.id.progressBar) ProgressBar progressBar;
@BindView(R.id.fab) FloatingActionButton fabio;
@BindView(R.id.pager) ViewPager viewPager;
@BindView(R.id.tab_layout) TabLayout tabLayout;
private int viewPagePosition;
private RepoHelper.TaskDelegate mTaskDelegate;
@Nullable @Nullable
@Override @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.modules_fragment, container, false); View viewMain = inflater.inflate(R.layout.modules_fragment, container, false);
ButterKnife.bind(this, view);
fabio.setOnClickListener(v -> {
Intent getContentIntent = FileUtils.createGetContentIntent(null);
getContentIntent.setType("application/zip");
Intent fileIntent = Intent.createChooser(getContentIntent, "Select a file");
startActivityForResult(fileIntent, FETCH_ZIP_CODE); ButterKnife.bind(this, viewMain);
}); prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
mSwipeRefreshLayout.setOnRefreshListener(() -> {
mTaskDelegate = result -> {
if (result.equals("OK")) {
RefreshUI();
}
};
new updateUI().execute(); recyclerView.setVisibility(View.GONE);
return view; new Utils.LoadModules(getActivity()).execute();
} new updateUI().execute();
prefs.edit().putBoolean("ignoreUpdateAlerts", false).apply();
@Override });
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
// Get the URI of the selected file
final Uri uri = data.getData();
Log.i("Magisk", "ModulesFragment: Uri = " + uri.toString() + " or ");
new Utils.FlashZIP(getActivity(),uri).execute();
try {
// Get the file path from the URI
FileInfo fileInfo = FileUtils.getFileInfo(getActivity(), uri);
Toast.makeText(getActivity(),
"File Selected: " + fileInfo.getDisplayName() + " size: " + fileInfo.getSize(), Toast.LENGTH_LONG).show();
if (!fileInfo.isExternal()) {
} else { prefs.registerOnSharedPreferenceChangeListener((sharedPreferences, s) -> {
if (s.contains("updated")) {
viewMain.invalidate();
viewMain.requestLayout();
}
} catch (Exception e) {
Log.e("FileSelectorTestAc...", "File select error", e);
} }
} });
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_module, menu);
}
private void RefreshUI() {
viewPagePosition = tabLayout.getSelectedTabPosition();
listModules.clear();
listModulesCache.clear();
progressBar.setVisibility(View.VISIBLE);
viewPager.setAdapter(new TabsAdapter(getChildFragmentManager()));
tabLayout.setupWithViewPager(viewPager);
viewPager.setCurrentItem(viewPagePosition);
new Utils.LoadModules(getActivity()).execute();
Collections.sort(listModules, new CustomComparator());
Collections.sort(listModulesCache, new CustomComparator());
new updateUI().execute(); new updateUI().execute();
}
void selectPage(int pageIndex) {
tabLayout.setScrollPosition(pageIndex, 0f, true);
viewPager.setCurrentItem(pageIndex);
}
public static class NormalModuleFragment extends BaseModuleFragment {
@Override
protected List<Module> listModules() {
return listModules;
}
}
public static class CacheModuleFragment extends BaseModuleFragment {
@Override
protected List<Module> listModules() {
return listModulesCache;
}
return viewMain;
} }
private class updateUI extends AsyncTask<Void, Void, Void> { private class updateUI extends AsyncTask<Void, Void, Void> {
...@@ -154,53 +73,46 @@ public class ModulesFragment extends Fragment { ...@@ -154,53 +73,46 @@ public class ModulesFragment extends Fragment {
@Override @Override
protected void onPostExecute(Void v) { protected void onPostExecute(Void v) {
super.onPostExecute(v); super.onPostExecute(v);
progressBar.setVisibility(View.GONE);
viewPager.setAdapter(new TabsAdapter(getChildFragmentManager()));
tabLayout.setupWithViewPager(viewPager);
selectPage(viewPagePosition);
} if (listModules().size() == 0) {
} emptyTv.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE);
private class TabsAdapter extends FragmentPagerAdapter { } else {
recyclerView.setVisibility(View.VISIBLE);
}
recyclerView.setAdapter(new ModulesAdapter(listModules(), (chk, position) -> {
// On Checkbox change listener
CheckBox chbox = (CheckBox) chk;
String[] tabTitles = new String[]{ if (!chbox.isChecked()) {
getString(R.string.modules), getString(R.string.cache_modules) listModules().get(position).createDisableFile();
}; Snackbar.make(chk, R.string.disable_file_created, Snackbar.LENGTH_SHORT).show();
} else {
listModules().get(position).removeDisableFile();
Snackbar.make(chk, R.string.disable_file_removed, Snackbar.LENGTH_SHORT).show();
}
}, (deleteBtn, position) -> {
// On delete button click listener
public TabsAdapter(FragmentManager fm) { listModules().get(position).createRemoveFile();
super(fm); Snackbar.make(deleteBtn, R.string.remove_file_created, Snackbar.LENGTH_SHORT).show();
} }, (undeleteBtn, position) -> {
// On undelete button click listener
@Override listModules().get(position).deleteRemoveFile();
public int getCount() { Snackbar.make(undeleteBtn, R.string.remove_file_deleted, Snackbar.LENGTH_SHORT).show();
return tabTitles.length; }));
}
@Override if (mSwipeRefreshLayout.isRefreshing())
public String getPageTitle(int position) { mSwipeRefreshLayout.setRefreshing(false);
return tabTitles[position];
}
@Override
public Fragment getItem(int position) {
if (position == 0) {
NormalModuleFragment nmf = new NormalModuleFragment();
nmf.SetDelegate(mTaskDelegate);
return nmf;
} else {
CacheModuleFragment cmf = new CacheModuleFragment();
cmf.SetDelegate(mTaskDelegate);
return cmf;
}
} }
} }
public class CustomComparator implements Comparator<Module> {
@Override // protected abstract List<Module> listModules();
public int compare(Module o1, Module o2) { protected List<Module> listModules() {
return o1.getName().compareTo(o2.getName()); return listModules;
}
} }
} }
package com.topjohnwu.magisk; package com.topjohnwu.magisk;
import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
...@@ -50,7 +48,7 @@ public class ReposFragment extends Fragment { ...@@ -50,7 +48,7 @@ public class ReposFragment extends Fragment {
@Override @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.single_repo_fragment, container, false); View view = inflater.inflate(R.layout.repos_fragment, container, false);
mView = view; mView = view;
ButterKnife.bind(this, view); ButterKnife.bind(this, view);
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
......
package com.topjohnwu.magisk; package com.topjohnwu.magisk;
import android.Manifest; import android.Manifest;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
......
...@@ -15,8 +15,9 @@ public class Module { ...@@ -15,8 +15,9 @@ public class Module {
private String mName = null; private String mName = null;
private String mVersion = "(No version provided)"; private String mVersion = "(No version provided)";
private String mDescription = "(No description provided)"; private String mDescription = "(No description provided)";
private String mUrl,mSupportUrl,mDonateUrl,mZipUrl,mBaseUrl,mManifestUrl,mAuthor,mLogUrl; private String mSupportUrl, mDonateUrl, mZipUrl, mAuthor, mLogUrl;
private boolean mEnable, mRemove,mUpdateAvailable, mIsInstalled,mIsCacheModule; private boolean mEnable = false, mRemove = false, mUpdateAvailable = false, mIsInstalled,
mIsCacheModule = false;
private String mId; private String mId;
...@@ -38,47 +39,32 @@ public class Module { ...@@ -38,47 +39,32 @@ public class Module {
} }
switch (props[0]) { switch (props[0]) {
case "versionCode": case "id":
this.mVersionCode = Integer.valueOf(props[1]); this.mId = props[1];
break; break;
case "name": case "name":
this.mName = props[1]; this.mName = props[1];
break; break;
case "author":
this.mAuthor = props[1];
break;
case "id":
this.mId = props[1];
break;
case "version": case "version":
this.mVersion = props[1]; this.mVersion = props[1];
break; break;
case "description": case "versionCode":
this.mDescription = props[1]; this.mVersionCode = Integer.parseInt(props[1]);
break; break;
case "donate": case "author":
this.mDonateUrl = props[1]; this.mAuthor = props[1];
break; break;
case "cacheModule": case "description":
this.mIsCacheModule = Boolean.valueOf(props[1]); this.mDescription = props[1];
break; break;
case "support": case "support":
this.mSupportUrl = props[1]; this.mSupportUrl = props[1];
break; break;
case "donateUrl": case "donate":
this.mDonateUrl = props[1]; this.mDonateUrl = props[1];
break; break;
case "zipUrl": case "cacheModule":
this.mZipUrl = props[1]; this.mIsCacheModule = Boolean.parseBoolean(props[1]);
break;
case "baseUrl":
this.mBaseUrl = props[1];
break;
case "manifestUrl":
this.mManifestUrl = props[1];
break;
case "logUrl":
this.mLogUrl = props[1];
break; break;
default: default:
Log.d("Magisk", "Module: Manifest string not recognized: " + props[0]); Log.d("Magisk", "Module: Manifest string not recognized: " + props[0]);
...@@ -165,18 +151,18 @@ public class Module { ...@@ -165,18 +151,18 @@ public class Module {
} }
public Module(Repo repo) { // public Module(Repo repo) {
//
mName = repo.getName(); // mName = repo.getName();
mVersion = repo.getmVersion(); // mVersion = repo.getmVersion();
mDescription = repo.getDescription(); // mDescription = repo.getDescription();
mId = repo.getId(); // mId = repo.getId();
mVersionCode = repo.getmVersionCode(); // mVersionCode = repo.getmVersionCode();
mUrl = repo.getmZipUrl(); // mUrl = repo.getmZipUrl();
mEnable = true; // mEnable = true;
mRemove = false; // mRemove = false;
//
} // }
...@@ -224,22 +210,24 @@ public class Module { ...@@ -224,22 +210,24 @@ public class Module {
return mRemove; return mRemove;
} }
public boolean isCache() {
return mIsCacheModule;
}
public void setCache() {
mIsCacheModule = true;
}
public String getmDonateUrl() { public String getmDonateUrl() {
return mDonateUrl; return mDonateUrl;
} }
public String getmZipUrl() { return mZipUrl; } public String getmZipUrl() { return mZipUrl; }
public String getmManifestUrl() {
return mManifestUrl;
}
public String getmSupportUrl() { public String getmSupportUrl() {
return mSupportUrl; return mSupportUrl;
} }
public boolean isInstalled() {return mIsInstalled; }
public boolean isUpdateAvailable() { return mUpdateAvailable; } public boolean isUpdateAvailable() { return mUpdateAvailable; }
} }
\ No newline at end of file
...@@ -444,7 +444,6 @@ public class Utils { ...@@ -444,7 +444,6 @@ public class Utils {
@Override @Override
protected Void doInBackground(Void... voids) { protected Void doInBackground(Void... voids) {
ModulesFragment.listModules.clear(); ModulesFragment.listModules.clear();
ModulesFragment.listModulesCache.clear();
List<String> magisk = getModList(MAGISK_PATH); List<String> magisk = getModList(MAGISK_PATH);
Log.d("Magisk", "Utils: Reload called, loading modules"); Log.d("Magisk", "Utils: Reload called, loading modules");
List<String> magiskCache = getModList(MAGISK_CACHE_PATH); List<String> magiskCache = getModList(MAGISK_CACHE_PATH);
...@@ -456,7 +455,10 @@ public class Utils { ...@@ -456,7 +455,10 @@ public class Utils {
for (String mod : magiskCache) { for (String mod : magiskCache) {
Log.d("Magisk", "Utils: Adding cache module from string " + mod); Log.d("Magisk", "Utils: Adding cache module from string " + mod);
ModulesFragment.listModulesCache.add(new Module(mod, mContext)); Module cacheMod = new Module(mod, mContext);
// Prevent people forgot to change module.prop
cacheMod.setCache();
ModulesFragment.listModules.add(cacheMod);
} }
return null; return null;
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" <android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content" android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="fill_parent"
android:layout_marginTop="?attr/actionBarSize"
<RelativeLayout android:orientation="vertical">
android:layout_width="fill_parent"
android:layout_height="wrap_content"> <android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_height="match_parent"
android:id="@+id/llayout" android:clipToPadding="false"
android:layout_width="match_parent" android:paddingBottom="60dip"
android:layout_height="fill_parent" app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical"> <TextView
android:id="@+id/empty_rv"
<android.support.design.widget.TabLayout android:layout_width="match_parent"
android:id="@+id/tab_layout" android:layout_height="match_parent"
android:layout_width="match_parent" android:layout_gravity="center"
android:layout_height="wrap_content" android:fontFamily="sans-serif-light"
android:background="?attr/colorPrimary" android:gravity="center"
android:elevation="4dp" android:text="@string/no_modules_found"
android:minHeight="?attr/actionBarSize" android:textSize="20sp"
app:tabGravity="fill" android:textStyle="italic"
app:tabIndicatorColor="?attr/colorAccent" android:visibility="gone"/>
app:tabMode="fixed"
app:tabTextAppearance="@style/TextAppearance.Design.Tab" /> </android.support.v4.widget.SwipeRefreshLayout>
\ No newline at end of file
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true" />
</LinearLayout>
<LinearLayout
android:id="@+id/ly_bar_bottom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:orientation="horizontal">
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:baselineAlignBottom="false"
android:clickable="true"
android:src="@drawable/ic_add"
app:layout_anchorGravity="bottom|right|end"
/>
</LinearLayout>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
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">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="60dip"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>
<TextView
android:id="@+id/empty_rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:fontFamily="sans-serif-light"
android:gravity="center"
android:text="@string/no_modules_found"
android:textSize="20sp"
android:textStyle="italic"
android:visibility="gone"/>
</android.support.v4.widget.SwipeRefreshLayout>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment