diff --git a/src/com/android/settings/notification/DockAudioMediaPreferenceController.java b/src/com/android/settings/notification/DockAudioMediaPreferenceController.java index d9367d58ab2..71704341566 100644 --- a/src/com/android/settings/notification/DockAudioMediaPreferenceController.java +++ b/src/com/android/settings/notification/DockAudioMediaPreferenceController.java @@ -19,6 +19,8 @@ package com.android.settings.notification; import static com.android.settings.notification.SettingPref.TYPE_GLOBAL; import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; import android.content.res.Resources; import android.provider.Settings.Global; @@ -41,7 +43,7 @@ public class DockAudioMediaPreferenceController extends SettingPrefController { DEFAULT_DOCK_AUDIO_MEDIA, DOCK_AUDIO_MEDIA_DISABLED, DOCK_AUDIO_MEDIA_ENABLED) { @Override public boolean isApplicable(Context context) { - return context.getResources().getBoolean( + return isLeDesk() && context.getResources().getBoolean( com.android.settings.R.bool.has_dock_settings); } @@ -60,4 +62,18 @@ public class DockAudioMediaPreferenceController extends SettingPrefController { } }; } + + /** + * Checks the state of docking type + * @return true if it is low-end dock types + */ + private boolean isLeDesk() { + IntentFilter intentFilter = new IntentFilter(Intent.ACTION_DOCK_EVENT); + Intent dockStatus = mContext.registerReceiver(null, intentFilter); + if (dockStatus == null) { + return false; + } + int dockState = dockStatus.getIntExtra(Intent.EXTRA_DOCK_STATE, -1); + return dockState == Intent.EXTRA_DOCK_STATE_LE_DESK; + } } diff --git a/tests/robotests/src/com/android/settings/notification/DockAudioMediaPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/DockAudioMediaPreferenceControllerTest.java index d237b271f57..5e4efd007c7 100644 --- a/tests/robotests/src/com/android/settings/notification/DockAudioMediaPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/notification/DockAudioMediaPreferenceControllerTest.java @@ -19,14 +19,19 @@ package com.android.settings.notification; import static com.google.common.truth.Truth.assertThat; import static org.mockito.Answers.RETURNS_DEEP_STUBS; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.nullable; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import android.app.admin.DevicePolicyManager; +import android.content.BroadcastReceiver; import android.content.ContentResolver; import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; import android.provider.Settings.Global; import androidx.fragment.app.FragmentActivity; @@ -72,6 +77,7 @@ public class DockAudioMediaPreferenceControllerTest { mController = new DockAudioMediaPreferenceController(mContext, mSetting, null); when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); doReturn(mScreen).when(mSetting).getPreferenceScreen(); + fakeDockState(Intent.EXTRA_DOCK_STATE_LE_DESK); } @Test @@ -90,6 +96,34 @@ public class DockAudioMediaPreferenceControllerTest { assertThat(mController.isAvailable()).isFalse(); } + @Test + public void isAvailable_undocked_shouldReturnFalse() { + when(mContext.registerReceiver(nullable(BroadcastReceiver.class), + any(IntentFilter.class))).thenReturn(null); + when(mContext.getResources().getBoolean(com.android.settings.R.bool.has_dock_settings)) + .thenReturn(true); + + assertThat(mController.isAvailable()).isFalse(); + } + + @Test + public void isAvailable_highEndDock_shouldReturnFalse() { + fakeDockState(Intent.EXTRA_DOCK_STATE_HE_DESK); + when(mContext.getResources().getBoolean(com.android.settings.R.bool.has_dock_settings)) + .thenReturn(true); + + assertThat(mController.isAvailable()).isFalse(); + } + + @Test + public void isAvailable_lowEndDock_shouldReturnTrue() { + fakeDockState(Intent.EXTRA_DOCK_STATE_LE_DESK); + when(mContext.getResources().getBoolean(com.android.settings.R.bool.has_dock_settings)) + .thenReturn(true); + + assertThat(mController.isAvailable()).isTrue(); + } + @Test public void displayPreference_dockAudioDisabled_shouldSelectFirstItem() { Global.putInt(mContentResolver, Global.DOCK_AUDIO_MEDIA_ENABLED, 0); @@ -127,4 +161,11 @@ public class DockAudioMediaPreferenceControllerTest { assertThat(Global.getInt(mContentResolver, Global.DOCK_AUDIO_MEDIA_ENABLED, 0)) .isEqualTo(1); } + + private void fakeDockState(int dockState) { + Intent intent = new Intent(Intent.ACTION_DOCK_EVENT); + intent.putExtra(Intent.EXTRA_DOCK_STATE, dockState); + when(mContext.registerReceiver(nullable(BroadcastReceiver.class), + any(IntentFilter.class))).thenReturn(intent); + } }