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 com.android.internal.app.AlertActivity;
20 import com.android.internal.app.AlertController;
21 import com.android.settings.R;
23 import android.app.Activity;
24 import android.bluetooth.BluetoothAdapter;
25 import android.content.DialogInterface;
26 import android.content.Intent;
27 import android.os.Bundle;
28 import android.util.Log;
29 import android.view.View;
30 import android.widget.TextView;
33 * RequestPermissionHelperActivity asks the user whether to enable discovery.
34 * This is usually started by RequestPermissionActivity.
36 public class RequestPermissionHelperActivity extends AlertActivity implements
37 DialogInterface.OnClickListener {
38 private static final String TAG = "RequestPermissionHelperActivity";
40 public static final String ACTION_INTERNAL_REQUEST_BT_ON =
41 "com.android.settings.bluetooth.ACTION_INTERNAL_REQUEST_BT_ON";
43 public static final String ACTION_INTERNAL_REQUEST_BT_ON_AND_DISCOVERABLE =
44 "com.android.settings.bluetooth.ACTION_INTERNAL_REQUEST_BT_ON_AND_DISCOVERABLE";
46 private LocalBluetoothAdapter mLocalAdapter;
50 // True if requesting BT to be turned on
51 // False if requesting BT to be turned on + discoverable mode
52 private boolean mEnableOnly;
55 protected void onCreate(Bundle savedInstanceState) {
56 super.onCreate(savedInstanceState);
58 // Note: initializes mLocalAdapter and returns true on error
68 final AlertController.AlertParams p = mAlertParams;
69 p.mIconId = android.R.drawable.ic_dialog_info;
70 p.mTitle = getString(R.string.bluetooth_permission_request);
72 View view = getLayoutInflater().inflate(R.layout.bluetooth_discoverable, null);
74 TextView tv = (TextView) view.findViewById(R.id.message);
77 tv.setText(getString(R.string.bluetooth_ask_enablement));
79 if (mTimeout == BluetoothDiscoverableEnabler.DISCOVERABLE_TIMEOUT_NEVER) {
80 tv.setText(getString(R.string.bluetooth_ask_enablement_and_lasting_discovery));
82 tv.setText(getString(R.string.bluetooth_ask_enablement_and_discovery, mTimeout));
86 p.mPositiveButtonText = getString(R.string.yes);
87 p.mPositiveButtonListener = this;
88 p.mNegativeButtonText = getString(R.string.no);
89 p.mNegativeButtonListener = this;
94 public void onClick(DialogInterface dialog, int which) {
96 // FIXME: fix this ugly switch logic!
102 // TODO There's a better way.
105 btState = mLocalAdapter.getBluetoothState();
107 } while (btState == BluetoothAdapter.STATE_TURNING_OFF && --retryCount > 0);
108 } catch (InterruptedException ignored) {
112 if (btState == BluetoothAdapter.STATE_TURNING_ON
113 || btState == BluetoothAdapter.STATE_ON
114 || mLocalAdapter.enable()) {
115 returnCode = RequestPermissionActivity.RESULT_BT_STARTING_OR_STARTED;
117 returnCode = RESULT_CANCELED;
121 case BUTTON_NEGATIVE:
122 returnCode = RESULT_CANCELED;
127 setResult(returnCode);
131 * Parse the received Intent and initialize mLocalBluetoothAdapter.
132 * @return true if an error occurred; false otherwise
134 private boolean parseIntent() {
135 Intent intent = getIntent();
136 if (intent != null && intent.getAction().equals(ACTION_INTERNAL_REQUEST_BT_ON)) {
138 } else if (intent != null
139 && intent.getAction().equals(ACTION_INTERNAL_REQUEST_BT_ON_AND_DISCOVERABLE)) {
141 // Value used for display purposes. Not range checking.
142 mTimeout = intent.getIntExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
143 BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT);
145 setResult(RESULT_CANCELED);
149 LocalBluetoothManager manager = LocalBluetoothManager.getInstance(this);
150 if (manager == null) {
151 Log.e(TAG, "Error: there's a problem starting Bluetooth");
152 setResult(RESULT_CANCELED);
155 mLocalAdapter = manager.getBluetoothAdapter();
161 public void onBackPressed() {
162 setResult(RESULT_CANCELED);
163 super.onBackPressed();