2016-08-26 10:45:35 +00:00
|
|
|
package com.topjohnwu.magisk;
|
|
|
|
|
2016-09-19 06:46:07 +00:00
|
|
|
import android.app.Activity;
|
2016-08-31 21:52:42 +00:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
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-27 11:02:41 +00:00
|
|
|
import android.os.Handler;
|
2016-08-31 21:52:42 +00:00
|
|
|
import android.preference.PreferenceManager;
|
2016-09-19 06:46:07 +00:00
|
|
|
import android.provider.Settings;
|
2016-08-26 10:45:35 +00:00
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.support.v4.app.Fragment;
|
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-19 06:46:07 +00:00
|
|
|
import android.widget.Toast;
|
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.BindColor;
|
|
|
|
import butterknife.BindView;
|
|
|
|
import butterknife.ButterKnife;
|
|
|
|
|
|
|
|
public class RootFragment extends Fragment {
|
|
|
|
|
2016-08-31 21:52:42 +00:00
|
|
|
public SharedPreferences prefs;
|
|
|
|
@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;
|
|
|
|
@BindColor(R.color.red500)
|
|
|
|
int red500;
|
|
|
|
@BindColor(R.color.green500)
|
|
|
|
int green500;
|
|
|
|
@BindColor(R.color.grey500)
|
|
|
|
int grey500;
|
|
|
|
@BindColor(R.color.accent)
|
|
|
|
int accent;
|
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-16 04:46:10 +00:00
|
|
|
private boolean autoRootStatus;
|
|
|
|
|
2016-08-26 10:45:35 +00:00
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
|
|
|
View view = inflater.inflate(R.layout.root_fragment, container, false);
|
|
|
|
ButterKnife.bind(this, view);
|
|
|
|
|
2016-08-31 21:52:42 +00:00
|
|
|
prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
|
2016-09-19 06:46:07 +00:00
|
|
|
|
|
|
|
|
2016-09-16 04:46:10 +00:00
|
|
|
if (prefs.contains("autoRootEnable")) {
|
|
|
|
autoRootStatus = prefs.getBoolean("autoRootEnable",false);
|
|
|
|
rootToggle.setEnabled(false);
|
|
|
|
} else {
|
|
|
|
autoRootStatus = false;
|
|
|
|
rootToggle.setEnabled(true);
|
|
|
|
}
|
|
|
|
autoRootToggle.setChecked(autoRootStatus);
|
|
|
|
new updateUI().execute();
|
2016-08-26 10:45:35 +00:00
|
|
|
|
2016-08-27 11:02:41 +00:00
|
|
|
rootToggle.setOnClickListener(toggle -> {
|
2016-09-17 18:32:08 +00:00
|
|
|
Utils.toggleRoot(((CompoundButton) toggle).isChecked());
|
|
|
|
new updateUI().execute();
|
2016-08-31 21:52:42 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
autoRootToggle.setOnClickListener(toggle -> {
|
2016-09-16 04:46:10 +00:00
|
|
|
ToggleAutoRoot(autoRootToggle.isChecked());
|
|
|
|
new Handler().postDelayed(() -> new updateUI().execute(), 1000);
|
2016-08-31 21:52:42 +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-17 18:32:08 +00:00
|
|
|
new updateUI().execute();
|
2016-08-26 10:45:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2016-09-19 06:46:07 +00:00
|
|
|
|
|
|
|
private void CheckAccessPermissions() {
|
|
|
|
if (!Utils.hasStatsPermission(getActivity())) {
|
|
|
|
Toast.makeText(getActivity(),"Please allow Usage Access for auto root to work.",Toast.LENGTH_LONG).show();
|
|
|
|
startActivityForResult(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS), 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
|
// Check which request we're responding to
|
|
|
|
if (requestCode == 100) {
|
|
|
|
// Make sure the request was successful
|
|
|
|
if (resultCode == Activity.RESULT_OK) {
|
|
|
|
Log.d("Magisk","Got result code OK for permissions");
|
|
|
|
Toast.makeText(getActivity(),"Auto-root disabled, permissions required.",Toast.LENGTH_LONG).show();
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
autoRootToggle.setEnabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-16 04:46:10 +00:00
|
|
|
private void ToggleAutoRoot(boolean toggleState) {
|
2016-09-19 06:46:07 +00:00
|
|
|
CheckAccessPermissions();
|
|
|
|
if (Utils.hasStatsPermission(getActivity())) {
|
|
|
|
autoRootStatus = toggleState;
|
|
|
|
SharedPreferences.Editor editor = prefs.edit();
|
|
|
|
editor.putBoolean("autoRootEnable", (toggleState));
|
|
|
|
editor.apply();
|
|
|
|
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 {
|
2016-09-19 07:42:20 +00:00
|
|
|
Intent myIntent = new Intent(getActivity(), MonitorService.class);
|
|
|
|
getActivity().stopService(myIntent);
|
2016-09-19 06:46:07 +00:00
|
|
|
rootToggle.setEnabled(true);
|
2016-09-16 04:46:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-31 21:52:42 +00:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
new updateUI().execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Void v) {
|
|
|
|
super.onPostExecute(v);
|
|
|
|
|
|
|
|
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()) {
|
|
|
|
selinuxStatusContainer.setBackgroundColor(grey500);
|
|
|
|
selinuxStatusIcon.setImageResource(statusUnknown);
|
|
|
|
|
|
|
|
selinuxStatus.setText(R.string.selinux_error_info);
|
|
|
|
selinuxStatus.setTextColor(grey500);
|
|
|
|
selinuxToggle.setChecked(false);
|
|
|
|
} else if (selinux.get(0).equals("Enforcing")) {
|
|
|
|
selinuxStatusContainer.setBackgroundColor(green500);
|
|
|
|
selinuxStatusIcon.setImageResource(statusOK);
|
|
|
|
|
|
|
|
selinuxStatus.setText(R.string.selinux_enforcing_info);
|
|
|
|
selinuxStatus.setTextColor(green500);
|
|
|
|
selinuxToggle.setChecked(true);
|
|
|
|
} else {
|
|
|
|
selinuxStatusContainer.setBackgroundColor(red500);
|
|
|
|
selinuxStatusIcon.setImageResource(statusError);
|
|
|
|
|
|
|
|
selinuxStatus.setText(R.string.selinux_permissive_info);
|
|
|
|
selinuxStatus.setTextColor(red500);
|
|
|
|
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
|
|
|
|
rootStatusContainer.setBackgroundColor(grey500);
|
|
|
|
rootStatusIcon.setImageResource(statusUnknown);
|
|
|
|
rootStatus.setTextColor(grey500);
|
|
|
|
rootStatus.setText(R.string.root_error);
|
|
|
|
rootToggle.setChecked(false);
|
|
|
|
safetyNetStatusIcon.setImageResource(statusUnknown);
|
|
|
|
safetyNetStatus.setText(R.string.root_error_info);
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
// Not rooted
|
|
|
|
rootStatusContainer.setBackgroundColor(green500);
|
|
|
|
rootStatusIcon.setImageResource(statusOK);
|
|
|
|
rootStatus.setTextColor(green500);
|
|
|
|
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-16 04:46:10 +00:00
|
|
|
if (autoRootStatus) {
|
|
|
|
rootStatusContainer.setBackgroundColor(green500);
|
|
|
|
rootStatusIcon.setImageResource(statusAuto);
|
|
|
|
rootStatusIcon.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
|
|
|
|
rootStatus.setTextColor(green500);
|
|
|
|
rootStatus.setText(R.string.root_auto_unmounted);
|
|
|
|
rootToggle.setEnabled(false);
|
|
|
|
safetyNetStatusIcon.setImageResource(statusOK);
|
|
|
|
safetyNetStatus.setText(R.string.root_auto_unmounted_info);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
rootToggle.setEnabled(true);
|
2016-09-17 18:32:08 +00:00
|
|
|
if (Utils.rootEnabled()) {
|
2016-08-26 10:45:35 +00:00
|
|
|
// Mounted
|
|
|
|
rootStatusContainer.setBackgroundColor(accent);
|
|
|
|
rootStatusIcon.setImageResource(statusError);
|
|
|
|
rootStatus.setTextColor(accent);
|
2016-09-17 18:32:08 +00:00
|
|
|
rootStatus.setText(R.string.root_enabled);
|
2016-08-26 10:45:35 +00:00
|
|
|
rootToggle.setChecked(true);
|
|
|
|
safetyNetStatusIcon.setImageResource(statusError);
|
2016-09-17 18:32:08 +00:00
|
|
|
safetyNetStatus.setText(R.string.root_enabled_info);
|
2016-08-26 10:45:35 +00:00
|
|
|
break;
|
2016-09-18 04:30:46 +00:00
|
|
|
} else {
|
2016-09-17 18:32:08 +00:00
|
|
|
// Disabled
|
2016-08-26 10:45:35 +00:00
|
|
|
rootStatusContainer.setBackgroundColor(green500);
|
|
|
|
rootStatusIcon.setImageResource(statusOK);
|
|
|
|
rootStatus.setTextColor(green500);
|
2016-09-17 18:32:08 +00:00
|
|
|
rootStatus.setText(R.string.root_disabled);
|
2016-08-26 10:45:35 +00:00
|
|
|
rootToggle.setChecked(false);
|
|
|
|
safetyNetStatusIcon.setImageResource(statusOK);
|
2016-09-17 18:32:08 +00:00
|
|
|
safetyNetStatus.setText(R.string.root_disabled_info);
|
2016-08-26 10:45:35 +00:00
|
|
|
break;
|
2016-09-16 04:46:10 +00:00
|
|
|
}
|
2016-08-26 10:45:35 +00:00
|
|
|
}
|
|
|
|
case 2:
|
|
|
|
// Improper root
|
|
|
|
rootStatusContainer.setBackgroundColor(red500);
|
|
|
|
rootStatusIcon.setImageResource(statusError);
|
|
|
|
rootStatus.setTextColor(red500);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|