Skip to content

Commit 1a13dff

Browse files
authored
Handle when the session lifecycle service binding fails (firebase#5777)
Pair with @PolinaGo. This fixes firebase#5718.
1 parent b77c218 commit 1a13dff

3 files changed

Lines changed: 79 additions & 5 deletions

File tree

firebase-sessions/firebase-sessions.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ dependencies {
8787
testImplementation(libs.truth)
8888

8989
androidTestImplementation(libs.androidx.test.junit)
90+
androidTestImplementation(libs.androidx.test.rules)
9091
androidTestImplementation(libs.androidx.test.runner)
9192
androidTestImplementation(libs.truth)
9293
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.firebase.sessions
18+
19+
import android.content.ComponentName
20+
import android.content.Context
21+
import android.content.Intent
22+
import android.content.ServiceConnection
23+
import android.os.IBinder
24+
import androidx.test.core.app.ApplicationProvider
25+
import androidx.test.ext.junit.runners.AndroidJUnit4
26+
import androidx.test.rule.ServiceTestRule
27+
import com.google.common.truth.Truth.assertThat
28+
import org.junit.Rule
29+
import org.junit.Test
30+
import org.junit.runner.RunWith
31+
32+
@RunWith(AndroidJUnit4::class)
33+
class SessionLifecycleServiceBinderTest {
34+
@get:Rule val serviceRule = ServiceTestRule()
35+
36+
@Test
37+
fun bindSessionLifecycleService() {
38+
val serviceConnection =
39+
object : ServiceConnection {
40+
var connected: Boolean = false
41+
42+
override fun onServiceConnected(className: ComponentName?, serviceBinder: IBinder?) {
43+
connected = true
44+
}
45+
46+
override fun onServiceDisconnected(className: ComponentName?) {
47+
connected = false
48+
}
49+
}
50+
51+
val sessionLifecycleServiceIntent =
52+
Intent(ApplicationProvider.getApplicationContext(), SessionLifecycleService::class.java)
53+
54+
serviceRule.bindService(
55+
sessionLifecycleServiceIntent,
56+
serviceConnection,
57+
Context.BIND_IMPORTANT or Context.BIND_AUTO_CREATE,
58+
)
59+
60+
assertThat(serviceConnection.connected).isTrue()
61+
}
62+
}

firebase-sessions/src/main/kotlin/com/google/firebase/sessions/SessionLifecycleServiceBinder.kt

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,22 @@ internal class SessionLifecycleServiceBinderImpl(private val firebaseApp: Fireba
4949
// This is necessary for the onBind() to be called by each process
5050
intent.action = android.os.Process.myPid().toString()
5151
intent.putExtra(SessionLifecycleService.CLIENT_CALLBACK_MESSENGER, callback)
52-
appContext.bindService(
53-
intent,
54-
serviceConnection,
55-
Context.BIND_IMPORTANT or Context.BIND_AUTO_CREATE
56-
)
52+
53+
val isServiceBound =
54+
try {
55+
appContext.bindService(
56+
intent,
57+
serviceConnection,
58+
Context.BIND_IMPORTANT or Context.BIND_AUTO_CREATE,
59+
)
60+
} catch (ex: SecurityException) {
61+
Log.w(TAG, "Failed to bind session lifecycle service to application.", ex)
62+
false
63+
}
64+
if (!isServiceBound) {
65+
appContext.unbindService(serviceConnection)
66+
Log.i(TAG, "Session lifecycle service binding failed.")
67+
}
5768
}
5869
}
5970

0 commit comments

Comments
 (0)