Merge "Move hasHeadTracker to background thread in device details page" into main

This commit is contained in:
Haijie Hong
2023-12-26 06:37:09 +00:00
committed by Android (Google) Code Review
2 changed files with 32 additions and 5 deletions

View File

@@ -37,6 +37,9 @@ import com.android.settings.R;
import com.android.settings.overlay.FeatureFactory; import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.core.lifecycle.Lifecycle; import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.utils.ThreadUtils;
import java.util.concurrent.atomic.AtomicBoolean;
/** /**
* The controller of the Spatial audio setting in the bluetooth detail settings. * The controller of the Spatial audio setting in the bluetooth detail settings.
@@ -56,6 +59,8 @@ public class BluetoothDetailsSpatialAudioController extends BluetoothDetailsCont
@VisibleForTesting @VisibleForTesting
AudioDeviceAttributes mAudioDevice = null; AudioDeviceAttributes mAudioDevice = null;
AtomicBoolean mHasHeadTracker = new AtomicBoolean(false);
public BluetoothDetailsSpatialAudioController( public BluetoothDetailsSpatialAudioController(
Context context, Context context,
PreferenceFragmentCompat fragment, PreferenceFragmentCompat fragment,
@@ -77,7 +82,13 @@ public class BluetoothDetailsSpatialAudioController extends BluetoothDetailsCont
String key = switchPreference.getKey(); String key = switchPreference.getKey();
if (TextUtils.equals(key, KEY_SPATIAL_AUDIO)) { if (TextUtils.equals(key, KEY_SPATIAL_AUDIO)) {
updateSpatializerEnabled(switchPreference.isChecked()); updateSpatializerEnabled(switchPreference.isChecked());
refreshSpatialAudioEnabled(switchPreference); ThreadUtils.postOnBackgroundThread(
() -> {
mHasHeadTracker.set(
mAudioDevice != null && mSpatializer.hasHeadTracker(mAudioDevice));
mContext.getMainExecutor()
.execute(() -> refreshSpatialAudioEnabled(switchPreference));
});
return true; return true;
} else if (TextUtils.equals(key, KEY_HEAD_TRACKING)) { } else if (TextUtils.equals(key, KEY_HEAD_TRACKING)) {
updateSpatializerHeadTracking(switchPreference.isChecked()); updateSpatializerHeadTracking(switchPreference.isChecked());
@@ -124,7 +135,15 @@ public class BluetoothDetailsSpatialAudioController extends BluetoothDetailsCont
if (mAudioDevice == null) { if (mAudioDevice == null) {
getAvailableDevice(); getAvailableDevice();
} }
ThreadUtils.postOnBackgroundThread(
() -> {
mHasHeadTracker.set(
mAudioDevice != null && mSpatializer.hasHeadTracker(mAudioDevice));
mContext.getMainExecutor().execute(this::refreshUi);
});
}
private void refreshUi() {
TwoStatePreference spatialAudioPref = mProfilesContainer.findPreference(KEY_SPATIAL_AUDIO); TwoStatePreference spatialAudioPref = mProfilesContainer.findPreference(KEY_SPATIAL_AUDIO);
if (spatialAudioPref == null && mAudioDevice != null) { if (spatialAudioPref == null && mAudioDevice != null) {
spatialAudioPref = createSpatialAudioPreference(mProfilesContainer.getContext()); spatialAudioPref = createSpatialAudioPreference(mProfilesContainer.getContext());
@@ -145,7 +164,8 @@ public class BluetoothDetailsSpatialAudioController extends BluetoothDetailsCont
refreshSpatialAudioEnabled(spatialAudioPref); refreshSpatialAudioEnabled(spatialAudioPref);
} }
private void refreshSpatialAudioEnabled(TwoStatePreference spatialAudioPref) { private void refreshSpatialAudioEnabled(
TwoStatePreference spatialAudioPref) {
boolean isSpatialAudioOn = mSpatializer.getCompatibleAudioDevices().contains(mAudioDevice); boolean isSpatialAudioOn = mSpatializer.getCompatibleAudioDevices().contains(mAudioDevice);
Log.d(TAG, "refresh() isSpatialAudioOn : " + isSpatialAudioOn); Log.d(TAG, "refresh() isSpatialAudioOn : " + isSpatialAudioOn);
spatialAudioPref.setChecked(isSpatialAudioOn); spatialAudioPref.setChecked(isSpatialAudioOn);
@@ -160,9 +180,8 @@ public class BluetoothDetailsSpatialAudioController extends BluetoothDetailsCont
private void refreshHeadTracking(TwoStatePreference spatialAudioPref, private void refreshHeadTracking(TwoStatePreference spatialAudioPref,
TwoStatePreference headTrackingPref) { TwoStatePreference headTrackingPref) {
boolean isHeadTrackingAvailable = boolean isHeadTrackingAvailable = spatialAudioPref.isChecked() && mHasHeadTracker.get();
spatialAudioPref.isChecked() && mSpatializer.hasHeadTracker(mAudioDevice); Log.d(TAG, "refresh() has head tracker : " + mHasHeadTracker.get());
Log.d(TAG, "refresh() has head tracker : " + mSpatializer.hasHeadTracker(mAudioDevice));
headTrackingPref.setVisible(isHeadTrackingAvailable); headTrackingPref.setVisible(isHeadTrackingAvailable);
if (isHeadTrackingAvailable) { if (isHeadTrackingAvailable) {
headTrackingPref.setChecked(mSpatializer.isHeadTrackerEnabled(mAudioDevice)); headTrackingPref.setChecked(mSpatializer.isHeadTrackerEnabled(mAudioDevice));

View File

@@ -43,6 +43,7 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment; import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowLooper;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -120,6 +121,7 @@ public class BluetoothDetailsSpatialAudioControllerTest extends BluetoothDetails
when(mSpatializer.getCompatibleAudioDevices()).thenReturn(compatibleAudioDevices); when(mSpatializer.getCompatibleAudioDevices()).thenReturn(compatibleAudioDevices);
mController.refresh(); mController.refresh();
ShadowLooper.idleMainLooper();
assertThat(mSpatialAudioPref.isChecked()).isTrue(); assertThat(mSpatialAudioPref.isChecked()).isTrue();
} }
@@ -130,6 +132,7 @@ public class BluetoothDetailsSpatialAudioControllerTest extends BluetoothDetails
when(mSpatializer.getCompatibleAudioDevices()).thenReturn(compatibleAudioDevices); when(mSpatializer.getCompatibleAudioDevices()).thenReturn(compatibleAudioDevices);
mController.refresh(); mController.refresh();
ShadowLooper.idleMainLooper();
assertThat(mSpatialAudioPref.isChecked()).isFalse(); assertThat(mSpatialAudioPref.isChecked()).isFalse();
} }
@@ -142,6 +145,7 @@ public class BluetoothDetailsSpatialAudioControllerTest extends BluetoothDetails
when(mSpatializer.hasHeadTracker(mController.mAudioDevice)).thenReturn(true); when(mSpatializer.hasHeadTracker(mController.mAudioDevice)).thenReturn(true);
mController.refresh(); mController.refresh();
ShadowLooper.idleMainLooper();
assertThat(mHeadTrackingPref.isVisible()).isTrue(); assertThat(mHeadTrackingPref.isVisible()).isTrue();
} }
@@ -156,6 +160,7 @@ public class BluetoothDetailsSpatialAudioControllerTest extends BluetoothDetails
when(mSpatializer.hasHeadTracker(mController.mAudioDevice)).thenReturn(false); when(mSpatializer.hasHeadTracker(mController.mAudioDevice)).thenReturn(false);
mController.refresh(); mController.refresh();
ShadowLooper.idleMainLooper();
verify(mProfilesContainer).removePreference(mHeadTrackingPref); verify(mProfilesContainer).removePreference(mHeadTrackingPref);
} }
@@ -166,6 +171,7 @@ public class BluetoothDetailsSpatialAudioControllerTest extends BluetoothDetails
when(mSpatializer.getCompatibleAudioDevices()).thenReturn(compatibleAudioDevices); when(mSpatializer.getCompatibleAudioDevices()).thenReturn(compatibleAudioDevices);
mController.refresh(); mController.refresh();
ShadowLooper.idleMainLooper();
verify(mProfilesContainer).removePreference(mHeadTrackingPref); verify(mProfilesContainer).removePreference(mHeadTrackingPref);
} }
@@ -181,6 +187,7 @@ public class BluetoothDetailsSpatialAudioControllerTest extends BluetoothDetails
when(mSpatializer.isHeadTrackerEnabled(mController.mAudioDevice)).thenReturn(true); when(mSpatializer.isHeadTrackerEnabled(mController.mAudioDevice)).thenReturn(true);
mController.refresh(); mController.refresh();
ShadowLooper.idleMainLooper();
assertThat(mHeadTrackingPref.isChecked()).isTrue(); assertThat(mHeadTrackingPref.isChecked()).isTrue();
} }
@@ -196,6 +203,7 @@ public class BluetoothDetailsSpatialAudioControllerTest extends BluetoothDetails
when(mSpatializer.isHeadTrackerEnabled(mController.mAudioDevice)).thenReturn(false); when(mSpatializer.isHeadTrackerEnabled(mController.mAudioDevice)).thenReturn(false);
mController.refresh(); mController.refresh();
ShadowLooper.idleMainLooper();
assertThat(mHeadTrackingPref.isChecked()).isFalse(); assertThat(mHeadTrackingPref.isChecked()).isFalse();
} }