Merge "Import translations. DO NOT MERGE" into pi-dev
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 44e7e98..062648a 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -5321,8 +5321,6 @@
     <string name="battery_detail_background">While in background</string>
     <!-- Title for battery usage amount by this app. [CHAR LIMIT=80] -->
     <string name="battery_detail_power_usage">Battery usage</string>
-    <!-- Description for battery usage amount, i.e. 16% of overall app usage(340 mAh). [CHAR LIMIT=120] -->
-    <string name="battery_detail_power_percentage"><xliff:g id="percent">%1$s</xliff:g> of total app usage (<xliff:g id="power">%2$d</xliff:g>mAh)</string>
     <!-- Title for the battery usage group, which means all the battery data are calculated 'since full charge' [CHAR LIMIT=40] -->
     <string name ="battery_detail_info_title">Since full charge</string>
     <!-- Title for the battery management group [CHAR LIMIT=40] -->
diff --git a/res/xml/connected_devices.xml b/res/xml/connected_devices.xml
index 4a3c4f2..5b371fa 100644
--- a/res/xml/connected_devices.xml
+++ b/res/xml/connected_devices.xml
@@ -21,8 +21,14 @@
     android:title="@string/connected_devices_dashboard_title">
 
     <PreferenceCategory
+        android:key="available_device_list"
+        android:title="@string/connected_device_available_media_title"
+        settings:controller="com.android.settings.connecteddevice.AvailableMediaDeviceGroupController"/>
+
+    <PreferenceCategory
         android:key="connected_device_list"
-        android:title="@string/connected_device_connected_title"/>
+        android:title="@string/connected_device_connected_title"
+        settings:controller="com.android.settings.connecteddevice.ConnectedDeviceGroupController"/>
 
     <com.android.settingslib.RestrictedPreference
         android:key="add_bt_devices"
diff --git a/res/xml/power_usage_detail.xml b/res/xml/power_usage_detail.xml
index acc62b6..2be2a52 100644
--- a/res/xml/power_usage_detail.xml
+++ b/res/xml/power_usage_detail.xml
@@ -61,11 +61,6 @@
             android:title="@string/battery_detail_background"
             android:selectable="false"/>
 
-        <Preference
-            android:key="app_power_usage"
-            android:title="@string/battery_detail_power_usage"
-            android:selectable="false"/>
-
     </PreferenceCategory>
 
 </PreferenceScreen>
\ No newline at end of file
diff --git a/res/xml/sound_settings.xml b/res/xml/sound_settings.xml
index 735b3b7..7ba1c76 100644
--- a/res/xml/sound_settings.xml
+++ b/res/xml/sound_settings.xml
@@ -30,6 +30,14 @@
         android:order="-180"
         settings:controller="com.android.settings.notification.MediaVolumePreferenceController"/>
 
+    <!-- Media output switcher -->
+    <ListPreference
+        android:key="media_output"
+        android:title="@string/media_output_title"
+        android:dialogTitle="@string/media_output_title"
+        android:order="-175"
+        settings:controller="com.android.settings.sound.MediaOutputPreferenceController"/>
+
     <!-- Ring volume -->
     <com.android.settings.notification.VolumeSeekBarPreference
         android:key="ring_volume"
@@ -45,6 +53,14 @@
         android:title="@string/vibrate_when_ringing_title"
         android:order="-160"/>
 
+    <!-- Hands free profile output switcher -->
+    <ListPreference
+        android:key="take_call_on_output"
+        android:title="@string/take_call_on_title"
+        android:dialogTitle="@string/take_call_on_title"
+        android:order="-155"
+        settings:controller="com.android.settings.sound.HandsFreeProfileOutputPreferenceController"/>
+
     <!-- Alarm volume -->
     <com.android.settings.notification.VolumeSeekBarPreference
         android:key="alarm_volume"
diff --git a/src/com/android/settings/Settings.java b/src/com/android/settings/Settings.java
index e5df27a..0ad964b 100644
--- a/src/com/android/settings/Settings.java
+++ b/src/com/android/settings/Settings.java
@@ -154,6 +154,7 @@
         }
     }
     public static class WebViewAppPickerActivity extends SettingsActivity { /* empty */ }
+    public static class AdvancedConnectedDeviceActivity extends SettingsActivity { /* empty */ }
 
     // Top level categories for new IA
     public static class NetworkDashboardActivity extends SettingsActivity {}
@@ -163,6 +164,5 @@
     public static class StorageDashboardActivity extends SettingsActivity {}
     public static class AccountDashboardActivity extends SettingsActivity {}
     public static class SystemDashboardActivity extends SettingsActivity {}
-    public static class AdvancedConnectedDeviceActivity extends SettingsActivity {}
 
 }
diff --git a/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdater.java b/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdater.java
new file mode 100644
index 0000000..1e4b693
--- /dev/null
+++ b/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdater.java
@@ -0,0 +1,118 @@
+/*
+ * 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.bluetooth;
+
+import android.bluetooth.BluetoothProfile;
+import android.bluetooth.BluetoothAdapter;
+import android.content.Context;
+import android.media.AudioManager;
+import android.support.annotation.VisibleForTesting;
+import android.util.Log;
+import com.android.settings.connecteddevice.DevicePreferenceCallback;
+import com.android.settings.dashboard.DashboardFragment;
+import com.android.settingslib.bluetooth.LocalBluetoothManager;
+import com.android.settingslib.bluetooth.CachedBluetoothDevice;
+
+/**
+ * Controller to maintain available media Bluetooth devices
+ */
+public class AvailableMediaBluetoothDeviceUpdater extends BluetoothDeviceUpdater {
+
+    private static final String TAG = "AvailableMediaBluetoothDeviceUpdater";
+    private static final boolean DBG = false;
+
+    private final AudioManager mAudioManager;
+
+    public AvailableMediaBluetoothDeviceUpdater(Context context, DashboardFragment fragment,
+            DevicePreferenceCallback devicePreferenceCallback) {
+        super(context, fragment, devicePreferenceCallback);
+        mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
+    }
+
+    @VisibleForTesting
+    AvailableMediaBluetoothDeviceUpdater(DashboardFragment fragment,
+            DevicePreferenceCallback devicePreferenceCallback,
+            LocalBluetoothManager localBluetoothManager) {
+        super(fragment, devicePreferenceCallback, localBluetoothManager);
+        mAudioManager = (AudioManager) fragment.getContext().
+                getSystemService(Context.AUDIO_SERVICE);
+    }
+
+    @Override
+    public void onAudioModeChanged() {
+        forceUpdate();
+    }
+
+    @Override
+    public void onConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state) {
+        if (DBG) {
+            Log.d(TAG,"onConnectionStateChanged() device : " +
+                    cachedDevice.getName() + ", state : " + state);
+        }
+        if (state == BluetoothAdapter.STATE_CONNECTED) {
+            if (isFilterMatched(cachedDevice)) {
+                addPreference(cachedDevice);
+            } else {
+                removePreference(cachedDevice);
+            }
+        } else if (state == BluetoothAdapter.STATE_DISCONNECTED) {
+            removePreference(cachedDevice);
+        }
+    }
+
+    @Override
+    public boolean isFilterMatched(CachedBluetoothDevice cachedDevice) {
+        final int audioMode = mAudioManager.getMode();
+        final int currentAudioProfile;
+
+        if (audioMode == AudioManager.MODE_RINGTONE
+                || audioMode == AudioManager.MODE_IN_CALL
+                || audioMode == AudioManager.MODE_IN_COMMUNICATION) {
+            // in phone call
+            currentAudioProfile = BluetoothProfile.HEADSET;
+        } else {
+            // without phone call
+            currentAudioProfile = BluetoothProfile.A2DP;
+        }
+
+        boolean isFilterMatched = false;
+        if (isDeviceConnected(cachedDevice)) {
+            if (DBG) {
+                Log.d(TAG, "isFilterMatched() current audio profile : " + currentAudioProfile);
+            }
+            // According to the current audio profile type,
+            // this page will show the bluetooth device that have corresponding profile.
+            // For example:
+            // If current audio profile is a2dp, show the bluetooth device that have a2dp profile.
+            // If current audio profile is headset,
+            // show the bluetooth device that have headset profile.
+            switch (currentAudioProfile) {
+                case BluetoothProfile.A2DP:
+                    isFilterMatched = cachedDevice.isA2dpDevice();
+                    break;
+                case BluetoothProfile.HEADSET:
+                    isFilterMatched = cachedDevice.isHfpDevice();
+                    break;
+            }
+            if (DBG) {
+                Log.d(TAG, "isFilterMatched() device : " +
+                        cachedDevice.getName() + ", isFilterMatched : " + isFilterMatched);
+            }
+        }
+        return isFilterMatched;
+    }
+}
+
diff --git a/src/com/android/settings/bluetooth/BluetoothDeviceUpdater.java b/src/com/android/settings/bluetooth/BluetoothDeviceUpdater.java
index f71d27a..595d951 100644
--- a/src/com/android/settings/bluetooth/BluetoothDeviceUpdater.java
+++ b/src/com/android/settings/bluetooth/BluetoothDeviceUpdater.java
@@ -28,17 +28,15 @@
 import com.android.settings.core.SubSettingLauncher;
 import com.android.settings.dashboard.DashboardFragment;
 import com.android.settings.widget.GearPreference;
-import com.android.settingslib.bluetooth.A2dpProfile;
 import com.android.settingslib.bluetooth.BluetoothCallback;
 import com.android.settingslib.bluetooth.BluetoothDeviceFilter;
 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
-import com.android.settingslib.bluetooth.HeadsetProfile;
 import com.android.settingslib.bluetooth.LocalBluetoothManager;
+import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
 
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
-import java.util.Objects;
 
 /**
  * Update the bluetooth devices. It gets bluetooth event from {@link LocalBluetoothManager} using
@@ -48,7 +46,8 @@
  * In {@link BluetoothDeviceUpdater}, it uses {@link BluetoothDeviceFilter.Filter} to detect
  * whether the {@link CachedBluetoothDevice} is relevant.
  */
-public abstract class BluetoothDeviceUpdater implements BluetoothCallback {
+public abstract class BluetoothDeviceUpdater implements BluetoothCallback,
+        LocalBluetoothProfileManager.ServiceListener {
     private static final String TAG = "BluetoothDeviceUpdater";
     private static final String BLUETOOTH_SHOW_DEVICES_WITHOUT_NAMES_PROPERTY =
             "persist.bluetooth.showdeviceswithoutnames";
@@ -116,6 +115,7 @@
     public void registerCallback() {
         mLocalManager.setForegroundActivity(mFragment.getContext());
         mLocalManager.getEventManager().registerCallback(this);
+        mLocalManager.getProfileManager().addServiceListener(this);
         forceUpdate();
     }
 
@@ -125,6 +125,7 @@
     public void unregisterCallback() {
         mLocalManager.setForegroundActivity(null);
         mLocalManager.getEventManager().unregisterCallback(this);
+        mLocalManager.getProfileManager().removeServiceListener(this);
     }
 
     /**
@@ -175,6 +176,17 @@
     public void onAudioModeChanged() {
     }
 
+    @Override
+    public void onServiceConnected() {
+        // When bluetooth service connected update the UI
+        forceUpdate();
+    }
+
+    @Override
+    public void onServiceDisconnected() {
+
+    }
+
     /**
      * Set the context to generate the {@link Preference}, so it could get the correct theme.
      */
@@ -226,4 +238,16 @@
             mPreferenceMap.remove(device);
         }
     }
+
+    /**
+     * @return {@code true} if {@code cachedBluetoothDevice} is connected
+     * and the bond state is bonded.
+     */
+    public boolean isDeviceConnected(CachedBluetoothDevice cachedDevice) {
+        if (cachedDevice == null) {
+            return false;
+        }
+        final BluetoothDevice device = cachedDevice.getDevice();
+        return device.getBondState() == BluetoothDevice.BOND_BONDED && device.isConnected();
+    }
 }
diff --git a/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdater.java b/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdater.java
index 3b5c8b0..55f4bb1 100644
--- a/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdater.java
+++ b/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdater.java
@@ -16,10 +16,11 @@
 package com.android.settings.bluetooth;
 
 import android.bluetooth.BluetoothAdapter;
-import android.bluetooth.BluetoothDevice;
+import android.bluetooth.BluetoothProfile;
 import android.content.Context;
+import android.media.AudioManager;
 import android.support.annotation.VisibleForTesting;
-
+import android.util.Log;
 import com.android.settings.connecteddevice.DevicePreferenceCallback;
 import com.android.settings.dashboard.DashboardFragment;
 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
@@ -30,9 +31,15 @@
  */
 public class ConnectedBluetoothDeviceUpdater extends BluetoothDeviceUpdater {
 
+    private static final String TAG = "ConnBluetoothDeviceUpdater";
+    private static final boolean DBG = false;
+
+    private final AudioManager mAudioManager;
+
     public ConnectedBluetoothDeviceUpdater(Context context, DashboardFragment fragment,
             DevicePreferenceCallback devicePreferenceCallback) {
         super(context, fragment, devicePreferenceCallback);
+        mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
     }
 
     @VisibleForTesting
@@ -40,12 +47,28 @@
             DevicePreferenceCallback devicePreferenceCallback,
             LocalBluetoothManager localBluetoothManager) {
         super(fragment, devicePreferenceCallback, localBluetoothManager);
+        mAudioManager = (AudioManager) fragment.getContext().
+                getSystemService(Context.AUDIO_SERVICE);
+    }
+
+    @Override
+    public void onAudioModeChanged() {
+        forceUpdate();
     }
 
     @Override
     public void onConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state) {
+        if (DBG) {
+            Log.d(TAG,"onConnectionStateChanged() device : " +
+                    cachedDevice.getName() + ", state : " + state);
+        }
+
         if (state == BluetoothAdapter.STATE_CONNECTED) {
-            addPreference(cachedDevice);
+            if (isFilterMatched(cachedDevice)) {
+                addPreference(cachedDevice);
+            } else {
+                removePreference(cachedDevice);
+            }
         } else if (state == BluetoothAdapter.STATE_DISCONNECTED) {
             removePreference(cachedDevice);
         }
@@ -53,7 +76,44 @@
 
     @Override
     public boolean isFilterMatched(CachedBluetoothDevice cachedDevice) {
-        final BluetoothDevice device = cachedDevice.getDevice();
-        return device.getBondState() == BluetoothDevice.BOND_BONDED && device.isConnected();
+        final int audioMode = mAudioManager.getMode();
+        final int currentAudioProfile;
+
+        if (audioMode == AudioManager.MODE_RINGTONE
+                || audioMode == AudioManager.MODE_IN_CALL
+                || audioMode == AudioManager.MODE_IN_COMMUNICATION) {
+            // in phone call
+            currentAudioProfile = BluetoothProfile.HEADSET;
+        } else {
+            // without phone call
+            currentAudioProfile = BluetoothProfile.A2DP;
+        }
+
+        boolean isFilterMatched = false;
+        if (isDeviceConnected(cachedDevice)) {
+            if (DBG) {
+                Log.d(TAG, "isFilterMatched() current audio profile : " + currentAudioProfile);
+            }
+            // According to the current audio profile type,
+            // this page will show the bluetooth device that doesn't have corresponding profile.
+            // For example:
+            // If current audio profile is a2dp,
+            // show the bluetooth device that doesn't have a2dp profile.
+            // If current audio profile is headset,
+            // show the bluetooth device that doesn't have headset profile.
+            switch (currentAudioProfile) {
+                case BluetoothProfile.A2DP:
+                    isFilterMatched = !cachedDevice.isA2dpDevice();
+                    break;
+                case BluetoothProfile.HEADSET:
+                    isFilterMatched = !cachedDevice.isHfpDevice();
+                    break;
+            }
+            if (DBG) {
+                Log.d(TAG, "isFilterMatched() device : " +
+                        cachedDevice.getName() + ", isFilterMatched : " + isFilterMatched);
+            }
+        }
+        return isFilterMatched;
     }
 }
diff --git a/src/com/android/settings/connecteddevice/AvailableMediaDeviceGroupController.java b/src/com/android/settings/connecteddevice/AvailableMediaDeviceGroupController.java
new file mode 100644
index 0000000..fd14d47
--- /dev/null
+++ b/src/com/android/settings/connecteddevice/AvailableMediaDeviceGroupController.java
@@ -0,0 +1,108 @@
+/*
+ * 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.pm.PackageManager;
+import android.support.annotation.VisibleForTesting;
+import android.support.v7.preference.Preference;
+import android.support.v7.preference.PreferenceGroup;
+import android.support.v7.preference.PreferenceScreen;
+import com.android.settings.bluetooth.AvailableMediaBluetoothDeviceUpdater;
+import com.android.settings.bluetooth.BluetoothDeviceUpdater;
+import com.android.settings.core.BasePreferenceController;
+import com.android.settings.dashboard.DashboardFragment;
+import com.android.settingslib.core.lifecycle.LifecycleObserver;
+import com.android.settingslib.core.lifecycle.events.OnStart;
+import com.android.settingslib.core.lifecycle.events.OnStop;
+
+/**
+ * Controller to maintain the {@link android.support.v7.preference.PreferenceGroup} for all
+ * available media devices. It uses {@link DevicePreferenceCallback}
+ * to add/remove {@link Preference}
+ */
+public class AvailableMediaDeviceGroupController extends BasePreferenceController
+        implements LifecycleObserver, OnStart, OnStop, DevicePreferenceCallback {
+
+    private static final String KEY = "available_device_list";
+
+    @VisibleForTesting
+    PreferenceGroup mPreferenceGroup;
+    private BluetoothDeviceUpdater mBluetoothDeviceUpdater;
+
+    public AvailableMediaDeviceGroupController(Context context) {
+        super(context, KEY);
+    }
+
+    @Override
+    public void onStart() {
+        mBluetoothDeviceUpdater.registerCallback();
+    }
+
+    @Override
+    public void onStop() {
+        mBluetoothDeviceUpdater.unregisterCallback();
+    }
+
+    @Override
+    public void displayPreference(PreferenceScreen screen) {
+        super.displayPreference(screen);
+        if (isAvailable()) {
+            mPreferenceGroup = (PreferenceGroup) screen.findPreference(KEY);
+            mPreferenceGroup.setVisible(false);
+            mBluetoothDeviceUpdater.setPrefContext(screen.getContext());
+            mBluetoothDeviceUpdater.forceUpdate();
+        }
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)
+                ? AVAILABLE
+                : DISABLED_UNSUPPORTED;
+    }
+
+    @Override
+    public String getPreferenceKey() {
+        return KEY;
+    }
+
+    @Override
+    public void onDeviceAdded(Preference preference) {
+        if (mPreferenceGroup.getPreferenceCount() == 0) {
+            mPreferenceGroup.setVisible(true);
+        }
+        mPreferenceGroup.addPreference(preference);
+    }
+
+    @Override
+    public void onDeviceRemoved(Preference preference) {
+        mPreferenceGroup.removePreference(preference);
+        if (mPreferenceGroup.getPreferenceCount() == 0) {
+            mPreferenceGroup.setVisible(false);
+        }
+    }
+
+    public void init(DashboardFragment fragment) {
+        mBluetoothDeviceUpdater = new AvailableMediaBluetoothDeviceUpdater(fragment.getContext(),
+                fragment, AvailableMediaDeviceGroupController.this);
+    }
+
+    @VisibleForTesting
+    public void setBluetoothDeviceUpdater(BluetoothDeviceUpdater bluetoothDeviceUpdater) {
+        mBluetoothDeviceUpdater  = bluetoothDeviceUpdater;
+    }
+}
diff --git a/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragment.java b/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragment.java
index 2b55ab1..227fd9b 100644
--- a/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragment.java
+++ b/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragment.java
@@ -40,7 +40,7 @@
     @VisibleForTesting
     static final String KEY_CONNECTED_DEVICES = "connected_device_list";
     @VisibleForTesting
-    static final String KEY_SAVED_DEVICES = "saved_device_list";
+    static final String KEY_AVAILABLE_DEVICES = "available_device_list";
 
     @Override
     public int getMetricsCategory() {
@@ -64,14 +64,12 @@
 
     @Override
     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
-        return buildPreferenceControllers(context, getLifecycle(), this);
+        return buildPreferenceControllers(context, getLifecycle());
     }
 
     private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
-            Lifecycle lifecycle, DashboardFragment dashboardFragment) {
+            Lifecycle lifecycle) {
         final List<AbstractPreferenceController> controllers = new ArrayList<>();
-        controllers.add(new ConnectedDeviceGroupController(context, dashboardFragment, lifecycle));
-
         final NfcPreferenceController nfcPreferenceController =
                 new NfcPreferenceController(context);
         controllers.add(nfcPreferenceController);
@@ -83,6 +81,13 @@
         return controllers;
     }
 
+    @Override
+    public void onAttach(Context context) {
+        super.onAttach(context);
+        use(AvailableMediaDeviceGroupController.class).init(this);
+        use(ConnectedDeviceGroupController.class).init(this);
+    }
+
     @VisibleForTesting
     static class SummaryProvider implements SummaryLoader.SummaryProvider {
 
@@ -136,16 +141,15 @@
                 @Override
                 public List<AbstractPreferenceController> createPreferenceControllers(Context
                         context) {
-                    return buildPreferenceControllers(context, null /* lifecycle */,
-                            null /* dashboardFragment */);
+                    return buildPreferenceControllers(context, null /* lifecycle */);
                 }
 
                 @Override
                 public List<String> getNonIndexableKeys(Context context) {
                     List<String> keys = super.getNonIndexableKeys(context);
                     // Disable because they show dynamic data
+                    keys.add(KEY_AVAILABLE_DEVICES);
                     keys.add(KEY_CONNECTED_DEVICES);
-                    keys.add(KEY_SAVED_DEVICES);
                     return keys;
                 }
             };
diff --git a/src/com/android/settings/connecteddevice/ConnectedDeviceGroupController.java b/src/com/android/settings/connecteddevice/ConnectedDeviceGroupController.java
index 731acae..3a1aa05 100644
--- a/src/com/android/settings/connecteddevice/ConnectedDeviceGroupController.java
+++ b/src/com/android/settings/connecteddevice/ConnectedDeviceGroupController.java
@@ -48,19 +48,8 @@
     private BluetoothDeviceUpdater mBluetoothDeviceUpdater;
     private ConnectedUsbDeviceUpdater mConnectedUsbDeviceUpdater;
 
-    public ConnectedDeviceGroupController(Context context, DashboardFragment fragment,
-            Lifecycle lifecycle) {
+    public ConnectedDeviceGroupController(Context context) {
         super(context, KEY);
-        init(lifecycle, new ConnectedBluetoothDeviceUpdater(context, fragment, this),
-                new ConnectedUsbDeviceUpdater(context, fragment, this));
-    }
-
-    @VisibleForTesting
-    ConnectedDeviceGroupController(DashboardFragment fragment, Lifecycle lifecycle,
-            BluetoothDeviceUpdater bluetoothDeviceUpdater,
-            ConnectedUsbDeviceUpdater connectedUsbDeviceUpdater) {
-        super(fragment.getContext(), KEY);
-        init(lifecycle, bluetoothDeviceUpdater, connectedUsbDeviceUpdater);
     }
 
     @Override
@@ -116,12 +105,15 @@
         }
     }
 
-    private void init(Lifecycle lifecycle, BluetoothDeviceUpdater bluetoothDeviceUpdater,
+    @VisibleForTesting
+    public void init(BluetoothDeviceUpdater bluetoothDeviceUpdater,
             ConnectedUsbDeviceUpdater connectedUsbDeviceUpdater) {
-        if (lifecycle != null && isAvailable()) {
-            lifecycle.addObserver(this);
-        }
         mBluetoothDeviceUpdater = bluetoothDeviceUpdater;
         mConnectedUsbDeviceUpdater = connectedUsbDeviceUpdater;
     }
+
+    public void init(DashboardFragment fragment) {
+        init(new ConnectedBluetoothDeviceUpdater(fragment.getContext(), fragment, this),
+                new ConnectedUsbDeviceUpdater(fragment.getContext(), fragment, this));
+    }
 }
diff --git a/src/com/android/settings/connecteddevice/SavedDeviceGroupController.java b/src/com/android/settings/connecteddevice/SavedDeviceGroupController.java
index d045c9e..b1f8360 100644
--- a/src/com/android/settings/connecteddevice/SavedDeviceGroupController.java
+++ b/src/com/android/settings/connecteddevice/SavedDeviceGroupController.java
@@ -50,13 +50,6 @@
         super(context, KEY);
     }
 
-    @VisibleForTesting
-    SavedDeviceGroupController(DashboardFragment fragment,
-            BluetoothDeviceUpdater bluetoothDeviceUpdater) {
-        super(fragment.getContext(), KEY);
-        mBluetoothDeviceUpdater = bluetoothDeviceUpdater;
-    }
-
     @Override
     public void onStart() {
         mBluetoothDeviceUpdater.registerCallback();
@@ -109,4 +102,9 @@
         mBluetoothDeviceUpdater = new SavedBluetoothDeviceUpdater(fragment.getContext(),
                 fragment, SavedDeviceGroupController.this);
     }
+
+    @VisibleForTesting
+    public void setBluetoothDeviceUpdater(BluetoothDeviceUpdater bluetoothDeviceUpdater) {
+        mBluetoothDeviceUpdater  = bluetoothDeviceUpdater;
+    }
 }
diff --git a/src/com/android/settings/core/FeatureFlags.java b/src/com/android/settings/core/FeatureFlags.java
index a6e961b..e77c27b 100644
--- a/src/com/android/settings/core/FeatureFlags.java
+++ b/src/com/android/settings/core/FeatureFlags.java
@@ -25,4 +25,5 @@
     public static final String ABOUT_PHONE_V2 = "settings_about_phone_v2";
     public static final String BLUETOOTH_WHILE_DRIVING = "settings_bluetooth_while_driving";
     public static final String DATA_USAGE_SETTINGS_V2 = "settings_data_usage_v2";
+    public static final String AUDIO_SWITCHER_SETTINGS = "settings_audio_switcher";
 }
diff --git a/src/com/android/settings/fuelgauge/AdvancedPowerUsageDetail.java b/src/com/android/settings/fuelgauge/AdvancedPowerUsageDetail.java
index 0c82faa..683395e 100644
--- a/src/com/android/settings/fuelgauge/AdvancedPowerUsageDetail.java
+++ b/src/com/android/settings/fuelgauge/AdvancedPowerUsageDetail.java
@@ -87,7 +87,6 @@
 
     private static final String KEY_PREF_FOREGROUND = "app_usage_foreground";
     private static final String KEY_PREF_BACKGROUND = "app_usage_background";
-    private static final String KEY_PREF_POWER_USAGE = "app_power_usage";
     private static final String KEY_PREF_HEADER = "header_view";
 
     private static final int REQUEST_UNINSTALL = 0;
@@ -109,8 +108,6 @@
     @VisibleForTesting
     Preference mBackgroundPreference;
     @VisibleForTesting
-    Preference mPowerUsagePreference;
-    @VisibleForTesting
     AnomalySummaryPreferenceController mAnomalySummaryPreferenceController;
     private AppButtonsPreferenceController mAppButtonsPreferenceController;
     private BackgroundActivityPreferenceController mBackgroundActivityPreferenceController;
@@ -221,7 +218,6 @@
                 (SettingsActivity) getActivity(), this);
         mForegroundPreference = findPreference(KEY_PREF_FOREGROUND);
         mBackgroundPreference = findPreference(KEY_PREF_BACKGROUND);
-        mPowerUsagePreference = findPreference(KEY_PREF_POWER_USAGE);
         mHeaderPreference = (LayoutPreference) findPreference(KEY_PREF_HEADER);
 
         if (mPackageName != null) {
@@ -297,8 +293,6 @@
         mBackgroundPreference.setSummary(
                 TextUtils.expandTemplate(getText(R.string.battery_active_for),
                         StringUtil.formatElapsedTime(context, backgroundTimeMs, false)));
-        mPowerUsagePreference.setSummary(
-                getString(R.string.battery_detail_power_percentage, usagePercent, powerMah));
     }
 
     @Override
diff --git a/src/com/android/settings/sound/AudioSwitchPreferenceController.java b/src/com/android/settings/sound/AudioSwitchPreferenceController.java
new file mode 100644
index 0000000..25a0518
--- /dev/null
+++ b/src/com/android/settings/sound/AudioSwitchPreferenceController.java
@@ -0,0 +1,335 @@
+/*
+ * 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.sound;
+
+
+import static android.media.AudioManager.STREAM_DEVICES_CHANGED_ACTION;
+import static android.media.MediaRouter.ROUTE_TYPE_REMOTE_DISPLAY;
+
+import android.bluetooth.BluetoothDevice;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.media.AudioDeviceCallback;
+import android.media.AudioDeviceInfo;
+import android.media.AudioManager;
+import android.media.MediaRouter;
+import android.media.MediaRouter.Callback;
+import android.os.Handler;
+import android.os.Looper;
+import android.support.v7.preference.ListPreference;
+import android.support.v7.preference.Preference;
+import android.support.v7.preference.PreferenceScreen;
+import android.text.TextUtils;
+import android.util.FeatureFlagUtils;
+
+import com.android.internal.util.ArrayUtils;
+import com.android.settings.R;
+import com.android.settings.bluetooth.Utils;
+import com.android.settings.core.BasePreferenceController;
+import com.android.settings.core.FeatureFlags;
+import com.android.settingslib.bluetooth.BluetoothCallback;
+import com.android.settingslib.bluetooth.CachedBluetoothDevice;
+import com.android.settingslib.bluetooth.LocalBluetoothManager;
+import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
+import com.android.settingslib.core.lifecycle.LifecycleObserver;
+import com.android.settingslib.core.lifecycle.events.OnStart;
+import com.android.settingslib.core.lifecycle.events.OnStop;
+
+import java.util.List;
+
+/**
+ * Abstract class for audio switcher controller to notify subclass
+ * updating the current status of switcher entry. Subclasses must overwrite
+ * {@link #setActiveBluetoothDevice(BluetoothDevice)} to set the
+ * active device for corresponding profile.
+ */
+public abstract class AudioSwitchPreferenceController extends BasePreferenceController
+        implements Preference.OnPreferenceChangeListener, BluetoothCallback,
+        LifecycleObserver, OnStart, OnStop {
+
+    private static final int INVALID_INDEX = -1;
+
+    protected final AudioManager mAudioManager;
+    protected final MediaRouter mMediaRouter;
+    protected final LocalBluetoothProfileManager mProfileManager;
+    protected int mSelectedIndex;
+    protected Preference mPreference;
+    protected List<BluetoothDevice> mConnectedDevices;
+
+    private final AudioManagerAudioDeviceCallback mAudioManagerAudioDeviceCallback;
+    private final LocalBluetoothManager mLocalBluetoothManager;
+    private final MediaRouterCallback mMediaRouterCallback;
+    private final WiredHeadsetBroadcastReceiver mReceiver;
+    private final Handler mHandler;
+
+    public AudioSwitchPreferenceController(Context context, String preferenceKey) {
+        super(context, preferenceKey);
+        mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
+        mMediaRouter = (MediaRouter) context.getSystemService(Context.MEDIA_ROUTER_SERVICE);
+        mLocalBluetoothManager = Utils.getLocalBtManager(mContext);
+        mLocalBluetoothManager.setForegroundActivity(context);
+        mProfileManager = mLocalBluetoothManager.getProfileManager();
+        mHandler = new Handler(Looper.getMainLooper());
+        mAudioManagerAudioDeviceCallback = new AudioManagerAudioDeviceCallback();
+        mReceiver = new WiredHeadsetBroadcastReceiver();
+        mMediaRouterCallback = new MediaRouterCallback();
+    }
+
+    /**
+     * Make this method as final, ensure that subclass will checking
+     * the feature flag and they could mistakenly break it via overriding.
+     */
+    @Override
+    public final int getAvailabilityStatus() {
+        return FeatureFlagUtils.isEnabled(mContext, FeatureFlags.AUDIO_SWITCHER_SETTINGS)
+                ? AVAILABLE : DISABLED_UNSUPPORTED;
+    }
+
+    @Override
+    public boolean onPreferenceChange(Preference preference, Object newValue) {
+        final String address = (String) newValue;
+        if (!(preference instanceof ListPreference)) {
+            return false;
+        }
+
+        final ListPreference listPreference = (ListPreference) preference;
+        if (TextUtils.equals(address, mContext.getText(R.string.media_output_default_summary))) {
+            // Switch to default device which address is device name
+            mSelectedIndex = getDefaultDeviceIndex();
+            setActiveBluetoothDevice(null);
+            listPreference.setSummary(mContext.getText(R.string.media_output_default_summary));
+        } else {
+            // Switch to BT device which address is hardware address
+            final int connectedDeviceIndex = getConnectedDeviceIndex(address);
+            if (connectedDeviceIndex == INVALID_INDEX) {
+                return false;
+            }
+            final BluetoothDevice btDevice = mConnectedDevices.get(connectedDeviceIndex);
+            mSelectedIndex = connectedDeviceIndex;
+            setActiveBluetoothDevice(btDevice);
+            listPreference.setSummary(btDevice.getName());
+        }
+        return true;
+    }
+
+    public abstract void setActiveBluetoothDevice(BluetoothDevice device);
+
+    @Override
+    public void displayPreference(PreferenceScreen screen) {
+        super.displayPreference(screen);
+        mPreference = screen.findPreference(mPreferenceKey);
+    }
+
+    @Override
+    public void onStart() {
+        register();
+    }
+
+    @Override
+    public void onStop() {
+        unregister();
+    }
+
+    /**
+     * Only concerned about whether the local adapter is connected to any profile of any device and
+     * are not really concerned about which profile.
+     */
+    @Override
+    public void onConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state) {
+        updateState(mPreference);
+    }
+
+    @Override
+    public void onActiveDeviceChanged(CachedBluetoothDevice activeDevice, int bluetoothProfile) {
+        updateState(mPreference);
+    }
+
+    @Override
+    public void onAudioModeChanged() {
+        updateState(mPreference);
+    }
+
+    @Override
+    public void onBluetoothStateChanged(int bluetoothState) {
+    }
+
+    /**
+     * The local Bluetooth adapter has started the remote device discovery process.
+     */
+    @Override
+    public void onScanningStateChanged(boolean started) {
+    }
+
+    /**
+     * Indicates a change in the bond state of a remote
+     * device. For example, if a device is bonded (paired).
+     */
+    @Override
+    public void onDeviceAdded(CachedBluetoothDevice cachedDevice) {
+        updateState(mPreference);
+    }
+
+    @Override
+    public void onDeviceDeleted(CachedBluetoothDevice cachedDevice) {
+    }
+
+    @Override
+    public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
+    }
+
+    protected boolean isOngoingCallStatus() {
+        int audioMode = mAudioManager.getMode();
+        return audioMode == AudioManager.MODE_RINGTONE
+                || audioMode == AudioManager.MODE_IN_CALL
+                || audioMode == AudioManager.MODE_IN_COMMUNICATION;
+    }
+
+    int getDefaultDeviceIndex() {
+        // Default device is after all connected devices.
+        return ArrayUtils.size(mConnectedDevices);
+    }
+
+    void setupPreferenceEntries(CharSequence[] mediaOutputs, CharSequence[] mediaValues,
+            BluetoothDevice activeDevice) {
+        // default to current device
+        mSelectedIndex = getDefaultDeviceIndex();
+        // default device is after all connected devices.
+        mediaOutputs[mSelectedIndex] = mContext.getText(R.string.media_output_default_summary);
+        // use default device name as address
+        mediaValues[mSelectedIndex] = mContext.getText(R.string.media_output_default_summary);
+        for (int i = 0, size = mConnectedDevices.size(); i < size; i++) {
+            final BluetoothDevice btDevice = mConnectedDevices.get(i);
+            mediaOutputs[i] = btDevice.getName();
+            mediaValues[i] = btDevice.getAddress();
+            if (btDevice.equals(activeDevice)) {
+                // select the active connected device.
+                mSelectedIndex = i;
+            }
+        }
+    }
+
+    void setPreference(CharSequence[] mediaOutputs, CharSequence[] mediaValues,
+            Preference preference) {
+        final ListPreference listPreference = (ListPreference) preference;
+        listPreference.setEntries(mediaOutputs);
+        listPreference.setEntryValues(mediaValues);
+        listPreference.setValueIndex(mSelectedIndex);
+        listPreference.setSummary(mediaOutputs[mSelectedIndex]);
+    }
+
+    private int getConnectedDeviceIndex(String hardwareAddress) {
+        if (mConnectedDevices != null) {
+            for (int i = 0, size = mConnectedDevices.size(); i < size; i++) {
+                final BluetoothDevice btDevice = mConnectedDevices.get(i);
+                if (TextUtils.equals(btDevice.getAddress(), hardwareAddress)) {
+                    return i;
+                }
+            }
+        }
+        return INVALID_INDEX;
+    }
+
+    private void register() {
+        mLocalBluetoothManager.getEventManager().registerCallback(this);
+        mAudioManager.registerAudioDeviceCallback(mAudioManagerAudioDeviceCallback, mHandler);
+        mMediaRouter.addCallback(ROUTE_TYPE_REMOTE_DISPLAY, mMediaRouterCallback);
+
+        // Register for misc other intent broadcasts.
+        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
+        intentFilter.addAction(STREAM_DEVICES_CHANGED_ACTION);
+        mContext.registerReceiver(mReceiver, intentFilter);
+    }
+
+    private void unregister() {
+        mLocalBluetoothManager.getEventManager().unregisterCallback(this);
+        mAudioManager.unregisterAudioDeviceCallback(mAudioManagerAudioDeviceCallback);
+        mMediaRouter.removeCallback(mMediaRouterCallback);
+        mContext.unregisterReceiver(mReceiver);
+    }
+
+    /** Callback for headset plugged and unplugged events. */
+    private class AudioManagerAudioDeviceCallback extends AudioDeviceCallback {
+        @Override
+        public void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) {
+            updateState(mPreference);
+        }
+
+        @Override
+        public void onAudioDevicesRemoved(AudioDeviceInfo[] devices) {
+            updateState(mPreference);
+        }
+    }
+
+    /** Receiver for wired headset plugged and unplugged events. */
+    private class WiredHeadsetBroadcastReceiver extends BroadcastReceiver {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            final String action = intent.getAction();
+            if (AudioManager.ACTION_HEADSET_PLUG.equals(action) ||
+                    AudioManager.STREAM_DEVICES_CHANGED_ACTION.equals(action)) {
+                updateState(mPreference);
+            }
+        }
+    }
+
+    /** Callback for cast device events. */
+    private class MediaRouterCallback extends Callback {
+        @Override
+        public void onRouteSelected(MediaRouter router, int type, MediaRouter.RouteInfo info) {
+        }
+
+        @Override
+        public void onRouteUnselected(MediaRouter router, int type, MediaRouter.RouteInfo info) {
+        }
+
+        @Override
+        public void onRouteAdded(MediaRouter router, MediaRouter.RouteInfo info) {
+            if (info != null && !info.isDefault()) {
+                // cast mode
+                updateState(mPreference);
+            }
+        }
+
+        @Override
+        public void onRouteRemoved(MediaRouter router, MediaRouter.RouteInfo info) {
+        }
+
+        @Override
+        public void onRouteChanged(MediaRouter router, MediaRouter.RouteInfo info) {
+            if (info != null && !info.isDefault()) {
+                // cast mode
+                updateState(mPreference);
+            }
+        }
+
+        @Override
+        public void onRouteGrouped(MediaRouter router, MediaRouter.RouteInfo info,
+                MediaRouter.RouteGroup group, int index) {
+        }
+
+        @Override
+        public void onRouteUngrouped(MediaRouter router, MediaRouter.RouteInfo info,
+                MediaRouter.RouteGroup group) {
+        }
+
+        @Override
+        public void onRouteVolumeChanged(MediaRouter router, MediaRouter.RouteInfo info) {
+        }
+    }
+}
diff --git a/src/com/android/settings/sound/HandsFreeProfileOutputPreferenceController.java b/src/com/android/settings/sound/HandsFreeProfileOutputPreferenceController.java
new file mode 100644
index 0000000..b0b3dc5
--- /dev/null
+++ b/src/com/android/settings/sound/HandsFreeProfileOutputPreferenceController.java
@@ -0,0 +1,94 @@
+/*
+ * 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.sound;
+
+import android.bluetooth.BluetoothDevice;
+import android.content.Context;
+import android.support.v7.preference.Preference;
+
+import com.android.internal.util.ArrayUtils;
+import com.android.settings.R;
+import com.android.settingslib.bluetooth.HeadsetProfile;
+
+/**
+ * This class allows switching between HFP-connected BT devices
+ * while in on-call state.
+ */
+public class HandsFreeProfileOutputPreferenceController extends
+        AudioSwitchPreferenceController {
+
+    public HandsFreeProfileOutputPreferenceController(Context context, String key) {
+        super(context, key);
+    }
+
+    @Override
+    public void updateState(Preference preference) {
+        if (preference == null) {
+            // In case UI is not ready.
+            return;
+        }
+
+        if (!isOngoingCallStatus()) {
+            // Without phone call, disable the switch entry.
+            preference.setEnabled(false);
+            preference.setSummary(mContext.getText(R.string.media_output_default_summary));
+            return;
+        }
+
+        // Ongoing call status, list all the connected devices support hands free profile.
+        // Select current active device.
+        // Disable switch entry if there is no connected device.
+        mConnectedDevices = null;
+        BluetoothDevice activeDevice = null;
+
+        final HeadsetProfile headsetProfile = mProfileManager.getHeadsetProfile();
+        if (headsetProfile != null) {
+            mConnectedDevices = headsetProfile.getConnectedDevices();
+            activeDevice = headsetProfile.getActiveDevice();
+        }
+
+        final int numDevices = ArrayUtils.size(mConnectedDevices);
+        if (numDevices == 0) {
+            // No connected devices, disable switch entry.
+            preference.setEnabled(false);
+            preference.setSummary(mContext.getText(R.string.media_output_default_summary));
+            return;
+        }
+
+        preference.setEnabled(true);
+        CharSequence[] mediaOutputs = new CharSequence[numDevices + 1];
+        CharSequence[] mediaValues = new CharSequence[numDevices + 1];
+
+        // Setup devices entries, select active connected device
+        setupPreferenceEntries(mediaOutputs, mediaValues, activeDevice);
+
+        if (mAudioManager.isWiredHeadsetOn() && !mAudioManager.isBluetoothScoOn()) {
+            // If wired headset is plugged in and active, select to default device.
+            mSelectedIndex = getDefaultDeviceIndex();
+        }
+
+        // Display connected devices, default device and show the active device
+        setPreference(mediaOutputs, mediaValues, preference);
+    }
+
+    @Override
+    public void setActiveBluetoothDevice(BluetoothDevice device) {
+        if (isOngoingCallStatus()) {
+            mProfileManager.getHeadsetProfile().setActiveDevice(device);
+        }
+    }
+}
diff --git a/src/com/android/settings/sound/MediaOutputPreferenceController.java b/src/com/android/settings/sound/MediaOutputPreferenceController.java
new file mode 100644
index 0000000..2e52f77
--- /dev/null
+++ b/src/com/android/settings/sound/MediaOutputPreferenceController.java
@@ -0,0 +1,116 @@
+/*
+ * 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.sound;
+
+import static android.media.MediaRouter.ROUTE_TYPE_REMOTE_DISPLAY;
+
+import android.bluetooth.BluetoothDevice;
+import android.content.Context;
+import android.media.AudioManager;
+import android.media.MediaRouter;
+import android.support.v7.preference.Preference;
+
+import com.android.internal.util.ArrayUtils;
+import com.android.settings.R;
+import com.android.settingslib.bluetooth.A2dpProfile;
+
+
+/**
+ * This class which allows switching between a2dp-connected BT devices.
+ * A few conditions will disable this switcher:
+ * - No available BT device(s)
+ * - Media stream captured by cast device
+ * - During a call.
+ */
+public class MediaOutputPreferenceController extends AudioSwitchPreferenceController {
+
+    public MediaOutputPreferenceController(Context context, String key) {
+        super(context, key);
+    }
+
+    @Override
+    public void updateState(Preference preference) {
+        if (preference == null) {
+            // In case UI is not ready.
+            return;
+        }
+
+        if (mAudioManager.isMusicActiveRemotely() || isCastDevice(mMediaRouter)) {
+            // TODO(76455906): Workaround for cast mode, need a solid way to identify cast mode.
+            // In cast mode, disable switch entry.
+            preference.setEnabled(false);
+            preference.setSummary(mContext.getText(R.string.media_output_summary_unavailable));
+            return;
+        }
+
+        if (isOngoingCallStatus()) {
+            // Ongoing call status, switch entry for media will be disabled.
+            preference.setEnabled(false);
+            preference.setSummary(
+                    mContext.getText(R.string.media_out_summary_ongoing_call_state));
+            return;
+        }
+
+        // Otherwise, list all of the A2DP connected device and display the active device.
+        mConnectedDevices = null;
+        BluetoothDevice activeDevice = null;
+        if (mAudioManager.getMode() == AudioManager.MODE_NORMAL) {
+            final A2dpProfile a2dpProfile = mProfileManager.getA2dpProfile();
+            if (a2dpProfile != null) {
+                mConnectedDevices = a2dpProfile.getConnectedDevices();
+                activeDevice = a2dpProfile.getActiveDevice();
+            }
+        }
+
+        final int numDevices = ArrayUtils.size(mConnectedDevices);
+        if (numDevices == 0) {
+            // Disable switch entry if there is no connected devices.
+            preference.setEnabled(false);
+            preference.setSummary(mContext.getText(R.string.media_output_default_summary));
+            return;
+        }
+
+        preference.setEnabled(true);
+        CharSequence[] mediaOutputs = new CharSequence[numDevices + 1];
+        CharSequence[] mediaValues = new CharSequence[numDevices + 1];
+
+        // Setup devices entries, select active connected device
+        setupPreferenceEntries(mediaOutputs, mediaValues, activeDevice);
+
+        if (mAudioManager.isWiredHeadsetOn() && !mAudioManager.isBluetoothA2dpOn()) {
+            // If wired headset is plugged in and active, select to default device.
+            mSelectedIndex = getDefaultDeviceIndex();
+        }
+
+        // Display connected devices, default device and show the active device
+        setPreference(mediaOutputs, mediaValues, preference);
+    }
+
+    @Override
+    public void setActiveBluetoothDevice(BluetoothDevice device) {
+        if (mAudioManager.getMode() == AudioManager.MODE_NORMAL) {
+            mProfileManager.getA2dpProfile().setActiveDevice(device);
+        }
+    }
+
+    private static boolean isCastDevice(MediaRouter mediaRouter) {
+        final MediaRouter.RouteInfo selected = mediaRouter.getSelectedRoute(
+                ROUTE_TYPE_REMOTE_DISPLAY);
+        return selected != null && selected.getPresentationDisplay() != null
+                && selected.getPresentationDisplay().isValid();
+    }
+}
diff --git a/tests/robotests/assets/grandfather_invalid_base_preference_controller_constructor b/tests/robotests/assets/grandfather_invalid_base_preference_controller_constructor
index 7faa14a..c2b3da9 100644
--- a/tests/robotests/assets/grandfather_invalid_base_preference_controller_constructor
+++ b/tests/robotests/assets/grandfather_invalid_base_preference_controller_constructor
@@ -5,8 +5,6 @@
 com.android.settings.applications.appinfo.InstantAppButtonsPreferenceController
 com.android.settings.bluetooth.BluetoothDeviceNamePreferenceController
 com.android.settings.bluetooth.BluetoothDeviceRenamePreferenceController
-com.android.settings.connecteddevice.ConnectedDeviceGroupController
-com.android.settings.connecteddevice.SavedDeviceGroupController
 com.android.settings.datausage.DataUsageSummaryPreferenceController
 com.android.settings.fuelgauge.RestrictAppPreferenceController
 com.android.settings.fuelgauge.batterysaver.AutoBatterySeekBarPreferenceController
diff --git a/tests/robotests/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdaterTest.java b/tests/robotests/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdaterTest.java
new file mode 100644
index 0000000..fbc698c
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdaterTest.java
@@ -0,0 +1,211 @@
+/*
+ * 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.bluetooth;
+
+import android.bluetooth.BluetoothAdapter;
+import android.bluetooth.BluetoothDevice;
+import android.content.Context;
+
+import android.media.AudioManager;
+import com.android.settings.connecteddevice.DevicePreferenceCallback;
+import com.android.settings.dashboard.DashboardFragment;
+import com.android.settings.testutils.SettingsRobolectricTestRunner;
+
+import com.android.settings.testutils.shadow.ShadowAudioManager;
+import com.android.settingslib.bluetooth.CachedBluetoothDevice;
+import com.android.settingslib.bluetooth.HeadsetProfile;
+import com.android.settingslib.bluetooth.LocalBluetoothManager;
+import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
+import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
+
+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;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(shadows = {ShadowAudioManager.class})
+public class AvailableMediaBluetoothDeviceUpdaterTest {
+    @Mock
+    private DashboardFragment mDashboardFragment;
+    @Mock
+    private DevicePreferenceCallback mDevicePreferenceCallback;
+    @Mock
+    private CachedBluetoothDevice mCachedBluetoothDevice;
+    @Mock
+    private BluetoothDevice mBluetoothDevice;
+    @Mock
+    private LocalBluetoothManager mLocalManager;
+    @Mock
+    private LocalBluetoothProfileManager mLocalBluetoothProfileManager;
+    @Mock
+    private HeadsetProfile mHeadsetProfile;
+    @Mock
+    private CachedBluetoothDeviceManager mCachedDeviceManager;
+
+    private Context mContext;
+    private AvailableMediaBluetoothDeviceUpdater mBluetoothDeviceUpdater;
+    private Collection<CachedBluetoothDevice> cachedDevices;
+    private ShadowAudioManager mShadowAudioManager;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+
+        mShadowAudioManager = ShadowAudioManager.getShadow();
+        mContext = RuntimeEnvironment.application;
+        doReturn(mContext).when(mDashboardFragment).getContext();
+        cachedDevices =
+                new ArrayList<CachedBluetoothDevice>(new ArrayList<CachedBluetoothDevice>());
+
+        when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
+        when(mLocalManager.getProfileManager()).thenReturn(mLocalBluetoothProfileManager);
+        when(mLocalBluetoothProfileManager.getHeadsetProfile()).thenReturn(mHeadsetProfile);
+        when(mLocalManager.getCachedDeviceManager()).thenReturn(mCachedDeviceManager);
+        when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(cachedDevices);
+
+        mBluetoothDeviceUpdater = spy(new AvailableMediaBluetoothDeviceUpdater(mDashboardFragment,
+                mDevicePreferenceCallback, mLocalManager));
+        mBluetoothDeviceUpdater.setPrefContext(mContext);
+        doNothing().when(mBluetoothDeviceUpdater).addPreference(any());
+        doNothing().when(mBluetoothDeviceUpdater).removePreference(any());
+    }
+
+    @Test
+    public void onAudioModeChanged_hfpDeviceConnected_inCall_addPreference() {
+        mShadowAudioManager.setMode(AudioManager.MODE_IN_CALL);
+        when(mBluetoothDeviceUpdater.
+                isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
+        when(mCachedBluetoothDevice.isHfpDevice()).thenReturn(true);
+        cachedDevices.add(mCachedBluetoothDevice);
+
+        mBluetoothDeviceUpdater.onAudioModeChanged();
+
+        verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice);
+    }
+
+    @Test
+    public void onAudioModeChanged_hfpDeviceConnected_notInCall_removePreference() {
+        mShadowAudioManager.setMode(AudioManager.MODE_NORMAL);
+        when(mBluetoothDeviceUpdater.
+                isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
+        when(mCachedBluetoothDevice.isHfpDevice()).thenReturn(true);
+        cachedDevices.add(mCachedBluetoothDevice);
+
+        mBluetoothDeviceUpdater.onAudioModeChanged();
+
+        verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
+    }
+
+    @Test
+    public void onAudioModeChanged_a2dpDeviceConnected_inCall_removePreference() {
+        mShadowAudioManager.setMode(AudioManager.MODE_IN_CALL);
+        when(mBluetoothDeviceUpdater.
+                isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
+        when(mCachedBluetoothDevice.isA2dpDevice()).thenReturn(true);
+        cachedDevices.add(mCachedBluetoothDevice);
+
+        mBluetoothDeviceUpdater.onAudioModeChanged();
+
+        verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
+    }
+
+    @Test
+    public void onAudioModeChanged_a2dpDeviceConnected_notInCall_addPreference() {
+        mShadowAudioManager.setMode(AudioManager.MODE_NORMAL);
+        when(mBluetoothDeviceUpdater.
+                isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
+        when(mCachedBluetoothDevice.isA2dpDevice()).thenReturn(true);
+        cachedDevices.add(mCachedBluetoothDevice);
+
+        mBluetoothDeviceUpdater.onAudioModeChanged();
+
+        verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice);
+    }
+
+    @Test
+    public void onConnectionStateChanged_a2dpDeviceConnected_notInCall_addPreference() {
+        mShadowAudioManager.setMode(AudioManager.MODE_NORMAL);
+        when(mBluetoothDeviceUpdater.
+                isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
+        when(mCachedBluetoothDevice.isA2dpDevice()).thenReturn(true);
+
+        mBluetoothDeviceUpdater.onConnectionStateChanged(mCachedBluetoothDevice,
+                BluetoothAdapter.STATE_CONNECTED);
+
+        verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice);
+    }
+
+    @Test
+    public void onConnectionStateChanged_a2dpDeviceConnected_inCall_removePreference() {
+        mShadowAudioManager.setMode(AudioManager.MODE_IN_CALL);
+        when(mBluetoothDeviceUpdater.
+                isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
+        when(mCachedBluetoothDevice.isA2dpDevice()).thenReturn(true);
+
+        mBluetoothDeviceUpdater.onConnectionStateChanged(mCachedBluetoothDevice,
+                BluetoothAdapter.STATE_CONNECTED);
+
+        verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
+    }
+    @Test
+    public void onConnectionStateChanged_hfpDeviceConnected_notInCall_removePreference() {
+        mShadowAudioManager.setMode(AudioManager.MODE_NORMAL);
+        when(mBluetoothDeviceUpdater.
+                isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
+        when(mCachedBluetoothDevice.isHfpDevice()).thenReturn(true);
+
+        mBluetoothDeviceUpdater.onConnectionStateChanged(mCachedBluetoothDevice,
+                BluetoothAdapter.STATE_CONNECTED);
+
+        verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
+    }
+
+    @Test
+    public void onConnectionStateChanged_hfpDeviceConnected_inCall_addPreference() {
+        mShadowAudioManager.setMode(AudioManager.MODE_IN_CALL);
+        when(mBluetoothDeviceUpdater.
+                isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
+        when(mCachedBluetoothDevice.isHfpDevice()).thenReturn(true);
+
+        mBluetoothDeviceUpdater.onConnectionStateChanged(mCachedBluetoothDevice,
+                BluetoothAdapter.STATE_CONNECTED);
+
+        verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice);
+    }
+
+    @Test
+    public void onConnectionStateChanged_deviceDisconnected_removePreference() {
+        mBluetoothDeviceUpdater.onConnectionStateChanged(mCachedBluetoothDevice,
+                BluetoothAdapter.STATE_DISCONNECTED);
+
+        verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
+    }
+}
+
diff --git a/tests/robotests/src/com/android/settings/bluetooth/BluetoothDeviceUpdaterTest.java b/tests/robotests/src/com/android/settings/bluetooth/BluetoothDeviceUpdaterTest.java
index 905924d..51afb44 100644
--- a/tests/robotests/src/com/android/settings/bluetooth/BluetoothDeviceUpdaterTest.java
+++ b/tests/robotests/src/com/android/settings/bluetooth/BluetoothDeviceUpdaterTest.java
@@ -23,6 +23,7 @@
 import static org.mockito.Mockito.when;
 
 import android.bluetooth.BluetoothDevice;
+import android.bluetooth.BluetoothProfile;
 import android.content.Context;
 import android.content.Intent;
 import android.support.v7.preference.Preference;
@@ -32,7 +33,11 @@
 import com.android.settings.dashboard.DashboardFragment;
 import com.android.settings.testutils.SettingsRobolectricTestRunner;
 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
+import com.android.settingslib.bluetooth.HeadsetProfile;
+import com.android.settingslib.bluetooth.A2dpProfile;
 
+import com.android.settingslib.bluetooth.LocalBluetoothManager;
+import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -54,6 +59,14 @@
     private BluetoothDevice mBluetoothDevice;
     @Mock
     private SettingsActivity mSettingsActivity;
+    @Mock
+    private LocalBluetoothManager mLocalManager;
+    @Mock
+    private LocalBluetoothProfileManager mLocalBluetoothProfileManager;
+    @Mock
+    private HeadsetProfile mHeadsetProfile;
+    @Mock
+    private A2dpProfile mA2dpProfile;
 
     private Context mContext;
     private BluetoothDeviceUpdater mBluetoothDeviceUpdater;
@@ -66,10 +79,14 @@
         mContext = RuntimeEnvironment.application;
         doReturn(mContext).when(mDashboardFragment).getContext();
         when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
+        when(mLocalManager.getProfileManager()).thenReturn(mLocalBluetoothProfileManager);
+        when(mLocalBluetoothProfileManager.getHeadsetProfile()).thenReturn(mHeadsetProfile);
+        when(mLocalBluetoothProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile);
 
         mPreference = new BluetoothDevicePreference(mContext, mCachedBluetoothDevice, false);
         mBluetoothDeviceUpdater =
-            new BluetoothDeviceUpdater(mDashboardFragment, mDevicePreferenceCallback, null) {
+            new BluetoothDeviceUpdater(mDashboardFragment, mDevicePreferenceCallback,
+                    mLocalManager) {
             @Override
             public boolean isFilterMatched(CachedBluetoothDevice cachedBluetoothDevice) {
                 return true;
@@ -134,4 +151,20 @@
         assertThat(intentCaptor.getValue().getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT))
                 .isEqualTo(BluetoothDeviceDetailsFragment.class.getName());
     }
+
+    @Test
+    public void isDeviceConnected_deviceConnected() {
+        doReturn(BluetoothDevice.BOND_BONDED).when(mBluetoothDevice).getBondState();
+        doReturn(true).when(mBluetoothDevice).isConnected();
+
+        assertThat(mBluetoothDeviceUpdater.isDeviceConnected(mCachedBluetoothDevice)).isTrue();
+    }
+
+    @Test
+    public void isDeviceConnected_deviceNotConnected() {
+        doReturn(BluetoothDevice.BOND_BONDED).when(mBluetoothDevice).getBondState();
+        doReturn(false).when(mBluetoothDevice).isConnected();
+
+        assertThat(mBluetoothDeviceUpdater.isDeviceConnected(mCachedBluetoothDevice)).isFalse();
+    }
 }
diff --git a/tests/robotests/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdaterTest.java b/tests/robotests/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdaterTest.java
index 036f09d..9d69f59 100644
--- a/tests/robotests/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdaterTest.java
+++ b/tests/robotests/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdaterTest.java
@@ -26,10 +26,16 @@
 import android.bluetooth.BluetoothDevice;
 import android.content.Context;
 
+import android.media.AudioManager;
 import com.android.settings.connecteddevice.DevicePreferenceCallback;
 import com.android.settings.dashboard.DashboardFragment;
 import com.android.settings.testutils.SettingsRobolectricTestRunner;
+import com.android.settings.testutils.shadow.ShadowAudioManager;
 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
+import com.android.settingslib.bluetooth.HeadsetProfile;
+import com.android.settingslib.bluetooth.LocalBluetoothManager;
+import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
+import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -37,10 +43,14 @@
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
+
+import java.util.ArrayList;
+import java.util.Collection;
 
 @RunWith(SettingsRobolectricTestRunner.class)
+@Config(shadows = {ShadowAudioManager.class})
 public class ConnectedBluetoothDeviceUpdaterTest {
-
     @Mock
     private DashboardFragment mDashboardFragment;
     @Mock
@@ -49,47 +59,103 @@
     private CachedBluetoothDevice mCachedBluetoothDevice;
     @Mock
     private BluetoothDevice mBluetoothDevice;
+    @Mock
+    private LocalBluetoothManager mLocalManager;
+    @Mock
+    private LocalBluetoothProfileManager mLocalBluetoothProfileManager;
+    @Mock
+    private HeadsetProfile mHeadsetProfile;
 
     private Context mContext;
     private ConnectedBluetoothDeviceUpdater mBluetoothDeviceUpdater;
 
+    @Mock
+    private CachedBluetoothDeviceManager mCachedDeviceManager;
+
+    private Collection<CachedBluetoothDevice> cachedDevices;
+    private ShadowAudioManager mShadowAudioManager;
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
 
+        mShadowAudioManager = ShadowAudioManager.getShadow();
         mContext = RuntimeEnvironment.application;
         doReturn(mContext).when(mDashboardFragment).getContext();
+        cachedDevices =
+                new ArrayList<CachedBluetoothDevice>(new ArrayList<CachedBluetoothDevice>());
+
         when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
+        when(mLocalManager.getProfileManager()).thenReturn(mLocalBluetoothProfileManager);
+        when(mLocalBluetoothProfileManager.getHeadsetProfile()).thenReturn(mHeadsetProfile);
+        when(mLocalManager.getCachedDeviceManager()).thenReturn(mCachedDeviceManager);
+        when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(cachedDevices);
 
         mBluetoothDeviceUpdater = spy(new ConnectedBluetoothDeviceUpdater(mDashboardFragment,
-                mDevicePreferenceCallback, null));
+                mDevicePreferenceCallback, mLocalManager));
         mBluetoothDeviceUpdater.setPrefContext(mContext);
         doNothing().when(mBluetoothDeviceUpdater).addPreference(any());
         doNothing().when(mBluetoothDeviceUpdater).removePreference(any());
     }
 
     @Test
-    public void testUpdate_filterMatch_addPreference() {
-        doReturn(BluetoothDevice.BOND_BONDED).when(mBluetoothDevice).getBondState();
-        doReturn(true).when(mBluetoothDevice).isConnected();
+    public void onAudioModeChanged_hfpDeviceConnected_notInCall_addPreference() {
+        mShadowAudioManager.setMode(AudioManager.MODE_NORMAL);
+        when(mBluetoothDeviceUpdater.
+                isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
+        when(mCachedBluetoothDevice.isHfpDevice()).thenReturn(true);
+        cachedDevices.add(mCachedBluetoothDevice);
 
-        mBluetoothDeviceUpdater.update(mCachedBluetoothDevice);
+        mBluetoothDeviceUpdater.onAudioModeChanged();
 
         verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice);
     }
 
     @Test
-    public void testUpdate_filterNotMatch_removePreference() {
-        doReturn(BluetoothDevice.BOND_NONE).when(mBluetoothDevice).getBondState();
-        doReturn(false).when(mBluetoothDevice).isConnected();
+    public void onAudioModeChanged_hfpDeviceConnected_inCall_removePreference() {
+        mShadowAudioManager.setMode(AudioManager.MODE_IN_CALL);
+        when(mBluetoothDeviceUpdater.
+                isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
+        when(mCachedBluetoothDevice.isHfpDevice()).thenReturn(true);
+        cachedDevices.add(mCachedBluetoothDevice);
 
-        mBluetoothDeviceUpdater.update(mCachedBluetoothDevice);
+        mBluetoothDeviceUpdater.onAudioModeChanged();
 
         verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
     }
 
     @Test
-    public void testOnConnectionStateChanged_deviceConnected_addPreference() {
+    public void onAudioModeChanged_a2dpDeviceConnected_notInCall_removePreference() {
+        mShadowAudioManager.setMode(AudioManager.MODE_NORMAL);
+        when(mBluetoothDeviceUpdater.
+                isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
+        when(mCachedBluetoothDevice.isA2dpDevice()).thenReturn(true);
+        cachedDevices.add(mCachedBluetoothDevice);
+
+        mBluetoothDeviceUpdater.onAudioModeChanged();
+
+        verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
+    }
+
+    @Test
+    public void onAudioModeChanged_a2dpDeviceConnected_inCall_addPreference() {
+        mShadowAudioManager.setMode(AudioManager.MODE_IN_CALL);
+        when(mBluetoothDeviceUpdater.
+                isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
+        when(mCachedBluetoothDevice.isA2dpDevice()).thenReturn(true);
+        cachedDevices.add(mCachedBluetoothDevice);
+
+        mBluetoothDeviceUpdater.onAudioModeChanged();
+
+        verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice);
+    }
+
+    @Test
+    public void onConnectionStateChanged_a2dpDeviceConnected_inCall_addPreference() {
+        mShadowAudioManager.setMode(AudioManager.MODE_IN_CALL);
+        when(mBluetoothDeviceUpdater.
+                isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
+        when(mCachedBluetoothDevice.isA2dpDevice()).thenReturn(true);
+
         mBluetoothDeviceUpdater.onConnectionStateChanged(mCachedBluetoothDevice,
                 BluetoothAdapter.STATE_CONNECTED);
 
@@ -97,7 +163,45 @@
     }
 
     @Test
-    public void testOnConnectionStateChanged_deviceDisconnected_removePreference() {
+    public void onConnectionStateChanged_a2dpDeviceConnected_notInCall_removePreference() {
+        mShadowAudioManager.setMode(AudioManager.MODE_NORMAL);
+        when(mBluetoothDeviceUpdater.
+                isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
+        when(mCachedBluetoothDevice.isA2dpDevice()).thenReturn(true);
+
+        mBluetoothDeviceUpdater.onConnectionStateChanged(mCachedBluetoothDevice,
+                BluetoothAdapter.STATE_CONNECTED);
+
+        verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
+    }
+    @Test
+    public void onConnectionStateChanged_hfpDeviceConnected_inCall_removePreference() {
+        mShadowAudioManager.setMode(AudioManager.MODE_IN_CALL);
+        when(mBluetoothDeviceUpdater.
+                isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
+        when(mCachedBluetoothDevice.isHfpDevice()).thenReturn(true);
+
+        mBluetoothDeviceUpdater.onConnectionStateChanged(mCachedBluetoothDevice,
+                BluetoothAdapter.STATE_CONNECTED);
+
+        verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
+    }
+
+    @Test
+    public void onConnectionStateChanged_hfpDeviceConnected_notInCall_addPreference() {
+        mShadowAudioManager.setMode(AudioManager.MODE_NORMAL);
+        when(mBluetoothDeviceUpdater.
+                isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
+        when(mCachedBluetoothDevice.isHfpDevice()).thenReturn(true);
+
+        mBluetoothDeviceUpdater.onConnectionStateChanged(mCachedBluetoothDevice,
+                BluetoothAdapter.STATE_CONNECTED);
+
+        verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice);
+    }
+
+    @Test
+    public void onConnectionStateChanged_deviceDisconnected_removePreference() {
         mBluetoothDeviceUpdater.onConnectionStateChanged(mCachedBluetoothDevice,
                 BluetoothAdapter.STATE_DISCONNECTED);
 
diff --git a/tests/robotests/src/com/android/settings/connecteddevice/AvailableMediaDeviceGroupControllerTest.java b/tests/robotests/src/com/android/settings/connecteddevice/AvailableMediaDeviceGroupControllerTest.java
new file mode 100644
index 0000000..4e6d5e5
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/connecteddevice/AvailableMediaDeviceGroupControllerTest.java
@@ -0,0 +1,154 @@
+/*
+ * 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.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.Matchers.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 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.AvailableMediaBluetoothDeviceUpdater;
+import com.android.settings.dashboard.DashboardFragment;
+import com.android.settings.testutils.SettingsRobolectricTestRunner;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Answers;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RuntimeEnvironment;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+public class AvailableMediaDeviceGroupControllerTest {
+
+    private static final String PREFERENCE_KEY_1 = "pref_key_1";
+
+    @Mock
+    private DashboardFragment mDashboardFragment;
+    @Mock
+    private AvailableMediaBluetoothDeviceUpdater mAvailableMediaBluetoothDeviceUpdater;
+    @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 AvailableMediaDeviceGroupController mAvailableMediaDeviceGroupController;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+
+        mContext = spy(RuntimeEnvironment.application);
+        mPreference = new Preference(mContext);
+        mPreference.setKey(PREFERENCE_KEY_1);
+        mPreferenceGroup = spy(new PreferenceScreen(mContext, null));
+        when(mPreferenceGroup.getPreferenceManager()).thenReturn(mPreferenceManager);
+        doReturn(mContext).when(mDashboardFragment).getContext();
+        doReturn(mPackageManager).when(mContext).getPackageManager();
+        doReturn(true).when(mPackageManager).hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);
+
+        mAvailableMediaDeviceGroupController = new AvailableMediaDeviceGroupController(mContext);
+        mAvailableMediaDeviceGroupController.
+                setBluetoothDeviceUpdater(mAvailableMediaBluetoothDeviceUpdater);
+        mAvailableMediaDeviceGroupController.mPreferenceGroup = mPreferenceGroup;
+    }
+
+    @Test
+    public void onDeviceAdded_firstAdd_becomeVisibleAndPreferenceAdded() {
+        mAvailableMediaDeviceGroupController.onDeviceAdded(mPreference);
+
+        assertThat(mPreferenceGroup.isVisible()).isTrue();
+        assertThat(mPreferenceGroup.findPreference(PREFERENCE_KEY_1)).isEqualTo(mPreference);
+    }
+
+    @Test
+    public void onDeviceRemoved_lastRemove_becomeInvisibleAndPreferenceRemoved() {
+        mPreferenceGroup.addPreference(mPreference);
+
+        mAvailableMediaDeviceGroupController.onDeviceRemoved(mPreference);
+
+        assertThat(mPreferenceGroup.isVisible()).isFalse();
+        assertThat(mPreferenceGroup.getPreferenceCount()).isEqualTo(0);
+    }
+
+    @Test
+    public void onDeviceRemoved_notLastRemove_stillVisible() {
+        mPreferenceGroup.setVisible(true);
+        mPreferenceGroup.addPreference(mPreference);
+        mPreferenceGroup.addPreference(new Preference(mContext));
+
+        mAvailableMediaDeviceGroupController.onDeviceRemoved(mPreference);
+
+        assertThat(mPreferenceGroup.isVisible()).isTrue();
+    }
+
+    @Test
+    public void displayPreference_becomeInvisible() {
+        doReturn(mPreferenceGroup).when(mPreferenceScreen).findPreference(anyString());
+
+        mAvailableMediaDeviceGroupController.displayPreference(mPreferenceScreen);
+
+        assertThat(mPreferenceGroup.isVisible()).isFalse();
+    }
+
+    @Test
+    public void testRegister() {
+        // register the callback in onStart()
+        mAvailableMediaDeviceGroupController.onStart();
+        verify(mAvailableMediaBluetoothDeviceUpdater).registerCallback();
+    }
+
+    @Test
+    public void testUnregister() {
+        // unregister the callback in onStop()
+        mAvailableMediaDeviceGroupController.onStop();
+        verify(mAvailableMediaBluetoothDeviceUpdater).unregisterCallback();
+    }
+
+    @Test
+    public void testGetAvailabilityStatus_noBluetoothFeature_returnUnSupported() {
+        doReturn(false).when(mPackageManager).hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);
+
+        assertThat(mAvailableMediaDeviceGroupController.getAvailabilityStatus()).isEqualTo(
+                DISABLED_UNSUPPORTED);
+    }
+
+    @Test
+    public void testGetAvailabilityStatus_BluetoothFeature_returnSupported() {
+        doReturn(true).when(mPackageManager).hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);
+
+        assertThat(mAvailableMediaDeviceGroupController.getAvailabilityStatus()).isEqualTo(
+                AVAILABLE);
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragmentTest.java b/tests/robotests/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragmentTest.java
index 443e885..0230540 100644
--- a/tests/robotests/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragmentTest.java
+++ b/tests/robotests/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragmentTest.java
@@ -16,9 +16,9 @@
 package com.android.settings.connecteddevice;
 
 import static com.android.settings.connecteddevice.ConnectedDeviceDashboardFragment
-        .KEY_CONNECTED_DEVICES;
+        .KEY_AVAILABLE_DEVICES;
 import static com.android.settings.connecteddevice.ConnectedDeviceDashboardFragment
-        .KEY_SAVED_DEVICES;
+        .KEY_CONNECTED_DEVICES;
 
 import static com.google.common.truth.Truth.assertThat;
 
@@ -78,7 +78,7 @@
         final List<String> niks = ConnectedDeviceDashboardFragment.SEARCH_INDEX_DATA_PROVIDER
                 .getNonIndexableKeys(mContext);
 
-        assertThat(niks).containsExactly(KEY_CONNECTED_DEVICES, KEY_SAVED_DEVICES,
+        assertThat(niks).containsExactly(KEY_CONNECTED_DEVICES, KEY_AVAILABLE_DEVICES,
                 NfcPreferenceController.KEY_TOGGLE_NFC);
     }
 }
diff --git a/tests/robotests/src/com/android/settings/connecteddevice/ConnectedDeviceGroupControllerTest.java b/tests/robotests/src/com/android/settings/connecteddevice/ConnectedDeviceGroupControllerTest.java
index 373c255..e3e11bc 100644
--- a/tests/robotests/src/com/android/settings/connecteddevice/ConnectedDeviceGroupControllerTest.java
+++ b/tests/robotests/src/com/android/settings/connecteddevice/ConnectedDeviceGroupControllerTest.java
@@ -15,6 +15,7 @@
  */
 package com.android.settings.connecteddevice;
 
+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;
@@ -45,8 +46,12 @@
 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.ShadowApplicationPackageManager;
 
 @RunWith(SettingsRobolectricTestRunner.class)
+@Config(shadows = ShadowApplicationPackageManager.class)
 public class ConnectedDeviceGroupControllerTest {
 
     private static final String PREFERENCE_KEY_1 = "pref_key_1";
@@ -61,33 +66,29 @@
     private PreferenceScreen mPreferenceScreen;
     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     private PreferenceManager mPreferenceManager;
-    @Mock
-    private PackageManager mPackageManager;
 
+    private ShadowApplicationPackageManager mPackageManager;
     private PreferenceGroup mPreferenceGroup;
     private Context mContext;
     private Preference mPreference;
     private ConnectedDeviceGroupController mConnectedDeviceGroupController;
-    private LifecycleOwner mLifecycleOwner;
-    private Lifecycle mLifecycle;
 
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
 
-        mContext = spy(RuntimeEnvironment.application);
+        mContext = RuntimeEnvironment.application;
         mPreference = new Preference(mContext);
         mPreference.setKey(PREFERENCE_KEY_1);
-        mLifecycleOwner = () -> mLifecycle;
-        mLifecycle = new Lifecycle(mLifecycleOwner);
+        mPackageManager = (ShadowApplicationPackageManager) Shadows.shadowOf(
+                mContext.getPackageManager());
         mPreferenceGroup = spy(new PreferenceScreen(mContext, null));
         when(mPreferenceGroup.getPreferenceManager()).thenReturn(mPreferenceManager);
         doReturn(mContext).when(mDashboardFragment).getContext();
-        doReturn(mPackageManager).when(mContext).getPackageManager();
-        doReturn(true).when(mPackageManager).hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);
 
-        mConnectedDeviceGroupController = new ConnectedDeviceGroupController(mDashboardFragment,
-                mLifecycle, mConnectedBluetoothDeviceUpdater, mConnectedUsbDeviceUpdater);
+        mConnectedDeviceGroupController = new ConnectedDeviceGroupController(mContext);
+        mConnectedDeviceGroupController
+                .init(mConnectedBluetoothDeviceUpdater, mConnectedUsbDeviceUpdater);
         mConnectedDeviceGroupController.mPreferenceGroup = mPreferenceGroup;
     }
 
@@ -130,23 +131,34 @@
     }
 
     @Test
-    public void testLifecycle() {
+    public void testRegister() {
         // register the callback in onStart()
-        mLifecycle.handleLifecycleEvent(android.arch.lifecycle.Lifecycle.Event.ON_START);
+        mConnectedDeviceGroupController.onStart();
         verify(mConnectedBluetoothDeviceUpdater).registerCallback();
         verify(mConnectedUsbDeviceUpdater).registerCallback();
+    }
 
+    @Test
+    public void testUnregister() {
         // unregister the callback in onStop()
-        mLifecycle.handleLifecycleEvent(android.arch.lifecycle.Lifecycle.Event.ON_STOP);
+        mConnectedDeviceGroupController.onStop();
         verify(mConnectedBluetoothDeviceUpdater).unregisterCallback();
         verify(mConnectedUsbDeviceUpdater).unregisterCallback();
     }
 
     @Test
     public void testGetAvailabilityStatus_noBluetoothFeature_returnUnSupported() {
-        doReturn(false).when(mPackageManager).hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);
+        mPackageManager.setSystemFeature(PackageManager.FEATURE_BLUETOOTH, false);
 
         assertThat(mConnectedDeviceGroupController.getAvailabilityStatus()).isEqualTo(
                 DISABLED_UNSUPPORTED);
     }
+
+    @Test
+    public void testGetAvailabilityStatus_BluetoothFeature_returnSupported() {
+        mPackageManager.setSystemFeature(PackageManager.FEATURE_BLUETOOTH, true);
+
+        assertThat(mConnectedDeviceGroupController.getAvailabilityStatus()).isEqualTo(
+                AVAILABLE);
+    }
 }
diff --git a/tests/robotests/src/com/android/settings/connecteddevice/SavedDeviceGroupControllerTest.java b/tests/robotests/src/com/android/settings/connecteddevice/SavedDeviceGroupControllerTest.java
index a0d3d7b..1b1b88d 100644
--- a/tests/robotests/src/com/android/settings/connecteddevice/SavedDeviceGroupControllerTest.java
+++ b/tests/robotests/src/com/android/settings/connecteddevice/SavedDeviceGroupControllerTest.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2018 The Android Open Source Project
+ * 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.
@@ -14,9 +14,11 @@
  * limitations under the License
  */
 package com.android.settings.connecteddevice;
+
 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.spy;
 import static org.mockito.Mockito.verify;
@@ -28,6 +30,7 @@
 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;
@@ -39,6 +42,7 @@
 @RunWith(SettingsRobolectricTestRunner.class)
 public class SavedDeviceGroupControllerTest {
     private static final String PREFERENCE_KEY_1 = "pref_key_1";
+
     @Mock
     private DashboardFragment mDashboardFragment;
     @Mock
@@ -47,10 +51,12 @@
     private PreferenceManager mPreferenceManager;
     @Mock
     private PackageManager mPackageManager;
+
     private Context mContext;
     private SavedDeviceGroupController mSavedDeviceGroupController;
     private LifecycleOwner mLifecycleOwner;
     private Lifecycle mLifecycle;
+
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
@@ -59,9 +65,10 @@
         mLifecycle = new Lifecycle(mLifecycleOwner);
         doReturn(mContext).when(mDashboardFragment).getContext();
         doReturn(mPackageManager).when(mContext).getPackageManager();
-        mSavedDeviceGroupController = new SavedDeviceGroupController(mDashboardFragment,
-                mBluetoothDeviceUpdater);
+        mSavedDeviceGroupController = new SavedDeviceGroupController(mContext);
+        mSavedDeviceGroupController.setBluetoothDeviceUpdater(mBluetoothDeviceUpdater);
     }
+
     @Test
     public void testRegister() {
         // register the callback in onStart()
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/AdvancedPowerUsageDetailTest.java b/tests/robotests/src/com/android/settings/fuelgauge/AdvancedPowerUsageDetailTest.java
index b7bcb95..0be6389 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/AdvancedPowerUsageDetailTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/AdvancedPowerUsageDetailTest.java
@@ -136,7 +136,6 @@
     private Context mContext;
     private Preference mForegroundPreference;
     private Preference mBackgroundPreference;
-    private Preference mPowerUsagePreference;
     private AdvancedPowerUsageDetail mFragment;
     private SettingsActivity mTestActivity;
     private List<Anomaly> mAnomalies;
@@ -214,10 +213,8 @@
 
         mForegroundPreference = new Preference(mContext);
         mBackgroundPreference = new Preference(mContext);
-        mPowerUsagePreference = new Preference(mContext);
         mFragment.mForegroundPreference = mForegroundPreference;
         mFragment.mBackgroundPreference = mBackgroundPreference;
-        mFragment.mPowerUsagePreference = mPowerUsagePreference;
         mFragment.mAnomalySummaryPreferenceController = mAnomalySummaryPreferenceController;
 
         mAnomalies = new ArrayList<>();
@@ -412,15 +409,11 @@
                 R.string.battery_used_for);
         doReturn(mContext.getText(R.string.battery_active_for)).when(mFragment).getText(
                 R.string.battery_active_for);
-        doReturn(mContext.getString(R.string.battery_detail_power_percentage, USAGE_PERCENT,
-                POWER_MAH)).when(mFragment)
-                .getString(R.string.battery_detail_power_percentage, USAGE_PERCENT, POWER_MAH);
 
         mFragment.initPreference();
 
         assertThat(mForegroundPreference.getSummary().toString()).isEqualTo("Used for 0 min");
         assertThat(mBackgroundPreference.getSummary().toString()).isEqualTo("Active for 0 min");
-        assertThat(mPowerUsagePreference.getSummary()).isEqualTo("16% of total app usage (150mAh)");
     }
 
     @Test
diff --git a/tests/robotests/src/com/android/settings/sound/AudioOutputSwitchPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/sound/AudioOutputSwitchPreferenceControllerTest.java
new file mode 100644
index 0000000..2168a2a
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/sound/AudioOutputSwitchPreferenceControllerTest.java
@@ -0,0 +1,251 @@
+/*
+ * 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.sound;
+
+
+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.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.bluetooth.BluetoothAdapter;
+import android.bluetooth.BluetoothDevice;
+import android.bluetooth.BluetoothManager;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.IntentFilter;
+import android.support.v7.preference.ListPreference;
+import android.support.v7.preference.PreferenceManager;
+import android.support.v7.preference.PreferenceScreen;
+import android.util.FeatureFlagUtils;
+
+import com.android.settings.R;
+import com.android.settings.core.FeatureFlags;
+import com.android.settings.testutils.SettingsRobolectricTestRunner;
+import com.android.settings.testutils.shadow.ShadowAudioManager;
+import com.android.settings.testutils.shadow.ShadowBluetoothUtils;
+import com.android.settings.testutils.shadow.ShadowMediaRouter;
+import com.android.settingslib.bluetooth.A2dpProfile;
+import com.android.settingslib.bluetooth.BluetoothCallback;
+import com.android.settingslib.bluetooth.BluetoothEventManager;
+import com.android.settingslib.bluetooth.CachedBluetoothDevice;
+import com.android.settingslib.bluetooth.LocalBluetoothManager;
+import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
+
+import org.junit.After;
+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.ShadowBluetoothDevice;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(shadows = {
+        ShadowAudioManager.class,
+        ShadowMediaRouter.class,
+        ShadowBluetoothUtils.class,
+        ShadowBluetoothDevice.class}
+)
+public class AudioOutputSwitchPreferenceControllerTest {
+    private static final String TEST_KEY = "Test_Key";
+    private static final String TEST_DEVICE_NAME_1 = "Test_A2DP_BT_Device_NAME_1";
+    private static final String TEST_DEVICE_NAME_2 = "Test_A2DP_BT_Device_NAME_2";
+    private static final String TEST_DEVICE_ADDRESS_1 = "00:07:80:78:A4:69";
+    private static final String TEST_DEVICE_ADDRESS_2 = "00:00:00:00:00:00";
+
+    @Mock
+    private LocalBluetoothManager mLocalManager;
+    @Mock
+    private BluetoothEventManager mBluetoothEventManager;
+    @Mock
+    private CachedBluetoothDevice mCachedBluetoothDevice;
+    @Mock
+    private LocalBluetoothProfileManager mLocalBluetoothProfileManager;
+    @Mock
+    private A2dpProfile mA2dpProfile;
+
+    private Context mContext;
+    private PreferenceScreen mScreen;
+    private ListPreference mPreference;
+    private ShadowAudioManager mShadowAudioManager;
+    private ShadowMediaRouter mShadowMediaRouter;
+    private BluetoothManager mBluetoothManager;
+    private BluetoothAdapter mBluetoothAdapter;
+    private BluetoothDevice mBluetoothDevice;
+    private ShadowBluetoothDevice mShadowBluetoothDevice;
+    private LocalBluetoothManager mLocalBluetoothManager;
+    private AudioSwitchPreferenceController mController;
+    private List<BluetoothDevice> mConnectedDevices;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mContext = spy(RuntimeEnvironment.application);
+
+        mShadowAudioManager = ShadowAudioManager.getShadow();
+        mShadowMediaRouter = ShadowMediaRouter.getShadow();
+
+        ShadowBluetoothUtils.sLocalBluetoothManager = mLocalManager;
+        mLocalBluetoothManager = ShadowBluetoothUtils.getLocalBtManager(mContext);
+
+        when(mLocalBluetoothManager.getEventManager()).thenReturn(mBluetoothEventManager);
+        when(mLocalBluetoothManager.getProfileManager()).thenReturn(mLocalBluetoothProfileManager);
+        when(mLocalBluetoothProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile);
+
+        mBluetoothManager = new BluetoothManager(mContext);
+        mBluetoothAdapter = mBluetoothManager.getAdapter();
+
+        mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(TEST_DEVICE_ADDRESS_1);
+        mShadowBluetoothDevice = Shadows.shadowOf(mBluetoothDevice);
+        mShadowBluetoothDevice.setName(TEST_DEVICE_NAME_1);
+        when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
+
+        mController = new AudioSwitchPreferenceControllerTestable(mContext, TEST_KEY);
+        mScreen = spy(new PreferenceScreen(mContext, null));
+        mPreference = new ListPreference(mContext);
+        mConnectedDevices = new ArrayList<>(1);
+        mConnectedDevices.add(mBluetoothDevice);
+
+        when(mScreen.getPreferenceManager()).thenReturn(mock(PreferenceManager.class));
+        when(mScreen.getContext()).thenReturn(mContext);
+        when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
+        mScreen.addPreference(mPreference);
+        mController.displayPreference(mScreen);
+    }
+
+    @After
+    public void tearDown() {
+        mShadowAudioManager.reset();
+        mShadowMediaRouter.reset();
+        ShadowBluetoothUtils.reset();
+    }
+
+    @Test
+    public void getAvailabilityStatus_byDefault_isAvailable() {
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
+    }
+
+    @Test
+    public void getAvailabilityStatus_whenNotVisible_isDisable() {
+        FeatureFlagUtils.setEnabled(mContext, FeatureFlags.AUDIO_SWITCHER_SETTINGS, false);
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_UNSUPPORTED);
+    }
+
+    @Test
+    public void onStart_shouldRegisterCallbackAndRegisterReceiver() {
+        mController.onStart();
+
+        verify(mLocalBluetoothManager.getEventManager()).registerCallback(
+                any(BluetoothCallback.class));
+        verify(mContext).registerReceiver(any(BroadcastReceiver.class), any(IntentFilter.class));
+    }
+
+    @Test
+    public void onStop_shouldUnregisterCallbackAndUnregisterReceiver() {
+        mController.onStart();
+        mController.onStop();
+
+        verify(mLocalBluetoothManager.getEventManager()).unregisterCallback(
+                any(BluetoothCallback.class));
+        verify(mContext).unregisterReceiver(any(BroadcastReceiver.class));
+    }
+
+    @Test
+    public void onPreferenceChange_toThisDevice_shouldSetDefaultSummary() {
+        mController.mConnectedDevices = mConnectedDevices;
+
+        mController.onPreferenceChange(mPreference,
+                mContext.getText(R.string.media_output_default_summary));
+
+        assertThat(mPreference.getSummary()).isEqualTo(
+                mContext.getText(R.string.media_output_default_summary));
+    }
+
+    /**
+     * One Bluetooth devices are available, and select the device.
+     * Preference summary should be device name.
+     */
+    @Test
+    public void onPreferenceChange_toBtDevice_shouldSetBtDeviceName() {
+        mController.mConnectedDevices = mConnectedDevices;
+
+        mController.onPreferenceChange(mPreference, TEST_DEVICE_ADDRESS_1);
+
+        assertThat(mPreference.getSummary()).isEqualTo(mBluetoothDevice.getName());
+    }
+
+    /**
+     * More than one Bluetooth devices are available, and select second device.
+     * Preference summary should be second device name.
+     */
+    @Test
+    public void onPreferenceChange_toBtDevices_shouldSetSecondBtDeviceName() {
+        ShadowBluetoothDevice shadowBluetoothDevice;
+        BluetoothDevice secondBluetoothDevice;
+        secondBluetoothDevice = mBluetoothAdapter.getRemoteDevice(TEST_DEVICE_ADDRESS_2);
+        shadowBluetoothDevice = Shadows.shadowOf(secondBluetoothDevice);
+        shadowBluetoothDevice.setName(TEST_DEVICE_NAME_2);
+        List<BluetoothDevice> connectedDevices = new ArrayList<>(2);
+        connectedDevices.add(mBluetoothDevice);
+        connectedDevices.add(secondBluetoothDevice);
+        mController.mConnectedDevices = connectedDevices;
+
+        mController.onPreferenceChange(mPreference, TEST_DEVICE_ADDRESS_2);
+
+        assertThat(mPreference.getSummary()).isEqualTo(secondBluetoothDevice.getName());
+    }
+
+    /**
+     * mConnectedDevices is Null.
+     * onPreferenceChange should return false.
+     */
+    @Test
+    public void onPreferenceChange_connectedDeviceIsNull_shouldReturnFalse() {
+        mController.mConnectedDevices = null;
+
+        assertThat(mController.onPreferenceChange(mPreference, TEST_DEVICE_ADDRESS_1)).isFalse();
+    }
+
+    private class AudioSwitchPreferenceControllerTestable extends
+            AudioSwitchPreferenceController {
+        AudioSwitchPreferenceControllerTestable(Context context, String key) {
+            super(context, key);
+        }
+
+        @Override
+        public void setActiveBluetoothDevice(BluetoothDevice device) {
+        }
+
+        @Override
+        public String getPreferenceKey() {
+            return null;
+        }
+    }
+}
\ No newline at end of file
diff --git a/tests/robotests/src/com/android/settings/sound/HandsFreeProfileOutputPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/sound/HandsFreeProfileOutputPreferenceControllerTest.java
new file mode 100644
index 0000000..c6c4b45
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/sound/HandsFreeProfileOutputPreferenceControllerTest.java
@@ -0,0 +1,240 @@
+/*
+ * 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.sound;
+
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.bluetooth.BluetoothAdapter;
+import android.bluetooth.BluetoothDevice;
+import android.bluetooth.BluetoothManager;
+import android.content.Context;
+import android.media.AudioManager;
+import android.support.v7.preference.ListPreference;
+import android.support.v7.preference.PreferenceManager;
+import android.support.v7.preference.PreferenceScreen;
+
+import com.android.settings.R;
+import com.android.settings.testutils.SettingsRobolectricTestRunner;
+import com.android.settings.testutils.shadow.ShadowAudioManager;
+import com.android.settings.testutils.shadow.ShadowBluetoothUtils;
+import com.android.settings.testutils.shadow.ShadowMediaRouter;
+import com.android.settingslib.bluetooth.BluetoothEventManager;
+import com.android.settingslib.bluetooth.CachedBluetoothDevice;
+import com.android.settingslib.bluetooth.HeadsetProfile;
+import com.android.settingslib.bluetooth.LocalBluetoothManager;
+import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
+
+import org.junit.After;
+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.ShadowBluetoothDevice;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(shadows = {
+        ShadowAudioManager.class,
+        ShadowMediaRouter.class,
+        ShadowBluetoothUtils.class,
+        ShadowBluetoothDevice.class}
+)
+public class HandsFreeProfileOutputPreferenceControllerTest {
+    private static final String TEST_KEY = "Test_Key";
+    private static final String TEST_DEVICE_NAME_1 = "Test_HAP_BT_Device_NAME_1";
+    private static final String TEST_DEVICE_NAME_2 = "Test_HAP_BT_Device_NAME_2";
+    private static final String TEST_DEVICE_ADDRESS_1 = "00:07:80:78:A4:69";
+    private static final String TEST_DEVICE_ADDRESS_2 = "00:00:00:00:00:00";
+
+    @Mock
+    private LocalBluetoothManager mLocalManager;
+    @Mock
+    private BluetoothEventManager mBluetoothEventManager;
+    @Mock
+    private CachedBluetoothDevice mCachedBluetoothDevice;
+    @Mock
+    private LocalBluetoothProfileManager mLocalBluetoothProfileManager;
+    @Mock
+    private HeadsetProfile mHeadsetProfile;
+
+    private Context mContext;
+    private PreferenceScreen mScreen;
+    private ListPreference mPreference;
+    private ShadowAudioManager mShadowAudioManager;
+    private ShadowMediaRouter mShadowMediaRouter;
+    private BluetoothManager mBluetoothManager;
+    private BluetoothAdapter mBluetoothAdapter;
+    private BluetoothDevice mBluetoothDevice;
+    private ShadowBluetoothDevice mShadowBluetoothDevice;
+    private LocalBluetoothManager mLocalBluetoothManager;
+    private AudioSwitchPreferenceController mController;
+    private List<BluetoothDevice> mConnectedDevices;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mContext = spy(RuntimeEnvironment.application);
+
+        mShadowAudioManager = ShadowAudioManager.getShadow();
+        mShadowMediaRouter = ShadowMediaRouter.getShadow();
+
+        ShadowBluetoothUtils.sLocalBluetoothManager = mLocalManager;
+        mLocalBluetoothManager = ShadowBluetoothUtils.getLocalBtManager(mContext);
+
+        when(mLocalBluetoothManager.getEventManager()).thenReturn(mBluetoothEventManager);
+        when(mLocalBluetoothManager.getProfileManager()).thenReturn(mLocalBluetoothProfileManager);
+        when(mLocalBluetoothProfileManager.getHeadsetProfile()).thenReturn(mHeadsetProfile);
+
+        mBluetoothManager = new BluetoothManager(mContext);
+        mBluetoothAdapter = mBluetoothManager.getAdapter();
+        mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(TEST_DEVICE_ADDRESS_1);
+        mShadowBluetoothDevice = Shadows.shadowOf(mBluetoothDevice);
+        mShadowBluetoothDevice.setName(TEST_DEVICE_NAME_1);
+        when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
+
+        mController = new HandsFreeProfileOutputPreferenceController(mContext, TEST_KEY);
+        mScreen = spy(new PreferenceScreen(mContext, null));
+        mPreference = new ListPreference(mContext);
+        mConnectedDevices = new ArrayList<>(1);
+        mConnectedDevices.add(mBluetoothDevice);
+
+        when(mScreen.getPreferenceManager()).thenReturn(mock(PreferenceManager.class));
+        when(mScreen.getContext()).thenReturn(mContext);
+        when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
+        mScreen.addPreference(mPreference);
+        mController.displayPreference(mScreen);
+    }
+
+    @After
+    public void tearDown() {
+        mShadowAudioManager.reset();
+        mShadowMediaRouter.reset();
+        ShadowBluetoothUtils.reset();
+    }
+
+    @Test
+    public void setActiveBluetoothDevice_duringACalling_shouldSetBtDeviceActive() {
+        mShadowAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
+
+        mController.setActiveBluetoothDevice(mBluetoothDevice);
+
+        verify(mHeadsetProfile).setActiveDevice(mBluetoothDevice);
+    }
+
+    @Test
+    public void updateState_shouldSetSummary() {
+        mController.updateState(mPreference);
+
+        assertThat(mPreference.getSummary()).isEqualTo(
+                mContext.getText(R.string.media_output_default_summary));
+    }
+
+    /**
+     * One Headset Bluetooth device is available and activated
+     * Preference should be enabled
+     * Preference summary should be activate device name
+     */
+    @Test
+    public void updateState_oneHeadsetsAvailableAndActivated_shouldSetDeviceName() {
+        mShadowAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
+        when(mHeadsetProfile.getConnectedDevices()).thenReturn(mConnectedDevices);
+        when(mHeadsetProfile.getActiveDevice()).thenReturn(mBluetoothDevice);
+
+        mController.updateState(mPreference);
+
+        assertThat(mPreference.isEnabled()).isTrue();
+        assertThat(mPreference.getSummary()).isEqualTo(mBluetoothDevice.getName());
+    }
+
+    /**
+     * More than one Headset Bluetooth devices are available, and second device is active.
+     * Preference should be enabled
+     * Preference summary should be activate device name
+     */
+    @Test
+    public void updateState_moreThanOneHapBtDevicesAreAvailable_shouldSetActivatedDeviceName() {
+        ShadowBluetoothDevice shadowBluetoothDevice;
+        mShadowAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
+        BluetoothDevice secondBluetoothDevice;
+        secondBluetoothDevice = mBluetoothAdapter.getRemoteDevice(TEST_DEVICE_ADDRESS_2);
+        shadowBluetoothDevice = Shadows.shadowOf(secondBluetoothDevice);
+        shadowBluetoothDevice.setName(TEST_DEVICE_NAME_2);
+        List<BluetoothDevice> connectedDevices = new ArrayList<>(2);
+        connectedDevices.add(mBluetoothDevice);
+        connectedDevices.add(secondBluetoothDevice);
+
+        when(mHeadsetProfile.getConnectedDevices()).thenReturn(connectedDevices);
+        when(mHeadsetProfile.getActiveDevice()).thenReturn(secondBluetoothDevice);
+
+        mController.updateState(mPreference);
+
+        assertThat(mPreference.isEnabled()).isTrue();
+        assertThat(mPreference.getSummary()).isEqualTo(secondBluetoothDevice.getName());
+    }
+
+    /**
+     * Hands Free Profile Bluetooth device(s) are available, but wired headset is plugged in
+     * and activated.
+     * Preference should be enabled
+     * Preference summary should be "This device"
+     */
+    @Test
+    public void hapBtDevicesAreAvailableButWiredHeadsetIsActivated_shouldSetDefaultSummary() {
+        mShadowAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
+        mShadowAudioManager.setWiredHeadsetOn(true);
+        mShadowAudioManager.setBluetoothScoOn(false);
+        when(mHeadsetProfile.getConnectedDevices()).thenReturn(mConnectedDevices);
+        when(mHeadsetProfile.getActiveDevice()).thenReturn(
+                mBluetoothDevice); // BT device is still activated in this case
+
+        mController.updateState(mPreference);
+
+        assertThat(mPreference.isEnabled()).isTrue();
+        assertThat(mPreference.getSummary()).isEqualTo(
+                mContext.getText(R.string.media_output_default_summary));
+    }
+
+    /**
+     * No available Headset BT devices
+     * Preference should be disabled
+     * Preference summary should be "This device"
+     */
+    @Test
+    public void noAvailableHeadsetBtDevices_preferenceEnableIsFalse_shouldSetDefaultSummary() {
+        mShadowAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
+        List<BluetoothDevice> emptyDeviceList = new ArrayList<>();
+        when(mHeadsetProfile.getConnectedDevices()).thenReturn(emptyDeviceList);
+
+        mController.updateState(mPreference);
+
+        assertThat(mPreference.isEnabled()).isFalse();
+        assertThat(mPreference.getSummary()).isEqualTo(
+                mContext.getText(R.string.media_output_default_summary));
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/sound/MediaOutputPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/sound/MediaOutputPreferenceControllerTest.java
new file mode 100644
index 0000000..2b15b8e
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/sound/MediaOutputPreferenceControllerTest.java
@@ -0,0 +1,290 @@
+/*
+ * 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.sound;
+
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.bluetooth.BluetoothAdapter;
+import android.bluetooth.BluetoothDevice;
+import android.bluetooth.BluetoothManager;
+import android.content.Context;
+import android.media.AudioManager;
+import android.support.v7.preference.ListPreference;
+import android.support.v7.preference.PreferenceManager;
+import android.support.v7.preference.PreferenceScreen;
+
+import com.android.settings.R;
+import com.android.settings.testutils.SettingsRobolectricTestRunner;
+import com.android.settings.testutils.shadow.ShadowAudioManager;
+import com.android.settings.testutils.shadow.ShadowBluetoothUtils;
+import com.android.settings.testutils.shadow.ShadowMediaRouter;
+import com.android.settingslib.bluetooth.A2dpProfile;
+import com.android.settingslib.bluetooth.BluetoothEventManager;
+import com.android.settingslib.bluetooth.CachedBluetoothDevice;
+import com.android.settingslib.bluetooth.LocalBluetoothManager;
+import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
+
+import org.junit.After;
+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.ShadowBluetoothDevice;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(shadows = {
+        ShadowAudioManager.class,
+        ShadowMediaRouter.class,
+        ShadowBluetoothUtils.class,
+        ShadowBluetoothDevice.class}
+)
+public class MediaOutputPreferenceControllerTest {
+    private static final String TEST_KEY = "Test_Key";
+    private static final String TEST_DEVICE_NAME_1 = "Test_A2DP_BT_Device_NAME_1";
+    private static final String TEST_DEVICE_NAME_2 = "Test_A2DP_BT_Device_NAME_2";
+    private static final String TEST_DEVICE_ADDRESS_1 = "00:07:80:78:A4:69";
+    private static final String TEST_DEVICE_ADDRESS_2 = "00:00:00:00:00:00";
+
+    @Mock
+    private LocalBluetoothManager mLocalManager;
+    @Mock
+    private BluetoothEventManager mBluetoothEventManager;
+    @Mock
+    private CachedBluetoothDevice mCachedBluetoothDevice;
+    @Mock
+    private LocalBluetoothProfileManager mLocalBluetoothProfileManager;
+    @Mock
+    private A2dpProfile mA2dpProfile;
+
+    private Context mContext;
+    private PreferenceScreen mScreen;
+    private ListPreference mPreference;
+    private ShadowAudioManager mShadowAudioManager;
+    private ShadowMediaRouter mShadowMediaRouter;
+    private BluetoothManager mBluetoothManager;
+    private BluetoothAdapter mBluetoothAdapter;
+    private BluetoothDevice mBluetoothDevice;
+    private ShadowBluetoothDevice mShadowBluetoothDevice;
+    private LocalBluetoothManager mLocalBluetoothManager;
+    private AudioSwitchPreferenceController mController;
+    private List<BluetoothDevice> mConnectedDevices;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mContext = spy(RuntimeEnvironment.application);
+
+        mShadowAudioManager = ShadowAudioManager.getShadow();
+        mShadowMediaRouter = ShadowMediaRouter.getShadow();
+
+        ShadowBluetoothUtils.sLocalBluetoothManager = mLocalManager;
+        mLocalBluetoothManager = ShadowBluetoothUtils.getLocalBtManager(mContext);
+
+        when(mLocalBluetoothManager.getEventManager()).thenReturn(mBluetoothEventManager);
+        when(mLocalBluetoothManager.getProfileManager()).thenReturn(mLocalBluetoothProfileManager);
+        when(mLocalBluetoothProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile);
+
+        mBluetoothManager = new BluetoothManager(mContext);
+        mBluetoothAdapter = mBluetoothManager.getAdapter();
+        mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(TEST_DEVICE_ADDRESS_1);
+        mShadowBluetoothDevice = Shadows.shadowOf(mBluetoothDevice);
+        mShadowBluetoothDevice.setName(TEST_DEVICE_NAME_1);
+        when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
+
+        mController = new MediaOutputPreferenceController(mContext, TEST_KEY);
+        mScreen = spy(new PreferenceScreen(mContext, null));
+        mPreference = new ListPreference(mContext);
+        mConnectedDevices = new ArrayList<>(1);
+        mConnectedDevices.add(mBluetoothDevice);
+
+        when(mScreen.getPreferenceManager()).thenReturn(mock(PreferenceManager.class));
+        when(mScreen.getContext()).thenReturn(mContext);
+        when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
+        mScreen.addPreference(mPreference);
+        mController.displayPreference(mScreen);
+    }
+
+    @After
+    public void tearDown() {
+        mShadowAudioManager.reset();
+        mShadowMediaRouter.reset();
+        ShadowBluetoothUtils.reset();
+    }
+
+    @Test
+    public void setActiveBluetoothDevice_withoutRingAndCall_shouldSetBtDeviceActive() {
+        mShadowAudioManager.setMode(AudioManager.MODE_NORMAL);
+
+        mController.setActiveBluetoothDevice(mBluetoothDevice);
+
+        verify(mA2dpProfile).setActiveDevice(mBluetoothDevice);
+    }
+
+    @Test
+    public void updateState_shouldSetSummary() {
+        mController.updateState(mPreference);
+
+        assertThat(mPreference.getSummary()).isEqualTo(
+                mContext.getText(R.string.media_output_default_summary));
+    }
+
+    /**
+     * On going call state:
+     * Preference should be disabled
+     * Default string should be "Unavailable during calls"
+     */
+    @Test
+    public void updateState_duringACall_shouldSetDefaultSummary() {
+        mShadowAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
+
+        mController.updateState(mPreference);
+
+        assertThat(mPreference.isEnabled()).isFalse();
+        assertThat(mPreference.getSummary()).isEqualTo(
+                mContext.getText(R.string.media_out_summary_ongoing_call_state));
+    }
+
+    /**
+     * No available A2dp BT devices:
+     * Preference should be disabled
+     * Preference summary should be "This device"
+     */
+    @Test
+    public void updateState_noAvailableA2dpBtDevices_shouldDisableAndSetDefaultSummary() {
+        mShadowAudioManager.setMode(AudioManager.MODE_NORMAL);
+        List<BluetoothDevice> emptyDeviceList = new ArrayList<>();
+        when(mA2dpProfile.getConnectedDevices()).thenReturn(emptyDeviceList);
+
+        mController.updateState(mPreference);
+
+        assertThat(mPreference.isEnabled()).isFalse();
+        String defaultString = mContext.getString(R.string.media_output_default_summary);
+        assertThat(mPreference.getSummary()).isEqualTo(defaultString);
+    }
+
+    /**
+     * Media stream is captured by something else (cast device):
+     * Preference should be disabled
+     * Preference summary should be "unavailable"
+     */
+    @Test
+    public void updateState_mediaStreamIsCapturedByCast_shouldDisableAndSetDefaultSummary() {
+        mShadowAudioManager.setMusicActiveRemotely(true);
+
+        mController.updateState(mPreference);
+
+        assertThat(mPreference.isEnabled()).isFalse();
+        String defaultString = mContext.getString(R.string.media_output_summary_unavailable);
+        assertThat(mPreference.getSummary()).isEqualTo(defaultString);
+    }
+
+    /**
+     * One A2DP Bluetooth device is available and active.
+     * Preference should be enabled
+     * Preference summary should be activate device name
+     */
+    @Test
+    public void updateState_oneA2dpBtDeviceAreAvailable_shouldSetActivatedDeviceName() {
+        mShadowAudioManager.setMode(AudioManager.MODE_NORMAL);
+        when(mA2dpProfile.getConnectedDevices()).thenReturn(mConnectedDevices);
+        when(mA2dpProfile.getActiveDevice()).thenReturn(mBluetoothDevice);
+
+        mController.updateState(mPreference);
+
+        assertThat(mPreference.isEnabled()).isTrue();
+        assertThat(mPreference.getSummary()).isEqualTo(mBluetoothDevice.getName());
+    }
+
+    /**
+     * More than one A2DP Bluetooth devices are available, and second device is active.
+     * Preference should be enabled
+     * Preference summary should be activate device name
+     */
+    @Test
+    public void updateState_moreThanOneA2DpBtDevicesAreAvailable_shouldSetActivatedDeviceName() {
+        ShadowBluetoothDevice shadowBluetoothDevice;
+        mShadowAudioManager.setMode(AudioManager.MODE_NORMAL);
+        BluetoothDevice secondBluetoothDevice;
+        secondBluetoothDevice = mBluetoothAdapter.getRemoteDevice(TEST_DEVICE_ADDRESS_2);
+        shadowBluetoothDevice = Shadows.shadowOf(secondBluetoothDevice);
+        shadowBluetoothDevice.setName(TEST_DEVICE_NAME_2);
+        List<BluetoothDevice> connectedDevices = new ArrayList<>(2);
+        connectedDevices.add(mBluetoothDevice);
+        connectedDevices.add(secondBluetoothDevice);
+
+        when(mA2dpProfile.getConnectedDevices()).thenReturn(connectedDevices);
+        when(mA2dpProfile.getActiveDevice()).thenReturn(secondBluetoothDevice);
+
+        mController.updateState(mPreference);
+
+        assertThat(mPreference.isEnabled()).isTrue();
+        assertThat(mPreference.getSummary()).isEqualTo(secondBluetoothDevice.getName());
+    }
+
+    /**
+     * A2DP Bluetooth device(s) are available, but wired headset is plugged in and activated
+     * Preference should be enabled
+     * Preference summary should be "This device"
+     */
+    @Test
+    public void updateState_a2dpDevicesAvailableWiredHeadsetIsActivated_shouldSetDefaultSummary() {
+        mShadowAudioManager.setMode(AudioManager.MODE_NORMAL);
+        mShadowAudioManager.setWiredHeadsetOn(true);
+        mShadowAudioManager.setBluetoothA2dpOn(false);
+        when(mA2dpProfile.getConnectedDevices()).thenReturn(mConnectedDevices);
+        when(mA2dpProfile.getActiveDevice()).thenReturn(
+                mBluetoothDevice); // BT device is still activated in this case
+
+        mController.updateState(mPreference);
+
+        assertThat(mPreference.isEnabled()).isTrue();
+        String defaultString = mContext.getString(R.string.media_output_default_summary);
+        assertThat(mPreference.getSummary()).isEqualTo(defaultString);
+    }
+
+
+    /**
+     * A2DP Bluetooth device(s) are available, but current device speaker is activated
+     * Preference should be enabled
+     * Preference summary should be "This device"
+     */
+    @Test
+    public void updateState_a2dpDevicesAvailableCurrentDeviceActivated_shouldSetDefaultSummary() {
+        mShadowAudioManager.setMode(AudioManager.MODE_NORMAL);
+        when(mA2dpProfile.getConnectedDevices()).thenReturn(mConnectedDevices);
+        when(mA2dpProfile.getActiveDevice()).thenReturn(null);
+
+        mController.updateState(mPreference);
+
+        assertThat(mPreference.isEnabled()).isTrue();
+        String defaultString = mContext.getString(R.string.media_output_default_summary);
+        assertThat(mPreference.getSummary()).isEqualTo(defaultString);
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/testutils/shadow/ShadowAudioManager.java b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowAudioManager.java
index ed53eea..6817648 100644
--- a/tests/robotests/src/com/android/settings/testutils/shadow/ShadowAudioManager.java
+++ b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowAudioManager.java
@@ -16,23 +16,58 @@
 
 package com.android.settings.testutils.shadow;
 
+import static org.robolectric.RuntimeEnvironment.application;
+
+import android.media.AudioDeviceCallback;
 import android.media.AudioManager;
+import android.os.Handler;
 
 import org.robolectric.annotation.Implementation;
 import org.robolectric.annotation.Implements;
+import org.robolectric.annotation.Resetter;
+import org.robolectric.shadow.api.Shadow;
 
-@Implements(AudioManager.class)
-public class ShadowAudioManager {
+import java.util.ArrayList;
 
+@Implements(value = AudioManager.class, inheritImplementationMethods = true)
+public class ShadowAudioManager extends org.robolectric.shadows.ShadowAudioManager {
     private int mRingerMode;
-
-    @Implementation
-    public void setRingerModeInternal(int mode) {
-        mRingerMode = mode;
-    }
+    private boolean mMusicActiveRemotely = false;
+    private ArrayList<AudioDeviceCallback> mDeviceCallbacks = new ArrayList();
 
     @Implementation
     private int getRingerModeInternal() {
         return mRingerMode;
     }
+
+    public static ShadowAudioManager getShadow() {
+        return Shadow.extract(application.getSystemService(AudioManager.class));
+    }
+
+    public void setRingerModeInternal(int mode) {
+        mRingerMode = mode;
+    }
+
+    public void registerAudioDeviceCallback(AudioDeviceCallback callback, Handler handler) {
+        mDeviceCallbacks.add(callback);
+    }
+
+    public void unregisterAudioDeviceCallback(AudioDeviceCallback callback) {
+        if (mDeviceCallbacks.contains(callback)) {
+            mDeviceCallbacks.remove(callback);
+        }
+    }
+
+    public void setMusicActiveRemotely(boolean flag) {
+        mMusicActiveRemotely = flag;
+    }
+
+    public boolean isMusicActiveRemotely() {
+        return mMusicActiveRemotely;
+    }
+
+    @Resetter
+    public void reset() {
+        mDeviceCallbacks.clear();
+    }
 }
diff --git a/tests/robotests/src/com/android/settings/testutils/shadow/ShadowBluetoothUtils.java b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowBluetoothUtils.java
new file mode 100644
index 0000000..fcf79e2
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowBluetoothUtils.java
@@ -0,0 +1,43 @@
+/*
+ * 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.content.Context;
+
+import com.android.settings.bluetooth.Utils;
+import com.android.settingslib.bluetooth.LocalBluetoothManager;
+
+import org.robolectric.annotation.Implementation;
+import org.robolectric.annotation.Implements;
+import org.robolectric.annotation.Resetter;
+
+@Implements(Utils.class)
+public class ShadowBluetoothUtils {
+    public static LocalBluetoothManager sLocalBluetoothManager;
+
+
+
+    @Implementation
+    public static LocalBluetoothManager getLocalBtManager(Context context) {
+        return sLocalBluetoothManager;
+    }
+
+    @Resetter
+    public static void reset() {
+        sLocalBluetoothManager = null;
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/testutils/shadow/ShadowMediaRouter.java b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowMediaRouter.java
new file mode 100644
index 0000000..faaa0f0
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowMediaRouter.java
@@ -0,0 +1,57 @@
+/*
+ * 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.media.MediaRouter;
+
+import static org.robolectric.RuntimeEnvironment.application;
+
+import org.robolectric.annotation.Implements;
+import org.robolectric.annotation.Resetter;
+import org.robolectric.shadow.api.Shadow;
+
+import java.util.concurrent.CopyOnWriteArrayList;
+
+@Implements(value = MediaRouter.class, inheritImplementationMethods = true)
+public class ShadowMediaRouter extends org.robolectric.shadows.ShadowMediaRouter {
+    MediaRouter.RouteInfo mSelectedRoute;
+
+    final CopyOnWriteArrayList<MediaRouter.Callback> mCallbacks =
+            new CopyOnWriteArrayList<>();
+
+    public MediaRouter.RouteInfo getSelectedRoute(int type) {
+        return mSelectedRoute;
+    }
+
+    public void addCallback(int types, MediaRouter.Callback cb) {
+        mCallbacks.add(cb);
+    }
+
+    public void removeCallback(MediaRouter.Callback cb) {
+        if (mCallbacks.contains(cb))
+            mCallbacks.remove(cb);
+    }
+
+    public static ShadowMediaRouter getShadow() {
+        return Shadow.extract(application.getSystemService(MediaRouter.class));
+    }
+
+    @Resetter
+    public void reset() {
+        mCallbacks.clear();
+    }
+}