• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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 
17 package com.android.settings.bluetooth;
18 
19 import android.app.Notification;
20 import android.app.NotificationChannel;
21 import android.app.NotificationManager;
22 import android.app.PendingIntent;
23 import android.bluetooth.BluetoothDevice;
24 import android.content.BroadcastReceiver;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.os.PowerManager;
28 import android.os.UserManager;
29 import android.util.Log;
30 
31 import com.android.settings.R;
32 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
33 import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
34 import com.android.settingslib.bluetooth.LocalBluetoothManager;
35 
36 /**
37  * BluetoothPermissionRequest is a receiver to receive Bluetooth connection
38  * access request.
39  */
40 public final class BluetoothPermissionRequest extends BroadcastReceiver {
41 
42     private static final String TAG = "BluetoothPermissionRequest";
43     private static final boolean DEBUG = Utils.V;
44     private static final int NOTIFICATION_ID = android.R.drawable.stat_sys_data_bluetooth;
45 
46     private static final String NOTIFICATION_TAG_PBAP = "Phonebook Access" ;
47     private static final String NOTIFICATION_TAG_MAP = "Message Access";
48     private static final String NOTIFICATION_TAG_SAP = "SIM Access";
49     /* TODO: Consolidate this multiple defined but common channel ID with other
50      * handlers that declare and use the same channel ID */
51     private static final String BLUETOOTH_NOTIFICATION_CHANNEL =
52         "bluetooth_notification_channel";
53 
54     private NotificationChannel mNotificationChannel = null;
55 
56     Context mContext;
57     int mRequestType;
58     BluetoothDevice mDevice;
59 
60     @Override
onReceive(Context context, Intent intent)61     public void onReceive(Context context, Intent intent) {
62         mContext = context;
63         String action = intent.getAction();
64 
65         if (DEBUG) Log.d(TAG, "onReceive" + action);
66 
67         if (action.equals(BluetoothDevice.ACTION_CONNECTION_ACCESS_REQUEST)) {
68             UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
69             // skip the notification for managed profiles.
70             if (um.isManagedProfile()) {
71                 if (DEBUG) Log.d(TAG, "Blocking notification for managed profile.");
72                 return;
73             }
74             // convert broadcast intent into activity intent (same action string)
75             mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
76             mRequestType = intent.getIntExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE,
77                                                  BluetoothDevice.REQUEST_TYPE_PROFILE_CONNECTION);
78 
79             if (DEBUG) {
80                 Log.d(TAG, "onReceive request type: " + mRequestType);
81             }
82 
83             // Even if the user has already made the choice, Bluetooth still may not know that if
84             // the user preference data have not been migrated from Settings app's shared
85             // preferences to Bluetooth app's. In that case, Bluetooth app broadcasts an
86             // ACTION_CONNECTION_ACCESS_REQUEST intent to ask to Settings app.
87             //
88             // If that happens, 'checkUserChoice()' here will do migration because it finds or
89             // creates a 'CachedBluetoothDevice' object for the device.
90             //
91             // After migration is done, 'checkUserChoice()' replies to the request by sending an
92             // ACTION_CONNECTION_ACCESS_REPLY intent. And we don't need to start permission activity
93             // dialog or notification.
94             if (checkUserChoice()) {
95                 return;
96             }
97 
98             Intent connectionAccessIntent = new Intent(action);
99             connectionAccessIntent.setClass(context, BluetoothPermissionActivity.class);
100             // We use the FLAG_ACTIVITY_MULTIPLE_TASK since we can have multiple concurrent access
101             // requests.
102             connectionAccessIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
103                     | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
104             // This is needed to create two pending intents to the same activity. The value is not
105             // used in the activity.
106             connectionAccessIntent.setType(Integer.toString(mRequestType));
107             connectionAccessIntent.putExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE,
108                                             mRequestType);
109             connectionAccessIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
110 
111             String deviceAddress = mDevice != null ? mDevice.getAddress() : null;
112             String deviceName = mDevice != null ? mDevice.getName() : null;
113             String title = null;
114             String message = null;
115             PowerManager powerManager =
116                 (PowerManager) context.getSystemService(Context.POWER_SERVICE);
117 
118             if (powerManager.isScreenOn()
119                     && LocalBluetoothPreferences.shouldShowDialogInForeground(
120                             context, deviceAddress, deviceName)) {
121                 context.startActivity(connectionAccessIntent);
122             } else {
123                 // Put up a notification that leads to the dialog
124 
125                 // Create an intent triggered by clicking on the
126                 // "Clear All Notifications" button
127 
128                 Intent deleteIntent = new Intent(BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY);
129                 deleteIntent.setPackage("com.android.bluetooth");
130                 deleteIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
131                 deleteIntent.putExtra(BluetoothDevice.EXTRA_CONNECTION_ACCESS_RESULT,
132                         BluetoothDevice.CONNECTION_ACCESS_NO);
133                 deleteIntent.putExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE, mRequestType);
134                 String deviceAlias = Utils.createRemoteName(context, mDevice);
135                 switch (mRequestType) {
136                     case BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS:
137                         title = context.getString(R.string.bluetooth_phonebook_request);
138                         message = context.getString(
139                                 R.string.bluetooth_phonebook_access_notification_content);
140                         break;
141                     case BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS:
142                         title = context.getString(R.string.bluetooth_map_request);
143                         message = context.getString(
144                                 R.string.bluetooth_message_access_notification_content);
145                         break;
146                     case BluetoothDevice.REQUEST_TYPE_SIM_ACCESS:
147                         title = context.getString(
148                                 R.string.bluetooth_sim_card_access_notification_title);
149                         message = context.getString(
150                                 R.string.bluetooth_sim_card_access_notification_content,
151                                 deviceAlias, deviceAlias);
152                         break;
153                     default:
154                         title = context.getString(
155                                 R.string.bluetooth_connect_access_notification_title);
156                         message = context.getString(
157                                 R.string.bluetooth_connect_access_notification_content,
158                                 deviceAlias, deviceAlias);
159                         break;
160                 }
161                 NotificationManager notificationManager =
162                     (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
163                 if (mNotificationChannel == null) {
164                     mNotificationChannel = new NotificationChannel(BLUETOOTH_NOTIFICATION_CHANNEL,
165                             context.getString(R.string.bluetooth),
166                             NotificationManager.IMPORTANCE_HIGH);
167                     notificationManager.createNotificationChannel(mNotificationChannel);
168                 }
169                 Notification notification = new Notification.Builder(context,
170                         BLUETOOTH_NOTIFICATION_CHANNEL)
171                         .setContentTitle(title)
172                         .setTicker(message)
173                         .setContentText(message)
174                         .setStyle(new Notification.BigTextStyle().bigText(message))
175                         .setSmallIcon(android.R.drawable.stat_sys_data_bluetooth)
176                         .setAutoCancel(true)
177                         .setPriority(Notification.PRIORITY_MAX)
178                         .setOnlyAlertOnce(false)
179                         .setDefaults(Notification.DEFAULT_ALL)
180                         .setContentIntent(PendingIntent.getActivity(context, 0,
181                                 connectionAccessIntent, 0))
182                         .setDeleteIntent(PendingIntent.getBroadcast(context, 0, deleteIntent, 0))
183                         .setColor(context.getColor(
184                                 com.android.internal.R.color.system_notification_accent_color))
185                         .setLocalOnly(true)
186                         .build();
187 
188                 notification.flags |= Notification.FLAG_NO_CLEAR; // Cannot be set with the builder.
189 
190                 notificationManager.notify(getNotificationTag(mRequestType), NOTIFICATION_ID,
191                         notification);
192             }
193         } else if (action.equals(BluetoothDevice.ACTION_CONNECTION_ACCESS_CANCEL)) {
194             // Remove the notification
195             NotificationManager manager = (NotificationManager) context
196                 .getSystemService(Context.NOTIFICATION_SERVICE);
197             mRequestType = intent.getIntExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE,
198                                         BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS);
199             manager.cancel(getNotificationTag(mRequestType), NOTIFICATION_ID);
200         }
201     }
202 
getNotificationTag(int requestType)203     private String getNotificationTag(int requestType) {
204         if(requestType == BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS) {
205             return NOTIFICATION_TAG_PBAP;
206         } else if(mRequestType == BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS) {
207             return NOTIFICATION_TAG_MAP;
208         } else if(mRequestType == BluetoothDevice.REQUEST_TYPE_SIM_ACCESS) {
209             return NOTIFICATION_TAG_SAP;
210         }
211         return null;
212     }
213 
214     /**
215      * @return true user had made a choice, this method replies to the request according
216      *              to user's previous decision
217      *         false user hadnot made any choice on this device
218      */
checkUserChoice()219     private boolean checkUserChoice() {
220         boolean processed = false;
221 
222         // ignore if it is something else than phonebook/message settings it wants us to remember
223         if (mRequestType != BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS
224                 && mRequestType != BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS
225                 && mRequestType != BluetoothDevice.REQUEST_TYPE_SIM_ACCESS) {
226             if (DEBUG) Log.d(TAG, "checkUserChoice(): Unknown RequestType " + mRequestType);
227             return processed;
228         }
229 
230         LocalBluetoothManager bluetoothManager = Utils.getLocalBtManager(mContext);
231         CachedBluetoothDeviceManager cachedDeviceManager =
232                 bluetoothManager.getCachedDeviceManager();
233         CachedBluetoothDevice cachedDevice = cachedDeviceManager.findDevice(mDevice);
234         if (cachedDevice == null) {
235             cachedDevice = cachedDeviceManager.addDevice(mDevice);
236         }
237 
238         String intentName = BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY;
239 
240         if (mRequestType == BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS) {
241             int phonebookPermission = mDevice.getPhonebookAccessPermission();
242 
243             if (phonebookPermission == BluetoothDevice.ACCESS_UNKNOWN) {
244                 // Leave 'processed' as false.
245             } else if (phonebookPermission == BluetoothDevice.ACCESS_ALLOWED) {
246                 sendReplyIntentToReceiver(true);
247                 processed = true;
248             } else if (phonebookPermission == BluetoothDevice.ACCESS_REJECTED) {
249                 sendReplyIntentToReceiver(false);
250                 processed = true;
251             } else {
252                 Log.e(TAG, "Bad phonebookPermission: " + phonebookPermission);
253             }
254         } else if (mRequestType == BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS) {
255             int messagePermission = mDevice.getMessageAccessPermission();
256 
257             if (messagePermission == BluetoothDevice.ACCESS_UNKNOWN) {
258                 // Leave 'processed' as false.
259             } else if (messagePermission == BluetoothDevice.ACCESS_ALLOWED) {
260                 sendReplyIntentToReceiver(true);
261                 processed = true;
262             } else if (messagePermission == BluetoothDevice.ACCESS_REJECTED) {
263                 sendReplyIntentToReceiver(false);
264                 processed = true;
265             } else {
266                 Log.e(TAG, "Bad messagePermission: " + messagePermission);
267             }
268         } else if(mRequestType == BluetoothDevice.REQUEST_TYPE_SIM_ACCESS) {
269             int simPermission = mDevice.getSimAccessPermission();
270 
271             if (simPermission == BluetoothDevice.ACCESS_UNKNOWN) {
272                 // Leave 'processed' as false.
273             } else if (simPermission == BluetoothDevice.ACCESS_ALLOWED) {
274                 sendReplyIntentToReceiver(true);
275                 processed = true;
276             } else if (simPermission == BluetoothDevice.ACCESS_REJECTED) {
277                 sendReplyIntentToReceiver(false);
278                 processed = true;
279             } else {
280                 Log.e(TAG, "Bad simPermission: " + simPermission);
281             }
282         }
283         if (DEBUG) Log.d(TAG,"checkUserChoice(): returning " + processed);
284         return processed;
285     }
286 
sendReplyIntentToReceiver(final boolean allowed)287     private void sendReplyIntentToReceiver(final boolean allowed) {
288         Intent intent = new Intent(BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY);
289 
290         intent.putExtra(BluetoothDevice.EXTRA_CONNECTION_ACCESS_RESULT,
291                 allowed ? BluetoothDevice.CONNECTION_ACCESS_YES
292                         : BluetoothDevice.CONNECTION_ACCESS_NO);
293         intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
294         intent.putExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE, mRequestType);
295         mContext.sendBroadcast(intent, android.Manifest.permission.BLUETOOTH_ADMIN);
296     }
297 }
298