-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
69 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
107 changes: 68 additions & 39 deletions
107
demo/src/main/kotlin/com/houvven/lsposed/scope/demo/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,86 @@ | ||
package com.houvven.lsposed.scope.demo | ||
|
||
import android.content.ComponentName | ||
import android.content.ServiceConnection | ||
import android.os.Bundle | ||
import android.os.IBinder | ||
import android.util.Log | ||
import androidx.activity.ComponentActivity | ||
import io.github.houvven.lservice.ILSPosedBridgeService | ||
import io.github.houvven.lservice.LSPosedBridgeRootService | ||
import androidx.activity.compose.setContent | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import io.github.houvven.lservice.LServiceBridgeRootService | ||
import kotlinx.coroutines.delay | ||
import org.lsposed.lspd.ILSPManagerService | ||
import org.lsposed.lspd.service.ILSPApplicationService | ||
import java.util.Optional | ||
|
||
class MainActivity : ComponentActivity() { | ||
|
||
private var managerService: ILSPManagerService? = null | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
LSPosedBridgeRootService.bindRootService(this, LSPosedBridgeServiceConnection) | ||
Thread { | ||
while (!LSPosedBridgeServiceConnection.serviceOptional.isPresent) { | ||
Thread.sleep(200) | ||
} | ||
|
||
LSPosedBridgeServiceConnection.serviceOptional.ifPresent { bridgeService -> | ||
val applicationServiceBinder = | ||
bridgeService.obtainLSPosedApplicationServiceBinder() ?: return@ifPresent | ||
val applicationService = | ||
ILSPApplicationService.Stub.asInterface(applicationServiceBinder) | ||
|
||
val managerServiceBinder = | ||
bridgeService.obtainLSPosedManagerServiceBinder(applicationService) | ||
?: return@ifPresent | ||
val managerService = ILSPManagerService.Stub.asInterface(managerServiceBinder) | ||
val xposedApiVersion = managerService.xposedApiVersion | ||
Log.i("MainActivity", "xposedApiVersion: $xposedApiVersion") | ||
} | ||
}.start() | ||
} | ||
val connection = LServiceBridgeRootService.DefaultServiceConnection() | ||
LServiceBridgeRootService.bind(this.applicationContext, connection, apkFile) | ||
|
||
setContent { | ||
var isBinderAlive by remember { mutableStateOf(false) } | ||
var api by remember { mutableStateOf("") } | ||
var xposedApiVersion by remember { mutableStateOf("") } | ||
var xposedVersionName by remember { mutableStateOf("") } | ||
var xposedVersionCode by remember { mutableStateOf("") } | ||
|
||
object LSPosedBridgeServiceConnection : ServiceConnection { | ||
MaterialTheme { | ||
Column( | ||
verticalArrangement = Arrangement.Top, | ||
modifier = Modifier | ||
) { | ||
Text(text = "api: $api") | ||
Text(text = "xposedApiVersion: $xposedApiVersion") | ||
Text(text = "xposedVersionName: $xposedVersionName") | ||
Text(text = "xposedVersionCode: $xposedVersionCode") | ||
} | ||
} | ||
|
||
private const val TAG = "LSPosedBridgeServiceConnection" | ||
LaunchedEffect(key1 = this) { | ||
while (connection.lServiceBridge == null) { | ||
delay(200) | ||
} | ||
isBinderAlive = true | ||
} | ||
|
||
var serviceOptional: Optional<ILSPosedBridgeService> = Optional.empty() | ||
private set | ||
LaunchedEffect(key1 = isBinderAlive) { | ||
while (managerService == null) { | ||
runCatching { | ||
managerService = connection.getManagerService(connection.applicationService) | ||
}.onFailure { delay(200) } | ||
} | ||
managerService!!.let { | ||
api = it.api | ||
xposedApiVersion = it.xposedApiVersion.toString() | ||
xposedVersionName = it.xposedVersionName | ||
xposedVersionCode = it.xposedVersionCode.toString() | ||
} | ||
} | ||
|
||
override fun onServiceConnected(name: ComponentName, service: IBinder) { | ||
Log.i(TAG, "onServiceConnected: $name, ${service.interfaceDescriptor}") | ||
serviceOptional = Optional.of(ILSPosedBridgeService.Stub.asInterface(service)) | ||
} | ||
} | ||
|
||
override fun onServiceDisconnected(name: ComponentName?) { | ||
Log.i(TAG, "onServiceDisconnected: $name") | ||
serviceOptional = Optional.empty() | ||
private val apkFile: String | ||
get() { | ||
val file = cacheDir.resolve("lsposed.apk") | ||
if (!file.exists()) { | ||
assets.open("lsposed-manager.apk").use { inputStream -> | ||
file.outputStream().use { out -> | ||
out.write(inputStream.readBytes()) | ||
out.flush() | ||
} | ||
} | ||
} | ||
return file.absolutePath | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters