Camera: Address device state listener delay
For the long running process with the frequent camera characteristic acquiring, the size of mDeviceStateListeners could increase significantly. This could cause a delay when removeAll() is invoked from handleStateChange(). This change will address this by removing old listeners when we add a new one. Bug: 288070122 Test: Manual test with a foldable device with frequent camera characteristic acquiring Change-Id: I84c807f00c876734df06d244675e39e9cfc868a2
This commit is contained in:
committed by
Emilian Peev
parent
4694c51dd5
commit
7ebffbe2c0
@@ -67,6 +67,7 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.Executor;
|
||||
@@ -178,22 +179,20 @@ public final class CameraManager {
|
||||
boolean folded = ArrayUtils.contains(mFoldedDeviceStates, state);
|
||||
|
||||
mFoldedDeviceState = folded;
|
||||
ArrayList<WeakReference<DeviceStateListener>> invalidListeners = new ArrayList<>();
|
||||
for (WeakReference<DeviceStateListener> listener : mDeviceStateListeners) {
|
||||
DeviceStateListener callback = listener.get();
|
||||
Iterator<WeakReference<DeviceStateListener>> it = mDeviceStateListeners.iterator();
|
||||
while(it.hasNext()) {
|
||||
DeviceStateListener callback = it.next().get();
|
||||
if (callback != null) {
|
||||
callback.onDeviceStateChanged(folded);
|
||||
} else {
|
||||
invalidListeners.add(listener);
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
if (!invalidListeners.isEmpty()) {
|
||||
mDeviceStateListeners.removeAll(invalidListeners);
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void addDeviceStateListener(DeviceStateListener listener) {
|
||||
listener.onDeviceStateChanged(mFoldedDeviceState);
|
||||
mDeviceStateListeners.removeIf(l -> l.get() == null);
|
||||
mDeviceStateListeners.add(new WeakReference<>(listener));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user