1dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie/*
2dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie * Copyright (C) 2011 The Android Open Source Project
3dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie *
4dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie * Licensed under the Apache License, Version 2.0 (the "License");
5dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie * you may not use this file except in compliance with the License.
6dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie * You may obtain a copy of the License at
7dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie *
8dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie *      http://www.apache.org/licenses/LICENSE-2.0
9dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie *
10dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie * Unless required by applicable law or agreed to in writing, software
11dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie * distributed under the License is distributed on an "AS IS" BASIS,
12dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie * See the License for the specific language governing permissions and
14dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie * limitations under the License.
15dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie */
16dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie
17dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xiepackage com.android.settings.bluetooth;
18dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie
19dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xieimport android.app.Notification;
20dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xieimport android.app.NotificationManager;
21dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xieimport android.app.PendingIntent;
22dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xieimport android.bluetooth.BluetoothDevice;
23dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xieimport android.content.BroadcastReceiver;
24dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xieimport android.content.Context;
25dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xieimport android.content.Intent;
26dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xieimport android.os.PowerManager;
27dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xieimport android.util.Log;
28dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie
29dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xieimport com.android.settings.R;
30dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie
31dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie/**
32dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie * BluetoothPermissionRequest is a receiver to receive Bluetooth connection
33dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie * access request.
34dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie */
35dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xiepublic final class BluetoothPermissionRequest extends BroadcastReceiver {
36dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie
37dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie    private static final String TAG = "BluetoothPermissionRequest";
38dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie    private static final boolean DEBUG = Utils.V;
395d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz    private static final int NOTIFICATION_ID = android.R.drawable.stat_sys_data_bluetooth;
405d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz
415d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz    private static final String NOTIFICATION_TAG_PBAP = "Phonebook Access" ;
425d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz    private static final String NOTIFICATION_TAG_MAP = "Message Access";
435d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz
44dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie
45b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie    Context mContext;
46b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie    int mRequestType;
47b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie    BluetoothDevice mDevice;
48b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie    String mReturnPackage = null;
49b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie    String mReturnClass = null;
50b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie
51dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie    @Override
52dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie    public void onReceive(Context context, Intent intent) {
53b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie        mContext = context;
54dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie        String action = intent.getAction();
55dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie
565d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz        if (DEBUG) Log.d(TAG, "onReceive" + action);
57dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie
58dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie        if (action.equals(BluetoothDevice.ACTION_CONNECTION_ACCESS_REQUEST)) {
59dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie            // convert broadcast intent into activity intent (same action string)
60b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie            mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
61b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie            mRequestType = intent.getIntExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE,
62dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie                                                 BluetoothDevice.REQUEST_TYPE_PROFILE_CONNECTION);
63b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie            mReturnPackage = intent.getStringExtra(BluetoothDevice.EXTRA_PACKAGE_NAME);
64b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie            mReturnClass = intent.getStringExtra(BluetoothDevice.EXTRA_CLASS_NAME);
65dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie
665d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz            if (DEBUG) Log.d(TAG, "onReceive request type: " + mRequestType + " return "
675d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                    + mReturnPackage + "," + mReturnClass);
68b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie
69b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie            // Check if user had made decisions on accepting or rejecting the phonebook access
70b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie            // request. If there is, reply the request and return, no need to start permission
71b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie            // activity dialog or notification.
72b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie            if (checkUserChoice()) {
73b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie                return;
74b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie            }
75dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie
765d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz            Intent connectionAccessIntent = new Intent(action);
775d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz            connectionAccessIntent.setClass(context, BluetoothPermissionActivity.class);
785d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz            // We use the FLAG_ACTIVITY_MULTIPLE_TASK since we can have multiple concurrent access requests
795d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz            connectionAccessIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
805d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz            connectionAccessIntent.setType(Integer.toString(mRequestType)); /* This is needed to create two pending
815d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                                                                               intents to the same activity.
825d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                                                                               The value is not used in the activity. */
835d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz            connectionAccessIntent.putExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE,
845d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                                            mRequestType);
855d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz            connectionAccessIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
865d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz            connectionAccessIntent.putExtra(BluetoothDevice.EXTRA_PACKAGE_NAME, mReturnPackage);
875d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz            connectionAccessIntent.putExtra(BluetoothDevice.EXTRA_CLASS_NAME, mReturnClass);
88dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie
895d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz            String deviceAddress = mDevice != null ? mDevice.getAddress() : null;
905d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz            String title = null;
915d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz            String message = null;
92dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie            PowerManager powerManager =
93dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie                (PowerManager) context.getSystemService(Context.POWER_SERVICE);
94dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie
95dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie            if (powerManager.isScreenOn() &&
96dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie                LocalBluetoothPreferences.shouldShowDialogInForeground(context, deviceAddress) ) {
97dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie                context.startActivity(connectionAccessIntent);
98dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie            } else {
99dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie                // Put up a notification that leads to the dialog
100dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie
101dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie                // Create an intent triggered by clicking on the
102dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie                // "Clear All Notifications" button
103dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie
104dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie                Intent deleteIntent = new Intent(BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY);
105b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie                deleteIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
106dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie                deleteIntent.putExtra(BluetoothDevice.EXTRA_CONNECTION_ACCESS_RESULT,
107dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie                        BluetoothDevice.CONNECTION_ACCESS_NO);
1085d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                deleteIntent.putExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE, mRequestType);
109b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie                String deviceName = mDevice != null ? mDevice.getAliasName() : null;
1105d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                switch (mRequestType) {
1115d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                    case BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS:
1125d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                        title = context.getString(R.string.bluetooth_phonebook_request);
1135d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                        message = context.getString(R.string.bluetooth_pb_acceptance_dialog_text, deviceName, deviceName);
1145d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                        break;
1155d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                    case BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS:
1165d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                        title = context.getString(R.string.bluetooth_map_request);
1175d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                        message = context.getString(R.string.bluetooth_map_acceptance_dialog_text, deviceName, deviceName);
1185d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                        break;
1195d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                    default:
1205d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                        title = context.getString(R.string.bluetooth_connection_permission_request);
1215d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                        message = context.getString(R.string.bluetooth_connection_dialog_text, deviceName, deviceName);
1225d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                        break;
1235d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                }
1245d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                Notification notification = new Notification.Builder(context)
1255d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                                        .setContentTitle(title)
1265d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                                        .setTicker(message)
1275d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                                        .setContentText(message)
1285d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                                        .setSmallIcon(android.R.drawable.stat_sys_data_bluetooth)
1295d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                                        .setAutoCancel(true)
1305d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                                        .setPriority(Notification.PRIORITY_MAX)
1315d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                                        .setOnlyAlertOnce(false)
1325d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                                        .setDefaults(Notification.DEFAULT_ALL)
1335d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                                        .setContentIntent(PendingIntent.getActivity(context, 0, connectionAccessIntent, 0))
1345d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                                        .setDeleteIntent(PendingIntent.getBroadcast(context, 0, deleteIntent, 0))
1355d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                                        .build();
1365d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz
1375d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                notification.flags |= Notification.FLAG_NO_CLEAR; /* cannot be set with the builder */
138dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie
139dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie                NotificationManager notificationManager =
140dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
1415d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz
1425d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                notificationManager.notify(getNotificationTag(mRequestType),NOTIFICATION_ID, notification);
143dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie            }
144dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie        } else if (action.equals(BluetoothDevice.ACTION_CONNECTION_ACCESS_CANCEL)) {
145dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie            // Remove the notification
146dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie            NotificationManager manager = (NotificationManager) context
147dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie                .getSystemService(Context.NOTIFICATION_SERVICE);
1485d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz            mRequestType = intent.getIntExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE,
1495d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz                                        BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS);
1505d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz            manager.cancel(getNotificationTag(mRequestType), NOTIFICATION_ID);
1515d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz        }
1525d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz    }
1535d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz
1545d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz    private String getNotificationTag(int requestType) {
1555d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz        if(requestType == BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS) {
1565d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz            return NOTIFICATION_TAG_PBAP;
1575d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz        } else if(mRequestType == BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS) {
1585d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz            return NOTIFICATION_TAG_MAP;
159dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie        }
1605d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz        return null;
161b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie    }
162b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie
163b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie    /**
164b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie     * @return true user had made a choice, this method replies to the request according
165b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie     *              to user's previous decision
166b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie     *         false user hadnot made any choice on this device
167b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie     */
168b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie    private boolean checkUserChoice() {
169b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie        boolean processed = false;
170b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie
1715b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie        // ignore if it is something else than phonebook/message settings it wants us to remember
1725b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie        if (mRequestType != BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS
1735b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie                && mRequestType != BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS) {
1745b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie            if (DEBUG) Log.d(TAG, "Unknown RequestType: " + mRequestType);
175b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie            return processed;
176b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie        }
177b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie
178b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie        LocalBluetoothManager bluetoothManager = LocalBluetoothManager.getInstance(mContext);
179b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie        CachedBluetoothDeviceManager cachedDeviceManager =
180b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie            bluetoothManager.getCachedDeviceManager();
181b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie        CachedBluetoothDevice cachedDevice = cachedDeviceManager.findDevice(mDevice);
182b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie
183b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie        if (cachedDevice == null) {
184b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie            cachedDevice = cachedDeviceManager.addDevice(bluetoothManager.getBluetoothAdapter(),
185b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie                bluetoothManager.getProfileManager(), mDevice);
186b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie        }
187b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie
1885b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie        if(mRequestType == BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS) {
189b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie
1905b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie            int phonebookPermission = cachedDevice.getPhonebookPermissionChoice();
1915b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie
1925b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie            if (phonebookPermission == CachedBluetoothDevice.ACCESS_UNKNOWN) {
1935b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie                return processed;
1945b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie            }
1955b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie
1965b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie            String intentName = BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY;
1975b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie            if (phonebookPermission == CachedBluetoothDevice.ACCESS_ALLOWED) {
1985b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie                sendIntentToReceiver(intentName, true, BluetoothDevice.EXTRA_ALWAYS_ALLOWED, true);
1995b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie                processed = true;
2005b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie            } else if (phonebookPermission == CachedBluetoothDevice.ACCESS_REJECTED) {
2015b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie                sendIntentToReceiver(intentName, false,
2025b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie                                     null, false ); // dummy value, no effect since previous param is null
2035b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie                processed = true;
2045b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie            } else {
2055b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie                Log.e(TAG, "Bad phonebookPermission: " + phonebookPermission);
2065b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie            }
207b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie
2085b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie        } else if(mRequestType == BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS) {
2095b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie
2105b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie            int messagePermission = cachedDevice.getMessagePermissionChoice();
2115b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie
2125b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie            if (messagePermission == CachedBluetoothDevice.ACCESS_UNKNOWN) {
2135b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie                return processed;
2145b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie            }
2155b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie
2165b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie            String intentName = BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY;
2175b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie            if (messagePermission == CachedBluetoothDevice.ACCESS_ALLOWED) {
2185b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie                sendIntentToReceiver(intentName, true, BluetoothDevice.EXTRA_ALWAYS_ALLOWED, true);
2195b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie                processed = true;
2205b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie            } else if (messagePermission == CachedBluetoothDevice.ACCESS_REJECTED) {
2215b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie                sendIntentToReceiver(intentName, false,
2225b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie                                     null, false); // dummy value, no effect since previous param is null
2235b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie                processed = true;
2245b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie            } else {
2255b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie                Log.e(TAG, "Bad messagePermission: " + messagePermission);
2265b5b1e39ddd28af55cf5dd4a9ca9bf29828a66a6Matthew Xie            }
227b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie        }
2285d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz        if(DEBUG) Log.d(TAG,"checkUserChoice(): returning " + processed);
229b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie        return processed;
230b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie    }
231b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie
232b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie    private void sendIntentToReceiver(final String intentName, final boolean allowed,
233b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie                                      final String extraName, final boolean extraValue) {
234b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie        Intent intent = new Intent(intentName);
235b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie
236b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie        if (mReturnPackage != null && mReturnClass != null) {
237b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie            intent.setClassName(mReturnPackage, mReturnClass);
238b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie        }
239b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie
240b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie        intent.putExtra(BluetoothDevice.EXTRA_CONNECTION_ACCESS_RESULT,
241b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie                        allowed ? BluetoothDevice.CONNECTION_ACCESS_YES :
242b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie                        BluetoothDevice.CONNECTION_ACCESS_NO);
243b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie
244b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie        if (extraName != null) {
245b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie            intent.putExtra(extraName, extraValue);
246b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie        }
247b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
2485d2595f05182910c1dd0ed1302bf59071b55ae79Kim Schulz        intent.putExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE, mRequestType);
249b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie        mContext.sendBroadcast(intent, android.Manifest.permission.BLUETOOTH_ADMIN);
250b707255a440e53ec2865c4b34c7ea07d16d471bdMatthew Xie    }
251dbed12020c87d686ddcdbdbb8fba8f7b43a4e8d3Matthew Xie}
252