From d2ba0117fc20b321aedecbad1c3552627d39921c Mon Sep 17 00:00:00 2001 From: jackqdyulei Date: Wed, 29 Mar 2017 16:06:32 -0700 Subject: [PATCH] Expose mac address in bluetooth main page This mac address means the owned device, not the device to connect. Bug: 35875420 Test: RunSettingsRoboTests Change-Id: I142f49fdca72d8ffbb9b8b2e2666a61aefb80505 --- res/values/strings.xml | 2 + .../settings/bluetooth/BluetoothSettings.java | 26 ++++++--- .../bluetooth/BluetoothSettingsTest.java | 56 ++++++++++++++++++- 3 files changed, 74 insertions(+), 10 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index bb4e2a4d413..f605a9a3775 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -246,6 +246,8 @@ %1$s is visible to nearby devices while Bluetooth settings is open. + + Bluetooth MAC address: %1$s Disconnect %1$s? diff --git a/src/com/android/settings/bluetooth/BluetoothSettings.java b/src/com/android/settings/bluetooth/BluetoothSettings.java index 317a350d80a..92a77c91dbf 100644 --- a/src/com/android/settings/bluetooth/BluetoothSettings.java +++ b/src/com/android/settings/bluetooth/BluetoothSettings.java @@ -28,6 +28,7 @@ import android.content.res.Resources; import android.os.Bundle; import android.provider.Settings; import android.support.annotation.VisibleForTesting; +import android.support.v7.preference.Preference; import android.support.v7.preference.PreferenceCategory; import android.support.v7.preference.PreferenceGroup; import android.support.v7.preference.PreferenceScreen; @@ -57,6 +58,7 @@ import com.android.settings.widget.SwitchBar; import com.android.settings.widget.SwitchBarController; import com.android.settingslib.bluetooth.BluetoothDeviceFilter; import com.android.settingslib.bluetooth.CachedBluetoothDevice; +import com.android.settingslib.bluetooth.LocalBluetoothAdapter; import com.android.settingslib.bluetooth.LocalBluetoothManager; import java.util.ArrayList; @@ -70,7 +72,7 @@ import static android.os.UserManager.DISALLOW_CONFIG_BLUETOOTH; * BluetoothSettings is the Settings screen for Bluetooth configuration and * connection management. */ -public final class BluetoothSettings extends DeviceListPreferenceFragment implements Indexable { +public class BluetoothSettings extends DeviceListPreferenceFragment implements Indexable { private static final String TAG = "BluetoothSettings"; private static final int MENU_ID_SCAN = Menu.FIRST; @@ -361,13 +363,7 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment implem startScanning(); } - final Resources res = getResources(); - final Locale locale = res.getConfiguration().getLocales().get(0); - final BidiFormatter bidiFormatter = BidiFormatter.getInstance(locale); - mMyDevicePreference.setTitle(res.getString( - R.string.bluetooth_is_visible_message, - bidiFormatter.unicodeWrap(mLocalAdapter.getName()))); - + updateMyDevicePreference(mMyDevicePreference); getActivity().invalidateOptionsMenu(); // mLocalAdapter.setScanMode is internally synchronized so it is okay for multiple @@ -477,6 +473,20 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment implem } } + @VisibleForTesting + void updateMyDevicePreference(Preference myDevicePreference) { + final BidiFormatter bidiFormatter = BidiFormatter.getInstance(); + + myDevicePreference.setTitle(getString( + R.string.bluetooth_footer_mac_message, + bidiFormatter.unicodeWrap(mLocalAdapter.getAddress()))); + } + + @VisibleForTesting + void setLocalBluetoothAdapter(LocalBluetoothAdapter localAdapter) { + mLocalAdapter = localAdapter; + } + private final GearPreference.OnGearClickListener mDeviceProfilesListener = pref -> { // User clicked on advanced options icon for a device in the list if (!(pref instanceof BluetoothDevicePreference)) { diff --git a/tests/robotests/src/com/android/settings/bluetooth/BluetoothSettingsTest.java b/tests/robotests/src/com/android/settings/bluetooth/BluetoothSettingsTest.java index 9e90fdf37a0..35207f51736 100644 --- a/tests/robotests/src/com/android/settings/bluetooth/BluetoothSettingsTest.java +++ b/tests/robotests/src/com/android/settings/bluetooth/BluetoothSettingsTest.java @@ -16,22 +16,74 @@ package com.android.settings.bluetooth; +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; + +import android.content.Context; +import android.content.res.Resources; +import android.os.UserManager; +import android.support.v7.preference.Preference; + +import com.android.settings.R; import com.android.settings.SettingsRobolectricTestRunner; import com.android.settings.TestConfig; +import com.android.settingslib.bluetooth.LocalBluetoothAdapter; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; @RunWith(SettingsRobolectricTestRunner.class) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) public class BluetoothSettingsTest { + private static final String FOOTAGE_MAC_STRING = "Bluetooth mac: xxxx"; + + @Mock + private UserManager mUserManager; + @Mock + private Resources mResource; + @Mock + private Context mContext; + @Mock + private LocalBluetoothAdapter mLocalAdapter; + private BluetoothSettings mFragment; + private Preference mMyDevicePreference; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + + mFragment = spy(new BluetoothSettings()); + doReturn(mContext).when(mFragment).getContext(); + doReturn(mResource).when(mFragment).getResources(); + + mMyDevicePreference = new Preference(RuntimeEnvironment.application); + + mFragment.setLocalBluetoothAdapter(mLocalAdapter); + } @Test public void setTextSpan_notSpannable_shouldNotCrash() { final String str = "test"; - final BluetoothSettings settings = new BluetoothSettings(); - settings.setTextSpan(str, "hello"); + mFragment.setTextSpan(str, "hello"); + } + + @Test + public void setUpdateMyDevicePreference_setTitleCorrectly() { + doReturn(FOOTAGE_MAC_STRING).when(mFragment).getString( + eq(R.string.bluetooth_footer_mac_message), any()); + + mFragment.updateMyDevicePreference(mMyDevicePreference); + + assertThat(mMyDevicePreference.getTitle()).isEqualTo(FOOTAGE_MAC_STRING); } }