OSDN Git Service

DO NOT MERGE Give PBAP permission only after user confirms am: e36ca3e96d
[android-x86/packages-apps-Settings.git] / src / com / android / settings / bluetooth / BluetoothPairingController.java
1 /*
2  * Copyright (C) 2016 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 package com.android.settings.bluetooth;
17
18 import android.bluetooth.BluetoothClass;
19 import android.bluetooth.BluetoothDevice;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.text.Editable;
23 import android.util.Log;
24 import android.widget.CompoundButton;
25 import android.widget.CompoundButton.OnCheckedChangeListener;
26 import com.android.settings.R;
27 import com.android.settings.bluetooth.BluetoothPairingDialogFragment.BluetoothPairingDialogListener;
28 import com.android.settingslib.bluetooth.LocalBluetoothManager;
29 import com.android.settingslib.bluetooth.LocalBluetoothProfile;
30 import java.util.Locale;
31
32 /**
33  * A controller used by {@link BluetoothPairingDialog} to manage connection state while we try to
34  * pair with a bluetooth device. It includes methods that allow the
35  * {@link BluetoothPairingDialogFragment} to interrogate the current state as well.
36  */
37 public class BluetoothPairingController implements OnCheckedChangeListener,
38         BluetoothPairingDialogListener {
39
40     private static final String TAG = "BTPairingController";
41
42     // Different types of dialogs we can map to
43     public static final int INVALID_DIALOG_TYPE = -1;
44     public static final int USER_ENTRY_DIALOG = 0;
45     public static final int CONFIRMATION_DIALOG = 1;
46     public static final int DISPLAY_PASSKEY_DIALOG = 2;
47
48     private static final int BLUETOOTH_PIN_MAX_LENGTH = 16;
49     private static final int BLUETOOTH_PASSKEY_MAX_LENGTH = 6;
50
51     // Bluetooth dependencies for the connection we are trying to establish
52     private LocalBluetoothManager mBluetoothManager;
53     private BluetoothDevice mDevice;
54     private int mType;
55     private String mUserInput;
56     private String mPasskeyFormatted;
57     private int mPasskey;
58     private String mDeviceName;
59     private LocalBluetoothProfile mPbapClientProfile;
60     private boolean mPbapAllowed;
61
62     /**
63      * Creates an instance of a BluetoothPairingController.
64      *
65      * @param intent - must contain {@link BluetoothDevice#EXTRA_PAIRING_VARIANT}, {@link
66      * BluetoothDevice#EXTRA_PAIRING_KEY}, and {@link BluetoothDevice#EXTRA_DEVICE}. Missing extra
67      * will lead to undefined behavior.
68      */
69     public BluetoothPairingController(Intent intent, Context context) {
70         mBluetoothManager = Utils.getLocalBtManager(context);
71         mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
72
73         String message = "";
74         if (mBluetoothManager == null) {
75             throw new IllegalStateException("Could not obtain LocalBluetoothManager");
76         } else if (mDevice == null) {
77             throw new IllegalStateException("Could not find BluetoothDevice");
78         }
79
80         mType = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR);
81         mPasskey = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_KEY, BluetoothDevice.ERROR);
82         mDeviceName = mBluetoothManager.getCachedDeviceManager().getName(mDevice);
83         mPbapClientProfile = mBluetoothManager.getProfileManager().getPbapClientProfile();
84         mPasskeyFormatted = formatKey(mPasskey);
85
86     }
87
88     @Override
89     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
90         if (isChecked) {
91             mPbapAllowed = true;
92         } else {
93             mPbapAllowed = false;
94         }
95     }
96
97     @Override
98     public void onDialogPositiveClick(BluetoothPairingDialogFragment dialog) {
99         if (getDialogType() == USER_ENTRY_DIALOG) {
100             if (mPbapAllowed) {
101                 mDevice.setPhonebookAccessPermission(BluetoothDevice.ACCESS_ALLOWED);
102             } else {
103                 mDevice.setPhonebookAccessPermission(BluetoothDevice.ACCESS_REJECTED);
104             }
105             onPair(mUserInput);
106         } else {
107             onPair(null);
108         }
109     }
110
111     @Override
112     public void onDialogNegativeClick(BluetoothPairingDialogFragment dialog) {
113         mDevice.setPhonebookAccessPermission(BluetoothDevice.ACCESS_REJECTED);
114         onCancel();
115     }
116
117     /**
118      * A method for querying which bluetooth pairing dialog fragment variant this device requires.
119      *
120      * @return - The dialog view variant needed for this device.
121      */
122     public int getDialogType() {
123         switch (mType) {
124             case BluetoothDevice.PAIRING_VARIANT_PIN:
125             case BluetoothDevice.PAIRING_VARIANT_PIN_16_DIGITS:
126             case BluetoothDevice.PAIRING_VARIANT_PASSKEY:
127                 return USER_ENTRY_DIALOG;
128
129             case BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION:
130             case BluetoothDevice.PAIRING_VARIANT_CONSENT:
131             case BluetoothDevice.PAIRING_VARIANT_OOB_CONSENT:
132                 return CONFIRMATION_DIALOG;
133
134             case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY:
135             case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN:
136                 return DISPLAY_PASSKEY_DIALOG;
137
138             default:
139                 return INVALID_DIALOG_TYPE;
140         }
141     }
142
143     /**
144      * @return - A string containing the name provided by the device.
145      */
146     public String getDeviceName() {
147         return mDeviceName;
148     }
149
150     /**
151      * A method for querying if the bluetooth device has a profile already set up on this device.
152      *
153      * @return - A boolean indicating if the device has previous knowledge of a profile for this
154      * device.
155      */
156     public boolean isProfileReady() {
157         return mPbapClientProfile != null && mPbapClientProfile.isProfileReady();
158     }
159
160     /**
161      * A method for querying if the bluetooth device has access to contacts on the device.
162      *
163      * @return - A boolean indicating if the bluetooth device has permission to access the device
164      * contacts
165      */
166     public boolean getContactSharingState() {
167         switch (mDevice.getPhonebookAccessPermission()) {
168             case BluetoothDevice.ACCESS_ALLOWED:
169                 return true;
170             case BluetoothDevice.ACCESS_REJECTED:
171                 return false;
172             default:
173                 if (mDevice.getBluetoothClass().getDeviceClass()
174                         == BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE) {
175                     return true;
176                 }
177                 return false;
178         }
179     }
180
181     /**
182      * A method for querying if the provided editable is a valid passkey/pin format for this device.
183      *
184      * @param s - The passkey/pin
185      * @return - A boolean indicating if the passkey/pin is of the correct format.
186      */
187     public boolean isPasskeyValid(Editable s) {
188         boolean requires16Digits = mType == BluetoothDevice.PAIRING_VARIANT_PIN_16_DIGITS;
189         return s.length() >= 16 && requires16Digits || s.length() > 0 && !requires16Digits;
190     }
191
192     /**
193      * A method for querying what message should be shown to the user as additional text in the
194      * dialog for this device. Returns -1 to indicate a device type that does not use this message.
195      *
196      * @return - The message ID to show the user.
197      */
198     public int getDeviceVariantMessageId() {
199         switch (mType) {
200             case BluetoothDevice.PAIRING_VARIANT_PIN_16_DIGITS:
201             case BluetoothDevice.PAIRING_VARIANT_PIN:
202                 return R.string.bluetooth_enter_pin_other_device;
203
204             case BluetoothDevice.PAIRING_VARIANT_PASSKEY:
205                 return R.string.bluetooth_enter_passkey_other_device;
206
207             default:
208                 return INVALID_DIALOG_TYPE;
209         }
210     }
211
212     /**
213      * A method for querying what message hint should be shown to the user as additional text in the
214      * dialog for this device. Returns -1 to indicate a device type that does not use this message.
215      *
216      * @return - The message ID to show the user.
217      */
218     public int getDeviceVariantMessageHintId() {
219         switch (mType) {
220             case BluetoothDevice.PAIRING_VARIANT_PIN_16_DIGITS:
221                 return R.string.bluetooth_pin_values_hint_16_digits;
222
223             case BluetoothDevice.PAIRING_VARIANT_PIN:
224             case BluetoothDevice.PAIRING_VARIANT_PASSKEY:
225                 return R.string.bluetooth_pin_values_hint;
226
227             default:
228                 return INVALID_DIALOG_TYPE;
229         }
230     }
231
232     /**
233      * A method for querying the maximum passkey/pin length for this device.
234      *
235      * @return - An int indicating the maximum length
236      */
237     public int getDeviceMaxPasskeyLength() {
238         switch (mType) {
239             case BluetoothDevice.PAIRING_VARIANT_PIN_16_DIGITS:
240             case BluetoothDevice.PAIRING_VARIANT_PIN:
241                 return BLUETOOTH_PIN_MAX_LENGTH;
242
243             case BluetoothDevice.PAIRING_VARIANT_PASSKEY:
244                 return BLUETOOTH_PASSKEY_MAX_LENGTH;
245
246             default:
247                 return 0;
248         }
249
250     }
251
252     /**
253      * A method for querying if the device uses an alphanumeric passkey.
254      *
255      * @return - a boolean indicating if the passkey can be alphanumeric.
256      */
257     public boolean pairingCodeIsAlphanumeric() {
258         switch (mType) {
259             case BluetoothDevice.PAIRING_VARIANT_PASSKEY:
260                 return false;
261
262             default:
263                 return true;
264         }
265     }
266
267     /**
268      * A method used by the dialogfragment to notify the controller that the dialog has been
269      * displayed for bluetooth device types that just care about it being displayed.
270      */
271     protected void notifyDialogDisplayed() {
272         // send an OK to the framework, indicating that the dialog has been displayed.
273         if (mType == BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY) {
274             mDevice.setPairingConfirmation(true);
275         } else if (mType == BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN) {
276             byte[] pinBytes = BluetoothDevice.convertPinToBytes(mPasskeyFormatted);
277             mDevice.setPin(pinBytes);
278         }
279     }
280
281     /**
282      * A method for querying if this bluetooth device type has a key it would like displayed
283      * to the user.
284      *
285      * @return - A boolean indicating if a key exists which should be displayed to the user.
286      */
287     public boolean isDisplayPairingKeyVariant() {
288         switch (mType) {
289             case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY:
290             case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN:
291             case BluetoothDevice.PAIRING_VARIANT_OOB_CONSENT:
292                 return true;
293             default:
294                 return false;
295         }
296     }
297
298     /**
299      * A method for querying if this bluetooth device type has other content it would like displayed
300      * to the user.
301      *
302      * @return - A boolean indicating if content exists which should be displayed to the user.
303      */
304     public boolean hasPairingContent() {
305         switch (mType) {
306             case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY:
307             case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN:
308             case BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION:
309                 return true;
310
311             default:
312                 return false;
313         }
314     }
315
316     /**
317      * A method for obtaining any additional content this bluetooth device has for displaying to the
318      * user.
319      *
320      * @return - A string containing the additional content, null if none exists.
321      * @see {@link BluetoothPairingController#hasPairingContent()}
322      */
323     public String getPairingContent() {
324         if (hasPairingContent()) {
325             return mPasskeyFormatted;
326         } else {
327             return null;
328         }
329     }
330
331     /**
332      * A method that exists to allow the fragment to update the controller with input the user has
333      * provided in the fragment.
334      *
335      * @param input - A string containing the user input.
336      */
337     protected void updateUserInput(String input) {
338         mUserInput = input;
339     }
340
341     /**
342      * Returns the provided passkey in a format that this device expects. Only works for numeric
343      * passkeys/pins.
344      *
345      * @param passkey - An integer containing the passkey to format.
346      * @return - A string containing the formatted passkey/pin
347      */
348     private String formatKey(int passkey) {
349         switch (mType) {
350             case BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION:
351             case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY:
352                 return String.format(Locale.US, "%06d", passkey);
353
354             case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN:
355                 return String.format("%04d", passkey);
356
357             default:
358                 return null;
359         }
360     }
361
362     /**
363      * handles the necessary communication with the bluetooth device to establish a successful
364      * pairing
365      *
366      * @param passkey - The passkey we will attempt to pair to the device with.
367      */
368     private void onPair(String passkey) {
369         Log.d(TAG, "Pairing dialog accepted");
370         switch (mType) {
371             case BluetoothDevice.PAIRING_VARIANT_PIN:
372             case BluetoothDevice.PAIRING_VARIANT_PIN_16_DIGITS:
373                 byte[] pinBytes = BluetoothDevice.convertPinToBytes(passkey);
374                 if (pinBytes == null) {
375                     return;
376                 }
377                 mDevice.setPin(pinBytes);
378                 break;
379
380             case BluetoothDevice.PAIRING_VARIANT_PASSKEY:
381                 int pass = Integer.parseInt(passkey);
382                 mDevice.setPasskey(pass);
383                 break;
384
385             case BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION:
386             case BluetoothDevice.PAIRING_VARIANT_CONSENT:
387                 mDevice.setPairingConfirmation(true);
388                 break;
389
390             case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY:
391             case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN:
392                 // Do nothing.
393                 break;
394
395             case BluetoothDevice.PAIRING_VARIANT_OOB_CONSENT:
396                 mDevice.setRemoteOutOfBandData();
397                 break;
398
399             default:
400                 Log.e(TAG, "Incorrect pairing type received");
401         }
402     }
403
404     /**
405      * A method for properly ending communication with the bluetooth device. Will be called by the
406      * {@link BluetoothPairingDialogFragment} when it is dismissed.
407      */
408     public void onCancel() {
409         Log.d(TAG, "Pairing dialog canceled");
410         mDevice.cancelPairingUserInput();
411     }
412
413     /**
414      * A method for checking if this device is equal to another device.
415      *
416      * @param device - The other device being compared to this device.
417      * @return - A boolean indicating if the devices were equal.
418      */
419     public boolean deviceEquals(BluetoothDevice device) {
420         return mDevice == device;
421     }
422 }