BluetoothPairingDialog.java revision cb6a3b3d9e39d63bbf10d947579989e44bcc0b98
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 int 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            mConfirmationPasskey =
99                intent.getIntExtra(BluetoothIntent.PASSKEY, BluetoothClass.ERROR);
100            if (mConfirmationPasskey == BluetoothClass.ERROR) {
101                Log.e(TAG, "Invalid ConfirmationPasskey received, not showing any dialog");
102                return;
103            }
104            createConfirmationDialog();
105        } else {
106            Log.e(TAG, "Incorrect pairing type received, not showing any dialog");
107        }
108
109        /*
110         * Leave this registered through pause/resume since we still want to
111         * finish the activity in the background if pairing is canceled.
112         */
113        registerReceiver(mReceiver, new IntentFilter(BluetoothIntent.PAIRING_CANCEL_ACTION));
114    }
115
116    private void createUserEntryDialog() {
117        final AlertController.AlertParams p = mAlertParams;
118        p.mIconId = android.R.drawable.ic_dialog_info;
119        p.mTitle = getString(R.string.bluetooth_pin_entry);
120        p.mView = createView();
121        p.mPositiveButtonText = getString(android.R.string.ok);
122        p.mPositiveButtonListener = this;
123        p.mNegativeButtonText = getString(android.R.string.cancel);
124        p.mNegativeButtonListener = this;
125        setupAlert();
126
127        mOkButton = mAlert.getButton(DialogInterface.BUTTON_POSITIVE);
128        mOkButton.setEnabled(false);
129    }
130
131    private View createView() {
132        View view = getLayoutInflater().inflate(R.layout.bluetooth_pin_entry, null);
133
134        String name = mLocalManager.getCachedDeviceManager().getName(mDevice);
135        TextView messageView = (TextView) view.findViewById(R.id.message);
136        mPairingView = (EditText) view.findViewById(R.id.text);
137        mPairingView.addTextChangedListener(this);
138
139        if (mType == BluetoothDevice.PAIRING_VARIANT_PIN) {
140            messageView.setText(getString(R.string.bluetooth_enter_pin_msg, name));
141            // Maximum of 16 characters in a PIN adb sync
142            mPairingView.setFilters(new InputFilter[] {
143                    new LengthFilter(BLUETOOTH_PIN_MAX_LENGTH) });
144        } else if (mType == BluetoothDevice.PAIRING_VARIANT_PASSKEY){
145            messageView.setText(getString(R.string.bluetooth_enter_passkey_msg, name));
146            // Maximum of 6 digits for passkey
147            mPairingView.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED);
148            mPairingView.setFilters(new InputFilter[] {
149                    new LengthFilter(BLUETOOTH_PASSKEY_MAX_LENGTH)});
150        } else {
151            mPairingView.setVisibility(View.GONE);
152            messageView.setText(getString(R.string.bluetooth_confirm_passkey_msg, name,
153                    mConfirmationPasskey));
154        }
155        return view;
156    }
157
158    private void createConfirmationDialog() {
159        final AlertController.AlertParams p = mAlertParams;
160        p.mIconId = android.R.drawable.ic_dialog_info;
161        p.mTitle = getString(R.string.bluetooth_pin_entry);
162        p.mView = createView();
163        p.mPositiveButtonText = getString(R.string.bluetooth_pairing_accept);
164        p.mPositiveButtonListener = this;
165        p.mNegativeButtonText = getString(R.string.bluetooth_pairing_decline);
166        p.mNegativeButtonListener = this;
167        setupAlert();
168    }
169
170    @Override
171    protected void onRestoreInstanceState(Bundle savedInstanceState) {
172        super.onRestoreInstanceState(savedInstanceState);
173
174        mReceivedPairingCanceled = savedInstanceState.getBoolean(INSTANCE_KEY_PAIRING_CANCELED);
175        if (mReceivedPairingCanceled) {
176            onReceivedPairingCanceled();
177        }
178    }
179
180    @Override
181    protected void onSaveInstanceState(Bundle outState) {
182        super.onSaveInstanceState(outState);
183
184        outState.putBoolean(INSTANCE_KEY_PAIRING_CANCELED, mReceivedPairingCanceled);
185    }
186
187    @Override
188    protected void onDestroy() {
189        super.onDestroy();
190
191        unregisterReceiver(mReceiver);
192    }
193
194    public void afterTextChanged(Editable s) {
195        if (s.length() > 0) {
196            mOkButton.setEnabled(true);
197        }
198    }
199
200    private void onReceivedPairingCanceled() {
201        mReceivedPairingCanceled = true;
202
203        TextView messageView = (TextView) findViewById(R.id.message);
204        messageView.setText(getString(R.string.bluetooth_pairing_error_message,
205                mDevice.getName()));
206
207        mPairingView.setVisibility(View.GONE);
208        mPairingView.clearFocus();
209        mPairingView.removeTextChangedListener(this);
210
211        mOkButton = mAlert.getButton(DialogInterface.BUTTON_POSITIVE);
212        mOkButton.setEnabled(true);
213        mOkButton.setText(android.R.string.ok);
214        mAlert.getButton(DialogInterface.BUTTON_NEGATIVE).setVisibility(View.GONE);
215    }
216
217    private void onPair(String value) {
218        if (mType == BluetoothDevice.PAIRING_VARIANT_PIN) {
219            byte[] pinBytes = BluetoothDevice.convertPinToBytes(value);
220            if (pinBytes == null) {
221                return;
222            }
223            mDevice.setPin(pinBytes);
224        } else if (mType == BluetoothDevice.PAIRING_VARIANT_PASSKEY) {
225            int passkey = Integer.getInteger(value);
226            mDevice.setPasskey(passkey);
227        } else {
228            mDevice.setPairingConfirmation(true);
229        }
230    }
231
232    private void onCancel() {
233        mDevice.cancelPairingUserInput();
234    }
235
236    public void onClick(DialogInterface dialog, int which) {
237        switch (which) {
238            case DialogInterface.BUTTON_POSITIVE:
239                onPair(mPairingView.getText().toString());
240                break;
241
242            case DialogInterface.BUTTON_NEGATIVE:
243                onCancel();
244                break;
245        }
246    }
247
248    /* Not used */
249    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
250    }
251
252    /* Not used */
253    public void onTextChanged(CharSequence s, int start, int before, int count) {
254    }
255
256}
257