Snap for 6317653 from fdb60fa1e62f4c097bf8bff6001d1afa83020871 to qt-qpr3-release

Change-Id: If7e164045b504f0cd43015590bdd85d4be2ad289
diff --git a/res/xml/authenticator.xml b/res/xml/authenticator.xml
index b719fec..ab08a61 100644
--- a/res/xml/authenticator.xml
+++ b/res/xml/authenticator.xml
@@ -17,5 +17,4 @@
         xmlns:android="http://schemas.android.com/apk/res/android"
         android:icon="@mipmap/bt_share"
         android:smallIcon="@mipmap/bt_share"
-        android:accountType="@string/pbap_account_type"
-        android:label="@string/pbap_account_type" />
+        android:accountType="@string/pbap_account_type" />
diff --git a/src/com/android/bluetooth/btservice/AdapterService.java b/src/com/android/bluetooth/btservice/AdapterService.java
index 009e42c..a65bcfd 100644
--- a/src/com/android/bluetooth/btservice/AdapterService.java
+++ b/src/com/android/bluetooth/btservice/AdapterService.java
@@ -1559,7 +1559,6 @@
             if (service == null) {
                 return false;
             }
-            service.disable();
             return service.factoryReset();
 
         }
diff --git a/src/com/android/bluetooth/pbap/BluetoothPbapObexServer.java b/src/com/android/bluetooth/pbap/BluetoothPbapObexServer.java
index 34ee962..979becd 100755
--- a/src/com/android/bluetooth/pbap/BluetoothPbapObexServer.java
+++ b/src/com/android/bluetooth/pbap/BluetoothPbapObexServer.java
@@ -97,7 +97,6 @@
     private static final String[] LEGAL_PATH = {
             "/telecom",
             "/telecom/pb",
-            "/telecom/fav",
             "/telecom/ich",
             "/telecom/och",
             "/telecom/mch",
@@ -107,7 +106,6 @@
     @SuppressWarnings("unused") private static final String[] LEGAL_PATH_WITH_SIM = {
             "/telecom",
             "/telecom/pb",
-            "/telecom/fav",
             "/telecom/ich",
             "/telecom/och",
             "/telecom/mch",
@@ -140,9 +138,6 @@
     // phone book
     private static final String PB = "pb";
 
-    // favorites
-    private static final String FAV = "fav";
-
     private static final String TELECOM_PATH = "/telecom";
 
     private static final String ICH_PATH = "/telecom/ich";
@@ -155,8 +150,6 @@
 
     private static final String PB_PATH = "/telecom/pb";
 
-    private static final String FAV_PATH = "/telecom/fav";
-
     // type for list vcard objects
     private static final String TYPE_LISTING = "x-bt/vcard-listing";
 
@@ -219,8 +212,6 @@
         public static final int MISSED_CALL_HISTORY = 4;
 
         public static final int COMBINED_CALL_HISTORY = 5;
-
-        public static final int FAVORITES = 6;
     }
 
     public BluetoothPbapObexServer(Handler callback, Context context,
@@ -450,8 +441,6 @@
 
             if (mCurrentPath.equals(PB_PATH)) {
                 appParamValue.needTag = ContentType.PHONEBOOK;
-            } else if (mCurrentPath.equals(FAV_PATH)) {
-                appParamValue.needTag = ContentType.FAVORITES;
             } else if (mCurrentPath.equals(ICH_PATH)) {
                 appParamValue.needTag = ContentType.INCOMING_CALL_HISTORY;
             } else if (mCurrentPath.equals(OCH_PATH)) {
@@ -489,11 +478,6 @@
                 if (D) {
                     Log.v(TAG, "download phonebook request");
                 }
-            } else if (isNameMatchTarget(name, FAV)) {
-                appParamValue.needTag = ContentType.FAVORITES;
-                if (D) {
-                    Log.v(TAG, "download favorites request");
-                }
             } else if (isNameMatchTarget(name, ICH)) {
                 appParamValue.needTag = ContentType.INCOMING_CALL_HISTORY;
                 appParamValue.callHistoryVersionCounter =
@@ -767,8 +751,7 @@
         result.append("<vCard-listing version=\"1.0\">");
 
         // Phonebook listing request
-        if ((appParamValue.needTag == ContentType.PHONEBOOK)
-                || (appParamValue.needTag == ContentType.FAVORITES)) {
+        if (appParamValue.needTag == ContentType.PHONEBOOK) {
             String type = "";
             if (appParamValue.searchAttr.equals("0")) {
                 type = "name";
@@ -965,7 +948,7 @@
                     checkPbapFeatureSupport(mFolderVersionCounterbitMask);
         }
         boolean needSendPhonebookVersionCounters = false;
-        if (isNameMatchTarget(name, PB) || isNameMatchTarget(name, FAV)) {
+        if (isNameMatchTarget(name, PB)) {
             needSendPhonebookVersionCounters =
                     checkPbapFeatureSupport(mFolderVersionCounterbitMask);
         }
@@ -1209,12 +1192,11 @@
         if (appParamValue.needTag == 0) {
             Log.w(TAG, "wrong path!");
             return ResponseCodes.OBEX_HTTP_NOT_ACCEPTABLE;
-        } else if ((appParamValue.needTag == ContentType.PHONEBOOK)
-                || (appParamValue.needTag == ContentType.FAVORITES)) {
+        } else if (appParamValue.needTag == ContentType.PHONEBOOK) {
             if (intIndex < 0 || intIndex >= size) {
                 Log.w(TAG, "The requested vcard is not acceptable! name= " + name);
                 return ResponseCodes.OBEX_HTTP_NOT_FOUND;
-            } else if ((intIndex == 0) && (appParamValue.needTag == ContentType.PHONEBOOK)) {
+            } else if (intIndex == 0) {
                 // For PB_PATH, 0.vcf is the phone number of this phone.
                 String ownerVcard = mVcardManager.getOwnerPhoneNumberVcard(vcard21,
                         appParamValue.ignorefilter ? null : appParamValue.propertySelector);
@@ -1270,49 +1252,30 @@
 
         int requestSize =
                 pbSize >= appParamValue.maxListCount ? appParamValue.maxListCount : pbSize;
-        /**
-         * startIndex (resp., lastIndex) corresponds to the index of the first (resp., last)
-         * vcard entry in the phonebook object.
-         * PBAP v1.2.3: only pb starts indexing at 0.vcf (owner card), the other phonebook
-         * objects (e.g., fav) start at 1.vcf. Additionally, the owner card is included in
-         * pb's pbSize. This means pbSize corresponds to the index of the last vcf in the fav
-         * phonebook object, but does not for the pb phonebook object.
-         */
-        int startIndex = 1;
-        int lastIndex = pbSize;
-        if (appParamValue.needTag == BluetoothPbapObexServer.ContentType.PHONEBOOK) {
-            startIndex = 0;
-            lastIndex = pbSize - 1;
-        }
-        // [startPoint, endPoint] denote the range of vcf indices to send, inclusive.
-        int startPoint = startIndex + appParamValue.listStartOffset;
-        int endPoint = startPoint + requestSize - 1;
-        if (appParamValue.listStartOffset < 0 || startPoint > lastIndex) {
+        int startPoint = appParamValue.listStartOffset;
+        if (startPoint < 0 || startPoint >= pbSize) {
             Log.w(TAG, "listStartOffset is not correct! " + startPoint);
             return ResponseCodes.OBEX_HTTP_OK;
         }
-        if (endPoint > lastIndex) {
-            endPoint = lastIndex;
-        }
 
         // Limit the number of call log to CALLLOG_NUM_LIMIT
-        if ((appParamValue.needTag != BluetoothPbapObexServer.ContentType.PHONEBOOK)
-                && (appParamValue.needTag != BluetoothPbapObexServer.ContentType.FAVORITES)) {
+        if (appParamValue.needTag != BluetoothPbapObexServer.ContentType.PHONEBOOK) {
             if (requestSize > CALLLOG_NUM_LIMIT) {
                 requestSize = CALLLOG_NUM_LIMIT;
             }
         }
 
+        int endPoint = startPoint + requestSize - 1;
+        if (endPoint > pbSize - 1) {
+            endPoint = pbSize - 1;
+        }
         if (D) {
             Log.d(TAG, "pullPhonebook(): requestSize=" + requestSize + " startPoint=" + startPoint
                     + " endPoint=" + endPoint);
         }
 
         boolean vcard21 = appParamValue.vcard21;
-        boolean favorites =
-                (appParamValue.needTag == BluetoothPbapObexServer.ContentType.FAVORITES);
-        if ((appParamValue.needTag == BluetoothPbapObexServer.ContentType.PHONEBOOK)
-                || favorites) {
+        if (appParamValue.needTag == BluetoothPbapObexServer.ContentType.PHONEBOOK) {
             if (startPoint == 0) {
                 String ownerVcard = mVcardManager.getOwnerPhoneNumberVcard(vcard21,
                         appParamValue.ignorefilter ? null : appParamValue.propertySelector);
@@ -1322,13 +1285,13 @@
                     return mVcardManager.composeAndSendPhonebookVcards(op, 1, endPoint, vcard21,
                             ownerVcard, needSendBody, pbSize, appParamValue.ignorefilter,
                             appParamValue.propertySelector, appParamValue.vCardSelector,
-                            appParamValue.vCardSelectorOperator, mVcardSelector, favorites);
+                            appParamValue.vCardSelectorOperator, mVcardSelector);
                 }
             } else {
                 return mVcardManager.composeAndSendPhonebookVcards(op, startPoint, endPoint,
                         vcard21, null, needSendBody, pbSize, appParamValue.ignorefilter,
                         appParamValue.propertySelector, appParamValue.vCardSelector,
-                        appParamValue.vCardSelectorOperator, mVcardSelector, favorites);
+                        appParamValue.vCardSelectorOperator, mVcardSelector);
             }
         } else {
             return mVcardManager.composeAndSendSelectedCallLogVcards(appParamValue.needTag, op,
diff --git a/src/com/android/bluetooth/pbap/BluetoothPbapService.java b/src/com/android/bluetooth/pbap/BluetoothPbapService.java
index 280186f..e7dba2a 100644
--- a/src/com/android/bluetooth/pbap/BluetoothPbapService.java
+++ b/src/com/android/bluetooth/pbap/BluetoothPbapService.java
@@ -140,8 +140,7 @@
     private ObexServerSockets mServerSockets = null;
 
     private static final int SDP_PBAP_SERVER_VERSION = 0x0102;
-    // PBAP v1.2.3, Sec. 7.1.2: local phonebook and favorites
-    private static final int SDP_PBAP_SUPPORTED_REPOSITORIES = 0x0009;
+    private static final int SDP_PBAP_SUPPORTED_REPOSITORIES = 0x0001;
     private static final int SDP_PBAP_SUPPORTED_FEATURES = 0x021F;
 
     /* PBAP will use Bluetooth notification ID from 1000000 (included) to 2000000 (excluded).
diff --git a/src/com/android/bluetooth/pbap/BluetoothPbapVcardManager.java b/src/com/android/bluetooth/pbap/BluetoothPbapVcardManager.java
index 8801c16..5ba2b4b 100755
--- a/src/com/android/bluetooth/pbap/BluetoothPbapVcardManager.java
+++ b/src/com/android/bluetooth/pbap/BluetoothPbapVcardManager.java
@@ -153,8 +153,7 @@
         int size;
         switch (type) {
             case BluetoothPbapObexServer.ContentType.PHONEBOOK:
-            case BluetoothPbapObexServer.ContentType.FAVORITES:
-                size = getContactsSize(type);
+                size = getContactsSize();
                 break;
             default:
                 size = getCallHistorySize(type);
@@ -166,30 +165,16 @@
         return size;
     }
 
-    /**
-     * Returns the number of contacts (i.e., vcf) in a phonebook object.
-     * @param type specifies which phonebook object, e.g., pb, fav
-     * @return
-     */
-    public final int getContactsSize(final int type) {
+    public final int getContactsSize() {
         final Uri myUri = DevicePolicyUtils.getEnterprisePhoneUri(mContext);
         Cursor contactCursor = null;
-        String selectionClause = null;
-        if (type == BluetoothPbapObexServer.ContentType.FAVORITES) {
-            selectionClause = Phone.STARRED + " = 1";
-        }
         try {
-            contactCursor = mResolver.query(myUri,
-                    new String[]{Phone.CONTACT_ID}, selectionClause,
-                    null, Phone.CONTACT_ID);
+            contactCursor = mResolver.query(myUri, new String[]{Phone.CONTACT_ID}, null, null,
+                    Phone.CONTACT_ID);
             if (contactCursor == null) {
                 return 0;
             }
-            int contactsSize = getDistinctContactIdSize(contactCursor);
-            if (type == BluetoothPbapObexServer.ContentType.PHONEBOOK) {
-                contactsSize += 1; // pb has the 0.vcf owner's card
-            }
-            return contactsSize;
+            return getDistinctContactIdSize(contactCursor) + 1; // always has the 0.vcf
         } catch (CursorWindowAllocationException e) {
             Log.e(TAG, "CursorWindowAllocationException while getting Contacts size");
         } finally {
@@ -566,7 +551,7 @@
     final int composeAndSendPhonebookVcards(Operation op, final int startPoint, final int endPoint,
             final boolean vcardType21, String ownerVCard, int needSendBody, int pbSize,
             boolean ignorefilter, byte[] filter, byte[] vcardselector, String vcardselectorop,
-            boolean vcardselect, boolean favorites) {
+            boolean vcardselect) {
         if (startPoint < 1 || startPoint > endPoint) {
             Log.e(TAG, "internal error: startPoint or endPoint is not correct.");
             return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
@@ -577,15 +562,9 @@
         Cursor contactIdCursor = new MatrixCursor(new String[]{
                 Phone.CONTACT_ID
         });
-
-        String selectionClause = null;
-        if (favorites) {
-            selectionClause = Phone.STARRED + " = 1";
-        }
-
         try {
-            contactCursor = mResolver.query(myUri, PHONES_CONTACTS_PROJECTION, selectionClause,
-                    null, Phone.CONTACT_ID);
+            contactCursor = mResolver.query(myUri, PHONES_CONTACTS_PROJECTION, null, null,
+                    Phone.CONTACT_ID);
             if (contactCursor != null) {
                 contactIdCursor =
                         ContactCursorFilter.filterByRange(contactCursor, startPoint, endPoint);
diff --git a/src/com/android/bluetooth/pbapclient/PbapClientConnectionHandler.java b/src/com/android/bluetooth/pbapclient/PbapClientConnectionHandler.java
index 53670ff..4e4a240 100644
--- a/src/com/android/bluetooth/pbapclient/PbapClientConnectionHandler.java
+++ b/src/com/android/bluetooth/pbapclient/PbapClientConnectionHandler.java
@@ -99,7 +99,7 @@
     private static final long PBAP_FILTER_NICKNAME = 1 << 23;
 
     private static final int PBAP_SUPPORTED_FEATURE =
-            PBAP_FEATURE_DEFAULT_IMAGE_FORMAT | PBAP_FEATURE_BROWSING | PBAP_FEATURE_DOWNLOADING;
+            PBAP_FEATURE_DEFAULT_IMAGE_FORMAT | PBAP_FEATURE_DOWNLOADING;
     private static final long PBAP_REQUESTED_FIELDS =
             PBAP_FILTER_VERSION | PBAP_FILTER_FN | PBAP_FILTER_N | PBAP_FILTER_PHOTO
                     | PBAP_FILTER_ADR | PBAP_FILTER_EMAIL | PBAP_FILTER_TEL | PBAP_FILTER_NICKNAME;
diff --git a/src/com/android/bluetooth/pbapclient/PbapClientService.java b/src/com/android/bluetooth/pbapclient/PbapClientService.java
index f150cdd..02b1e7a 100644
--- a/src/com/android/bluetooth/pbapclient/PbapClientService.java
+++ b/src/com/android/bluetooth/pbapclient/PbapClientService.java
@@ -19,9 +19,11 @@
 import android.accounts.Account;
 import android.accounts.AccountManager;
 import android.bluetooth.BluetoothDevice;
+import android.bluetooth.BluetoothHeadsetClient;
 import android.bluetooth.BluetoothProfile;
 import android.bluetooth.IBluetoothPbapClient;
 import android.content.BroadcastReceiver;
+import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
@@ -31,6 +33,7 @@
 import com.android.bluetooth.R;
 import com.android.bluetooth.btservice.AdapterService;
 import com.android.bluetooth.btservice.ProfileService;
+import com.android.bluetooth.hfpclient.connserv.HfpClientConnectionService;
 import com.android.bluetooth.sdp.SdpManager;
 
 import java.util.ArrayList;
@@ -71,6 +74,9 @@
         filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
         // delay initial download until after the user is unlocked to add an account.
         filter.addAction(Intent.ACTION_USER_UNLOCKED);
+        // To remove call logs when PBAP was never connected while calls were made,
+        // we also listen for HFP to become disconnected.
+        filter.addAction(BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED);
         try {
             registerReceiver(mPbapBroadcastReceiver, filter);
         } catch (Exception e) {
@@ -128,6 +134,21 @@
         }
     }
 
+    private void removeHfpCallLog(String accountName, Context context) {
+        if (DBG) Log.d(TAG, "Removing call logs from " + accountName);
+        // Delete call logs belonging to accountName==BD_ADDR that also match
+        // component name "hfpclient".
+        ComponentName componentName = new ComponentName(context, HfpClientConnectionService.class);
+        String selectionFilter = CallLog.Calls.PHONE_ACCOUNT_ID + "=? AND "
+                + CallLog.Calls.PHONE_ACCOUNT_COMPONENT_NAME + "=?";
+        String[] selectionArgs = new String[]{accountName, componentName.flattenToString()};
+        try {
+            getContentResolver().delete(CallLog.Calls.CONTENT_URI, selectionFilter, selectionArgs);
+        } catch (IllegalArgumentException e) {
+            Log.w(TAG, "Call Logs could not be deleted, they may not exist yet.");
+        }
+    }
+
     private void registerSdpRecord() {
         SdpManager sdpManager = SdpManager.getDefaultManager();
         if (sdpManager == null) {
@@ -171,6 +192,21 @@
                 for (PbapClientStateMachine stateMachine : mPbapClientStateMachineMap.values()) {
                     stateMachine.resumeDownload();
                 }
+            } else if (action.equals(BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED)) {
+                // PbapClientConnectionHandler has code to remove calllogs when PBAP disconnects.
+                // However, if PBAP was never connected/enabled in the first place, and calls are
+                // made over HFP, these calllogs will not be removed when the device disconnects.
+                // This code ensures callogs are still removed in this case.
+                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
+                int newState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1);
+
+                if (newState == BluetoothProfile.STATE_DISCONNECTED) {
+                    if (DBG) {
+                        Log.d(TAG, "Received intent to disconnect HFP with " + device);
+                    }
+                    // HFP client stores entries in calllog.db by BD_ADDR and component name
+                    removeHfpCallLog(device.getAddress(), context);
+                }
             }
         }
     }