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

Commit

Permalink
Add ServiceManagerCompat (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
SanmerDev authored May 22, 2024
1 parent 0fa0608 commit 89e1e06
Show file tree
Hide file tree
Showing 21 changed files with 285 additions and 374 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package com.sanmer.mrepo.compat
package com.sanmer.mrepo

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import com.sanmer.mrepo.datastore.WorkingMode
import dev.sanmer.mrepo.compat.ShizukuProvider
import dev.sanmer.mrepo.compat.SuProvider
import dev.sanmer.mrepo.compat.ServiceManagerCompat
import dev.sanmer.mrepo.compat.stub.IFileManager
import dev.sanmer.mrepo.compat.stub.IModuleManager
import dev.sanmer.mrepo.compat.stub.IServiceManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.withContext
import timber.log.Timber

object ProviderCompat {
object Compat {
private var mServiceOrNull: IServiceManager? = null
private val mService get() = checkNotNull(mServiceOrNull) {
"IServiceManager haven't been received"
Expand All @@ -37,15 +34,12 @@ object ProviderCompat {
return alive
}

suspend fun init(mode: WorkingMode) = withContext(Dispatchers.Main) {
if (isAlive) {
return@withContext true
}

try {
suspend fun init(mode: WorkingMode) = when {
isAlive -> true
else -> try {
mServiceOrNull = when (mode) {
WorkingMode.MODE_SHIZUKU -> ShizukuProvider.launch()
WorkingMode.MODE_ROOT -> SuProvider.launch()
WorkingMode.MODE_SHIZUKU -> ServiceManagerCompat.fromShizuku()
WorkingMode.MODE_ROOT -> ServiceManagerCompat.fromLibSu()
else -> null
}

Expand All @@ -57,7 +51,7 @@ object ProviderCompat {
}
}

fun <T> get(fallback: T, block: ProviderCompat.() -> T): T {
fun <T> get(fallback: T, block: Compat.() -> T): T {
return when {
isAlive -> block(this)
else -> fallback
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.sanmer.mrepo.repository

import com.sanmer.mrepo.compat.ProviderCompat
import com.sanmer.mrepo.Compat
import com.sanmer.mrepo.database.entity.Repo
import com.sanmer.mrepo.network.runRequest
import com.sanmer.mrepo.stub.IRepoManager
Expand All @@ -15,15 +15,15 @@ class ModulesRepository @Inject constructor(
private val localRepository: LocalRepository,
) {
suspend fun getLocalAll() = withContext(Dispatchers.IO) {
with(ProviderCompat.moduleManager.modules) {
with(Compat.moduleManager.modules) {
localRepository.deleteLocalAll()
localRepository.insertLocal(this)
localRepository.clearUpdatableTag(map { it.id })
}
}

suspend fun getLocal(id: String) = withContext(Dispatchers.IO) {
val module = ProviderCompat.moduleManager.getModuleById(id)
val module = Compat.moduleManager.getModuleById(id)
localRepository.insertLocal(module)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.lifecycleScope
import com.sanmer.mrepo.compat.ProviderCompat
import com.sanmer.mrepo.Compat
import com.sanmer.mrepo.repository.UserPreferencesRepository
import com.sanmer.mrepo.ui.providable.LocalUserPreferences
import com.sanmer.mrepo.ui.theme.AppTheme
Expand Down Expand Up @@ -49,11 +49,11 @@ class InstallActivity : ComponentActivity() {
}

LaunchedEffect(userPreferences) {
ProviderCompat.init(preferences.workingMode)
Compat.init(preferences.workingMode)
}

LaunchedEffect(ProviderCompat.isAlive) {
if (ProviderCompat.isAlive) {
LaunchedEffect(Compat.isAlive) {
if (Compat.isAlive) {
initModule(intent)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import androidx.compose.runtime.setValue
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.lifecycleScope
import com.sanmer.mrepo.Compat
import com.sanmer.mrepo.app.Const
import com.sanmer.mrepo.compat.ProviderCompat
import com.sanmer.mrepo.database.entity.Repo.Companion.toRepo
import com.sanmer.mrepo.datastore.WorkingMode
import com.sanmer.mrepo.network.NetworkUtils
Expand Down Expand Up @@ -60,7 +60,7 @@ class MainActivity : ComponentActivity() {
localRepository.insertRepo(Const.DEMO_REPO_URL.toRepo())
}

ProviderCompat.init(preferences.workingMode)
Compat.init(preferences.workingMode)
NetworkUtils.setEnableDoh(preferences.useDoh)
setInstallActivityEnabled(preferences.isRoot)
}
Expand Down
26 changes: 15 additions & 11 deletions app/src/main/kotlin/com/sanmer/mrepo/viewmodel/InstallViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.sanmer.mrepo.Compat
import com.sanmer.mrepo.app.Event
import com.sanmer.mrepo.compat.MediaStoreCompat.copyToDir
import com.sanmer.mrepo.compat.MediaStoreCompat.getPathForUri
import com.sanmer.mrepo.compat.ProviderCompat
import com.sanmer.mrepo.repository.ModulesRepository
import com.sanmer.mrepo.model.local.LocalModule
import com.sanmer.mrepo.repository.LocalRepository
import com.sanmer.mrepo.repository.UserPreferencesRepository
import com.sanmer.mrepo.utils.extensions.tmpDir
import dagger.hilt.android.lifecycle.HiltViewModel
import dev.sanmer.mrepo.compat.content.State
import dev.sanmer.mrepo.compat.stub.IInstallCallback
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.first
Expand All @@ -28,7 +30,7 @@ import javax.inject.Inject

@HiltViewModel
class InstallViewModel @Inject constructor(
private val modulesRepository: ModulesRepository,
private val localRepository: LocalRepository,
private val userPreferencesRepository: UserPreferencesRepository,
) : ViewModel() {
val logs = mutableListOf<String>()
Expand Down Expand Up @@ -57,7 +59,7 @@ class InstallViewModel @Inject constructor(
val path = context.getPathForUri(uri)
Timber.d("path = $path")

ProviderCompat.moduleManager
Compat.moduleManager
.getModuleInfo(path)?.let {
Timber.d("module = $it")
install(path)
Expand All @@ -74,7 +76,7 @@ class InstallViewModel @Inject constructor(
}
}

ProviderCompat.moduleManager
Compat.moduleManager
.getModuleInfo(tmpFile.path)?.let {
Timber.d("module = $it")
install(tmpFile.path)
Expand All @@ -101,9 +103,9 @@ class InstallViewModel @Inject constructor(
logs.add(msg)
}

override fun onSuccess(id: String) {
override fun onSuccess(module: LocalModule?) {
event = Event.SUCCEEDED
getLocal(id)
module?.let(::insertLocal)

if (deleteZipFile) {
deleteBySu(zipPath)
Expand All @@ -115,18 +117,20 @@ class InstallViewModel @Inject constructor(
}

console.add("- Installing ${zipFile.name}")
ProviderCompat.moduleManager.install(zipPath, callback)
Compat.moduleManager.install(zipPath, callback)
}

private fun getLocal(id: String) {
private fun insertLocal(module: LocalModule) {
viewModelScope.launch {
modulesRepository.getLocal(id)
localRepository.insertLocal(
module.copy(state = State.UPDATE)
)
}
}

private fun deleteBySu(zipPath: String) {
runCatching {
ProviderCompat.fileManager.deleteOnExit(zipPath)
Compat.fileManager.deleteOnExit(zipPath)
}.onFailure {
Timber.e(it)
}.onSuccess {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewModelScope
import com.sanmer.mrepo.compat.ProviderCompat
import com.sanmer.mrepo.Compat
import com.sanmer.mrepo.database.entity.Repo
import com.sanmer.mrepo.database.entity.Repo.Companion.toRepo
import com.sanmer.mrepo.model.json.UpdateJson
Expand All @@ -37,7 +37,7 @@ class ModuleViewModel @Inject constructor(
private val userPreferencesRepository: UserPreferencesRepository,
savedStateHandle: SavedStateHandle
) : ViewModel() {
val isProviderAlive get() = ProviderCompat.isAlive
val isProviderAlive get() = Compat.isAlive

private val moduleId = getModuleId(savedStateHandle)
var online: OnlineModule by mutableStateOf(OnlineModule.example())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewModelScope
import com.sanmer.mrepo.compat.ProviderCompat
import com.sanmer.mrepo.Compat
import com.sanmer.mrepo.datastore.modules.ModulesMenuCompat
import com.sanmer.mrepo.datastore.repository.Option
import com.sanmer.mrepo.model.json.UpdateJson
Expand Down Expand Up @@ -45,7 +45,7 @@ class ModulesViewModel @Inject constructor(
private val modulesRepository: ModulesRepository,
private val userPreferencesRepository: UserPreferencesRepository,
) : ViewModel() {
val isProviderAlive get() = ProviderCompat.isAlive
val isProviderAlive get() = Compat.isAlive

private val modulesMenu get() = userPreferencesRepository.data
.map { it.modulesMenu }
Expand Down Expand Up @@ -86,7 +86,7 @@ class ModulesViewModel @Inject constructor(
}

private fun providerObserver() {
ProviderCompat.isAliveFlow
Compat.isAliveFlow
.onEach {
if (it) getLocalAll()

Expand Down Expand Up @@ -182,12 +182,12 @@ class ModulesViewModel @Inject constructor(
isOpsRunning = opsTasks.contains(module.id),
toggle = {
opsTasks.add(module.id)
ProviderCompat.moduleManager
Compat.moduleManager
.disable(module.id, opsCallback)
},
change = {
opsTasks.add(module.id)
ProviderCompat.moduleManager
Compat.moduleManager
.remove(module.id, opsCallback)
}
)
Expand All @@ -196,12 +196,12 @@ class ModulesViewModel @Inject constructor(
isOpsRunning = opsTasks.contains(module.id),
toggle = {
opsTasks.add(module.id)
ProviderCompat.moduleManager
Compat.moduleManager
.enable(module.id, opsCallback)
},
change = {
opsTasks.add(module.id)
ProviderCompat.moduleManager
Compat.moduleManager
.remove(module.id, opsCallback)
}
)
Expand All @@ -211,7 +211,7 @@ class ModulesViewModel @Inject constructor(
toggle = {},
change = {
opsTasks.add(module.id)
ProviderCompat.moduleManager
Compat.moduleManager
.enable(module.id, opsCallback)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.sanmer.mrepo.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.sanmer.mrepo.compat.ProviderCompat
import com.sanmer.mrepo.Compat
import com.sanmer.mrepo.datastore.DarkMode
import com.sanmer.mrepo.datastore.WorkingMode
import com.sanmer.mrepo.repository.UserPreferencesRepository
Expand All @@ -16,9 +16,9 @@ import javax.inject.Inject
class SettingsViewModel @Inject constructor(
private val userPreferencesRepository: UserPreferencesRepository
) : ViewModel() {
val isProviderAlive get() = ProviderCompat.isAlive
val isProviderAlive get() = Compat.isAlive

val version get() = ProviderCompat.get("") {
val version get() = Compat.get("") {
with(moduleManager) { "$version (${versionCode})" }
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package dev.sanmer.mrepo.compat.stub;

import dev.sanmer.mrepo.compat.content.LocalModule;

interface IInstallCallback {
void onStdout(String msg);
void onStderr(String msg);
void onSuccess(String id);
void onSuccess(in LocalModule module);
void onFailure();
}
12 changes: 0 additions & 12 deletions compat/src/main/kotlin/dev/sanmer/mrepo/compat/Const.kt

This file was deleted.

Loading

0 comments on commit 89e1e06

Please sign in to comment.