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:
@@ -1,20 +1,23 @@
|
||||
package com.topjohnwu.magisk;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.ProgressDialog;
|
||||
import android.app.DownloadManager;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.ActivityCompat;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.content.FileProvider;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.text.Html;
|
||||
import android.text.TextUtils;
|
||||
@@ -26,22 +29,19 @@ import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.topjohnwu.magisk.utils.DownloadReceiver;
|
||||
import com.topjohnwu.magisk.utils.Shell;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.List;
|
||||
|
||||
import butterknife.BindColor;
|
||||
@@ -50,7 +50,7 @@ import butterknife.ButterKnife;
|
||||
|
||||
public class MagiskFragment extends Fragment {
|
||||
|
||||
private static final String JSON_UPDATE_CHECK = "https://raw.githubusercontent.com/topjohnwu/MagiskManager/master/app/magisk_update.json";
|
||||
private static final String JSON_UPDATE_CHECK = "https://raw.githubusercontent.com/topjohnwu/MagiskManager/updates/magisk_update.json";
|
||||
|
||||
@BindView(R.id.progressBarVersion) ProgressBar progressBar;
|
||||
|
||||
@@ -82,6 +82,7 @@ public class MagiskFragment extends Fragment {
|
||||
private String mLastLink;
|
||||
private boolean mLastIsApp;
|
||||
private List<String> version;
|
||||
private long apkID, zipID;
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
@@ -100,7 +101,7 @@ public class MagiskFragment extends Fragment {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
|
||||
if (requestCode == 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
new DownloadFile(getContext(), mLastLink, mLastIsApp).execute();
|
||||
downloadFile();
|
||||
} else {
|
||||
Toast.makeText(getContext(), R.string.permissionNotGranted, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
@@ -123,32 +124,73 @@ public class MagiskFragment extends Fragment {
|
||||
String text = app ? getString(R.string.app_name) : getString(R.string.magisk);
|
||||
final String msg = getString(R.string.update_available_message, text, versionCode, changelog);
|
||||
|
||||
clickView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
new AlertDialog.Builder(getContext())
|
||||
.setTitle(R.string.update_available)
|
||||
.setMessage(Html.fromHtml(msg))
|
||||
.setCancelable(false)
|
||||
.setPositiveButton(R.string.update, new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialogInterface, int i) {
|
||||
mLastLink = link;
|
||||
mLastIsApp = app;
|
||||
clickView.setOnClickListener(view -> new AlertDialog.Builder(getContext())
|
||||
.setTitle(R.string.update_available)
|
||||
.setMessage(Html.fromHtml(msg))
|
||||
.setCancelable(false)
|
||||
.setPositiveButton(R.string.update, (dialogInterface, i) -> {
|
||||
mLastLink = link;
|
||||
mLastIsApp = app;
|
||||
|
||||
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
|
||||
&& Build.VERSION.SDK_INT >= 23) {
|
||||
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
|
||||
return;
|
||||
}
|
||||
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
|
||||
downloadFile();
|
||||
} else {
|
||||
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
|
||||
}
|
||||
|
||||
new DownloadFile(getContext(), link, app).execute();
|
||||
}
|
||||
})
|
||||
.setNegativeButton(R.string.no_thanks, null)
|
||||
.show();
|
||||
}
|
||||
});
|
||||
})
|
||||
.setNegativeButton(R.string.no_thanks, null)
|
||||
.show());
|
||||
}
|
||||
|
||||
private void downloadFile() {
|
||||
|
||||
File downloadFile, dir = new File(Environment.getExternalStorageDirectory() + "/MagiskManager");
|
||||
DownloadReceiver receiver;
|
||||
|
||||
if (mLastIsApp) {
|
||||
downloadFile = new File(dir + "/MagiskManager.apk");
|
||||
|
||||
} else {
|
||||
downloadFile = new File(dir + "/Magisk.zip");
|
||||
}
|
||||
|
||||
if (!dir.exists()) dir.mkdir();
|
||||
|
||||
DownloadManager downloadManager = (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE);
|
||||
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(mLastLink));
|
||||
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
|
||||
.setDestinationUri(Uri.fromFile(downloadFile));
|
||||
|
||||
if (downloadFile.exists()) downloadFile.delete();
|
||||
|
||||
long downloadID = downloadManager.enqueue(request);
|
||||
|
||||
if (mLastIsApp) {
|
||||
receiver = new DownloadReceiver(downloadID) {
|
||||
@Override
|
||||
public void task(File file) {
|
||||
Intent install = new Intent(Intent.ACTION_INSTALL_PACKAGE);
|
||||
install.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
install.setData(FileProvider.getUriForFile(context, "com.topjohnwu.magisk.provider", file));
|
||||
context.startActivity(install);
|
||||
}
|
||||
};
|
||||
} else {
|
||||
receiver = new DownloadReceiver(downloadID) {
|
||||
@Override
|
||||
public void task(final File file) {
|
||||
new AlertDialog.Builder(context)
|
||||
.setTitle("Reboot Recovery")
|
||||
.setMessage("Do you want to flash in recovery now?")
|
||||
.setCancelable(false)
|
||||
.setPositiveButton("Yes, flash now", (dialogInterface, i) -> Toast.makeText(context, file.getPath(), Toast.LENGTH_LONG).show())
|
||||
.setNegativeButton(R.string.no_thanks, null)
|
||||
.show();
|
||||
}
|
||||
};
|
||||
}
|
||||
getActivity().registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
|
||||
}
|
||||
|
||||
private class updateUI extends AsyncTask<Void, Void, Void> {
|
||||
@@ -164,11 +206,9 @@ public class MagiskFragment extends Fragment {
|
||||
protected void onPostExecute(Void v) {
|
||||
super.onPostExecute(v);
|
||||
|
||||
progressBar.setVisibility(View.GONE);
|
||||
|
||||
version = Shell.sh("getprop magisk.version");
|
||||
|
||||
if (version.isEmpty()) {
|
||||
if (version.get(0).replaceAll("\\s", "").isEmpty()) {
|
||||
magiskStatusContainer.setBackgroundColor(grey500);
|
||||
magiskStatusIcon.setImageResource(statusUnknown);
|
||||
|
||||
@@ -182,6 +222,7 @@ public class MagiskFragment extends Fragment {
|
||||
magiskVersion.setText(getString(R.string.magisk_version, version.get(0)));
|
||||
}
|
||||
|
||||
progressBar.setVisibility(View.GONE);
|
||||
magiskStatusView.setVisibility(View.VISIBLE);
|
||||
}
|
||||
}
|
||||
@@ -251,7 +292,7 @@ public class MagiskFragment extends Fragment {
|
||||
appCheckUpdatesStatus.setText(getString(R.string.up_to_date, getString(R.string.app_name)));
|
||||
}
|
||||
|
||||
String v = version.isEmpty() ? "" : version.get(0);
|
||||
String v = version.get(0).replaceAll("\\s", "");
|
||||
|
||||
int versionInt = TextUtils.isEmpty(v) ? 0 : Integer.parseInt(v);
|
||||
|
||||
@@ -267,103 +308,4 @@ public class MagiskFragment extends Fragment {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class DownloadFile extends AsyncTask<Void, Integer, Boolean> {
|
||||
|
||||
private final Context context;
|
||||
private final String link;
|
||||
private final File downloadFile;
|
||||
private final ProgressDialog progress;
|
||||
|
||||
public DownloadFile(Context context, String link, boolean apk) {
|
||||
this.link = link;
|
||||
this.context = context;
|
||||
|
||||
File dir = new File(Environment.getExternalStorageDirectory() + "/Magisk");
|
||||
|
||||
if (!dir.exists()) dir.mkdir();
|
||||
|
||||
if (apk) {
|
||||
downloadFile = new File(dir + "/MagiskManager.apk");
|
||||
} else {
|
||||
downloadFile = new File(dir + "/Magisk.zip");
|
||||
}
|
||||
|
||||
Toast.makeText(context, downloadFile.getPath(), Toast.LENGTH_SHORT).show();
|
||||
|
||||
progress = new ProgressDialog(getContext());
|
||||
progress.setTitle(null);
|
||||
progress.setMessage(getString(R.string.loading));
|
||||
progress.setIndeterminate(true);
|
||||
progress.setCancelable(false);
|
||||
progress.setButton(android.app.AlertDialog.BUTTON_POSITIVE, getString(android.R.string.cancel), new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialogInterface, int i) {
|
||||
cancel(true);
|
||||
}
|
||||
});
|
||||
progress.show();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Boolean doInBackground(Void... voids) {
|
||||
try {
|
||||
URL u = new URL(link);
|
||||
URLConnection conn = u.openConnection();
|
||||
conn.connect();
|
||||
|
||||
int length = conn.getContentLength();
|
||||
|
||||
InputStream input = new BufferedInputStream(u.openStream(), 8192);
|
||||
OutputStream output = new FileOutputStream(downloadFile);
|
||||
|
||||
byte data[] = new byte[1024];
|
||||
long total = 0;
|
||||
|
||||
int count;
|
||||
while ((count = input.read(data)) != -1) {
|
||||
total += count;
|
||||
output.write(data, 0, count);
|
||||
|
||||
publishProgress((int) ((total * 100) / length));
|
||||
}
|
||||
|
||||
output.flush();
|
||||
|
||||
output.close();
|
||||
input.close();
|
||||
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onProgressUpdate(Integer... values) {
|
||||
super.onProgressUpdate(values);
|
||||
|
||||
progress.setMessage(getString(R.string.loading) + " " + values[0] + "%");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Boolean result) {
|
||||
super.onPostExecute(result);
|
||||
progress.dismiss();
|
||||
if (!result) {
|
||||
Toast.makeText(context, R.string.error_download_file, Toast.LENGTH_LONG).show();
|
||||
return;
|
||||
}
|
||||
|
||||
if (downloadFile.getPath().contains("apk")) {
|
||||
Intent intent = new Intent(Intent.ACTION_VIEW);
|
||||
intent.setDataAndType(Uri.fromFile(downloadFile), "application/vnd.android.package-archive");
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
startActivity(intent);
|
||||
} else {
|
||||
Toast.makeText(context, R.string.flash_recovery, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user