blob: c30113e8a41a7aae86a643ea91779e845467657e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.bluetooth;
import com.android.internal.app.AlertActivity;
import com.android.internal.app.AlertController;
import com.android.settings.R;
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import android.bluetooth.BluetoothAdapter;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
/**
* RequestPermissionHelperActivity asks the user whether to enable discovery.
* This is usually started by RequestPermissionActivity.
*/
public class RequestPermissionHelperActivity extends AlertActivity implements
DialogInterface.OnClickListener {
private static final String TAG = "RequestPermissionHelperActivity";
public static final String ACTION_INTERNAL_REQUEST_BT_ON =
"com.android.settings.bluetooth.ACTION_INTERNAL_REQUEST_BT_ON";
public static final String ACTION_INTERNAL_REQUEST_BT_ON_AND_DISCOVERABLE =
"com.android.settings.bluetooth.ACTION_INTERNAL_REQUEST_BT_ON_AND_DISCOVERABLE";
private LocalBluetoothAdapter mLocalAdapter;
private int mTimeout;
// True if requesting BT to be turned on
// False if requesting BT to be turned on + discoverable mode
private boolean mEnableOnly;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Note: initializes mLocalAdapter and returns true on error
if (parseIntent()) {
finish();
return;
}
createDialog();
if (getResources().getBoolean(R.bool.auto_confirm_bluetooth_activation_dialog) == true) {
// dismiss dialog immediately if settings say so
onClick(null, BUTTON_POSITIVE);
dismiss();
}
}
void createDialog() {
final AlertController.AlertParams p = mAlertParams;
if (mEnableOnly) {
p.mMessage = getString(R.string.bluetooth_ask_enablement);
} else {
if (mTimeout == BluetoothDiscoverableEnabler.DISCOVERABLE_TIMEOUT_NEVER) {
p.mMessage = getString(R.string.bluetooth_ask_enablement_and_lasting_discovery);
} else {
p.mMessage = getString(R.string.bluetooth_ask_enablement_and_discovery, mTimeout);
}
}
p.mPositiveButtonText = getString(R.string.allow);
p.mPositiveButtonListener = this;
p.mNegativeButtonText = getString(R.string.deny);
p.mNegativeButtonListener = this;
setupAlert();
}
public void onClick(DialogInterface dialog, int which) {
int returnCode;
// FIXME: fix this ugly switch logic!
switch (which) {
case BUTTON_POSITIVE:
int btState = 0;
try {
// TODO There's a better way.
int retryCount = 30;
do {
btState = mLocalAdapter.getBluetoothState();
Thread.sleep(100);
} while (btState == BluetoothAdapter.STATE_TURNING_OFF && --retryCount > 0);
} catch (InterruptedException ignored) {
// don't care
}
if (btState == BluetoothAdapter.STATE_TURNING_ON
|| btState == BluetoothAdapter.STATE_ON
|| mLocalAdapter.enable()) {
returnCode = RequestPermissionActivity.RESULT_BT_STARTING_OR_STARTED;
} else {
returnCode = RESULT_CANCELED;
}
break;
case BUTTON_NEGATIVE:
returnCode = RESULT_CANCELED;
break;
default:
return;
}
setResult(returnCode);
}
/**
* Parse the received Intent and initialize mLocalBluetoothAdapter.
* @return true if an error occurred; false otherwise
*/
private boolean parseIntent() {
Intent intent = getIntent();
if (intent != null && intent.getAction().equals(ACTION_INTERNAL_REQUEST_BT_ON)) {
mEnableOnly = true;
} else if (intent != null
&& intent.getAction().equals(ACTION_INTERNAL_REQUEST_BT_ON_AND_DISCOVERABLE)) {
mEnableOnly = false;
// Value used for display purposes. Not range checking.
mTimeout = intent.getIntExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT);
} else {
setResult(RESULT_CANCELED);
return true;
}
LocalBluetoothManager manager = Utils.getLocalBtManager(this);
if (manager == null) {
Log.e(TAG, "Error: there's a problem starting Bluetooth");
setResult(RESULT_CANCELED);
return true;
}
mLocalAdapter = manager.getBluetoothAdapter();
return false;
}
@Override
public void onBackPressed() {
setResult(RESULT_CANCELED);
super.onBackPressed();
}
}
|