From d904ec9c0a28d70fa2b93ac25437063518350996 Mon Sep 17 00:00:00 2001 From: SanmerDev Date: Thu, 23 May 2024 15:45:20 +0800 Subject: [PATCH] Tidy up `impl` --- .../compat/impl/APatchModuleManagerImpl.kt | 27 ++------ .../compat/impl/BaseModuleManagerImpl.kt | 67 +++++++++++++------ .../mrepo/compat/impl/FileManagerImpl.kt | 18 ++--- .../compat/impl/KernelSUModuleManagerImpl.kt | 27 ++------ .../compat/impl/MagiskModuleManagerImpl.kt | 27 ++------ .../dev/sanmer/mrepo/compat/impl/Platform.kt | 4 +- .../mrepo/compat/impl/ServiceManagerImpl.kt | 8 +-- 7 files changed, 72 insertions(+), 106 deletions(-) diff --git a/compat/src/main/kotlin/dev/sanmer/mrepo/compat/impl/APatchModuleManagerImpl.kt b/compat/src/main/kotlin/dev/sanmer/mrepo/compat/impl/APatchModuleManagerImpl.kt index bbaf35d8..6af5e7b8 100644 --- a/compat/src/main/kotlin/dev/sanmer/mrepo/compat/impl/APatchModuleManagerImpl.kt +++ b/compat/src/main/kotlin/dev/sanmer/mrepo/compat/impl/APatchModuleManagerImpl.kt @@ -1,6 +1,5 @@ package dev.sanmer.mrepo.compat.impl -import com.topjohnwu.superuser.CallbackList import com.topjohnwu.superuser.Shell import dev.sanmer.mrepo.compat.stub.IInstallCallback import dev.sanmer.mrepo.compat.stub.IModuleOpsCallback @@ -48,26 +47,10 @@ internal class APatchModuleManagerImpl( } override fun install(path: String, callback: IInstallCallback) { - val cmd = "apd module install '${path}'" - - val stdout = object : CallbackList() { - override fun onAddElement(msg: String?) { - msg?.let(callback::onStdout) - } - } - - val stderr = object : CallbackList() { - override fun onAddElement(msg: String?) { - msg?.let(callback::onStderr) - } - } - - val result = shell.newJob().add(cmd).to(stdout, stderr).exec() - if (result.isSuccess) { - val module = getModuleInfo(path) - callback.onSuccess(module) - } else { - callback.onFailure() - } + install( + cmd = "apd module install '${path}'", + path = path, + callback = callback + ) } } \ No newline at end of file diff --git a/compat/src/main/kotlin/dev/sanmer/mrepo/compat/impl/BaseModuleManagerImpl.kt b/compat/src/main/kotlin/dev/sanmer/mrepo/compat/impl/BaseModuleManagerImpl.kt index c2e9fb2d..46906b0e 100644 --- a/compat/src/main/kotlin/dev/sanmer/mrepo/compat/impl/BaseModuleManagerImpl.kt +++ b/compat/src/main/kotlin/dev/sanmer/mrepo/compat/impl/BaseModuleManagerImpl.kt @@ -1,9 +1,11 @@ package dev.sanmer.mrepo.compat.impl +import com.topjohnwu.superuser.CallbackList import com.topjohnwu.superuser.Shell import com.topjohnwu.superuser.ShellUtils import dev.sanmer.mrepo.compat.content.LocalModule import dev.sanmer.mrepo.compat.content.State +import dev.sanmer.mrepo.compat.stub.IInstallCallback import dev.sanmer.mrepo.compat.stub.IModuleManager import java.io.File import java.util.zip.ZipFile @@ -33,38 +35,37 @@ internal abstract class BaseModuleManagerImpl( return mVersionCode } - override fun getModules() = modulesDir.listFiles().orEmpty() - .mapNotNull { moduleDir -> - runCatching { - readProps(moduleDir) - ?.toModule( - state = readState(moduleDir), - lastUpdated = readLastUpdated(moduleDir) - ) - }.getOrNull() + override fun getModules() = modulesDir.listFiles() + .orEmpty() + .mapNotNull { dir -> + readProps(dir) + ?.toModule( + state = readState(dir), + lastUpdated = readLastUpdated(dir) + ) } - override fun getModuleById(id: String) = runCatching { - val moduleDir = modulesDir.resolve(id) - readProps(moduleDir) + override fun getModuleById(id: String): LocalModule? { + val dir = modulesDir.resolve(id) + + return readProps(dir) ?.toModule( - state = readState(moduleDir), - lastUpdated = readLastUpdated(moduleDir) + state = readState(dir), + lastUpdated = readLastUpdated(dir) ) - }.getOrNull() + } - override fun getModuleInfo(zipPath: String) = runCatching { + override fun getModuleInfo(zipPath: String): LocalModule? { val zipFile = ZipFile(zipPath) - val entry = zipFile.getEntry(PROP_FILE) ?: return@runCatching null + val entry = zipFile.getEntry(PROP_FILE) ?: return null - zipFile.getInputStream(entry).use { + return zipFile.getInputStream(entry).use { it.bufferedReader() .readText() .let(::readProps) - - }.toModule() - - }.getOrNull() + .toModule() + } + } private fun readProps(props: String) = props.lines() .associate { line -> @@ -128,6 +129,28 @@ internal abstract class BaseModuleManagerImpl( private fun String.exec() = ShellUtils.fastCmd(shell, this) + internal fun install(cmd: String, path: String, callback: IInstallCallback) { + val stdout = object : CallbackList() { + override fun onAddElement(msg: String?) { + msg?.let(callback::onStdout) + } + } + + val stderr = object : CallbackList() { + override fun onAddElement(msg: String?) { + msg?.let(callback::onStderr) + } + } + + val result = shell.newJob().add(cmd).to(stdout, stderr).exec() + if (result.isSuccess) { + val module = getModuleInfo(path) + callback.onSuccess(module) + } else { + callback.onFailure() + } + } + internal fun String.submit(cb: Shell.ResultCallback) = shell .newJob().add(this).to(ArrayList(), null) .submit(cb) diff --git a/compat/src/main/kotlin/dev/sanmer/mrepo/compat/impl/FileManagerImpl.kt b/compat/src/main/kotlin/dev/sanmer/mrepo/compat/impl/FileManagerImpl.kt index 36aa89bd..1d59c9ef 100644 --- a/compat/src/main/kotlin/dev/sanmer/mrepo/compat/impl/FileManagerImpl.kt +++ b/compat/src/main/kotlin/dev/sanmer/mrepo/compat/impl/FileManagerImpl.kt @@ -4,18 +4,12 @@ import dev.sanmer.mrepo.compat.stub.IFileManager import java.io.File internal class FileManagerImpl : IFileManager.Stub() { - override fun deleteOnExit(path: String): Boolean { - val file = File(path) - if (!file.exists()) return false - - if (file.isFile) { - return file.delete() - } - - if (file.isDirectory) { - return file.deleteRecursively() + override fun deleteOnExit(path: String) = with(File(path)) { + when { + !exists() -> false + isFile -> delete() + isDirectory -> deleteRecursively() + else -> false } - - return false } } \ No newline at end of file diff --git a/compat/src/main/kotlin/dev/sanmer/mrepo/compat/impl/KernelSUModuleManagerImpl.kt b/compat/src/main/kotlin/dev/sanmer/mrepo/compat/impl/KernelSUModuleManagerImpl.kt index 1a1a77b1..f817d1cf 100644 --- a/compat/src/main/kotlin/dev/sanmer/mrepo/compat/impl/KernelSUModuleManagerImpl.kt +++ b/compat/src/main/kotlin/dev/sanmer/mrepo/compat/impl/KernelSUModuleManagerImpl.kt @@ -1,6 +1,5 @@ package dev.sanmer.mrepo.compat.impl -import com.topjohnwu.superuser.CallbackList import com.topjohnwu.superuser.Shell import dev.sanmer.mrepo.compat.stub.IInstallCallback import dev.sanmer.mrepo.compat.stub.IModuleOpsCallback @@ -48,26 +47,10 @@ internal class KernelSUModuleManagerImpl( } override fun install(path: String, callback: IInstallCallback) { - val cmd = "ksud module install '${path}'" - - val stdout = object : CallbackList() { - override fun onAddElement(msg: String?) { - msg?.let(callback::onStdout) - } - } - - val stderr = object : CallbackList() { - override fun onAddElement(msg: String?) { - msg?.let(callback::onStderr) - } - } - - val result = shell.newJob().add(cmd).to(stdout, stderr).exec() - if (result.isSuccess) { - val module = getModuleInfo(path) - callback.onSuccess(module) - } else { - callback.onFailure() - } + install( + cmd = "ksud module install '${path}'", + path = path, + callback = callback + ) } } \ No newline at end of file diff --git a/compat/src/main/kotlin/dev/sanmer/mrepo/compat/impl/MagiskModuleManagerImpl.kt b/compat/src/main/kotlin/dev/sanmer/mrepo/compat/impl/MagiskModuleManagerImpl.kt index 70b6457e..f2f93ae1 100644 --- a/compat/src/main/kotlin/dev/sanmer/mrepo/compat/impl/MagiskModuleManagerImpl.kt +++ b/compat/src/main/kotlin/dev/sanmer/mrepo/compat/impl/MagiskModuleManagerImpl.kt @@ -1,6 +1,5 @@ package dev.sanmer.mrepo.compat.impl -import com.topjohnwu.superuser.CallbackList import com.topjohnwu.superuser.Shell import dev.sanmer.mrepo.compat.stub.IInstallCallback import dev.sanmer.mrepo.compat.stub.IModuleOpsCallback @@ -51,26 +50,10 @@ internal class MagiskModuleManagerImpl( } override fun install(path: String, callback: IInstallCallback) { - val cmd = "magisk --install-module '${path}'" - - val stdout = object : CallbackList() { - override fun onAddElement(msg: String?) { - msg?.let(callback::onStdout) - } - } - - val stderr = object : CallbackList() { - override fun onAddElement(msg: String?) { - msg?.let(callback::onStderr) - } - } - - val result = shell.newJob().add(cmd).to(stdout, stderr).exec() - if (result.isSuccess) { - val module = getModuleInfo(path) - callback.onSuccess(module) - } else { - callback.onFailure() - } + install( + cmd = "magisk --install-module '${path}'", + path = path, + callback = callback + ) } } \ No newline at end of file diff --git a/compat/src/main/kotlin/dev/sanmer/mrepo/compat/impl/Platform.kt b/compat/src/main/kotlin/dev/sanmer/mrepo/compat/impl/Platform.kt index 078e55eb..76324624 100644 --- a/compat/src/main/kotlin/dev/sanmer/mrepo/compat/impl/Platform.kt +++ b/compat/src/main/kotlin/dev/sanmer/mrepo/compat/impl/Platform.kt @@ -1,7 +1,7 @@ package dev.sanmer.mrepo.compat.impl enum class Platform { - MAGISK, - KERNELSU, + Magisk, + KernelSU, APatch } \ No newline at end of file diff --git a/compat/src/main/kotlin/dev/sanmer/mrepo/compat/impl/ServiceManagerImpl.kt b/compat/src/main/kotlin/dev/sanmer/mrepo/compat/impl/ServiceManagerImpl.kt index 05ead478..dbb7b09b 100644 --- a/compat/src/main/kotlin/dev/sanmer/mrepo/compat/impl/ServiceManagerImpl.kt +++ b/compat/src/main/kotlin/dev/sanmer/mrepo/compat/impl/ServiceManagerImpl.kt @@ -20,8 +20,8 @@ internal class ServiceManagerImpl : IServiceManager.Stub() { private val platform by lazy { when { - "which magisk".execResult() -> Platform.MAGISK - "which ksud".execResult() -> Platform.KERNELSU + "which magisk".execResult() -> Platform.Magisk + "which ksud".execResult() -> Platform.KernelSU "which apd".execResult() -> Platform.APatch else -> throw IllegalArgumentException("unsupported platform: $seLinuxContext") } @@ -29,8 +29,8 @@ internal class ServiceManagerImpl : IServiceManager.Stub() { private val moduleManager by lazy { when (platform) { - Platform.MAGISK -> MagiskModuleManagerImpl(main) - Platform.KERNELSU -> KernelSUModuleManagerImpl(main) + Platform.Magisk -> MagiskModuleManagerImpl(main) + Platform.KernelSU -> KernelSUModuleManagerImpl(main) Platform.APatch -> APatchModuleManagerImpl(main) } }