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

340 lines
12 KiB
Java
Raw Normal View History

2016-12-25 03:05:22 +08:00
package com.topjohnwu.magisk;
2017-07-20 01:44:32 +08:00
import android.app.NotificationManager;
import android.content.Context;
2016-12-25 03:05:22 +08:00
import android.os.Bundle;
import android.support.annotation.Nullable;
2017-05-21 12:16:38 +08:00
import android.support.design.widget.Snackbar;
2016-12-25 03:05:22 +08:00
import android.support.v4.widget.SwipeRefreshLayout;
2017-05-21 12:16:38 +08:00
import android.support.v7.widget.CardView;
2016-12-25 03:05:22 +08:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
2017-05-21 12:16:38 +08:00
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
2016-12-25 03:05:22 +08:00
import android.widget.ImageView;
2017-05-20 03:04:14 +08:00
import android.widget.LinearLayout;
2016-12-25 03:05:22 +08:00
import android.widget.ProgressBar;
2017-05-21 12:16:38 +08:00
import android.widget.Spinner;
2016-12-25 03:05:22 +08:00
import android.widget.TextView;
2017-02-12 19:49:46 +08:00
import com.topjohnwu.magisk.asyncs.CheckUpdates;
import com.topjohnwu.magisk.components.ExpandableView;
2017-02-07 02:01:32 +08:00
import com.topjohnwu.magisk.components.Fragment;
2017-05-21 12:16:38 +08:00
import com.topjohnwu.magisk.components.SnackbarMaker;
2016-12-25 03:05:22 +08:00
import com.topjohnwu.magisk.utils.Shell;
import com.topjohnwu.magisk.utils.Topic;
2017-01-10 22:30:05 +08:00
import com.topjohnwu.magisk.utils.Utils;
2016-12-25 03:05:22 +08:00
2017-05-21 12:16:38 +08:00
import java.util.ArrayList;
import java.util.List;
2016-12-25 03:05:22 +08:00
import butterknife.BindColor;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
2016-12-25 03:05:22 +08:00
2017-05-24 00:37:15 +08:00
public class MagiskFragment extends Fragment
implements Topic.Subscriber, SwipeRefreshLayout.OnRefreshListener, ExpandableView {
2016-12-25 03:05:22 +08:00
private Container expandableContainer = new Container();
2017-01-07 02:46:50 +08:00
2017-09-03 15:35:14 +08:00
private MagiskManager mm;
private Unbinder unbinder;
2017-08-31 03:07:33 +08:00
private static boolean shownDialog = false;
2017-05-21 12:16:38 +08:00
2016-12-25 03:05:22 +08:00
@BindView(R.id.swipeRefreshLayout) SwipeRefreshLayout mSwipeRefreshLayout;
2017-05-23 16:51:23 +08:00
@BindView(R.id.magisk_update_card) CardView magiskUpdateCard;
2017-05-19 08:37:57 -07:00
@BindView(R.id.magisk_update_icon) ImageView magiskUpdateIcon;
2016-12-25 03:05:22 +08:00
@BindView(R.id.magisk_update_status) TextView magiskUpdateText;
2017-05-23 16:51:23 +08:00
@BindView(R.id.magisk_update_progress) ProgressBar magiskUpdateProgress;
2016-12-25 03:05:22 +08:00
2017-05-19 08:37:57 -07:00
@BindView(R.id.magisk_status_icon) ImageView magiskStatusIcon;
@BindView(R.id.magisk_version) TextView magiskVersionText;
2016-12-25 03:05:22 +08:00
@BindView(R.id.root_status_icon) ImageView rootStatusIcon;
@BindView(R.id.root_status) TextView rootStatusText;
2017-05-23 16:51:23 +08:00
@BindView(R.id.safetyNet_card) CardView safetyNetCard;
2017-05-20 03:04:14 +08:00
@BindView(R.id.safetyNet_refresh) ImageView safetyNetRefreshIcon;
2016-12-25 03:05:22 +08:00
@BindView(R.id.safetyNet_status) TextView safetyNetStatusText;
@BindView(R.id.safetyNet_check_progress) ProgressBar safetyNetProgress;
2017-05-20 03:04:14 +08:00
@BindView(R.id.expand_layout) LinearLayout expandLayout;
@BindView(R.id.cts_status_icon) ImageView ctsStatusIcon;
@BindView(R.id.cts_status) TextView ctsStatusText;
@BindView(R.id.basic_status_icon) ImageView basicStatusIcon;
@BindView(R.id.basic_status) TextView basicStatusText;
2016-12-25 03:05:22 +08:00
2017-05-21 12:16:38 +08:00
@BindView(R.id.bootimage_card) CardView bootImageCard;
@BindView(R.id.block_spinner) Spinner spinner;
@BindView(R.id.detect_bootimage) Button detectButton;
@BindView(R.id.install_option_card) CardView installOptionCard;
@BindView(R.id.keep_force_enc) CheckBox keepEncChkbox;
@BindView(R.id.keep_verity) CheckBox keepVerityChkbox;
@BindView(R.id.install_button) CardView installButton;
@BindView(R.id.install_text) TextView installText;
@BindView(R.id.uninstall_button) CardView uninstallButton;
2016-12-25 03:05:22 +08:00
@BindColor(R.color.red500) int colorBad;
@BindColor(R.color.green500) int colorOK;
@BindColor(R.color.yellow500) int colorWarn;
@BindColor(R.color.grey500) int colorNeutral;
@BindColor(R.color.blue500) int colorInfo;
2017-05-20 03:04:14 +08:00
@OnClick(R.id.safetyNet_title)
2017-09-05 17:43:13 +08:00
void safetyNet() {
safetyNetProgress.setVisibility(View.VISIBLE);
2017-05-20 03:04:14 +08:00
safetyNetRefreshIcon.setVisibility(View.GONE);
safetyNetStatusText.setText(R.string.checking_safetyNet_status);
2017-05-20 03:04:14 +08:00
Utils.checkSafetyNet(getActivity());
collapse();
}
2017-05-21 12:16:38 +08:00
@OnClick(R.id.install_button)
2017-09-05 17:43:13 +08:00
void install() {
2017-08-31 03:07:33 +08:00
shownDialog = true;
2017-09-03 14:58:21 +08:00
// Show Manager update first
2017-09-03 15:35:14 +08:00
if (mm.remoteManagerVersionCode > BuildConfig.VERSION_CODE) {
2017-09-05 17:43:13 +08:00
Utils.showManagerInstallDialog(getActivity());
2017-09-03 14:58:21 +08:00
return;
}
2017-09-05 17:43:13 +08:00
((NotificationManager) mm.getSystemService(Context.NOTIFICATION_SERVICE)).cancelAll();
Utils.showMagiskInstallDialog(this,
keepEncChkbox.isChecked(), keepVerityChkbox.isChecked());
2017-05-21 12:16:38 +08:00
}
@OnClick(R.id.uninstall_button)
2017-09-05 17:43:13 +08:00
void uninstall() {
Utils.showUninstallDialog(this);
2017-05-21 12:16:38 +08:00
}
2016-12-25 03:05:22 +08:00
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
2017-05-21 12:16:38 +08:00
View v = inflater.inflate(R.layout.fragment_magisk, container, false);
unbinder = ButterKnife.bind(this, v);
2017-08-30 02:28:24 +08:00
getActivity().setTitle(R.string.magisk);
2017-09-03 15:35:14 +08:00
mm = getApplication();
2016-12-25 03:05:22 +08:00
expandableContainer.expandLayout = expandLayout;
setupExpandable();
2017-01-07 01:19:18 +08:00
2017-05-24 00:37:15 +08:00
mSwipeRefreshLayout.setOnRefreshListener(this);
2017-05-23 16:51:23 +08:00
updateUI();
2017-02-07 04:09:49 +08:00
2016-12-25 03:05:22 +08:00
return v;
}
2017-05-24 00:37:15 +08:00
@Override
public void onRefresh() {
2017-09-03 15:35:14 +08:00
mm.getMagiskInfo();
2017-05-24 00:37:15 +08:00
updateUI();
magiskUpdateText.setText(R.string.checking_for_updates);
magiskUpdateProgress.setVisibility(View.VISIBLE);
magiskUpdateIcon.setVisibility(View.GONE);
safetyNetStatusText.setText(R.string.safetyNet_check_text);
2017-09-03 15:35:14 +08:00
mm.safetyNetDone.hasPublished = false;
mm.updateCheckDone.hasPublished = false;
mm.remoteMagiskVersionString = null;
mm.remoteMagiskVersionCode = -1;
2017-05-24 00:37:15 +08:00
collapse();
2017-08-31 03:07:33 +08:00
shownDialog = false;
2017-05-24 00:37:15 +08:00
// Trigger state check
2017-09-03 15:35:14 +08:00
if (Utils.checkNetworkStatus(mm)) {
2017-05-24 00:37:15 +08:00
new CheckUpdates(getActivity()).exec();
} else {
mSwipeRefreshLayout.setRefreshing(false);
}
}
2016-12-25 15:11:59 +08:00
@Override
public void onTopicPublished(Topic topic) {
2017-09-03 15:35:14 +08:00
if (topic == mm.updateCheckDone) {
2016-12-25 15:11:59 +08:00
updateCheckUI();
2017-09-03 15:35:14 +08:00
} else if (topic == mm.safetyNetDone) {
2016-12-25 15:11:59 +08:00
updateSafetyNetUI();
}
}
2016-12-25 03:05:22 +08:00
@Override
public Topic[] getSubscription() {
2017-09-03 15:35:14 +08:00
return new Topic[] { mm.updateCheckDone, mm.safetyNetDone };
2016-12-25 03:05:22 +08:00
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
@Override
public Container getContainer() {
return expandableContainer;
}
2017-09-05 17:43:13 +08:00
public String getSelectedBootImage() {
if (Shell.rootAccess()) {
if (mm.bootBlock != null) {
return mm.bootBlock;
} else {
int idx = spinner.getSelectedItemPosition();
if (idx > 0) {
return mm.blockList.get(idx - 1);
} else {
SnackbarMaker.make(getActivity(),
R.string.manual_boot_image, Snackbar.LENGTH_LONG).show();
return null;
}
}
} else {
return null;
}
}
2017-05-23 16:51:23 +08:00
private void updateUI() {
((MainActivity) getActivity()).checkHideSection();
2017-07-18 03:34:06 +08:00
2017-09-03 02:34:23 +08:00
boolean hasNetwork = Utils.checkNetworkStatus(getActivity());
boolean hasRoot = Shell.rootAccess();
2017-09-03 15:35:14 +08:00
boolean isUpToDate = mm.magiskVersionCode > 1300;
2017-09-03 02:34:23 +08:00
magiskUpdateCard.setVisibility(hasNetwork ? View.VISIBLE : View.GONE);
safetyNetCard.setVisibility(hasNetwork ? View.VISIBLE : View.GONE);
bootImageCard.setVisibility(hasNetwork && hasRoot ? View.VISIBLE : View.GONE);
installOptionCard.setVisibility(hasNetwork ? View.VISIBLE : View.GONE);
uninstallButton.setVisibility(isUpToDate && hasRoot ? View.VISIBLE : View.GONE);
2017-05-23 16:51:23 +08:00
2016-12-25 22:36:51 +08:00
int image, color;
2017-09-03 15:35:14 +08:00
if (mm.magiskVersionCode < 0) {
2017-05-19 08:37:57 -07:00
color = colorBad;
image = R.drawable.ic_cancel;
2016-12-25 22:36:51 +08:00
magiskVersionText.setText(R.string.magisk_version_error);
} else {
2017-05-19 08:37:57 -07:00
color = colorOK;
image = R.drawable.ic_check_circle;
2017-09-03 15:35:14 +08:00
magiskVersionText.setText(getString(R.string.current_magisk_title, "v" + mm.magiskVersionString));
2016-12-25 03:05:22 +08:00
}
2017-05-19 08:37:57 -07:00
magiskStatusIcon.setImageResource(image);
magiskStatusIcon.setColorFilter(color);
2017-01-26 13:46:54 +08:00
switch (Shell.rootStatus) {
case 0:
2016-12-25 03:05:22 +08:00
color = colorBad;
image = R.drawable.ic_cancel;
rootStatusText.setText(R.string.not_rooted);
2017-01-26 13:46:54 +08:00
break;
case 1:
2017-09-03 15:35:14 +08:00
if (mm.suVersion != null) {
2017-01-26 13:46:54 +08:00
color = colorOK;
image = R.drawable.ic_check_circle;
2017-09-03 15:35:14 +08:00
rootStatusText.setText(mm.suVersion);
2017-01-26 13:46:54 +08:00
break;
}
case -1:
default:
2016-12-25 03:05:22 +08:00
color = colorNeutral;
image = R.drawable.ic_help;
rootStatusText.setText(R.string.root_error);
}
2017-05-19 08:37:57 -07:00
2016-12-25 03:05:22 +08:00
rootStatusIcon.setImageResource(image);
2017-05-19 08:37:57 -07:00
rootStatusIcon.setColorFilter(color);
2017-07-18 03:34:06 +08:00
2017-09-03 00:17:42 +08:00
List<String> items = new ArrayList<>();
2017-09-03 15:35:14 +08:00
if (mm.bootBlock != null) {
items.add(getString(R.string.auto_detect, mm.bootBlock));
2017-09-03 00:17:42 +08:00
spinner.setEnabled(false);
2017-07-18 03:34:06 +08:00
} else {
2017-09-03 00:17:42 +08:00
items.add(getString(R.string.cannot_auto_detect));
2017-09-16 02:41:24 +08:00
if (mm.blockList != null)
items.addAll(mm.blockList);
2017-07-18 03:34:06 +08:00
}
2017-09-03 00:17:42 +08:00
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
2016-12-25 03:05:22 +08:00
}
private void updateCheckUI() {
int image, color;
2017-09-03 15:35:14 +08:00
if (mm.remoteMagiskVersionCode < 0) {
2016-12-25 03:05:22 +08:00
color = colorNeutral;
image = R.drawable.ic_help;
magiskUpdateText.setText(R.string.cannot_check_updates);
} else {
color = colorOK;
image = R.drawable.ic_check_circle;
2017-09-03 15:35:14 +08:00
magiskUpdateText.setText(getString(R.string.install_magisk_title, "v" + mm.remoteMagiskVersionString));
2016-12-25 03:05:22 +08:00
}
2017-02-05 04:40:52 +08:00
installButton.setVisibility(View.VISIBLE);
2017-09-03 15:35:14 +08:00
if (mm.remoteManagerVersionCode > BuildConfig.VERSION_CODE) {
2017-09-03 14:58:21 +08:00
installText.setText(getString(R.string.update, getString(R.string.app_name)));
2017-09-03 15:35:14 +08:00
} else if (mm.magiskVersionCode > 0 && mm.remoteMagiskVersionCode > mm.magiskVersionCode) {
2017-09-03 14:58:21 +08:00
installText.setText(getString(R.string.update, getString(R.string.magisk)));
2017-09-03 00:17:42 +08:00
} else {
installText.setText(R.string.install);
}
2017-09-03 15:35:14 +08:00
if (!shownDialog && (mm.remoteMagiskVersionCode > mm.magiskVersionCode
|| mm.remoteManagerVersionCode > BuildConfig.VERSION_CODE)) {
2017-09-03 14:58:21 +08:00
install();
}
2017-05-19 08:37:57 -07:00
magiskUpdateIcon.setImageResource(image);
magiskUpdateIcon.setColorFilter(color);
magiskUpdateIcon.setVisibility(View.VISIBLE);
2016-12-25 03:05:22 +08:00
2017-05-23 16:51:23 +08:00
magiskUpdateProgress.setVisibility(View.GONE);
2016-12-25 03:05:22 +08:00
mSwipeRefreshLayout.setRefreshing(false);
}
private void updateSafetyNetUI() {
int image, color;
safetyNetProgress.setVisibility(View.GONE);
2017-05-20 03:04:14 +08:00
safetyNetRefreshIcon.setVisibility(View.VISIBLE);
2017-09-03 15:35:14 +08:00
if (mm.SNCheckResult.failed) {
safetyNetStatusText.setText(mm.SNCheckResult.errmsg);
2017-05-20 03:04:14 +08:00
collapse();
} else {
safetyNetStatusText.setText(R.string.safetyNet_check_success);
2017-09-03 15:35:14 +08:00
if (mm.SNCheckResult.ctsProfile) {
2017-05-20 03:04:14 +08:00
color = colorOK;
image = R.drawable.ic_check_circle;
} else {
2016-12-25 03:05:22 +08:00
color = colorBad;
image = R.drawable.ic_cancel;
2017-05-20 03:04:14 +08:00
}
2017-09-03 15:35:14 +08:00
ctsStatusText.setText("ctsProfile: " + mm.SNCheckResult.ctsProfile);
2017-05-20 03:04:14 +08:00
ctsStatusIcon.setImageResource(image);
ctsStatusIcon.setColorFilter(color);
2017-09-03 15:35:14 +08:00
if (mm.SNCheckResult.basicIntegrity) {
2016-12-25 03:05:22 +08:00
color = colorOK;
image = R.drawable.ic_check_circle;
2017-05-20 03:04:14 +08:00
} else {
color = colorBad;
image = R.drawable.ic_cancel;
}
2017-09-03 15:35:14 +08:00
basicStatusText.setText("basicIntegrity: " + mm.SNCheckResult.basicIntegrity);
2017-05-20 03:04:14 +08:00
basicStatusIcon.setImageResource(image);
basicStatusIcon.setColorFilter(color);
expand();
2016-12-25 03:05:22 +08:00
}
2017-05-20 03:04:14 +08:00
}
2016-12-25 03:05:22 +08:00
}