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

496 lines
20 KiB
Java
Raw Normal View History

2016-12-24 20:05:22 +01:00
package com.topjohnwu.magisk;
2017-05-19 21:04:14 +02:00
import android.animation.Animator;
import android.animation.ValueAnimator;
2017-07-19 19:44:32 +02:00
import android.app.NotificationManager;
2017-05-21 06:16:38 +02:00
import android.app.ProgressDialog;
2017-07-19 19:44:32 +02:00
import android.content.Context;
2017-05-21 06:16:38 +02:00
import android.content.Intent;
import android.net.Uri;
2016-12-24 20:05:22 +01:00
import android.os.Bundle;
2017-05-21 06:16:38 +02:00
import android.os.CountDownTimer;
2016-12-24 20:05:22 +01:00
import android.support.annotation.Nullable;
2017-05-21 06:16:38 +02:00
import android.support.design.widget.Snackbar;
2016-12-24 20:05:22 +01:00
import android.support.v4.widget.SwipeRefreshLayout;
2017-05-21 06:16:38 +02:00
import android.support.v7.widget.CardView;
2016-12-24 20:05:22 +01:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
2017-05-19 21:04:14 +02:00
import android.view.ViewTreeObserver;
2017-05-21 06:16:38 +02:00
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
2016-12-24 20:05:22 +01:00
import android.widget.ImageView;
2017-05-19 21:04:14 +02:00
import android.widget.LinearLayout;
2016-12-24 20:05:22 +01:00
import android.widget.ProgressBar;
2017-05-21 06:16:38 +02:00
import android.widget.Spinner;
2016-12-24 20:05:22 +01:00
import android.widget.TextView;
2017-02-12 12:49:46 +01:00
import com.topjohnwu.magisk.asyncs.CheckUpdates;
2017-02-14 22:24:02 +01:00
import com.topjohnwu.magisk.components.AlertDialogBuilder;
2017-02-06 19:01:32 +01:00
import com.topjohnwu.magisk.components.Fragment;
2017-05-21 06:16:38 +02:00
import com.topjohnwu.magisk.components.SnackbarMaker;
import com.topjohnwu.magisk.receivers.DownloadReceiver;
2016-12-24 20:05:22 +01:00
import com.topjohnwu.magisk.utils.Shell;
import com.topjohnwu.magisk.utils.Topic;
2017-01-10 15:30:05 +01:00
import com.topjohnwu.magisk.utils.Utils;
2016-12-24 20:05:22 +01:00
2017-05-21 06:16:38 +02:00
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
2016-12-24 20:05:22 +01:00
import butterknife.BindColor;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
2016-12-24 20:05:22 +01:00
2017-05-23 18:37:15 +02:00
public class MagiskFragment extends Fragment
implements Topic.Subscriber, SwipeRefreshLayout.OnRefreshListener {
2016-12-24 20:05:22 +01:00
2017-07-19 19:44:32 +02:00
public static final String SHOW_DIALOG = "dialog";
2017-08-21 21:01:54 +02:00
private static final String UNINSTALLER = "magisk_uninstaller.sh";
private static final String UTIL_FUNCTIONS= "util_functions.sh";
2017-05-21 06:16:38 +02:00
private static int expandHeight = 0;
private static boolean mExpanded = false;
2017-01-06 19:46:50 +01:00
2017-05-21 06:16:38 +02:00
private MagiskManager magiskManager;
private Unbinder unbinder;
2017-05-21 06:16:38 +02:00
2016-12-24 20:05:22 +01:00
@BindView(R.id.swipeRefreshLayout) SwipeRefreshLayout mSwipeRefreshLayout;
2017-05-23 10:51:23 +02:00
@BindView(R.id.magisk_update_card) CardView magiskUpdateCard;
2017-05-19 17:37:57 +02:00
@BindView(R.id.magisk_update_icon) ImageView magiskUpdateIcon;
2016-12-24 20:05:22 +01:00
@BindView(R.id.magisk_update_status) TextView magiskUpdateText;
2017-05-23 10:51:23 +02:00
@BindView(R.id.magisk_update_progress) ProgressBar magiskUpdateProgress;
2016-12-24 20:05:22 +01:00
2017-05-19 17:37:57 +02:00
@BindView(R.id.magisk_status_icon) ImageView magiskStatusIcon;
@BindView(R.id.magisk_version) TextView magiskVersionText;
2016-12-24 20:05:22 +01:00
@BindView(R.id.root_status_icon) ImageView rootStatusIcon;
@BindView(R.id.root_status) TextView rootStatusText;
2017-05-23 10:51:23 +02:00
@BindView(R.id.safetyNet_card) CardView safetyNetCard;
2017-05-19 21:04:14 +02:00
@BindView(R.id.safetyNet_refresh) ImageView safetyNetRefreshIcon;
2016-12-24 20:05:22 +01:00
@BindView(R.id.safetyNet_status) TextView safetyNetStatusText;
@BindView(R.id.safetyNet_check_progress) ProgressBar safetyNetProgress;
2017-05-19 21:04:14 +02: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-24 20:05:22 +01:00
2017-05-21 06:16:38 +02: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-24 20:05:22 +01: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-19 21:04:14 +02:00
@OnClick(R.id.safetyNet_title)
public void safetyNet() {
safetyNetProgress.setVisibility(View.VISIBLE);
2017-05-19 21:04:14 +02:00
safetyNetRefreshIcon.setVisibility(View.GONE);
safetyNetStatusText.setText(R.string.checking_safetyNet_status);
2017-05-19 21:04:14 +02:00
Utils.checkSafetyNet(getActivity());
collapse();
}
2017-05-21 06:16:38 +02:00
@OnClick(R.id.install_button)
public void install() {
String bootImage = null;
2017-07-17 21:34:06 +02:00
if (Shell.rootAccess()) {
2017-05-21 06:16:38 +02:00
if (magiskManager.bootBlock != null) {
bootImage = magiskManager.bootBlock;
} else {
2017-07-17 21:34:06 +02:00
int idx = spinner.getSelectedItemPosition();
2017-05-21 06:16:38 +02:00
if (idx > 0) {
bootImage = magiskManager.blockList.get(idx - 1);
} else {
2017-07-17 21:34:06 +02:00
SnackbarMaker.make(getActivity(), R.string.manual_boot_image, Snackbar.LENGTH_LONG).show();
2017-05-21 06:16:38 +02:00
return;
}
}
}
final String finalBootImage = bootImage;
String filename = "Magisk-v" + magiskManager.remoteMagiskVersionString + ".zip";
new AlertDialogBuilder(getActivity())
.setTitle(getString(R.string.repo_install_title, getString(R.string.magisk)))
.setMessage(getString(R.string.repo_install_msg, filename))
.setCancelable(true)
.setPositiveButton(Shell.rootAccess() ? R.string.install : R.string.download,
2017-07-19 19:44:32 +02:00
(d, i) -> {
((NotificationManager) getActivity()
.getSystemService(Context.NOTIFICATION_SERVICE)).cancelAll();
Utils.dlAndReceive(
getActivity(),
new DownloadReceiver() {
private String boot = finalBootImage;
private boolean enc = keepEncChkbox.isChecked();
private boolean verity = keepVerityChkbox.isChecked();
@Override
public void onDownloadDone(Uri uri) {
if (Shell.rootAccess()) {
2017-08-03 17:33:08 +02:00
Shell.getShell(getActivity()).su_raw(
2017-07-19 19:44:32 +02:00
"rm -f /dev/.magisk",
"echo \"BOOTIMAGE=" + boot + "\" >> /dev/.magisk",
"echo \"KEEPFORCEENCRYPT=" + String.valueOf(enc) + "\" >> /dev/.magisk",
"echo \"KEEPVERITY=" + String.valueOf(verity) + "\" >> /dev/.magisk"
);
startActivity(new Intent(getActivity(), FlashActivity.class).setData(uri));
} else {
Utils.showUriSnack(getActivity(), uri);
}
2017-07-17 21:34:06 +02:00
}
2017-07-19 19:44:32 +02:00
},
magiskManager.magiskLink,
Utils.getLegalFilename(filename));
}
)
2017-07-17 21:34:06 +02:00
.setNeutralButton(R.string.release_notes, (d, i) -> {
2017-05-21 06:16:38 +02:00
if (magiskManager.releaseNoteLink != null) {
Intent openReleaseNoteLink = new Intent(Intent.ACTION_VIEW, Uri.parse(magiskManager.releaseNoteLink));
openReleaseNoteLink.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
magiskManager.startActivity(openReleaseNoteLink);
}
})
.setNegativeButton(R.string.no_thanks, null)
.show();
}
@OnClick(R.id.uninstall_button)
public void uninstall() {
new AlertDialogBuilder(getActivity())
.setTitle(R.string.uninstall_magisk_title)
.setMessage(R.string.uninstall_magisk_msg)
.setPositiveButton(R.string.yes, (dialogInterface, i) -> {
try {
2017-08-21 21:01:54 +02:00
InputStream in = magiskManager.getAssets().open(UNINSTALLER);
File uninstaller = new File(magiskManager.getCacheDir(), UNINSTALLER);
2017-05-21 06:16:38 +02:00
FileOutputStream out = new FileOutputStream(uninstaller);
byte[] bytes = new byte[1024];
int read;
while ((read = in.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
in.close();
out.close();
2017-08-21 21:01:54 +02:00
in = magiskManager.getAssets().open(UTIL_FUNCTIONS);
File utils = new File(magiskManager.getCacheDir(), UTIL_FUNCTIONS);
2017-07-10 18:50:27 +02:00
out = new FileOutputStream(utils);
while ((read = in.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
in.close();
out.close();
2017-05-21 06:16:38 +02:00
ProgressDialog progress = new ProgressDialog(getActivity());
progress.setTitle(R.string.reboot);
progress.show();
new CountDownTimer(5000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
progress.setMessage(getString(R.string.reboot_countdown, millisUntilFinished / 1000));
}
@Override
public void onFinish() {
progress.setMessage(getString(R.string.reboot_countdown, 0));
2017-08-03 17:33:08 +02:00
Shell.getShell(getActivity()).su_raw(
2017-08-21 21:01:54 +02:00
"mv -f " + uninstaller + " /cache/" + UNINSTALLER,
"mv -f " + utils + " /data/magisk/" + UTIL_FUNCTIONS,
2017-05-21 06:16:38 +02:00
"reboot"
);
}
}.start();
} catch (IOException e) {
e.printStackTrace();
}
})
.setNegativeButton(R.string.no_thanks, null)
.show();
}
2016-12-24 20:05:22 +01:00
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
2017-05-21 06:16:38 +02:00
View v = inflater.inflate(R.layout.fragment_magisk, container, false);
unbinder = ButterKnife.bind(this, v);
2017-05-19 17:37:57 +02:00
magiskManager = getApplication();
2016-12-24 20:05:22 +01:00
2017-05-19 21:04:14 +02:00
expandLayout.getViewTreeObserver().addOnPreDrawListener(
new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
if (expandHeight == 0) {
final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
expandLayout.measure(widthSpec, heightSpec);
expandHeight = expandLayout.getMeasuredHeight();
}
expandLayout.getViewTreeObserver().removeOnPreDrawListener(this);
setExpanded();
return true;
}
});
2017-01-06 18:19:18 +01:00
2017-05-23 18:37:15 +02:00
mSwipeRefreshLayout.setOnRefreshListener(this);
2016-12-24 20:05:22 +01:00
2017-05-23 10:51:23 +02:00
updateUI();
2017-02-06 21:09:49 +01:00
2017-07-19 19:44:32 +02:00
if (getArguments() != null && getArguments().getBoolean(SHOW_DIALOG))
install();
2017-07-22 18:12:15 +02:00
getActivity().setTitle(R.string.magisk);
2016-12-24 20:05:22 +01:00
return v;
}
2017-05-23 18:37:15 +02:00
@Override
public void onRefresh() {
2017-08-11 19:31:34 +02:00
magiskManager.getMagiskInfo();
2017-05-23 18:37:15 +02:00
updateUI();
magiskUpdateText.setText(R.string.checking_for_updates);
magiskUpdateProgress.setVisibility(View.VISIBLE);
magiskUpdateIcon.setVisibility(View.GONE);
safetyNetStatusText.setText(R.string.safetyNet_check_text);
magiskManager.safetyNetDone.hasPublished = false;
magiskManager.updateCheckDone.hasPublished = false;
2017-05-23 18:37:15 +02:00
magiskManager.remoteMagiskVersionString = null;
magiskManager.remoteMagiskVersionCode = -1;
collapse();
// Trigger state check
if (Utils.checkNetworkStatus(magiskManager)) {
new CheckUpdates(getActivity()).exec();
} else {
mSwipeRefreshLayout.setRefreshing(false);
}
}
2016-12-25 08:11:59 +01:00
@Override
public void onTopicPublished(Topic topic) {
if (topic == magiskManager.updateCheckDone) {
2016-12-25 08:11:59 +01:00
updateCheckUI();
} else if (topic == magiskManager.safetyNetDone) {
2016-12-25 08:11:59 +01:00
updateSafetyNetUI();
}
}
2016-12-24 20:05:22 +01:00
@Override
public Topic[] getSubscription() {
return new Topic[] { magiskManager.updateCheckDone, magiskManager.safetyNetDone };
2016-12-24 20:05:22 +01:00
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
2017-05-23 10:51:23 +02:00
private void updateUI() {
((MainActivity) getActivity()).checkHideSection();
2017-07-17 21:34:06 +02:00
2017-05-23 10:51:23 +02:00
final int ROOT = 0x1, NETWORK = 0x2, UPTODATE = 0x4;
int status = 0;
status |= Shell.rootAccess() ? ROOT : 0;
status |= Utils.checkNetworkStatus(magiskManager) ? NETWORK : 0;
status |= magiskManager.magiskVersionCode >= 130 ? UPTODATE : 0;
magiskUpdateCard.setVisibility(Utils.checkBits(status, NETWORK) ? View.VISIBLE : View.GONE);
safetyNetCard.setVisibility(Utils.checkBits(status, NETWORK) ? View.VISIBLE : View.GONE);
bootImageCard.setVisibility(Utils.checkBits(status, NETWORK, ROOT) ? View.VISIBLE : View.GONE);
installOptionCard.setVisibility(Utils.checkBits(status, NETWORK, ROOT) ? View.VISIBLE : View.GONE);
installButton.setVisibility(Utils.checkBits(status, NETWORK) ? View.VISIBLE : View.GONE);
uninstallButton.setVisibility(Utils.checkBits(status, UPTODATE, ROOT) ? View.VISIBLE : View.GONE);
2016-12-25 15:36:51 +01:00
int image, color;
2017-05-19 17:37:57 +02:00
if (magiskManager.magiskVersionCode < 0) {
color = colorBad;
image = R.drawable.ic_cancel;
2016-12-25 15:36:51 +01:00
magiskVersionText.setText(R.string.magisk_version_error);
} else {
2017-05-19 17:37:57 +02:00
color = colorOK;
image = R.drawable.ic_check_circle;
magiskVersionText.setText(getString(R.string.current_magisk_title, "v" + magiskManager.magiskVersionString));
2016-12-24 20:05:22 +01:00
}
2017-05-19 17:37:57 +02:00
magiskStatusIcon.setImageResource(image);
magiskStatusIcon.setColorFilter(color);
2017-01-26 06:46:54 +01:00
switch (Shell.rootStatus) {
case 0:
2016-12-24 20:05:22 +01:00
color = colorBad;
image = R.drawable.ic_cancel;
rootStatusText.setText(R.string.not_rooted);
2017-01-26 06:46:54 +01:00
break;
case 1:
2017-05-19 17:37:57 +02:00
if (magiskManager.suVersion != null) {
2017-01-26 06:46:54 +01:00
color = colorOK;
image = R.drawable.ic_check_circle;
2017-05-19 17:37:57 +02:00
rootStatusText.setText(magiskManager.suVersion);
2017-01-26 06:46:54 +01:00
break;
}
case -1:
default:
2016-12-24 20:05:22 +01:00
color = colorNeutral;
image = R.drawable.ic_help;
rootStatusText.setText(R.string.root_error);
}
2017-05-19 17:37:57 +02:00
2016-12-24 20:05:22 +01:00
rootStatusIcon.setImageResource(image);
2017-05-19 17:37:57 +02:00
rootStatusIcon.setColorFilter(color);
2017-07-17 21:34:06 +02:00
if (!Shell.rootAccess()) {
installText.setText(R.string.download);
} else {
2017-07-19 20:54:34 +02:00
if (magiskManager.remoteMagiskVersionCode > magiskManager.magiskVersionCode) {
installText.setText(R.string.update);
} else {
installText.setText(R.string.reinstall);
}
2017-07-17 21:34:06 +02:00
List<String> items = new ArrayList<>();
if (magiskManager.bootBlock != null) {
items.add(getString(R.string.auto_detect, magiskManager.bootBlock));
spinner.setEnabled(false);
} else {
items.add(getString(R.string.cannot_auto_detect));
items.addAll(magiskManager.blockList);
}
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-24 20:05:22 +01:00
}
private void updateCheckUI() {
int image, color;
2017-05-19 17:37:57 +02:00
if (magiskManager.remoteMagiskVersionCode < 0) {
2016-12-24 20:05:22 +01: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-05-19 17:37:57 +02:00
magiskUpdateText.setText(getString(R.string.install_magisk_title, "v" + magiskManager.remoteMagiskVersionString));
2016-12-24 20:05:22 +01:00
}
2017-02-04 21:40:52 +01:00
2017-05-19 17:37:57 +02:00
magiskUpdateIcon.setImageResource(image);
magiskUpdateIcon.setColorFilter(color);
magiskUpdateIcon.setVisibility(View.VISIBLE);
2016-12-24 20:05:22 +01:00
2017-05-23 10:51:23 +02:00
magiskUpdateProgress.setVisibility(View.GONE);
2016-12-24 20:05:22 +01:00
mSwipeRefreshLayout.setRefreshing(false);
2017-07-19 19:44:32 +02:00
if (magiskManager.remoteMagiskVersionCode > magiskManager.magiskVersionCode)
install();
2016-12-24 20:05:22 +01:00
}
private void updateSafetyNetUI() {
int image, color;
safetyNetProgress.setVisibility(View.GONE);
2017-05-19 21:04:14 +02:00
safetyNetRefreshIcon.setVisibility(View.VISIBLE);
if (magiskManager.SNCheckResult.failed) {
safetyNetStatusText.setText(magiskManager.SNCheckResult.errmsg);
collapse();
} else {
safetyNetStatusText.setText(R.string.safetyNet_check_success);
if (magiskManager.SNCheckResult.ctsProfile) {
color = colorOK;
image = R.drawable.ic_check_circle;
} else {
2016-12-24 20:05:22 +01:00
color = colorBad;
image = R.drawable.ic_cancel;
2017-05-19 21:04:14 +02:00
}
ctsStatusText.setText("ctsProfile: " + magiskManager.SNCheckResult.ctsProfile);
ctsStatusIcon.setImageResource(image);
ctsStatusIcon.setColorFilter(color);
if (magiskManager.SNCheckResult.basicIntegrity) {
2016-12-24 20:05:22 +01:00
color = colorOK;
image = R.drawable.ic_check_circle;
2017-05-19 21:04:14 +02:00
} else {
color = colorBad;
image = R.drawable.ic_cancel;
}
basicStatusText.setText("basicIntegrity: " + magiskManager.SNCheckResult.basicIntegrity);
basicStatusIcon.setImageResource(image);
basicStatusIcon.setColorFilter(color);
expand();
2016-12-24 20:05:22 +01:00
}
2017-05-19 21:04:14 +02:00
}
private void setExpanded() {
ViewGroup.LayoutParams layoutParams = expandLayout.getLayoutParams();
layoutParams.height = mExpanded ? expandHeight : 0;
expandLayout.setLayoutParams(layoutParams);
expandLayout.setVisibility(mExpanded ? View.VISIBLE : View.GONE);
}
private void expand() {
if (mExpanded) return;
expandLayout.setVisibility(View.VISIBLE);
ValueAnimator mAnimator = slideAnimator(0, expandHeight);
mAnimator.start();
mExpanded = true;
}
private void collapse() {
if (!mExpanded) return;
int finalHeight = expandLayout.getHeight();
ValueAnimator mAnimator = slideAnimator(finalHeight, 0);
mAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationEnd(Animator animator) {
expandLayout.setVisibility(View.GONE);
}
@Override
public void onAnimationStart(Animator animator) {}
@Override
public void onAnimationCancel(Animator animator) {}
@Override
public void onAnimationRepeat(Animator animator) {}
});
mAnimator.start();
mExpanded = false;
}
private ValueAnimator slideAnimator(int start, int end) {
ValueAnimator animator = ValueAnimator.ofInt(start, end);
animator.addUpdateListener(valueAnimator -> {
int value = (Integer) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = expandLayout.getLayoutParams();
layoutParams.height = value;
expandLayout.setLayoutParams(layoutParams);
});
return animator;
2016-12-24 20:05:22 +01:00
}
}