BluetoothPairingDialog.java revision 67efa271bbff6f7ab355f8a0f121eca29a4dbec2
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.settings.bluetooth; 18 19import android.bluetooth.BluetoothClass; 20import android.bluetooth.BluetoothDevice; 21import android.bluetooth.BluetoothIntent; 22import android.content.BroadcastReceiver; 23import android.content.Context; 24import android.content.DialogInterface; 25import android.content.Intent; 26import android.content.IntentFilter; 27import android.os.Bundle; 28import android.text.Editable; 29import android.text.InputFilter; 30import android.text.InputType; 31import android.text.TextWatcher; 32import android.text.InputFilter.LengthFilter; 33import android.util.Log; 34import android.view.View; 35import android.widget.Button; 36import android.widget.EditText; 37import android.widget.TextView; 38 39import com.android.internal.app.AlertActivity; 40import com.android.internal.app.AlertController; 41import com.android.settings.R; 42 43/** 44 * BluetoothPairingDialog asks the user to enter a PIN / Passkey / simple confirmation 45 * for pairing with a remote Bluetooth device. It is an activity that appears as a dialog. 46 */ 47public class BluetoothPairingDialog extends AlertActivity implements DialogInterface.OnClickListener, 48 TextWatcher { 49 private static final String TAG = "BluetoothPairingDialog"; 50 51 private final int BLUETOOTH_PIN_MAX_LENGTH = 16; 52 private final int BLUETOOTH_PASSKEY_MAX_LENGTH = 6; 53 private LocalBluetoothManager mLocalManager; 54 private BluetoothDevice mDevice; 55 private int mType; 56 private String mConfirmationPasskey; 57 private EditText mPairingView; 58 private Button mOkButton; 59 60 private static final String INSTANCE_KEY_PAIRING_CANCELED = "received_pairing_canceled"; 61 private boolean mReceivedPairingCanceled; 62 63 private BroadcastReceiver mReceiver = new BroadcastReceiver() { 64 @Override 65 public void onReceive(Context context, Intent intent) { 66 if (!BluetoothIntent.PAIRING_CANCEL_ACTION.equals(intent.getAction())) { 67 return; 68 } 69 70 BluetoothDevice device = intent.getParcelableExtra(BluetoothIntent.DEVICE); 71 if (device == null || device.equals(mDevice)) { 72 onReceivedPairingCanceled(); 73 } 74 } 75 }; 76 77 @Override 78 protected void onCreate(Bundle savedInstanceState) { 79 super.onCreate(savedInstanceState); 80 81 Intent intent = getIntent(); 82 if (!intent.getAction().equals(BluetoothIntent.PAIRING_REQUEST_ACTION)) 83 { 84 Log.e(TAG, 85 "Error: this activity may be started only with intent " + 86 BluetoothIntent.PAIRING_REQUEST_ACTION); 87 finish(); 88 } 89 90 mLocalManager = LocalBluetoothManager.getInstance(this); 91 mDevice = intent.getParcelableExtra(BluetoothIntent.DEVICE); 92 mType = intent.getIntExtra(BluetoothIntent.PAIRING_VARIANT, BluetoothClass.ERROR); 93 if (mType == BluetoothDevice.PAIRING_VARIANT_PIN) { 94 createUserEntryDialog(); 95 } else if (mType == BluetoothDevice.PAIRING_VARIANT_PASSKEY) { 96 createUserEntryDialog(); 97 } else if (mType == BluetoothDevice.PAIRING_VARIANT_CONFIRMATION){ 98 int passkey = 99 intent.getIntExtra(BluetoothIntent.PASSKEY, BluetoothClass.ERROR); 100 if (passkey == BluetoothClass.ERROR) { 101 Log.e(TAG, "Invalid ConfirmationPasskey received, not showing any dialog"); 102 return; 103 } 104 mConfirmationPasskey = String.format("%06d", passkey); 105 createConfirmationDialog(); 106 } else { 107 Log.e(TAG, "Incorrect pairing type received, not showing any dialog"); 108 } 109 110 /* 111 * Leave this registered through pause/resume since we still want to 112 * finish the activity in the background if pairing is canceled. 113 */ 114 registerReceiver(mReceiver, new IntentFilter(BluetoothIntent.PAIRING_CANCEL_ACTION)); 115 } 116 117 private void createUserEntryDialog() { 118 final AlertController.AlertParams p = mAlertParams; 119 p.mIconId = android.R.drawable.ic_dialog_info; 120 p.mTitle = getString(R.string.bluetooth_pin_entry); 121 p.mView = createView(); 122 p.mPositiveButtonText = getString(android.R.string.ok); 123 p.mPositiveButtonListener = this; 124 p.mNegativeButtonText = getString(android.R.string.cancel); 125 p.mNegativeButtonListener = this; 126 setupAlert(); 127 128 mOkButton = mAlert.getButton(DialogInterface.BUTTON_POSITIVE); 129 mOkButton.setEnabled(false); 130 } 131 132 private View createView() { 133 View view = getLayoutInflater().inflate(R.layout.bluetooth_pin_entry, null); 134 135 String name = mLocalManager.getCachedDeviceManager().getName(mDevice); 136 TextView messageView = (TextView) view.findViewById(R.id.message); 137 mPairingView = (EditText) view.findViewById(R.id.text); 138 mPairingView.addTextChangedListener(this); 139 140 if (mType == BluetoothDevice.PAIRING_VARIANT_PIN) { 141 messageView.setText(getString(R.string.bluetooth_enter_pin_msg, name)); 142 // Maximum of 16 characters in a PIN adb sync 143 mPairingView.setFilters(new InputFilter[] { 144 new LengthFilter(BLUETOOTH_PIN_MAX_LENGTH) }); 145 } else if (mType == BluetoothDevice.PAIRING_VARIANT_PASSKEY){ 146 messageView.setText(getString(R.string.bluetooth_enter_passkey_msg, name)); 147 // Maximum of 6 digits for passkey 148 mPairingView.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED); 149 mPairingView.setFilters(new InputFilter[] { 150 new LengthFilter(BLUETOOTH_PASSKEY_MAX_LENGTH)}); 151 } else { 152 mPairingView.setVisibility(View.GONE); 153 messageView.setText(getString(R.string.bluetooth_confirm_passkey_msg, name, 154 mConfirmationPasskey)); 155 } 156 return view; 157 } 158 159 private void createConfirmationDialog() { 160 final AlertController.AlertParams p = mAlertParams; 161 p.mIconId = android.R.drawable.ic_dialog_info; 162 p.mTitle = getString(R.string.bluetooth_pin_entry); 163 p.mView = createView(); 164 p.mPositiveButtonText = getString(R.string.bluetooth_pairing_accept); 165 p.mPositiveButtonListener = this; 166 p.mNegativeButtonText = getString(R.string.bluetooth_pairing_decline); 167 p.mNegativeButtonListener = this; 168 setupAlert(); 169 } 170 171 @Override 172 protected void onRestoreInstanceState(Bundle savedInstanceState) { 173 super.onRestoreInstanceState(savedInstanceState); 174 175 mReceivedPairingCanceled = savedInstanceState.getBoolean(INSTANCE_KEY_PAIRING_CANCELED); 176 if (mReceivedPairingCanceled) { 177 onReceivedPairingCanceled(); 178 } 179 } 180 181 @Override 182 protected void onSaveInstanceState(Bundle outState) { 183 super.onSaveInstanceState(outState); 184 185 outState.putBoolean(INSTANCE_KEY_PAIRING_CANCELED, mReceivedPairingCanceled); 186 } 187 188 @Override 189 protected void onDestroy() { 190 super.onDestroy(); 191 192 unregisterReceiver(mReceiver); 193 } 194 195 public void afterTextChanged(Editable s) { 196 if (s.length() > 0) { 197 mOkButton.setEnabled(true); 198 } 199 } 200 201 private void onReceivedPairingCanceled() { 202 mReceivedPairingCanceled = true; 203 204 TextView messageView = (TextView) findViewById(R.id.message); 205 messageView.setText(getString(R.string.bluetooth_pairing_error_message, 206 mDevice.getName())); 207 208 mPairingView.setVisibility(View.GONE); 209 mPairingView.clearFocus(); 210 mPairingView.removeTextChangedListener(this); 211 212 mOkButton = mAlert.getButton(DialogInterface.BUTTON_POSITIVE); 213 mOkButton.setEnabled(true); 214 mOkButton.setText(android.R.string.ok); 215 mAlert.getButton(DialogInterface.BUTTON_NEGATIVE).setVisibility(View.GONE); 216 } 217 218 private void onPair(String value) { 219 if (mType == BluetoothDevice.PAIRING_VARIANT_PIN) { 220 byte[] pinBytes = BluetoothDevice.convertPinToBytes(value); 221 if (pinBytes == null) { 222 return; 223 } 224 mDevice.setPin(pinBytes); 225 } else if (mType == BluetoothDevice.PAIRING_VARIANT_PASSKEY) { 226 int passkey = Integer.parseInt(value); 227 mDevice.setPasskey(passkey); 228 } else { 229 mDevice.setPairingConfirmation(true); 230 } 231 } 232 233 private void onCancel() { 234 mDevice.cancelPairingUserInput(); 235 } 236 237 public void onClick(DialogInterface dialog, int which) { 238 switch (which) { 239 case DialogInterface.BUTTON_POSITIVE: 240 onPair(mPairingView.getText().toString()); 241 break; 242 243 case DialogInterface.BUTTON_NEGATIVE: 244 onCancel(); 245 break; 246 } 247 } 248 249 /* Not used */ 250 public void beforeTextChanged(CharSequence s, int start, int count, int after) { 251 } 252 253 /* Not used */ 254 public void onTextChanged(CharSequence s, int start, int before, int count) { 255 } 256 257} 258