Magisk/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java

162 lines
4.5 KiB
Java
Raw Normal View History

2016-08-22 21:18:28 +00:00
package com.topjohnwu.magisk;
2016-08-17 11:00:55 +00:00
import android.os.AsyncTask;
import android.os.Bundle;
2016-08-21 13:29:42 +00:00
import android.support.annotation.Nullable;
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-08-21 13:29:42 +00:00
import android.view.LayoutInflater;
2016-08-25 10:40:00 +00:00
import android.view.MenuItem;
2016-08-21 13:29:42 +00:00
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
2016-08-20 15:26:49 +00:00
2016-08-22 21:18:28 +00:00
import com.topjohnwu.magisk.module.Module;
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-08-17 11:00:55 +00:00
import java.util.List;
2016-08-22 19:50:46 +00:00
import java.util.concurrent.ExecutionException;
2016-08-17 11:00:55 +00:00
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-08-25 10:40:00 +00:00
private static final String MAGISK_PATH = "/magisk";
private static final String MAGISK_CACHE_PATH = "/cache/magisk";
2016-08-25 10:08:07 +00:00
private static List<Module> listModules = new ArrayList<>();
private static List<Module> listModulesCache = new ArrayList<>();
public static loadModules loadMod;
2016-08-17 11:00:55 +00:00
@BindView(R.id.progressBar) ProgressBar progressBar;
@BindView(R.id.pager) ViewPager viewPager;
@BindView(R.id.tab_layout) TabLayout tabLayout;
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) {
View view = inflater.inflate(R.layout.modules_fragment, container, false);
ButterKnife.bind(this, view);
2016-08-17 11:00:55 +00:00
2016-08-25 10:08:07 +00:00
new updateUI().execute();
2016-08-21 13:29:42 +00:00
setHasOptionsMenu(true);
return view;
2016-08-17 11:00:55 +00:00
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.force_reload:
2016-08-25 10:08:07 +00:00
loadMod = new loadModules();
loadMod.execute();
new updateUI().execute();
break;
}
return super.onOptionsItemSelected(item);
}
2016-08-22 21:18:28 +00:00
public static class NormalModuleFragment extends BaseModuleFragment {
@Override
protected List<Module> listModules() {
2016-08-25 10:08:07 +00:00
return listModules;
}
}
public static class CacheModuleFragment extends BaseModuleFragment {
@Override
protected List<Module> listModules() {
2016-08-25 10:08:07 +00:00
return listModulesCache;
}
}
2016-08-25 10:08:07 +00:00
public static class loadModules extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
listModules.clear();
listModulesCache.clear();
List<String> magisk = Utils.getModList(MAGISK_PATH);
List<String> magiskCache = Utils.getModList(MAGISK_CACHE_PATH);
if (!magisk.isEmpty()) {
for (String mod : magisk) {
listModules.add(new Module(mod));
}
}
if (!magiskCache.isEmpty()) {
for (String mod : magiskCache) {
listModulesCache.add(new Module(mod));
}
}
return null;
}
}
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) {
2016-08-25 10:08:07 +00:00
// Ensure loadMod is done
2016-08-22 19:50:46 +00:00
try {
2016-08-25 10:08:07 +00:00
loadMod.get();
2016-08-22 19:50:46 +00:00
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
2016-08-22 21:18:28 +00:00
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-17 11:00:55 +00:00
progressBar.setVisibility(View.GONE);
viewPager.setAdapter(new TabsAdapter(getChildFragmentManager()));
tabLayout.setupWithViewPager(viewPager);
2016-08-17 11:00:55 +00:00
}
}
private class TabsAdapter extends FragmentPagerAdapter {
String[] tabTitles = new String[]{
2016-08-23 10:39:36 +00:00
getString(R.string.modules), getString(R.string.cache_modules)
};
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-08-22 21:18:28 +00:00
return new NormalModuleFragment();
} else {
return new CacheModuleFragment();
}
}
}
2016-08-17 11:00:55 +00:00
}