Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
Code cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
SanmerDev committed Aug 6, 2023
1 parent 3159b49 commit e6a41cd
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 60 deletions.
2 changes: 0 additions & 2 deletions app/src/main/aidl/com/sanmer/mrepo/provider/ISuProvider.aidl
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.sanmer.mrepo.provider;

interface ISuProvider {
int getPid();
String getContext();
int getEnforce();
IBinder getFileSystemService();
}
4 changes: 0 additions & 4 deletions app/src/main/kotlin/com/sanmer/mrepo/provider/SuProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@ import kotlinx.coroutines.flow.StateFlow
interface SuProvider {
val state: StateFlow<Event>

val pid: Int

val context: String

val enforce: Int

fun getFileSystemManager(): FileSystemManager

fun getModulesApi(): ModulesLocalApi
Expand Down
14 changes: 1 addition & 13 deletions app/src/main/kotlin/com/sanmer/mrepo/provider/SuProviderImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.IBinder
import android.os.Process
import com.sanmer.mrepo.BuildConfig
import com.sanmer.mrepo.api.ApiInitializerListener
import com.sanmer.mrepo.api.local.KernelSuModulesApi
Expand Down Expand Up @@ -109,12 +108,11 @@ class SuProviderImpl @Inject constructor(

private class SuService : RootService() {
override fun onBind(intent: Intent): IBinder = object : ISuProvider.Stub() {
override fun getPid(): Int = Process.myPid()
override fun getContext(): String = getContextImpl()
override fun getEnforce(): Int = getEnforceImpl()
override fun getFileSystemService(): IBinder = FileSystemManager.getService()
}

@Suppress("SameParameterValue")
private inline fun <T> safe(default: T, block: () -> T): T {
return try {
block()
Expand All @@ -129,19 +127,9 @@ class SuProviderImpl @Inject constructor(
.readText()
.replace("[^a-z0-9:_,]".toRegex(), "")
}

private fun getEnforceImpl() = safe(1) {
"/sys/fs/selinux/enforce".toFile()
.readText()
.replace("[^0-9]".toRegex(), "")
.toInt()
}

}

override val pid: Int get() = mProvider.pid
override val context: String get() = mProvider.context
override val enforce: Int get() = mProvider.enforce

override fun getFileSystemManager(): FileSystemManager =
FileSystemManager.getRemote(mProvider.fileSystemService)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ class SuRepository @Inject constructor(
private val api: ModulesLocalApi get() = suProvider.getModulesApi()

val state: StateFlow<Event> get() = suProvider.state
val pid get() = suProvider.pid
val context get() = suProvider.context
val enforce get() = suProvider.enforce
val fs: FileSystemManager get() = suProvider.getFileSystemManager()

val version get() = api.version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import com.sanmer.mrepo.ui.animate.slideInBottomToTop
import com.sanmer.mrepo.ui.animate.slideOutTopToBottom
import com.sanmer.mrepo.ui.component.NavigateUpTopBar
import com.sanmer.mrepo.ui.utils.isScrollingUp
import com.sanmer.mrepo.utils.SvcPower
import com.sanmer.mrepo.utils.ModuleUtils
import com.sanmer.mrepo.viewmodel.InstallViewModel

@Composable
Expand Down Expand Up @@ -158,7 +158,7 @@ private fun TopBar(

@Composable
private fun FloatingButton() = ExtendedFloatingActionButton(
onClick = { SvcPower.reboot() },
onClick = { ModuleUtils.reboot() },
text = { Text(text = stringResource(id = R.string.install_reboot)) },
icon = {
Icon(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import androidx.compose.ui.unit.dp
import com.sanmer.mrepo.R
import com.sanmer.mrepo.app.utils.OsUtils
import com.sanmer.mrepo.ui.component.DropdownMenu
import com.sanmer.mrepo.utils.SvcPower
import com.sanmer.mrepo.utils.ModuleUtils

private enum class Menu(
@StringRes val label: Int,
Expand Down Expand Up @@ -89,7 +89,7 @@ private fun MenuItem(
) = DropdownMenuItem(
text = { Text(text = stringResource(id = value.label)) },
onClick = {
SvcPower.reboot(value.reason)
ModuleUtils.reboot(value.reason)
onClose()
}
)
8 changes: 8 additions & 0 deletions app/src/main/kotlin/com/sanmer/mrepo/utils/ModuleUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,12 @@ object ModuleUtils {
}
}
}

fun reboot(reason: String = "") {
if (reason == "recovery") {
// KEYCODE_POWER = 26, hide incorrect "Factory data reset" message
Shell.cmd("/system/bin/input keyevent 26").submit()
}
Shell.cmd("/system/bin/svc power reboot $reason || /system/bin/reboot $reason").submit()
}
}
13 changes: 0 additions & 13 deletions app/src/main/kotlin/com/sanmer/mrepo/utils/SvcPower.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,26 +1,5 @@
package com.sanmer.mrepo.utils.extensions

import androidx.compose.runtime.snapshots.SnapshotStateList

inline fun <reified T> MutableList<T>.update(value: T) {
val index = indexOfFirst { it == value }
if (index == -1) {
add(value)
} else {
set(index, value)
}
}

inline fun <reified T> SnapshotStateList<T>.update(value: T) {
val index = indexOfFirst { it == value }
if (index == -1) {
add(value)
} else {
removeAt(index)
add(index, value)
}
}

inline fun <reified T> List<List<T>>.merge(): List<T> {
val values = mutableListOf<T>()
forEach { values.addAll(it) }
Expand Down

0 comments on commit e6a41cd

Please sign in to comment.