Clean ups

This commit is contained in:
topjohnwu 2019-07-20 22:37:34 -07:00
parent 8ca188f4d4
commit 6fb032b3c2
8 changed files with 66 additions and 123 deletions

View File

@ -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

View File

@ -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;

View File

@ -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",

View File

@ -9,7 +9,7 @@ sealed class Patching(
private val console: MutableList<String>,
logs: MutableList<String>,
private val resultListener: FlashResultListener
) : MagiskInstaller(console, logs) {
) : MagiskInstaller(file, console, logs) {
override fun onResult(success: Boolean) {
if (success) {

View File

@ -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<String> console;
private final List<String> logs;
@ -56,9 +55,10 @@ public abstract class MagiskInstaller {
logs = NOPList.getInstance();
}
public MagiskInstaller(List<String> out, List<String> err) {
public MagiskInstaller(Uri zip, List<String> out, List<String> 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())

View File

@ -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
}
}

View File

@ -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
}
}

View File

@ -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
}