mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-06-13 13:47:44 +02:00
Stop using platform provided DownloadManager
This commit is contained in:
@ -1,7 +0,0 @@
|
||||
package a;
|
||||
|
||||
import com.topjohnwu.magisk.NoUIActivity;
|
||||
|
||||
public class g extends NoUIActivity {
|
||||
/* stub */
|
||||
}
|
@ -1,75 +0,0 @@
|
||||
package com.topjohnwu.magisk.components;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Bundle;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.topjohnwu.magisk.R;
|
||||
import com.topjohnwu.magisk.utils.Download;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
|
||||
public abstract class BaseActivity extends FlavorActivity {
|
||||
|
||||
public static final String INTENT_PERM = "perm_dialog";
|
||||
|
||||
protected static Runnable permissionGrantCallback;
|
||||
|
||||
public static void runWithPermission(Context context, String[] permissions, Runnable callback) {
|
||||
boolean granted = true;
|
||||
for (String perm : permissions) {
|
||||
if (ContextCompat.checkSelfPermission(context, perm) != PackageManager.PERMISSION_GRANTED)
|
||||
granted = false;
|
||||
}
|
||||
if (granted) {
|
||||
Download.EXTERNAL_PATH.mkdirs();
|
||||
callback.run();
|
||||
} else {
|
||||
// Passed in context should be an activity if not granted, need to show dialog!
|
||||
permissionGrantCallback = callback;
|
||||
if (!(context instanceof BaseActivity)) {
|
||||
// Start activity to show dialog
|
||||
Intent intent = new Intent(context, a.g.class);
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
intent.putExtra(INTENT_PERM, permissions);
|
||||
context.startActivity(intent);
|
||||
} else {
|
||||
ActivityCompat.requestPermissions((BaseActivity) context, permissions, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void runWithPermission(String[] permissions, Runnable callback) {
|
||||
runWithPermission(this, permissions, callback);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
String[] perms = getIntent().getStringArrayExtra(INTENT_PERM);
|
||||
if (perms != null)
|
||||
ActivityCompat.requestPermissions(this, perms, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||
boolean grant = true;
|
||||
for (int result : grantResults) {
|
||||
if (result != PackageManager.PERMISSION_GRANTED)
|
||||
grant = false;
|
||||
}
|
||||
if (grant) {
|
||||
if (permissionGrantCallback != null) {
|
||||
permissionGrantCallback.run();
|
||||
}
|
||||
} else {
|
||||
Toast.makeText(this, R.string.no_rw_storage, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
permissionGrantCallback = null;
|
||||
}
|
||||
}
|
@ -1,58 +0,0 @@
|
||||
package com.topjohnwu.magisk.receivers;
|
||||
|
||||
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.widget.Toast;
|
||||
|
||||
import com.topjohnwu.magisk.R;
|
||||
import com.topjohnwu.magisk.utils.Download;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public abstract class DownloadReceiver extends BroadcastReceiver {
|
||||
protected File mFile;
|
||||
private long downloadID;
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
DownloadManager 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:
|
||||
Uri uri = Uri.parse(c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)));
|
||||
onDownloadDone(context, uri);
|
||||
break;
|
||||
default:
|
||||
Toast.makeText(context, R.string.download_file_error, Toast.LENGTH_LONG).show();
|
||||
break;
|
||||
}
|
||||
context.unregisterReceiver(this);
|
||||
}
|
||||
c.close();
|
||||
}
|
||||
Download.isDownloading = false;
|
||||
}
|
||||
|
||||
public DownloadReceiver setDownloadID(long id) {
|
||||
downloadID = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public DownloadReceiver setFile(File file) {
|
||||
mFile = file;
|
||||
return this;
|
||||
}
|
||||
|
||||
public abstract void onDownloadDone(Context context, Uri uri);
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.topjohnwu.magisk.receivers;
|
||||
package com.topjohnwu.magisk.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
@ -9,22 +9,20 @@ import java.io.File;
|
||||
|
||||
import androidx.core.content.FileProvider;
|
||||
|
||||
public class ManagerInstall extends DownloadReceiver {
|
||||
@Override
|
||||
public void onDownloadDone(Context context, Uri uri) {
|
||||
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(context,
|
||||
context.getPackageName() + ".provider", new File(uri.getPath()));
|
||||
Uri content = FileProvider.getUriForFile(c, c.getPackageName() + ".provider", apk);
|
||||
install.setData(content);
|
||||
context.startActivity(install);
|
||||
c.startActivity(install);
|
||||
} else {
|
||||
Intent install = new Intent(Intent.ACTION_VIEW);
|
||||
install.setDataAndType(uri, "application/vnd.android.package-archive");
|
||||
install.setDataAndType(Uri.fromFile(apk), "application/vnd.android.package-archive");
|
||||
install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
context.startActivity(install);
|
||||
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