Skip to content

Commit

Permalink
Clean ups
Browse files Browse the repository at this point in the history
  • Loading branch information
topjohnwu committed Jul 21, 2019
1 parent 8ca188f commit 6fb032b
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 123 deletions.
30 changes: 18 additions & 12 deletions app/src/main/java/com/topjohnwu/magisk/Config.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/topjohnwu/magisk/Info.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
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;
Expand All @@ -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;
Expand All @@ -56,9 +55,10 @@ protected 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();
Expand Down Expand Up @@ -105,7 +105,7 @@ protected boolean extractZip() {

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())
Expand Down
35 changes: 35 additions & 0 deletions app/src/main/java/com/topjohnwu/magisk/utils/ProgInputStream.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
80 changes: 0 additions & 80 deletions app/src/main/java/com/topjohnwu/magisk/utils/XNetwork.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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) {

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

Expand Down

0 comments on commit 6fb032b

Please sign in to comment.