Implement previously connected device UI

* Move save device group to "Previously connected devices"

Bug: 74134939
Test: make -j60 RunSettingsRoboTests
Change-Id: Iff7894033df402d42dcc0ccaea6db3106edb7013
Merged-In: Iff7894033df402d42dcc0ccaea6db3106edb7013
diff --git a/res/drawable/ic_devices_other_black.xml b/res/drawable/ic_devices_other_black.xml
new file mode 100644
index 0000000..9974eaf
--- /dev/null
+++ b/res/drawable/ic_devices_other_black.xml
@@ -0,0 +1,29 @@
+<!--
+    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.
+-->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+        android:viewportWidth="24"
+        android:viewportHeight="24"
+        android:width="24dp"
+        android:height="24dp">
+    <path android:fillColor="#FF000000"
+          android:pathData="M3 6h18V4H3C1.9 4 1 4.9 1 6v12c0 1.10.9 2 2 2h4v-2H3V6z M13 12H9
+          v1.78C8.39 14.33 8 15.11 8 16c0 0.89 0.39 1.67 1 2.22 V20h4v-1.78c0.61-0.55 1-1.34
+          1-2.22s-0.39-1.67-1-2.22V12z M11 17.5c-0.83 0-1.5-0.67-1.5-1.5s0.67-1.5 1.5-1.5s1.5
+          0.67 1.5 1.5 S11.83 17.5 11 17.5z M22 8h-6c-0.5 0-1 0.5-1 1v10c0 0.5 0.5 1 1
+          1h6c0.5 0 1-0.5 1-1V9C23 8.5 22.5 8 22 8z M21 18h-4v-8h4V18z" />
+    <path android:fillColor="#00000000"
+          android:pathData="M0 0h24v24H0V0z"/>
+</vector>
\ No newline at end of file
diff --git a/res/values/strings.xml b/res/values/strings.xml
index a351bd8..323d075 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -6660,6 +6660,8 @@
     <string name="help_url_font_size" translatable="false"></string>
     <!-- Help URL, Display size [DO NOT TRANSLATE] -->
     <string name="help_url_display_size" translatable="false"></string>
+    <!-- Help URL, Previously connected bluetooth devices [DO NOT TRANSLATE] -->
+    <string name="help_url_previously_connected_devices" translatable="false"></string>
     <!-- Help URL, Auto brightness [DO NOT TRANSLATE] -->
     <string name="help_url_auto_brightness" translatable="false" />
 
diff --git a/res/xml/connected_devices.xml b/res/xml/connected_devices.xml
index 7428019..030428f 100644
--- a/res/xml/connected_devices.xml
+++ b/res/xml/connected_devices.xml
@@ -24,27 +24,29 @@
         android:key="connected_device_list"
         android:title="@string/connected_device_connected_title"/>
 
-    <PreferenceCategory
-        android:key="saved_device_list"
-        android:title="@string/connected_device_saved_title"/>
-
     <com.android.settingslib.RestrictedPreference
-        android:fragment="com.android.settings.bluetooth.BluetoothPairingDetail"
         android:key="add_bt_devices"
         android:title="@string/connected_device_add_device_title"
         android:icon="@drawable/ic_menu_add"
         android:summary="@string/connected_device_add_device_summary"
+        android:fragment="com.android.settings.bluetooth.BluetoothPairingDetail"
         settings:allowDividerAbove="true"
         settings:userRestriction="no_config_bluetooth"
         settings:useAdminDisabledSummary="true"
         settings:controller="com.android.settings.connecteddevice.AddDevicePreferenceController"/>
 
+    <Preference
+        android:key="previously_connected_devices"
+        android:title="@string/connected_device_previously_connected_title"
+        android:icon="@drawable/ic_devices_other_black"
+        android:fragment="com.android.settings.connecteddevice.PreviouslyConnectedDeviceDashboardFragment"
+        settings:allowDividerAbove="true"/>
+
     <com.android.settingslib.RestrictedSwitchPreference
         android:key="toggle_bluetooth_switch"
         android:title="@string/bluetooth_settings_title"
         android:icon="@drawable/ic_settings_bluetooth"
         android:summary="@string/bluetooth_pref_summary"
-        settings:allowDividerAbove="true"
         settings:controller="com.android.settings.bluetooth.BluetoothSwitchPreferenceController"
         settings:userRestriction="no_bluetooth"
         settings:platform_slice="true"/>
@@ -56,7 +58,7 @@
         android:summary="@string/nfc_quick_toggle_summary"/>
 
     <Preference
-        android:fragment="com.android.settings.connecteddevice.AdvancedConnectedDeviceDashboardFragment"
         android:key="connection_preferences"
-        android:title="@string/connected_device_connections_title"/>
+        android:title="@string/connected_device_connections_title"
+        android:fragment="com.android.settings.connecteddevice.AdvancedConnectedDeviceDashboardFragment"/>
 </PreferenceScreen>
diff --git a/res/xml/previously_connected_devices.xml b/res/xml/previously_connected_devices.xml
new file mode 100644
index 0000000..b0578a7
--- /dev/null
+++ b/res/xml/previously_connected_devices.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 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.
+-->
+
+<PreferenceScreen
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:settings="http://schemas.android.com/apk/res-auto"
+    android:key="connected_devices_screen_previously"
+    android:title="@string/connected_device_previously_connected_title">
+
+    <PreferenceCategory
+        android:key="saved_device_list"
+        settings:controller="com.android.settings.connecteddevice.SavedDeviceGroupController"/>
+
+</PreferenceScreen>
\ No newline at end of file
diff --git a/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragment.java b/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragment.java
index b3dd8ac..942fd7b 100644
--- a/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragment.java
+++ b/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragment.java
@@ -72,7 +72,6 @@
             Lifecycle lifecycle, DashboardFragment dashboardFragment) {
         final List<AbstractPreferenceController> controllers = new ArrayList<>();
         controllers.add(new ConnectedDeviceGroupController(context, dashboardFragment, lifecycle));
-        controllers.add(new SavedDeviceGroupController(context, dashboardFragment, lifecycle));
 
         final NfcPreferenceController nfcPreferenceController =
                 new NfcPreferenceController(context);
diff --git a/src/com/android/settings/connecteddevice/PreviouslyConnectedDeviceDashboardFragment.java b/src/com/android/settings/connecteddevice/PreviouslyConnectedDeviceDashboardFragment.java
new file mode 100644
index 0000000..195daf3
--- /dev/null
+++ b/src/com/android/settings/connecteddevice/PreviouslyConnectedDeviceDashboardFragment.java
@@ -0,0 +1,85 @@
+/*
+ * 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 android.content.Context;
+import android.content.res.Resources;
+import com.android.internal.logging.nano.MetricsProto;
+import com.android.settings.R;
+import com.android.settings.dashboard.DashboardFragment;
+import com.android.settings.search.BaseSearchIndexProvider;
+import com.android.settings.search.SearchIndexableRaw;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * This fragment contains previously connected device
+ */
+public class PreviouslyConnectedDeviceDashboardFragment extends DashboardFragment {
+
+    private static final String TAG = "PreConnectedDeviceFrag";
+    static final String KEY_PREVIOUSLY_CONNECTED_DEVICES = "saved_device_list";
+
+    @Override
+    public int getHelpResource() {
+        return R.string.help_url_previously_connected_devices;
+    }
+
+    @Override
+    protected int getPreferenceScreenResId() {
+        return R.xml.previously_connected_devices;
+    }
+
+    @Override
+    protected String getLogTag() {
+        return TAG;
+    }
+
+    @Override
+    public int getMetricsCategory() {
+        return MetricsProto.MetricsEvent.PREVIOUSLY_CONNECTED_DEVICES;
+    }
+
+    @Override
+    public void onAttach(Context context) {
+        super.onAttach(context);
+        use(SavedDeviceGroupController.class).init(this);
+    }
+
+    /**
+     * For Search.
+     */
+    public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
+            new BaseSearchIndexProvider() {
+        @Override
+        public List<SearchIndexableRaw> getRawDataToIndex(
+                Context context, boolean enabled) {
+            final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
+            final Resources res = context.getResources();
+
+            // Add fragment title
+            SearchIndexableRaw data = new SearchIndexableRaw(context);
+            data.key = KEY_PREVIOUSLY_CONNECTED_DEVICES;
+            data.title = res.getString(
+                    R.string.connected_device_previously_connected_title);
+            data.screenTitle = res.getString(
+                    R.string.connected_device_previously_connected_title);
+            result.add(data);
+            return result;
+        }
+    };
+}
\ No newline at end of file
diff --git a/src/com/android/settings/connecteddevice/SavedDeviceGroupController.java b/src/com/android/settings/connecteddevice/SavedDeviceGroupController.java
index ff21bff..d045c9e 100644
--- a/src/com/android/settings/connecteddevice/SavedDeviceGroupController.java
+++ b/src/com/android/settings/connecteddevice/SavedDeviceGroupController.java
@@ -46,18 +46,15 @@
     PreferenceGroup mPreferenceGroup;
     private BluetoothDeviceUpdater mBluetoothDeviceUpdater;
 
-    public SavedDeviceGroupController(Context context, DashboardFragment fragment,
-            Lifecycle lifecycle) {
+    public SavedDeviceGroupController(Context context) {
         super(context, KEY);
-        init(lifecycle, new SavedBluetoothDeviceUpdater(context, fragment,
-                SavedDeviceGroupController.this));
     }
 
     @VisibleForTesting
-    SavedDeviceGroupController(DashboardFragment fragment, Lifecycle lifecycle,
+    SavedDeviceGroupController(DashboardFragment fragment,
             BluetoothDeviceUpdater bluetoothDeviceUpdater) {
         super(fragment.getContext(), KEY);
-        init(lifecycle, bluetoothDeviceUpdater);
+        mBluetoothDeviceUpdater = bluetoothDeviceUpdater;
     }
 
     @Override
@@ -108,10 +105,8 @@
         }
     }
 
-    private void init(Lifecycle lifecycle, BluetoothDeviceUpdater bluetoothDeviceUpdater) {
-        if (lifecycle != null && isAvailable()) {
-            lifecycle.addObserver(this);
-        }
-        mBluetoothDeviceUpdater = bluetoothDeviceUpdater;
+    public void init(DashboardFragment fragment) {
+        mBluetoothDeviceUpdater = new SavedBluetoothDeviceUpdater(fragment.getContext(),
+                fragment, SavedDeviceGroupController.this);
     }
 }
diff --git a/src/com/android/settings/core/gateway/SettingsGateway.java b/src/com/android/settings/core/gateway/SettingsGateway.java
index afa5f5e..ede23dc 100644
--- a/src/com/android/settings/core/gateway/SettingsGateway.java
+++ b/src/com/android/settings/core/gateway/SettingsGateway.java
@@ -55,6 +55,7 @@
 import com.android.settings.bluetooth.BluetoothDeviceDetailsFragment;
 import com.android.settings.connecteddevice.AdvancedConnectedDeviceDashboardFragment;
 import com.android.settings.connecteddevice.ConnectedDeviceDashboardFragment;
+import com.android.settings.connecteddevice.PreviouslyConnectedDeviceDashboardFragment;
 import com.android.settings.connecteddevice.usb.UsbDetailsFragment;
 import com.android.settings.datausage.DataUsageList;
 import com.android.settings.datausage.DataUsageSummary;
@@ -257,6 +258,7 @@
             DataUsageList.class.getName(),
             DirectoryAccessDetails.class.getName(),
             ToggleBackupSettingFragment.class.getName(),
+            PreviouslyConnectedDeviceDashboardFragment.class.getName(),
     };
 
     public static final String[] SETTINGS_FOR_RESTRICTED = {
diff --git a/src/com/android/settings/search/SearchIndexableResourcesImpl.java b/src/com/android/settings/search/SearchIndexableResourcesImpl.java
index 3e4e0be6..33bc2e0 100644
--- a/src/com/android/settings/search/SearchIndexableResourcesImpl.java
+++ b/src/com/android/settings/search/SearchIndexableResourcesImpl.java
@@ -22,6 +22,7 @@
 import com.android.settings.DisplaySettings;
 import com.android.settings.LegalSettings;
 import com.android.settings.connecteddevice.AdvancedConnectedDeviceDashboardFragment;
+import com.android.settings.connecteddevice.PreviouslyConnectedDeviceDashboardFragment;
 import com.android.settings.datausage.DataUsageSummaryLegacy;
 import com.android.settings.deviceinfo.aboutphone.MyDeviceInfoFragment;
 import com.android.settings.accessibility.AccessibilitySettings;
@@ -183,6 +184,7 @@
         addIndex(MyDeviceInfoFragment.class);
         addIndex(VibrationSettings.class);
         addIndex(RecentLocationRequestSeeAllFragment.class);
+        addIndex(PreviouslyConnectedDeviceDashboardFragment.class);
     }
 
     @Override
diff --git a/tests/robotests/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdaterTest.java b/tests/robotests/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdaterTest.java
index ee76269..e0ba829 100644
--- a/tests/robotests/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdaterTest.java
+++ b/tests/robotests/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdaterTest.java
@@ -30,6 +30,8 @@
 import com.android.settings.dashboard.DashboardFragment;
 import com.android.settings.testutils.SettingsRobolectricTestRunner;
 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
+import com.android.settingslib.bluetooth.LocalBluetoothManager;
+import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -49,6 +51,10 @@
     private CachedBluetoothDevice mCachedBluetoothDevice;
     @Mock
     private BluetoothDevice mBluetoothDevice;
+    @Mock
+    private LocalBluetoothManager mLocalManager;
+    @Mock
+    private LocalBluetoothProfileManager mLocalBluetoothProfileManager;
 
     private Context mContext;
     private BluetoothDeviceUpdater mBluetoothDeviceUpdater;
@@ -60,9 +66,10 @@
         mContext = RuntimeEnvironment.application;
         doReturn(mContext).when(mDashboardFragment).getContext();
         when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
+        when(mLocalManager.getProfileManager()).thenReturn(mLocalBluetoothProfileManager);
 
         mBluetoothDeviceUpdater = spy(new SavedBluetoothDeviceUpdater(mDashboardFragment,
-                mDevicePreferenceCallback, null));
+                mDevicePreferenceCallback, mLocalManager));
         mBluetoothDeviceUpdater.setPrefContext(mContext);
         doNothing().when(mBluetoothDeviceUpdater).addPreference(any());
         doNothing().when(mBluetoothDeviceUpdater).removePreference(any());
diff --git a/tests/robotests/src/com/android/settings/connecteddevice/SavedDeviceGroupControllerTest.java b/tests/robotests/src/com/android/settings/connecteddevice/SavedDeviceGroupControllerTest.java
index 3d606dd..a0d3d7b 100644
--- a/tests/robotests/src/com/android/settings/connecteddevice/SavedDeviceGroupControllerTest.java
+++ b/tests/robotests/src/com/android/settings/connecteddevice/SavedDeviceGroupControllerTest.java
@@ -14,32 +14,20 @@
  * limitations under the License
  */
 package com.android.settings.connecteddevice;
-
-import static android.arch.lifecycle.Lifecycle.Event.ON_START;
-
 import static com.android.settings.core.BasePreferenceController.AVAILABLE;
 import static com.android.settings.core.BasePreferenceController.DISABLED_UNSUPPORTED;
-
 import static com.google.common.truth.Truth.assertThat;
-
 import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.verify;
-
 import android.arch.lifecycle.LifecycleOwner;
 import android.content.Context;
 import android.content.pm.PackageManager;
-import android.support.v7.preference.Preference;
-import android.support.v7.preference.PreferenceGroup;
 import android.support.v7.preference.PreferenceManager;
-import android.support.v7.preference.PreferenceScreen;
-
 import com.android.settings.bluetooth.BluetoothDeviceUpdater;
 import com.android.settings.dashboard.DashboardFragment;
 import com.android.settings.testutils.SettingsRobolectricTestRunner;
 import com.android.settingslib.core.lifecycle.Lifecycle;
-
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -47,68 +35,55 @@
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 import org.robolectric.RuntimeEnvironment;
-import org.robolectric.annotation.Config;
 
 @RunWith(SettingsRobolectricTestRunner.class)
 public class SavedDeviceGroupControllerTest {
     private static final String PREFERENCE_KEY_1 = "pref_key_1";
-
     @Mock
     private DashboardFragment mDashboardFragment;
     @Mock
     private BluetoothDeviceUpdater mBluetoothDeviceUpdater;
-    @Mock
-    private PreferenceScreen mPreferenceScreen;
     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     private PreferenceManager mPreferenceManager;
     @Mock
     private PackageManager mPackageManager;
-
-    private PreferenceGroup mPreferenceGroup;
     private Context mContext;
-    private Preference mPreference;
-    private SavedDeviceGroupController mConnectedDeviceGroupController;
+    private SavedDeviceGroupController mSavedDeviceGroupController;
     private LifecycleOwner mLifecycleOwner;
     private Lifecycle mLifecycle;
-
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
-
         mContext = spy(RuntimeEnvironment.application);
         mLifecycleOwner = () -> mLifecycle;
         mLifecycle = new Lifecycle(mLifecycleOwner);
-
         doReturn(mContext).when(mDashboardFragment).getContext();
         doReturn(mPackageManager).when(mContext).getPackageManager();
+        mSavedDeviceGroupController = new SavedDeviceGroupController(mDashboardFragment,
+                mBluetoothDeviceUpdater);
     }
-
     @Test
-    public void constructor_noBluetoothFeature_shouldNotRegisterCallback() {
-        doReturn(false).when(mPackageManager).hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);
-
-        mConnectedDeviceGroupController = new SavedDeviceGroupController(mDashboardFragment,
-                mLifecycle, mBluetoothDeviceUpdater);
-
-        assertThat(mConnectedDeviceGroupController.getAvailabilityStatus()).isEqualTo(
-                DISABLED_UNSUPPORTED);
-
-        mLifecycle.handleLifecycleEvent(ON_START);
-        verify(mBluetoothDeviceUpdater, never()).registerCallback();
-    }
-
-
-    @Test
-    public void constructor_hasBluetoothFeature_shouldRegisterCallback() {
-        doReturn(true).when(mPackageManager).hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);
-
-        mConnectedDeviceGroupController = new SavedDeviceGroupController(mDashboardFragment,
-                mLifecycle, mBluetoothDeviceUpdater);
-
-        assertThat(mConnectedDeviceGroupController.getAvailabilityStatus()).isEqualTo(
-                AVAILABLE);
-
-        mLifecycle.handleLifecycleEvent(ON_START);
+    public void testRegister() {
+        // register the callback in onStart()
+        mSavedDeviceGroupController.onStart();
         verify(mBluetoothDeviceUpdater).registerCallback();
     }
-}
+    @Test
+    public void testUnregister() {
+        // unregister the callback in onStop()
+        mSavedDeviceGroupController.onStop();
+        verify(mBluetoothDeviceUpdater).unregisterCallback();
+    }
+    @Test
+    public void testGetAvailabilityStatus_noBluetoothFeature_returnUnSupported() {
+        doReturn(false).when(mPackageManager).hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);
+        assertThat(mSavedDeviceGroupController.getAvailabilityStatus()).isEqualTo(
+                DISABLED_UNSUPPORTED);
+    }
+    @Test
+    public void testGetAvailabilityStatus_BluetoothFeature_returnSupported() {
+        doReturn(true).when(mPackageManager).hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);
+        assertThat(mSavedDeviceGroupController.getAvailabilityStatus()).isEqualTo(
+                AVAILABLE);
+    }
+}
\ No newline at end of file