Commit 830fde80 authored by topjohnwu's avatar topjohnwu

Download with DownloadManager

parent c44ce77e
# Magisk Manager
Because I love to stay on cutting edge, the project can only be compiled on Android Studio Version 2.2.0+ (currently in beta)
apply plugin: 'com.android.application' apply plugin: 'com.android.application'
apply plugin: 'android-apt' //apply plugin: 'android-apt'
android { android {
compileSdkVersion 24 compileSdkVersion 24
...@@ -11,13 +11,21 @@ android { ...@@ -11,13 +11,21 @@ android {
targetSdkVersion 24 targetSdkVersion 24
versionCode 4 versionCode 4
versionName "2.0" versionName "2.0"
jackOptions {
enabled true
}
} }
buildTypes { buildTypes {
release { release {
minifyEnabled false minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
} }
} }
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
} }
dependencies { dependencies {
...@@ -26,6 +34,6 @@ dependencies { ...@@ -26,6 +34,6 @@ dependencies {
compile 'com.android.support:cardview-v7:24.2.0' compile 'com.android.support:cardview-v7:24.2.0'
compile 'com.android.support:design:24.2.0' compile 'com.android.support:design:24.2.0'
compile 'com.jakewharton:butterknife:8.2.1' compile 'com.jakewharton:butterknife:8.4.0'
apt 'com.jakewharton:butterknife-compiler:8.2.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
} }
...@@ -26,6 +26,15 @@ ...@@ -26,6 +26,15 @@
<activity <activity
android:name=".AboutActivity" android:name=".AboutActivity"
android:theme="@style/AppTheme.Transparent"/> android:theme="@style/AppTheme.Transparent"/>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.topjohnwu.magisk.provider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application> </application>
</manifest> </manifest>
\ No newline at end of file
...@@ -85,10 +85,6 @@ public class ModulesFragment extends Fragment { ...@@ -85,10 +85,6 @@ public class ModulesFragment extends Fragment {
@Override @Override
protected Void doInBackground(Void... voids) { protected Void doInBackground(Void... voids) {
listModules.clear();
listModulesCache.clear();
listModules.clear(); listModules.clear();
listModulesCache.clear(); listModulesCache.clear();
List<String> magisk = Utils.getModList(MAGISK_PATH); List<String> magisk = Utils.getModList(MAGISK_PATH);
...@@ -98,7 +94,6 @@ public class ModulesFragment extends Fragment { ...@@ -98,7 +94,6 @@ public class ModulesFragment extends Fragment {
listModules.add(new Module(mod)); listModules.add(new Module(mod));
} }
} }
if (!magiskCache.isEmpty()) { if (!magiskCache.isEmpty()) {
for (String mod : magiskCache) { for (String mod : magiskCache) {
listModulesCache.add(new Module(mod)); listModulesCache.add(new Module(mod));
......
...@@ -2,6 +2,7 @@ package com.topjohnwu.magisk; ...@@ -2,6 +2,7 @@ package com.topjohnwu.magisk;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.view.LayoutInflater; import android.view.LayoutInflater;
...@@ -63,20 +64,14 @@ public class RootFragment extends Fragment { ...@@ -63,20 +64,14 @@ public class RootFragment extends Fragment {
new updateUI().execute(); new updateUI().execute();
rootToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { rootToggle.setOnClickListener(toggle -> {
@Override Shell.su(((CompoundButton) toggle).isChecked() ? "setprop magisk.root 1" : "setprop magisk.root 0");
public void onCheckedChanged(CompoundButton compoundButton, boolean b) { new Handler().postDelayed(() -> new updateUI().execute(), 1000);
Shell.su(b ? "setprop magisk.root 1" : "setprop magisk.root 0");
new updateUI().execute();
}
}); });
selinuxToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { selinuxToggle.setOnClickListener(toggle -> {
@Override Shell.su(((CompoundButton) toggle).isChecked() ? "setenforce 1" : "setenforce 0");
public void onCheckedChanged(CompoundButton compoundButton, boolean b) { new Handler().postDelayed(() -> new updateUI().execute(), 1000);
Shell.su(b ? "setenforce 1" : "setenforce 0");
new updateUI().execute();
}
}); });
return view; return view;
...@@ -132,7 +127,6 @@ public class RootFragment extends Fragment { ...@@ -132,7 +127,6 @@ public class RootFragment extends Fragment {
} }
if (new File("/system/framework/twframework.jar").exists()) { if (new File("/system/framework/twframework.jar").exists()) {
selinuxToggleView.setVisibility(View.GONE);
selinuxStatus.append("\n" + getString(R.string.selinux_samsung_info)); selinuxStatus.append("\n" + getString(R.string.selinux_samsung_info));
} }
......
...@@ -71,12 +71,7 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView ...@@ -71,12 +71,7 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView
if (savedInstanceState == null) { if (savedInstanceState == null) {
mDrawerHandler.removeCallbacksAndMessages(null); mDrawerHandler.removeCallbacksAndMessages(null);
mDrawerHandler.postDelayed(new Runnable() { mDrawerHandler.postDelayed(() -> navigate(mSelectedId), 250);
@Override
public void run() {
navigate(mSelectedId);
}
}, 250);
} }
navigationView.setNavigationItemSelectedListener(this); navigationView.setNavigationItemSelectedListener(this);
...@@ -103,12 +98,7 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView ...@@ -103,12 +98,7 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView
public boolean onNavigationItemSelected(@NonNull final MenuItem menuItem) { public boolean onNavigationItemSelected(@NonNull final MenuItem menuItem) {
mSelectedId = menuItem.getItemId(); mSelectedId = menuItem.getItemId();
mDrawerHandler.removeCallbacksAndMessages(null); mDrawerHandler.removeCallbacksAndMessages(null);
mDrawerHandler.postDelayed(new Runnable() { mDrawerHandler.postDelayed(() -> navigate(menuItem.getItemId()), 250);
@Override
public void run() {
navigate(menuItem.getItemId());
}
}, 250);
drawer.closeDrawer(GravityCompat.START); drawer.closeDrawer(GravityCompat.START);
return true; return true;
......
package com.topjohnwu.magisk.utils;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;
import com.topjohnwu.magisk.R;
import java.io.File;
/**
* Created by topjohnwu on 2016/8/27.
*/
public abstract class DownloadReceiver extends BroadcastReceiver{
public Context context;
DownloadManager downloadManager;
long downloadID;
public DownloadReceiver(long downloadID) {
this.downloadID = downloadID;
}
@Override
public void onReceive(Context context, Intent intent) {
this.context = context;
downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
String action = intent.getAction();
if(DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)){
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(downloadID);
Cursor c = downloadManager.query(query);
if (c.moveToFirst()) {
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
int status = c.getInt(columnIndex);
switch (status) {
case DownloadManager.STATUS_SUCCESSFUL:
File file = new File(Uri.parse(c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))).getPath());
task(file);
break;
default:
Toast.makeText(context, R.string.error_download_file, Toast.LENGTH_LONG).show();
break;
}
context.unregisterReceiver(this);
}
}
}
public abstract void task(File file);
}
...@@ -14,10 +14,8 @@ import java.util.List; ...@@ -14,10 +14,8 @@ import java.util.List;
public class Shell { public class Shell {
public static String suPath;
// -1 = problematic/unknown issue; 0 = not rooted; 1 = properly rooted; 2 = improperly rooted; // -1 = problematic/unknown issue; 0 = not rooted; 1 = properly rooted; 2 = improperly rooted;
public static int rootStatus = 0; public static int rootStatus;
private static Process rootShell; private static Process rootShell;
private static DataOutputStream rootSTDIN; private static DataOutputStream rootSTDIN;
...@@ -29,27 +27,27 @@ public class Shell { ...@@ -29,27 +27,27 @@ public class Shell {
} }
private static void init() { private static void init() {
List<String> ret = sh("getprop magisk.supath");
if(!ret.isEmpty()) {
suPath = ret.get(0) + "/su";
rootStatus = 1;
} else {
suPath = "su";
rootStatus = 2;
}
try { try {
rootShell = Runtime.getRuntime().exec(suPath); rootShell = Runtime.getRuntime().exec(sh("getprop magisk.supath").get(0) + "/su");
rootSTDIN = new DataOutputStream(rootShell.getOutputStream()); rootStatus = 1;
rootSTDOUT = new StreamGobbler(rootShell.getInputStream(), rootOutList);
rootSTDOUT.start();
} catch (IOException e) { } catch (IOException e) {
// runtime error! No binary found! Means no root try {
rootStatus = 0; // Improper root
return; rootShell = Runtime.getRuntime().exec("su");
rootStatus = 2;
} catch (IOException err) {
// No root
rootStatus = 0;
return;
}
} }
ret = su("echo -BOC-", "id"); rootSTDIN = new DataOutputStream(rootShell.getOutputStream());
rootSTDOUT = new StreamGobbler(rootShell.getInputStream(), rootOutList);
rootSTDOUT.start();
List<String> ret = su("echo -BOC-", "id");
if (ret == null) { if (ret == null) {
// Something wrong with root, not allowed? // Something wrong with root, not allowed?
rootStatus = -1; rootStatus = -1;
...@@ -74,7 +72,6 @@ public class Shell { ...@@ -74,7 +72,6 @@ public class Shell {
if (rootAccess()) { if (rootAccess()) {
rootSTDIN.write("exit\n".getBytes("UTF-8")); rootSTDIN.write("exit\n".getBytes("UTF-8"));
rootSTDIN.flush(); rootSTDIN.flush();
rootSTDIN.flush();
rootShell.waitFor(); rootShell.waitFor();
rootSTDIN.close(); rootSTDIN.close();
rootSTDOUT.join(); rootSTDOUT.join();
...@@ -134,39 +131,37 @@ public class Shell { ...@@ -134,39 +131,37 @@ public class Shell {
rootOutList.clear(); rootOutList.clear();
try { try {
try { for (String write : commands) {
for (String write : commands) { rootSTDIN.write((write + "\n").getBytes("UTF-8"));
rootSTDIN.write((write + "\n").getBytes("UTF-8"));
rootSTDIN.flush();
}
rootSTDIN.write(("echo \'-done-\'\n").getBytes("UTF-8"));
rootSTDIN.flush(); rootSTDIN.flush();
} catch (IOException e) {
if (!e.getMessage().contains("EPIPE")) {
throw e;
}
} }
rootSTDIN.write(("echo \' \'\n").getBytes("UTF-8"));
rootSTDIN.flush();
rootSTDIN.write(("echo \'-done-\'\n").getBytes("UTF-8"));
rootSTDIN.flush();
} catch (IOException e) {
if (!e.getMessage().contains("EPIPE")) {
return null;
}
}
while (true) { while (true) {
try { try {
// Process terminated, it means the interactive shell cannot be initialized // Process terminated, it means the interactive shell cannot be initialized
rootShell.exitValue(); rootShell.exitValue();
return null; return null;
} catch (IllegalThreadStateException e) { } catch (IllegalThreadStateException e) {
// Process still running, gobble output until done // Process still running, gobble output until done
if (rootOutList != null && !rootOutList.isEmpty()) { int end = rootOutList.size() - 1;
if (rootOutList.get(rootOutList.size() - 1).equals("-done-")) { if (rootOutList != null && end > 0) {
rootOutList.remove(rootOutList.size() - 1); if (rootOutList.get(end).equals("-done-")) {
break; rootOutList.remove(end);
} rootOutList.remove(end - 1);
break;
} }
rootSTDOUT.join(100);
} }
try { rootSTDOUT.join(100); } catch (InterruptedException err) { return null; }
} }
} catch (IOException | InterruptedException e) {
// shell probably not found
return null;
} }
return new ArrayList<>(rootOutList); return new ArrayList<>(rootOutList);
......
...@@ -36,8 +36,7 @@ public class StreamGobbler extends Thread { ...@@ -36,8 +36,7 @@ public class StreamGobbler extends Thread {
try { try {
String line; String line;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
if (!line.replaceAll("\\s", "").isEmpty()) writer.add(line);
writer.add(line);
} }
} catch (IOException e) { } catch (IOException e) {
// reader probably closed, expected exit condition // reader probably closed, expected exit condition
......
...@@ -37,7 +37,7 @@ public class Utils { ...@@ -37,7 +37,7 @@ public class Utils {
public static List<String> readFile(String path) { public static List<String> readFile(String path) {
List<String> ret; List<String> ret;
ret = Shell.sh("cat " + path); ret = Shell.sh("cat " + path);
if (ret.isEmpty() && Shell.rootAccess()) ret = Shell.su("cat " + path, "echo \' \'"); if (ret.isEmpty() && Shell.rootAccess()) ret = Shell.su("cat " + path);
return ret; return ret;
} }
......
...@@ -12,14 +12,14 @@ ...@@ -12,14 +12,14 @@
<string name="root_mounted">Root mounted</string> <string name="root_mounted">Root mounted</string>
<string name="root_mounted_info">Root mounted and enabled. Safety Net (Android Pay) will NOT work</string> <string name="root_mounted_info">Root mounted and enabled. Safety Net (Android Pay) will NOT work</string>
<string name="root_unmounted">Root not mounted</string> <string name="root_unmounted">Root not mounted</string>
<string name="root_unmounted_info">Safety Net (Android Pay) should work, but no root temporarily</string> <string name="root_unmounted_info">Safety Net (Android Pay) should work, but no root temporarily\nYou might need to manually add a card in Android Pay app to refresh the root status of AP</string>
<string name="root_system">Improperly Installed</string> <string name="root_system">Improperly Installed</string>
<string name="root_system_info">Root improperly installed. Safety Net (Android Pay) will NOT work, and impossible to toggle</string> <string name="root_system_info">Root improperly installed. Safety Net (Android Pay) will NOT work, and impossible to toggle</string>
<string name="selinux_error_info">SELinux Status Unknown</string> <string name="selinux_error_info">SELinux Status Unknown</string>
<string name="selinux_enforcing_info">SELinux is enforced</string> <string name="selinux_enforcing_info">SELinux is enforced</string>
<string name="selinux_permissive_info">SELinux is permissive\nOnly turn off SELinux if necessary!</string> <string name="selinux_permissive_info">SELinux is permissive\nOnly turn off SELinux if necessary!</string>
<string name="selinux_samsung_info">Samsung do not support switching SELinux status!</string> <string name="selinux_samsung_info">Samsung need custom kernel for switching SELinux status!</string>
<string name="root_toggle">Root Toggle</string> <string name="root_toggle">Root Toggle</string>
<string name="selinux_toggle">SELinux Toggle</string> <string name="selinux_toggle">SELinux Toggle</string>
......
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="external_files" path="."/>
</paths>
\ No newline at end of file
...@@ -6,8 +6,7 @@ buildscript { ...@@ -6,8 +6,7 @@ buildscript {
mavenCentral() mavenCentral()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:2.1.3' classpath 'com.android.tools.build:gradle:2.2.0-beta1'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files // in the individual module build.gradle files
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment