You've already forked Magisk
mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-09-06 06:36:58 +00:00
Download with DownloadManager
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
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;
|
||||
|
||||
public class Shell {
|
||||
|
||||
|
||||
public static String suPath;
|
||||
// -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 DataOutputStream rootSTDIN;
|
||||
@@ -29,27 +27,27 @@ public class Shell {
|
||||
}
|
||||
|
||||
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 {
|
||||
rootShell = Runtime.getRuntime().exec(suPath);
|
||||
rootSTDIN = new DataOutputStream(rootShell.getOutputStream());
|
||||
rootSTDOUT = new StreamGobbler(rootShell.getInputStream(), rootOutList);
|
||||
rootSTDOUT.start();
|
||||
rootShell = Runtime.getRuntime().exec(sh("getprop magisk.supath").get(0) + "/su");
|
||||
rootStatus = 1;
|
||||
} catch (IOException e) {
|
||||
// runtime error! No binary found! Means no root
|
||||
rootStatus = 0;
|
||||
return;
|
||||
try {
|
||||
// Improper root
|
||||
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) {
|
||||
// Something wrong with root, not allowed?
|
||||
rootStatus = -1;
|
||||
@@ -74,7 +72,6 @@ public class Shell {
|
||||
if (rootAccess()) {
|
||||
rootSTDIN.write("exit\n".getBytes("UTF-8"));
|
||||
rootSTDIN.flush();
|
||||
rootSTDIN.flush();
|
||||
rootShell.waitFor();
|
||||
rootSTDIN.close();
|
||||
rootSTDOUT.join();
|
||||
@@ -134,39 +131,37 @@ public class Shell {
|
||||
rootOutList.clear();
|
||||
|
||||
try {
|
||||
try {
|
||||
for (String write : commands) {
|
||||
rootSTDIN.write((write + "\n").getBytes("UTF-8"));
|
||||
rootSTDIN.flush();
|
||||
}
|
||||
rootSTDIN.write(("echo \'-done-\'\n").getBytes("UTF-8"));
|
||||
for (String write : commands) {
|
||||
rootSTDIN.write((write + "\n").getBytes("UTF-8"));
|
||||
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) {
|
||||
try {
|
||||
// Process terminated, it means the interactive shell cannot be initialized
|
||||
rootShell.exitValue();
|
||||
return null;
|
||||
} catch (IllegalThreadStateException e) {
|
||||
// Process still running, gobble output until done
|
||||
if (rootOutList != null && !rootOutList.isEmpty()) {
|
||||
if (rootOutList.get(rootOutList.size() - 1).equals("-done-")) {
|
||||
rootOutList.remove(rootOutList.size() - 1);
|
||||
break;
|
||||
}
|
||||
while (true) {
|
||||
try {
|
||||
// Process terminated, it means the interactive shell cannot be initialized
|
||||
rootShell.exitValue();
|
||||
return null;
|
||||
} catch (IllegalThreadStateException e) {
|
||||
// Process still running, gobble output until done
|
||||
int end = rootOutList.size() - 1;
|
||||
if (rootOutList != null && end > 0) {
|
||||
if (rootOutList.get(end).equals("-done-")) {
|
||||
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);
|
||||
|
||||
@@ -36,8 +36,7 @@ public class StreamGobbler extends Thread {
|
||||
try {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
if (!line.replaceAll("\\s", "").isEmpty())
|
||||
writer.add(line);
|
||||
writer.add(line);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// reader probably closed, expected exit condition
|
||||
|
||||
@@ -37,7 +37,7 @@ public class Utils {
|
||||
public static List<String> readFile(String path) {
|
||||
List<String> ret;
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user