Skip to content

Refactor usage of ProcessName in Fireperf. #6963

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

Merged
merged 5 commits into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package com.google.firebase.perf.metrics;

import static com.google.firebase.perf.util.AppProcessesProvider.getAppProcesses;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
Expand Down Expand Up @@ -509,17 +511,10 @@ public static boolean isAnyAppProcessInForeground(Context appContext) {
if (activityManager == null) {
return true;
}
List<ActivityManager.RunningAppProcessInfo> appProcesses =
activityManager.getRunningAppProcesses();
if (appProcesses != null) {
String appProcessName = appContext.getPackageName();
String allowedAppProcessNamePrefix = appProcessName + ":";
List<ActivityManager.RunningAppProcessInfo> appProcesses = getAppProcesses(appContext);
if (!appProcesses.isEmpty()) {
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
continue;
}
if (appProcess.processName.equals(appProcessName)
|| appProcess.processName.startsWith(allowedAppProcessNamePrefix)) {
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
boolean isAppInForeground = true;

// For the case when the app is in foreground and the device transitions to sleep mode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package com.google.firebase.perf.transport;

import static com.google.firebase.sessions.ProcessDetailsProvider.getProcessDetailsProvider;
import static com.google.firebase.perf.util.AppProcessesProvider.getProcessName;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.MINUTES;

Expand Down Expand Up @@ -231,8 +231,7 @@ private void finishInitialization() {
applicationInfoBuilder = ApplicationInfo.newBuilder();
applicationInfoBuilder
.setGoogleAppId(firebaseApp.getOptions().getApplicationId())
.setProcessName(
getProcessDetailsProvider().getCurrentProcessDetails(appContext).getProcessName())
.setProcessName(getProcessName(appContext))
.setAndroidAppInfo(
AndroidApplicationInfo.newBuilder()
.setPackageName(packageName)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2025 Google LLC
//
// 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 com.google.firebase.perf.util

import android.app.ActivityManager
import android.app.Application
import android.content.Context
import android.os.Build
import android.os.Process
import com.google.android.gms.common.util.ProcessUtils

/**
* A singleton that contains helper functions to get relevant process details. TODO(b/418041083):
* Explore using a common utility. See [com.google.firebase.sessions.ProcessDetailsProvider].
*/
object AppProcessesProvider {
/** Gets the details for all of this app's running processes. */
@JvmStatic
fun getAppProcesses(context: Context): List<ActivityManager.RunningAppProcessInfo> {
val appUid = context.applicationInfo.uid
val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as? ActivityManager
val runningAppProcesses = activityManager?.runningAppProcesses ?: listOf()

return runningAppProcesses.filterNotNull().filter {
// Only collect process info for this app's processes.
it.uid == appUid
}
}

/**
* Gets this app's current process name.
*
* If the current process details are not found for whatever reason, returns an empty string.
*/
@JvmStatic
fun getProcessName(context: Context): String {
val pid = Process.myPid()
return getAppProcesses(context).find { it.pid == pid }?.processName ?: getProcessName()
}

/** Gets the app's current process name. If it could not be found, return the default. */
private fun getProcessName(default: String = ""): String {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.TIRAMISU) {
return Process.myProcessName()
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
Application.getProcessName()?.let {
return it
}
}

// GMS core has different ways to get the process name on old api levels.
ProcessUtils.getMyProcessName()?.let {
return it
}

// Returns default if nothing works.
return default
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ import com.google.android.gms.common.util.ProcessUtils
*
* @hide
*/
object ProcessDetailsProvider {
@JvmStatic
fun getProcessDetailsProvider(): ProcessDetailsProvider {
return this
}
internal object ProcessDetailsProvider {
/** Gets the details for all of this app's running processes. */
fun getAppProcessDetails(context: Context): List<ProcessDetails> {
val appUid = context.applicationInfo.uid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ internal data class DataCollectionStatus(
)

/** Container for information about the process */
data class ProcessDetails(
internal data class ProcessDetails(
val processName: String,
val pid: Int,
val importance: Int,
Expand Down
Loading