diff --git a/vector/src/main/java/im/vector/app/features/location/LocationSharingAndroidServiceBinder.kt b/vector/src/main/java/im/vector/app/features/location/LocationSharingAndroidServiceBinder.kt new file mode 100644 index 0000000000..6d791365f7 --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/location/LocationSharingAndroidServiceBinder.kt @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.features.location + +import android.os.Binder + +class LocationSharingAndroidServiceBinder : Binder() { + + private var locationSharingAndroidService: LocationSharingAndroidService? = null + + fun setup(service: LocationSharingAndroidService) { + locationSharingAndroidService = service + } + + fun cleanUp() { + locationSharingAndroidService = null + } + + fun getService() = locationSharingAndroidService +} diff --git a/vector/src/main/java/im/vector/app/features/location/LocationSharingServiceConnection.kt b/vector/src/main/java/im/vector/app/features/location/LocationSharingServiceConnection.kt index 9e905060d9..7e873a3716 100644 --- a/vector/src/main/java/im/vector/app/features/location/LocationSharingServiceConnection.kt +++ b/vector/src/main/java/im/vector/app/features/location/LocationSharingServiceConnection.kt @@ -67,7 +67,7 @@ class LocationSharingServiceConnection @Inject constructor( } override fun onServiceConnected(className: ComponentName, binder: IBinder) { - locationSharingAndroidService = (binder as LocationSharingAndroidService.LocalBinder).getService().also { service -> + locationSharingAndroidService = (binder as LocationSharingAndroidServiceBinder).getService()?.also { service -> service.callback = this getActiveSessionCoroutineScope()?.let { scope -> service.roomIdsOfActiveLives diff --git a/vector/src/main/java/im/vector/app/features/location/live/tracking/LocationSharingAndroidService.kt b/vector/src/main/java/im/vector/app/features/location/live/tracking/LocationSharingAndroidService.kt index 6babc94385..819ec8634e 100644 --- a/vector/src/main/java/im/vector/app/features/location/live/tracking/LocationSharingAndroidService.kt +++ b/vector/src/main/java/im/vector/app/features/location/live/tracking/LocationSharingAndroidService.kt @@ -17,7 +17,6 @@ package im.vector.app.features.location.live.tracking import android.content.Intent -import android.os.Binder import android.os.IBinder import android.os.Parcelable import androidx.core.app.NotificationManagerCompat @@ -62,7 +61,7 @@ class LocationSharingAndroidService : VectorAndroidService(), LocationTracker.Ca @Inject lateinit var getLiveLocationShareSummaryUseCase: GetLiveLocationShareSummaryUseCase @Inject lateinit var checkIfEventIsRedactedUseCase: CheckIfEventIsRedactedUseCase - private val binder = LocalBinder() + private var binder: LocationSharingAndroidServiceBinder? = null private val liveInfoSet = linkedSetOf() var callback: Callback? = null @@ -76,6 +75,7 @@ class LocationSharingAndroidService : VectorAndroidService(), LocationTracker.Ca override fun onCreate() { super.onCreate() Timber.i("onCreate") + binder = LocationSharingAndroidServiceBinder().also { it.setup(this) } initLocationTracking() } @@ -205,6 +205,8 @@ class LocationSharingAndroidService : VectorAndroidService(), LocationTracker.Ca override fun onDestroy() { super.onDestroy() Timber.i("onDestroy") + binder?.cleanUp() + binder = null jobs.forEach { it.cancel() } jobs.clear() locationTracker.removeCallback(this) @@ -251,14 +253,10 @@ class LocationSharingAndroidService : VectorAndroidService(), LocationTracker.Ca return liveInfoSet.map { it.roomArgs.roomId }.toSet() } - override fun onBind(intent: Intent?): IBinder { + override fun onBind(intent: Intent?): IBinder? { return binder } - inner class LocalBinder : Binder() { - fun getService(): LocationSharingAndroidService = this@LocationSharingAndroidService - } - interface Callback { fun onServiceError(error: Throwable) }