diff --git a/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragment.java b/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragment.java index c57c0b595f7..482c45faa43 100644 --- a/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragment.java +++ b/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragment.java @@ -26,7 +26,10 @@ import com.android.settings.dashboard.DashboardFragment; import com.android.settings.dashboard.SummaryLoader; import com.android.settings.nfc.NfcPreferenceController; import com.android.settings.search.BaseSearchIndexProvider; +import com.android.settingslib.core.AbstractPreferenceController; +import com.android.settingslib.core.lifecycle.Lifecycle; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -59,12 +62,32 @@ public class ConnectedDeviceDashboardFragment extends DashboardFragment { return R.xml.connected_devices; } + @Override + protected List createPreferenceControllers(Context context) { + return buildPreferenceControllers(context, getLifecycle()); + } + + private static List buildPreferenceControllers(Context context, + Lifecycle lifecycle) { + final List controllers = new ArrayList<>(); + final DiscoverableFooterPreferenceController discoverableFooterPreferenceController = + new DiscoverableFooterPreferenceController(context); + controllers.add(discoverableFooterPreferenceController); + + if (lifecycle != null) { + lifecycle.addObserver(discoverableFooterPreferenceController); + } + + return controllers; + } + @Override public void onAttach(Context context) { super.onAttach(context); use(AvailableMediaDeviceGroupController.class).init(this); use(ConnectedDeviceGroupController.class).init(this); use(PreviouslyConnectedDevicePreferenceController.class).init(this); + use(DiscoverableFooterPreferenceController.class).init(this); } @VisibleForTesting @@ -109,6 +132,12 @@ public class ConnectedDeviceDashboardFragment extends DashboardFragment { return Arrays.asList(sir); } + @Override + public List createPreferenceControllers(Context + context) { + return buildPreferenceControllers(context, null /* lifecycle */); + } + @Override public List getNonIndexableKeys(Context context) { List keys = super.getNonIndexableKeys(context); diff --git a/src/com/android/settings/connecteddevice/DiscoverableFooterPreferenceController.java b/src/com/android/settings/connecteddevice/DiscoverableFooterPreferenceController.java new file mode 100644 index 00000000000..6d2b7154b4c --- /dev/null +++ b/src/com/android/settings/connecteddevice/DiscoverableFooterPreferenceController.java @@ -0,0 +1,145 @@ +/* + * Copyright (C) 2018 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.connecteddevice; + +import android.bluetooth.BluetoothAdapter; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.content.pm.PackageManager; +import android.support.v7.preference.PreferenceScreen; +import android.text.BidiFormatter; +import android.text.TextUtils; + +import com.android.internal.annotations.VisibleForTesting; +import com.android.settings.bluetooth.AlwaysDiscoverable; +import com.android.settings.bluetooth.Utils; +import com.android.settings.core.BasePreferenceController; +import com.android.settings.dashboard.DashboardFragment; +import com.android.settings.R; +import com.android.settingslib.bluetooth.LocalBluetoothAdapter; +import com.android.settingslib.bluetooth.LocalBluetoothManager; +import com.android.settingslib.core.lifecycle.events.OnPause; +import com.android.settingslib.core.lifecycle.events.OnResume; +import com.android.settingslib.core.lifecycle.LifecycleObserver; +import com.android.settingslib.widget.FooterPreference; +import com.android.settingslib.widget.FooterPreferenceMixin; + +/** + * Controller that shows and updates the bluetooth device name + */ +public class DiscoverableFooterPreferenceController extends BasePreferenceController + implements LifecycleObserver, OnResume, OnPause { + private static final String KEY = "discoverable_footer_preference"; + + @VisibleForTesting + BroadcastReceiver mBluetoothChangedReceiver; + private FooterPreferenceMixin mFooterPreferenceMixin; + private FooterPreference mPreference; + private LocalBluetoothManager mLocalManager; + private LocalBluetoothAdapter mLocalAdapter; + private AlwaysDiscoverable mAlwaysDiscoverable; + + public DiscoverableFooterPreferenceController(Context context) { + super(context, KEY); + mLocalManager = Utils.getLocalBtManager(context); + if (mLocalManager == null) { + return; + } + mLocalAdapter = mLocalManager.getBluetoothAdapter(); + mAlwaysDiscoverable = new AlwaysDiscoverable(context, mLocalAdapter); + initReceiver(); + } + + private void initReceiver() { + mBluetoothChangedReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + if (intent.getAction().equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { + final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, + BluetoothAdapter.ERROR); + updateFooterPreferenceTitle(state); + } + } + }; + } + + public void init(DashboardFragment fragment) { + mFooterPreferenceMixin = new FooterPreferenceMixin(fragment, fragment.getLifecycle()); + } + + @VisibleForTesting + void init(FooterPreferenceMixin footerPreferenceMixin, FooterPreference preference, + AlwaysDiscoverable alwaysDiscoverable) { + mFooterPreferenceMixin = footerPreferenceMixin; + mPreference = preference; + mAlwaysDiscoverable = alwaysDiscoverable; + } + + @Override + public void displayPreference(PreferenceScreen screen) { + super.displayPreference(screen); + addFooterPreference(screen); + } + + @Override + public int getAvailabilityStatus() { + return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH) + ? AVAILABLE + : UNSUPPORTED_ON_DEVICE; + } + + private void addFooterPreference(PreferenceScreen screen) { + mPreference = mFooterPreferenceMixin.createFooterPreference(); + mPreference.setKey(KEY); + screen.addPreference(mPreference); + } + + @Override + public void onResume() { + mContext.registerReceiver(mBluetoothChangedReceiver, + new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)); + mAlwaysDiscoverable.start(); + updateFooterPreferenceTitle(mLocalAdapter.getState()); + } + + @Override + public void onPause() { + mContext.unregisterReceiver(mBluetoothChangedReceiver); + mAlwaysDiscoverable.stop(); + } + + private void updateFooterPreferenceTitle (int bluetoothState) { + if (bluetoothState == BluetoothAdapter.STATE_ON) { + mPreference.setTitle(getPreferenceTitle()); + } else { + mPreference.setTitle(R.string.bluetooth_off_footer); + } + } + + private CharSequence getPreferenceTitle() { + final String deviceName = mLocalAdapter.getName(); + if (TextUtils.isEmpty(deviceName)) { + return null; + } + + return TextUtils.expandTemplate( + mContext.getText(R.string.bluetooth_device_name_summary), + BidiFormatter.getInstance().unicodeWrap(deviceName)); + } +} \ No newline at end of file diff --git a/tests/robotests/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragmentTest.java b/tests/robotests/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragmentTest.java index cea258d67ce..eecbca5afe2 100644 --- a/tests/robotests/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragmentTest.java +++ b/tests/robotests/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragmentTest.java @@ -31,6 +31,7 @@ import android.provider.SearchIndexableResource; import com.android.settings.R; import com.android.settings.testutils.SettingsRobolectricTestRunner; +import com.android.settings.testutils.shadow.ShadowBluetoothAdapter; import com.android.settings.testutils.shadow.ShadowBluetoothPan; import com.android.settings.testutils.shadow.ShadowConnectivityManager; import com.android.settings.testutils.shadow.ShadowUserManager; @@ -47,7 +48,7 @@ import java.util.List; @RunWith(SettingsRobolectricTestRunner.class) @Config(shadows = {ShadowBluetoothPan.class, ShadowUserManager.class, - ShadowConnectivityManager.class}) + ShadowConnectivityManager.class, ShadowBluetoothAdapter.class}) public class ConnectedDeviceDashboardFragmentTest { @Mock private PackageManager mPackageManager; diff --git a/tests/robotests/src/com/android/settings/connecteddevice/DiscoverableFooterPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/connecteddevice/DiscoverableFooterPreferenceControllerTest.java new file mode 100644 index 00000000000..8cad0bfe70d --- /dev/null +++ b/tests/robotests/src/com/android/settings/connecteddevice/DiscoverableFooterPreferenceControllerTest.java @@ -0,0 +1,180 @@ +/* + * Copyright 2018 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.connecteddevice; + +import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.bluetooth.BluetoothAdapter; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.support.v7.preference.PreferenceScreen; +import android.text.BidiFormatter; +import android.text.TextUtils; + +import com.android.settings.core.BasePreferenceController; +import com.android.settings.bluetooth.AlwaysDiscoverable; +import com.android.settings.R; +import com.android.settings.testutils.SettingsRobolectricTestRunner; +import com.android.settings.testutils.shadow.ShadowBluetoothAdapter; +import com.android.settings.testutils.shadow.ShadowBluetoothPan; +import com.android.settings.testutils.shadow.ShadowLocalBluetoothAdapter; +import com.android.settingslib.widget.FooterPreference; +import com.android.settingslib.widget.FooterPreferenceMixin; + +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.Shadows; +import org.robolectric.annotation.Config; +import org.robolectric.shadows.ShadowApplication; + +import java.util.ArrayList; +import java.util.List; + +@RunWith(SettingsRobolectricTestRunner.class) +@Config(shadows = {ShadowBluetoothPan.class, ShadowBluetoothAdapter.class, + ShadowLocalBluetoothAdapter.class}) +public class DiscoverableFooterPreferenceControllerTest { + private static final String DEVICE_NAME = "device name"; + private static final String KEY = "discoverable_footer_preference"; + + @Mock + private PackageManager mPackageManager; + @Mock + private PreferenceScreen mScreen; + @Mock + private FooterPreferenceMixin mFooterPreferenceMixin; + @Mock + private AlwaysDiscoverable mAlwaysDiscoverable; + + private Context mContext; + private FooterPreference mPreference; + private DiscoverableFooterPreferenceController mDiscoverableFooterPreferenceController; + private BroadcastReceiver mBluetoothChangedReceiver; + private ShadowApplication mShadowApplication; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mShadowApplication = Shadows.shadowOf(RuntimeEnvironment.application); + mContext = spy(RuntimeEnvironment.application); + + doReturn(mPackageManager).when(mContext).getPackageManager(); + mDiscoverableFooterPreferenceController = + new DiscoverableFooterPreferenceController(mContext); + mPreference = spy(new FooterPreference(mContext)); + mDiscoverableFooterPreferenceController.init(mFooterPreferenceMixin, mPreference, + mAlwaysDiscoverable); + mBluetoothChangedReceiver = mDiscoverableFooterPreferenceController + .mBluetoothChangedReceiver; + } + + @Test + public void getAvailabilityStatus_noBluetoothFeature_returnUnSupported() { + when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)).thenReturn(false); + + assertThat(mDiscoverableFooterPreferenceController.getAvailabilityStatus()).isEqualTo( + BasePreferenceController.UNSUPPORTED_ON_DEVICE); + } + + @Test + public void getAvailabilityStatus_BluetoothFeature_returnAvailable() { + when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)).thenReturn(true); + + assertThat(mDiscoverableFooterPreferenceController.getAvailabilityStatus()).isEqualTo( + BasePreferenceController.AVAILABLE); + } + + @Test + public void displayPreference() { + when(mFooterPreferenceMixin.createFooterPreference()).thenReturn(mPreference); + mDiscoverableFooterPreferenceController.displayPreference(mScreen); + + verify(mPreference).setKey(KEY); + verify(mScreen).addPreference(mPreference); + } + + @Test + public void onResume() { + mDiscoverableFooterPreferenceController.onResume(); + assertThat(getRegisteredBroadcastReceivers()).contains(mBluetoothChangedReceiver); + verify(mAlwaysDiscoverable).start(); + } + + @Test + public void onPause() { + mDiscoverableFooterPreferenceController.onResume(); + mDiscoverableFooterPreferenceController.onPause(); + + assertThat(getRegisteredBroadcastReceivers()).doesNotContain(mBluetoothChangedReceiver); + verify(mAlwaysDiscoverable).stop(); + } + + @Test + public void onBluetoothStateChanged_bluetoothOn_updateTitle() { + ShadowLocalBluetoothAdapter.setName(DEVICE_NAME); + sendBluetoothStateChangedIntent(BluetoothAdapter.STATE_ON); + + assertThat(mPreference.getTitle()).isEqualTo(generateTitle(DEVICE_NAME)); + } + + @Test + public void onBluetoothStateChanged_bluetoothOff_updateTitle(){ + ShadowLocalBluetoothAdapter.setName(DEVICE_NAME); + sendBluetoothStateChangedIntent(BluetoothAdapter.STATE_OFF); + + assertThat(mPreference.getTitle()).isEqualTo(generateTitle(null)); + } + + private CharSequence generateTitle(String deviceName) { + if (deviceName == null) { + return mContext.getString(R.string.bluetooth_off_footer); + + } else { + return TextUtils.expandTemplate( + mContext.getText(R.string.bluetooth_device_name_summary), + BidiFormatter.getInstance().unicodeWrap(deviceName)); + } + } + + private void sendBluetoothStateChangedIntent(int state) { + Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED); + intent.putExtra(BluetoothAdapter.EXTRA_STATE, state); + mBluetoothChangedReceiver.onReceive(mContext, intent); + } + + /** + * Return a list of all the registered broadcast receivers + */ + private List getRegisteredBroadcastReceivers() { + List registeredBroadcastReceivers = new ArrayList(); + List registeredReceivers = + mShadowApplication.getRegisteredReceivers(); + for (ShadowApplication.Wrapper wrapper : registeredReceivers) { + registeredBroadcastReceivers.add(wrapper.getBroadcastReceiver()); + } + return registeredBroadcastReceivers; + } +} \ No newline at end of file diff --git a/tests/robotests/src/com/android/settings/testutils/shadow/ShadowBluetoothAdapter.java b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowBluetoothAdapter.java new file mode 100644 index 00000000000..2bd2ec33aaa --- /dev/null +++ b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowBluetoothAdapter.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2018 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.testutils.shadow; + +import android.bluetooth.BluetoothAdapter; + +import org.robolectric.annotation.Implementation; +import org.robolectric.annotation.Implements; + +import java.util.ArrayList; +import java.util.List; + +@Implements(value = BluetoothAdapter.class, inheritImplementationMethods = true) +public class ShadowBluetoothAdapter extends org.robolectric.shadows.ShadowBluetoothAdapter { + /** + * Do nothing, implement it to avoid null pointer error inside BluetoothAdapter + */ + @Implementation + public List getSupportedProfiles() { + return new ArrayList(); + } +} diff --git a/tests/robotests/src/com/android/settings/testutils/shadow/ShadowLocalBluetoothAdapter.java b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowLocalBluetoothAdapter.java new file mode 100644 index 00000000000..d215e2abfeb --- /dev/null +++ b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowLocalBluetoothAdapter.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2018 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.testutils.shadow; + +import com.android.settingslib.bluetooth.LocalBluetoothAdapter; + +import org.robolectric.annotation.Implementation; +import org.robolectric.annotation.Implements; + +@Implements(LocalBluetoothAdapter.class) +public class ShadowLocalBluetoothAdapter { + + private static String sName; + + @Implementation + public String getName() { + return sName; + } + + public static void setName(String name) { + sName = name; + } +}