Merge "Fix deadlock in BaseDataProducer." into tm-qpr-dev
This commit is contained in:
@@ -38,7 +38,6 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -129,14 +128,13 @@ public final class DeviceStateManagerFoldingFeatureProducer
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onListenersChanged(
|
protected void onListenersChanged() {
|
||||||
@NonNull Set<Consumer<List<CommonFoldingFeature>>> callbacks) {
|
super.onListenersChanged();
|
||||||
super.onListenersChanged(callbacks);
|
if (hasListeners()) {
|
||||||
if (callbacks.isEmpty()) {
|
mRawFoldSupplier.addDataChangedCallback(this::notifyFoldingFeatureChange);
|
||||||
|
} else {
|
||||||
mCurrentDeviceState = INVALID_DEVICE_STATE;
|
mCurrentDeviceState = INVALID_DEVICE_STATE;
|
||||||
mRawFoldSupplier.removeDataChangedCallback(this::notifyFoldingFeatureChange);
|
mRawFoldSupplier.removeDataChangedCallback(this::notifyFoldingFeatureChange);
|
||||||
} else {
|
|
||||||
mRawFoldSupplier.addDataChangedCallback(this::notifyFoldingFeatureChange);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -31,7 +31,6 @@ import androidx.window.util.BaseDataProducer;
|
|||||||
import com.android.internal.R;
|
import com.android.internal.R;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -86,11 +85,11 @@ public final class RawFoldingFeatureProducer extends BaseDataProducer<String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onListenersChanged(Set<Consumer<String>> callbacks) {
|
protected void onListenersChanged() {
|
||||||
if (callbacks.isEmpty()) {
|
if (hasListeners()) {
|
||||||
unregisterObserversIfNeeded();
|
|
||||||
} else {
|
|
||||||
registerObserversIfNeeded();
|
registerObserversIfNeeded();
|
||||||
|
} else {
|
||||||
|
unregisterObserversIfNeeded();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -51,10 +51,10 @@ public abstract class BaseDataProducer<T> implements DataProducer<T>,
|
|||||||
public final void addDataChangedCallback(@NonNull Consumer<T> callback) {
|
public final void addDataChangedCallback(@NonNull Consumer<T> callback) {
|
||||||
synchronized (mLock) {
|
synchronized (mLock) {
|
||||||
mCallbacks.add(callback);
|
mCallbacks.add(callback);
|
||||||
Optional<T> currentData = getCurrentData();
|
|
||||||
currentData.ifPresent(callback);
|
|
||||||
onListenersChanged(mCallbacks);
|
|
||||||
}
|
}
|
||||||
|
Optional<T> currentData = getCurrentData();
|
||||||
|
currentData.ifPresent(callback);
|
||||||
|
onListenersChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -67,11 +67,22 @@ public abstract class BaseDataProducer<T> implements DataProducer<T>,
|
|||||||
public final void removeDataChangedCallback(@NonNull Consumer<T> callback) {
|
public final void removeDataChangedCallback(@NonNull Consumer<T> callback) {
|
||||||
synchronized (mLock) {
|
synchronized (mLock) {
|
||||||
mCallbacks.remove(callback);
|
mCallbacks.remove(callback);
|
||||||
onListenersChanged(mCallbacks);
|
}
|
||||||
|
onListenersChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns {@code true} if there are any registered callbacks {@code false} if there are no
|
||||||
|
* registered callbacks.
|
||||||
|
*/
|
||||||
|
// TODO(b/278132889) Improve the structure of BaseDataProdcuer while avoiding known issues.
|
||||||
|
public final boolean hasListeners() {
|
||||||
|
synchronized (mLock) {
|
||||||
|
return !mCallbacks.isEmpty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void onListenersChanged(Set<Consumer<T>> callbacks) {}
|
protected void onListenersChanged() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the current data if available and {@code Optional.empty()} otherwise.
|
* @return the current data if available and {@code Optional.empty()} otherwise.
|
||||||
|
|||||||
Reference in New Issue
Block a user