You've already forked Magisk
mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-09-06 06:36:58 +00:00
Added (ported back) features from initial design [retrofit,moshi,kotpref]
Marked most of the old classes using Networking as deprecated to clearly visualise their future removal
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
package com.topjohnwu.magisk.model.entity
|
||||
|
||||
import com.squareup.moshi.Json
|
||||
import com.topjohnwu.magisk.utils.timeFormatStandard
|
||||
import com.topjohnwu.magisk.utils.toTime
|
||||
|
||||
data class GithubRepo(
|
||||
@Json(name = "name") val name: String,
|
||||
@Json(name = "updated_at") val updatedAt: String
|
||||
) {
|
||||
val updatedAtMillis by lazy { updatedAt.toTime(timeFormatStandard) }
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.topjohnwu.magisk.model.entity
|
||||
|
||||
import com.squareup.moshi.Json
|
||||
import com.squareup.moshi.JsonClass
|
||||
|
||||
data class MagiskConfig(
|
||||
val app: MagiskApp,
|
||||
val uninstaller: MagiskLink,
|
||||
val magisk: MagiskFlashable
|
||||
)
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
data class MagiskApp(
|
||||
val version: String,
|
||||
val versionCode: String,
|
||||
val link: String,
|
||||
val note: String
|
||||
)
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
data class MagiskLink(
|
||||
val link: String
|
||||
)
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
data class MagiskFlashable(
|
||||
val version: String,
|
||||
val versionCode: String,
|
||||
val link: String,
|
||||
val note: String,
|
||||
@Json(name = "md5") val hash: String
|
||||
)
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.topjohnwu.magisk.model.entity
|
||||
|
||||
import java.util.*
|
||||
|
||||
data class MagiskLog(
|
||||
val fromUid: Int,
|
||||
val toUid: Int,
|
||||
val fromPid: Int,
|
||||
val packageName: String,
|
||||
val appName: String,
|
||||
val command: String,
|
||||
val action: Boolean,
|
||||
val date: Date
|
||||
)
|
||||
|
||||
data class WrappedMagiskLog(
|
||||
val time: Long,
|
||||
val items: List<MagiskLog>
|
||||
)
|
||||
|
||||
fun Map<String, String>.toLog(): MagiskLog {
|
||||
return MagiskLog(
|
||||
fromUid = get("from_uid")?.toIntOrNull() ?: -1,
|
||||
toUid = get("to_uid")?.toIntOrNull() ?: -1,
|
||||
fromPid = get("from_pid")?.toIntOrNull() ?: -1,
|
||||
packageName = get("package_name").orEmpty(),
|
||||
appName = get("app_name").orEmpty(),
|
||||
command = get("command").orEmpty(),
|
||||
action = get("action")?.toIntOrNull() != 0,
|
||||
date = get("time")?.toLongOrNull()?.toDate() ?: Date()
|
||||
)
|
||||
}
|
||||
|
||||
fun Long.toDate() = Date(this)
|
||||
|
||||
|
||||
fun MagiskLog.toMap() = mapOf(
|
||||
"from_uid" to fromUid,
|
||||
"to_uid" to toUid,
|
||||
"from_pid" to fromPid,
|
||||
"package_name" to packageName,
|
||||
"app_name" to appName,
|
||||
"command" to command,
|
||||
"action" to action,
|
||||
"time" to date
|
||||
).mapValues { it.toString() }
|
||||
@@ -0,0 +1,81 @@
|
||||
package com.topjohnwu.magisk.model.entity
|
||||
|
||||
import android.os.Parcelable
|
||||
import androidx.annotation.AnyThread
|
||||
import androidx.annotation.NonNull
|
||||
import androidx.annotation.WorkerThread
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
import com.topjohnwu.magisk.Constants
|
||||
import com.topjohnwu.magisk.data.database.base.su
|
||||
import io.reactivex.Single
|
||||
import kotlinx.android.parcel.Parcelize
|
||||
import okhttp3.ResponseBody
|
||||
import java.io.File
|
||||
|
||||
interface MagiskModule : Parcelable {
|
||||
val id: String
|
||||
val name: String
|
||||
val author: String
|
||||
val version: String
|
||||
val versionCode: String
|
||||
}
|
||||
|
||||
@Entity(tableName = "repos")
|
||||
@Parcelize
|
||||
data class Repository(
|
||||
@PrimaryKey @NonNull
|
||||
override val id: String,
|
||||
override val name: String,
|
||||
override val author: String,
|
||||
override val version: String,
|
||||
override val versionCode: String,
|
||||
val lastUpdate: Long
|
||||
) : MagiskModule
|
||||
|
||||
@Parcelize
|
||||
data class Module(
|
||||
override val id: String,
|
||||
override val name: String,
|
||||
override val author: String,
|
||||
override val version: String,
|
||||
override val versionCode: String,
|
||||
val path: String
|
||||
) : MagiskModule
|
||||
|
||||
@AnyThread
|
||||
fun File.toModule(): Single<Module> {
|
||||
val path = "${Constants.MAGISK_PATH}/$name"
|
||||
return "dos2unix < $path/module.prop".su()
|
||||
.map { it.first().toModule(path) }
|
||||
}
|
||||
|
||||
fun Map<String, String>.toModule(path: String): Module {
|
||||
return Module(
|
||||
id = get("id").orEmpty(),
|
||||
name = get("name").orEmpty(),
|
||||
author = get("author").orEmpty(),
|
||||
version = get("version").orEmpty(),
|
||||
versionCode = get("versionCode").orEmpty(),
|
||||
path = path
|
||||
)
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
fun ResponseBody.toRepository(lastUpdate: Long) = string()
|
||||
.split(Regex("\\n"))
|
||||
.map { it.split("=", limit = 2) }
|
||||
.filter { it.size == 2 }
|
||||
.map { Pair(it[0], it[1]) }
|
||||
.toMap()
|
||||
.toRepository(lastUpdate)
|
||||
|
||||
@AnyThread
|
||||
fun Map<String, String>.toRepository(lastUpdate: Long) = Repository(
|
||||
id = get("id").orEmpty(),
|
||||
name = get("name").orEmpty(),
|
||||
author = get("author").orEmpty(),
|
||||
version = get("version").orEmpty(),
|
||||
versionCode = get("versionCode").orEmpty(),
|
||||
lastUpdate = lastUpdate
|
||||
)
|
||||
@@ -0,0 +1,74 @@
|
||||
package com.topjohnwu.magisk.model.entity
|
||||
|
||||
import android.content.pm.ApplicationInfo
|
||||
import android.content.pm.PackageManager
|
||||
|
||||
|
||||
data class MagiskPolicy(
|
||||
val uid: Int,
|
||||
val packageName: String,
|
||||
val appName: String,
|
||||
val policy: Int,
|
||||
val until: Long,
|
||||
val logging: Boolean,
|
||||
val notification: Boolean,
|
||||
val applicationInfo: ApplicationInfo
|
||||
)
|
||||
|
||||
/*@Throws(PackageManager.NameNotFoundException::class)
|
||||
fun ContentValues.toPolicy(pm: PackageManager): MagiskPolicy {
|
||||
val uid = getAsInteger("uid")
|
||||
val packageName = getAsString("package_name")
|
||||
val info = pm.getApplicationInfo(packageName, 0)
|
||||
if (info.uid != uid)
|
||||
throw PackageManager.NameNotFoundException()
|
||||
return MagiskPolicy(
|
||||
uid = uid,
|
||||
packageName = packageName,
|
||||
policy = getAsInteger("policy"),
|
||||
until = getAsInteger("until").toLong(),
|
||||
logging = getAsInteger("logging") != 0,
|
||||
notification = getAsInteger("notification") != 0,
|
||||
applicationInfo = info,
|
||||
appName = info.loadLabel(pm).toString()
|
||||
)
|
||||
}
|
||||
fun MagiskPolicy.toContentValues() = ContentValues().apply {
|
||||
put("uid", uid)
|
||||
put("uid", uid)
|
||||
put("package_name", packageName)
|
||||
put("policy", policy)
|
||||
put("until", until)
|
||||
put("logging", if (logging) 1 else 0)
|
||||
put("notification", if (notification) 1 else 0)
|
||||
}*/
|
||||
|
||||
fun MagiskPolicy.toMap() = mapOf(
|
||||
"uid" to uid,
|
||||
"package_name" to packageName,
|
||||
"policy" to policy,
|
||||
"until" to until,
|
||||
"logging" to if (logging) 1 else 0,
|
||||
"notification" to if (notification) 1 else 0
|
||||
).mapValues { it.toString() }
|
||||
|
||||
@Throws(PackageManager.NameNotFoundException::class)
|
||||
fun Map<String, String>.toPolicy(pm: PackageManager): MagiskPolicy {
|
||||
val uid = get("uid")?.toIntOrNull() ?: -1
|
||||
val packageName = get("package_name").orEmpty()
|
||||
val info = pm.getApplicationInfo(packageName, 0)
|
||||
|
||||
if (info.uid != uid)
|
||||
throw PackageManager.NameNotFoundException()
|
||||
|
||||
return MagiskPolicy(
|
||||
uid = uid,
|
||||
packageName = packageName,
|
||||
policy = get("policy")?.toIntOrNull() ?: -1,
|
||||
until = get("until")?.toLongOrNull() ?: -1L,
|
||||
logging = get("logging")?.toIntOrNull() != 0,
|
||||
notification = get("notification")?.toIntOrNull() != 0,
|
||||
applicationInfo = info,
|
||||
appName = info.loadLabel(pm).toString()
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
package com.topjohnwu.magisk.model.entity
|
||||
|
||||
data class Version(val version: String, val versionCode: Int)
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.topjohnwu.magisk.model.zip
|
||||
|
||||
import com.topjohnwu.magisk.utils.forEach
|
||||
import com.topjohnwu.magisk.utils.withStreams
|
||||
import com.topjohnwu.superuser.io.SuFile
|
||||
import java.io.File
|
||||
import java.util.zip.ZipInputStream
|
||||
|
||||
|
||||
class Zip private constructor(private val values: Builder) {
|
||||
|
||||
companion object {
|
||||
operator fun invoke(builder: Builder.() -> Unit): Zip {
|
||||
return Zip(Builder().apply(builder))
|
||||
}
|
||||
}
|
||||
|
||||
class Builder {
|
||||
lateinit var zip: File
|
||||
lateinit var destination: File
|
||||
var excludeDirs = true
|
||||
}
|
||||
|
||||
data class Path(val path: String, val pullFromDir: Boolean = true)
|
||||
|
||||
fun unzip(vararg paths: Pair<String, Boolean>) =
|
||||
unzip(*paths.map { Path(it.first, it.second) }.toTypedArray())
|
||||
|
||||
@Suppress("RedundantLambdaArrow")
|
||||
fun unzip(vararg paths: Path) {
|
||||
ensureRequiredParams()
|
||||
|
||||
values.zip.zipStream().use {
|
||||
it.forEach { e ->
|
||||
val currentPath = paths.firstOrNull { e.name.startsWith(it.path) }
|
||||
val isDirectory = values.excludeDirs && e.isDirectory
|
||||
if (currentPath == null || isDirectory) {
|
||||
// Ignore directories, only create files
|
||||
return@forEach
|
||||
}
|
||||
|
||||
val name = if (currentPath.pullFromDir) {
|
||||
e.name.substring(e.name.lastIndexOf('/') + 1)
|
||||
} else {
|
||||
e.name
|
||||
}
|
||||
|
||||
val out = File(values.destination, name)
|
||||
.ensureExists()
|
||||
.outputStream()
|
||||
//.suOutputStream()
|
||||
|
||||
withStreams(it, out) { reader, writer ->
|
||||
reader.copyTo(writer)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun ensureRequiredParams() {
|
||||
if (!values.zip.exists()) {
|
||||
throw RuntimeException("Zip file does not exist")
|
||||
}
|
||||
}
|
||||
|
||||
private fun File.ensureExists() =
|
||||
if ((!parentFile.exists() && !parentFile.mkdirs()) || parentFile is SuFile) {
|
||||
SuFile(parentFile, name).apply { parentFile.mkdirs() }
|
||||
} else {
|
||||
this
|
||||
}
|
||||
|
||||
private fun File.zipStream() = ZipInputStream(inputStream())
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user