Skip to content

Commit b994bd4

Browse files
authored
Fix sessions test app for target api 34 (#6226)
1 parent 3ec4d00 commit b994bd4

File tree

4 files changed

+38
-16
lines changed

4 files changed

+38
-16
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ internal class SessionLifecycleServiceBinderImpl(private val firebaseApp: Fireba
3636
SessionLifecycleServiceBinder {
3737

3838
override fun bindToService(callback: Messenger, serviceConnection: ServiceConnection) {
39-
val appContext = firebaseApp.applicationContext.applicationContext
39+
val appContext: Context = firebaseApp.applicationContext.applicationContext
4040
Intent(appContext, SessionLifecycleService::class.java).also { intent ->
4141
Log.d(TAG, "Binding service to application.")
4242
// This is necessary for the onBind() to be called by each process
4343
intent.action = android.os.Process.myPid().toString()
4444
intent.putExtra(SessionLifecycleService.CLIENT_CALLBACK_MESSENGER, callback)
45+
intent.setPackage(appContext.packageName)
4546

4647
val isServiceBound =
4748
try {

firebase-sessions/test-app/src/main/AndroidManifest.xml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@
2424
android:supportsRtl="true"
2525
android:theme="@style/Theme.Widget_test_app"
2626
tools:targetApi="31">
27-
<meta-data
28-
android:name="firebase_performance_logcat_enabled"
29-
android:value="true" />
30-
<!-- Override the background timeout for the test app to be 5s instead of 30m -->
31-
<meta-data
32-
android:name="firebase_sessions_sessions_restart_timeout"
33-
android:value="5" />
3427
<activity
3528
android:exported="true"
3629
android:label="@string/app_name"
@@ -42,14 +35,22 @@
4235
<category android:name="android.intent.category.LAUNCHER" />
4336
</intent-filter>
4437
</activity>
45-
38+
<!-- Override the background timeout for the test app to be 5s instead of 30m -->
4639
<activity
4740
android:exported="true"
4841
android:label="@string/app_name"
4942
android:name=".SecondActivity"
5043
android:process=":second"
5144
android:theme="@style/Theme.Widget_test_app.NoActionBar" />
5245

46+
<meta-data
47+
android:name="firebase_performance_logcat_enabled"
48+
android:value="true" />
49+
50+
<meta-data
51+
android:name="firebase_sessions_sessions_restart_timeout"
52+
android:value="5" />
53+
5354
<receiver
5455
android:exported="false"
5556
android:name="CrashWidgetProvider"
@@ -67,6 +68,7 @@
6768
<service
6869
android:enabled="true"
6970
android:exported="false"
71+
android:foregroundServiceType="shortService"
7072
android:name=".ForegroundService"
7173
android:process=":foregroundServiceProcess" />
7274

firebase-sessions/test-app/src/main/kotlin/com/google/firebase/testing/sessions/ForegroundService.kt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import android.app.PendingIntent
2222
import android.app.Service
2323
import android.content.Context
2424
import android.content.Intent
25+
import android.content.pm.ServiceInfo
2526
import android.os.Build
2627
import android.os.IBinder
2728
import android.util.Log
@@ -42,7 +43,7 @@ class ForegroundService : Service() {
4243
this,
4344
0,
4445
Intent(this, MainActivity::class.java),
45-
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
46+
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
4647
)
4748

4849
val crashIntent = Intent(CrashBroadcastReceiver.CRASH_ACTION)
@@ -57,7 +58,7 @@ class ForegroundService : Service() {
5758
this,
5859
0,
5960
Intent(this, SecondActivity::class.java).setAction("MESSAGE"),
60-
PendingIntent.FLAG_IMMUTABLE
61+
PendingIntent.FLAG_IMMUTABLE,
6162
)
6263

6364
val notification =
@@ -72,7 +73,11 @@ class ForegroundService : Service() {
7273
.addAction(R.drawable.ic_launcher_foreground, "Send Message", pendingMsg)
7374
.build()
7475

75-
startForeground(1, notification)
76+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
77+
startForeground(1, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_SHORT_SERVICE)
78+
} else {
79+
startForeground(1, notification)
80+
}
7681
return START_STICKY
7782
}
7883

@@ -91,7 +96,7 @@ class ForegroundService : Service() {
9196
NotificationChannel(
9297
CHANNEL_ID,
9398
"Foreground Service Channel",
94-
NotificationManager.IMPORTANCE_DEFAULT
99+
NotificationManager.IMPORTANCE_DEFAULT,
95100
)
96101
val manager = getSystemService(NotificationManager::class.java)
97102
manager!!.createNotificationChannel(serviceChannel)
@@ -105,7 +110,7 @@ class ForegroundService : Service() {
105110
Log.i(TAG, "Starting foreground serice")
106111
ContextCompat.startForegroundService(
107112
context,
108-
Intent(context, ForegroundService::class.java).putExtra("inputExtra", message)
113+
Intent(context, ForegroundService::class.java).putExtra("inputExtra", message),
109114
)
110115
}
111116

firebase-sessions/test-app/src/main/kotlin/com/google/firebase/testing/sessions/TestApplication.kt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package com.google.firebase.testing.sessions
1818

1919
import android.annotation.SuppressLint
2020
import android.content.IntentFilter
21+
import android.os.Build
2122
import android.os.Handler
2223
import android.os.Looper
2324
import android.widget.TextView
@@ -30,8 +31,21 @@ class TestApplication : MultiDexApplication() {
3031

3132
override fun onCreate() {
3233
super.onCreate()
33-
registerReceiver(broadcastReceiver, IntentFilter(CrashBroadcastReceiver.CRASH_ACTION))
34-
registerReceiver(broadcastReceiver, IntentFilter(CrashBroadcastReceiver.TOAST_ACTION))
34+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
35+
registerReceiver(
36+
broadcastReceiver,
37+
IntentFilter(CrashBroadcastReceiver.CRASH_ACTION),
38+
RECEIVER_NOT_EXPORTED,
39+
)
40+
registerReceiver(
41+
broadcastReceiver,
42+
IntentFilter(CrashBroadcastReceiver.TOAST_ACTION),
43+
RECEIVER_NOT_EXPORTED,
44+
)
45+
} else {
46+
registerReceiver(broadcastReceiver, IntentFilter(CrashBroadcastReceiver.CRASH_ACTION))
47+
registerReceiver(broadcastReceiver, IntentFilter(CrashBroadcastReceiver.TOAST_ACTION))
48+
}
3549
}
3650

3751
class FakeSessionSubscriber : SessionSubscriber {

0 commit comments

Comments
 (0)