mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-11-12 21:02:58 +00:00
Stop using platform provided DownloadManager
This commit is contained in:
28
app/src/main/java/com/topjohnwu/magisk/utils/APKInstall.java
Normal file
28
app/src/main/java/com/topjohnwu/magisk/utils/APKInstall.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package com.topjohnwu.magisk.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import androidx.core.content.FileProvider;
|
||||
|
||||
public class APKInstall {
|
||||
public static void install(Context c, File apk) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
Intent install = new Intent(Intent.ACTION_INSTALL_PACKAGE);
|
||||
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
Uri content = FileProvider.getUriForFile(c, c.getPackageName() + ".provider", apk);
|
||||
install.setData(content);
|
||||
c.startActivity(install);
|
||||
} else {
|
||||
Intent install = new Intent(Intent.ACTION_VIEW);
|
||||
install.setDataAndType(Uri.fromFile(apk), "application/vnd.android.package-archive");
|
||||
install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
c.startActivity(install);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,9 @@
|
||||
package com.topjohnwu.magisk.utils;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.DownloadManager;
|
||||
import android.content.Context;
|
||||
import android.content.IntentFilter;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.net.Uri;
|
||||
import android.os.Environment;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.topjohnwu.magisk.R;
|
||||
import com.topjohnwu.magisk.components.BaseActivity;
|
||||
import com.topjohnwu.magisk.receivers.DownloadReceiver;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@@ -25,33 +16,6 @@ public class Download {
|
||||
EXTERNAL_PATH.mkdirs();
|
||||
}
|
||||
|
||||
public static boolean isDownloading = false;
|
||||
|
||||
public static void receive(Context context, DownloadReceiver receiver, String link, String filename) {
|
||||
if (isDownloading)
|
||||
return;
|
||||
|
||||
BaseActivity.runWithPermission(context,
|
||||
new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, () -> {
|
||||
File file = new File(EXTERNAL_PATH, getLegalFilename(filename));
|
||||
file.delete();
|
||||
|
||||
Toast.makeText(context, context.getString(R.string.downloading_toast, filename),
|
||||
Toast.LENGTH_LONG).show();
|
||||
|
||||
isDownloading = true;
|
||||
|
||||
DownloadManager.Request request = new DownloadManager
|
||||
.Request(Uri.parse(link))
|
||||
.setDestinationUri(Uri.fromFile(file));
|
||||
|
||||
DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
|
||||
receiver.setDownloadID(dm.enqueue(request)).setFile(file);
|
||||
context.getApplicationContext().registerReceiver(receiver,
|
||||
new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
|
||||
});
|
||||
}
|
||||
|
||||
public static String getLegalFilename(CharSequence filename) {
|
||||
return filename.toString().replace(" ", "_").replace("'", "").replace("\"", "")
|
||||
.replace("$", "").replace("`", "").replace("*", "").replace("/", "_")
|
||||
|
||||
@@ -1,79 +1,24 @@
|
||||
package com.topjohnwu.magisk.utils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class WebService {
|
||||
|
||||
public static String getString(String url) {
|
||||
return getString(url, null);
|
||||
}
|
||||
|
||||
public static String getString(String url, Map<String, String> header) {
|
||||
try {
|
||||
HttpURLConnection conn = request(url, header);
|
||||
return getString(conn);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static String getString(HttpURLConnection conn) {
|
||||
try {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
|
||||
try (BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
|
||||
int len;
|
||||
char buf[] = new char[4096];
|
||||
while ((len = br.read(buf)) != -1) {
|
||||
builder.append(buf, 0, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
conn.disconnect();
|
||||
return builder.toString();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static HttpURLConnection request(String address, Map<String, String> header) throws IOException {
|
||||
public static HttpURLConnection request(String address) throws IOException {
|
||||
URL url = new URL(address);
|
||||
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setReadTimeout(15000);
|
||||
conn.setConnectTimeout(15000);
|
||||
|
||||
if (header != null) {
|
||||
for (Map.Entry<String, String> entry : header.entrySet()) {
|
||||
conn.setRequestProperty(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
conn.connect();
|
||||
|
||||
if (header != null) {
|
||||
header.clear();
|
||||
for (Map.Entry<String, List<String>> entry : conn.getHeaderFields().entrySet()) {
|
||||
List<String> l = entry.getValue();
|
||||
header.put(entry.getKey(), l.get(l.size() - 1));
|
||||
}
|
||||
}
|
||||
|
||||
return conn;
|
||||
}
|
||||
|
||||
public static HttpURLConnection mustRequest(String address, Map<String, String> header) throws IOException {
|
||||
public static HttpURLConnection mustRequest(String address) throws IOException {
|
||||
HttpURLConnection conn;
|
||||
do {
|
||||
conn = WebService.request(address, header);
|
||||
conn = WebService.request(address);
|
||||
int total = conn.getContentLength();
|
||||
if (total < 0)
|
||||
conn.disconnect();
|
||||
|
||||
Reference in New Issue
Block a user