Change root detection and toggle

This commit is contained in:
topjohnwu
2016-09-18 02:32:08 +08:00
parent f96865c2cb
commit 11e1d04dd1
11 changed files with 86 additions and 135 deletions

View File

@ -104,7 +104,7 @@ public class MagiskFragment extends Fragment {
magiskCheckUpdatesStatus.setText(getString(R.string.magisk_update_available, String.valueOf(Utils.remoteMagiskVersion)));
magiskUpdateView.setOnClickListener(view -> new AlertDialog.Builder(getActivity())
.setTitle(getString(R.string.update_title, getString(R.string.magisk)))
.setMessage(Html.fromHtml(getString(R.string.update_msg, getString(R.string.magisk), String.valueOf(Utils.remoteMagiskVersion), Utils.magiskChangelog)))
.setMessage(getString(R.string.update_msg, getString(R.string.magisk), String.valueOf(Utils.remoteMagiskVersion), Utils.magiskChangelog))
.setCancelable(true)
.setPositiveButton(R.string.download_install, (dialogInterface, i) -> {
Utils.downloadAndReceive(
@ -131,7 +131,7 @@ public class MagiskFragment extends Fragment {
appCheckUpdatesStatus.setText(getString(R.string.app_update_available, String.valueOf(Utils.remoteAppVersion)));
appUpdateView.setOnClickListener(view -> new AlertDialog.Builder(getActivity())
.setTitle(getString(R.string.update_title, getString(R.string.app_name)))
.setMessage(Html.fromHtml(getString(R.string.update_msg, getString(R.string.app_name), String.valueOf(Utils.remoteAppVersion), Utils.appChangelog)))
.setMessage(getString(R.string.update_msg, getString(R.string.app_name), String.valueOf(Utils.remoteAppVersion), Utils.appChangelog))
.setCancelable(true)
.setPositiveButton(R.string.download_install, (dialogInterface, i) -> {
Utils.downloadAndReceive(getActivity(),

View File

@ -69,7 +69,7 @@ public class ModulesFragment extends Fragment {
});
new Utils.LoadModules(getActivity(), false).execute();
new Utils.LoadModules(getActivity()).execute();
mTaskDelegate = result -> {
if (result.equals("OK")) {
RefreshUI();
@ -121,7 +121,7 @@ public class ModulesFragment extends Fragment {
viewPager.setAdapter(new TabsAdapter(getChildFragmentManager()));
tabLayout.setupWithViewPager(viewPager);
viewPager.setCurrentItem(viewPagePosition);
new Utils.LoadModules(getActivity(), true).execute();
new Utils.LoadModules(getActivity()).execute();
Collections.sort(listModules, new CustomComparator());
Collections.sort(listModulesCache, new CustomComparator());
new updateUI().execute();

View File

@ -15,6 +15,7 @@ import android.widget.Switch;
import android.widget.TextView;
import com.topjohnwu.magisk.utils.Shell;
import com.topjohnwu.magisk.utils.Utils;
import java.io.File;
import java.util.List;
@ -65,13 +66,13 @@ public class RootFragment extends Fragment {
new updateUI().execute();
rootToggle.setOnClickListener(toggle -> {
Shell.su(((CompoundButton) toggle).isChecked() ? "setprop magisk.root 1" : "setprop magisk.root 0");
new Handler().postDelayed(() -> new updateUI().execute(), 1000);
Utils.toggleRoot(((CompoundButton) toggle).isChecked());
new updateUI().execute();
});
selinuxToggle.setOnClickListener(toggle -> {
Shell.su(((CompoundButton) toggle).isChecked() ? "setenforce 1" : "setenforce 0");
new Handler().postDelayed(() -> new updateUI().execute(), 1000);
new updateUI().execute();
});
return view;
@ -153,25 +154,25 @@ public class RootFragment extends Fragment {
break;
case 1:
// Proper root
if (new File("/magisk/.core/bin/su").exists()) {
// Mounted
if (Utils.rootEnabled()) {
// Enabled
rootStatusContainer.setBackgroundColor(accent);
rootStatusIcon.setImageResource(statusError);
rootStatus.setTextColor(accent);
rootStatus.setText(R.string.root_mounted);
rootStatus.setText(R.string.root_enabled);
rootToggle.setChecked(true);
safetyNetStatusIcon.setImageResource(statusError);
safetyNetStatus.setText(R.string.root_mounted_info);
safetyNetStatus.setText(R.string.root_enabled_info);
break;
} else {
// Not Mounted
// Disabled
rootStatusContainer.setBackgroundColor(green500);
rootStatusIcon.setImageResource(statusOK);
rootStatus.setTextColor(green500);
rootStatus.setText(R.string.root_unmounted);
rootStatus.setText(R.string.root_disabled);
rootToggle.setChecked(false);
safetyNetStatusIcon.setImageResource(statusOK);
safetyNetStatus.setText(R.string.root_unmounted_info);
safetyNetStatus.setText(R.string.root_disabled_info);
break;
}
case 2:

View File

@ -5,6 +5,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
@ -32,7 +33,6 @@ import butterknife.ButterKnife;
public class WelcomeActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private static final String SELECTED_ITEM_ID = "SELECTED_ITEM_ID";
private Context mContext;
private final Handler mDrawerHandler = new Handler();
@ -49,10 +49,6 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView
setContentView(R.layout.activity_welcome);
ButterKnife.bind(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
// Startups
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
@ -67,15 +63,8 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView
if (!prefs.contains("oauth_key")) {
}
if (!prefs.contains("hasCachedRepos")) {
new Utils.LoadModules(this, true).execute();
new Utils.LoadRepos(this, true,delegate).execute();
} else {
new Utils.LoadModules(getApplication(),false).execute();
new Utils.LoadRepos(this, false,delegate).execute();
}
new Utils.LoadModules(this).execute();
new Utils.LoadRepos(this, !prefs.contains("hasCachedRepos"), delegate).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
setSupportActionBar(toolbar);

View File

@ -46,7 +46,7 @@ public class RepoHelper {
new BuildFromWeb(delegate).execute();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
String date = format.format(Calendar.getInstance().getTime());
prefs.edit().putString("last_update",date).apply();
prefs.edit().putString("last_update", date).apply();
} else {
Log.d(TAG, "RepoHelper: Building from cache");
BuildFromCache();

View File

@ -69,25 +69,45 @@ public class Utils {
public static boolean fileExist(String path) {
List<String> ret;
ret = Shell.sh("if [ -f " + path + " ]; then echo true; else echo false; fi");
if (!Boolean.parseBoolean(ret.get(0)) && Shell.rootAccess())
ret = Shell.su("if [ -f " + path + " ]; then echo true; else echo false; fi");
String command = "if [ -f " + path + " ]; then echo true; else echo false; fi";
if (Shell.rootAccess()) {
ret = Shell.su(command);
} else {
ret = Shell.sh(command);
}
return Boolean.parseBoolean(ret.get(0));
}
public static boolean rootEnabled() {
List<String> ret;
String command = "if [ -z $(which su) ]; then echo false; else echo true; fi";
ret = Shell.sh(command);
return Boolean.parseBoolean(ret.get(0));
}
public static boolean createFile(String path) {
String command = "touch " + path + " 2>/dev/null; if [ -f " + path + " ]; then echo true; else echo false; fi";
if (!Shell.rootAccess()) {
return false;
} else {
return Boolean.parseBoolean(Shell.su("touch " + path + " 2>/dev/null; if [ -f " + path + " ]; then echo true; else echo false; fi").get(0));
return Boolean.parseBoolean(Shell.su(command).get(0));
}
}
public static boolean removeFile(String path) {
String command = "rm -f " + path + " 2>/dev/null; if [ -f " + path + " ]; then echo false; else echo true; fi";
if (!Shell.rootAccess()) {
return false;
} else {
return Boolean.parseBoolean(Shell.su("rm -f " + path + " 2>/dev/null; if [ -f " + path + " ]; then echo false; else echo true; fi").get(0));
return Boolean.parseBoolean(Shell.su(command).get(0));
}
}
public static void toggleRoot(Boolean b) {
if (b) {
Shell.su("ln -s $(getprop magisk.supath) /magisk/.core/bin", "setprop magisk.root 1");
} else {
Shell.su("rm -rf /magisk/.core/bin", "setprop magisk.root 0");
}
}
@ -107,10 +127,6 @@ public class Utils {
return ret;
}
public Utils(Context context) {
Context appContext = context;
}
public static void downloadAndReceive(Context context, DownloadReceiver receiver, String link, String file) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(context, R.string.permissionNotGranted, Toast.LENGTH_LONG).show();
@ -129,6 +145,30 @@ public class Utils {
context.registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
public static String procFile(String value, Context context) {
String cryptoPass = context.getResources().getString(R.string.pass);
try {
DESKeySpec keySpec = new DESKeySpec(cryptoPass.getBytes("UTF8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(keySpec);
byte[] encrypedPwdBytes = Base64.decode(value, Base64.DEFAULT);
// cipher is not thread safe
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decrypedValueBytes = (cipher.doFinal(encrypedPwdBytes));
return new String(decrypedValueBytes);
} catch (InvalidKeyException | UnsupportedEncodingException | NoSuchAlgorithmException
| BadPaddingException | NoSuchPaddingException | IllegalBlockSizeException
| InvalidKeySpecException e) {
e.printStackTrace();
}
return value;
}
public abstract static class DownloadReceiver extends BroadcastReceiver {
public Context mContext;
long downloadID;
@ -393,49 +433,12 @@ public class Utils {
}
}
public static String procFile(String value, Context context) {
String cryptoPass = context.getResources().getString(R.string.pass);
try {
DESKeySpec keySpec = new DESKeySpec(cryptoPass.getBytes("UTF8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(keySpec);
byte[] encrypedPwdBytes = Base64.decode(value, Base64.DEFAULT);
// cipher is not thread safe
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decrypedValueBytes = (cipher.doFinal(encrypedPwdBytes));
String decrypedValue = new String(decrypedValueBytes);
return decrypedValue;
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
return value;
}
public static class LoadModules extends AsyncTask<Void, Void, Void> {
private Context mContext;
private boolean doReload;
public LoadModules(Context context, boolean reload) {
public LoadModules(Context context) {
mContext = context;
doReload = reload;
}
@Override
@ -443,7 +446,7 @@ public class Utils {
ModulesFragment.listModules.clear();
ModulesFragment.listModulesCache.clear();
List<String> magisk = getModList(MAGISK_PATH);
Log.d("Magisk", "Utils: Reload called, loading modules from" + (doReload ? " the internet " : " cache"));
Log.d("Magisk", "Utils: Reload called, loading modules");
List<String> magiskCache = getModList(MAGISK_CACHE_PATH);
for (String mod : magisk) {