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