Refactor notifyScreenshotListeners method in WMS

Refactored notfiyScreenshotListeners method in WindowManagerService to
reduce unnecessary iteration over visible activities.

Bug: 274128914
Test: atest CtsWindowManagerDeviceTestCases:ActivityCaptureCallbackTests
Change-Id: I6ca97dba0dfd4674ab8fc6f2ad0762c7e30c1356
This commit is contained in:
Omar Eissa
2023-03-08 11:55:10 +00:00
parent 9397e3117e
commit d449468a34

View File

@@ -9528,30 +9528,6 @@ public class WindowManagerService extends IWindowManager.Stub
mSurfaceSyncGroupController.markSyncGroupReady(syncGroupToken);
}
private ArraySet<ActivityRecord> getVisibleActivityRecords(int displayId) {
ArraySet<ActivityRecord> result = new ArraySet<>();
synchronized (mGlobalLock) {
ArraySet<ComponentName> addedActivities = new ArraySet<>();
DisplayContent displayContent = mRoot.getDisplayContent(displayId);
if (displayContent != null) {
displayContent.forAllWindows(
(w) -> {
if (w.isVisible()
&& w.isDisplayed()
&& w.mActivityRecord != null
&& !addedActivities.contains(
w.mActivityRecord.mActivityComponent)
&& w.mActivityRecord.isVisible()
&& w.isVisibleNow()) {
addedActivities.add(w.mActivityRecord.mActivityComponent);
result.add(w.mActivityRecord);
}
},
true /* traverseTopToBottom */);
}
}
return result;
}
/**
* Must be called when a screenshot is taken via hardware chord.
@@ -9567,14 +9543,20 @@ public class WindowManagerService extends IWindowManager.Stub
throw new SecurityException("Requires STATUS_BAR_SERVICE permission");
}
synchronized (mGlobalLock) {
ArraySet<ComponentName> notifiedApps = new ArraySet<>();
ArraySet<ActivityRecord> visibleApps = getVisibleActivityRecords(displayId);
for (ActivityRecord ar : visibleApps) {
if (ar.isRegisteredForScreenCaptureCallback()) {
ar.reportScreenCaptured();
notifiedApps.add(ar.mActivityComponent);
}
final DisplayContent displayContent = mRoot.getDisplayContent(displayId);
if (displayContent == null) {
return new ArrayList<>();
}
ArraySet<ComponentName> notifiedApps = new ArraySet<>();
displayContent.forAllActivities(
(ar) -> {
if (!notifiedApps.contains(ar.mActivityComponent) && ar.isVisible()
&& ar.isRegisteredForScreenCaptureCallback()) {
ar.reportScreenCaptured();
notifiedApps.add(ar.mActivityComponent);
}
},
true /* traverseTopToBottom */);
return List.copyOf(notifiedApps);
}
}