Do not place files into /sdcard/MagiskManager

This commit is contained in:
topjohnwu 2018-08-25 16:00:27 -04:00
parent 3948e67c8f
commit 9bbfcf326c
10 changed files with 58 additions and 66 deletions

View File

@ -1,9 +1,9 @@
package com.topjohnwu.magisk;
import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
@ -57,25 +57,26 @@ public class FlashActivity extends BaseActivity {
@OnClick(R.id.save_logs)
void saveLogs() {
Calendar now = Calendar.getInstance();
String filename = String.format(Locale.US,
"install_log_%04d%02d%02d_%02d%02d%02d.log",
now.get(Calendar.YEAR), now.get(Calendar.MONTH) + 1,
now.get(Calendar.DAY_OF_MONTH), now.get(Calendar.HOUR_OF_DAY),
now.get(Calendar.MINUTE), now.get(Calendar.SECOND));
runWithPermission(new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, () -> {
Calendar now = Calendar.getInstance();
String filename = String.format(Locale.US,
"magisk_install_log_%04d%02d%02d_%02d%02d%02d.log",
now.get(Calendar.YEAR), now.get(Calendar.MONTH) + 1,
now.get(Calendar.DAY_OF_MONTH), now.get(Calendar.HOUR_OF_DAY),
now.get(Calendar.MINUTE), now.get(Calendar.SECOND));
File logFile = new File(Download.EXTERNAL_PATH + "/logs", filename);
logFile.getParentFile().mkdirs();
try (FileWriter writer = new FileWriter(logFile)) {
for (String s : logs) {
writer.write(s);
writer.write('\n');
File logFile = new File(Download.EXTERNAL_PATH, filename);
try (FileWriter writer = new FileWriter(logFile)) {
for (String s : logs) {
writer.write(s);
writer.write('\n');
}
} catch (IOException e) {
e.printStackTrace();
return;
}
} catch (IOException e) {
e.printStackTrace();
return;
}
Utils.toast(logFile.getPath(), Toast.LENGTH_LONG);
Utils.toast(logFile.getPath(), Toast.LENGTH_LONG);
});
}
@Override
@ -158,7 +159,7 @@ public class FlashActivity extends BaseActivity {
@Override
protected void onPostExecute(Integer result) {
if (result == 1) {
new Handler().postDelayed(() ->
Data.mainHandler.postDelayed(() ->
RootUtils.uninstallPkg(getActivity().getPackageName()), 3000);
} else {
super.onPostExecute(result);

View File

@ -108,16 +108,15 @@ public class MagiskLogFragment extends BaseFragment {
now.get(Calendar.DAY_OF_MONTH), now.get(Calendar.HOUR_OF_DAY),
now.get(Calendar.MINUTE), now.get(Calendar.SECOND));
File targetFile = new File(Download.EXTERNAL_PATH + "/logs", filename);
targetFile.getParentFile().mkdirs();
File logFile = new File(Download.EXTERNAL_PATH, filename);
try {
targetFile.createNewFile();
logFile.createNewFile();
} catch (IOException e) {
return;
}
Shell.su("cat " + Const.MAGISK_LOG + " > " + targetFile)
Shell.su("cat " + Const.MAGISK_LOG + " > " + logFile)
.submit(result ->
SnackbarMaker.make(txtLog, targetFile.getPath(), Snackbar.LENGTH_SHORT).show());
SnackbarMaker.make(txtLog, logFile.getPath(), Snackbar.LENGTH_SHORT).show());
}
public void clearLogs() {

View File

@ -100,16 +100,15 @@ public class ReposAdapter extends SectionedAdapter<ReposAdapter.SectionHolder, R
new MarkDownWindow((BaseActivity) context, null, repo.getDetailUrl()).exec());
holder.downloadImage.setOnClickListener(v -> {
String filename = repo.getName() + "-" + repo.getVersion() + ".zip";
new CustomAlertDialog((BaseActivity) context)
.setTitle(context.getString(R.string.repo_install_title, repo.getName()))
.setMessage(context.getString(R.string.repo_install_msg, filename))
.setMessage(context.getString(R.string.repo_install_msg, repo.getDownloadFilename()))
.setCancelable(true)
.setPositiveButton(R.string.install, (d, i) ->
new ProcessRepoZip((BaseActivity) context, repo.getZipUrl(), filename, true).exec()
new ProcessRepoZip((BaseActivity) context, repo, true).exec()
)
.setNeutralButton(R.string.download, (d, i) ->
new ProcessRepoZip((BaseActivity) context, repo.getZipUrl(), filename, false).exec())
new ProcessRepoZip((BaseActivity) context, repo, false).exec())
.setNegativeButton(R.string.no_thanks, null)
.show();
});

View File

@ -1,6 +1,5 @@
package com.topjohnwu.magisk.asyncs;
import android.Manifest;
import android.app.Activity;
import android.app.ProgressDialog;
import android.net.Uri;
@ -15,7 +14,6 @@ import com.topjohnwu.magisk.Data;
import com.topjohnwu.magisk.FlashActivity;
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.components.BaseActivity;
import com.topjohnwu.magisk.container.TarEntry;
import com.topjohnwu.magisk.utils.Download;
import com.topjohnwu.magisk.utils.Utils;
@ -79,11 +77,6 @@ public class InstallMagisk extends ParallelTask<Void, Void, Boolean> {
bootUri = boot;
}
@Override
protected BaseActivity getActivity() {
return (BaseActivity) super.getActivity();
}
@Override
protected void onPreExecute() {
if (mode == FIX_ENV_MODE) {
@ -137,10 +130,7 @@ public class InstallMagisk extends ParallelTask<Void, Void, Boolean> {
}
private void extractFiles(String arch) throws IOException {
String filename = Utils.fmt("Magisk-v%s(%d).zip",
Data.remoteMagiskVersionString, Data.remoteMagiskVersionCode);
File zip = new File(Download.EXTERNAL_PATH, filename);
zip.getParentFile().mkdirs();
File zip = new File(mm.getFilesDir(), "magisk.zip");
BufferedInputStream buf;
if (!ShellUtils.checkSum("MD5", zip, Data.magiskMD5)) {
@ -404,10 +394,4 @@ public class InstallMagisk extends ParallelTask<Void, Void, Boolean> {
activity.buttonPanel.setVisibility(View.VISIBLE);
}
}
@Override
public void exec(Void... voids) {
getActivity().runWithPermission(
new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, super::exec);
}
}

View File

@ -4,15 +4,16 @@ import android.Manifest;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.widget.Toast;
import com.topjohnwu.magisk.Const;
import com.topjohnwu.magisk.Data;
import com.topjohnwu.magisk.FlashActivity;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.components.BaseActivity;
import com.topjohnwu.magisk.components.SnackbarMaker;
import com.topjohnwu.magisk.container.Repo;
import com.topjohnwu.magisk.utils.Download;
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.magisk.utils.WebService;
@ -38,17 +39,15 @@ public class ProcessRepoZip extends ParallelTask<Void, Object, Boolean> {
private ProgressDialog progressDialog;
private boolean mInstall;
private String mLink;
private File mFile;
private Repo mRepo;
private int progress = 0, total = -1;
private Handler mHandler;
public ProcessRepoZip(BaseActivity context, String link, String filename, boolean install) {
public ProcessRepoZip(BaseActivity context, Repo repo, boolean install) {
super(context);
mLink = link;
mFile = new File(Download.EXTERNAL_PATH, Download.getLegalFilename(filename));
mInstall = install;
mHandler = new Handler();
mRepo = repo;
mInstall = install && Shell.rootAccess();
mFile = new File(Download.EXTERNAL_PATH, repo.getDownloadFilename());
}
private void removeTopFolder(File input, File output) throws IOException {
@ -93,7 +92,7 @@ public class ProcessRepoZip extends ParallelTask<Void, Object, Boolean> {
if (activity == null) return null;
try {
// Request zip from Internet
HttpURLConnection conn = WebService.mustRequest(mLink, null);
HttpURLConnection conn = WebService.mustRequest(mRepo.getZipUrl(), null);
total = conn.getContentLength();
// Temp files
@ -111,7 +110,7 @@ public class ProcessRepoZip extends ParallelTask<Void, Object, Boolean> {
}
conn.disconnect();
mHandler.post(() -> {
Data.mainHandler.post(() -> {
progressDialog.setTitle(R.string.zip_process_title);
progressDialog.setMessage(getActivity().getString(R.string.zip_process_msg));
});
@ -140,7 +139,7 @@ public class ProcessRepoZip extends ParallelTask<Void, Object, Boolean> {
progressDialog.dismiss();
if (result) {
Uri uri = Uri.fromFile(mFile);
if (Shell.rootAccess() && mInstall) {
if (mInstall) {
Intent intent = new Intent(activity, FlashActivity.class);
intent.setData(uri).putExtra(Const.Key.FLASH_ACTION, Const.Value.FLASH_ZIP);
activity.startActivity(intent);
@ -175,7 +174,7 @@ public class ProcessRepoZip extends ParallelTask<Void, Object, Boolean> {
public synchronized int read() throws IOException {
int b = super.read();
if (b > 0) {
mHandler.post(() -> updateDlProgress(1));
Data.mainHandler.post(() -> updateDlProgress(1));
}
return b;
}
@ -189,7 +188,7 @@ public class ProcessRepoZip extends ParallelTask<Void, Object, Boolean> {
public synchronized int read(@NonNull byte[] b, int off, int len) throws IOException {
int read = super.read(b, off, len);
if (read > 0) {
mHandler.post(() -> updateDlProgress(read));
Data.mainHandler.post(() -> updateDlProgress(read));
}
return read;
}

View File

@ -19,7 +19,7 @@ import java.util.List;
class InstallMethodDialog extends AlertDialog.Builder {
InstallMethodDialog(BaseActivity activity, List<String> options, String filename) {
InstallMethodDialog(BaseActivity activity, List<String> options) {
super(activity);
setTitle(R.string.select_method);
setItems(options.toArray(new String [0]), (dialog, idx) -> {
@ -45,6 +45,8 @@ class InstallMethodDialog extends AlertDialog.Builder {
});
break;
case 0:
String filename = Utils.fmt("Magisk-v%s(%d).zip",
Data.remoteMagiskVersionString, Data.remoteMagiskVersionCode);
Download.receive(activity, new DownloadReceiver() {
@Override
public void onDownloadDone(Context context, Uri uri) {

View File

@ -34,7 +34,7 @@ public class MagiskInstallDialog extends CustomAlertDialog {
if (!s.isEmpty() && Boolean.parseBoolean(s)) {
options.add(mm.getString(R.string.install_inactive_slot));
}
new InstallMethodDialog(activity, options, filename).show();
new InstallMethodDialog(activity, options).show();
});
setNegativeButton(R.string.no_thanks, null);
if (!TextUtils.isEmpty(Data.magiskNoteLink)) {

View File

@ -4,6 +4,7 @@ import android.content.ContentValues;
import android.database.Cursor;
import com.topjohnwu.magisk.Const;
import com.topjohnwu.magisk.utils.Download;
import com.topjohnwu.magisk.utils.Logger;
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.magisk.utils.WebService;
@ -82,6 +83,10 @@ public class Repo extends BaseModule {
return mLastUpdate;
}
public String getDownloadFilename() {
return Download.getLegalFilename(getName() + "-" + getVersion() + ".zip");
}
public class IllegalRepoException extends Exception {
IllegalRepoException(String message) {
super(message);

View File

@ -12,6 +12,7 @@ import android.widget.Toast;
import com.topjohnwu.magisk.NoUIActivity;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.utils.Download;
public abstract class BaseActivity extends FlavorActivity {
@ -26,6 +27,7 @@ public abstract class BaseActivity extends FlavorActivity {
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!

View File

@ -10,6 +10,7 @@ import android.net.Uri;
import android.os.Environment;
import android.widget.Toast;
import com.topjohnwu.magisk.Data;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.components.BaseActivity;
import com.topjohnwu.magisk.receivers.DownloadReceiver;
@ -19,7 +20,11 @@ import java.io.File;
public class Download {
public static final File EXTERNAL_PATH =
new File(Environment.getExternalStorageDirectory(), "MagiskManager");
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
static {
EXTERNAL_PATH.mkdirs();
}
public static boolean isDownloading = false;
@ -30,11 +35,7 @@ public class Download {
BaseActivity.runWithPermission(context,
new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, () -> {
File file = new File(EXTERNAL_PATH, getLegalFilename(filename));
if ((!file.getParentFile().exists() && !file.getParentFile().mkdirs())
|| (file.exists() && !file.delete())) {
return;
}
file.delete();
Toast.makeText(context, context.getString(R.string.downloading_toast, filename),
Toast.LENGTH_LONG).show();