From 6fb032b3c2e1b7f8763bc56976110c4ab31e9e22 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 20 Jul 2019 22:37:34 -0700 Subject: [PATCH] Clean ups --- .../main/java/com/topjohnwu/magisk/Config.kt | 30 ++++--- .../main/java/com/topjohnwu/magisk/Info.java | 1 + .../data/repository/MagiskRepository.kt | 24 ------ .../topjohnwu/magisk/model/flash/Patching.kt | 2 +- .../magisk/tasks/MagiskInstaller.java | 8 +- .../topjohnwu/magisk/utils/ProgInputStream.kt | 35 ++++++++ .../com/topjohnwu/magisk/utils/XNetwork.kt | 80 ------------------- .../magisk/view/dialogs/EnvFixDialog.kt | 9 ++- 8 files changed, 66 insertions(+), 123 deletions(-) create mode 100644 app/src/main/java/com/topjohnwu/magisk/utils/ProgInputStream.kt delete mode 100644 app/src/main/java/com/topjohnwu/magisk/utils/XNetwork.kt diff --git a/app/src/main/java/com/topjohnwu/magisk/Config.kt b/app/src/main/java/com/topjohnwu/magisk/Config.kt index e1f4f891d..a84ce4ecc 100644 --- a/app/src/main/java/com/topjohnwu/magisk/Config.kt +++ b/app/src/main/java/com/topjohnwu/magisk/Config.kt @@ -1,6 +1,7 @@ package com.topjohnwu.magisk import android.content.Context +import android.content.SharedPreferences import android.util.Xml import androidx.core.content.edit import com.topjohnwu.magisk.data.database.SettingsDao @@ -137,6 +138,22 @@ object Config : PreferenceModel, DBConfig { } fun initialize() = prefs.edit { + parsePrefs(this) + + if (!prefs.contains(Key.UPDATE_CHANNEL)) + putString(Key.UPDATE_CHANNEL, defaultChannel.toString()) + + // Get actual state + putBoolean(Key.COREONLY, Const.MAGISK_DISABLE_FILE.exists()) + + // Write database configs + putString(Key.ROOT_ACCESS, rootMode.toString()) + putString(Key.SU_MNT_NS, suMntNamespaceMode.toString()) + putString(Key.SU_MULTIUSER_MODE, suMultiuserMode.toString()) + putBoolean(Key.SU_FINGERPRINT, FingerprintHelper.useFingerprint()) + } + + private fun parsePrefs(editor: SharedPreferences.Editor) = editor.apply { val config = SuFile.open("/data/adb", Const.MANAGER_CONFIGS) if (config.exists()) runCatching { val input = SuFileInputStream(config).buffered() @@ -184,19 +201,8 @@ object Config : PreferenceModel, DBConfig { } } config.delete() + remove(Key.ETAG_KEY) } - remove(Key.ETAG_KEY) - if (!prefs.contains(Key.UPDATE_CHANNEL)) - putString(Key.UPDATE_CHANNEL, defaultChannel.toString()) - - // Get actual state - putBoolean(Key.COREONLY, Const.MAGISK_DISABLE_FILE.exists()) - - // Write database configs - putString(Key.ROOT_ACCESS, rootMode.toString()) - putString(Key.SU_MNT_NS, suMntNamespaceMode.toString()) - putString(Key.SU_MULTIUSER_MODE, suMultiuserMode.toString()) - putBoolean(Key.SU_FINGERPRINT, FingerprintHelper.useFingerprint()) } @JvmStatic diff --git a/app/src/main/java/com/topjohnwu/magisk/Info.java b/app/src/main/java/com/topjohnwu/magisk/Info.java index 1d9a1b1db..4de190ed6 100644 --- a/app/src/main/java/com/topjohnwu/magisk/Info.java +++ b/app/src/main/java/com/topjohnwu/magisk/Info.java @@ -13,6 +13,7 @@ public final class Info { @NonNull public static String magiskVersionString = ""; + @NonNull public static UpdateInfo remote = new UpdateInfo(); public static boolean keepVerity = false; diff --git a/app/src/main/java/com/topjohnwu/magisk/data/repository/MagiskRepository.kt b/app/src/main/java/com/topjohnwu/magisk/data/repository/MagiskRepository.kt index c3973ca39..bfdf9ffe2 100644 --- a/app/src/main/java/com/topjohnwu/magisk/data/repository/MagiskRepository.kt +++ b/app/src/main/java/com/topjohnwu/magisk/data/repository/MagiskRepository.kt @@ -12,7 +12,6 @@ import com.topjohnwu.magisk.model.entity.HideTarget import com.topjohnwu.magisk.utils.Utils import com.topjohnwu.magisk.utils.inject import com.topjohnwu.magisk.utils.toSingle -import com.topjohnwu.magisk.utils.writeToCachedFile import com.topjohnwu.superuser.Shell import io.reactivex.Single @@ -22,25 +21,8 @@ class MagiskRepository( private val packageManager: PackageManager ) { - fun fetchMagisk() = fetchUpdate() - .flatMap { apiRaw.fetchFile(it.magisk.link) } - .map { it.writeToCachedFile(context, FILE_MAGISK_ZIP) } - - fun fetchManager() = fetchUpdate() - .flatMap { apiRaw.fetchFile(it.app.link) } - .map { it.writeToCachedFile(context, FILE_MAGISK_APK) } - - fun fetchUninstaller() = fetchUpdate() - .flatMap { apiRaw.fetchFile(it.uninstaller.link) } - .map { it.writeToCachedFile(context, FILE_UNINSTALLER_ZIP) } - fun fetchSafetynet() = apiRaw.fetchSafetynet() - fun fetchBootctl() = apiRaw - .fetchBootctl() - .map { it.writeToCachedFile(context, FILE_BOOTCTL_SH) } - - fun fetchUpdate() = when (Config.updateChannel) { Config.Value.DEFAULT_CHANNEL, Config.Value.STABLE_CHANNEL -> apiRaw.fetchStableUpdate() Config.Value.BETA_CHANNEL -> apiRaw.fetchBetaUpdate() @@ -83,12 +65,6 @@ class MagiskRepository( private val Boolean.state get() = if (this) "add" else "rm" companion object { - const val FILE_MAGISK_ZIP = "magisk.zip" - const val FILE_MAGISK_APK = "magisk.apk" - const val FILE_UNINSTALLER_ZIP = "uninstaller.zip" - const val FILE_SAFETY_NET_APK = "safetynet.apk" - const val FILE_BOOTCTL_SH = "bootctl" - private val blacklist = listOf( let { val app: App by inject(); app }.packageName, "android", diff --git a/app/src/main/java/com/topjohnwu/magisk/model/flash/Patching.kt b/app/src/main/java/com/topjohnwu/magisk/model/flash/Patching.kt index 1973320bb..cb63165f5 100644 --- a/app/src/main/java/com/topjohnwu/magisk/model/flash/Patching.kt +++ b/app/src/main/java/com/topjohnwu/magisk/model/flash/Patching.kt @@ -9,7 +9,7 @@ sealed class Patching( private val console: MutableList, logs: MutableList, private val resultListener: FlashResultListener -) : MagiskInstaller(console, logs) { +) : MagiskInstaller(file, console, logs) { override fun onResult(success: Boolean) { if (success) { diff --git a/app/src/main/java/com/topjohnwu/magisk/tasks/MagiskInstaller.java b/app/src/main/java/com/topjohnwu/magisk/tasks/MagiskInstaller.java index 2a5325a75..630c1feb5 100644 --- a/app/src/main/java/com/topjohnwu/magisk/tasks/MagiskInstaller.java +++ b/app/src/main/java/com/topjohnwu/magisk/tasks/MagiskInstaller.java @@ -29,7 +29,6 @@ import org.kamranzafar.jtar.TarOutputStream; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; @@ -45,7 +44,7 @@ public abstract class MagiskInstaller { protected String srcBoot; protected File destFile; protected File installDir; - protected File zipFile = new File(App.self.getCacheDir(), "magisk.zip"); + protected Uri zipUri; private final List console; private final List logs; @@ -56,9 +55,10 @@ public abstract class MagiskInstaller { logs = NOPList.getInstance(); } - public MagiskInstaller(List out, List err) { + public MagiskInstaller(Uri zip, List out, List err) { console = out; logs = err; + zipUri = zip; installDir = new File(App.deContext.getFilesDir().getParent(), "install"); Shell.sh("rm -rf " + installDir).exec(); installDir.mkdirs(); @@ -105,7 +105,7 @@ public abstract class MagiskInstaller { try { ZipInputStream zi = new ZipInputStream(new BufferedInputStream( - new FileInputStream(zipFile), (int) zipFile.length())); + App.self.getContentResolver().openInputStream(zipUri))); ZipEntry ze; while ((ze = zi.getNextEntry()) != null) { if (ze.isDirectory()) diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/ProgInputStream.kt b/app/src/main/java/com/topjohnwu/magisk/utils/ProgInputStream.kt new file mode 100644 index 000000000..cc49f9cd8 --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/utils/ProgInputStream.kt @@ -0,0 +1,35 @@ +package com.topjohnwu.magisk.utils + +import com.topjohnwu.superuser.internal.UiThreadHandler +import java.io.FilterInputStream +import java.io.InputStream + +class ProgInputStream( + base: InputStream, + val progressEmitter: (Long) -> Unit = {} +) : FilterInputStream(base) { + + private var bytesRead = 0L + + override fun read(): Int { + val b = read() + if (b >= 0) { + bytesRead++ + UiThreadHandler.run { progressEmitter(bytesRead) } + } + return b + } + + override fun read(b: ByteArray): Int { + return read(b, 0, b.size) + } + + override fun read(b: ByteArray, off: Int, len: Int): Int { + val sz = super.read(b, off, len) + if (sz > 0) { + bytesRead += sz + UiThreadHandler.run { progressEmitter(bytesRead) } + } + return sz + } +} \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/XNetwork.kt b/app/src/main/java/com/topjohnwu/magisk/utils/XNetwork.kt deleted file mode 100644 index e86229166..000000000 --- a/app/src/main/java/com/topjohnwu/magisk/utils/XNetwork.kt +++ /dev/null @@ -1,80 +0,0 @@ -package com.topjohnwu.magisk.utils - -import android.content.Context -import com.topjohnwu.superuser.internal.UiThreadHandler -import okhttp3.ResponseBody -import java.io.File -import java.io.FilterInputStream -import java.io.InputStream -import java.io.OutputStream - -inline fun ResponseBody.writeToCachedFile( - context: Context, - fileName: String, - progress: (Long) -> Unit = {} -): File = byteStream().writeToCachedFile(context, fileName, progress) - -inline fun InputStream.writeToCachedFile( - context: Context, - fileName: String, - progress: (Long) -> Unit = {} -) = context.cachedFile(fileName).apply { - writeToFile(this, progress) -} - -inline fun InputStream.writeToFile(file: File, progress: (Long) -> Unit = {}) = file.apply { - writeTo(file.outputStream(), progress) -} - -inline fun InputStream.writeTo(output: OutputStream, progress: (Long) -> Unit = {}) { - withStreams(this, output) { inStream, outStream -> - inStream.copyToWithProgress(outStream, progress) - } -} - -fun ResponseBody.writeToString() = string() - -inline fun InputStream.copyToWithProgress( - out: OutputStream, - progressEmitter: (Long) -> Unit, - bufferSize: Int = DEFAULT_BUFFER_SIZE -): Long { - var bytesCopied: Long = 0 - val buffer = ByteArray(bufferSize) - var bytes = read(buffer) - while (bytes >= 0) { - out.write(buffer, 0, bytes) - bytesCopied += bytes - bytes = read(buffer) - progressEmitter(bytesCopied) - } - return bytesCopied -} - -class ProgInputStream(base: InputStream, - val progressEmitter: (Long) -> Unit = {}) : FilterInputStream(base) { - - private var bytesRead : Long = 0 - - override fun read(): Int { - val b = read() - if (b >= 0) { - bytesRead++ - UiThreadHandler.run { progressEmitter(bytesRead) } - } - return b - } - - override fun read(b: ByteArray): Int { - return read(b, 0, b.size) - } - - override fun read(b: ByteArray, off: Int, len: Int): Int { - val sz = super.read(b, off, len) - if (sz > 0) { - bytesRead += sz - UiThreadHandler.run { progressEmitter(bytesRead) } - } - return sz - } -} \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/view/dialogs/EnvFixDialog.kt b/app/src/main/java/com/topjohnwu/magisk/view/dialogs/EnvFixDialog.kt index 7ee303395..2f965e085 100644 --- a/app/src/main/java/com/topjohnwu/magisk/view/dialogs/EnvFixDialog.kt +++ b/app/src/main/java/com/topjohnwu/magisk/view/dialogs/EnvFixDialog.kt @@ -3,16 +3,19 @@ package com.topjohnwu.magisk.view.dialogs import android.app.Activity import android.app.ProgressDialog import android.widget.Toast +import androidx.core.net.toUri import com.topjohnwu.magisk.Info import com.topjohnwu.magisk.R import com.topjohnwu.magisk.tasks.MagiskInstaller import com.topjohnwu.magisk.utils.Utils +import com.topjohnwu.magisk.utils.cachedFile import com.topjohnwu.magisk.utils.reboot import com.topjohnwu.net.Networking import com.topjohnwu.superuser.Shell import com.topjohnwu.superuser.ShellUtils import com.topjohnwu.superuser.internal.UiThreadHandler import com.topjohnwu.superuser.io.SuFile +import java.io.File class EnvFixDialog(activity: Activity) : CustomAlertDialog(activity) { @@ -28,8 +31,10 @@ class EnvFixDialog(activity: Activity) : CustomAlertDialog(activity) { override fun operations(): Boolean { installDir = SuFile("/data/adb/magisk") Shell.su("rm -rf /data/adb/magisk/*").exec() - if (!ShellUtils.checkSum("MD5", zipFile, Info.remote.magisk.hash)) - Networking.get(Info.remote.magisk.link).execForFile(zipFile) + val zip : File = activity.cachedFile("magisk.zip") + if (!ShellUtils.checkSum("MD5", zip, Info.remote.magisk.hash)) + Networking.get(Info.remote.magisk.link).execForFile(zip) + zipUri = zip.toUri() return extractZip() && Shell.su("fix_env").exec().isSuccess }