diff --git a/app/src/full/java/com/topjohnwu/magisk/FlashActivity.java b/app/src/full/java/com/topjohnwu/magisk/FlashActivity.java index 0e419330a..e5c33eed0 100644 --- a/app/src/full/java/com/topjohnwu/magisk/FlashActivity.java +++ b/app/src/full/java/com/topjohnwu/magisk/FlashActivity.java @@ -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); diff --git a/app/src/full/java/com/topjohnwu/magisk/MagiskLogFragment.java b/app/src/full/java/com/topjohnwu/magisk/MagiskLogFragment.java index 4be68540b..bbd4677b8 100644 --- a/app/src/full/java/com/topjohnwu/magisk/MagiskLogFragment.java +++ b/app/src/full/java/com/topjohnwu/magisk/MagiskLogFragment.java @@ -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() { diff --git a/app/src/full/java/com/topjohnwu/magisk/adapters/ReposAdapter.java b/app/src/full/java/com/topjohnwu/magisk/adapters/ReposAdapter.java index fc337462d..7eb283946 100644 --- a/app/src/full/java/com/topjohnwu/magisk/adapters/ReposAdapter.java +++ b/app/src/full/java/com/topjohnwu/magisk/adapters/ReposAdapter.java @@ -100,16 +100,15 @@ public class ReposAdapter extends SectionedAdapter { - 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(); }); diff --git a/app/src/full/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java b/app/src/full/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java index a8ae8be61..daccc3bf8 100644 --- a/app/src/full/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java +++ b/app/src/full/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java @@ -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 { 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 { } 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 { activity.buttonPanel.setVisibility(View.VISIBLE); } } - - @Override - public void exec(Void... voids) { - getActivity().runWithPermission( - new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, super::exec); - } } diff --git a/app/src/full/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java b/app/src/full/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java index c006d7a22..b7e94c27b 100644 --- a/app/src/full/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java +++ b/app/src/full/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java @@ -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 { 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 { 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 { } 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 { 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 { 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 { 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; } diff --git a/app/src/full/java/com/topjohnwu/magisk/components/InstallMethodDialog.java b/app/src/full/java/com/topjohnwu/magisk/components/InstallMethodDialog.java index 16e902c72..2ae011aa4 100644 --- a/app/src/full/java/com/topjohnwu/magisk/components/InstallMethodDialog.java +++ b/app/src/full/java/com/topjohnwu/magisk/components/InstallMethodDialog.java @@ -19,7 +19,7 @@ import java.util.List; class InstallMethodDialog extends AlertDialog.Builder { - InstallMethodDialog(BaseActivity activity, List options, String filename) { + InstallMethodDialog(BaseActivity activity, List 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) { diff --git a/app/src/full/java/com/topjohnwu/magisk/components/MagiskInstallDialog.java b/app/src/full/java/com/topjohnwu/magisk/components/MagiskInstallDialog.java index 039193188..52a3ca4f6 100644 --- a/app/src/full/java/com/topjohnwu/magisk/components/MagiskInstallDialog.java +++ b/app/src/full/java/com/topjohnwu/magisk/components/MagiskInstallDialog.java @@ -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)) { diff --git a/app/src/full/java/com/topjohnwu/magisk/container/Repo.java b/app/src/full/java/com/topjohnwu/magisk/container/Repo.java index 5b7718b1f..48031cf84 100644 --- a/app/src/full/java/com/topjohnwu/magisk/container/Repo.java +++ b/app/src/full/java/com/topjohnwu/magisk/container/Repo.java @@ -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); diff --git a/app/src/main/java/com/topjohnwu/magisk/components/BaseActivity.java b/app/src/main/java/com/topjohnwu/magisk/components/BaseActivity.java index acbffdd18..51d8378f7 100644 --- a/app/src/main/java/com/topjohnwu/magisk/components/BaseActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/components/BaseActivity.java @@ -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! diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Download.java b/app/src/main/java/com/topjohnwu/magisk/utils/Download.java index 498c713ca..183781127 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Download.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Download.java @@ -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();