Updated UI

Added "Root" section
This commit is contained in:
dvdandroid 2016-08-26 12:45:35 +02:00
parent c86c2661af
commit ab318ef99e
12 changed files with 741 additions and 477 deletions

View File

@ -9,7 +9,7 @@ android {
applicationId "com.topjohnwu.magisk"
minSdkVersion 21
targetSdkVersion 24
versionCode 3
versionCode 4
versionName "2.0"
}
buildTypes {

View File

@ -1,21 +1,47 @@
package com.topjohnwu.magisk;
import android.Manifest;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.text.Html;
import android.text.TextUtils;
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;
import android.widget.Toast;
import com.topjohnwu.magisk.utils.Shell;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import butterknife.BindColor;
@ -24,67 +50,105 @@ import butterknife.ButterKnife;
public class MagiskFragment extends Fragment {
@BindView(R.id.progressBar) ProgressBar progressBar;
private static final String JSON_UPDATE_CHECK = "https://raw.githubusercontent.com/topjohnwu/MagiskManager/master/app/magisk_update.json";
@BindView(R.id.progressBarVersion) ProgressBar progressBar;
@BindView(R.id.rootSwitchView) View rootToggleView;
@BindView(R.id.selinuxSwitchView) View selinuxToggleView;
@BindView(R.id.magiskStatusView) View magiskStatusView;
@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.selinux_toggle) Switch selinuxToggle;
@BindView(R.id.magisk_status_container) View magiskStatusContainer;
@BindView(R.id.magisk_status_icon) ImageView magiskStatusIcon;
@BindView(R.id.magisk_version) TextView magiskVersion;
@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.app_updateView) View appUpdateView;
@BindView(R.id.app_check_updates_container) View appCheckUpdatesContainer;
@BindView(R.id.app_check_updates_icon) ImageView appCheckUpdatesIcon;
@BindView(R.id.app_check_updates_status) TextView appCheckUpdatesStatus;
@BindView(R.id.app_check_updates_progress) ProgressBar appCheckUpdatesProgress;
@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.magisk_updateView) View magiskUpdateView;
@BindView(R.id.magisk_check_updates_container) View magiskCheckUpdatesContainer;
@BindView(R.id.magisk_check_updates_icon) ImageView magiskCheckUpdatesIcon;
@BindView(R.id.magisk_check_updates_status) TextView magiskCheckUpdatesStatus;
@BindView(R.id.magisk_check_updates_progress) ProgressBar magiskCheckUpdatesProgress;
@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;
@BindColor(R.color.blue500) int blue500;
@BindColor(R.color.grey500) int grey500;
int statusOK = R.drawable.ic_check_circle;
int statusError = R.drawable.ic_error;
int statusUnknown = R.drawable.ic_help;
private String mLastLink;
private boolean mLastIsApp;
private List<String> version;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.magisk_fragment, container, false);
ButterKnife.bind(this, view);
View v = inflater.inflate(R.layout.magisk_fragment, container, false);
ButterKnife.bind(this, v);
new updateUI().execute();
new CheckUpdates().execute();
return v;
}
rootToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Shell.su(b ? "setprop magisk.root 1" : "setprop magisk.root 0");
new updateUI().execute();
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
new DownloadFile(getContext(), mLastLink, mLastIsApp).execute();
} else {
Toast.makeText(getContext(), R.string.permissionNotGranted, Toast.LENGTH_LONG).show();
}
}
private void setupCardView(final boolean app, final String versionCode, final String link, String changelog) {
View clickView;
if (app) {
clickView = appUpdateView;
appCheckUpdatesContainer.setBackgroundColor(blue500);
appCheckUpdatesIcon.setImageResource(R.drawable.ic_file_download);
appCheckUpdatesStatus.setText(R.string.app_update_available);
} else {
clickView = magiskUpdateView;
magiskCheckUpdatesContainer.setBackgroundColor(blue500);
magiskCheckUpdatesIcon.setImageResource(R.drawable.ic_file_download);
magiskCheckUpdatesStatus.setText(R.string.magisk_update_available);
}
String text = app ? getString(R.string.app_name) : getString(R.string.magisk);
final String msg = getString(R.string.update_available_message, text, versionCode, changelog);
clickView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new AlertDialog.Builder(getContext())
.setTitle(R.string.update_available)
.setMessage(Html.fromHtml(msg))
.setCancelable(false)
.setPositiveButton(R.string.update, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mLastLink = link;
mLastIsApp = app;
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
&& Build.VERSION.SDK_INT >= 23) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
return;
}
new DownloadFile(getContext(), link, app).execute();
}
})
.setNegativeButton(R.string.no_thanks, null)
.show();
}
});
selinuxToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Shell.su(b ? "setenforce 1" : "setenforce 0");
new updateUI().execute();
}
});
return view;
}
private class updateUI extends AsyncTask<Void, Void, Void> {
@ -102,18 +166,7 @@ public class MagiskFragment extends Fragment {
progressBar.setVisibility(View.GONE);
magiskStatusView.setVisibility(View.VISIBLE);
rootStatusView.setVisibility(View.VISIBLE);
safetynetStatusView.setVisibility(View.VISIBLE);
selinuxStatusView.setVisibility(View.VISIBLE);
if (Shell.rootAccess()) {
rootToggleView.setVisibility(View.VISIBLE);
selinuxToggleView.setVisibility(View.VISIBLE);
}
List<String> selinux = Shell.sh("getenforce");
List<String> version = Shell.sh("getprop magisk.version");
version = Shell.sh("getprop magisk.version");
if (version.isEmpty()) {
magiskStatusContainer.setBackgroundColor(grey500);
@ -129,91 +182,188 @@ public class MagiskFragment extends Fragment {
magiskVersion.setText(getString(R.string.magisk_version, version.get(0)));
}
if (selinux.isEmpty()) {
selinuxStatusContainer.setBackgroundColor(grey500);
selinuxStatusIcon.setImageResource(statusUnknown);
magiskStatusView.setVisibility(View.VISIBLE);
}
}
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);
private class CheckUpdates extends AsyncTask<Void, Void, String> {
selinuxStatus.setText(R.string.selinux_enforcing_info);
selinuxStatus.setTextColor(green500);
selinuxToggle.setChecked(true);
@Override
protected String doInBackground(Void... voids) {
try {
HttpURLConnection c = (HttpURLConnection) new URL(JSON_UPDATE_CHECK).openConnection();
c.setRequestMethod("GET");
c.setInstanceFollowRedirects(false);
c.setDoOutput(false);
c.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
return sb.toString();
} catch (IOException e) {
return null;
}
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
appCheckUpdatesProgress.setVisibility(View.GONE);
magiskCheckUpdatesProgress.setVisibility(View.GONE);
if (result == null) {
appCheckUpdatesContainer.setBackgroundColor(accent);
magiskCheckUpdatesContainer.setBackgroundColor(accent);
appCheckUpdatesIcon.setImageResource(R.drawable.ic_warning);
magiskCheckUpdatesIcon.setImageResource(R.drawable.ic_warning);
appCheckUpdatesStatus.setText(R.string.cannot_check_updates);
magiskCheckUpdatesStatus.setText(R.string.cannot_check_updates);
return;
}
try {
JSONObject json = new JSONObject(result);
JSONObject app = json.getJSONObject("app");
JSONObject magisk = json.getJSONObject("magisk");
String appVersionCode = app.getString("versionCode");
String appLink = app.getString("link");
String appChangelog = app.getString("changelog");
String magiskVersionCode = magisk.getString("versionCode");
String magiskLink = magisk.getString("link");
String magiskChangelog = magisk.getString("changelog");
if (Integer.parseInt(appVersionCode) > BuildConfig.VERSION_CODE) {
setupCardView(true, appVersionCode, appLink, appChangelog);
} else {
selinuxStatusContainer.setBackgroundColor(red500);
selinuxStatusIcon.setImageResource(statusError);
selinuxStatus.setText(R.string.selinux_permissive_info);
selinuxStatus.setTextColor(red500);
selinuxToggle.setChecked(false);
appCheckUpdatesContainer.setBackgroundColor(green500);
appCheckUpdatesIcon.setImageResource(R.drawable.ic_check_circle);
appCheckUpdatesStatus.setText(getString(R.string.up_to_date, getString(R.string.app_name)));
}
if (new File("/system/framework/twframework.jar").exists()) {
selinuxToggleView.setVisibility(View.GONE);
selinuxStatus.append("\n" + getString(R.string.selinux_samsung_info));
}
String v = version.isEmpty() ? "" : version.get(0);
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
if (new File("/system/xbin/su").exists()) {
// Mounted
rootStatusContainer.setBackgroundColor(accent);
rootStatusIcon.setImageResource(statusError);
rootStatus.setTextColor(accent);
rootStatus.setText(R.string.root_mounted);
rootToggle.setChecked(true);
safetyNetStatusIcon.setImageResource(statusError);
safetyNetStatus.setText(R.string.root_mounted_info);
break;
int versionInt = TextUtils.isEmpty(v) ? 0 : Integer.parseInt(v);
if (Integer.parseInt(magiskVersionCode) > versionInt) {
setupCardView(false, magiskVersionCode, magiskLink, magiskChangelog);
} else {
// Not Mounted
rootStatusContainer.setBackgroundColor(green500);
rootStatusIcon.setImageResource(statusOK);
rootStatus.setTextColor(green500);
rootStatus.setText(R.string.root_unmounted);
rootToggle.setChecked(false);
safetyNetStatusIcon.setImageResource(statusOK);
safetyNetStatus.setText(R.string.root_unmounted_info);
break;
magiskCheckUpdatesContainer.setBackgroundColor(green500);
magiskCheckUpdatesIcon.setImageResource(R.drawable.ic_check_circle);
magiskCheckUpdatesStatus.setText(getString(R.string.up_to_date, getString(R.string.magisk)));
}
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);
rootToggleView.setVisibility(View.GONE);
break;
} catch (JSONException ignored) {
}
}
}
private class DownloadFile extends AsyncTask<Void, Integer, Boolean> {
private final Context context;
private final String link;
private final File downloadFile;
private final ProgressDialog progress;
public DownloadFile(Context context, String link, boolean apk) {
this.link = link;
this.context = context;
File dir = new File(Environment.getExternalStorageDirectory() + "/Magisk");
if (!dir.exists()) dir.mkdir();
if (apk) {
downloadFile = new File(dir + "/MagiskManager.apk");
} else {
downloadFile = new File(dir + "/Magisk.zip");
}
Toast.makeText(context, downloadFile.getPath(), Toast.LENGTH_SHORT).show();
progress = new ProgressDialog(getContext());
progress.setTitle(null);
progress.setMessage(getString(R.string.loading));
progress.setIndeterminate(true);
progress.setCancelable(false);
progress.setButton(android.app.AlertDialog.BUTTON_POSITIVE, getString(android.R.string.cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
cancel(true);
}
});
progress.show();
}
@Override
protected Boolean doInBackground(Void... voids) {
try {
URL u = new URL(link);
URLConnection conn = u.openConnection();
conn.connect();
int length = conn.getContentLength();
InputStream input = new BufferedInputStream(u.openStream(), 8192);
OutputStream output = new FileOutputStream(downloadFile);
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
publishProgress((int) ((total * 100) / length));
}
output.flush();
output.close();
input.close();
return true;
} catch (IOException e) {
return false;
}
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
progress.setMessage(getString(R.string.loading) + " " + values[0] + "%");
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
progress.dismiss();
if (!result) {
Toast.makeText(context, R.string.error_download_file, Toast.LENGTH_LONG).show();
return;
}
if (downloadFile.getPath().contains("apk")) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(downloadFile), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
} else {
Toast.makeText(context, R.string.flash_recovery, Toast.LENGTH_LONG).show();
}
}
}
}

View File

@ -0,0 +1,199 @@
package com.topjohnwu.magisk;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
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;
import com.topjohnwu.magisk.utils.Shell;
import java.io.File;
import java.util.List;
import butterknife.BindColor;
import butterknife.BindView;
import butterknife.ButterKnife;
public class RootFragment extends Fragment {
@BindView(R.id.progressBar) ProgressBar progressBar;
@BindView(R.id.rootSwitchView) View rootToggleView;
@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.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;
int statusOK = R.drawable.ic_check_circle;
int statusError = R.drawable.ic_error;
int statusUnknown = R.drawable.ic_help;
@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);
new updateUI().execute();
rootToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Shell.su(b ? "setprop magisk.root 1" : "setprop magisk.root 0");
new updateUI().execute();
}
});
selinuxToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Shell.su(b ? "setenforce 1" : "setenforce 0");
new updateUI().execute();
}
});
return view;
}
private class updateUI extends AsyncTask<Void, Void, Void> {
@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);
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()) {
selinuxToggleView.setVisibility(View.GONE);
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
if (new File("/system/xbin/su").exists()) {
// Mounted
rootStatusContainer.setBackgroundColor(accent);
rootStatusIcon.setImageResource(statusError);
rootStatus.setTextColor(accent);
rootStatus.setText(R.string.root_mounted);
rootToggle.setChecked(true);
safetyNetStatusIcon.setImageResource(statusError);
safetyNetStatus.setText(R.string.root_mounted_info);
break;
} else {
// Not Mounted
rootStatusContainer.setBackgroundColor(green500);
rootStatusIcon.setImageResource(statusOK);
rootStatus.setTextColor(green500);
rootStatus.setText(R.string.root_unmounted);
rootToggle.setChecked(false);
safetyNetStatusIcon.setImageResource(statusOK);
safetyNetStatus.setText(R.string.root_unmounted_info);
break;
}
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);
rootToggleView.setVisibility(View.GONE);
selinuxToggleView.setVisibility(View.GONE);
break;
}
}
}
}

View File

@ -1,53 +1,21 @@
package com.topjohnwu.magisk;
import android.Manifest;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import com.topjohnwu.magisk.utils.Shell;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
@ -55,7 +23,6 @@ import butterknife.ButterKnife;
public class WelcomeActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private static final String SELECTED_ITEM_ID = "SELECTED_ITEM_ID";
private static final String JSON_UPDATE_CHECK = "https://raw.githubusercontent.com/topjohnwu/MagiskManager/updater/app/magisk_update.json";
private final Handler mDrawerHandler = new Handler();
@ -66,9 +33,6 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView
@IdRes
private int mSelectedId = R.id.magisk;
private String mLastLink;
private boolean mLastIsApp;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -117,7 +81,6 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView
navigationView.setNavigationItemSelectedListener(this);
new CheckUpdates(this).execute();
}
@Override
@ -160,6 +123,11 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView
tag = "magisk";
navFragment = new MagiskFragment();
break;
case R.id.root:
setTitle(R.string.root);
tag = "root";
navFragment = new RootFragment();
break;
case R.id.modules:
setTitle(R.string.modules);
tag = "modules";
@ -187,216 +155,4 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
new DownloadFile(this, mLastLink, mLastIsApp).execute();
} else {
Toast.makeText(this, R.string.permissionNotGranted, Toast.LENGTH_LONG).show();
}
}
private class CheckUpdates extends AsyncTask<Void, Void, String> {
private final Context context;
public CheckUpdates(Context context) {
this.context = context;
}
@Override
protected String doInBackground(Void... voids) {
try {
HttpURLConnection c = (HttpURLConnection) new URL(JSON_UPDATE_CHECK).openConnection();
c.setRequestMethod("GET");
c.setInstanceFollowRedirects(false);
c.setDoOutput(false);
c.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
return sb.toString();
} catch (IOException e) {
return null;
}
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (result == null) return;
try {
JSONObject json = new JSONObject(result);
JSONObject app = json.getJSONObject("app");
JSONObject magisk = json.getJSONObject("magisk");
String appVersionCode = app.getString("versionCode");
String appLink = app.getString("link");
String appChangelog = app.getString("changelog");
String magiskVersionCode = magisk.getString("versionCode");
String magiskLink = magisk.getString("link");
String magiskChangelog = magisk.getString("changelog");
if (Integer.parseInt(appVersionCode) > BuildConfig.VERSION_CODE) {
showUpdateDialog(true, appVersionCode, appLink, appChangelog);
}
List<String> versionSh = Shell.sh("getprop magisk.version");
String version = versionSh.isEmpty() ? "" : versionSh.get(0);
int versionInt = TextUtils.isEmpty(version) ? 0 : Integer.parseInt(version);
if (Integer.parseInt(magiskVersionCode) > versionInt) {
showUpdateDialog(false, magiskVersionCode, magiskLink, magiskChangelog);
}
} catch (JSONException ignored) {
}
}
private boolean isUpdateIgnored(String version) {
SharedPreferences prefs = getSharedPreferences(getPackageName() + "_preferences", MODE_PRIVATE);
return prefs.getBoolean("update_ignored_" + version, false);
}
private void setUpdateIgnored(String version) {
SharedPreferences prefs = getSharedPreferences(getPackageName() + "_preferences", MODE_PRIVATE);
prefs.edit().putBoolean("update_ignored_" + version, true).apply();
}
private void showUpdateDialog(final boolean app, final String versionCode, final String link, String changelog) {
if (isUpdateIgnored(versionCode)) return;
String text = app ? getString(R.string.app_name) : getString(R.string.magisk);
String msg = getString(R.string.update_available_message, text, versionCode, changelog);
new AlertDialog.Builder(context)
.setTitle(R.string.update_available)
.setMessage(Html.fromHtml(msg))
.setCancelable(false)
.setPositiveButton(R.string.update, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mLastLink = link;
mLastIsApp = app;
if (ActivityCompat.checkSelfPermission(WelcomeActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
&& Build.VERSION.SDK_INT >= 23) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
return;
}
new DownloadFile(context, link, app).execute();
}
})
.setNegativeButton(R.string.no_thanks, null)
.setNeutralButton(R.string.never_show_again, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
setUpdateIgnored(versionCode);
}
})
.show();
}
}
private class DownloadFile extends AsyncTask<Void, Integer, Boolean> {
private final Context context;
private final String link;
private final File downloadFile;
private final ProgressDialog progress;
public DownloadFile(Context context, String link, boolean apk) {
this.link = link;
this.context = context;
File dir = new File(Environment.getExternalStorageDirectory() + "/Magisk");
if (!dir.exists()) dir.mkdir();
if (apk) {
downloadFile = new File(dir + "/MagiskManager.apk");
} else {
downloadFile = new File(dir + "/Magisk.zip");
}
Toast.makeText(context, downloadFile.getPath(), Toast.LENGTH_SHORT).show();
progress = ProgressDialog.show(context, null, getString(R.string.loading), true, false);
}
@Override
protected Boolean doInBackground(Void... voids) {
try {
URL u = new URL(link);
URLConnection conn = u.openConnection();
conn.connect();
int length = conn.getContentLength();
InputStream input = new BufferedInputStream(u.openStream(), 8192);
OutputStream output = new FileOutputStream(downloadFile);
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
publishProgress((int) ((total * 100) / length));
}
output.flush();
output.close();
input.close();
return true;
} catch (IOException e) {
return false;
}
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
progress.setMessage(getString(R.string.loading) + values[0] + "%");
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
progress.dismiss();
if (!result) {
Toast.makeText(context, R.string.error_download_file, Toast.LENGTH_LONG).show();
return;
}
if (downloadFile.getPath().contains("apk")) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(downloadFile), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
} else {
Toast.makeText(context, R.string.flash_recovery, Toast.LENGTH_LONG).show();
}
}
}
}

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#fff"
android:pathData="M19,9h-4V3H9v6H5l7,7 7,-7zM5,18v2h14v-2H5z"/>
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#fff"
android:pathData="M1,21h22L12,2 1,21zM13,18h-2v-2h2v2zM13,14h-2v-4h2v4z"/>
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24"
android:viewportWidth="24">
<path
android:fillColor="#000"
android:pathData="M3,5A2,2 0 0,1 5,3H19A2,2 0 0,1 21,5V19A2,2 0 0,1 19,21H5C3.89,21 3,20.1 3,19V5M7,18H9L9.35,16H13.35L13,18H15L15.35,16H17.35L17.71,14H15.71L16.41,10H18.41L18.76,8H16.76L17.12,6H15.12L14.76,8H10.76L11.12,6H9.12L8.76,8H6.76L6.41,10H8.41L7.71,14H5.71L5.35,16H7.35L7,18M10.41,10H14.41L13.71,14H9.71L10.41,10Z"/>
</vector>

View File

@ -4,58 +4,14 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginTop="?attr/actionBarSize">
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<android.support.v7.widget.CardView
android:id="@+id/rootSwitchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"
app:cardCornerRadius="0dp"
android:visibility="gone">
<Switch
android:id="@+id/root_toggle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:checked="true"
android:text="@string/root_toggle"
android:textSize="16sp"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/selinuxSwitchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:elevation="2dp"
app:cardCornerRadius="0dp"
android:visibility="gone">
<Switch
android:id="@+id/selinux_toggle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:checked="true"
android:text="@string/selinux_toggle"
android:textSize="16sp"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/magiskStatusView"
android:layout_width="match_parent"
@ -64,8 +20,7 @@
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_marginTop="6dp"
app:cardUseCompatPadding="true"
android:visibility="gone">
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
@ -76,6 +31,7 @@
android:id="@+id/magisk_status_container"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="?android:attr/selectableItemBackground"
android:foregroundGravity="center"
android:orientation="vertical">
@ -85,6 +41,12 @@
android:layout_height="84dp"
android:layout_gravity="center"/>
<ProgressBar
android:id="@+id/progressBarVersion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</FrameLayout>
<TextView
@ -98,19 +60,17 @@
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/rootStatusView"
android:id="@+id/app_updateView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_marginTop="6dp"
app:cardUseCompatPadding="true"
android:visibility="gone">
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
@ -118,22 +78,29 @@
android:orientation="vertical">
<FrameLayout
android:id="@+id/root_status_container"
android:id="@+id/app_check_updates_container"
android:layout_width="match_parent"
android:layout_height="100dp"
android:foregroundGravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/root_status_icon"
android:id="@+id/app_check_updates_icon"
android:layout_width="84dp"
android:layout_height="84dp"
android:layout_gravity="center"
android:src="@drawable/ic_check_circle"/>
<ProgressBar
android:id="@+id/app_check_updates_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</FrameLayout>
<TextView
android:id="@+id/root_status"
android:id="@+id/app_check_updates_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
@ -146,51 +113,14 @@
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/safetynetStatusView"
android:id="@+id/magisk_updateView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_marginTop="6dp"
app:cardUseCompatPadding="true"
android:visibility="gone">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="4dp">
<ImageView
android:id="@+id/safety_net_icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:tint="#757575"/>
<TextView
android:id="@+id/safety_net_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="6dp"
android:textStyle="bold"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/selinuxStatusView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_marginTop="6dp"
app:cardUseCompatPadding="true"
android:visibility="gone">
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
@ -198,22 +128,30 @@
android:orientation="vertical">
<FrameLayout
android:id="@+id/selinux_status_container"
android:id="@+id/magisk_check_updates_container"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="?android:attr/selectableItemBackground"
android:foregroundGravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/selinux_status_icon"
android:id="@+id/magisk_check_updates_icon"
android:layout_width="84dp"
android:layout_height="84dp"
android:layout_gravity="center"
android:src="@drawable/ic_check_circle"/>
<ProgressBar
android:id="@+id/magisk_check_updates_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</FrameLayout>
<TextView
android:id="@+id/selinux_status"
android:id="@+id/magisk_check_updates_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
@ -224,7 +162,6 @@
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>

View File

@ -0,0 +1,184 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginTop="?attr/actionBarSize">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<android.support.v7.widget.CardView
android:id="@+id/rootSwitchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"
android:visibility="gone"
app:cardCornerRadius="0dp">
<Switch
android:id="@+id/root_toggle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:checked="true"
android:text="@string/root_toggle"
android:textSize="16sp"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/selinuxSwitchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"
android:visibility="gone"
app:cardCornerRadius="0dp">
<Switch
android:id="@+id/selinux_toggle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:checked="true"
android:text="@string/selinux_toggle"
android:textSize="16sp"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/rootStatusView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_marginTop="6dp"
android:visibility="gone"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:id="@+id/root_status_container"
android:layout_width="match_parent"
android:layout_height="100dp"
android:foregroundGravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/root_status_icon"
android:layout_width="84dp"
android:layout_height="84dp"
android:layout_gravity="center"/>
</FrameLayout>
<TextView
android:id="@+id/root_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:padding="6dp"
android:textStyle="bold"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/safetynetStatusView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_marginTop="6dp"
android:visibility="gone"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="4dp">
<ImageView
android:id="@+id/safety_net_icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:tint="#757575"/>
<TextView
android:id="@+id/safety_net_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="6dp"
android:textStyle="bold"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/selinuxStatusView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_marginTop="6dp"
android:visibility="gone"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:id="@+id/selinux_status_container"
android:layout_width="match_parent"
android:layout_height="100dp"
android:foregroundGravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/selinux_status_icon"
android:layout_width="84dp"
android:layout_height="84dp"
android:layout_gravity="center"/>
</FrameLayout>
<TextView
android:id="@+id/selinux_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:padding="6dp"
android:textStyle="bold"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>

View File

@ -10,6 +10,11 @@
android:icon="@drawable/magisk"
android:title="@string/magisk"/>
<item
android:id="@+id/root"
android:icon="@drawable/root"
android:title="@string/root"/>
<item
android:id="@+id/modules"
android:icon="@drawable/ic_extension"

View File

@ -9,6 +9,7 @@
<color name="red500">#F44336</color>
<color name="green500">#4CAF50</color>
<color name="blue500">#2196F3</color>
<color name="grey500">#9E9E9E</color>
</resources>

View File

@ -64,5 +64,10 @@
<string name="never_show_again">Don\'t show again</string>
<string name="error_download_file">Error downloading file</string>
<string name="flash_recovery">File downloaded in Magisk directory, you can flash it in recovery</string>
<string name="root">Root</string>
<string name="app_update_available">An update for Magisk Manager is available!</string>
<string name="magisk_update_available">An update for Magisk is available!</string>
<string name="cannot_check_updates">Cannot check for updates</string>
<string name="up_to_date">You\'ve got the latest version available of %1$s</string>
</resources>