Merge "Allow space management for device admins" into klp-dev
diff --git a/res/drawable-hdpi/ic_settings_home.png b/res/drawable-hdpi/ic_settings_home.png
new file mode 100644
index 0000000..42354cd
--- /dev/null
+++ b/res/drawable-hdpi/ic_settings_home.png
Binary files differ
diff --git a/res/drawable-mdpi/ic_settings_home.png b/res/drawable-mdpi/ic_settings_home.png
new file mode 100644
index 0000000..8064773
--- /dev/null
+++ b/res/drawable-mdpi/ic_settings_home.png
Binary files differ
diff --git a/res/drawable-xhdpi/ic_settings_home.png b/res/drawable-xhdpi/ic_settings_home.png
new file mode 100644
index 0000000..687025d
--- /dev/null
+++ b/res/drawable-xhdpi/ic_settings_home.png
Binary files differ
diff --git a/res/layout/bluetooth_access.xml b/res/layout/bluetooth_access.xml
index 3bfb689..f398cef 100644
--- a/res/layout/bluetooth_access.xml
+++ b/res/layout/bluetooth_access.xml
@@ -40,7 +40,8 @@
             style="?android:attr/textAppearanceMedium"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
-            android:layout_marginTop="2dip"
+            android:layout_marginTop="20dip"
+            android:layout_marginBottom="20dip"
             android:text="@string/bluetooth_remember_choice" />
 
     </LinearLayout>
diff --git a/res/layout/preference_home_app.xml b/res/layout/preference_home_app.xml
new file mode 100644
index 0000000..8418161
--- /dev/null
+++ b/res/layout/preference_home_app.xml
@@ -0,0 +1,80 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2013 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.
+-->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:minHeight="?android:attr/listPreferredItemHeight"
+    android:gravity="center_vertical"
+    android:paddingStart="@*android:dimen/preference_item_padding_side">
+
+    <LinearLayout
+        android:layout_width="wrap_content"
+        android:layout_height="match_parent"
+        android:layout_weight="1"
+        android:id="@+id/home_app_pref"
+        android:focusable="true"
+        android:clickable="true"
+        android:gravity="center_vertical"
+        android:background="?android:attr/selectableItemBackground" >
+            <RadioButton
+                android:id="@+id/home_radio"
+                android:layout_width="wrap_content"
+                android:layout_height="match_parent"
+                android:layout_marginStart="10dip"
+                android:layout_marginEnd="4dip"
+                android:layout_gravity="center_vertical"
+                android:orientation="vertical"
+                android:clickable="false"
+                android:focusable="false" />
+            <ImageView
+                android:id="@+android:id/icon"
+                android:layout_width="48dp"
+                android:layout_height="48dp"
+                android:layout_gravity="center"
+                android:minWidth="48dp"
+                android:scaleType="centerInside"
+                android:layout_marginEnd="@*android:dimen/preference_item_padding_inner"
+                />
+            <TextView
+                android:id="@+android:id/title"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:singleLine="true"
+                android:layout_weight="1"
+                android:textAppearance="?android:attr/textAppearanceMedium"
+                android:ellipsize="end" />
+    </LinearLayout>
+    <View
+        android:id="@+id/home_divider"
+        android:layout_width="2dip"
+        android:layout_height="match_parent"
+        android:layout_marginTop="5dip"
+        android:layout_marginBottom="5dip"
+        android:background="@android:drawable/divider_horizontal_dark" />
+    <ImageView
+        android:id="@+id/home_app_uninstall"
+        android:layout_width="wrap_content"
+        android:layout_height="fill_parent"
+        android:paddingStart="12dip"
+        android:paddingEnd="12dp"
+        android:src="@drawable/ic_menu_delete_holo_dark"
+        android:contentDescription="@string/home_app_uninstall_button"
+        android:layout_gravity="center"
+        android:clickable="true"
+        android:focusable="true"
+        android:background="?android:attr/selectableItemBackground" />
+</LinearLayout>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 8ffa17f..4b3e261 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -276,6 +276,8 @@
     <string name="bluetooth_connected_no_headset">Connected (no phone)</string>
     <!-- Bluetooth settings.  Message when connected to a device, except for media audio. [CHAR LIMIT=40] -->
     <string name="bluetooth_connected_no_a2dp">Connected (no media)</string>
+    <!-- Bluetooth settings.  Message when connected to a device, except for map. [CHAR LIMIT=40] -->
+    <string name="bluetooth_connected_no_map">Connected (no message access)</string>
     <!-- Bluetooth settings.  Message when connected to a device, except for phone/media audio. [CHAR LIMIT=40] -->
     <string name="bluetooth_connected_no_headset_no_a2dp">Connected (no phone or media)</string>
     <!-- Bluetooth settings.  Message when a device is disconnected -->
@@ -349,7 +351,7 @@
     <string name="bluetooth_connection_dialog_text">"Do you want to connect to \u0022<xliff:g id="device_name">%1$s</xliff:g>\u0022?"</string>
 
     <!-- Activity label of BluetoothPbPermissionActivity, also used as Strings in the permission dialog [CHAR LIMIT=none] -->
-    <string name="bluetooth_phonebook_request">"Phone book request"</string>
+    <string name="bluetooth_phonebook_request">"Phone book access request"</string>
 
     <!-- Bluetooth phone book permission Alert Activity text [CHAR LIMIT=none] -->
     <string name="bluetooth_pb_acceptance_dialog_text">%1$s wants to access your contacts and call history. Give access to %2$s?</string>
@@ -360,7 +362,7 @@
     <string name="bluetooth_pb_remember_choice">Don\'t ask again</string>
 
     <!-- Activity label of BluetoothMessagePermissionActivity, also used as Strings in the permission dialog [CHAR LIMIT=none] -->
-    <string name="bluetooth_map_request">"Message request"</string>
+    <string name="bluetooth_map_request">"Message access request"</string>
 
     <!-- Bluetooth message permission Alert Activity text [CHAR LIMIT=none] -->
     <string name="bluetooth_map_acceptance_dialog_text">%1$s wants to access your messages. Give access to %2$s?</string>
@@ -1147,6 +1149,8 @@
     <string name="bluetooth_profile_pan">Internet access</string>
     <!-- Bluetooth settings. The user-visible string that is used whenever referring to the PAN profile (sharing this device's Internet connection). [CHAR LIMIT=40]-->
     <string name="bluetooth_profile_pan_nap">Internet connection sharing</string>
+    <!-- Bluetooth settings.  The user-visible string that is used whenever referring to the map profile. -->
+    <string name="bluetooth_profile_map">Message Access</string>
 
     <!-- Bluetooth settings.  Message for disconnecting from the A2DP profile. [CHAR LIMIT=80] -->
     <string name="bluetooth_disconnect_a2dp_profile"><xliff:g id="device_name">%1$s</xliff:g> will be disconnected from media audio.</string>
@@ -1179,6 +1183,8 @@
     <string name="bluetooth_headset_profile_summary_connected">Connected to phone audio</string>
     <!-- Bluetooth settings.  Connection options screen.  The summary for the OPP checkbox preference when OPP is connected. -->
     <string name="bluetooth_opp_profile_summary_connected">Connected to file transfer server</string>
+    <!-- Bluetooth settings.  Connection options screen.  The summary for the map checkbox preference when map is connected. -->
+    <string name="bluetooth_map_profile_summary_connected">Connected to map</string>
     <!-- Bluetooth settings.  Connection options screen.  The summary for the OPP checkbox preference when OPP is not connected. -->
     <string name="bluetooth_opp_profile_summary_not_connected">Not connected to file transfer server</string>
     <!-- Bluetooth settings. Connection options screen. The summary for the HID checkbox preference when HID is connected. -->
@@ -1202,6 +1208,8 @@
          for the PAN checkbox preference that describes how checking it
          will set the PAN profile as preferred. -->
     <string name="bluetooth_pan_profile_summary_use_for">Use for Internet access</string>
+    <!-- Bluetooth settings.  Connection options screen.  The summary for the map checkbox preference that describes how checking it will set the map profile as preferred. -->
+    <string name="bluetooth_map_profile_summary_use_for">Use for map</string>
 
     <!-- Bluetooth settings.  Dock Setting Title -->
     <string name="bluetooth_dock_settings">Dock Settings</string>
@@ -1732,6 +1740,7 @@
     <!-- Sound and alerts settings -->
     <skip/>
     <!-- Main Settings screen setting option name to go into the display settings screen -->
+    <string name="home_settings">Home</string>
     <string name="display_settings_title">Display</string>
     <!-- Sound settings screen heading -->
     <string name="sound_settings">Sound</string>
@@ -4667,6 +4676,9 @@
     <!-- Applicaitons with restrictions - settings button [CHAR LIMIT=30] -->
     <string name="apps_with_restrictions_settings_button">Expand settings for application</string>
 
+    <!-- Home application selection - uninstall button [CHAR LIMIT=80] -->
+    <string name="home_app_uninstall_button">Uninstall this application</string>
+
     <!-- Warning message when changing a global setting for a tablet.[CHAR LIMIT=none] -->
     <string name="global_change_warning" product="tablet">This setting affects all users on this tablet.</string>
     <!-- Warning message when changing a global setting for a phone.[CHAR LIMIT=none] -->
diff --git a/res/xml/home_selection.xml b/res/xml/home_selection.xml
new file mode 100644
index 0000000..a348f9d
--- /dev/null
+++ b/res/xml/home_selection.xml
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2013 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
+        android:title="@string/home_settings"
+        xmlns:settings="http://schemas.android.com/apk/res/com.android.settings"
+        android:key="home"
+        android:order="0">
+
+</PreferenceScreen>
diff --git a/res/xml/settings_headers.xml b/res/xml/settings_headers.xml
index 6aa6691..55b3bca 100644
--- a/res/xml/settings_headers.xml
+++ b/res/xml/settings_headers.xml
@@ -62,6 +62,13 @@
     <header android:id="@+id/device_section"
         android:title="@string/header_category_device" />
 
+    <!-- Home -->
+    <header
+        android:id="@+id/home_settings"
+        android:icon="@drawable/ic_settings_home"
+        android:fragment="com.android.settings.HomeSettings"
+        android:title="@string/home_settings" />
+
     <!-- Sound -->
     <header
         android:id="@+id/sound_settings"
diff --git a/src/com/android/settings/HomeSettings.java b/src/com/android/settings/HomeSettings.java
new file mode 100644
index 0000000..6d937fd
--- /dev/null
+++ b/src/com/android/settings/HomeSettings.java
@@ -0,0 +1,243 @@
+/*
+ * Copyright (C) 2013 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;
+
+import java.util.ArrayList;
+
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.content.pm.ActivityInfo;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.graphics.ColorFilter;
+import android.graphics.ColorMatrix;
+import android.graphics.ColorMatrixColorFilter;
+import android.graphics.drawable.Drawable;
+import android.net.Uri;
+import android.os.Bundle;
+import android.os.Handler;
+import android.preference.Preference;
+import android.preference.PreferenceGroup;
+import android.util.Log;
+import android.view.View;
+import android.view.View.OnClickListener;
+import android.widget.ImageView;
+import android.widget.RadioButton;
+
+public class HomeSettings extends SettingsPreferenceFragment {
+    static final String TAG = "HomeSettings";
+
+    static final int REQUESTING_UNINSTALL = 10;
+
+    public static final String CURRENT_HOME = "current_home";
+
+    PreferenceGroup mPrefGroup;
+
+    PackageManager mPm;
+    ComponentName[] mHomeComponentSet;
+    ArrayList<HomeAppPreference> mPrefs;
+    HomeAppPreference mCurrentHome = null;
+    final IntentFilter mHomeFilter;
+
+    public HomeSettings() {
+        mHomeFilter = new IntentFilter(Intent.ACTION_MAIN);
+        mHomeFilter.addCategory(Intent.CATEGORY_HOME);
+        mHomeFilter.addCategory(Intent.CATEGORY_DEFAULT);
+    }
+
+    OnClickListener mHomeClickListener = new OnClickListener() {
+        @Override
+        public void onClick(View v) {
+            int index = (Integer)v.getTag();
+            HomeAppPreference pref = mPrefs.get(index);
+            if (!pref.isChecked) {
+                makeCurrentHome(pref);
+            }
+        }
+    };
+
+    OnClickListener mDeleteClickListener = new OnClickListener() {
+        @Override
+        public void onClick(View v) {
+            int index = (Integer)v.getTag();
+            uninstallApp(mPrefs.get(index));
+        }
+    };
+
+    void makeCurrentHome(HomeAppPreference newHome) {
+        if (mCurrentHome != null) {
+            mCurrentHome.setChecked(false);
+        }
+        newHome.setChecked(true);
+        mCurrentHome = newHome;
+
+        mPm.replacePreferredActivity(mHomeFilter, IntentFilter.MATCH_CATEGORY_EMPTY,
+                mHomeComponentSet, newHome.activityName);
+    }
+
+    void uninstallApp(HomeAppPreference pref) {
+        // Uninstallation is done by asking the OS to do it
+       Uri packageURI = Uri.parse("package:" + pref.activityName.getPackageName());
+       Intent uninstallIntent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageURI);
+       uninstallIntent.putExtra(Intent.EXTRA_UNINSTALL_ALL_USERS, false);
+       int requestCode = REQUESTING_UNINSTALL + (pref.isChecked ? 1 : 0);
+       startActivityForResult(uninstallIntent, requestCode);
+   }
+
+    @Override
+    public void onActivityResult(int requestCode, int resultCode, Intent data) {
+        super.onActivityResult(requestCode, resultCode, data);
+
+        // Rebuild the list now that we might have nuked something
+        buildHomeActivitiesList();
+
+        // if the previous home app is now gone, fall back to the system one
+        if (requestCode > REQUESTING_UNINSTALL) {
+            // if mCurrentHome has gone null, it means we didn't find the previously-
+            // default home app when rebuilding the list, i.e. it was the one we
+            // just uninstalled.  When that happens we make the system-bundled
+            // home app the active default.
+            if (mCurrentHome == null) {
+                for (int i = 0; i < mPrefs.size(); i++) {
+                    HomeAppPreference pref = mPrefs.get(i);
+                    if (pref.isSystem) {
+                        makeCurrentHome(pref);
+                        break;
+                    }
+                }
+            }
+        }
+    }
+
+    void buildHomeActivitiesList() {
+        ArrayList<ResolveInfo> homeActivities = new ArrayList<ResolveInfo>();
+        ComponentName currentDefaultHome  = mPm.getHomeActivities(homeActivities);
+
+        Context context = getActivity();
+        mCurrentHome = null;
+        mPrefGroup.removeAll();
+        mPrefs = new ArrayList<HomeAppPreference>();
+        mHomeComponentSet = new ComponentName[homeActivities.size()];
+        int prefIndex = 0;
+        for (int i = 0; i < homeActivities.size(); i++) {
+            final ResolveInfo candidate = homeActivities.get(i);
+            final ActivityInfo info = candidate.activityInfo;
+            ComponentName activityName = new ComponentName(info.packageName, info.name);
+            mHomeComponentSet[i] = activityName;
+            try {
+                Drawable icon = info.loadIcon(mPm);
+                CharSequence name = info.loadLabel(mPm);
+                boolean isSystem = (info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
+                HomeAppPreference pref = new HomeAppPreference(context, activityName, prefIndex,
+                        icon, name, this, isSystem);
+                mPrefs.add(pref);
+                mPrefGroup.addPreference(pref);
+                pref.setEnabled(true);
+                if (activityName.equals(currentDefaultHome)) {
+                    mCurrentHome = pref;
+                }
+                prefIndex++;
+            } catch (Exception e) {
+                Log.v(TAG, "Problem dealing with activity " + activityName, e);
+            }
+        }
+
+        if (mCurrentHome != null) {
+            new Handler().post(new Runnable() {
+               public void run() {
+                   mCurrentHome.setChecked(true);
+               }
+            });
+        }
+    }
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        addPreferencesFromResource(R.xml.home_selection);
+
+        mPm = getPackageManager();
+        mPrefGroup = (PreferenceGroup) findPreference("home");
+    }
+
+    @Override
+    public void onResume() {
+        super.onResume();
+        buildHomeActivitiesList();
+    }
+
+    class HomeAppPreference extends Preference {
+        ComponentName activityName;
+        int index;
+        boolean isSystem;
+        HomeSettings fragment;
+        final ColorFilter grayscaleFilter;
+        boolean isChecked;
+
+        public HomeAppPreference(Context context, ComponentName activity,
+                int i, Drawable icon, CharSequence title,
+                HomeSettings parent, boolean sys) {
+            super(context);
+            setLayoutResource(R.layout.preference_home_app);
+            setIcon(icon);
+            setTitle(title);
+            activityName = activity;
+            fragment = parent;
+            index = i;
+            isSystem = sys;
+
+            ColorMatrix colorMatrix = new ColorMatrix();
+            colorMatrix.setSaturation(0f);
+            float[] matrix = colorMatrix.getArray();
+            matrix[18] = 0.5f;
+            grayscaleFilter = new ColorMatrixColorFilter(colorMatrix);
+        }
+
+        @Override
+        protected void onBindView(View view) {
+            super.onBindView(view);
+
+            RadioButton radio = (RadioButton) view.findViewById(R.id.home_radio);
+            radio.setChecked(isChecked);
+
+            Integer indexObj = new Integer(index);
+
+            ImageView icon = (ImageView) view.findViewById(R.id.home_app_uninstall);
+            if (isSystem) {
+                icon.setEnabled(false);
+                icon.setColorFilter(grayscaleFilter);
+            } else {
+                icon.setOnClickListener(mDeleteClickListener);
+                icon.setTag(indexObj);
+            }
+
+            View v = view.findViewById(R.id.home_app_pref);
+            v.setOnClickListener(mHomeClickListener);
+            v.setTag(indexObj);
+        }
+
+        void setChecked(boolean state) {
+            if (state != isChecked) {
+                isChecked = state;
+                notifyChanged();
+            }
+        }
+    }
+}
diff --git a/src/com/android/settings/Settings.java b/src/com/android/settings/Settings.java
index e754f98..bcfffcb 100644
--- a/src/com/android/settings/Settings.java
+++ b/src/com/android/settings/Settings.java
@@ -26,8 +26,10 @@
 import android.content.IntentFilter;
 import android.content.SharedPreferences;
 import android.content.pm.ActivityInfo;
+import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager;
 import android.content.pm.PackageManager.NameNotFoundException;
+import android.content.pm.ResolveInfo;
 import android.graphics.drawable.Drawable;
 import android.os.Bundle;
 import android.os.INetworkManagementService;
@@ -144,7 +146,8 @@
             R.id.about_settings,
             R.id.accessibility_settings,
             R.id.print_settings,
-            R.id.nfc_payment_settings
+            R.id.nfc_payment_settings,
+            R.id.home_settings
     };
 
     private SharedPreferences mDevelopmentPreferences;
@@ -558,6 +561,8 @@
             } else if (id == R.id.account_settings) {
                 int headerIndex = i + 1;
                 i = insertAccountsHeaders(target, headerIndex);
+            } else if (id == R.id.home_settings) {
+                updateHomeSettingHeaders(header);
             } else if (id == R.id.user_settings) {
                 if (!UserHandle.MU_ENABLED
                         || !UserManager.supportsMultipleUsers()
@@ -658,6 +663,51 @@
         return headerIndex;
     }
 
+    private boolean isSystemApp(ResolveInfo ri) {
+        return ((ri.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
+    }
+
+    private void updateHomeSettingHeaders(Header header) {
+        final PackageManager pm = getPackageManager();
+        final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
+        try {
+            ComponentName currentHome = pm.getHomeActivities(homeApps);
+            ResolveInfo iconSource = null;
+            if (currentHome == null) {
+                // no current default, so find the system home app and use that
+                for (int which = 0; which < homeApps.size(); which++) {
+                    ResolveInfo ri = homeApps.get(which);
+                    if (isSystemApp(ri)) {
+                        iconSource = ri;
+                        break;
+                    }
+                }
+            } else {
+                // find the current-home entry in the returned set
+                for (int which = 0; which < homeApps.size(); which++) {
+                    ResolveInfo ri = homeApps.get(which);
+                    ComponentName riName = new ComponentName(ri.activityInfo.packageName,
+                            ri.activityInfo.name);
+                    if (riName.equals(currentHome)) {
+                        iconSource = ri;
+                        break;
+                    }
+                }
+            }
+            if (iconSource != null) {
+                if (header.extras == null) {
+                    header.extras = new Bundle();
+                }
+                header.extras.putParcelable(HomeSettings.CURRENT_HOME, iconSource.activityInfo);
+            } else {
+                Log.v(LOG_TAG, "No home app icon found");
+            }
+        } catch (Exception e) {
+            // Can't look up the home activity; bail on configuring the icon
+            Log.w(LOG_TAG, "Problem looking up home activity!", e);
+        }
+    }
+
     private void getMetaData() {
         try {
             ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
@@ -820,17 +870,17 @@
 
                     //$FALL-THROUGH$
                 case HEADER_TYPE_NORMAL:
-                    if (header.extras != null
-                            && header.extras.containsKey(ManageAccountsSettings.KEY_ACCOUNT_TYPE)) {
+                    if (header.extras != null &&
+                            header.extras.containsKey(ManageAccountsSettings.KEY_ACCOUNT_TYPE)) {
                         String accType = header.extras.getString(
                                 ManageAccountsSettings.KEY_ACCOUNT_TYPE);
-                        ViewGroup.LayoutParams lp = holder.icon.getLayoutParams();
-                        lp.width = getContext().getResources().getDimensionPixelSize(
-                                R.dimen.header_icon_width);
-                        lp.height = lp.width;
-                        holder.icon.setLayoutParams(lp);
                         Drawable icon = mAuthHelper.getDrawableForType(getContext(), accType);
-                        holder.icon.setImageDrawable(icon);
+                        setHeaderIcon(holder, icon);
+                    } else if (header.extras != null &&
+                            header.extras.containsKey(HomeSettings.CURRENT_HOME)) {
+                        ActivityInfo ai = header.extras.getParcelable(HomeSettings.CURRENT_HOME);
+                        Drawable icon = ai.loadIcon(getContext().getPackageManager());
+                        setHeaderIcon(holder, icon);
                     } else {
                         holder.icon.setImageResource(header.iconRes);
                     }
@@ -848,6 +898,15 @@
             return view;
         }
 
+        private void setHeaderIcon(HeaderViewHolder holder, Drawable icon) {
+            ViewGroup.LayoutParams lp = holder.icon.getLayoutParams();
+            lp.width = getContext().getResources().getDimensionPixelSize(
+                    R.dimen.header_icon_width);
+            lp.height = lp.width;
+            holder.icon.setLayoutParams(lp);
+            holder.icon.setImageDrawable(icon);
+        }
+
         public void resume() {
             mWifiEnabler.resume();
             mBluetoothEnabler.resume();
diff --git a/src/com/android/settings/bluetooth/BluetoothPermissionActivity.java b/src/com/android/settings/bluetooth/BluetoothPermissionActivity.java
index aac1d4e..012aff0 100755
--- a/src/com/android/settings/bluetooth/BluetoothPermissionActivity.java
+++ b/src/com/android/settings/bluetooth/BluetoothPermissionActivity.java
@@ -28,7 +28,6 @@
 import android.view.View;
 import android.widget.CheckBox;
 import android.widget.CompoundButton;
-import android.widget.EditText;
 import android.widget.TextView;
 import android.widget.Button;
 import android.widget.CompoundButton.OnCheckedChangeListener;
@@ -64,6 +63,9 @@
         public void onReceive(Context context, Intent intent) {
             String action = intent.getAction();
             if (action.equals(BluetoothDevice.ACTION_CONNECTION_ACCESS_CANCEL)) {
+                int requestType = intent.getIntExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE,
+                                               BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS);
+                if (requestType != mRequestType) return;
                 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                 if (mDevice.equals(device)) dismissDialog();
             }
@@ -94,6 +96,8 @@
         mRequestType = i.getIntExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE,
                                      BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS);
 
+        if(DEBUG) Log.i(TAG, "onCreate() Request type: " + mRequestType);
+
         if (mRequestType == BluetoothDevice.REQUEST_TYPE_PROFILE_CONNECTION) {
             showDialog(getString(R.string.bluetooth_connection_permission_request), mRequestType);
         } else if (mRequestType == BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS) {
@@ -117,6 +121,7 @@
         final AlertController.AlertParams p = mAlertParams;
         p.mIconId = android.R.drawable.ic_dialog_info;
         p.mTitle = title;
+        if(DEBUG) Log.i(TAG, "showDialog() Request type: " + mRequestType + " this: " + this);
         switch(requestType)
         {
         case BluetoothDevice.REQUEST_TYPE_PROFILE_CONNECTION:
@@ -137,15 +142,16 @@
         setupAlert();
 
     }
-
-    private String createDisplayText(String message) {
-        String mMessage1 = message;
-        return mMessage1;
+    @Override
+    public void onBackPressed() {
+        /*we need an answer so ignore back button presses during auth */
+        if(DEBUG) Log.i(TAG, "Back button pressed! ignoring");
+        return;
     }
-
     private String createRemoteName()
     {
         String mRemoteName = mDevice != null ? mDevice.getAliasName() : null;
+
         if (mRemoteName == null) mRemoteName = getString(R.string.unknown);
         return mRemoteName;
     }
@@ -154,8 +160,8 @@
         String mRemoteName = createRemoteName();
         mView = getLayoutInflater().inflate(R.layout.bluetooth_connection_access, null);
         messageView = (TextView)mView.findViewById(R.id.message);
-        messageView.setText(createDisplayText(getString(R.string.bluetooth_connection_dialog_text,
-                mRemoteName)));
+        messageView.setText(getString(R.string.bluetooth_connection_dialog_text,
+                mRemoteName));
         return mView;
     }
 
@@ -177,8 +183,8 @@
         String mRemoteName = createRemoteName();
         mView = getLayoutInflater().inflate(R.layout.bluetooth_access, null);
         messageView = (TextView)mView.findViewById(R.id.message);
-        messageView.setText(createDisplayText(getString(R.string.bluetooth_pb_acceptance_dialog_text,
-                mRemoteName, mRemoteName)));
+        messageView.setText(getString(R.string.bluetooth_pb_acceptance_dialog_text,
+                mRemoteName, mRemoteName));
         createCheckbox(R.id.bluetooth_remember_choice);
         return mView;
     }
@@ -186,8 +192,8 @@
         String mRemoteName = createRemoteName();
         mView = getLayoutInflater().inflate(R.layout.bluetooth_access, null);
         messageView = (TextView)mView.findViewById(R.id.message);
-        messageView.setText(createDisplayText(getString(R.string.bluetooth_map_acceptance_dialog_text,
-                mRemoteName, mRemoteName)));
+        messageView.setText(getString(R.string.bluetooth_map_acceptance_dialog_text,
+                mRemoteName, mRemoteName));
         createCheckbox(R.id.bluetooth_remember_choice);
         return mView;
     }
@@ -221,6 +227,8 @@
         if (mReturnPackage != null && mReturnClass != null) {
             intent.setClassName(mReturnPackage, mReturnClass);
         }
+        if(DEBUG) Log.i(TAG, "sendIntentToReceiver() Request type: " + mRequestType +
+                " mReturnPackage" + mReturnPackage + " mReturnClass" + mReturnClass);
 
         intent.putExtra(BluetoothDevice.EXTRA_CONNECTION_ACCESS_RESULT,
                         allowed ? BluetoothDevice.CONNECTION_ACCESS_YES :
@@ -230,6 +238,7 @@
             intent.putExtra(extraName, extraValue);
         }
         intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
+        intent.putExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE, mRequestType);
         sendBroadcast(intent, android.Manifest.permission.BLUETOOTH_ADMIN);
     }
 
@@ -271,12 +280,11 @@
                                                          bluetoothManager.getProfileManager(),
                                                          mDevice);
         }
-        if (permissionType == BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS){
+        if(permissionType == BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS){
             cachedDevice.setPhonebookPermissionChoice(permissionChoice);
         }else if (permissionType == BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS){
             cachedDevice.setMessagePermissionChoice(permissionChoice);
         }
-
     }
 
 }
diff --git a/src/com/android/settings/bluetooth/BluetoothPermissionRequest.java b/src/com/android/settings/bluetooth/BluetoothPermissionRequest.java
index 343018e..c3b93be 100644
--- a/src/com/android/settings/bluetooth/BluetoothPermissionRequest.java
+++ b/src/com/android/settings/bluetooth/BluetoothPermissionRequest.java
@@ -36,7 +36,11 @@
 
     private static final String TAG = "BluetoothPermissionRequest";
     private static final boolean DEBUG = Utils.V;
-    public static final int NOTIFICATION_ID = android.R.drawable.stat_sys_data_bluetooth;
+    private static final int NOTIFICATION_ID = android.R.drawable.stat_sys_data_bluetooth;
+
+    private static final String NOTIFICATION_TAG_PBAP = "Phonebook Access" ;
+    private static final String NOTIFICATION_TAG_MAP = "Message Access";
+
 
     Context mContext;
     int mRequestType;
@@ -49,7 +53,7 @@
         mContext = context;
         String action = intent.getAction();
 
-        if (DEBUG) Log.d(TAG, "onReceive");
+        if (DEBUG) Log.d(TAG, "onReceive" + action);
 
         if (action.equals(BluetoothDevice.ACTION_CONNECTION_ACCESS_REQUEST)) {
             // convert broadcast intent into activity intent (same action string)
@@ -59,14 +63,8 @@
             mReturnPackage = intent.getStringExtra(BluetoothDevice.EXTRA_PACKAGE_NAME);
             mReturnClass = intent.getStringExtra(BluetoothDevice.EXTRA_CLASS_NAME);
 
-            Intent connectionAccessIntent = new Intent(action);
-            connectionAccessIntent.setClass(context, BluetoothPermissionActivity.class);
-            connectionAccessIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-            connectionAccessIntent.putExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE,
-                                            mRequestType);
-            connectionAccessIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
-            connectionAccessIntent.putExtra(BluetoothDevice.EXTRA_PACKAGE_NAME, mReturnPackage);
-            connectionAccessIntent.putExtra(BluetoothDevice.EXTRA_CLASS_NAME, mReturnClass);
+            if (DEBUG) Log.d(TAG, "onReceive request type: " + mRequestType + " return "
+                    + mReturnPackage + "," + mReturnClass);
 
             // Check if user had made decisions on accepting or rejecting the phonebook access
             // request. If there is, reply the request and return, no need to start permission
@@ -75,8 +73,22 @@
                 return;
             }
 
-            String deviceAddress = mDevice != null ? mDevice.getAddress() : null;
+            Intent connectionAccessIntent = new Intent(action);
+            connectionAccessIntent.setClass(context, BluetoothPermissionActivity.class);
+            // We use the FLAG_ACTIVITY_MULTIPLE_TASK since we can have multiple concurrent access requests
+            connectionAccessIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
+            connectionAccessIntent.setType(Integer.toString(mRequestType)); /* This is needed to create two pending
+                                                                               intents to the same activity.
+                                                                               The value is not used in the activity. */
+            connectionAccessIntent.putExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE,
+                                            mRequestType);
+            connectionAccessIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
+            connectionAccessIntent.putExtra(BluetoothDevice.EXTRA_PACKAGE_NAME, mReturnPackage);
+            connectionAccessIntent.putExtra(BluetoothDevice.EXTRA_CLASS_NAME, mReturnClass);
 
+            String deviceAddress = mDevice != null ? mDevice.getAddress() : null;
+            String title = null;
+            String message = null;
             PowerManager powerManager =
                 (PowerManager) context.getSystemService(Context.POWER_SERVICE);
 
@@ -93,33 +105,61 @@
                 deleteIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
                 deleteIntent.putExtra(BluetoothDevice.EXTRA_CONNECTION_ACCESS_RESULT,
                         BluetoothDevice.CONNECTION_ACCESS_NO);
-
-                Notification notification = new Notification(
-                    android.R.drawable.stat_sys_data_bluetooth,
-                    context.getString(R.string.bluetooth_connection_permission_request),
-                    System.currentTimeMillis());
+                deleteIntent.putExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE, mRequestType);
                 String deviceName = mDevice != null ? mDevice.getAliasName() : null;
-                notification.setLatestEventInfo(context,
-                    context.getString(R.string.bluetooth_connection_permission_request),
-                    context.getString(R.string.bluetooth_connection_notif_message, deviceName),
-                    PendingIntent.getActivity(context, 0, connectionAccessIntent, 0));
-                notification.flags = Notification.FLAG_AUTO_CANCEL |
-                                     Notification.FLAG_ONLY_ALERT_ONCE;
-                notification.defaults = Notification.DEFAULT_SOUND;
-                notification.deleteIntent = PendingIntent.getBroadcast(context, 0, deleteIntent, 0);
+                switch (mRequestType) {
+                    case BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS:
+                        title = context.getString(R.string.bluetooth_phonebook_request);
+                        message = context.getString(R.string.bluetooth_pb_acceptance_dialog_text, deviceName, deviceName);
+                        break;
+                    case BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS:
+                        title = context.getString(R.string.bluetooth_map_request);
+                        message = context.getString(R.string.bluetooth_map_acceptance_dialog_text, deviceName, deviceName);
+                        break;
+                    default:
+                        title = context.getString(R.string.bluetooth_connection_permission_request);
+                        message = context.getString(R.string.bluetooth_connection_dialog_text, deviceName, deviceName);
+                        break;
+                }
+                Notification notification = new Notification.Builder(context)
+                                        .setContentTitle(title)
+                                        .setTicker(message)
+                                        .setContentText(message)
+                                        .setSmallIcon(android.R.drawable.stat_sys_data_bluetooth)
+                                        .setAutoCancel(true)
+                                        .setPriority(Notification.PRIORITY_MAX)
+                                        .setOnlyAlertOnce(false)
+                                        .setDefaults(Notification.DEFAULT_ALL)
+                                        .setContentIntent(PendingIntent.getActivity(context, 0, connectionAccessIntent, 0))
+                                        .setDeleteIntent(PendingIntent.getBroadcast(context, 0, deleteIntent, 0))
+                                        .build();
+
+                notification.flags |= Notification.FLAG_NO_CLEAR; /* cannot be set with the builder */
 
                 NotificationManager notificationManager =
                     (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
-                notificationManager.notify(NOTIFICATION_ID, notification);
+
+                notificationManager.notify(getNotificationTag(mRequestType),NOTIFICATION_ID, notification);
             }
         } else if (action.equals(BluetoothDevice.ACTION_CONNECTION_ACCESS_CANCEL)) {
             // Remove the notification
             NotificationManager manager = (NotificationManager) context
                 .getSystemService(Context.NOTIFICATION_SERVICE);
-            manager.cancel(NOTIFICATION_ID);
+            mRequestType = intent.getIntExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE,
+                                        BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS);
+            manager.cancel(getNotificationTag(mRequestType), NOTIFICATION_ID);
         }
     }
 
+    private String getNotificationTag(int requestType) {
+        if(requestType == BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS) {
+            return NOTIFICATION_TAG_PBAP;
+        } else if(mRequestType == BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS) {
+            return NOTIFICATION_TAG_MAP;
+        }
+        return null;
+    }
+
     /**
      * @return true user had made a choice, this method replies to the request according
      *              to user's previous decision
@@ -185,6 +225,7 @@
                 Log.e(TAG, "Bad messagePermission: " + messagePermission);
             }
         }
+        if(DEBUG) Log.d(TAG,"checkUserChoice(): returning " + processed);
         return processed;
     }
 
@@ -204,6 +245,7 @@
             intent.putExtra(extraName, extraValue);
         }
         intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
+        intent.putExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE, mRequestType);
         mContext.sendBroadcast(intent, android.Manifest.permission.BLUETOOTH_ADMIN);
     }
 }
diff --git a/src/com/android/settings/bluetooth/CachedBluetoothDevice.java b/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
index 9742027..a9fd5ed 100755
--- a/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
+++ b/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
@@ -140,6 +140,18 @@
                     mLocalNapRoleConnected = true;
                 }
             }
+            if (profile instanceof MapProfile) {
+                profile.setPreferred(mDevice, true);
+                refresh();
+            }
+        } else if (profile instanceof MapProfile &&
+                newProfileState == BluetoothProfile.STATE_DISCONNECTED) {
+            if (mProfiles.contains(profile)) {
+                mRemovedProfiles.add(profile);
+                mProfiles.remove(profile);
+            }
+            profile.setPreferred(mDevice, false);
+            refresh();
         } else if (mLocalNapRoleConnected && profile instanceof PanProfile &&
                 ((PanProfile) profile).isLocalRoleNap(mDevice) &&
                 newProfileState == BluetoothProfile.STATE_DISCONNECTED) {
diff --git a/src/com/android/settings/bluetooth/LocalBluetoothPreferences.java b/src/com/android/settings/bluetooth/LocalBluetoothPreferences.java
index 6f4a765..f00b801 100644
--- a/src/com/android/settings/bluetooth/LocalBluetoothPreferences.java
+++ b/src/com/android/settings/bluetooth/LocalBluetoothPreferences.java
@@ -20,14 +20,15 @@
 import android.content.Context;
 import android.content.SharedPreferences;
 import android.content.res.Configuration;
+import android.util.Log;
 
 /**
  * LocalBluetoothPreferences provides an interface to the preferences
  * related to Bluetooth.
  */
 final class LocalBluetoothPreferences {
-//    private static final String TAG = "LocalBluetoothPreferences";
-
+    private static final String TAG = "LocalBluetoothPreferences";
+    private static final boolean DEBUG = Utils.D;
     private static final String SHARED_PREFERENCES_NAME = "bluetooth_settings";
 
     // If a device was picked from the device picker or was in discoverable mode
@@ -61,6 +62,7 @@
             String deviceAddress) {
         LocalBluetoothManager manager = LocalBluetoothManager.getInstance(context);
         if (manager == null) {
+            if(DEBUG) Log.v(TAG, "manager == null - do not show dialog.");
             return false;
         }
 
@@ -72,6 +74,7 @@
         // If in appliance mode, do not show dialog in foreground.
         if ((context.getResources().getConfiguration().uiMode &
                 Configuration.UI_MODE_TYPE_APPLIANCE) == Configuration.UI_MODE_TYPE_APPLIANCE) {
+            if (DEBUG) Log.v(TAG, "in appliance mode - do not show dialog.");
             return false;
         }
 
@@ -109,6 +112,7 @@
                 }
             }
         }
+        if (DEBUG) Log.v(TAG, "Found no reason to show the dialog - do not show dialog.");
         return false;
     }
 
diff --git a/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java b/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java
index 59a6944..78d6441 100644
--- a/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java
+++ b/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java
@@ -19,6 +19,7 @@
 import android.bluetooth.BluetoothA2dp;
 import android.bluetooth.BluetoothDevice;
 import android.bluetooth.BluetoothHeadset;
+import android.bluetooth.BluetoothMap;
 import android.bluetooth.BluetoothInputDevice;
 import android.bluetooth.BluetoothPan;
 import android.bluetooth.BluetoothPbap;
@@ -44,7 +45,7 @@
  */
 final class LocalBluetoothProfileManager {
     private static final String TAG = "LocalBluetoothProfileManager";
-
+    private static final boolean DEBUG = Utils.D;
     /** Singleton instance. */
     private static LocalBluetoothProfileManager sInstance;
 
@@ -79,6 +80,7 @@
 
     private A2dpProfile mA2dpProfile;
     private HeadsetProfile mHeadsetProfile;
+    private MapProfile mMapProfile;
     private final HidProfile mHidProfile;
     private OppProfile mOppProfile;
     private final PanProfile mPanProfile;
@@ -119,11 +121,17 @@
         addPanProfile(mPanProfile, PanProfile.NAME,
                 BluetoothPan.ACTION_CONNECTION_STATE_CHANGED);
 
+        if(DEBUG) Log.d(TAG, "Adding local MAP profile");
+        mMapProfile = new MapProfile(mContext, mLocalAdapter,
+                mDeviceManager, this);
+        addProfile(mMapProfile, MapProfile.NAME,
+                BluetoothMap.ACTION_CONNECTION_STATE_CHANGED);
+
        //Create PBAP server profile, but do not add it to list of profiles
        // as we do not need to monitor the profile as part of profile list
         mPbapProfile = new PbapServerProfile(context);
 
-        Log.d(TAG, "LocalBluetoothProfileManager construction complete");
+        if (DEBUG) Log.d(TAG, "LocalBluetoothProfileManager construction complete");
     }
 
     /**
@@ -137,7 +145,7 @@
         // A2DP
         if (BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.AudioSource)) {
             if (mA2dpProfile == null) {
-                Log.d(TAG, "Adding local A2DP profile");
+                if(DEBUG) Log.d(TAG, "Adding local A2DP profile");
                 mA2dpProfile = new A2dpProfile(mContext, mLocalAdapter, mDeviceManager, this);
                 addProfile(mA2dpProfile, A2dpProfile.NAME,
                         BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
@@ -150,7 +158,7 @@
         if (BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.Handsfree_AG) ||
             BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.HSP_AG)) {
             if (mHeadsetProfile == null) {
-                Log.d(TAG, "Adding local HEADSET profile");
+                if (DEBUG) Log.d(TAG, "Adding local HEADSET profile");
                 mHeadsetProfile = new HeadsetProfile(mContext, mLocalAdapter,
                         mDeviceManager, this);
                 addProfile(mHeadsetProfile, HeadsetProfile.NAME,
@@ -163,7 +171,7 @@
         // OPP
         if (BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.ObexObjectPush)) {
             if (mOppProfile == null) {
-                Log.d(TAG, "Adding local OPP profile");
+                if(DEBUG) Log.d(TAG, "Adding local OPP profile");
                 mOppProfile = new OppProfile();
                 // Note: no event handler for OPP, only name map.
                 mProfileNameMap.put(OppProfile.NAME, mOppProfile);
@@ -301,7 +309,6 @@
         return mPbapProfile;
     }
 
-
     /**
      * Fill in a list of LocalBluetoothProfile objects that are supported by
      * the local device and the remote device.
@@ -354,7 +361,7 @@
         }
 
         if(isPanNapConnected)
-            Log.d(TAG, "Valid PAN-NAP connection exists.");
+            if(DEBUG) Log.d(TAG, "Valid PAN-NAP connection exists.");
         if ((BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.NAP) &&
             mPanProfile != null) || isPanNapConnected) {
             profiles.add(mPanProfile);
diff --git a/src/com/android/settings/bluetooth/MapProfile.java b/src/com/android/settings/bluetooth/MapProfile.java
new file mode 100644
index 0000000..fb1b180
--- /dev/null
+++ b/src/com/android/settings/bluetooth/MapProfile.java
@@ -0,0 +1,213 @@
+/*
+ * Copyright (C) 2012 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.BluetoothClass;
+import android.bluetooth.BluetoothDevice;
+import android.bluetooth.BluetoothMap;
+import android.bluetooth.BluetoothProfile;
+import android.bluetooth.BluetoothUuid;
+import android.content.Context;
+import android.os.ParcelUuid;
+import android.util.Log;
+
+import com.android.settings.R;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * MapProfile handles Bluetooth MAP profile.
+ */
+final class MapProfile implements LocalBluetoothProfile {
+    private static final String TAG = "MapProfile";
+    private static boolean V = true;
+
+    private BluetoothMap mService;
+    private boolean mIsProfileReady;
+
+    private final LocalBluetoothAdapter mLocalAdapter;
+    private final CachedBluetoothDeviceManager mDeviceManager;
+    private final LocalBluetoothProfileManager mProfileManager;
+
+    static final ParcelUuid[] UUIDS = {
+        BluetoothUuid.MAP,
+        BluetoothUuid.MNS,
+        BluetoothUuid.MAS,
+    };
+
+    static final String NAME = "MAP";
+
+    // Order of this profile in device profiles list
+
+    // These callbacks run on the main thread.
+    private final class MapServiceListener
+            implements BluetoothProfile.ServiceListener {
+
+        public void onServiceConnected(int profile, BluetoothProfile proxy) {
+            if (V) Log.d(TAG,"Bluetooth service connected");
+            mService = (BluetoothMap) proxy;
+            // We just bound to the service, so refresh the UI for any connected MAP devices.
+            List<BluetoothDevice> deviceList = mService.getConnectedDevices();
+            while (!deviceList.isEmpty()) {
+                BluetoothDevice nextDevice = deviceList.remove(0);
+                CachedBluetoothDevice device = mDeviceManager.findDevice(nextDevice);
+                // we may add a new device here, but generally this should not happen
+                if (device == null) {
+                    Log.w(TAG, "MapProfile found new device: " + nextDevice);
+                    device = mDeviceManager.addDevice(mLocalAdapter, mProfileManager, nextDevice);
+                }
+                device.onProfileStateChanged(MapProfile.this,
+                        BluetoothProfile.STATE_CONNECTED);
+                device.refresh();
+            }
+
+            mProfileManager.callServiceConnectedListeners();
+            mIsProfileReady=true;
+        }
+
+        public void onServiceDisconnected(int profile) {
+            if (V) Log.d(TAG,"Bluetooth service disconnected");
+            mProfileManager.callServiceDisconnectedListeners();
+            mIsProfileReady=false;
+        }
+    }
+
+    public boolean isProfileReady() {
+        if(V) Log.d(TAG,"isProfileReady(): "+ mIsProfileReady);
+        return mIsProfileReady;
+    }
+
+    MapProfile(Context context, LocalBluetoothAdapter adapter,
+            CachedBluetoothDeviceManager deviceManager,
+            LocalBluetoothProfileManager profileManager) {
+        mLocalAdapter = adapter;
+        mDeviceManager = deviceManager;
+        mProfileManager = profileManager;
+        mLocalAdapter.getProfileProxy(context, new MapServiceListener(),
+                BluetoothProfile.MAP);
+    }
+
+    public boolean isConnectable() {
+        return true;
+    }
+
+    public boolean isAutoConnectable() {
+        return true;
+    }
+
+    public boolean connect(BluetoothDevice device) {
+        if(V)Log.d(TAG,"connect() - should not get called");
+        return true; // MAP never connects out
+    }
+
+    public boolean disconnect(BluetoothDevice device) {
+        if (mService == null) return false;
+        List<BluetoothDevice> deviceList = mService.getConnectedDevices();
+        if (!deviceList.isEmpty() && deviceList.get(0).equals(device)) {
+            if (mService.getPriority(device) > BluetoothProfile.PRIORITY_ON) {
+                mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
+            }
+            return mService.disconnect(device);
+        } else {
+            return false;
+        }
+    }
+
+    public int getConnectionStatus(BluetoothDevice device) {
+        if (mService == null) return BluetoothProfile.STATE_DISCONNECTED;
+        List<BluetoothDevice> deviceList = mService.getConnectedDevices();
+        if(V) Log.d(TAG,"getConnectionStatus: status is: "+ mService.getConnectionState(device));
+
+        return !deviceList.isEmpty() && deviceList.get(0).equals(device)
+                ? mService.getConnectionState(device)
+                : BluetoothProfile.STATE_DISCONNECTED;
+    }
+
+    public boolean isPreferred(BluetoothDevice device) {
+        if (mService == null) return false;
+        return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF;
+    }
+
+    public int getPreferred(BluetoothDevice device) {
+        if (mService == null) return BluetoothProfile.PRIORITY_OFF;
+        return mService.getPriority(device);
+    }
+
+    public void setPreferred(BluetoothDevice device, boolean preferred) {
+        if (mService == null) return;
+        if (preferred) {
+            if (mService.getPriority(device) < BluetoothProfile.PRIORITY_ON) {
+                mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
+            }
+        } else {
+            mService.setPriority(device, BluetoothProfile.PRIORITY_OFF);
+        }
+    }
+
+    public List<BluetoothDevice> getConnectedDevices() {
+        if (mService == null) return new ArrayList<BluetoothDevice>(0);
+        return mService.getDevicesMatchingConnectionStates(
+              new int[] {BluetoothProfile.STATE_CONNECTED,
+                         BluetoothProfile.STATE_CONNECTING,
+                         BluetoothProfile.STATE_DISCONNECTING});
+    }
+
+    public String toString() {
+        return NAME;
+    }
+
+    public int getOrdinal() {
+        return BluetoothProfile.MAP;
+    }
+
+    public int getNameResource(BluetoothDevice device) {
+        return R.string.bluetooth_profile_map;
+    }
+
+    public int getSummaryResourceForDevice(BluetoothDevice device) {
+        int state = getConnectionStatus(device);
+        switch (state) {
+            case BluetoothProfile.STATE_DISCONNECTED:
+                return R.string.bluetooth_map_profile_summary_use_for;
+
+            case BluetoothProfile.STATE_CONNECTED:
+                return R.string.bluetooth_map_profile_summary_connected;
+
+            default:
+                return Utils.getConnectionStateSummary(state);
+        }
+    }
+
+    public int getDrawableResource(BluetoothClass btClass) {
+        return R.drawable.ic_bt_cellphone;
+    }
+
+    protected void finalize() {
+        if (V) Log.d(TAG, "finalize()");
+        if (mService != null) {
+            try {
+                BluetoothAdapter.getDefaultAdapter().closeProfileProxy(BluetoothProfile.MAP,
+                                                                       mService);
+                mService = null;
+            }catch (Throwable t) {
+                Log.w(TAG, "Error cleaning up MAP proxy", t);
+            }
+        }
+    }
+}