@@ -25,13 +25,6 @@ import io.github.sds100.keymapper.sysbridge.R
2525import io.github.sds100.keymapper.sysbridge.adb.AdbManager
2626import io.github.sds100.keymapper.sysbridge.ktx.loge
2727import io.github.sds100.keymapper.sysbridge.shizuku.ShizukuStarterService
28- import kotlinx.coroutines.Dispatchers
29- import kotlinx.coroutines.runBlocking
30- import kotlinx.coroutines.sync.Mutex
31- import kotlinx.coroutines.sync.withLock
32- import kotlinx.coroutines.withContext
33- import rikka.shizuku.Shizuku
34- import timber.log.Timber
3528import java.io.BufferedReader
3629import java.io.DataInputStream
3730import java.io.File
@@ -44,12 +37,19 @@ import java.util.zip.ZipEntry
4437import java.util.zip.ZipFile
4538import javax.inject.Inject
4639import javax.inject.Singleton
40+ import kotlinx.coroutines.Dispatchers
41+ import kotlinx.coroutines.runBlocking
42+ import kotlinx.coroutines.sync.Mutex
43+ import kotlinx.coroutines.sync.withLock
44+ import kotlinx.coroutines.withContext
45+ import rikka.shizuku.Shizuku
46+ import timber.log.Timber
4747
4848@Singleton
4949class SystemBridgeStarter @Inject constructor(
5050 @ApplicationContext private val ctx : Context ,
5151 private val adbManager : AdbManager ,
52- private val buildConfigProvider : BuildConfigProvider
52+ private val buildConfigProvider : BuildConfigProvider ,
5353) {
5454 private val userManager by lazy { ctx.getSystemService(UserManager ::class .java)!! }
5555
@@ -60,10 +60,7 @@ class SystemBridgeStarter @Inject constructor(
6060 private val startMutex: Mutex = Mutex ()
6161
6262 private val shizukuStarterConnection: ServiceConnection = object : ServiceConnection {
63- override fun onServiceConnected (
64- name : ComponentName ? ,
65- binder : IBinder ?
66- ) {
63+ override fun onServiceConnected (name : ComponentName ? , binder : IBinder ? ) {
6764 Timber .i(" Shizuku starter service connected" )
6865
6966 val service = IShizukuStarterService .Stub .asInterface(binder)
@@ -81,7 +78,6 @@ class SystemBridgeStarter @Inject constructor(
8178 }
8279 })
8380 }
84-
8581 } catch (e: RemoteException ) {
8682 Timber .e(" Exception starting with Shizuku starter service: $e " )
8783 } finally {
@@ -119,7 +115,7 @@ class SystemBridgeStarter @Inject constructor(
119115 try {
120116 Shizuku .bindUserService(
121117 args,
122- shizukuStarterConnection
118+ shizukuStarterConnection,
123119 )
124120 } catch (e: Exception ) {
125121 Timber .e(" Exception when starting System Bridge with Shizuku. $e " )
@@ -158,7 +154,9 @@ class SystemBridgeStarter @Inject constructor(
158154 })
159155 }
160156
161- suspend fun startSystemBridge (executeCommand : suspend (String ) -> KMResult <String >): KMResult <String > {
157+ suspend fun startSystemBridge (
158+ executeCommand : suspend (String ) -> KMResult <String >,
159+ ): KMResult <String > {
162160 startMutex.withLock {
163161 val externalFilesParent = try {
164162 ctx.getExternalFilesDir(null )?.parentFile
@@ -176,7 +174,7 @@ class SystemBridgeStarter @Inject constructor(
176174 // Create the start.sh shell script
177175 writeStarterScript(
178176 outputStarterScript,
179- outputStarterBinary.absolutePath
177+ outputStarterBinary.absolutePath,
180178 )
181179 Success (Unit )
182180 }
@@ -189,9 +187,12 @@ class SystemBridgeStarter @Inject constructor(
189187 .then { executeCommand(startCommand) }
190188 .then { output ->
191189 // Adb on Android 11 has no permission to access Android/data so use /data/user_de.
192- if (output.contains(" /Android/data/${ctx.packageName} /start.sh: Permission denied" )) {
190+ if (output.contains(
191+ " /Android/data/${ctx.packageName} /start.sh: Permission denied" ,
192+ )
193+ ) {
193194 Timber .w(
194- " ADB has no permission to access Android/data/${ctx.packageName} /start.sh. Trying to use /data/user_de instead..."
195+ " ADB has no permission to access Android/data/${ctx.packageName} /start.sh. Trying to use /data/user_de instead..." ,
195196 )
196197
197198 startSystemBridgeFromProtectedStorage(executeCommand)
@@ -203,15 +204,15 @@ class SystemBridgeStarter @Inject constructor(
203204 }
204205
205206 private suspend fun startSystemBridgeFromProtectedStorage (
206- executeCommand : suspend (String ) -> KMResult <String >
207+ executeCommand : suspend (String ) -> KMResult <String >,
207208 ): KMResult <String > {
208209 val protectedStorageDir =
209210 ctx.createDeviceProtectedStorageContext().filesDir.parentFile!!
210211
211212 Timber .i(" Protected storage dir: ${protectedStorageDir.absolutePath} " )
212213
213214 try {
214- Os .chmod(protectedStorageDir.absolutePath, 457 /* 0711 */ )
215+ Os .chmod(protectedStorageDir.absolutePath, 457 ) /* 0711 */
215216 } catch (e: ErrnoException ) {
216217 e.printStackTrace()
217218 }
@@ -227,7 +228,7 @@ class SystemBridgeStarter @Inject constructor(
227228
228229 writeStarterScript(
229230 outputStarterScript,
230- outputStarterBinary.absolutePath
231+ outputStarterBinary.absolutePath,
231232 )
232233 }
233234
@@ -236,20 +237,19 @@ class SystemBridgeStarter @Inject constructor(
236237
237238 // Make starter binary executable
238239 try {
239- Os .chmod(outputStarterBinary.absolutePath, 420 /* 0644 */ )
240+ Os .chmod(outputStarterBinary.absolutePath, 420 ) /* 0644 */
240241 } catch (e: ErrnoException ) {
241242 e.printStackTrace()
242243 }
243244
244245 // Make starter script executable
245246 try {
246- Os .chmod(outputStarterScript.absolutePath, 420 /* 0644 */ )
247+ Os .chmod(outputStarterScript.absolutePath, 420 ) /* 0644 */
247248 } catch (e: ErrnoException ) {
248249 e.printStackTrace()
249250 }
250251
251252 return executeCommand(startCommand)
252-
253253 } catch (e: IOException ) {
254254 loge(" write files" , e)
255255 return KMError .UnknownIOError
@@ -271,17 +271,15 @@ class SystemBridgeStarter @Inject constructor(
271271
272272 File (" $libPath /$libraryName " ).copyTo(out )
273273 return Success (Unit )
274-
275274 } catch (e: Exception ) {
276- Timber .w(" Native library not found. Extracting from APKs. Exception: ${e.toString()} " )
275+ Timber .w(" Native library not found. Extracting from APKs. Exception: $e " )
277276
278277 val apkPaths: Array <String > = arrayOf(baseApkPath, * splitApkPaths)
279278
280279 Timber .i(" APK paths: ${apkPaths.joinToString()} " )
281280
282281 for (apk in apkPaths) {
283282 with (ZipFile (apk)) {
284-
285283 for (abi in Build .SUPPORTED_ABIS ) {
286284 val expectedLibraryPath = " lib/$abi /$libraryName "
287285
0 commit comments