Skip to content

Fireperf fragments: lifecycle callbacks #3565

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 4 commits into from
Mar 21, 2022
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 @@ -22,6 +22,7 @@
import android.util.SparseIntArray;
import androidx.annotation.NonNull;
import androidx.core.app.FrameMetricsAggregator;
import androidx.fragment.app.FragmentActivity;
import com.google.android.gms.common.util.VisibleForTesting;
import com.google.firebase.perf.config.ConfigResolver;
import com.google.firebase.perf.logging.AndroidLogger;
Expand Down Expand Up @@ -140,7 +141,19 @@ public void incrementTsnsCount(int value) {
}

@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {}
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
if (isScreenTraceSupported(activity) && configResolver.isPerformanceMonitoringEnabled()) {
if (activity instanceof FragmentActivity) {
FragmentActivity fragmentActivity = (FragmentActivity) activity;
fragmentActivity
.getSupportFragmentManager()
.registerFragmentLifecycleCallbacks(
new FragmentStateMonitor(
fragmentActivity, clock, transportManager, this, frameMetricsAggregator),
true);
}
}
}

@Override
public void onActivityDestroyed(Activity activity) {}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2022 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.application;

import androidx.annotation.NonNull;
import androidx.core.app.FrameMetricsAggregator;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import com.google.firebase.perf.logging.AndroidLogger;
import com.google.firebase.perf.transport.TransportManager;
import com.google.firebase.perf.util.Clock;
import com.google.firebase.perf.util.Constants;

public class FragmentStateMonitor extends FragmentManager.FragmentLifecycleCallbacks {
private static final AndroidLogger logger = AndroidLogger.getInstance();
private final FragmentActivity activity;
private final Clock clock;
private final TransportManager transportManager;
private final AppStateMonitor appStateMonitor;
private final FrameMetricsAggregator frameMetricsAggregator;

public FragmentStateMonitor(
FragmentActivity activity,
Clock clock,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need a clock instance here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because new Trace(...) needs a clock, for making a screen trace

TransportManager transportManager,
AppStateMonitor appStateMonitor,
FrameMetricsAggregator fma) {
this.activity = activity;
this.clock = clock;
this.transportManager = transportManager;
this.appStateMonitor = appStateMonitor;
this.frameMetricsAggregator = fma;
}

/**
* Fragment screen trace name is prefix "_st_" concatenates with Fragment's class name.
*
* @param fragment fragment object.
* @return Fragment screen trace name.
*/
public static String getFragmentScreenTraceName(Fragment fragment) {
return Constants.SCREEN_TRACE_PREFIX + fragment.getClass().getSimpleName();
}

@Override
public void onFragmentResumed(@NonNull FragmentManager fm, @NonNull Fragment f) {
super.onFragmentResumed(fm, f);
// Start Fragment screen trace
logger.debug("FragmentMonitor %s.onFragmentResumed", f.getClass().getSimpleName());
}

@Override
public void onFragmentPaused(@NonNull FragmentManager fm, @NonNull Fragment f) {
super.onFragmentPaused(fm, f);
// Stop Fragment screen trace
logger.debug("FragmentMonitor %s.onFragmentPaused ", f.getClass().getSimpleName());
}
}