2016-08-22 21:18:28 +00:00
|
|
|
package com.topjohnwu.magisk;
|
2016-08-17 11:00:55 +00:00
|
|
|
|
2016-08-31 21:49:35 +00:00
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.Intent;
|
2016-09-15 17:52:58 +00:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.BitmapFactory;
|
2016-08-31 21:49:35 +00:00
|
|
|
import android.net.Uri;
|
2016-08-17 11:00:55 +00:00
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.os.Bundle;
|
2016-09-15 17:52:58 +00:00
|
|
|
import android.provider.MediaStore;
|
2016-08-21 13:29:42 +00:00
|
|
|
import android.support.annotation.Nullable;
|
2016-08-31 21:49:35 +00:00
|
|
|
import android.support.design.widget.FloatingActionButton;
|
2016-08-21 14:42:50 +00:00
|
|
|
import android.support.design.widget.TabLayout;
|
|
|
|
import android.support.v4.app.Fragment;
|
|
|
|
import android.support.v4.app.FragmentManager;
|
|
|
|
import android.support.v4.app.FragmentPagerAdapter;
|
|
|
|
import android.support.v4.view.ViewPager;
|
2016-09-15 17:52:58 +00:00
|
|
|
import android.util.Log;
|
2016-08-21 13:29:42 +00:00
|
|
|
import android.view.LayoutInflater;
|
2016-08-27 19:52:03 +00:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
2016-08-21 13:29:42 +00:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2016-08-21 14:08:45 +00:00
|
|
|
import android.widget.ProgressBar;
|
2016-09-15 17:52:58 +00:00
|
|
|
import android.widget.Toast;
|
2016-08-20 15:26:49 +00:00
|
|
|
|
2016-09-15 17:52:58 +00:00
|
|
|
import com.ipaulpro.afilechooser.FileInfo;
|
|
|
|
import com.ipaulpro.afilechooser.utils.FileUtils;
|
2016-08-22 21:18:28 +00:00
|
|
|
import com.topjohnwu.magisk.module.Module;
|
2016-09-13 12:31:43 +00:00
|
|
|
import com.topjohnwu.magisk.module.RepoHelper;
|
2016-08-24 21:58:15 +00:00
|
|
|
import com.topjohnwu.magisk.utils.Utils;
|
2016-08-17 11:00:55 +00:00
|
|
|
|
2016-08-25 10:40:00 +00:00
|
|
|
import java.util.ArrayList;
|
2016-09-12 21:47:32 +00:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Comparator;
|
2016-08-17 11:00:55 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
2016-08-21 13:29:42 +00:00
|
|
|
import butterknife.BindView;
|
|
|
|
import butterknife.ButterKnife;
|
|
|
|
|
2016-08-21 15:21:37 +00:00
|
|
|
public class ModulesFragment extends Fragment {
|
2016-08-17 11:00:55 +00:00
|
|
|
|
2016-09-15 12:35:12 +00:00
|
|
|
private static final int FETCH_ZIP_CODE = 2;
|
2016-08-28 22:35:07 +00:00
|
|
|
public static List<Module> listModules = new ArrayList<>();
|
|
|
|
public static List<Module> listModulesCache = new ArrayList<>();
|
2016-09-10 04:40:57 +00:00
|
|
|
@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;
|
2016-09-15 12:35:12 +00:00
|
|
|
private int viewPagePosition;
|
2016-09-13 12:31:43 +00:00
|
|
|
private RepoHelper.TaskDelegate mTaskDelegate;
|
2016-08-21 14:08:45 +00:00
|
|
|
|
2016-08-21 13:29:42 +00:00
|
|
|
@Nullable
|
2016-08-17 11:00:55 +00:00
|
|
|
@Override
|
2016-08-21 13:29:42 +00:00
|
|
|
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
2016-08-21 14:08:45 +00:00
|
|
|
View view = inflater.inflate(R.layout.modules_fragment, container, false);
|
2016-09-06 21:54:08 +00:00
|
|
|
|
2016-08-21 14:08:45 +00:00
|
|
|
ButterKnife.bind(this, view);
|
2016-09-14 22:12:47 +00:00
|
|
|
fabio.setOnClickListener(v -> {
|
2016-09-15 17:52:58 +00:00
|
|
|
Intent getContentIntent = FileUtils.createGetContentIntent(null);
|
|
|
|
getContentIntent.setType("application/zip");
|
|
|
|
Intent fileIntent = Intent.createChooser(getContentIntent, "Select a file");
|
|
|
|
|
2016-09-15 12:35:12 +00:00
|
|
|
startActivityForResult(fileIntent, FETCH_ZIP_CODE);
|
2016-09-14 22:12:47 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2016-09-10 04:40:57 +00:00
|
|
|
new Utils.LoadModules(getActivity(), false).execute();
|
2016-09-13 12:31:43 +00:00
|
|
|
mTaskDelegate = result -> {
|
|
|
|
if (result.equals("OK")) {
|
|
|
|
RefreshUI();
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2016-09-14 22:12:47 +00:00
|
|
|
|
2016-08-25 10:08:07 +00:00
|
|
|
new updateUI().execute();
|
2016-08-21 14:08:45 +00:00
|
|
|
return view;
|
2016-08-17 11:00:55 +00:00
|
|
|
}
|
|
|
|
|
2016-09-10 04:40:57 +00:00
|
|
|
@Override
|
2016-08-31 21:49:35 +00:00
|
|
|
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
2016-09-15 17:52:58 +00:00
|
|
|
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 {
|
|
|
|
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e("FileSelectorTestAc...", "File select error", e);
|
|
|
|
}
|
2016-09-10 04:40:57 +00:00
|
|
|
}
|
2016-09-15 17:52:58 +00:00
|
|
|
|
2016-09-10 04:40:57 +00:00
|
|
|
}
|
|
|
|
|
2016-09-14 22:12:47 +00:00
|
|
|
@Override
|
|
|
|
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
|
|
|
super.onCreateOptionsMenu(menu, inflater);
|
|
|
|
inflater.inflate(R.menu.menu_module, menu);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-13 12:31:43 +00:00
|
|
|
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(), true).execute();
|
2016-09-14 22:12:47 +00:00
|
|
|
Collections.sort(listModules, new CustomComparator());
|
|
|
|
Collections.sort(listModulesCache, new CustomComparator());
|
2016-09-13 12:31:43 +00:00
|
|
|
new updateUI().execute();
|
2016-08-23 16:28:27 +00:00
|
|
|
}
|
|
|
|
|
2016-09-10 04:40:57 +00:00
|
|
|
void selectPage(int pageIndex) {
|
|
|
|
tabLayout.setScrollPosition(pageIndex, 0f, true);
|
2016-09-09 21:49:25 +00:00
|
|
|
viewPager.setCurrentItem(pageIndex);
|
|
|
|
}
|
|
|
|
|
2016-08-22 21:18:28 +00:00
|
|
|
public static class NormalModuleFragment extends BaseModuleFragment {
|
2016-08-21 14:42:50 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected List<Module> listModules() {
|
2016-08-25 10:08:07 +00:00
|
|
|
return listModules;
|
2016-08-21 14:42:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class CacheModuleFragment extends BaseModuleFragment {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected List<Module> listModules() {
|
2016-08-25 10:08:07 +00:00
|
|
|
return listModulesCache;
|
2016-08-21 14:42:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-09-10 04:40:57 +00:00
|
|
|
|
2016-09-08 19:47:04 +00:00
|
|
|
private class updateUI extends AsyncTask<Void, Void, Void> {
|
2016-08-17 11:00:55 +00:00
|
|
|
|
|
|
|
@Override
|
2016-08-22 21:18:28 +00:00
|
|
|
protected Void doInBackground(Void... voids) {
|
|
|
|
return null;
|
2016-08-17 11:00:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-08-22 21:18:28 +00:00
|
|
|
protected void onPostExecute(Void v) {
|
|
|
|
super.onPostExecute(v);
|
2016-08-21 14:08:45 +00:00
|
|
|
progressBar.setVisibility(View.GONE);
|
2016-09-08 19:47:04 +00:00
|
|
|
viewPager.setAdapter(new TabsAdapter(getChildFragmentManager()));
|
2016-08-23 16:28:27 +00:00
|
|
|
tabLayout.setupWithViewPager(viewPager);
|
2016-09-09 21:49:25 +00:00
|
|
|
selectPage(viewPagePosition);
|
|
|
|
|
2016-08-17 11:00:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-21 14:42:50 +00:00
|
|
|
private class TabsAdapter extends FragmentPagerAdapter {
|
|
|
|
|
|
|
|
String[] tabTitles = new String[]{
|
2016-09-11 18:36:58 +00:00
|
|
|
getString(R.string.modules), getString(R.string.cache_modules)
|
2016-08-21 14:42:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
public TabsAdapter(FragmentManager fm) {
|
|
|
|
super(fm);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getCount() {
|
|
|
|
return tabTitles.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getPageTitle(int position) {
|
|
|
|
return tabTitles[position];
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Fragment getItem(int position) {
|
|
|
|
if (position == 0) {
|
2016-09-13 12:31:43 +00:00
|
|
|
NormalModuleFragment nmf = new NormalModuleFragment();
|
|
|
|
nmf.SetDelegate(mTaskDelegate);
|
|
|
|
return nmf;
|
2016-09-10 04:40:57 +00:00
|
|
|
} else {
|
2016-09-13 12:31:43 +00:00
|
|
|
CacheModuleFragment cmf = new CacheModuleFragment();
|
|
|
|
cmf.SetDelegate(mTaskDelegate);
|
|
|
|
return cmf;
|
2016-08-21 14:42:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-14 22:12:47 +00:00
|
|
|
|
2016-09-12 21:47:32 +00:00
|
|
|
public class CustomComparator implements Comparator<Module> {
|
|
|
|
@Override
|
|
|
|
public int compare(Module o1, Module o2) {
|
|
|
|
return o1.getName().compareTo(o2.getName());
|
|
|
|
}
|
|
|
|
}
|
2016-09-01 21:58:26 +00:00
|
|
|
|
2016-08-17 11:00:55 +00:00
|
|
|
}
|