Skip to content

Updating variable names from the source classes to follow Java's code convention (b/177441811) #2545

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 6 commits into from
Mar 26, 2021
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 @@ -180,15 +180,15 @@ public static FirebasePerformance getInstance() {
TransportManager.getInstance()
.initialize(firebaseApp, firebaseInstallationsApi, transportFactoryProvider);

Context context = firebaseApp.getApplicationContext();
Context appContext = firebaseApp.getApplicationContext();
// TODO(b/110178816): Explore moving off of main thread.
mMetadataBundle = extractMetadata(context);
mMetadataBundle = extractMetadata(appContext);

remoteConfigManager.setFirebaseRemoteConfigProvider(firebaseRemoteConfigProvider);
this.configResolver = configResolver;
this.configResolver.setMetadataBundle(mMetadataBundle);
this.configResolver.setApplicationContext(context);
gaugeManager.setApplicationContext(context);
this.configResolver.setApplicationContext(appContext);
gaugeManager.setApplicationContext(appContext);

mPerformanceCollectionForceEnabledState = configResolver.getIsPerformanceCollectionEnabled();
}
Expand Down Expand Up @@ -444,16 +444,16 @@ public HttpMetric newHttpMetric(@NonNull URL url, @NonNull @HttpMethod String ht
/**
* Extracts the metadata bundle from the ApplicationContext.
*
* @param context The ApplicationContext.
* @param appContext The ApplicationContext.
* @return A shallow copy of the bundle containing the metadata extracted from the context.
*/
private static ImmutableBundle extractMetadata(Context context) {
private static ImmutableBundle extractMetadata(Context appContext) {
Bundle bundle = null;
try {
ApplicationInfo ai =
context
appContext
.getPackageManager()
.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
.getApplicationInfo(appContext.getPackageName(), PackageManager.GET_META_DATA);

bundle = ai.metaData;
} catch (NameNotFoundException | NullPointerException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,74 +55,76 @@
public class AppStateMonitor implements ActivityLifecycleCallbacks {

private static final AndroidLogger logger = AndroidLogger.getInstance();
private static final String FRAME_METRICS_AGGREGATOR_CLASSNAME =
"androidx.core.app.FrameMetricsAggregator";

private static volatile AppStateMonitor sInstance;
private static volatile AppStateMonitor instance;

public static AppStateMonitor getInstance() {
if (sInstance == null) {
synchronized (AppStateMonitor.class) {
if (sInstance == null) {
sInstance = new AppStateMonitor(TransportManager.getInstance(), new Clock());
}
}
}
return sInstance;
}
private final WeakHashMap<Activity, Boolean> activityToResumedMap = new WeakHashMap<>();
private final WeakHashMap<Activity, Trace> activityToScreenTraceMap = new WeakHashMap<>();
private final Map<String, Long> metricToCountMap = new HashMap<>();
private final Set<WeakReference<AppStateCallback>> appStateSubscribers = new HashSet<>();
private Set<AppColdStartCallback> appColdStartSubscribers = new HashSet<>();

private static final String FRAME_METRICS_AGGREGATOR_CLASSNAME =
"androidx.core.app.FrameMetricsAggregator";
private boolean mRegistered = false;
private final TransportManager transportManager;
private ConfigResolver mConfigResolver;
private final Clock mClock;
private boolean mIsColdStart = true;
private final WeakHashMap<Activity, Boolean> mResumed = new WeakHashMap<>();
private Timer mStopTime; // The time app goes to background
private Timer mResumeTime; // The time app comes to foreground
private final Map<String, Long> mMetrics = new HashMap<>();
/* Count for TRACE_STARTED_NOT_STOPPED */
private AtomicInteger mTsnsCount = new AtomicInteger(0);
private final AtomicInteger tsnsCount = new AtomicInteger(0);

private ApplicationProcessState mCurrentState = ApplicationProcessState.BACKGROUND;
private final TransportManager transportManager;
private final ConfigResolver configResolver;
private final Clock clock;

private FrameMetricsAggregator frameMetricsAggregator;

private Timer resumeTime; // The time app comes to foreground
private Timer stopTime; // The time app goes to background

private Set<WeakReference<AppStateCallback>> appStateSubscribers =
new HashSet<WeakReference<AppStateCallback>>();
private Set<AppColdStartCallback> appColdStartSubscribers = new HashSet<AppColdStartCallback>();
private ApplicationProcessState currentAppState = ApplicationProcessState.BACKGROUND;

private boolean isRegisteredForLifecycleCallbacks = false;
private boolean isColdStart = true;
private boolean hasFrameMetricsAggregator = false;
private FrameMetricsAggregator mFrameMetricsAggregator;
private final WeakHashMap<Activity, Trace> mActivity2ScreenTrace = new WeakHashMap<>();

public static AppStateMonitor getInstance() {
if (instance == null) {
synchronized (AppStateMonitor.class) {
if (instance == null) {
instance = new AppStateMonitor(TransportManager.getInstance(), new Clock());
}
}
}
return instance;
}

AppStateMonitor(TransportManager transportManager, Clock clock) {
this.transportManager = transportManager;
mClock = clock;
mConfigResolver = ConfigResolver.getInstance();
this.clock = clock;
configResolver = ConfigResolver.getInstance();
hasFrameMetricsAggregator = hasFrameMetricsAggregatorClass();
if (hasFrameMetricsAggregator) {
mFrameMetricsAggregator = new FrameMetricsAggregator();
frameMetricsAggregator = new FrameMetricsAggregator();
}
}

public synchronized void registerActivityLifecycleCallbacks(Context context) {
// Make sure the callback is registered only once.
if (mRegistered) {
if (isRegisteredForLifecycleCallbacks) {
return;
}
Context appContext = context.getApplicationContext();
if (appContext instanceof Application) {
((Application) appContext).registerActivityLifecycleCallbacks(this);
mRegistered = true;
isRegisteredForLifecycleCallbacks = true;
}
}

public synchronized void unregisterActivityLifecycleCallbacks(Context context) {
if (!mRegistered) {
if (!isRegisteredForLifecycleCallbacks) {
return;
}
Context appContext = context.getApplicationContext();
if (appContext instanceof Application) {
((Application) appContext).unregisterActivityLifecycleCallbacks(this);
mRegistered = false;
isRegisteredForLifecycleCallbacks = false;
}
}

Expand All @@ -132,20 +134,20 @@ public void incrementCount(@NonNull String name, long value) {
// This method is called by RateLimiter.java when a log exceeds rate limit and to be dropped
// It can be on any thread. sendSessionLog() method is called in callback methods from main UI
// thread, thus we need synchronized access on mMetrics.
synchronized (mMetrics) {
Long v = mMetrics.get(name);
synchronized (metricToCountMap) {
Long v = metricToCountMap.get(name);
if (v == null) {
mMetrics.put(name, value);
metricToCountMap.put(name, value);
} else {
mMetrics.put(name, v + value);
metricToCountMap.put(name, v + value);
}
}
}

/** @hide */
/** @hide */
public void incrementTsnsCount(int value) {
mTsnsCount.addAndGet(value);
tsnsCount.addAndGet(value);
}

/** @hide */
Expand All @@ -162,13 +164,13 @@ public void onActivityDestroyed(Activity activity) {}
/** @hide */
@Override
public synchronized void onActivityStarted(Activity activity) {
if (isScreenTraceSupported(activity) && mConfigResolver.isPerformanceMonitoringEnabled()) {
if (isScreenTraceSupported(activity) && configResolver.isPerformanceMonitoringEnabled()) {
// Starts recording frame metrics for this activity.
mFrameMetricsAggregator.add(activity);
frameMetricsAggregator.add(activity);
// Start the Trace
Trace screenTrace = new Trace(getScreenTraceName(activity), transportManager, mClock, this);
Trace screenTrace = new Trace(getScreenTraceName(activity), transportManager, clock, this);
screenTrace.start();
mActivity2ScreenTrace.put(activity, screenTrace);
activityToScreenTraceMap.put(activity, screenTrace);
}
}

Expand All @@ -181,14 +183,13 @@ public synchronized void onActivityStopped(Activity activity) {
}

// Last activity has its onActivityStopped called, the app goes to background.
if (mResumed.containsKey(activity)) {
mResumed.remove(activity);
if (mResumed.isEmpty()) {
if (activityToResumedMap.containsKey(activity)) {
activityToResumedMap.remove(activity);
if (activityToResumedMap.isEmpty()) {
// no more activity in foreground, app goes to background.
mStopTime = mClock.getTime();
stopTime = clock.getTime();
updateAppState(ApplicationProcessState.BACKGROUND);
sendSessionLog(
Constants.TraceNames.FOREGROUND_TRACE_NAME.toString(), mResumeTime, mStopTime);
sendSessionLog(Constants.TraceNames.FOREGROUND_TRACE_NAME.toString(), resumeTime, stopTime);
}
}
}
Expand All @@ -201,29 +202,28 @@ public synchronized void onActivityResumed(Activity activity) {
// 1. At app startup, first activity comes to foreground.
// 2. app switch from background to foreground.
// 3. app already in foreground, current activity is replaced by another activity.
if (mResumed.isEmpty()) {
if (activityToResumedMap.isEmpty()) {
// The first resumed activity means app comes to foreground.
mResumeTime = mClock.getTime();
mResumed.put(activity, true);
resumeTime = clock.getTime();
activityToResumedMap.put(activity, true);
updateAppState(ApplicationProcessState.FOREGROUND);
if (mIsColdStart) {
if (isColdStart) {
// case 1: app startup.
sendAppColdStartUpdate();
mIsColdStart = false;
isColdStart = false;
} else {
// case 2: app switch from background to foreground.
sendSessionLog(
Constants.TraceNames.BACKGROUND_TRACE_NAME.toString(), mStopTime, mResumeTime);
sendSessionLog(Constants.TraceNames.BACKGROUND_TRACE_NAME.toString(), stopTime, resumeTime);
}
} else {
// case 3: app already in foreground, current activity is replaced by another activity.
mResumed.put(activity, true);
activityToResumedMap.put(activity, true);
}
}

/** Returns if this is the cold start of the app. */
public boolean isColdStart() {
return mIsColdStart;
return isColdStart;
}

/**
Expand All @@ -232,7 +232,7 @@ public boolean isColdStart() {
*/
/** @hide */
public ApplicationProcessState getAppState() {
return mCurrentState;
return currentAppState;
}

/**
Expand Down Expand Up @@ -276,13 +276,13 @@ public void registerForAppColdStart(AppColdStartCallback subscriber) {

/** Send update state update to registered subscribers. */
private void updateAppState(ApplicationProcessState newState) {
mCurrentState = newState;
currentAppState = newState;
synchronized (appStateSubscribers) {
for (Iterator<WeakReference<AppStateCallback>> i = appStateSubscribers.iterator();
i.hasNext(); ) {
AppStateCallback callback = i.next().get();
if (callback != null) {
callback.onUpdateAppState(mCurrentState);
callback.onUpdateAppState(currentAppState);
} else {
// The object pointing by WeakReference has already been garbage collected.
// Remove it from the Set.
Expand Down Expand Up @@ -310,7 +310,7 @@ private void sendAppColdStartUpdate() {
* @return true if app is in foreground, false if in background.
*/
public boolean isForeground() {
return mCurrentState == ApplicationProcessState.FOREGROUND;
return currentAppState == ApplicationProcessState.FOREGROUND;
}

@Override
Expand All @@ -325,20 +325,20 @@ public void onActivityPaused(Activity activity) {}
* @param activity activity object.
*/
private void sendScreenTrace(Activity activity) {
if (!mActivity2ScreenTrace.containsKey(activity)) {
if (!activityToScreenTraceMap.containsKey(activity)) {
return;
}
Trace screenTrace = mActivity2ScreenTrace.get(activity);
Trace screenTrace = activityToScreenTraceMap.get(activity);
if (screenTrace == null) {
return;
}
mActivity2ScreenTrace.remove(activity);
activityToScreenTraceMap.remove(activity);

int totalFrames = 0;
int slowFrames = 0;
int frozenFrames = 0;
// Stops recording metrics for this Activity and returns the currently-collected metrics
SparseIntArray[] arr = mFrameMetricsAggregator.remove(activity);
SparseIntArray[] arr = frameMetricsAggregator.remove(activity);
if (arr != null) {
SparseIntArray frameTimes = arr[FrameMetricsAggregator.TOTAL_INDEX];
if (frameTimes != null) {
Expand Down Expand Up @@ -394,7 +394,7 @@ private void sendScreenTrace(Activity activity) {
* @param endTime session trace end time.
*/
private void sendSessionLog(String name, Timer startTime, Timer endTime) {
if (!mConfigResolver.isPerformanceMonitoringEnabled()) {
if (!configResolver.isPerformanceMonitoringEnabled()) {
return;
}
// TODO(b/117776450): We should also capture changes in the Session ID.
Expand All @@ -405,15 +405,15 @@ private void sendSessionLog(String name, Timer startTime, Timer endTime) {
.setDurationUs(startTime.getDurationMicros(endTime))
.addPerfSessions(SessionManager.getInstance().perfSession().build());
// Atomically get mTsnsCount and set it to zero.
int tsnsCount = mTsnsCount.getAndSet(0);
synchronized (mMetrics) {
metric.putAllCounters(mMetrics);
int tsnsCount = this.tsnsCount.getAndSet(0);
synchronized (metricToCountMap) {
metric.putAllCounters(metricToCountMap);
if (tsnsCount != 0) {
metric.putCounters(CounterNames.TRACE_STARTED_NOT_STOPPED.toString(), tsnsCount);
}

// reset metrics.
mMetrics.clear();
metricToCountMap.clear();
}
// The Foreground and Background trace marks the transition between the two states,
// so we always specify the state to be ApplicationProcessState.FOREGROUND_BACKGROUND.
Expand Down Expand Up @@ -487,26 +487,26 @@ public static String getScreenTraceName(Activity activity) {

@VisibleForTesting
WeakHashMap<Activity, Boolean> getResumed() {
return mResumed;
return activityToResumedMap;
}

@VisibleForTesting
WeakHashMap<Activity, Trace> getActivity2ScreenTrace() {
return mActivity2ScreenTrace;
return activityToScreenTraceMap;
}

@VisibleForTesting
Timer getPauseTime() {
return mStopTime;
return stopTime;
}

@VisibleForTesting
Timer getResumeTime() {
return mResumeTime;
return resumeTime;
}

@VisibleForTesting
public void setIsColdStart(boolean isColdStart) {
mIsColdStart = isColdStart;
this.isColdStart = isColdStart;
}
}
Loading