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

146 lines
4.8 KiB
Java
Raw Normal View History

2016-08-23 05:18:28 +08:00
package com.topjohnwu.magisk;
2016-08-17 13:00:55 +02:00
import android.Manifest;
2016-12-11 20:38:15 +08:00
import android.app.Activity;
2016-09-23 16:22:11 -05:00
import android.content.Intent;
2016-08-17 13:00:55 +02:00
import android.os.Bundle;
2016-08-21 15:29:42 +02:00
import android.support.annotation.Nullable;
2016-09-18 22:56:12 +08:00
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
2016-08-21 15:29:42 +02:00
import android.view.LayoutInflater;
2017-12-02 22:50:59 +08:00
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
2016-08-21 15:29:42 +02:00
import android.view.View;
import android.view.ViewGroup;
2016-09-18 22:56:12 +08:00
import android.widget.TextView;
2016-08-20 17:26:49 +02:00
2016-11-08 00:09:08 +08:00
import com.topjohnwu.magisk.adapters.ModulesAdapter;
2017-02-12 19:49:46 +08:00
import com.topjohnwu.magisk.asyncs.LoadModules;
2017-02-07 02:01:32 +08:00
import com.topjohnwu.magisk.components.Fragment;
2017-09-30 03:04:23 +08:00
import com.topjohnwu.magisk.container.Module;
2017-11-06 04:41:23 +08:00
import com.topjohnwu.magisk.utils.Const;
2017-12-02 22:50:59 +08:00
import com.topjohnwu.magisk.utils.Shell;
import com.topjohnwu.magisk.utils.Topic;
import com.topjohnwu.magisk.utils.Utils;
2016-08-17 13:00:55 +02:00
2016-08-25 18:40:00 +08:00
import java.util.ArrayList;
2016-08-17 13:00:55 +02:00
import java.util.List;
2016-08-21 15:29:42 +02:00
import butterknife.BindView;
import butterknife.ButterKnife;
2017-08-08 16:09:45 +08:00
import butterknife.OnClick;
import butterknife.Unbinder;
2016-08-21 15:29:42 +02:00
public class ModulesFragment extends Fragment implements Topic.Subscriber {
2016-12-25 15:11:59 +08:00
private Unbinder unbinder;
2016-09-26 10:45:34 +08:00
@BindView(R.id.swipeRefreshLayout) SwipeRefreshLayout mSwipeRefreshLayout;
@BindView(R.id.recyclerView) RecyclerView recyclerView;
2017-01-28 01:10:50 +08:00
@BindView(R.id.empty_rv) TextView emptyRv;
2017-08-08 16:09:45 +08:00
@OnClick(R.id.fab)
public void selectFile() {
Utils.runWithPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE, () -> {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/zip");
2017-11-06 04:41:23 +08:00
startActivityForResult(intent, Const.ID.FETCH_ZIP);
});
2017-08-08 16:09:45 +08:00
}
2016-09-26 10:45:34 +08:00
2016-09-29 01:42:25 +08:00
private List<Module> listModules = new ArrayList<>();
2016-08-21 15:29:42 +02:00
@Nullable
2016-08-17 13:00:55 +02:00
@Override
2016-08-21 15:29:42 +02:00
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
2017-01-25 17:07:23 +08:00
View view = inflater.inflate(R.layout.fragment_modules, container, false);
unbinder = ButterKnife.bind(this, view);
2017-12-02 22:50:59 +08:00
setHasOptionsMenu(true);
2016-09-26 10:45:34 +08:00
mSwipeRefreshLayout.setOnRefreshListener(() -> {
2016-09-18 22:56:12 +08:00
recyclerView.setVisibility(View.GONE);
2017-10-16 00:54:48 +08:00
new LoadModules().exec();
2016-09-18 22:56:12 +08:00
});
2016-09-15 12:52:58 -05:00
2016-12-30 04:03:38 +08:00
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
mSwipeRefreshLayout.setEnabled(recyclerView.getChildAt(0).getTop() >= 0);
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
});
2017-07-23 00:12:15 +08:00
getActivity().setTitle(R.string.modules);
2016-09-27 22:57:20 +08:00
return view;
}
2016-12-25 15:11:59 +08:00
@Override
2017-10-07 17:12:36 +08:00
public void onTopicPublished(Topic topic, Object result) {
2016-12-25 15:11:59 +08:00
updateUI();
}
2017-07-23 00:12:15 +08:00
@Override
public Topic[] getSubscription() {
return new Topic[] { getApplication().moduleLoadDone };
2017-07-23 00:12:15 +08:00
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
2017-11-06 04:41:23 +08:00
if (requestCode == Const.ID.FETCH_ZIP && resultCode == Activity.RESULT_OK && data != null) {
// Get the URI of the selected file
2017-07-18 00:59:22 +08:00
Intent intent = new Intent(getActivity(), FlashActivity.class);
2017-11-06 04:41:23 +08:00
intent.setData(data.getData()).putExtra(Const.Key.FLASH_ACTION, Const.Value.FLASH_ZIP);
2017-07-18 00:59:22 +08:00
startActivity(intent);
}
2016-09-27 22:57:20 +08:00
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
2017-12-02 22:50:59 +08:00
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_reboot, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.reboot:
Shell.su_raw("/system/bin/reboot");
return true;
case R.id.reboot_recovery:
Shell.su_raw("/system/bin/reboot recovery");
return true;
case R.id.reboot_bootloader:
Shell.su_raw("/system/bin/reboot bootloader");
return true;
case R.id.reboot_download:
Shell.su_raw("/system/bin/reboot download");
return true;
default:
return false;
}
}
2016-09-27 22:57:20 +08:00
private void updateUI() {
2017-02-07 02:01:32 +08:00
listModules.clear();
listModules.addAll(getApplication().moduleMap.values());
2016-09-27 22:57:20 +08:00
if (listModules.size() == 0) {
2017-01-28 01:10:50 +08:00
emptyRv.setVisibility(View.VISIBLE);
2016-09-27 22:57:20 +08:00
recyclerView.setVisibility(View.GONE);
} else {
2017-01-28 01:10:50 +08:00
emptyRv.setVisibility(View.GONE);
2016-09-27 22:57:20 +08:00
recyclerView.setVisibility(View.VISIBLE);
recyclerView.setAdapter(new ModulesAdapter(listModules));
}
2016-09-27 22:57:20 +08:00
mSwipeRefreshLayout.setRefreshing(false);
}
2016-08-17 13:00:55 +02:00
}