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

Commit a860bf32 authored by Mill Chen's avatar Mill Chen
Browse files

Hide Dock speaker plays from sound pages

In order to ensure the setting only works on the analog docks, adding
the docking state checks.

Bug: 270492232
Test: manual test
1) Connect to digital dock
2) Check the sound page and the dock speaker plays shouldn't be
   displayed

Change-Id: I1dc1e0aac8b9067ec8f5f1defc721294caf2949e
parent 541a7153
Loading
Loading
Loading
Loading
+17 −1
Original line number Diff line number Diff line
@@ -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;
    }
}
+41 −0
Original line number Diff line number Diff line
@@ -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);
    }
}