2 * Copyright (C) 2009 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.annotation.NonNull;
20 import android.app.Activity;
21 import android.app.AlertDialog;
22 import android.bluetooth.BluetoothAdapter;
23 import android.bluetooth.BluetoothDevice;
24 import android.content.BroadcastReceiver;
25 import android.content.Context;
26 import android.content.DialogInterface;
27 import android.content.Intent;
28 import android.content.IntentFilter;
29 import android.content.pm.ApplicationInfo;
30 import android.content.pm.PackageManager;
31 import android.os.Bundle;
32 import android.text.TextUtils;
33 import android.util.Log;
35 import com.android.settings.R;
36 import com.android.settingslib.bluetooth.BluetoothDiscoverableTimeoutReceiver;
37 import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
38 import com.android.settingslib.bluetooth.LocalBluetoothManager;
41 * RequestPermissionActivity asks the user whether to enable discovery. This is
42 * usually started by an application wanted to start bluetooth and or discovery
44 public class RequestPermissionActivity extends Activity implements
45 DialogInterface.OnClickListener {
46 // Command line to test this
47 // adb shell am start -a android.bluetooth.adapter.action.REQUEST_ENABLE
48 // adb shell am start -a android.bluetooth.adapter.action.REQUEST_DISCOVERABLE
49 // adb shell am start -a android.bluetooth.adapter.action.REQUEST_DISABLE
51 private static final String TAG = "RequestPermissionActivity";
53 private static final int MAX_DISCOVERABLE_TIMEOUT = 3600; // 1 hr
55 static final int REQUEST_ENABLE = 1;
56 static final int REQUEST_ENABLE_DISCOVERABLE = 2;
57 static final int REQUEST_DISABLE = 3;
59 private LocalBluetoothAdapter mLocalAdapter;
61 private int mTimeout = BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT;
65 private AlertDialog mDialog;
67 private BroadcastReceiver mReceiver;
69 private @NonNull CharSequence mAppLabel;
72 protected void onCreate(Bundle savedInstanceState) {
73 super.onCreate(savedInstanceState);
75 setResult(Activity.RESULT_CANCELED);
77 // Note: initializes mLocalAdapter and returns true on error
83 int btState = mLocalAdapter.getState();
85 if (mRequest == REQUEST_DISABLE) {
87 case BluetoothAdapter.STATE_OFF:
88 case BluetoothAdapter.STATE_TURNING_OFF: {
92 case BluetoothAdapter.STATE_ON:
93 case BluetoothAdapter.STATE_TURNING_ON: {
94 Intent intent = new Intent(this, RequestPermissionHelperActivity.class);
95 intent.putExtra(RequestPermissionHelperActivity.EXTRA_APP_LABEL, mAppLabel);
96 intent.setAction(RequestPermissionHelperActivity
97 .ACTION_INTERNAL_REQUEST_BT_OFF);
99 startActivityForResult(intent, 0);
103 Log.e(TAG, "Unknown adapter state: " + btState);
109 case BluetoothAdapter.STATE_OFF:
110 case BluetoothAdapter.STATE_TURNING_OFF:
111 case BluetoothAdapter.STATE_TURNING_ON: {
113 * Strictly speaking STATE_TURNING_ON belong with STATE_ON;
114 * however, BT may not be ready when the user clicks yes and we
115 * would fail to turn on discovery mode. By kicking this to the
116 * RequestPermissionHelperActivity, this class will handle that
117 * case via the broadcast receiver.
121 * Start the helper activity to:
122 * 1) ask the user about enabling bt AND discovery
123 * 2) enable BT upon confirmation
125 Intent intent = new Intent(this, RequestPermissionHelperActivity.class);
126 intent.setAction(RequestPermissionHelperActivity.ACTION_INTERNAL_REQUEST_BT_ON);
127 intent.putExtra(RequestPermissionHelperActivity.EXTRA_APP_LABEL, mAppLabel);
128 if (mRequest == REQUEST_ENABLE_DISCOVERABLE) {
129 intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, mTimeout);
131 startActivityForResult(intent, 0);
134 case BluetoothAdapter.STATE_ON: {
135 if (mRequest == REQUEST_ENABLE) {
136 // Nothing to do. Already enabled.
139 // Ask the user about enabling discovery mode
145 Log.e(TAG, "Unknown adapter state: " + btState);
152 private void createDialog() {
153 if (getResources().getBoolean(R.bool.auto_confirm_bluetooth_activation_dialog)) {
154 onClick(null, DialogInterface.BUTTON_POSITIVE);
158 AlertDialog.Builder builder = new AlertDialog.Builder(this);
160 // Non-null receiver means we are toggling
161 if (mReceiver != null) {
164 case REQUEST_ENABLE_DISCOVERABLE: {
165 builder.setMessage(getString(R.string.bluetooth_turning_on));
169 builder.setMessage(getString(R.string.bluetooth_turning_off));
172 builder.setCancelable(false);
174 // Ask the user whether to turn on discovery mode or not
175 // For lasting discoverable mode there is a different message
176 if (mTimeout == BluetoothDiscoverableEnabler.DISCOVERABLE_TIMEOUT_NEVER) {
177 CharSequence message = mAppLabel != null
178 ? getString(R.string.bluetooth_ask_lasting_discovery, mAppLabel)
179 : getString(R.string.bluetooth_ask_lasting_discovery_no_name);
180 builder.setMessage(message);
182 CharSequence message = mAppLabel != null
183 ? getString(R.string.bluetooth_ask_discovery, mAppLabel, mTimeout)
184 : getString(R.string.bluetooth_ask_discovery_no_name, mTimeout);
185 builder.setMessage(message);
187 builder.setPositiveButton(getString(R.string.allow), this);
188 builder.setNegativeButton(getString(R.string.deny), this);
191 mDialog = builder.create();
196 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
197 if (resultCode != Activity.RESULT_OK) {
204 case REQUEST_ENABLE_DISCOVERABLE: {
205 if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON) {
208 // If BT is not up yet, show "Turning on Bluetooth..."
209 mReceiver = new StateChangeReceiver();
210 registerReceiver(mReceiver, new IntentFilter(
211 BluetoothAdapter.ACTION_STATE_CHANGED));
216 case REQUEST_DISABLE: {
217 if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_OFF) {
220 // If BT is not up yet, show "Turning off Bluetooth..."
221 mReceiver = new StateChangeReceiver();
222 registerReceiver(mReceiver, new IntentFilter(
223 BluetoothAdapter.ACTION_STATE_CHANGED));
234 public void onClick(DialogInterface dialog, int which) {
236 case DialogInterface.BUTTON_POSITIVE:
240 case DialogInterface.BUTTON_NEGATIVE:
241 setResult(RESULT_CANCELED);
247 private void proceedAndFinish() {
250 if (mRequest == REQUEST_ENABLE || mRequest == REQUEST_DISABLE) {
252 returnCode = RESULT_OK;
253 } else if (mLocalAdapter.setScanMode(
254 BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, mTimeout)) {
255 // If already in discoverable mode, this will extend the timeout.
256 long endTime = System.currentTimeMillis() + (long) mTimeout * 1000;
257 LocalBluetoothPreferences.persistDiscoverableEndTimestamp(
260 BluetoothDiscoverableTimeoutReceiver.setDiscoverableAlarm(this, endTime);
262 returnCode = mTimeout;
263 // Activity.RESULT_FIRST_USER should be 1
264 if (returnCode < RESULT_FIRST_USER) {
265 returnCode = RESULT_FIRST_USER;
268 returnCode = RESULT_CANCELED;
271 if (mDialog != null) {
275 setResult(returnCode);
279 private void cancelAndFinish() {
280 setResult(Activity.RESULT_CANCELED);
285 * Parse the received Intent and initialize mLocalBluetoothAdapter.
286 * @return true if an error occurred; false otherwise
288 private boolean parseIntent() {
289 Intent intent = getIntent();
290 if (intent == null) {
293 if (intent.getAction().equals(BluetoothAdapter.ACTION_REQUEST_ENABLE)) {
294 mRequest = REQUEST_ENABLE;
295 } else if (intent.getAction().equals(BluetoothAdapter.ACTION_REQUEST_DISABLE)) {
296 mRequest = REQUEST_DISABLE;
297 } else if (intent.getAction().equals(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE)) {
298 mRequest = REQUEST_ENABLE_DISCOVERABLE;
299 mTimeout = intent.getIntExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
300 BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT);
302 Log.d(TAG, "Setting Bluetooth Discoverable Timeout = " + mTimeout);
304 if (mTimeout < 1 || mTimeout > MAX_DISCOVERABLE_TIMEOUT) {
305 mTimeout = BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT;
308 Log.e(TAG, "Error: this activity may be started only with intent "
309 + BluetoothAdapter.ACTION_REQUEST_ENABLE + " or "
310 + BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
311 setResult(RESULT_CANCELED);
315 LocalBluetoothManager manager = Utils.getLocalBtManager(this);
316 if (manager == null) {
317 Log.e(TAG, "Error: there's a problem starting Bluetooth");
318 setResult(RESULT_CANCELED);
322 String packageName = getCallingPackage();
323 if (TextUtils.isEmpty(packageName)) {
324 packageName = getIntent().getStringExtra(Intent.EXTRA_PACKAGE_NAME);
326 if (!TextUtils.isEmpty(packageName)) {
328 ApplicationInfo applicationInfo = getPackageManager().getApplicationInfo(
330 mAppLabel = applicationInfo.loadSafeLabel(getPackageManager());
331 } catch (PackageManager.NameNotFoundException e) {
332 Log.e(TAG, "Couldn't find app with package name " + packageName);
333 setResult(RESULT_CANCELED);
338 mLocalAdapter = manager.getBluetoothAdapter();
344 protected void onDestroy() {
346 if (mReceiver != null) {
347 unregisterReceiver(mReceiver);
353 public void onBackPressed() {
354 setResult(RESULT_CANCELED);
355 super.onBackPressed();
358 private final class StateChangeReceiver extends BroadcastReceiver {
359 private static final long TOGGLE_TIMEOUT_MILLIS = 10000; // 10 sec
361 public StateChangeReceiver() {
362 getWindow().getDecorView().postDelayed(() -> {
363 if (!isFinishing() && !isDestroyed()) {
366 }, TOGGLE_TIMEOUT_MILLIS);
369 public void onReceive(Context context, Intent intent) {
370 if (intent == null) {
373 final int currentState = intent.getIntExtra(
374 BluetoothAdapter.EXTRA_STATE, BluetoothDevice.ERROR);
377 case REQUEST_ENABLE_DISCOVERABLE: {
378 if (currentState == BluetoothAdapter.STATE_ON) {
383 case REQUEST_DISABLE: {
384 if (currentState == BluetoothAdapter.STATE_OFF) {