Skip to content

Commit 5550fcc

Browse files
committed
Refactor getActivityWithIgnoredClass to consolidate synchronized blocks
1 parent 91c415a commit 5550fcc

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

firebase-appdistribution/src/main/java/com/google/firebase/appdistribution/impl/FirebaseAppDistributionLifecycleNotifier.java

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -232,16 +232,14 @@ Task<Activity> getForegroundActivity() {
232232
<A extends Activity> Task<Activity> getForegroundActivity(@Nullable Class<A> classToIgnore) {
233233
synchronized (lock) {
234234
if (currentActivity != null) {
235-
Activity foregroundActivity =
236-
getActivityWithIgnoredClass(currentActivity, previousActivity, classToIgnore);
235+
Activity foregroundActivity = getCurrentActivityWithIgnoredClass(classToIgnore);
237236
if (Looper.myLooper() == Looper.getMainLooper()) {
238237
// We're already on the UI thread, so just complete the task with the activity
239238
return Tasks.forResult(foregroundActivity);
240239
} else {
241240
// Run in UI thread so that returned Task will be completed on the UI thread
242241
return runAsyncInTask(
243-
uiThreadExecutor,
244-
() -> getActivityWithIgnoredClass(currentActivity, previousActivity, classToIgnore));
242+
uiThreadExecutor, () -> getCurrentActivityWithIgnoredClass(classToIgnore));
245243
}
246244
}
247245
}
@@ -252,28 +250,22 @@ <A extends Activity> Task<Activity> getForegroundActivity(@Nullable Class<A> cla
252250
@Override
253251
public void onResumed(Activity activity) {
254252
// Since this method is run on the UI thread, the Task is completed on the UI thread
255-
task.setResult(
256-
getActivityWithIgnoredClass(activity, getPreviousActivity(), classToIgnore));
253+
task.setResult(getCurrentActivityWithIgnoredClass(classToIgnore));
257254
removeOnActivityResumedListener(this);
258255
}
259256
});
260257
return task.getTask();
261258
}
262259

263260
@Nullable
264-
private Activity getPreviousActivity() {
261+
private <A extends Activity> Activity getCurrentActivityWithIgnoredClass(
262+
@Nullable Class<A> classToIgnore) {
265263
synchronized (lock) {
266-
return previousActivity;
267-
}
268-
}
269-
270-
@Nullable
271-
private static <A extends Activity> Activity getActivityWithIgnoredClass(
272-
Activity activity, @Nullable Activity fallbackActivity, @Nullable Class<A> classToIgnore) {
273-
if (classToIgnore != null && classToIgnore.isInstance(activity)) {
274-
return fallbackActivity;
275-
} else {
276-
return activity;
264+
if (classToIgnore != null && classToIgnore.isInstance(currentActivity)) {
265+
return previousActivity;
266+
} else {
267+
return currentActivity;
268+
}
277269
}
278270
}
279271

0 commit comments

Comments
 (0)