2016-08-26 10:45:35 +00:00
|
|
|
package com.topjohnwu.magisk;
|
|
|
|
|
2016-09-22 04:36:28 +00:00
|
|
|
import android.app.Fragment;
|
2016-08-31 21:52:42 +00:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
2016-09-25 05:16:28 +00:00
|
|
|
import android.content.res.TypedArray;
|
2016-09-16 04:46:10 +00:00
|
|
|
import android.graphics.Color;
|
|
|
|
import android.graphics.PorterDuff;
|
2016-08-26 10:45:35 +00:00
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.os.Bundle;
|
2016-08-31 21:52:42 +00:00
|
|
|
import android.preference.PreferenceManager;
|
2016-08-26 10:45:35 +00:00
|
|
|
import android.support.annotation.Nullable;
|
2016-09-22 04:36:28 +00:00
|
|
|
import android.support.design.widget.Snackbar;
|
2016-09-19 06:46:07 +00:00
|
|
|
import android.util.Log;
|
2016-08-26 10:45:35 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.CompoundButton;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.ProgressBar;
|
|
|
|
import android.widget.Switch;
|
|
|
|
import android.widget.TextView;
|
2016-09-22 21:47:54 +00:00
|
|
|
import android.widget.Toast;
|
2016-08-26 10:45:35 +00:00
|
|
|
|
2016-09-20 16:36:33 +00:00
|
|
|
import com.topjohnwu.magisk.services.MonitorService;
|
2016-09-24 04:25:12 +00:00
|
|
|
import com.topjohnwu.magisk.utils.Logger;
|
2016-08-26 10:45:35 +00:00
|
|
|
import com.topjohnwu.magisk.utils.Shell;
|
2016-09-16 04:46:10 +00:00
|
|
|
import com.topjohnwu.magisk.utils.Utils;
|
2016-08-26 10:45:35 +00:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import butterknife.BindView;
|
|
|
|
import butterknife.ButterKnife;
|
|
|
|
|
2016-09-24 15:07:30 +00:00
|
|
|
public class RootFragment extends Fragment {
|
2016-08-26 10:45:35 +00:00
|
|
|
|
2016-08-31 21:52:42 +00:00
|
|
|
public SharedPreferences prefs;
|
2016-09-26 02:45:34 +00:00
|
|
|
|
|
|
|
@BindView(R.id.progressBar) ProgressBar progressBar;
|
|
|
|
@BindView(R.id.rootSwitchView) View rootToggleView;
|
|
|
|
@BindView(R.id.autoRootSwitchView) View autoRootToggleView;
|
|
|
|
@BindView(R.id.selinuxSwitchView) View selinuxToggleView;
|
|
|
|
@BindView(R.id.rootStatusView) View rootStatusView;
|
|
|
|
@BindView(R.id.safetynetStatusView) View safetynetStatusView;
|
|
|
|
@BindView(R.id.selinuxStatusView) View selinuxStatusView;
|
|
|
|
@BindView(R.id.root_toggle) Switch rootToggle;
|
|
|
|
@BindView(R.id.auto_root_toggle) Switch autoRootToggle;
|
|
|
|
@BindView(R.id.selinux_toggle) Switch selinuxToggle;
|
|
|
|
@BindView(R.id.root_status_container) View rootStatusContainer;
|
|
|
|
@BindView(R.id.root_status_icon) ImageView rootStatusIcon;
|
|
|
|
@BindView(R.id.root_status) TextView rootStatus;
|
|
|
|
@BindView(R.id.selinux_status_container) View selinuxStatusContainer;
|
|
|
|
@BindView(R.id.selinux_status_icon) ImageView selinuxStatusIcon;
|
|
|
|
@BindView(R.id.selinux_status) TextView selinuxStatus;
|
|
|
|
@BindView(R.id.safety_net_status) TextView safetyNetStatus;
|
|
|
|
@BindView(R.id.safety_net_icon) ImageView safetyNetStatusIcon;
|
|
|
|
|
2016-08-26 10:45:35 +00:00
|
|
|
int statusOK = R.drawable.ic_check_circle;
|
2016-09-16 04:46:10 +00:00
|
|
|
int statusAuto = R.drawable.ic_autoroot;
|
2016-08-26 10:45:35 +00:00
|
|
|
int statusError = R.drawable.ic_error;
|
|
|
|
int statusUnknown = R.drawable.ic_help;
|
|
|
|
|
2016-09-25 05:16:28 +00:00
|
|
|
private int colorOK, colorFail, colorNeutral, colorWarn;
|
2016-09-16 04:46:10 +00:00
|
|
|
private boolean autoRootStatus;
|
2016-09-24 04:25:12 +00:00
|
|
|
private SharedPreferences.OnSharedPreferenceChangeListener listener;
|
2016-09-25 05:16:28 +00:00
|
|
|
private View view;
|
2016-09-16 04:46:10 +00:00
|
|
|
|
2016-08-26 10:45:35 +00:00
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
2016-09-22 04:36:28 +00:00
|
|
|
view = inflater.inflate(R.layout.root_fragment, container, false);
|
2016-08-26 10:45:35 +00:00
|
|
|
ButterKnife.bind(this, view);
|
2016-09-25 05:16:28 +00:00
|
|
|
int[] attrs0 = {R.attr.ColorOK};
|
|
|
|
int[] attrs1 = {R.attr.ColorFail};
|
|
|
|
int[] attrs2 = {R.attr.ColorNeutral};
|
|
|
|
int[] attrs3 = {R.attr.ColorWarn};
|
|
|
|
TypedArray ta0 = getActivity().obtainStyledAttributes(attrs0);
|
|
|
|
TypedArray ta1 = getActivity().obtainStyledAttributes(attrs1);
|
|
|
|
TypedArray ta2 = getActivity().obtainStyledAttributes(attrs2);
|
|
|
|
TypedArray ta3 = getActivity().obtainStyledAttributes(attrs3);
|
|
|
|
colorOK = ta0.getColor(0, Color.GRAY);
|
|
|
|
colorFail = ta1.getColor(0, Color.GRAY);
|
|
|
|
colorNeutral = ta2.getColor(0, Color.GRAY);
|
|
|
|
colorWarn = ta2.getColor(0, Color.GRAY);
|
|
|
|
ta0.recycle();
|
|
|
|
ta1.recycle();
|
|
|
|
ta2.recycle();
|
|
|
|
ta3.recycle();
|
2016-09-23 21:22:11 +00:00
|
|
|
autoRootStatus = Utils.autoToggleEnabled(getActivity());
|
2016-09-19 06:46:07 +00:00
|
|
|
|
2016-09-22 04:36:28 +00:00
|
|
|
if (autoRootStatus) {
|
|
|
|
if (!Utils.hasServicePermission(getActivity())) {
|
|
|
|
autoRootStatus = false;
|
|
|
|
}
|
2016-09-16 04:46:10 +00:00
|
|
|
}
|
2016-09-22 04:36:28 +00:00
|
|
|
rootToggle.setEnabled(!autoRootStatus);
|
2016-09-16 04:46:10 +00:00
|
|
|
autoRootToggle.setChecked(autoRootStatus);
|
2016-09-25 13:31:38 +00:00
|
|
|
new updateUI().executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
|
2016-08-26 10:45:35 +00:00
|
|
|
|
2016-09-24 15:07:30 +00:00
|
|
|
rootToggle.setOnClickListener(toggle -> Utils.toggleRoot(((CompoundButton) toggle).isChecked(), getActivity()));
|
2016-08-31 21:52:42 +00:00
|
|
|
|
|
|
|
autoRootToggle.setOnClickListener(toggle -> {
|
2016-09-22 04:36:28 +00:00
|
|
|
if (!Utils.hasServicePermission(getActivity())) {
|
|
|
|
Intent intent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
|
2016-09-23 21:22:11 +00:00
|
|
|
Toast.makeText(getActivity(), "Please enable accessibility access for Magisk's auto-toggle feature to work.", Toast.LENGTH_LONG).show();
|
2016-09-22 04:36:28 +00:00
|
|
|
startActivityForResult(intent, 100);
|
|
|
|
} else {
|
|
|
|
ToggleAutoRoot(autoRootToggle.isChecked());
|
2016-08-31 21:52:42 +00:00
|
|
|
|
2016-09-22 04:36:28 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-24 04:25:12 +00:00
|
|
|
|
2016-09-22 04:36:28 +00:00
|
|
|
);
|
2016-08-26 10:45:35 +00:00
|
|
|
|
2016-08-27 11:02:41 +00:00
|
|
|
selinuxToggle.setOnClickListener(toggle -> {
|
|
|
|
Shell.su(((CompoundButton) toggle).isChecked() ? "setenforce 1" : "setenforce 0");
|
2016-09-25 13:31:38 +00:00
|
|
|
new updateUI().executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
|
2016-08-26 10:45:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2016-09-24 04:25:12 +00:00
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
2016-09-25 05:16:28 +00:00
|
|
|
if (null != listener) {
|
|
|
|
prefs.unregisterOnSharedPreferenceChangeListener(listener);
|
|
|
|
}
|
2016-09-24 04:25:12 +00:00
|
|
|
}
|
2016-09-21 12:39:12 +00:00
|
|
|
|
2016-09-19 06:46:07 +00:00
|
|
|
@Override
|
|
|
|
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
|
// Check which request we're responding to
|
2016-09-20 05:05:41 +00:00
|
|
|
Log.d("Magisk", "Got result: " + requestCode + " and " + resultCode);
|
2016-09-19 06:46:07 +00:00
|
|
|
if (requestCode == 100) {
|
2016-09-22 04:36:28 +00:00
|
|
|
if (Utils.hasServicePermission(getActivity())) {
|
|
|
|
ToggleAutoRoot(true);
|
2016-09-25 05:16:28 +00:00
|
|
|
Snackbar.make(view, getActivity().getString(R.string.auto_toggle) + " has been enabled.", Snackbar.LENGTH_LONG).show();
|
|
|
|
|
2016-09-19 06:46:07 +00:00
|
|
|
} else {
|
2016-09-22 04:36:28 +00:00
|
|
|
autoRootToggle.setChecked(false);
|
2016-09-23 21:22:11 +00:00
|
|
|
Snackbar.make(view, getActivity().getString(R.string.auto_toggle) + " disabled, permissions required.", Snackbar.LENGTH_LONG).show();
|
2016-09-19 06:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-16 04:46:10 +00:00
|
|
|
private void ToggleAutoRoot(boolean toggleState) {
|
2016-09-20 05:05:41 +00:00
|
|
|
autoRootStatus = toggleState;
|
2016-09-23 21:22:11 +00:00
|
|
|
Utils.toggleAutoRoot(toggleState, getActivity());
|
2016-09-20 05:05:41 +00:00
|
|
|
if (toggleState) {
|
|
|
|
Intent myIntent = new Intent(getActivity(), MonitorService.class);
|
|
|
|
getActivity().startService(myIntent);
|
|
|
|
rootToggle.setEnabled(false);
|
|
|
|
boolean boo = Utils.isMyServiceRunning(MonitorService.class, getActivity());
|
|
|
|
if (boo) {
|
|
|
|
Intent myServiceIntent = new Intent(getActivity(), MonitorService.class);
|
|
|
|
getActivity().startService(myServiceIntent);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Intent myIntent = new Intent(getActivity(), MonitorService.class);
|
|
|
|
getActivity().stopService(myIntent);
|
|
|
|
rootToggle.setEnabled(true);
|
|
|
|
}
|
2016-09-19 21:48:13 +00:00
|
|
|
|
2016-09-16 04:46:10 +00:00
|
|
|
}
|
|
|
|
|
2016-08-31 21:52:42 +00:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
2016-09-23 21:22:11 +00:00
|
|
|
getActivity().setTitle("Root");
|
2016-09-25 05:16:28 +00:00
|
|
|
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
|
|
|
|
listener = (prefs1, key) -> {
|
|
|
|
|
|
|
|
if ((key.contains("autoRootEnable")) | (key.equals("root"))) {
|
2016-09-27 16:33:01 +00:00
|
|
|
Logger.dev("RootFragmnet, keychange detected for " + key);
|
2016-09-25 05:16:28 +00:00
|
|
|
new updateUI().execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
prefs.registerOnSharedPreferenceChangeListener(listener);
|
2016-08-31 21:52:42 +00:00
|
|
|
new updateUI().execute();
|
2016-09-24 18:46:42 +00:00
|
|
|
|
2016-08-31 21:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class updateUI extends AsyncTask<Void, Void, Void> {
|
2016-08-26 10:45:35 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Void doInBackground(Void... voids) {
|
|
|
|
// Make sure static block invoked
|
|
|
|
Shell.rootAccess();
|
2016-09-22 04:36:28 +00:00
|
|
|
// Set up Tile on UI Refresh
|
2016-09-25 05:16:28 +00:00
|
|
|
if (PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean("enable_quicktile", false)) {
|
2016-09-22 04:36:28 +00:00
|
|
|
Utils.SetupQuickSettingsTile(getActivity());
|
|
|
|
}
|
2016-09-24 04:25:12 +00:00
|
|
|
autoRootStatus = Utils.autoToggleEnabled(getActivity());
|
2016-08-26 10:45:35 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Void v) {
|
|
|
|
super.onPostExecute(v);
|
2016-09-24 13:07:20 +00:00
|
|
|
autoRootToggle.setChecked(autoRootStatus);
|
2016-08-26 10:45:35 +00:00
|
|
|
progressBar.setVisibility(View.GONE);
|
|
|
|
rootStatusView.setVisibility(View.VISIBLE);
|
|
|
|
safetynetStatusView.setVisibility(View.VISIBLE);
|
|
|
|
selinuxStatusView.setVisibility(View.VISIBLE);
|
|
|
|
|
|
|
|
if (Shell.rootAccess()) {
|
|
|
|
rootToggleView.setVisibility(View.VISIBLE);
|
2016-08-31 21:52:42 +00:00
|
|
|
autoRootToggleView.setVisibility(View.VISIBLE);
|
2016-08-26 10:45:35 +00:00
|
|
|
selinuxToggleView.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
List<String> selinux = Shell.sh("getenforce");
|
|
|
|
|
|
|
|
if (selinux.isEmpty()) {
|
2016-09-25 05:16:28 +00:00
|
|
|
selinuxStatusContainer.setBackgroundColor(colorNeutral);
|
2016-08-26 10:45:35 +00:00
|
|
|
selinuxStatusIcon.setImageResource(statusUnknown);
|
|
|
|
|
|
|
|
selinuxStatus.setText(R.string.selinux_error_info);
|
2016-09-25 05:16:28 +00:00
|
|
|
selinuxStatus.setTextColor(colorNeutral);
|
2016-08-26 10:45:35 +00:00
|
|
|
selinuxToggle.setChecked(false);
|
|
|
|
} else if (selinux.get(0).equals("Enforcing")) {
|
2016-09-25 05:16:28 +00:00
|
|
|
selinuxStatusContainer.setBackgroundColor(colorOK);
|
2016-08-26 10:45:35 +00:00
|
|
|
selinuxStatusIcon.setImageResource(statusOK);
|
|
|
|
|
|
|
|
selinuxStatus.setText(R.string.selinux_enforcing_info);
|
2016-09-25 05:16:28 +00:00
|
|
|
selinuxStatus.setTextColor(colorOK);
|
2016-08-26 10:45:35 +00:00
|
|
|
selinuxToggle.setChecked(true);
|
|
|
|
} else {
|
2016-09-25 05:16:28 +00:00
|
|
|
selinuxStatusContainer.setBackgroundColor(colorFail);
|
2016-08-26 10:45:35 +00:00
|
|
|
selinuxStatusIcon.setImageResource(statusError);
|
|
|
|
|
|
|
|
selinuxStatus.setText(R.string.selinux_permissive_info);
|
2016-09-25 05:16:28 +00:00
|
|
|
selinuxStatus.setTextColor(colorFail);
|
2016-08-26 10:45:35 +00:00
|
|
|
selinuxToggle.setChecked(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new File("/system/framework/twframework.jar").exists()) {
|
|
|
|
selinuxStatus.append("\n" + getString(R.string.selinux_samsung_info));
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (Shell.rootStatus) {
|
|
|
|
case -1:
|
|
|
|
// Root Error
|
2016-09-25 05:16:28 +00:00
|
|
|
rootStatusContainer.setBackgroundColor(colorFail);
|
2016-08-26 10:45:35 +00:00
|
|
|
rootStatusIcon.setImageResource(statusUnknown);
|
2016-09-25 05:16:28 +00:00
|
|
|
rootStatus.setTextColor(colorNeutral);
|
2016-08-26 10:45:35 +00:00
|
|
|
rootStatus.setText(R.string.root_error);
|
|
|
|
rootToggle.setChecked(false);
|
|
|
|
safetyNetStatusIcon.setImageResource(statusUnknown);
|
|
|
|
safetyNetStatus.setText(R.string.root_error_info);
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
// Not rooted
|
2016-09-25 05:16:28 +00:00
|
|
|
rootStatusContainer.setBackgroundColor(colorOK);
|
2016-08-26 10:45:35 +00:00
|
|
|
rootStatusIcon.setImageResource(statusOK);
|
2016-09-25 05:16:28 +00:00
|
|
|
rootStatus.setTextColor(colorOK);
|
2016-08-26 10:45:35 +00:00
|
|
|
rootStatus.setText(R.string.root_none);
|
|
|
|
rootToggle.setChecked(false);
|
|
|
|
safetyNetStatusIcon.setImageResource(statusOK);
|
|
|
|
safetyNetStatus.setText(R.string.root_none_info);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
// Proper root
|
2016-09-20 05:05:41 +00:00
|
|
|
if (autoRootStatus) {
|
2016-09-25 05:16:28 +00:00
|
|
|
rootStatusContainer.setBackgroundColor(colorOK);
|
2016-09-20 05:05:41 +00:00
|
|
|
rootStatusIcon.setImageResource(statusAuto);
|
|
|
|
rootStatusIcon.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
|
2016-09-25 05:16:28 +00:00
|
|
|
rootStatus.setTextColor(colorOK);
|
2016-09-20 05:05:41 +00:00
|
|
|
rootStatus.setText(R.string.root_auto_unmounted);
|
|
|
|
rootToggle.setEnabled(false);
|
2016-09-24 13:07:20 +00:00
|
|
|
autoRootToggle.setChecked(true);
|
2016-08-26 10:45:35 +00:00
|
|
|
safetyNetStatusIcon.setImageResource(statusOK);
|
2016-09-20 05:05:41 +00:00
|
|
|
safetyNetStatus.setText(R.string.root_auto_unmounted_info);
|
2016-08-26 10:45:35 +00:00
|
|
|
break;
|
2016-09-20 05:05:41 +00:00
|
|
|
} else {
|
|
|
|
rootToggle.setEnabled(true);
|
|
|
|
if (Utils.rootEnabled()) {
|
|
|
|
// Mounted
|
2016-09-25 05:16:28 +00:00
|
|
|
rootStatusContainer.setBackgroundColor(colorWarn);
|
2016-09-20 05:05:41 +00:00
|
|
|
rootStatusIcon.setImageResource(statusError);
|
2016-09-25 05:16:28 +00:00
|
|
|
rootStatus.setTextColor(colorWarn);
|
2016-09-20 05:05:41 +00:00
|
|
|
rootStatus.setText(R.string.root_enabled);
|
|
|
|
rootToggle.setChecked(true);
|
|
|
|
safetyNetStatusIcon.setImageResource(statusError);
|
|
|
|
safetyNetStatus.setText(R.string.root_enabled_info);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
// Disabled
|
2016-09-25 05:16:28 +00:00
|
|
|
rootStatusContainer.setBackgroundColor(colorOK);
|
2016-09-20 05:05:41 +00:00
|
|
|
rootStatusIcon.setImageResource(statusOK);
|
2016-09-25 05:16:28 +00:00
|
|
|
rootStatus.setTextColor(colorOK);
|
2016-09-20 05:05:41 +00:00
|
|
|
rootStatus.setText(R.string.root_disabled);
|
|
|
|
rootToggle.setChecked(false);
|
|
|
|
safetyNetStatusIcon.setImageResource(statusOK);
|
|
|
|
safetyNetStatus.setText(R.string.root_disabled_info);
|
|
|
|
break;
|
|
|
|
}
|
2016-08-26 10:45:35 +00:00
|
|
|
}
|
|
|
|
case 2:
|
|
|
|
// Improper root
|
2016-09-25 05:16:28 +00:00
|
|
|
rootStatusContainer.setBackgroundColor(colorFail);
|
2016-08-26 10:45:35 +00:00
|
|
|
rootStatusIcon.setImageResource(statusError);
|
2016-09-25 05:16:28 +00:00
|
|
|
rootStatus.setTextColor(colorFail);
|
2016-08-26 10:45:35 +00:00
|
|
|
rootStatus.setText(R.string.root_system);
|
|
|
|
rootToggle.setChecked(true);
|
|
|
|
safetyNetStatusIcon.setImageResource(statusError);
|
|
|
|
safetyNetStatus.setText(R.string.root_system_info);
|
2016-08-31 21:52:42 +00:00
|
|
|
autoRootToggleView.setVisibility(View.GONE);
|
2016-08-26 10:45:35 +00:00
|
|
|
rootToggleView.setVisibility(View.GONE);
|
|
|
|
selinuxToggleView.setVisibility(View.GONE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-21 12:39:12 +00:00
|
|
|
|
2016-08-26 10:45:35 +00:00
|
|
|
}
|