Skip to content

Commit

Permalink
improve module handling
Browse files Browse the repository at this point in the history
  • Loading branch information
DerGoogler committed Dec 23, 2024
1 parent 3fd4012 commit 40361eb
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package dev.dergoogler.mmrl.compat.content;

parcelable ModuleCompatibility;
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dev.dergoogler.mmrl.compat.stub;

import dev.dergoogler.mmrl.compat.content.LocalModule;
import dev.dergoogler.mmrl.compat.content.ModuleInfo;
import dev.dergoogler.mmrl.compat.content.ModuleCompatibility;
import dev.dergoogler.mmrl.compat.content.BulkModule;
import dev.dergoogler.mmrl.compat.stub.IShellCallback;
import dev.dergoogler.mmrl.compat.stub.IModuleOpsCallback;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import com.dergoogler.mmrl.ui.component.scrollbar.VerticalFastScrollbar
import com.dergoogler.mmrl.ui.providable.LocalUserPreferences
import com.dergoogler.mmrl.viewmodel.ModulesViewModel
import dev.dergoogler.mmrl.compat.activity.MMRLComponentActivity
import dev.dergoogler.mmrl.compat.content.ModuleCompatibility
import dev.dergoogler.mmrl.compat.ext.takeTrue

@Composable
Expand All @@ -49,6 +50,7 @@ fun ModulesList(
getVersionItem: @Composable (LocalModule) -> VersionItem?,
getProgress: @Composable (VersionItem?) -> Float,
onDownload: (LocalModule, VersionItem, Boolean) -> Unit,
moduleCompatibility: ModuleCompatibility,
) = Box(
modifier = Modifier.fillMaxSize()
) {
Expand All @@ -69,7 +71,8 @@ fun ModulesList(
getModuleOps = getModuleOps,
getVersionItem = getVersionItem,
getProgress = getProgress,
onDownload = onDownload
onDownload = onDownload,
moduleCompatibility = moduleCompatibility
)
}
}
Expand All @@ -89,6 +92,7 @@ fun ModuleItem(
getVersionItem: @Composable (LocalModule) -> VersionItem?,
getProgress: @Composable (VersionItem?) -> Float,
onDownload: (LocalModule, VersionItem, Boolean) -> Unit,
moduleCompatibility: ModuleCompatibility,
) {
val userPreferences = LocalUserPreferences.current

Expand Down Expand Up @@ -170,7 +174,7 @@ fun ModuleItem(

RemoveOrRestore(
module = module,
enabled = isProviderAlive && (!userPreferences.useShellForModuleStateChange || module.state != State.REMOVE),
enabled = isProviderAlive && (!(moduleCompatibility.canRestoreModules && userPreferences.useShellForModuleStateChange) || module.state != State.REMOVE),
onClick = ops.change
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ fun ModulesScreen(
getModuleOps = viewModel::createModuleOps,
getVersionItem = { viewModel.getVersionItem(it) },
getProgress = { viewModel.getProgress(it) },
onDownload = download
onDownload = download,
moduleCompatibility = viewModel.moduleCompatibility
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fun ModulesScreen(
icon = R.drawable.stars_outlined,
title = stringResource(id = R.string.settings_shell_module_state_change),
desc = stringResource(id = R.string.settings_shell_module_state_change_desc),
checked = userPreferences.useShellForModuleStateChange || viewModel.platform.isMagisk,
checked = userPreferences.useShellForModuleStateChange && viewModel.platform.isNotMagisk,
onChange = viewModel::setUseShellForModuleStateChange,
labels = listOf { KernelSuLabel(); APatchLabel() }
)
Expand All @@ -69,7 +69,7 @@ fun ModulesScreen(
icon = R.drawable.device_mobile_code,
title = stringResource(id = R.string.settings_shell_module_action),
desc = stringResource(id = R.string.settings_shell_module_action_desc),
checked = userPreferences.useShellForModuleAction || viewModel.platform.isMagisk,
checked = userPreferences.useShellForModuleAction,
onChange = viewModel::setUseShellForModuleAction,
labels = listOf { KernelSuLabel(); APatchLabel() }
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import com.dergoogler.mmrl.repository.UserPreferencesRepository
import com.dergoogler.mmrl.service.DownloadService
import com.dergoogler.mmrl.utils.Utils
import dagger.hilt.android.lifecycle.HiltViewModel
import dev.dergoogler.mmrl.compat.content.ModuleCompatibility
import dev.dergoogler.mmrl.compat.stub.IModuleOpsCallback
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
Expand All @@ -48,6 +49,18 @@ class ModulesViewModel @Inject constructor(
val isProviderAlive get() = Compat.isAlive
val platform get() = Compat.platform

val moduleCompatibility
get() = Compat.get(
ModuleCompatibility(
hasMagicMount = false,
canRestoreModules = false
)
) {
with(moduleManager) {
moduleCompatibility
}
}

private val modulesMenu
get() = userPreferencesRepository.data
.map { it.modulesMenu }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ class SettingsViewModel @Inject constructor(
with(moduleManager) { "$version (${versionCode})" }
}

val managerName
get() = Compat.get("") {
with(moduleManager) { managerName }
}

init {
Timber.d("SettingsViewModel init")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dev.dergoogler.mmrl.compat.content

import android.os.Parcelable
import kotlinx.parcelize.Parcelize

@Parcelize
data class ModuleCompatibility(
val hasMagicMount: Boolean,
val canRestoreModules: Boolean,
) : Parcelable {
val canNotRestoreModules get() = !canRestoreModules
val hasNotMagicMount get() = !hasMagicMount

companion object
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dev.dergoogler.mmrl.compat.impl

import com.topjohnwu.superuser.Shell
import dev.dergoogler.mmrl.compat.content.BulkModule
import dev.dergoogler.mmrl.compat.content.ModuleCompatibility
import dev.dergoogler.mmrl.compat.stub.IModuleOpsCallback
import dev.dergoogler.mmrl.compat.stub.IShellCallback

Expand All @@ -16,10 +17,12 @@ internal class APatchModuleManagerImpl(
return "APatch"
}

override fun hasMagicMount(): Boolean =
fileManager.exists("/data/adb/.bind_mount_enable") && (versionCode >= 11011 && !fileManager.exists(
override fun getModuleCompatibility() = ModuleCompatibility(
hasMagicMount = fileManager.exists("/data/adb/.bind_mount_enable") && (versionCode >= 11011 && !fileManager.exists(
"/data/adb/.overlay_enable"
))
)),
canRestoreModules = false
)

override fun enable(id: String, useShell: Boolean, callback: IModuleOpsCallback) {
val dir = modulesDir.resolve(id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.topjohnwu.superuser.ShellUtils
import dev.dergoogler.mmrl.compat.content.BulkModule
import dev.dergoogler.mmrl.compat.content.LocalModule
import dev.dergoogler.mmrl.compat.content.LocalModuleRunners
import dev.dergoogler.mmrl.compat.content.ModuleCompatibility
import dev.dergoogler.mmrl.compat.content.ModuleInfo
import dev.dergoogler.mmrl.compat.content.State
import dev.dergoogler.mmrl.compat.stub.IModuleManager
Expand Down Expand Up @@ -44,7 +45,10 @@ internal abstract class BaseModuleManagerImpl(
return mVersionCode
}

override fun hasMagicMount(): Boolean = false
override fun getModuleCompatibility() = ModuleCompatibility(
hasMagicMount = false,
canRestoreModules = true
)

override fun getSeLinuxContext(): String = seLinuxContext

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dev.dergoogler.mmrl.compat.impl

import com.topjohnwu.superuser.Shell
import dev.dergoogler.mmrl.compat.content.BulkModule
import dev.dergoogler.mmrl.compat.content.ModuleCompatibility
import dev.dergoogler.mmrl.compat.stub.IModuleOpsCallback
import dev.dergoogler.mmrl.compat.stub.IShellCallback

Expand All @@ -15,7 +16,10 @@ internal open class KernelSUModuleManagerImpl(
return "KernelSU"
}

override fun hasMagicMount(): Boolean = false
override fun getModuleCompatibility() = ModuleCompatibility(
hasMagicMount = false,
canRestoreModules = false
)

override fun enable(id: String, useShell: Boolean, callback: IModuleOpsCallback) {
val dir = modulesDir.resolve(id)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,41 @@
package dev.dergoogler.mmrl.compat.impl

import com.topjohnwu.superuser.Shell
import dev.dergoogler.mmrl.compat.content.ModuleCompatibility
import dev.dergoogler.mmrl.compat.stub.IModuleOpsCallback

internal class KsuNextModuleManagerImpl(
shell: Shell,
seLinuxContext: String,
) : KernelSUModuleManagerImpl(
shell, seLinuxContext
) {
override fun hasMagicMount(): Boolean = true
override fun getModuleCompatibility() = ModuleCompatibility(
hasMagicMount = true,
canRestoreModules = true
)

override fun enable(id: String, useShell: Boolean, callback: IModuleOpsCallback) {
val dir = modulesDir.resolve(id)
if (!dir.exists()) callback.onFailure(id, null)

if (useShell) {
"ksud module restore $id && ksud module enable $id".submit {
if (it.isSuccess) {
callback.onSuccess(id)
} else {
callback.onFailure(id, it.out.joinToString())
}
}
} else {
runCatching {
dir.resolve("remove").apply { if (exists()) delete() }
dir.resolve("disable").apply { if (exists()) delete() }
}.onSuccess {
callback.onSuccess(id)
}.onFailure {
callback.onFailure(id, it.message)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dev.dergoogler.mmrl.compat.impl

import com.topjohnwu.superuser.Shell
import dev.dergoogler.mmrl.compat.content.BulkModule
import dev.dergoogler.mmrl.compat.content.ModuleCompatibility
import dev.dergoogler.mmrl.compat.stub.IModuleOpsCallback
import dev.dergoogler.mmrl.compat.stub.IShellCallback

Expand All @@ -15,7 +16,10 @@ internal class MagiskModuleManagerImpl(
return "Magisk"
}

override fun hasMagicMount(): Boolean = true
override fun getModuleCompatibility() = ModuleCompatibility(
hasMagicMount = true,
canRestoreModules = true
)

override fun enable(id: String, useShell: Boolean, callback: IModuleOpsCallback) {
val dir = modulesDir.resolve(id)
Expand Down

0 comments on commit 40361eb

Please sign in to comment.