Merge "Add Bluetooth toggle prompts - settings"
This commit is contained in:
@@ -2025,10 +2025,11 @@
|
|||||||
android:label="@string/bluetooth_permission_request"
|
android:label="@string/bluetooth_permission_request"
|
||||||
android:excludeFromRecents="true"
|
android:excludeFromRecents="true"
|
||||||
android:permission="android.permission.BLUETOOTH"
|
android:permission="android.permission.BLUETOOTH"
|
||||||
android:theme="@*android:style/Theme.Material.Light.Dialog.Alert">
|
android:theme="@style/BluetoothPermission">
|
||||||
<intent-filter android:priority="1">
|
<intent-filter android:priority="1">
|
||||||
<action android:name="android.bluetooth.adapter.action.REQUEST_DISCOVERABLE" />
|
<action android:name="android.bluetooth.adapter.action.REQUEST_DISCOVERABLE" />
|
||||||
<action android:name="android.bluetooth.adapter.action.REQUEST_ENABLE" />
|
<action android:name="android.bluetooth.adapter.action.REQUEST_ENABLE" />
|
||||||
|
<action android:name="android.bluetooth.adapter.action.REQUEST_DISABLE" />
|
||||||
<category android:name="android.intent.category.DEFAULT" />
|
<category android:name="android.intent.category.DEFAULT" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
</activity>
|
</activity>
|
||||||
|
@@ -263,6 +263,9 @@
|
|||||||
<!-- This string asks the user whether or not to allow an app to enable bluetooth. [CHAR LIMIT=100] -->
|
<!-- This string asks the user whether or not to allow an app to enable bluetooth. [CHAR LIMIT=100] -->
|
||||||
<string name="bluetooth_ask_enablement">An app wants to turn Bluetooth ON for this device.</string>
|
<string name="bluetooth_ask_enablement">An app wants to turn Bluetooth ON for this device.</string>
|
||||||
|
|
||||||
|
<!-- This string asks the user whether or not to allow an app to disable bluetooth. [CHAR LIMIT=100] -->
|
||||||
|
<string name="bluetooth_ask_disablement">An app wants to turn Bluetooth OFF for this device.</string>
|
||||||
|
|
||||||
<!-- Strings for asking to the user whether to allow an app to enable discovery mode -->
|
<!-- Strings for asking to the user whether to allow an app to enable discovery mode -->
|
||||||
<string name="bluetooth_ask_discovery" product="tablet">An app wants to make your tablet visible to other Bluetooth devices for <xliff:g id="timeout">%1$d</xliff:g> seconds.</string>
|
<string name="bluetooth_ask_discovery" product="tablet">An app wants to make your tablet visible to other Bluetooth devices for <xliff:g id="timeout">%1$d</xliff:g> seconds.</string>
|
||||||
<!-- Strings for asking to the user whether to allow an app to enable discovery mode -->
|
<!-- Strings for asking to the user whether to allow an app to enable discovery mode -->
|
||||||
|
@@ -314,4 +314,8 @@
|
|||||||
<item name="android:navigationBarColor">#00000000</item>
|
<item name="android:navigationBarColor">#00000000</item>
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
|
<style name="BluetoothPermission" parent="@android:style/Theme.Material.Light.Dialog.Alert">
|
||||||
|
<item name="android:windowNoTitle">true</item>
|
||||||
|
</style>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
@@ -42,61 +42,32 @@ public class RequestPermissionActivity extends Activity implements
|
|||||||
// Command line to test this
|
// Command line to test this
|
||||||
// adb shell am start -a android.bluetooth.adapter.action.REQUEST_ENABLE
|
// adb shell am start -a android.bluetooth.adapter.action.REQUEST_ENABLE
|
||||||
// adb shell am start -a android.bluetooth.adapter.action.REQUEST_DISCOVERABLE
|
// adb shell am start -a android.bluetooth.adapter.action.REQUEST_DISCOVERABLE
|
||||||
|
// adb shell am start -a android.bluetooth.adapter.action.REQUEST_DISABLE
|
||||||
|
|
||||||
private static final String TAG = "RequestPermissionActivity";
|
private static final String TAG = "RequestPermissionActivity";
|
||||||
|
|
||||||
private static final int MAX_DISCOVERABLE_TIMEOUT = 3600; // 1 hr
|
private static final int MAX_DISCOVERABLE_TIMEOUT = 3600; // 1 hr
|
||||||
|
|
||||||
// Non-error return code: BT is starting or has started successfully. Used
|
static final int REQUEST_ENABLE = 1;
|
||||||
// by this Activity and RequestPermissionHelperActivity
|
static final int REQUEST_ENABLE_DISCOVERABLE = 2;
|
||||||
/* package */ static final int RESULT_BT_STARTING_OR_STARTED = -1000;
|
static final int REQUEST_DISABLE = 3;
|
||||||
|
|
||||||
private static final int REQUEST_CODE_START_BT = 1;
|
|
||||||
|
|
||||||
private LocalBluetoothAdapter mLocalAdapter;
|
private LocalBluetoothAdapter mLocalAdapter;
|
||||||
|
|
||||||
private int mTimeout = BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT;
|
private int mTimeout = BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT;
|
||||||
|
|
||||||
/*
|
private int mRequest;
|
||||||
* True if bluetooth wasn't enabled and RequestPermissionHelperActivity was
|
|
||||||
* started to ask the user and start bt.
|
|
||||||
*
|
|
||||||
* If/when that activity returns successfully, display please wait msg then
|
|
||||||
* go away when bt has started and discovery mode has been enabled.
|
|
||||||
*/
|
|
||||||
private boolean mNeededToEnableBluetooth;
|
|
||||||
|
|
||||||
// True if requesting BT to be turned on
|
|
||||||
// False if requesting BT to be turned on + discoverable mode
|
|
||||||
private boolean mEnableOnly;
|
|
||||||
|
|
||||||
private boolean mUserConfirmed;
|
|
||||||
|
|
||||||
private AlertDialog mDialog;
|
private AlertDialog mDialog;
|
||||||
|
|
||||||
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
private BroadcastReceiver mReceiver;
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onReceive(Context context, Intent intent) {
|
|
||||||
if (intent == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (mNeededToEnableBluetooth
|
|
||||||
&& BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {
|
|
||||||
int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothDevice.ERROR);
|
|
||||||
if (state == BluetoothAdapter.STATE_ON) {
|
|
||||||
if (mUserConfirmed) {
|
|
||||||
proceedAndFinish();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
|
setResult(Activity.RESULT_CANCELED);
|
||||||
|
|
||||||
// Note: initializes mLocalAdapter and returns true on error
|
// Note: initializes mLocalAdapter and returns true on error
|
||||||
if (parseIntent()) {
|
if (parseIntent()) {
|
||||||
finish();
|
finish();
|
||||||
@@ -105,68 +76,98 @@ public class RequestPermissionActivity extends Activity implements
|
|||||||
|
|
||||||
int btState = mLocalAdapter.getState();
|
int btState = mLocalAdapter.getState();
|
||||||
|
|
||||||
switch (btState) {
|
if (mRequest == REQUEST_DISABLE) {
|
||||||
case BluetoothAdapter.STATE_OFF:
|
switch (btState) {
|
||||||
case BluetoothAdapter.STATE_TURNING_OFF:
|
case BluetoothAdapter.STATE_OFF:
|
||||||
case BluetoothAdapter.STATE_TURNING_ON:
|
case BluetoothAdapter.STATE_TURNING_OFF: {
|
||||||
/*
|
|
||||||
* Strictly speaking STATE_TURNING_ON belong with STATE_ON;
|
|
||||||
* however, BT may not be ready when the user clicks yes and we
|
|
||||||
* would fail to turn on discovery mode. By kicking this to the
|
|
||||||
* RequestPermissionHelperActivity, this class will handle that
|
|
||||||
* case via the broadcast receiver.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Start the helper activity to:
|
|
||||||
* 1) ask the user about enabling bt AND discovery
|
|
||||||
* 2) enable BT upon confirmation
|
|
||||||
*/
|
|
||||||
registerReceiver(mReceiver,
|
|
||||||
new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
|
|
||||||
Intent intent = new Intent();
|
|
||||||
intent.setClass(this, RequestPermissionHelperActivity.class);
|
|
||||||
if (mEnableOnly) {
|
|
||||||
intent.setAction(RequestPermissionHelperActivity.ACTION_INTERNAL_REQUEST_BT_ON);
|
|
||||||
} else {
|
|
||||||
intent.setAction(RequestPermissionHelperActivity.
|
|
||||||
ACTION_INTERNAL_REQUEST_BT_ON_AND_DISCOVERABLE);
|
|
||||||
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, mTimeout);
|
|
||||||
}
|
|
||||||
startActivityForResult(intent, REQUEST_CODE_START_BT);
|
|
||||||
mNeededToEnableBluetooth = true;
|
|
||||||
break;
|
|
||||||
case BluetoothAdapter.STATE_ON:
|
|
||||||
if (mEnableOnly) {
|
|
||||||
// Nothing to do. Already enabled.
|
|
||||||
proceedAndFinish();
|
proceedAndFinish();
|
||||||
} else {
|
} break;
|
||||||
// Ask the user about enabling discovery mode
|
|
||||||
createDialog();
|
case BluetoothAdapter.STATE_ON:
|
||||||
}
|
case BluetoothAdapter.STATE_TURNING_ON: {
|
||||||
break;
|
Intent intent = new Intent(this, RequestPermissionHelperActivity.class);
|
||||||
default:
|
intent.setAction(RequestPermissionHelperActivity
|
||||||
Log.e(TAG, "Unknown adapter state: " + btState);
|
.ACTION_INTERNAL_REQUEST_BT_OFF);
|
||||||
|
startActivityForResult(intent, 0);
|
||||||
|
} break;
|
||||||
|
|
||||||
|
default: {
|
||||||
|
Log.e(TAG, "Unknown adapter state: " + btState);
|
||||||
|
cancelAndFinish();
|
||||||
|
} break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
switch (btState) {
|
||||||
|
case BluetoothAdapter.STATE_OFF:
|
||||||
|
case BluetoothAdapter.STATE_TURNING_OFF:
|
||||||
|
case BluetoothAdapter.STATE_TURNING_ON: {
|
||||||
|
/*
|
||||||
|
* Strictly speaking STATE_TURNING_ON belong with STATE_ON;
|
||||||
|
* however, BT may not be ready when the user clicks yes and we
|
||||||
|
* would fail to turn on discovery mode. By kicking this to the
|
||||||
|
* RequestPermissionHelperActivity, this class will handle that
|
||||||
|
* case via the broadcast receiver.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Start the helper activity to:
|
||||||
|
* 1) ask the user about enabling bt AND discovery
|
||||||
|
* 2) enable BT upon confirmation
|
||||||
|
*/
|
||||||
|
Intent intent = new Intent(this, RequestPermissionHelperActivity.class);
|
||||||
|
intent.setAction(RequestPermissionHelperActivity.ACTION_INTERNAL_REQUEST_BT_ON);
|
||||||
|
if (mRequest == REQUEST_ENABLE_DISCOVERABLE) {
|
||||||
|
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, mTimeout);
|
||||||
|
}
|
||||||
|
startActivityForResult(intent, 0);
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case BluetoothAdapter.STATE_ON: {
|
||||||
|
if (mRequest == REQUEST_ENABLE) {
|
||||||
|
// Nothing to do. Already enabled.
|
||||||
|
proceedAndFinish();
|
||||||
|
} else {
|
||||||
|
// Ask the user about enabling discovery mode
|
||||||
|
createDialog();
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
|
||||||
|
default: {
|
||||||
|
Log.e(TAG, "Unknown adapter state: " + btState);
|
||||||
|
cancelAndFinish();
|
||||||
|
} break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createDialog() {
|
private void createDialog() {
|
||||||
|
if (getResources().getBoolean(R.bool.auto_confirm_bluetooth_activation_dialog)) {
|
||||||
|
onClick(null, DialogInterface.BUTTON_POSITIVE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
||||||
|
|
||||||
if (mNeededToEnableBluetooth) {
|
// Non-null receiver means we are toggling
|
||||||
// RequestPermissionHelperActivity has gotten confirmation from user
|
if (mReceiver != null) {
|
||||||
// to turn on BT
|
switch (mRequest) {
|
||||||
builder.setMessage(getString(R.string.bluetooth_turning_on));
|
case REQUEST_ENABLE:
|
||||||
|
case REQUEST_ENABLE_DISCOVERABLE: {
|
||||||
|
builder.setMessage(getString(R.string.bluetooth_turning_on));
|
||||||
|
} break;
|
||||||
|
|
||||||
|
default: {
|
||||||
|
builder.setMessage(getString(R.string.bluetooth_turning_off));
|
||||||
|
} break;
|
||||||
|
}
|
||||||
builder.setCancelable(false);
|
builder.setCancelable(false);
|
||||||
} else {
|
} else {
|
||||||
// Ask the user whether to turn on discovery mode or not
|
// Ask the user whether to turn on discovery mode or not
|
||||||
// For lasting discoverable mode there is a different message
|
// For lasting discoverable mode there is a different message
|
||||||
if (mTimeout == BluetoothDiscoverableEnabler.DISCOVERABLE_TIMEOUT_NEVER) {
|
if (mTimeout == BluetoothDiscoverableEnabler.DISCOVERABLE_TIMEOUT_NEVER) {
|
||||||
builder.setMessage(
|
builder.setMessage(getString(R.string.bluetooth_ask_lasting_discovery));
|
||||||
getString(R.string.bluetooth_ask_lasting_discovery));
|
|
||||||
} else {
|
} else {
|
||||||
builder.setMessage(
|
builder.setMessage(getString(R.string.bluetooth_ask_discovery, mTimeout));
|
||||||
getString(R.string.bluetooth_ask_discovery, mTimeout));
|
|
||||||
}
|
}
|
||||||
builder.setPositiveButton(getString(R.string.allow), this);
|
builder.setPositiveButton(getString(R.string.allow), this);
|
||||||
builder.setNegativeButton(getString(R.string.deny), this);
|
builder.setNegativeButton(getString(R.string.deny), this);
|
||||||
@@ -174,36 +175,44 @@ public class RequestPermissionActivity extends Activity implements
|
|||||||
|
|
||||||
mDialog = builder.create();
|
mDialog = builder.create();
|
||||||
mDialog.show();
|
mDialog.show();
|
||||||
|
|
||||||
if (getResources().getBoolean(R.bool.auto_confirm_bluetooth_activation_dialog) == true) {
|
|
||||||
// dismiss dialog immediately if settings say so
|
|
||||||
onClick(null, DialogInterface.BUTTON_POSITIVE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||||
if (requestCode != REQUEST_CODE_START_BT) {
|
if (resultCode != Activity.RESULT_OK) {
|
||||||
Log.e(TAG, "Unexpected onActivityResult " + requestCode + ' ' + resultCode);
|
cancelAndFinish();
|
||||||
setResult(RESULT_CANCELED);
|
|
||||||
finish();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (resultCode != RESULT_BT_STARTING_OR_STARTED) {
|
|
||||||
setResult(resultCode);
|
|
||||||
finish();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Back from RequestPermissionHelperActivity. User confirmed to enable
|
switch (mRequest) {
|
||||||
// BT and discoverable mode.
|
case REQUEST_ENABLE:
|
||||||
mUserConfirmed = true;
|
case REQUEST_ENABLE_DISCOVERABLE: {
|
||||||
|
if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON) {
|
||||||
|
proceedAndFinish();
|
||||||
|
} else {
|
||||||
|
// If BT is not up yet, show "Turning on Bluetooth..."
|
||||||
|
mReceiver = new StateChangeReceiver();
|
||||||
|
registerReceiver(mReceiver, new IntentFilter(
|
||||||
|
BluetoothAdapter.ACTION_STATE_CHANGED));
|
||||||
|
createDialog();
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
|
||||||
if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON) {
|
case REQUEST_DISABLE: {
|
||||||
proceedAndFinish();
|
if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_OFF) {
|
||||||
} else {
|
proceedAndFinish();
|
||||||
// If BT is not up yet, show "Turning on Bluetooth..."
|
} else {
|
||||||
createDialog();
|
// If BT is not up yet, show "Turning off Bluetooth..."
|
||||||
|
mReceiver = new StateChangeReceiver();
|
||||||
|
registerReceiver(mReceiver, new IntentFilter(
|
||||||
|
BluetoothAdapter.ACTION_STATE_CHANGED));
|
||||||
|
createDialog();
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
|
||||||
|
default: {
|
||||||
|
cancelAndFinish();
|
||||||
|
} break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -223,8 +232,8 @@ public class RequestPermissionActivity extends Activity implements
|
|||||||
private void proceedAndFinish() {
|
private void proceedAndFinish() {
|
||||||
int returnCode;
|
int returnCode;
|
||||||
|
|
||||||
if (mEnableOnly) {
|
if (mRequest == REQUEST_ENABLE || mRequest == REQUEST_DISABLE) {
|
||||||
// BT enabled. Done
|
// BT toggled. Done
|
||||||
returnCode = RESULT_OK;
|
returnCode = RESULT_OK;
|
||||||
} else if (mLocalAdapter.setScanMode(
|
} else if (mLocalAdapter.setScanMode(
|
||||||
BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, mTimeout)) {
|
BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, mTimeout)) {
|
||||||
@@ -252,16 +261,26 @@ public class RequestPermissionActivity extends Activity implements
|
|||||||
finish();
|
finish();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void cancelAndFinish() {
|
||||||
|
setResult(Activity.RESULT_CANCELED);
|
||||||
|
finish();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse the received Intent and initialize mLocalBluetoothAdapter.
|
* Parse the received Intent and initialize mLocalBluetoothAdapter.
|
||||||
* @return true if an error occurred; false otherwise
|
* @return true if an error occurred; false otherwise
|
||||||
*/
|
*/
|
||||||
private boolean parseIntent() {
|
private boolean parseIntent() {
|
||||||
Intent intent = getIntent();
|
Intent intent = getIntent();
|
||||||
if (intent != null && intent.getAction().equals(BluetoothAdapter.ACTION_REQUEST_ENABLE)) {
|
if (intent == null) {
|
||||||
mEnableOnly = true;
|
return true;
|
||||||
} else if (intent != null
|
}
|
||||||
&& intent.getAction().equals(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE)) {
|
if (intent.getAction().equals(BluetoothAdapter.ACTION_REQUEST_ENABLE)) {
|
||||||
|
mRequest = REQUEST_ENABLE;
|
||||||
|
} else if (intent.getAction().equals(BluetoothAdapter.ACTION_REQUEST_DISABLE)) {
|
||||||
|
mRequest = REQUEST_DISABLE;
|
||||||
|
} else if (intent.getAction().equals(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE)) {
|
||||||
|
mRequest = REQUEST_ENABLE_DISCOVERABLE;
|
||||||
mTimeout = intent.getIntExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
|
mTimeout = intent.getIntExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
|
||||||
BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT);
|
BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT);
|
||||||
|
|
||||||
@@ -292,8 +311,9 @@ public class RequestPermissionActivity extends Activity implements
|
|||||||
@Override
|
@Override
|
||||||
protected void onDestroy() {
|
protected void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
if (mNeededToEnableBluetooth) {
|
if (mReceiver != null) {
|
||||||
unregisterReceiver(mReceiver);
|
unregisterReceiver(mReceiver);
|
||||||
|
mReceiver = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -302,4 +322,38 @@ public class RequestPermissionActivity extends Activity implements
|
|||||||
setResult(RESULT_CANCELED);
|
setResult(RESULT_CANCELED);
|
||||||
super.onBackPressed();
|
super.onBackPressed();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private final class StateChangeReceiver extends BroadcastReceiver {
|
||||||
|
private static final long TOGGLE_TIMEOUT_MILLIS = 10000; // 10 sec
|
||||||
|
|
||||||
|
public StateChangeReceiver() {
|
||||||
|
getWindow().getDecorView().postDelayed(() -> {
|
||||||
|
if (!isFinishing() && !isDestroyed()) {
|
||||||
|
cancelAndFinish();
|
||||||
|
}
|
||||||
|
}, TOGGLE_TIMEOUT_MILLIS);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onReceive(Context context, Intent intent) {
|
||||||
|
if (intent == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final int currentState = intent.getIntExtra(
|
||||||
|
BluetoothAdapter.EXTRA_STATE, BluetoothDevice.ERROR);
|
||||||
|
switch (mRequest) {
|
||||||
|
case REQUEST_ENABLE:
|
||||||
|
case REQUEST_ENABLE_DISCOVERABLE: {
|
||||||
|
if (currentState == BluetoothAdapter.STATE_ON) {
|
||||||
|
proceedAndFinish();
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case REQUEST_DISABLE: {
|
||||||
|
if (currentState == BluetoothAdapter.STATE_OFF) {
|
||||||
|
proceedAndFinish();
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package com.android.settings.bluetooth;
|
package com.android.settings.bluetooth;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
import android.bluetooth.BluetoothAdapter;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
@@ -24,106 +25,94 @@ import android.util.Log;
|
|||||||
|
|
||||||
import com.android.internal.app.AlertActivity;
|
import com.android.internal.app.AlertActivity;
|
||||||
import com.android.internal.app.AlertController;
|
import com.android.internal.app.AlertController;
|
||||||
|
import com.android.internal.util.ArrayUtils;
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* RequestPermissionHelperActivity asks the user whether to enable discovery.
|
* RequestPermissionHelperActivity asks the user whether to toggle Bluetooth.
|
||||||
* This is usually started by RequestPermissionActivity.
|
*
|
||||||
|
* TODO: This activity isn't needed - this should be folded in RequestPermissionActivity
|
||||||
*/
|
*/
|
||||||
public class RequestPermissionHelperActivity extends AlertActivity implements
|
public class RequestPermissionHelperActivity extends AlertActivity implements
|
||||||
DialogInterface.OnClickListener {
|
DialogInterface.OnClickListener {
|
||||||
private static final String TAG = "RequestPermissionHelperActivity";
|
private static final String TAG = "RequestPermissionHelperActivity";
|
||||||
|
|
||||||
public static final String ACTION_INTERNAL_REQUEST_BT_ON =
|
public static final String ACTION_INTERNAL_REQUEST_BT_ON =
|
||||||
"com.android.settings.bluetooth.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 =
|
public static final String ACTION_INTERNAL_REQUEST_BT_OFF =
|
||||||
"com.android.settings.bluetooth.ACTION_INTERNAL_REQUEST_BT_ON_AND_DISCOVERABLE";
|
"com.android.settings.bluetooth.ACTION_INTERNAL_REQUEST_BT_OFF";
|
||||||
|
|
||||||
private LocalBluetoothAdapter mLocalAdapter;
|
private LocalBluetoothAdapter mLocalAdapter;
|
||||||
|
|
||||||
private int mTimeout;
|
private int mTimeout = -1;
|
||||||
|
|
||||||
// True if requesting BT to be turned on
|
private int mRequest;
|
||||||
// False if requesting BT to be turned on + discoverable mode
|
|
||||||
private boolean mEnableOnly;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
|
setResult(RESULT_CANCELED);
|
||||||
|
|
||||||
// Note: initializes mLocalAdapter and returns true on error
|
// Note: initializes mLocalAdapter and returns true on error
|
||||||
if (parseIntent()) {
|
if (!parseIntent()) {
|
||||||
finish();
|
finish();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
createDialog();
|
if (getResources().getBoolean(R.bool.auto_confirm_bluetooth_activation_dialog)) {
|
||||||
|
// Don't even show the dialog if configured this way
|
||||||
if (getResources().getBoolean(R.bool.auto_confirm_bluetooth_activation_dialog) == true) {
|
|
||||||
// dismiss dialog immediately if settings say so
|
|
||||||
onClick(null, BUTTON_POSITIVE);
|
onClick(null, BUTTON_POSITIVE);
|
||||||
dismiss();
|
dismiss();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
createDialog();
|
||||||
}
|
}
|
||||||
|
|
||||||
void createDialog() {
|
void createDialog() {
|
||||||
final AlertController.AlertParams p = mAlertParams;
|
final AlertController.AlertParams p = mAlertParams;
|
||||||
|
|
||||||
if (mEnableOnly) {
|
switch (mRequest) {
|
||||||
p.mMessage = getString(R.string.bluetooth_ask_enablement);
|
case RequestPermissionActivity.REQUEST_ENABLE: {
|
||||||
} else {
|
if (mTimeout < 0) {
|
||||||
if (mTimeout == BluetoothDiscoverableEnabler.DISCOVERABLE_TIMEOUT_NEVER) {
|
p.mMessage = getString(R.string.bluetooth_ask_enablement);
|
||||||
p.mMessage = getString(R.string.bluetooth_ask_enablement_and_lasting_discovery);
|
} else if (mTimeout == BluetoothDiscoverableEnabler.DISCOVERABLE_TIMEOUT_NEVER) {
|
||||||
} else {
|
p.mMessage = getString(
|
||||||
p.mMessage = getString(R.string.bluetooth_ask_enablement_and_discovery, mTimeout);
|
R.string.bluetooth_ask_enablement_and_lasting_discovery);
|
||||||
}
|
} else {
|
||||||
|
p.mMessage = getString(
|
||||||
|
R.string.bluetooth_ask_enablement_and_discovery, mTimeout);
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case RequestPermissionActivity.REQUEST_DISABLE: {
|
||||||
|
p.mMessage = getString(R.string.bluetooth_ask_disablement);
|
||||||
|
} break;
|
||||||
}
|
}
|
||||||
|
|
||||||
p.mPositiveButtonText = getString(R.string.allow);
|
p.mPositiveButtonText = getString(R.string.allow);
|
||||||
p.mPositiveButtonListener = this;
|
p.mPositiveButtonListener = this;
|
||||||
p.mNegativeButtonText = getString(R.string.deny);
|
p.mNegativeButtonText = getString(R.string.deny);
|
||||||
p.mNegativeButtonListener = this;
|
|
||||||
|
|
||||||
setupAlert();
|
setupAlert();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onClick(DialogInterface dialog, int which) {
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
int returnCode;
|
switch (mRequest) {
|
||||||
// FIXME: fix this ugly switch logic!
|
case RequestPermissionActivity.REQUEST_ENABLE:
|
||||||
switch (which) {
|
case RequestPermissionActivity.REQUEST_ENABLE_DISCOVERABLE: {
|
||||||
case BUTTON_POSITIVE:
|
mLocalAdapter.enable();
|
||||||
int btState = 0;
|
setResult(Activity.RESULT_OK);
|
||||||
|
} break;
|
||||||
|
|
||||||
try {
|
case RequestPermissionActivity.REQUEST_DISABLE: {
|
||||||
// TODO There's a better way.
|
mLocalAdapter.disable();
|
||||||
int retryCount = 30;
|
setResult(Activity.RESULT_OK);
|
||||||
do {
|
} break;
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -132,33 +121,32 @@ public class RequestPermissionHelperActivity extends AlertActivity implements
|
|||||||
*/
|
*/
|
||||||
private boolean parseIntent() {
|
private boolean parseIntent() {
|
||||||
Intent intent = getIntent();
|
Intent intent = getIntent();
|
||||||
if (intent != null && intent.getAction().equals(ACTION_INTERNAL_REQUEST_BT_ON)) {
|
if (intent == null) {
|
||||||
mEnableOnly = true;
|
return false;
|
||||||
} else if (intent != null
|
}
|
||||||
&& intent.getAction().equals(ACTION_INTERNAL_REQUEST_BT_ON_AND_DISCOVERABLE)) {
|
|
||||||
mEnableOnly = false;
|
String action = intent.getAction();
|
||||||
// Value used for display purposes. Not range checking.
|
if (ACTION_INTERNAL_REQUEST_BT_ON.equals(action)) {
|
||||||
mTimeout = intent.getIntExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
|
mRequest = RequestPermissionActivity.REQUEST_ENABLE;
|
||||||
BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT);
|
if (intent.hasExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION)) {
|
||||||
|
// Value used for display purposes. Not range checking.
|
||||||
|
mTimeout = intent.getIntExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
|
||||||
|
BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT);
|
||||||
|
}
|
||||||
|
} else if (ACTION_INTERNAL_REQUEST_BT_OFF.equals(action)) {
|
||||||
|
mRequest = RequestPermissionActivity.REQUEST_DISABLE;
|
||||||
} else {
|
} else {
|
||||||
setResult(RESULT_CANCELED);
|
return false;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
LocalBluetoothManager manager = Utils.getLocalBtManager(this);
|
LocalBluetoothManager manager = Utils.getLocalBtManager(this);
|
||||||
if (manager == null) {
|
if (manager == null) {
|
||||||
Log.e(TAG, "Error: there's a problem starting Bluetooth");
|
Log.e(TAG, "Error: there's a problem starting Bluetooth");
|
||||||
setResult(RESULT_CANCELED);
|
return false;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mLocalAdapter = manager.getBluetoothAdapter();
|
mLocalAdapter = manager.getBluetoothAdapter();
|
||||||
|
|
||||||
return false;
|
return true;
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onBackPressed() {
|
|
||||||
setResult(RESULT_CANCELED);
|
|
||||||
super.onBackPressed();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user