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

Commit 6401efc9 authored by Alex Shabalin's avatar Alex Shabalin
Browse files

Stop hiding suggestion once `requestDeviceSuggestion` is called.

Before:
- Hide the device suggestion after showing the suggestion with
CONNECTION_FAILED state for 20 seconds.
- Show the device suggestion if available on receiving
`onSuggestedDeviceStateUpdated` callback.

After:
- Hide the device suggestion after showing the suggestion with
CONNECTION_FAILED state for 20 seconds.
- Show the device suggestion on making `requestDeviceSuggestion` call.

The former approach was error-prone because the
`onSuggestedDeviceStateUpdated` callback might never be called if the
suggested route remains unchanged.

Fix: 442626602
Test: atest SuggestedDeviceManagerTest
Flag: EXEMPT BUGFIX
Change-Id: I6e8f17cffe931f10f2292f1854fd6a71c8cb2719
parent 8262228a
Loading
Loading
Loading
Loading
+20 −1
Original line number Diff line number Diff line
@@ -64,6 +64,7 @@ class SuggestedDeviceManager(
  // [topSuggestion]. This is necessary to prevent hiding or changing the title of the suggested
  // device chip during connection attempts or when displaying error messages.
  @GuardedBy("lock") private var suggestedStateOverride: SuggestedDeviceState? = null
  @GuardedBy("lock") private var hideSuggestedDeviceState: Boolean = false

  init {
    if (useSuggestedDeviceConnectionManager()) {
@@ -79,7 +80,7 @@ class SuggestedDeviceManager(
      if (suggestedStateOverride?.connectionState == STATE_CONNECTING_FAILED) {
        // After the connection error, hide the suggestion chip until the new suggestion is
        // requested.
        topSuggestion = null
        hideSuggestedDeviceState = true
      }
      suggestedStateOverride = null
      updateSuggestedDeviceStateLocked(topSuggestion, mediaDevices)
@@ -149,6 +150,20 @@ class SuggestedDeviceManager(

  fun requestDeviceSuggestion() {
    localMediaManager.requestDeviceSuggestion()
    stopHidingSuggestedDeviceState()
  }

  private fun stopHidingSuggestedDeviceState() {
    var stateChanged = false
    synchronized(lock) {
      if (hideSuggestedDeviceState) {
        hideSuggestedDeviceState = false
        stateChanged = updateSuggestedDeviceStateLocked(topSuggestion, mediaDevices)
      }
    }
    if (stateChanged) {
      dispatchOnSuggestedDeviceUpdated()
    }
  }

  fun getSuggestedDevice(): SuggestedDeviceState? {
@@ -224,6 +239,10 @@ class SuggestedDeviceManager(
    newTopSuggestion: SuggestedDeviceInfo?,
    newMediaDevices: List<MediaDevice>,
  ): SuggestedDeviceState? {
    if (hideSuggestedDeviceState) {
      return null
    }

    if (newTopSuggestion == null) {
      return suggestedStateOverride ?: null
    }
+6 −1
Original line number Diff line number Diff line
@@ -498,9 +498,14 @@ class SuggestedDeviceManagerTest {
    deviceCallback.onDeviceListUpdate(listOf(mediaDevice1))
    verify(listener, never()).onSuggestedDeviceStateUpdated(any())

    // A new suggestion list makes the suggestedDeviceState restore.
    // A new suggestion list doesn't cause the suggestedDeviceState to be restored.
    clearInvocations(listener)
    deviceCallback.onDeviceSuggestionsUpdated(listOf(suggestedDeviceInfo1))
    verify(listener, never()).onSuggestedDeviceStateUpdated(any())

    // Requesting a new suggestion causes the suggestedDeviceState to be restored.
    clearInvocations(listener)
    mSuggestedDeviceManager.requestDeviceSuggestion()
    verify(listener)
      .onSuggestedDeviceStateUpdated(SuggestedDeviceState(suggestedDeviceInfo1, STATE_DISCONNECTED))
  }