| | |
| | | |
| | | class MainActivity : ComponentActivity() { |
| | | private var mediaAidlInterface: IMediaAidlInterface? = null |
| | | private var serviceConnection: ServiceConnection? = null |
| | | private var isServiceBound = false |
| | | private val isServiceBoundState = mutableStateOf(false) |
| | | |
| | | private val callback = object : IMyCallback.Stub() { |
| | | @Throws(RemoteException::class) |
| | |
| | | super.onCreate(savedInstanceState) |
| | | enableEdgeToEdge() |
| | | |
| | | var isServiceBoundState by mutableStateOf(false) |
| | | |
| | | val serviceConnection = object : ServiceConnection { |
| | | serviceConnection = object : ServiceConnection { |
| | | override fun onServiceConnected(name: ComponentName?, service: IBinder?) { |
| | | mediaAidlInterface = IMediaAidlInterface.Stub.asInterface(service) |
| | | isServiceBoundState = true |
| | | isServiceBound = true |
| | | isServiceBoundState.value = true |
| | | Timber.d("FloatingService connected") |
| | | } |
| | | |
| | | override fun onServiceDisconnected(name: ComponentName?) { |
| | | mediaAidlInterface = null |
| | | isServiceBoundState = false |
| | | isServiceBound = false |
| | | isServiceBoundState.value = false |
| | | Timber.d("FloatingService disconnected") |
| | | } |
| | | } |
| | |
| | | color = MaterialTheme.colorScheme.background |
| | | ) { |
| | | MainScreen( |
| | | isServiceBound = isServiceBoundState, |
| | | isServiceBound = isServiceBoundState.value, |
| | | onBindService = { |
| | | if (!isServiceBoundState) { |
| | | if (!isServiceBoundState.value && serviceConnection != null) { |
| | | val intent = Intent(this@MainActivity, FloatingService::class.java) |
| | | bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE) |
| | | bindService(intent, serviceConnection!!, Context.BIND_AUTO_CREATE) |
| | | Timber.d("Binding FloatingService") |
| | | } |
| | | }, |
| | | onUnbindService = { |
| | | if (isServiceBoundState) { |
| | | try { |
| | | mediaAidlInterface?.unregisterCallback(callback) |
| | | } catch (e: RemoteException) { |
| | | Timber.e(e, "Error unregistering callback") |
| | | } |
| | | unbindService(serviceConnection) |
| | | isServiceBoundState = false |
| | | mediaAidlInterface = null |
| | | Timber.d("Unbinding FloatingService") |
| | | } |
| | | unbindServiceInternal() |
| | | }, |
| | | onStartAndroidRecord = { startAndroidRecord() }, |
| | | onStopAndroidRecord = { stopAndroidRecord() }, |
| | |
| | | } |
| | | } |
| | | |
| | | override fun onDestroy() { |
| | | super.onDestroy() |
| | | if (mediaAidlInterface != null) { |
| | | private fun unbindServiceInternal() { |
| | | if (isServiceBound && serviceConnection != null) { |
| | | try { |
| | | mediaAidlInterface?.unregisterCallback(callback) |
| | | } catch (e: RemoteException) { |
| | | Timber.e(e, "Error unregistering callback") |
| | | } |
| | | try { |
| | | unbindService(serviceConnection!!) |
| | | Timber.d("Unbinding FloatingService") |
| | | } catch (e: Exception) { |
| | | Timber.e(e, "Error unbinding service") |
| | | } |
| | | isServiceBound = false |
| | | isServiceBoundState.value = false |
| | | mediaAidlInterface = null |
| | | } |
| | | } |
| | | |
| | | override fun onDestroy() { |
| | | super.onDestroy() |
| | | unbindServiceInternal() |
| | | } |
| | | } |
| | | |
| | | @Composable |