@@ -22,20 +22,19 @@ import kotlin.coroutines.resume
2222import kotlin.system.exitProcess
2323
2424
25- @Suppress(" unused" )
26- class UserService : IUserService .Stub {
27- @Keep
28- constructor () {
29- Log .i(" UserService" , " constructor" )
30- }
31-
32- @Keep
33- constructor (context: Context ) {
34- Log .i(" UserService" , " constructor with Context: context=$context " )
25+ // https://github.com/RikkaApps/Shizuku/issues/1171#issuecomment-2952442340
26+ @Keep
27+ class UserService (val context : Context ) : IUserService.Stub() {
28+
29+ init {
30+ Log .d(
31+ " UserService" ,
32+ " constructor(context=${context.packageName} ,pid=${android.os.Process .myPid()} ,uid=${android.os.Process .myUid()} )"
33+ )
3534 }
3635
3736 override fun destroy () {
38- Log .i (" UserService" , " destroy" )
37+ Log .d (" UserService" , " destroy" )
3938 exitProcess(0 )
4039 }
4140
@@ -44,6 +43,7 @@ class UserService : IUserService.Stub {
4443 }
4544
4645 override fun execCommand (command : String ): CommandResult {
46+ Log .d(" UserService" , " execCommand(command=$command )" )
4747 val process = Runtime .getRuntime().exec(" sh" )
4848 val outputStream = DataOutputStream (process.outputStream)
4949 val commandResult = try {
@@ -109,8 +109,23 @@ class UserService : IUserService.Stub {
109109 val screenshotBuffer = SurfaceControlHidden .captureDisplay(captureArgs)
110110 return screenshotBuffer?.asBitmap()
111111 }
112+
113+ override fun killLegacyService (): Int {
114+ val pid = android.os.Process .myPid()
115+ val idReg = " \\ d+" .toRegex()
116+ val legacyPids = execCommand(" ps | grep '${context.packageName} :$shizukuPsSuffix '" )
117+ .result.lineSequence()
118+ .mapNotNull { idReg.find(it)?.value?.toInt() }
119+ .filter { it != pid }.toList()
120+ if (legacyPids.isNotEmpty()) {
121+ execCommand(legacyPids.joinToString(" ;" ) { " kill $it " })
122+ }
123+ return legacyPids.size
124+ }
112125}
113126
127+ private const val shizukuPsSuffix = " shizuku-user-service"
128+
114129private fun unbindUserService (
115130 serviceArgs : Shizuku .UserServiceArgs ,
116131 connection : ServiceConnection ,
@@ -166,7 +181,7 @@ suspend fun buildServiceWrapper(): UserServiceWrapper? {
166181 val serviceArgs = Shizuku
167182 .UserServiceArgs (UserService ::class .componentName)
168183 .daemon(false )
169- .processNameSuffix(" shizuku-user-service " )
184+ .processNameSuffix(shizukuPsSuffix )
170185 .debuggable(META .debuggable)
171186 .version(META .versionCode)
172187 .tag(" default" )
0 commit comments