Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 50a832ae authored by Derek Jedral's avatar Derek Jedral
Browse files

Debounce multiple rapid suggestion requests

When UMO becomes visible, different listeners that check the visibility
state will all notify local media manager. We should throttle the
visibility updates so routes don't get erroneously notified.

Test: local test
Bug: 416745583
Flag: com.android.media.flags.enable_suggested_device_api
Change-Id: Iba689f45adf59f14d2b5ca5b39e5d6e07bf8120c
parent 6be02c16
Loading
Loading
Loading
Loading
+16 −1
Original line number Diff line number Diff line
@@ -63,6 +63,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
public class LocalMediaManager implements BluetoothCallback {
    private static final String TAG = "LocalMediaManager";
    private static final int MAX_DISCONNECTED_DEVICE_NUM = 5;
    private static final int MIN_DURATION_BETWEEN_SUGGESTION_REQUESTS_MILLIS = 5000;

    @Retention(RetentionPolicy.SOURCE)
    @IntDef({MediaDeviceState.STATE_CONNECTED,
@@ -112,6 +113,8 @@ public class LocalMediaManager implements BluetoothCallback {
    @VisibleForTesting
    BluetoothAdapter mBluetoothAdapter;

    private long mLastSuggestionRequestTime = 0L;

    /**
     * Register to start receiving callbacks for MediaDevice events.
     */
@@ -246,7 +249,19 @@ public class LocalMediaManager implements BluetoothCallback {

    /** Requests a suggestion from other routers. */
    public void requestDeviceSuggestion() {
        // Debounce multiple requests in a short duration
        long currentRequestTime = System.currentTimeMillis();
        if (currentRequestTime - mLastSuggestionRequestTime
                > MIN_DURATION_BETWEEN_SUGGESTION_REQUESTS_MILLIS) {
            Log.d(TAG, "requesting device suggestion");
            mLastSuggestionRequestTime = currentRequestTime;
            mInfoMediaManager.requestDeviceSuggestion();
        } else {
            Log.d(
                    TAG,
                    "requestDeviceSuggestion() ignored due to difference between requests: "
                            + (currentRequestTime - mLastSuggestionRequestTime));
        }
    }

    @Nullable