Do not show a recording app more than once

Prevent audio recording disclosure bar from expanding several times for
revealing same application name multiple times per single indicator's
"on-screen" session.

Bug: 149461313
Test: make, flash, run `adb shell appops start
<any_package_name> 27` and similar 'appops stop'
consecutively, see indicator expanding not more than once per
single indicator's session on the screen.

Change-Id: I2ea44a1ac46135cca603129298bb5ff9241fea92
This commit is contained in:
Sergey Nikolaienkov
2020-03-05 07:27:58 +01:00
parent 40f27f9454
commit fa016dfcbf

View File

@@ -43,7 +43,6 @@ import com.android.systemui.R;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
@@ -98,8 +97,27 @@ class AudioRecordingDisclosureBar {
private TextView mTextView;
@State private int mState = STATE_NOT_SHOWN;
private final Set<String> mAudioRecordingApps = new HashSet<>();
private final Queue<String> mPendingNotifications = new LinkedList<>();
/**
* Set of the applications that currently are conducting audio recording.
*/
private final Set<String> mActiveAudioRecordingPackages = new ArraySet<>();
/**
* Set of applications that we've notified the user about since the indicator came up. Meaning
* that if an application is in this list then at some point since the indicator came up, it
* was expanded showing this application's title.
* Used not to notify the user about the same application again while the indicator is shown.
* We empty this set every time the indicator goes off the screen (we always call {@code
* mSessionNotifiedPackages.clear()} before calling {@link #hide()}).
*/
private final Set<String> mSessionNotifiedPackages = new ArraySet<>();
/**
* If an application starts recording while the TV indicator is neither in {@link
* #STATE_NOT_SHOWN} nor in {@link #STATE_MINIMIZED}, then we add the application's package
* name to the queue, from which we take packages names one by one to disclose the
* corresponding applications' titles to the user, whenever the indicator eventually comes to
* one of the two aforementioned states.
*/
private final Queue<String> mPendingNotificationPackages = new LinkedList<>();
AudioRecordingDisclosureBar(Context context) {
mContext = context;
@@ -116,10 +134,14 @@ class AudioRecordingDisclosureBar {
}
private void onStartedRecording(String packageName) {
if (!mAudioRecordingApps.add(packageName)) {
if (!mActiveAudioRecordingPackages.add(packageName)) {
// This app is already known to perform recording
return;
}
if (!mSessionNotifiedPackages.add(packageName)) {
// We've already notified user about this app, no need to do it again.
return;
}
switch (mState) {
case STATE_NOT_SHOWN:
@@ -137,13 +159,13 @@ class AudioRecordingDisclosureBar {
case STATE_MINIMIZING:
// Currently animating or expanded. Thus add to the pending notifications, and it
// will be picked up once the indicator comes to the STATE_MINIMIZED.
mPendingNotifications.add(packageName);
mPendingNotificationPackages.add(packageName);
break;
}
}
private void onDoneRecording(String packageName) {
if (!mAudioRecordingApps.remove(packageName)) {
if (!mActiveAudioRecordingPackages.remove(packageName)) {
// Was not marked as an active recorder, do nothing
return;
}
@@ -151,7 +173,8 @@ class AudioRecordingDisclosureBar {
// If not MINIMIZED, will check whether the indicator should be hidden when the indicator
// comes to the STATE_MINIMIZED eventually. If is in the STATE_MINIMIZED, but there are
// other active recorders - simply ignore.
if (mState == STATE_MINIMIZED && mAudioRecordingApps.isEmpty()) {
if (mState == STATE_MINIMIZED && mActiveAudioRecordingPackages.isEmpty()) {
mSessionNotifiedPackages.clear();
hide();
}
}
@@ -303,11 +326,12 @@ class AudioRecordingDisclosureBar {
private void onMinimized() {
mState = STATE_MINIMIZED;
if (!mPendingNotifications.isEmpty()) {
if (!mPendingNotificationPackages.isEmpty()) {
// There is a new application that started recording, tell the user about it.
expand(mPendingNotifications.poll());
} else if (mAudioRecordingApps.isEmpty()) {
// Nobody is recording anymore, remove the indicator.
expand(mPendingNotificationPackages.poll());
} else if (mActiveAudioRecordingPackages.isEmpty()) {
// Nobody is recording anymore, clear state and remove the indicator.
mSessionNotifiedPackages.clear();
hide();
}
}
@@ -326,6 +350,12 @@ class AudioRecordingDisclosureBar {
mBgRight = null;
mState = STATE_NOT_SHOWN;
// Check if anybody started recording while we were in STATE_DISAPPEARING
if (!mPendingNotificationPackages.isEmpty()) {
// There is a new application that started recording, tell the user about it.
show(mPendingNotificationPackages.poll());
}
}
private void startPulsatingAnimation() {