Add a new toggle for Bluetooth while driving.

The toggle is controlled by a platform feature flag.

Bug: 66679618
Test: Robotest
Change-Id: Ibe8e8756a4ab54a83ee03c6cf008193ed107955d
diff --git a/res/values/strings.xml b/res/values/strings.xml
index eea6ddd..3e90522 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -9240,4 +9240,9 @@
     <!-- Title for preference showing the name of the device. [CHAR LIMIT=60]-->
     <string name="me_card_device_name_preference_title">Device name</string>
 
+    <!-- Settings item title for automatic Bluetooth on while driving preference [CHAR LIMIT=35] -->
+    <string name="bluetooth_on_while_driving_pref">Use Bluetooth when driving</string>
+    <!-- Settings item summary for automatic Bluetooth on while driving preference [CHAR LIMIT=100] -->
+    <string name="bluetooth_on_while_driving_summary">Turn on Bluetooth automatically when driving</string>
+
 </resources>
diff --git a/res/xml/connected_devices_advanced.xml b/res/xml/connected_devices_advanced.xml
index dfe23df..078d7dd 100644
--- a/res/xml/connected_devices_advanced.xml
+++ b/res/xml/connected_devices_advanced.xml
@@ -47,11 +47,18 @@
         android:summary="@string/summary_placeholder"
         android:order="-3"/>
 
+    <SwitchPreference
+        android:key="bluetooth_on_while_driving"
+        android:title="@string/bluetooth_on_while_driving_pref"
+        android:icon="@drawable/ic_settings_bluetooth"
+        android:summary="@string/bluetooth_on_while_driving_summary"
+        android:order="-2"/>
+
     <Preference
         android:key="usb_mode"
         android:title="@string/usb_pref"
         android:icon="@drawable/ic_usb"
-        android:order="-2">
+        android:order="-1">
         <intent android:action="android.intent.action.MAIN"
                 android:targetPackage="com.android.settings"
                 android:targetClass="com.android.settings.deviceinfo.UsbModeChooserActivity"/>
diff --git a/src/com/android/settings/connecteddevice/AdvancedConnectedDeviceDashboardFragment.java b/src/com/android/settings/connecteddevice/AdvancedConnectedDeviceDashboardFragment.java
index 02b1012..2a136bc 100644
--- a/src/com/android/settings/connecteddevice/AdvancedConnectedDeviceDashboardFragment.java
+++ b/src/com/android/settings/connecteddevice/AdvancedConnectedDeviceDashboardFragment.java
@@ -21,11 +21,9 @@
 
 import com.android.internal.logging.nano.MetricsProto;
 import com.android.settings.R;
-import com.android.settings.SettingsActivity;
 import com.android.settings.bluetooth.BluetoothFilesPreferenceController;
 import com.android.settings.bluetooth.BluetoothMasterSwitchPreferenceController;
 import com.android.settings.bluetooth.BluetoothSwitchPreferenceController;
-import com.android.settings.bluetooth.Utils;
 import com.android.settings.dashboard.DashboardFragment;
 import com.android.settings.deviceinfo.UsbBackend;
 import com.android.settings.nfc.NfcPreferenceController;
@@ -33,7 +31,6 @@
 import com.android.settings.search.BaseSearchIndexProvider;
 import com.android.settingslib.core.AbstractPreferenceController;
 import com.android.settingslib.core.lifecycle.Lifecycle;
-import com.android.settingslib.drawer.CategoryKey;
 
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -89,6 +86,7 @@
                 smsMirroringFeatureProvider.getController(context);
         controllers.add(smsMirroringController);
         controllers.add(new BluetoothFilesPreferenceController(context));
+        controllers.add(new BluetoothOnWhileDrivingPreferenceController(context));
         return controllers;
     }
 
diff --git a/src/com/android/settings/connecteddevice/BluetoothOnWhileDrivingPreferenceController.java b/src/com/android/settings/connecteddevice/BluetoothOnWhileDrivingPreferenceController.java
new file mode 100644
index 0000000..072de75
--- /dev/null
+++ b/src/com/android/settings/connecteddevice/BluetoothOnWhileDrivingPreferenceController.java
@@ -0,0 +1,59 @@
+/*
+ * 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.provider.Settings;
+import android.util.FeatureFlagUtils;
+
+import com.android.settings.core.FeatureFlags;
+import com.android.settings.core.PreferenceControllerMixin;
+import com.android.settings.core.TogglePreferenceController;
+
+/** Handles a toggle for a setting to turn on Bluetooth while driving. * */
+public class BluetoothOnWhileDrivingPreferenceController extends TogglePreferenceController
+        implements PreferenceControllerMixin {
+    static final String KEY_BLUETOOTH_ON_DRIVING = "bluetooth_on_while_driving";
+
+    public BluetoothOnWhileDrivingPreferenceController(Context context) {
+        super(context, KEY_BLUETOOTH_ON_DRIVING);
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        if (FeatureFlagUtils.isEnabled(mContext, FeatureFlags.BLUETOOTH_WHILE_DRIVING)) {
+            return AVAILABLE;
+        }
+        return DISABLED_UNSUPPORTED;
+    }
+
+    @Override
+    public boolean isChecked() {
+        return Settings.Secure.getInt(
+                        mContext.getContentResolver(),
+                        Settings.Secure.BLUETOOTH_ON_WHILE_DRIVING,
+                        0)
+                != 0;
+    }
+
+    @Override
+    public boolean setChecked(boolean isChecked) {
+        final int value = isChecked ? 1 : 0;
+        return Settings.Secure.putInt(
+                mContext.getContentResolver(), Settings.Secure.BLUETOOTH_ON_WHILE_DRIVING, value);
+    }
+}
diff --git a/src/com/android/settings/core/FeatureFlags.java b/src/com/android/settings/core/FeatureFlags.java
index 4b8ccd1..e1636b4 100644
--- a/src/com/android/settings/core/FeatureFlags.java
+++ b/src/com/android/settings/core/FeatureFlags.java
@@ -28,4 +28,5 @@
     public static final String ZONE_PICKER_V2 = "settings_zone_picker_v2";
     public static final String SUGGESTION_UI_V2 = "settings_suggestion_ui_v2";
     public static final String ABOUT_PHONE_V2 = "settings_about_phone_v2";
+    public static final String BLUETOOTH_WHILE_DRIVING = "settings_bluetooth_while_driving";
 }
diff --git a/tests/robotests/src/com/android/settings/connecteddevice/BluetoothOnWhileDrivingPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/connecteddevice/BluetoothOnWhileDrivingPreferenceControllerTest.java
new file mode 100644
index 0000000..3e60ca6
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/connecteddevice/BluetoothOnWhileDrivingPreferenceControllerTest.java
@@ -0,0 +1,83 @@
+/*
+ * 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 static com.google.common.truth.Truth.assertThat;
+
+import android.content.Context;
+import android.provider.Settings;
+import android.util.FeatureFlagUtils;
+
+import com.android.settings.TestConfig;
+import com.android.settings.core.BasePreferenceController;
+import com.android.settings.core.FeatureFlags;
+import com.android.settings.testutils.FakeFeatureFactory;
+import com.android.settings.testutils.SettingsRobolectricTestRunner;
+import com.android.settings.testutils.shadow.SettingsShadowSystemProperties;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+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, shadows =
+        SettingsShadowSystemProperties.class)
+public class BluetoothOnWhileDrivingPreferenceControllerTest {
+    private BluetoothOnWhileDrivingPreferenceController mController;
+    private Context mContext;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mContext = RuntimeEnvironment.application;
+        mController = new BluetoothOnWhileDrivingPreferenceController(mContext);
+    }
+
+    @After
+    public void teardown() {
+        SettingsShadowSystemProperties.clear();
+    }
+
+    @Test
+    public void getAvailabilityStatus_onWhenEnabled() {
+        FeatureFlagUtils.setEnabled(mContext, FeatureFlags.BLUETOOTH_WHILE_DRIVING, true);
+
+        assertThat(mController.getAvailabilityStatus())
+                .isEqualTo(BasePreferenceController.AVAILABLE);
+    }
+
+    @Test
+    public void getAvailabilityStatus_offWhenDisabled() {
+        assertThat(mController.getAvailabilityStatus())
+                .isEqualTo(BasePreferenceController.DISABLED_UNSUPPORTED);
+    }
+
+    @Test
+    public void setChecked_togglesSettingSecure() {
+        mController.setChecked(true);
+
+        assertThat(
+                Settings.Secure.getInt(
+                        mContext.getContentResolver(),
+                        Settings.Secure.BLUETOOTH_ON_WHILE_DRIVING,
+                        0))
+                .isEqualTo(1);
+    }
+}