From eb905ea4e2b8edbb746e4c43cc24e415edd2cdc9 Mon Sep 17 00:00:00 2001 From: jeffreyhuang Date: Wed, 11 Oct 2017 17:21:50 -0700 Subject: [PATCH] Refactor A2dpStore to be a non-static object - Updated all methods to be non-static - Introduced createCodecConfig to simplify controller code Bug: 34203528 Test: make RunSettingsRoboTests -j40 Change-Id: Ib923033645668fd7dd5529bdc9f5e4415023e11c --- .../development/BluetoothA2dpConfigStore.java | 80 +++++++++++++ .../development/BluetoothA2dpSharedStore.java | 108 ------------------ ...thAudioSampleRatePreferenceController.java | 39 +------ .../DevelopmentSettingsDashboardFragment.java | 3 +- ...dioSampleRatePreferenceControllerTest.java | 11 +- 5 files changed, 93 insertions(+), 148 deletions(-) create mode 100644 src/com/android/settings/development/BluetoothA2dpConfigStore.java delete mode 100644 src/com/android/settings/development/BluetoothA2dpSharedStore.java diff --git a/src/com/android/settings/development/BluetoothA2dpConfigStore.java b/src/com/android/settings/development/BluetoothA2dpConfigStore.java new file mode 100644 index 00000000000..edf74e31aa9 --- /dev/null +++ b/src/com/android/settings/development/BluetoothA2dpConfigStore.java @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.development; + +import android.bluetooth.BluetoothCodecConfig; + +/** + * Utility class for storing current Bluetooth A2DP profile values + */ +public class BluetoothA2dpConfigStore { + + // init default values + private int mCodecType = BluetoothCodecConfig.SOURCE_CODEC_TYPE_INVALID; + private int mCodecPriority = BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT; + private int mSampleRate = BluetoothCodecConfig.SAMPLE_RATE_NONE; + private int mBitsPerSample = BluetoothCodecConfig.BITS_PER_SAMPLE_NONE; + private int mChannelMode = BluetoothCodecConfig.CHANNEL_MODE_NONE; + private long mCodecSpecific1Value; + private long mCodecSpecific2Value; + private long mCodecSpecific3Value; + private long mCodecSpecific4Value; + + public void setCodecType(int codecType) { + mCodecType = codecType; + } + + public void setCodecPriority(int codecPriority) { + mCodecPriority = codecPriority; + } + + public void setSampleRate(int sampleRate) { + mSampleRate = sampleRate; + } + + public void setBitsPerSample(int bitsPerSample) { + mBitsPerSample = bitsPerSample; + } + + public void setChannelMode(int channelMode) { + mChannelMode = channelMode; + } + + public void setCodecSpecific1Value(int codecSpecific1Value) { + mCodecSpecific1Value = codecSpecific1Value; + } + + public void setCodecSpecific2Value(int codecSpecific2Value) { + mCodecSpecific2Value = codecSpecific2Value; + } + + public void setCodecSpecific3Value(int codecSpecific3Value) { + mCodecSpecific3Value = codecSpecific3Value; + } + + public void setCodecSpecific4Value(int codecSpecific4Value) { + mCodecSpecific4Value = codecSpecific4Value; + } + + public BluetoothCodecConfig createCodecConfig() { + return new BluetoothCodecConfig(mCodecType, mCodecPriority, + mSampleRate, mBitsPerSample, + mChannelMode, mCodecSpecific1Value, + mCodecSpecific2Value, mCodecSpecific3Value, + mCodecSpecific4Value); + } +} diff --git a/src/com/android/settings/development/BluetoothA2dpSharedStore.java b/src/com/android/settings/development/BluetoothA2dpSharedStore.java deleted file mode 100644 index bb7cb2e1161..00000000000 --- a/src/com/android/settings/development/BluetoothA2dpSharedStore.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright (C) 2017 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.settings.development; - -import android.bluetooth.BluetoothCodecConfig; - -/** - * Utility class for storing current Bluetooth A2DP profile values - */ -public class BluetoothA2dpSharedStore { - - // init default values - private static int sCodecType = BluetoothCodecConfig.SOURCE_CODEC_TYPE_INVALID; - private static int sCodecPriority = BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT; - private static int sSampleRate = BluetoothCodecConfig.SAMPLE_RATE_NONE; - private static int sBitsPerSample = BluetoothCodecConfig.BITS_PER_SAMPLE_NONE; - private static int sChannelMode = BluetoothCodecConfig.CHANNEL_MODE_NONE; - private static long sCodecSpecific1Value = 0; - private static long sCodecSpecific2Value = 0; - private static long sCodecSpecific3Value = 0; - private static long sCodecSpecific4Value = 0; - - public static int getCodecType() { - return sCodecType; - } - - public static int getCodecPriority() { - return sCodecPriority; - } - - public static int getSampleRate() { - return sSampleRate; - } - - public static int getBitsPerSample() { - return sBitsPerSample; - } - - public static int getChannelMode() { - return sChannelMode; - } - - public static long getCodecSpecific1Value() { - return sCodecSpecific1Value; - } - - public static long getCodecSpecific2Value() { - return sCodecSpecific2Value; - } - - public static long getCodecSpecific3Value() { - return sCodecSpecific3Value; - } - - public static long getCodecSpecific4Value() { - return sCodecSpecific4Value; - } - - public static void setCodecType(int codecType) { - sCodecType = codecType; - } - - public static void setCodecPriority(int codecPriority) { - sCodecPriority = codecPriority; - } - - public static void setSampleRate(int sampleRate) { - sSampleRate = sampleRate; - } - - public static void setBitsPerSample(int bitsPerSample) { - sBitsPerSample = bitsPerSample; - } - - public static void setChannelMode(int channelMode) { - sChannelMode = channelMode; - } - - public static void setCodecSpecific1Value(int codecSpecific1Value) { - sCodecSpecific1Value = codecSpecific1Value; - } - - public static void setCodecSpecific2Value(int codecSpecific2Value) { - sCodecSpecific2Value = codecSpecific2Value; - } - - public static void setCodecSpecific3Value(int codecSpecific3Value) { - sCodecSpecific3Value = codecSpecific3Value; - } - - public static void setCodecSpecific4Value(int codecSpecific4Value) { - sCodecSpecific4Value = codecSpecific4Value; - } -} diff --git a/src/com/android/settings/development/BluetoothAudioSampleRatePreferenceController.java b/src/com/android/settings/development/BluetoothAudioSampleRatePreferenceController.java index 43acaa50e55..baabe3d67c2 100644 --- a/src/com/android/settings/development/BluetoothAudioSampleRatePreferenceController.java +++ b/src/com/android/settings/development/BluetoothAudioSampleRatePreferenceController.java @@ -45,14 +45,16 @@ public class BluetoothAudioSampleRatePreferenceController extends private final String[] mListValues; private final String[] mListSummaries; private final Object mBluetoothA2dpLock; + private final BluetoothA2dpConfigStore mBluetoothA2dpConfigStore; private ListPreference mPreference; private BluetoothA2dp mBluetoothA2dp; public BluetoothAudioSampleRatePreferenceController(Context context, Lifecycle lifecycle, - Object bluetoothA2dpLock) { + Object bluetoothA2dpLock, BluetoothA2dpConfigStore store) { super(context); mBluetoothA2dpLock = bluetoothA2dpLock; + mBluetoothA2dpConfigStore = store; mListValues = context.getResources().getStringArray( R.array.bluetooth_a2dp_codec_sample_rate_values); mListSummaries = context.getResources().getStringArray( @@ -63,7 +65,6 @@ public class BluetoothAudioSampleRatePreferenceController extends } } - @Override public String getPreferenceKey() { return BLUETOOTH_SELECT_A2DP_SAMPLE_RATE_KEY; @@ -83,24 +84,10 @@ public class BluetoothAudioSampleRatePreferenceController extends } final int sampleRate = mapPreferenceValueToSampleRate(newValue.toString()); - BluetoothA2dpSharedStore.setSampleRate(sampleRate); - - final int codecTypeValue = BluetoothA2dpSharedStore.getCodecType(); - final int codecPriorityValue = BluetoothA2dpSharedStore.getCodecPriority(); - final int sampleRateValue = BluetoothA2dpSharedStore.getSampleRate(); - final int bitsPerSampleValue = BluetoothA2dpSharedStore.getBitsPerSample(); - final int channelModeValue = BluetoothA2dpSharedStore.getChannelMode(); - final long codecSpecific1Value = BluetoothA2dpSharedStore.getCodecSpecific1Value(); - final long codecSpecific2Value = BluetoothA2dpSharedStore.getCodecSpecific2Value(); - final long codecSpecific3Value = BluetoothA2dpSharedStore.getCodecSpecific3Value(); - final long codecSpecific4Value = BluetoothA2dpSharedStore.getCodecSpecific4Value(); + mBluetoothA2dpConfigStore.setSampleRate(sampleRate); // get values from shared store - BluetoothCodecConfig codecConfig = createCodecConfig(codecTypeValue, codecPriorityValue, - sampleRateValue, bitsPerSampleValue, - channelModeValue, codecSpecific1Value, - codecSpecific2Value, codecSpecific3Value, - codecSpecific4Value); + BluetoothCodecConfig codecConfig = mBluetoothA2dpConfigStore.createCodecConfig(); synchronized (mBluetoothA2dpLock) { if (mBluetoothA2dp != null) { @@ -129,7 +116,7 @@ public class BluetoothAudioSampleRatePreferenceController extends mContext.getResources().getString(STREAMING_LABEL_ID, mListSummaries[index])); // write value to shared store - BluetoothA2dpSharedStore.setSampleRate(sampleRate); + mBluetoothA2dpConfigStore.setSampleRate(sampleRate); } @Override @@ -226,18 +213,4 @@ public class BluetoothAudioSampleRatePreferenceController extends return mBluetoothA2dp.getCodecStatus().getCodecConfig(); } - - @VisibleForTesting - BluetoothCodecConfig createCodecConfig(int codecTypeValue, int codecPriorityValue, - int sampleRateValue, int bitsPerSampleValue, - int channelModeValue, long codecSpecific1Value, - long codecSpecific2Value, long codecSpecific3Value, - long codecSpecific4Value) { - return new BluetoothCodecConfig(codecTypeValue, codecPriorityValue, - sampleRateValue, bitsPerSampleValue, - channelModeValue, codecSpecific1Value, - codecSpecific2Value, codecSpecific3Value, - codecSpecific4Value); - } - } diff --git a/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java b/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java index 8c56ebb0ed3..c3488d70954 100644 --- a/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java +++ b/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java @@ -375,9 +375,10 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra controllers.add(new BluetoothAbsoluteVolumePreferenceController(context)); controllers.add(new BluetoothInbandRingingPreferenceController(context)); controllers.add(new BluetoothAvrcpVersionPreferenceController(context)); + final BluetoothA2dpConfigStore store = new BluetoothA2dpConfigStore(); // bluetooth audio codec controllers.add(new BluetoothAudioSampleRatePreferenceController(context, lifecycle, - bluetoothA2dpLock)); + bluetoothA2dpLock, store)); // bluetooth audio bits per sample // bluetooth audio channel mode // bluetooth audio ldac codec: playback quality diff --git a/tests/robotests/src/com/android/settings/development/BluetoothAudioSampleRatePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/development/BluetoothAudioSampleRatePreferenceControllerTest.java index de8c72a1b6f..27766d9997d 100644 --- a/tests/robotests/src/com/android/settings/development/BluetoothAudioSampleRatePreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/development/BluetoothAudioSampleRatePreferenceControllerTest.java @@ -42,7 +42,6 @@ import com.android.settings.TestConfig; import com.android.settings.testutils.SettingsRobolectricTestRunner; import com.android.settingslib.core.lifecycle.Lifecycle; -import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -63,6 +62,8 @@ public class BluetoothAudioSampleRatePreferenceControllerTest { private ListPreference mPreference; @Mock private PreferenceScreen mScreen; + @Mock + private BluetoothA2dpConfigStore mBluetoothA2dpConfigStore; /** * 0: Use System Selection (Default) @@ -83,11 +84,10 @@ public class BluetoothAudioSampleRatePreferenceControllerTest { mContext = RuntimeEnvironment.application; mLifecycle = new Lifecycle(); mController = spy(new BluetoothAudioSampleRatePreferenceController(mContext, mLifecycle, - new Object())); + new Object(), mBluetoothA2dpConfigStore)); doReturn(mBluetoothCodecConfig).when(mController).getCodecConfig(); doNothing().when(mController).setCodecConfigPreference(any()); - doReturn(mBluetoothCodecConfig).when(mController).createCodecConfig(anyInt(), anyInt(), - anyInt(), anyInt(), anyInt(), anyLong(), anyLong(), anyLong(), anyLong()); + when(mBluetoothA2dpConfigStore.createCodecConfig()).thenReturn(mBluetoothCodecConfig); mListValues = mContext.getResources().getStringArray( R.array.bluetooth_a2dp_codec_sample_rate_values); mListSummaries = mContext.getResources().getStringArray( @@ -115,8 +115,7 @@ public class BluetoothAudioSampleRatePreferenceControllerTest { verify(mPreference).setValue(mListValues[2]); verify(mPreference).setSummary( mContext.getResources().getString(STREAMING_LABEL_ID, mListSummaries[2])); - assertThat(BluetoothA2dpSharedStore.getSampleRate()).isEqualTo( - BluetoothCodecConfig.SAMPLE_RATE_48000); + verify(mBluetoothA2dpConfigStore).setSampleRate(BluetoothCodecConfig.SAMPLE_RATE_48000); } @Test