diff --git a/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragment.java b/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragment.java index d958b38409b..1ff1fcd0a96 100644 --- a/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragment.java +++ b/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragment.java @@ -26,8 +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.search.SearchIndexable; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -61,12 +63,27 @@ public class ConnectedDeviceDashboardFragment extends DashboardFragment { return R.xml.connected_devices; } + @Override + protected List createPreferenceControllers(Context context) { + return buildPreferenceControllers(context); + } + + private static List buildPreferenceControllers(Context context) { + final List controllers = new ArrayList<>(); + final DiscoverableFooterPreferenceController discoverableFooterPreferenceController = + new DiscoverableFooterPreferenceController(context); + controllers.add(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 diff --git a/src/com/android/settings/connecteddevice/DiscoverableFooterPreferenceController.java b/src/com/android/settings/connecteddevice/DiscoverableFooterPreferenceController.java new file mode 100644 index 00000000000..71e5409c1f6 --- /dev/null +++ b/src/com/android/settings/connecteddevice/DiscoverableFooterPreferenceController.java @@ -0,0 +1,68 @@ +/* + * 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.content.Context; +import android.content.pm.PackageManager; +import androidx.annotation.VisibleForTesting; +import androidx.preference.PreferenceScreen; +import com.android.settings.core.BasePreferenceController; +import com.android.settings.dashboard.DashboardFragment; +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 { + private static final String KEY = "discoverable_footer_preference"; + + private FooterPreference mPreference; + private FooterPreferenceMixin mFooterPreferenceMixin; + + + public DiscoverableFooterPreferenceController(Context context) { super(context, KEY); } + + public void init(DashboardFragment fragment) { + mFooterPreferenceMixin = new FooterPreferenceMixin(fragment, fragment.getLifecycle()); + } + + @VisibleForTesting + void init(FooterPreferenceMixin footerPreferenceMixin, FooterPreference preference) { + mFooterPreferenceMixin = footerPreferenceMixin; + mPreference = preference; + } + + @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); + } +} \ No newline at end of file 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..42fdeb95b37 --- /dev/null +++ b/tests/robotests/src/com/android/settings/connecteddevice/DiscoverableFooterPreferenceControllerTest.java @@ -0,0 +1,96 @@ +/* + * 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.ArgumentMatchers.anyString; +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.content.Context; +import android.content.pm.PackageManager; +import androidx.preference.PreferenceScreen; + +import com.android.settings.core.BasePreferenceController; +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.annotation.Config; + +@RunWith(SettingsRobolectricTestRunner.class) +@Config(shadows = {ShadowBluetoothPan.class, ShadowBluetoothAdapter.class, + ShadowLocalBluetoothAdapter.class}) +public class DiscoverableFooterPreferenceControllerTest { + @Mock + private PackageManager mPackageManager; + @Mock + private PreferenceScreen mScreen; + @Mock + private FooterPreferenceMixin mFooterPreferenceMixin; + + private Context mContext; + private DiscoverableFooterPreferenceController mDiscoverableFooterPreferenceController; + private FooterPreference mPreference; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mContext = spy(RuntimeEnvironment.application); + doReturn(mPackageManager).when(mContext).getPackageManager(); + mDiscoverableFooterPreferenceController = + new DiscoverableFooterPreferenceController(mContext); + mPreference = spy(new FooterPreference(mContext)); + mDiscoverableFooterPreferenceController.init(mFooterPreferenceMixin, mPreference); + } + + @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_returnSupported() { + 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(anyString()); + verify(mScreen).addPreference(mPreference); + } +} 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; + } +}