Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes in the Session Test App to verify Fireperf #no-changelog #6809

Merged
merged 15 commits into from
Mar 31, 2025
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ internal class SessionLifecycleService : Service() {
handlerThread.start()
messageHandler = MessageHandler(handlerThread.looper)
messenger = Messenger(messageHandler)
Log.d(TAG, "Service created on process ${android.os.Process.myPid()}")
}

/** Called when a new [SessionLifecycleClient] binds to this service. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.FirebaseApp
import com.google.firebase.perf.FirebasePerformance

open class BaseActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
FirebaseApp.initializeApp(this)
setProcessAttribute()
logProcessDetails()
logFirebaseDetails()
Log.i(TAG, "onCreate - ${getProcessName()} - ${getImportance()}")
}

Expand Down Expand Up @@ -64,9 +68,31 @@ open class BaseActivity : AppCompatActivity() {
return processInfo.importance
}

private fun getProcessName(): String =
protected fun getProcessName(): String =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) Application.getProcessName() else "unknown"

private fun logProcessDetails() {
val pid = android.os.Process.myPid()
val uid = android.os.Process.myUid()
val activity = javaClass.name
val process = getProcessName()
Log.i(TAG, "activity: $activity process: $process, pid: $pid, uid: $uid")
}

private fun logFirebaseDetails() {
val activity = javaClass.name
val firebaseApps = FirebaseApp.getApps(this)
val defaultFirebaseApp = FirebaseApp.getInstance()
Log.i(
TAG,
"activity: $activity firebase: ${defaultFirebaseApp.name} appsCount: ${firebaseApps.count()}"
)
}

private fun setProcessAttribute() {
FirebasePerformance.getInstance().putAttribute("process_name", getProcessName())
}

companion object {
val TAG = "BaseActivity"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.firebase.testing.sessions

import android.app.Application
import android.content.Intent
import android.content.Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
Expand All @@ -26,14 +27,20 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import com.google.firebase.crashlytics.FirebaseCrashlytics
import com.google.firebase.perf.FirebasePerformance
import com.google.firebase.testing.sessions.databinding.FragmentFirstBinding
import java.util.Date
import java.util.Locale
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/** A simple [Fragment] subclass as the default destination in the navigation. */
class FirstFragment : Fragment() {
val crashlytics = FirebaseCrashlytics.getInstance()
val performance = FirebasePerformance.getInstance()

private var _binding: FragmentFirstBinding? = null

Expand Down Expand Up @@ -64,6 +71,14 @@ class FirstFragment : Fragment() {
Thread.sleep(1_000)
}
}
binding.createTrace.setOnClickListener {
lifecycleScope.launch(Dispatchers.IO) {
val performanceTrace = performance.newTrace("test_trace")
performanceTrace.start()
delay(1000)
performanceTrace.stop()
}
}
binding.buttonForegroundProcess.setOnClickListener {
if (binding.buttonForegroundProcess.getText().startsWith("Start")) {
ForegroundService.startService(requireContext(), "Starting service at ${getDateText()}")
Expand All @@ -89,6 +104,7 @@ class FirstFragment : Fragment() {
intent.addFlags(FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
}
binding.processName.text = getProcessName()
}

override fun onResume() {
Expand All @@ -111,5 +127,9 @@ class FirstFragment : Fragment() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
SimpleDateFormat("HH:mm:ss", Locale.getDefault()).format(Date())
else "unknown"

fun getProcessName(): String =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) Application.getProcessName()
else "unknown"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
import android.os.Build
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.lifecycle.lifecycleScope
import com.google.firebase.perf.FirebasePerformance
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/** Second activity from the MainActivity that runs on a different process. */
class SecondActivity : BaseActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
Expand All @@ -38,12 +42,21 @@ class SecondActivity : BaseActivity() {
findViewById<Button>(R.id.second_crash_button).setOnClickListener {
throw IllegalStateException("SecondActivity has crashed")
}
findViewById<Button>(R.id.second_create_trace).setOnClickListener {
lifecycleScope.launch {
val performanceTrace = FirebasePerformance.getInstance().newTrace("test_trace")
performanceTrace.start()
delay(1000)
performanceTrace.stop()
}
}
findViewById<Button>(R.id.kill_background_processes).setOnClickListener {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
getSystemService(ActivityManager::class.java)
.killBackgroundProcesses("com.google.firebase.testing.sessions")
}
}
findViewById<TextView>(R.id.process_name_second).text = getProcessName()
}

override fun onResume() {
Expand Down
14 changes: 14 additions & 0 deletions firebase-sessions/test-app/src/main/res/layout/activity_second.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@
app:layout_constraintTop_toBottomOf="@id/prev_activity_button"
app:layout_constraintLeft_toLeftOf="parent" />

<Button
android:id="@+id/second_create_trace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/create_trace"
app:layout_constraintTop_toBottomOf="@id/second_crash_button"
app:layout_constraintLeft_toLeftOf="parent" />

<Button
android:id="@+id/kill_background_processes"
android:layout_width="wrap_content"
Expand All @@ -46,6 +54,12 @@
app:layout_constraintTop_toBottomOf="@id/second_crash_button"
app:layout_constraintLeft_toLeftOf="parent" />

<TextView
android:id="@+id/process_name_second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand Down
14 changes: 14 additions & 0 deletions firebase-sessions/test-app/src/main/res/layout/fragment_first.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/button_non_fatal" />

<Button
android:id="@+id/create_trace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/create_trace"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/button_anr" />

<Button
android:id="@+id/button_foreground_process"
android:layout_width="wrap_content"
Expand Down Expand Up @@ -79,6 +87,12 @@
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/start_splitscreen_same" />

<TextView
android:id="@+id/process_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand Down
1 change: 1 addition & 0 deletions firebase-sessions/test-app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<string name="crash_button_text">Crash!</string>
<string name="non_fatal_button_text">Non Fatal</string>
<string name="button_anr_text">ANR</string>
<string name="create_trace">Create New Trace: test_trace</string>
<string name="start_foreground_service_text">Start Foreground Service</string>
<string name="start_splitcreen_text">Start splitscreen - Different activity</string>
<string name="start_splitcreen_same_text">Start splitscreen - Same activity</string>
Expand Down
2 changes: 2 additions & 0 deletions firebase-sessions/test-app/test-app.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ dependencies {
implementation("androidx.navigation:navigation-fragment-ktx:2.4.1")
implementation("androidx.navigation:navigation-ui-ktx:2.4.1")
implementation("com.google.android.material:material:1.9.0")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.4.0")
implementation(libs.androidx.core)

androidTestImplementation("com.google.firebase:firebase-common:21.0.0")
Expand Down
Loading