2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.bluetooth;
19 import android.bluetooth.BluetoothDevice;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.DialogInterface;
23 import android.content.Intent;
24 import android.content.IntentFilter;
25 import android.os.Bundle;
26 import android.preference.Preference;
27 import android.util.Log;
28 import android.view.View;
29 import android.widget.TextView;
30 import android.widget.Button;
32 import com.android.internal.app.AlertActivity;
33 import com.android.internal.app.AlertController;
35 import com.android.settings.R;
38 * BluetoothPermissionActivity shows a dialog for accepting incoming
39 * profile connection request from untrusted devices.
40 * It is also used to show a dialogue for accepting incoming phonebook
41 * read request. The request could be initiated by PBAP PCE or by HF AT+CPBR.
43 public class BluetoothPermissionActivity extends AlertActivity implements
44 DialogInterface.OnClickListener, Preference.OnPreferenceChangeListener {
45 private static final String TAG = "BluetoothPermissionActivity";
46 private static final boolean DEBUG = Utils.D;
49 private TextView messageView;
50 private Button mOkButton;
51 private BluetoothDevice mDevice;
52 private String mReturnPackage = null;
53 private String mReturnClass = null;
55 private int mRequestType = 0;
56 private BroadcastReceiver mReceiver = new BroadcastReceiver() {
58 public void onReceive(Context context, Intent intent) {
59 String action = intent.getAction();
60 if (action.equals(BluetoothDevice.ACTION_CONNECTION_ACCESS_CANCEL)) {
61 int requestType = intent.getIntExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE,
62 BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS);
63 if (requestType != mRequestType) return;
64 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
65 if (mDevice.equals(device)) dismissDialog();
69 private boolean mReceiverRegistered = false;
71 private void dismissDialog() {
76 protected void onCreate(Bundle savedInstanceState) {
77 super.onCreate(savedInstanceState);
79 Intent i = getIntent();
80 String action = i.getAction();
81 if (!action.equals(BluetoothDevice.ACTION_CONNECTION_ACCESS_REQUEST)) {
82 Log.e(TAG, "Error: this activity may be started only with intent "
83 + "ACTION_CONNECTION_ACCESS_REQUEST");
88 mDevice = i.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
89 mReturnPackage = i.getStringExtra(BluetoothDevice.EXTRA_PACKAGE_NAME);
90 mReturnClass = i.getStringExtra(BluetoothDevice.EXTRA_CLASS_NAME);
91 mRequestType = i.getIntExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE,
92 BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS);
94 if(DEBUG) Log.i(TAG, "onCreate() Request type: " + mRequestType);
96 if (mRequestType == BluetoothDevice.REQUEST_TYPE_PROFILE_CONNECTION) {
97 showDialog(getString(R.string.bluetooth_connection_permission_request), mRequestType);
98 } else if (mRequestType == BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS) {
99 showDialog(getString(R.string.bluetooth_phonebook_request), mRequestType);
100 } else if (mRequestType == BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS) {
101 showDialog(getString(R.string.bluetooth_map_request), mRequestType);
104 Log.e(TAG, "Error: bad request type: " + mRequestType);
108 registerReceiver(mReceiver,
109 new IntentFilter(BluetoothDevice.ACTION_CONNECTION_ACCESS_CANCEL));
110 mReceiverRegistered = true;
114 private void showDialog(String title, int requestType)
116 final AlertController.AlertParams p = mAlertParams;
118 if(DEBUG) Log.i(TAG, "showDialog() Request type: " + mRequestType + " this: " + this);
121 case BluetoothDevice.REQUEST_TYPE_PROFILE_CONNECTION:
122 p.mView = createConnectionDialogView();
124 case BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS:
125 p.mView = createPhonebookDialogView();
127 case BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS:
128 p.mView = createMapDialogView();
131 p.mPositiveButtonText = getString(R.string.yes);
132 p.mPositiveButtonListener = this;
133 p.mNegativeButtonText = getString(R.string.no);
134 p.mNegativeButtonListener = this;
135 mOkButton = mAlert.getButton(DialogInterface.BUTTON_POSITIVE);
140 public void onBackPressed() {
141 /*we need an answer so ignore back button presses during auth */
142 if(DEBUG) Log.i(TAG, "Back button pressed! ignoring");
145 private String createRemoteName()
147 String mRemoteName = mDevice != null ? mDevice.getAliasName() : null;
149 if (mRemoteName == null) mRemoteName = getString(R.string.unknown);
153 // TODO(edjee): createConnectionDialogView, createPhonebookDialogView and createMapDialogView
154 // are similar. Refactor them into one method.
155 // Also, the string resources bluetooth_remember_choice and bluetooth_pb_remember_choice should
157 private View createConnectionDialogView() {
158 String mRemoteName = createRemoteName();
159 mView = getLayoutInflater().inflate(R.layout.bluetooth_access, null);
160 messageView = (TextView)mView.findViewById(R.id.message);
161 messageView.setText(getString(R.string.bluetooth_connection_dialog_text,
166 private View createPhonebookDialogView() {
167 String mRemoteName = createRemoteName();
168 mView = getLayoutInflater().inflate(R.layout.bluetooth_access, null);
169 messageView = (TextView)mView.findViewById(R.id.message);
170 messageView.setText(getString(R.string.bluetooth_pb_acceptance_dialog_text,
171 mRemoteName, mRemoteName));
175 private View createMapDialogView() {
176 String mRemoteName = createRemoteName();
177 mView = getLayoutInflater().inflate(R.layout.bluetooth_access, null);
178 messageView = (TextView)mView.findViewById(R.id.message);
179 messageView.setText(getString(R.string.bluetooth_map_acceptance_dialog_text,
180 mRemoteName, mRemoteName));
184 private void onPositive() {
185 if (DEBUG) Log.d(TAG, "onPositive");
186 sendReplyIntentToReceiver(true, true);
190 private void onNegative() {
191 if (DEBUG) Log.d(TAG, "onNegative");
193 boolean always = true;
194 if (mRequestType == BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS) {
195 LocalBluetoothManager bluetoothManager = LocalBluetoothManager.getInstance(this);
196 CachedBluetoothDeviceManager cachedDeviceManager =
197 bluetoothManager.getCachedDeviceManager();
198 CachedBluetoothDevice cachedDevice = cachedDeviceManager.findDevice(mDevice);
199 if (cachedDevice == null) {
200 cachedDevice = cachedDeviceManager.addDevice(bluetoothManager.getBluetoothAdapter(),
201 bluetoothManager.getProfileManager(),
204 always = cachedDevice.checkAndIncreaseMessageRejectionCount();
207 sendReplyIntentToReceiver(false, always);
210 private void sendReplyIntentToReceiver(final boolean allowed, final boolean always) {
211 Intent intent = new Intent(BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY);
213 if (mReturnPackage != null && mReturnClass != null) {
214 intent.setClassName(mReturnPackage, mReturnClass);
216 if (DEBUG) Log.i(TAG, "sendReplyIntentToReceiver() Request type: " + mRequestType +
217 " mReturnPackage" + mReturnPackage + " mReturnClass" + mReturnClass);
219 intent.putExtra(BluetoothDevice.EXTRA_CONNECTION_ACCESS_RESULT,
220 allowed ? BluetoothDevice.CONNECTION_ACCESS_YES
221 : BluetoothDevice.CONNECTION_ACCESS_NO);
222 intent.putExtra(BluetoothDevice.EXTRA_ALWAYS_ALLOWED, always);
223 intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
224 intent.putExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE, mRequestType);
225 sendBroadcast(intent, android.Manifest.permission.BLUETOOTH_ADMIN);
228 public void onClick(DialogInterface dialog, int which) {
230 case DialogInterface.BUTTON_POSITIVE:
234 case DialogInterface.BUTTON_NEGATIVE:
243 protected void onDestroy() {
245 if (mReceiverRegistered) {
246 unregisterReceiver(mReceiver);
247 mReceiverRegistered = false;
251 public boolean onPreferenceChange(Preference preference, Object newValue) {