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

Use ServiceManagerCompat #237

Merged
merged 7 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jobs:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: 'recursive'
fetch-depth: 0

- name: Set up signing key
Expand All @@ -39,6 +40,10 @@ jobs:
validate-wrappers: true
gradle-home-cache-cleanup: true

- name: Build dependencies
working-directory: compat
run: ./gradlew publishToMavenLocal

- name: Build with Gradle
run: ./gradlew assembleRelease

Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "compat"]
path = compat
url = [email protected]:SanmerApps/ServiceManagerCompat.git
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ protobuf {
}

dependencies {
compileOnly(projects.hiddenApi)
implementation(projects.compat)
implementation(projects.core)
implementation(libs.sanmer.su)

implementation(libs.androidx.activity.compose)
implementation(libs.androidx.appcompat)
Expand Down
14 changes: 2 additions & 12 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
-verbose
-dontpreverify
-optimizationpasses 5
-dontskipnonpubliclibraryclasses

-dontwarn org.conscrypt.**
-dontwarn kotlinx.serialization.**
-repackageclasses dev.sanmer.mrepo

# Keep DataStore fields
-keepclassmembers class * extends com.google.protobuf.GeneratedMessageLite* {
<fields>;
}

-repackageclasses com.sanmer.mrepo
-keepclassmembers class * extends com.google.protobuf.GeneratedMessageLite* { <fields>; }
2 changes: 0 additions & 2 deletions app/src/main/kotlin/com/sanmer/mrepo/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.sanmer.mrepo.network.NetworkUtils
import com.sanmer.mrepo.utils.timber.DebugTree
import com.sanmer.mrepo.utils.timber.ReleaseTree
import dagger.hilt.android.HiltAndroidApp
import dev.sanmer.mrepo.compat.ServiceManagerCompat
import timber.log.Timber

@HiltAndroidApp
Expand All @@ -22,7 +21,6 @@ class App : Application() {
override fun onCreate() {
super.onCreate()

ServiceManagerCompat.setHiddenApiExemptions()
NotificationUtils.init(this)
NetworkUtils.setCacheDir(cacheDir)
}
Expand Down
18 changes: 10 additions & 8 deletions app/src/main/kotlin/com/sanmer/mrepo/Compat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ 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.ServiceManagerCompat
import dev.sanmer.mrepo.compat.stub.IFileManager
import dev.sanmer.mrepo.compat.stub.IModuleManager
import dev.sanmer.mrepo.compat.stub.IPowerManager
import dev.sanmer.mrepo.compat.stub.IServiceManager
import dev.sanmer.mrepo.ModuleManager
import dev.sanmer.mrepo.stub.IModuleManager
import dev.sanmer.su.IServiceManager
import dev.sanmer.su.ServiceManagerCompat
import dev.sanmer.su.ServiceManagerCompat.createBy
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import timber.log.Timber
Expand All @@ -25,9 +25,11 @@ object Compat {
private val _isAliveFlow = MutableStateFlow(false)
val isAliveFlow get() = _isAliveFlow.asStateFlow()

val moduleManager: IModuleManager get() = mService.moduleManager
val fileManager: IFileManager get() = mService.fileManager
val powerManager: IPowerManager get() = mService.powerManager
val moduleManager: IModuleManager by lazy {
IModuleManager.Stub.asInterface(
ModuleManager::class.createBy(mService)
)
}

private fun state(): Boolean {
isAlive = mServiceOrNull != null
Expand Down
19 changes: 3 additions & 16 deletions app/src/main/kotlin/com/sanmer/mrepo/model/local/LocalModule.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
package com.sanmer.mrepo.model.local

import com.sanmer.mrepo.utils.Utils
import dev.sanmer.mrepo.compat.content.LocalModule
import dev.sanmer.mrepo.content.Module

typealias LocalModule = LocalModule
typealias LocalModule = Module

val LocalModule.versionDisplay get() = Utils.getVersionDisplay(version, versionCode)

fun LocalModule.Companion.example() =
LocalModule(
id = "local_example",
name = "Example",
version = "2022.08.16",
versionCode = 1703,
author = "Sanmer",
description = "This is an example!",
updateJson = "",
state = State.ENABLE,
lastUpdated = 0L
)
val Module.versionDisplay get() = Utils.getVersionDisplay(version, versionCode)
2 changes: 1 addition & 1 deletion app/src/main/kotlin/com/sanmer/mrepo/model/local/State.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.sanmer.mrepo.model.local

import dev.sanmer.mrepo.compat.content.State
import dev.sanmer.mrepo.content.State

typealias State = State
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ import javax.inject.Singleton
class ModulesRepository @Inject constructor(
private val localRepository: LocalRepository,
) {
private val mm get() = Compat.moduleManager

suspend fun getLocalAll() = withContext(Dispatchers.IO) {
with(Compat.moduleManager.modules) {
with(mm.modules) {
localRepository.deleteLocalAll()
localRepository.insertLocal(this)
localRepository.clearUpdatableTag(map { it.id })
}
}

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

Expand Down
39 changes: 17 additions & 22 deletions app/src/main/kotlin/com/sanmer/mrepo/viewmodel/InstallViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ 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.delegate.PowerManagerDelegate
import dev.sanmer.mrepo.compat.stub.IInstallCallback
import dev.sanmer.mrepo.content.State
import dev.sanmer.mrepo.stub.IInstallCallback
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
Expand All @@ -34,6 +33,8 @@ class InstallViewModel @Inject constructor(
private val localRepository: LocalRepository,
private val userPreferencesRepository: UserPreferencesRepository,
) : ViewModel() {
private val mm get() = Compat.moduleManager

val logs = mutableListOf<String>()
val console = mutableStateListOf<String>()
var event by mutableStateOf(Event.LOADING)
Expand All @@ -45,11 +46,7 @@ class InstallViewModel @Inject constructor(
Timber.d("InstallViewModel init")
}

fun reboot() {
PowerManagerDelegate(Compat.powerManager).apply {
reboot()
}
}
fun reboot() = mm.reboot()

suspend fun writeLogsTo(context: Context, uri: Uri) = withContext(Dispatchers.IO) {
runCatching {
Expand All @@ -74,13 +71,12 @@ class InstallViewModel @Inject constructor(
val path = context.getPathForUri(uri)
Timber.d("path = $path")

Compat.moduleManager
.getModuleInfo(path)?.let {
Timber.d("module = $it")
install(path)
mm.getModuleInfo(path)?.let {
Timber.d("module = $it")
install(path)

return@withContext
}
return@withContext
}

console.add("- Copying zip to temp directory")
val tmpFile = context.copyToDir(uri, context.tmpDir)
Expand All @@ -91,13 +87,12 @@ class InstallViewModel @Inject constructor(
}
}

Compat.moduleManager
.getModuleInfo(tmpFile.path)?.let {
Timber.d("module = $it")
install(tmpFile.path)
mm.getModuleInfo(tmpFile.path)?.let {
Timber.d("module = $it")
install(tmpFile.path)

return@withContext
}
return@withContext
}

event = Event.FAILED
console.add("- Zip parsing failed")
Expand Down Expand Up @@ -132,7 +127,7 @@ class InstallViewModel @Inject constructor(
}

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

private fun insertLocal(module: LocalModule) {
Expand All @@ -145,7 +140,7 @@ class InstallViewModel @Inject constructor(

private fun deleteBySu(zipPath: String) {
runCatching {
Compat.fileManager.deleteOnExit(zipPath)
mm.deleteOnExit(zipPath)
}.onFailure {
Timber.e(it)
}.onSuccess {
Expand Down
18 changes: 7 additions & 11 deletions app/src/main/kotlin/com/sanmer/mrepo/viewmodel/ModulesViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import com.sanmer.mrepo.repository.UserPreferencesRepository
import com.sanmer.mrepo.service.DownloadService
import com.sanmer.mrepo.utils.Utils
import dagger.hilt.android.lifecycle.HiltViewModel
import dev.sanmer.mrepo.compat.stub.IModuleOpsCallback
import dev.sanmer.mrepo.stub.IModuleOpsCallback
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.combine
Expand All @@ -45,6 +45,7 @@ class ModulesViewModel @Inject constructor(
private val modulesRepository: ModulesRepository,
private val userPreferencesRepository: UserPreferencesRepository,
) : ViewModel() {
private val mm get() = Compat.moduleManager
val isProviderAlive get() = Compat.isAlive

private val modulesMenu get() = userPreferencesRepository.data
Expand Down Expand Up @@ -182,27 +183,23 @@ class ModulesViewModel @Inject constructor(
isOpsRunning = opsTasks.contains(module.id),
toggle = {
opsTasks.add(module.id)
Compat.moduleManager
.disable(module.id, opsCallback)
mm.disable(module.id, opsCallback)
},
change = {
opsTasks.add(module.id)
Compat.moduleManager
.remove(module.id, opsCallback)
mm.remove(module.id, opsCallback)
}
)

State.DISABLE -> ModuleOps(
isOpsRunning = opsTasks.contains(module.id),
toggle = {
opsTasks.add(module.id)
Compat.moduleManager
.enable(module.id, opsCallback)
mm.enable(module.id, opsCallback)
},
change = {
opsTasks.add(module.id)
Compat.moduleManager
.remove(module.id, opsCallback)
mm.remove(module.id, opsCallback)
}
)

Expand All @@ -211,8 +208,7 @@ class ModulesViewModel @Inject constructor(
toggle = {},
change = {
opsTasks.add(module.id)
Compat.moduleManager
.enable(module.id, opsCallback)
mm.enable(module.id, opsCallback)
}
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import javax.inject.Inject
class SettingsViewModel @Inject constructor(
private val userPreferencesRepository: UserPreferencesRepository
) : ViewModel() {
private val mm get() = Compat.moduleManager
val isProviderAlive get() = Compat.isAlive

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

init {
Expand Down
1 change: 1 addition & 0 deletions compat
Submodule compat added at e58123
28 changes: 0 additions & 28 deletions compat/build.gradle.kts

This file was deleted.

12 changes: 0 additions & 12 deletions compat/src/main/AndroidManifest.xml

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading