AtPhonebook.java revision 01ba103be055c3124d2dabc31d8e65dc7742be49
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.bluetooth.hfp;
18
19import com.android.bluetooth.R;
20
21import com.android.internal.telephony.GsmAlphabet;
22
23import android.bluetooth.BluetoothDevice;
24import android.content.ContentResolver;
25import android.content.Context;
26import android.content.Intent;
27import android.database.Cursor;
28import android.net.Uri;
29import android.provider.CallLog.Calls;
30import android.provider.ContactsContract.CommonDataKinds.Phone;
31import android.provider.ContactsContract.PhoneLookup;
32import android.telephony.PhoneNumberUtils;
33import android.util.Log;
34
35import java.util.HashMap;
36
37/**
38 * Helper for managing phonebook presentation over AT commands
39 * @hide
40 */
41public class AtPhonebook {
42    private static final String TAG = "BluetoothAtPhonebook";
43    private static final boolean DBG = false;
44
45    /** The projection to use when querying the call log database in response
46     *  to AT+CPBR for the MC, RC, and DC phone books (missed, received, and
47     *   dialed calls respectively)
48     */
49    private static final String[] CALLS_PROJECTION = new String[] {
50        Calls._ID, Calls.NUMBER, Calls.NUMBER_PRESENTATION
51    };
52
53    /** The projection to use when querying the contacts database in response
54     *   to AT+CPBR for the ME phonebook (saved phone numbers).
55     */
56    private static final String[] PHONES_PROJECTION = new String[] {
57        Phone._ID, Phone.DISPLAY_NAME, Phone.NUMBER, Phone.TYPE
58    };
59
60    /** Android supports as many phonebook entries as the flash can hold, but
61     *  BT periphals don't. Limit the number we'll report. */
62    private static final int MAX_PHONEBOOK_SIZE = 16384;
63
64    private static final String OUTGOING_CALL_WHERE = Calls.TYPE + "=" + Calls.OUTGOING_TYPE;
65    private static final String INCOMING_CALL_WHERE = Calls.TYPE + "=" + Calls.INCOMING_TYPE;
66    private static final String MISSED_CALL_WHERE = Calls.TYPE + "=" + Calls.MISSED_TYPE;
67    private static final String VISIBLE_PHONEBOOK_WHERE = Phone.IN_VISIBLE_GROUP + "=1";
68
69    private class PhonebookResult {
70        public Cursor  cursor; // result set of last query
71        public int     numberColumn;
72        public int     numberPresentationColumn;
73        public int     typeColumn;
74        public int     nameColumn;
75    };
76
77    private Context mContext;
78    private ContentResolver mContentResolver;
79    private HeadsetStateMachine mStateMachine;
80    private String mCurrentPhonebook;
81    private String mCharacterSet = "UTF-8";
82
83    private int mCpbrIndex1, mCpbrIndex2;
84    private boolean mCheckingAccessPermission;
85
86    // package and class name to which we send intent to check phone book access permission
87    private static final String ACCESS_AUTHORITY_PACKAGE = "com.android.settings";
88    private static final String ACCESS_AUTHORITY_CLASS =
89        "com.android.settings.bluetooth.BluetoothPermissionRequest";
90    private static final String BLUETOOTH_ADMIN_PERM = android.Manifest.permission.BLUETOOTH_ADMIN;
91
92    private final HashMap<String, PhonebookResult> mPhonebooks =
93            new HashMap<String, PhonebookResult>(4);
94
95    final int TYPE_UNKNOWN = -1;
96    final int TYPE_READ = 0;
97    final int TYPE_SET = 1;
98    final int TYPE_TEST = 2;
99
100    public AtPhonebook(Context context, HeadsetStateMachine headsetState) {
101        mContext = context;
102        mContentResolver = context.getContentResolver();
103        mStateMachine = headsetState;
104        mPhonebooks.put("DC", new PhonebookResult());  // dialled calls
105        mPhonebooks.put("RC", new PhonebookResult());  // received calls
106        mPhonebooks.put("MC", new PhonebookResult());  // missed calls
107        mPhonebooks.put("ME", new PhonebookResult());  // mobile phonebook
108
109        mCurrentPhonebook = "ME";  // default to mobile phonebook
110
111        mCpbrIndex1 = mCpbrIndex2 = -1;
112        mCheckingAccessPermission = false;
113    }
114
115    public void cleanup() {
116        mPhonebooks.clear();
117    }
118
119    /** Returns the last dialled number, or null if no numbers have been called */
120    public String getLastDialledNumber() {
121        String[] projection = {Calls.NUMBER};
122        Cursor cursor = mContentResolver.query(Calls.CONTENT_URI, projection,
123                Calls.TYPE + "=" + Calls.OUTGOING_TYPE, null, Calls.DEFAULT_SORT_ORDER +
124                " LIMIT 1");
125        if (cursor == null) return null;
126
127        if (cursor.getCount() < 1) {
128            cursor.close();
129            return null;
130        }
131        cursor.moveToNext();
132        int column = cursor.getColumnIndexOrThrow(Calls.NUMBER);
133        String number = cursor.getString(column);
134        cursor.close();
135        return number;
136    }
137
138    public boolean getCheckingAccessPermission() {
139        return mCheckingAccessPermission;
140    }
141
142    public void setCheckingAccessPermission(boolean checkAccessPermission) {
143        mCheckingAccessPermission = checkAccessPermission;
144    }
145
146    public void setCpbrIndex(int cpbrIndex) {
147        mCpbrIndex1 = mCpbrIndex2 = cpbrIndex;
148    }
149
150    public void handleCscsCommand(String atString, int type)
151    {
152        log("handleCscsCommand - atString = " +atString);
153        // Select Character Set
154        int atCommandResult = HeadsetHalConstants.AT_RESPONSE_ERROR;
155        int atCommandErrorCode = -1;
156        String atCommandResponse = null;
157        switch (type) {
158            case TYPE_READ: // Read
159                log("handleCscsCommand - Read Command");
160                atCommandResponse = "+CSCS: \"" + mCharacterSet + "\"";
161                atCommandResult = HeadsetHalConstants.AT_RESPONSE_OK;
162                break;
163            case TYPE_TEST: // Test
164                log("handleCscsCommand - Test Command");
165                atCommandResponse = ( "+CSCS: (\"UTF-8\",\"IRA\",\"GSM\")");
166                atCommandResult = HeadsetHalConstants.AT_RESPONSE_OK;
167                break;
168            case TYPE_SET: // Set
169                log("handleCscsCommand - Set Command");
170                String[] args = atString.split("=");
171                if (args.length < 2 || !(args[1] instanceof String)) {
172                    mStateMachine.atResponseCodeNative(atCommandResult, atCommandErrorCode);
173                    break;
174                }
175                String characterSet = ((atString.split("="))[1]);
176                characterSet = characterSet.replace("\"", "");
177                if (characterSet.equals("GSM") || characterSet.equals("IRA") ||
178                    characterSet.equals("UTF-8") || characterSet.equals("UTF8")) {
179                    mCharacterSet = characterSet;
180                    atCommandResult = HeadsetHalConstants.AT_RESPONSE_OK;
181                } else {
182                    atCommandErrorCode = BluetoothCmeError.OPERATION_NOT_SUPPORTED;
183                }
184                break;
185            case TYPE_UNKNOWN:
186            default:
187                log("handleCscsCommand - Invalid chars");
188                atCommandErrorCode = BluetoothCmeError.TEXT_HAS_INVALID_CHARS;
189        }
190        if (atCommandResponse != null)
191            mStateMachine.atResponseStringNative(atCommandResponse);
192        mStateMachine.atResponseCodeNative(atCommandResult, atCommandErrorCode);
193    }
194
195    public void handleCpbsCommand(String atString, int type) {
196        // Select PhoneBook memory Storage
197        log("handleCpbsCommand - atString = " +atString);
198        int atCommandResult = HeadsetHalConstants.AT_RESPONSE_ERROR;
199        int atCommandErrorCode = -1;
200        String atCommandResponse = null;
201        switch (type) {
202            case TYPE_READ: // Read
203                log("handleCpbsCommand - read command");
204                // Return current size and max size
205                if ("SM".equals(mCurrentPhonebook)) {
206                    atCommandResponse = "+CPBS: \"SM\",0," + getMaxPhoneBookSize(0);
207                    atCommandResult = HeadsetHalConstants.AT_RESPONSE_OK;
208                    break;
209                }
210                PhonebookResult pbr = getPhonebookResult(mCurrentPhonebook, true);
211                if (pbr == null) {
212                    atCommandErrorCode = BluetoothCmeError.OPERATION_NOT_SUPPORTED;
213                    break;
214                }
215                int size = pbr.cursor.getCount();
216                atCommandResponse = "+CPBS: \"" + mCurrentPhonebook + "\"," + size + "," + getMaxPhoneBookSize(size);
217                pbr.cursor.close();
218                pbr.cursor = null;
219                atCommandResult = HeadsetHalConstants.AT_RESPONSE_OK;
220                break;
221            case TYPE_TEST: // Test
222                log("handleCpbsCommand - test command");
223                atCommandResponse = ("+CPBS: (\"ME\",\"SM\",\"DC\",\"RC\",\"MC\")");
224                atCommandResult = HeadsetHalConstants.AT_RESPONSE_OK;
225                break;
226            case TYPE_SET: // Set
227                log("handleCpbsCommand - set command");
228                String[] args = atString.split("=");
229                // Select phonebook memory
230                if (args.length < 2 || !(args[1] instanceof String)) {
231                    atCommandErrorCode = BluetoothCmeError.OPERATION_NOT_SUPPORTED;
232                    break;
233                }
234                String pb = ((String)args[1]).trim();
235                while (pb.endsWith("\"")) pb = pb.substring(0, pb.length() - 1);
236                while (pb.startsWith("\"")) pb = pb.substring(1, pb.length());
237                if (getPhonebookResult(pb, false) == null && !"SM".equals(pb)) {
238                   if (DBG) log("Dont know phonebook: '" + pb + "'");
239                   atCommandErrorCode = BluetoothCmeError.OPERATION_NOT_ALLOWED;
240                   break;
241                }
242                mCurrentPhonebook = pb;
243                atCommandResult = HeadsetHalConstants.AT_RESPONSE_OK;
244                break;
245            case TYPE_UNKNOWN:
246            default:
247                log("handleCpbsCommand - invalid chars");
248                atCommandErrorCode = BluetoothCmeError.TEXT_HAS_INVALID_CHARS;
249        }
250        if (atCommandResponse != null)
251            mStateMachine.atResponseStringNative(atCommandResponse);
252        mStateMachine.atResponseCodeNative(atCommandResult, atCommandErrorCode);
253    }
254
255    public void handleCpbrCommand(String atString, int type, BluetoothDevice remoteDevice) {
256        log("handleCpbrCommand - atString = " +atString);
257        int atCommandResult = HeadsetHalConstants.AT_RESPONSE_ERROR;
258        int atCommandErrorCode = -1;
259        String atCommandResponse = null;
260        switch (type) {
261            case TYPE_TEST: // Test
262                /* Ideally we should return the maximum range of valid index's
263                 * for the selected phone book, but this causes problems for the
264                 * Parrot CK3300. So instead send just the range of currently
265                 * valid index's.
266                 */
267                log("handleCpbrCommand - test command");
268                int size;
269                if ("SM".equals(mCurrentPhonebook)) {
270                    size = 0;
271                } else {
272                    PhonebookResult pbr = getPhonebookResult(mCurrentPhonebook, true); //false);
273                    if (pbr == null) {
274                        atCommandErrorCode = BluetoothCmeError.OPERATION_NOT_ALLOWED;
275                        mStateMachine.atResponseCodeNative(atCommandResult, atCommandErrorCode);
276                        break;
277                    }
278                    size = pbr.cursor.getCount();
279                    log("handleCpbrCommand - size = "+size);
280                    pbr.cursor.close();
281                    pbr.cursor = null;
282                }
283                if (size == 0) {
284                    /* Sending "+CPBR: (1-0)" can confused some carkits, send "1-1" * instead */
285                    size = 1;
286                }
287                atCommandResponse = "+CPBR: (1-" + size + "),30,30";
288                atCommandResult = HeadsetHalConstants.AT_RESPONSE_OK;
289                if (atCommandResponse != null)
290                    mStateMachine.atResponseStringNative(atCommandResponse);
291                mStateMachine.atResponseCodeNative(atCommandResult, atCommandErrorCode);
292                break;
293            // Read PhoneBook Entries
294            case TYPE_READ:
295            case TYPE_SET: // Set & read
296                // Phone Book Read Request
297                // AT+CPBR=<index1>[,<index2>]
298                log("handleCpbrCommand - set/read command");
299                if (mCpbrIndex1 != -1) {
300                   /* handling a CPBR at the moment, reject this CPBR command */
301                   atCommandErrorCode = BluetoothCmeError.OPERATION_NOT_ALLOWED;
302                   mStateMachine.atResponseCodeNative(atCommandResult, atCommandErrorCode);
303                   break;
304                }
305                // Parse indexes
306                int index1;
307                int index2;
308                if ((atString.split("=")).length < 2) {
309                    mStateMachine.atResponseCodeNative(atCommandResult, atCommandErrorCode);
310                    break;
311                }
312                String atCommand = (atString.split("="))[1];
313                String[] indices = atCommand.split(",");
314                for(int i = 0; i < indices.length; i++)
315                    //replace AT command separator ';' from the index if any
316                    indices[i] = indices[i].replace(';', ' ').trim();
317                try {
318                    index1 = Integer.parseInt(indices[0]);
319                    if (indices.length == 1)
320                        index2 = index1;
321                    else
322                        index2 = Integer.parseInt(indices[1]);
323                }
324                catch (Exception e) {
325                    log("handleCpbrCommand - exception - invalid chars: " + e.toString());
326                    atCommandErrorCode = BluetoothCmeError.TEXT_HAS_INVALID_CHARS;
327                    mStateMachine.atResponseCodeNative(atCommandResult, atCommandErrorCode);
328                    break;
329                }
330                mCpbrIndex1 = index1;
331                mCpbrIndex2 = index2;
332                mCheckingAccessPermission = true;
333
334                if (checkAccessPermission(remoteDevice)) {
335                    mCheckingAccessPermission = false;
336                    atCommandResult = processCpbrCommand();
337                    mCpbrIndex1 = mCpbrIndex2 = -1;
338                    mStateMachine.atResponseCodeNative(atCommandResult, atCommandErrorCode);
339                    break;
340                }
341                // no reponse here, will continue the process in handleAccessPermissionResult
342                break;
343                case TYPE_UNKNOWN:
344                default:
345                    log("handleCpbrCommand - invalid chars");
346                    atCommandErrorCode = BluetoothCmeError.TEXT_HAS_INVALID_CHARS;
347                    mStateMachine.atResponseCodeNative(atCommandResult, atCommandErrorCode);
348        }
349    }
350
351    /** Get the most recent result for the given phone book,
352     *  with the cursor ready to go.
353     *  If force then re-query that phonebook
354     *  Returns null if the cursor is not ready
355     */
356    private synchronized PhonebookResult getPhonebookResult(String pb, boolean force) {
357        if (pb == null) {
358            return null;
359        }
360        PhonebookResult pbr = mPhonebooks.get(pb);
361        if (pbr == null) {
362            pbr = new PhonebookResult();
363        }
364        if (force || pbr.cursor == null) {
365            if (!queryPhonebook(pb, pbr)) {
366                return null;
367            }
368        }
369
370        return pbr;
371    }
372
373    private synchronized boolean queryPhonebook(String pb, PhonebookResult pbr) {
374        String where;
375        boolean ancillaryPhonebook = true;
376
377        if (pb.equals("ME")) {
378            ancillaryPhonebook = false;
379            where = VISIBLE_PHONEBOOK_WHERE;
380        } else if (pb.equals("DC")) {
381            where = OUTGOING_CALL_WHERE;
382        } else if (pb.equals("RC")) {
383            where = INCOMING_CALL_WHERE;
384        } else if (pb.equals("MC")) {
385            where = MISSED_CALL_WHERE;
386        } else {
387            return false;
388        }
389
390        if (pbr.cursor != null) {
391            pbr.cursor.close();
392            pbr.cursor = null;
393        }
394
395        if (ancillaryPhonebook) {
396            pbr.cursor = mContentResolver.query(
397                    Calls.CONTENT_URI, CALLS_PROJECTION, where, null,
398                    Calls.DEFAULT_SORT_ORDER + " LIMIT " + MAX_PHONEBOOK_SIZE);
399            if (pbr.cursor == null) return false;
400
401            pbr.numberColumn = pbr.cursor.getColumnIndexOrThrow(Calls.NUMBER);
402            pbr.numberPresentationColumn =
403                    pbr.cursor.getColumnIndexOrThrow(Calls.NUMBER_PRESENTATION);
404            pbr.typeColumn = -1;
405            pbr.nameColumn = -1;
406        } else {
407            pbr.cursor = mContentResolver.query(Phone.CONTENT_URI, PHONES_PROJECTION,
408                    where, null, Phone.NUMBER + " LIMIT " + MAX_PHONEBOOK_SIZE);
409            if (pbr.cursor == null) return false;
410
411            pbr.numberColumn = pbr.cursor.getColumnIndex(Phone.NUMBER);
412            pbr.numberPresentationColumn = -1;
413            pbr.typeColumn = pbr.cursor.getColumnIndex(Phone.TYPE);
414            pbr.nameColumn = pbr.cursor.getColumnIndex(Phone.DISPLAY_NAME);
415        }
416        Log.i(TAG, "Refreshed phonebook " + pb + " with " + pbr.cursor.getCount() + " results");
417        return true;
418    }
419
420    synchronized void resetAtState() {
421        mCharacterSet = "UTF-8";
422        mCpbrIndex1 = mCpbrIndex2 = -1;
423        mCheckingAccessPermission = false;
424    }
425
426    private synchronized int getMaxPhoneBookSize(int currSize) {
427        // some car kits ignore the current size and request max phone book
428        // size entries. Thus, it takes a long time to transfer all the
429        // entries. Use a heuristic to calculate the max phone book size
430        // considering future expansion.
431        // maxSize = currSize + currSize / 2 rounded up to nearest power of 2
432        // If currSize < 100, use 100 as the currSize
433
434        int maxSize = (currSize < 100) ? 100 : currSize;
435        maxSize += maxSize / 2;
436        return roundUpToPowerOfTwo(maxSize);
437    }
438
439    private int roundUpToPowerOfTwo(int x) {
440        x |= x >> 1;
441        x |= x >> 2;
442        x |= x >> 4;
443        x |= x >> 8;
444        x |= x >> 16;
445        return x + 1;
446    }
447
448    // process CPBR command after permission check
449    /*package*/ int processCpbrCommand()
450    {
451        log("processCpbrCommand");
452        int atCommandResult = HeadsetHalConstants.AT_RESPONSE_ERROR;
453        int atCommandErrorCode = -1;
454        String atCommandResponse = null;
455        StringBuilder response = new StringBuilder();
456        String record;
457
458        // Shortcut SM phonebook
459        if ("SM".equals(mCurrentPhonebook)) {
460            atCommandResult = HeadsetHalConstants.AT_RESPONSE_OK;
461            return atCommandResult;
462        }
463
464        // Check phonebook
465        PhonebookResult pbr = getPhonebookResult(mCurrentPhonebook, true); //false);
466        if (pbr == null) {
467            atCommandErrorCode = BluetoothCmeError.OPERATION_NOT_ALLOWED;
468            return atCommandResult;
469        }
470
471        // More sanity checks
472        // Send OK instead of ERROR if these checks fail.
473        // When we send error, certain kits like BMW disconnect the
474        // Handsfree connection.
475        if (pbr.cursor.getCount() == 0 || mCpbrIndex1 <= 0 || mCpbrIndex2 < mCpbrIndex1  ||
476            mCpbrIndex2 > pbr.cursor.getCount() || mCpbrIndex1 > pbr.cursor.getCount()) {
477            atCommandResult = HeadsetHalConstants.AT_RESPONSE_OK;
478            return atCommandResult;
479        }
480
481        // Process
482        atCommandResult = HeadsetHalConstants.AT_RESPONSE_OK;
483        int errorDetected = -1; // no error
484        pbr.cursor.moveToPosition(mCpbrIndex1 - 1);
485        log("mCpbrIndex1 = "+mCpbrIndex1+ " and mCpbrIndex2 = "+mCpbrIndex2);
486        for (int index = mCpbrIndex1; index <= mCpbrIndex2; index++) {
487            String number = pbr.cursor.getString(pbr.numberColumn);
488            String name = null;
489            int type = -1;
490            if (pbr.nameColumn == -1 && number != null && number.length() > 0) {
491                // try caller id lookup
492                // TODO: This code is horribly inefficient. I saw it
493                // take 7 seconds to process 100 missed calls.
494                Cursor c = mContentResolver.
495                    query(Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, number),
496                          new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup.TYPE},
497                          null, null, null);
498                if (c != null) {
499                    if (c.moveToFirst()) {
500                        name = c.getString(0);
501                        type = c.getInt(1);
502                    }
503                    c.close();
504                }
505                if (DBG && name == null) log("Caller ID lookup failed for " + number);
506
507            } else if (pbr.nameColumn != -1) {
508                name = pbr.cursor.getString(pbr.nameColumn);
509            } else {
510                log("processCpbrCommand: empty name and number");
511            }
512            if (name == null) name = "";
513            name = name.trim();
514            if (name.length() > 28) name = name.substring(0, 28);
515
516            if (pbr.typeColumn != -1) {
517                type = pbr.cursor.getInt(pbr.typeColumn);
518                name = name + "/" + getPhoneType(type);
519            }
520
521            if (number == null) number = "";
522            int regionType = PhoneNumberUtils.toaFromString(number);
523
524            number = number.trim();
525            number = PhoneNumberUtils.stripSeparators(number);
526            if (number.length() > 30) number = number.substring(0, 30);
527            int numberPresentation = Calls.PRESENTATION_ALLOWED;
528            if (pbr.numberPresentationColumn != -1) {
529                numberPresentation = pbr.cursor.getInt(pbr.numberPresentationColumn);
530            }
531            if (numberPresentation != Calls.PRESENTATION_ALLOWED) {
532                number = "";
533                // TODO: there are 3 types of numbers should have resource
534                // strings for: unknown, private, and payphone
535                name = mContext.getString(R.string.unknownNumber);
536            }
537
538            // TODO(): Handle IRA commands. It's basically
539            // a 7 bit ASCII character set.
540            if (!name.equals("") && mCharacterSet.equals("GSM")) {
541                byte[] nameByte = GsmAlphabet.stringToGsm8BitPacked(name);
542                if (nameByte == null) {
543                    name = mContext.getString(R.string.unknownNumber);
544                } else {
545                    name = new String(nameByte);
546                }
547            }
548
549            record = "+CPBR: " + index + ",\"" + number + "\"," + regionType + ",\"" + name + "\"";
550            record = record + "\r\n\r\n";
551            atCommandResponse = record;
552            log("processCpbrCommand - atCommandResponse = "+atCommandResponse);
553            mStateMachine.atResponseStringNative(atCommandResponse);
554            if (!pbr.cursor.moveToNext()) {
555                break;
556            }
557        }
558        if(pbr != null && pbr.cursor != null) {
559            pbr.cursor.close();
560            pbr.cursor = null;
561        }
562        return atCommandResult;
563    }
564
565    // Check if the remote device has premission to read our phone book
566    // Return true if it has the permission
567    // false if not known and we have sent our Intent to check
568    private boolean checkAccessPermission(BluetoothDevice remoteDevice) {
569        log("checkAccessPermission");
570        boolean trust = remoteDevice.getTrustState();
571
572        if (trust) {
573            return true;
574        }
575
576        log("checkAccessPermission - ACTION_CONNECTION_ACCESS_REQUEST");
577        Intent intent = new Intent(BluetoothDevice.ACTION_CONNECTION_ACCESS_REQUEST);
578        intent.setClassName(ACCESS_AUTHORITY_PACKAGE, ACCESS_AUTHORITY_CLASS);
579        intent.putExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE,
580        BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS);
581        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, remoteDevice);
582        // Leave EXTRA_PACKAGE_NAME and EXTRA_CLASS_NAME field empty
583        // BluetoothHandsfree's broadcast receiver is anonymous, cannot be targeted
584        mContext.sendBroadcast(intent, BLUETOOTH_ADMIN_PERM);
585        return false;
586    }
587
588    private static String getPhoneType(int type) {
589        switch (type) {
590            case Phone.TYPE_HOME:
591                return "H";
592            case Phone.TYPE_MOBILE:
593                return "M";
594            case Phone.TYPE_WORK:
595                return "W";
596            case Phone.TYPE_FAX_HOME:
597            case Phone.TYPE_FAX_WORK:
598                return "F";
599            case Phone.TYPE_OTHER:
600            case Phone.TYPE_CUSTOM:
601            default:
602                return "O";
603        }
604    }
605
606    private static void log(String msg) {
607        Log.d(TAG, msg);
608    }
609}
610