b/2234854 Fixed Bluetooth API return codes for requesting permission to enable bluetooth
Also added test app.
This commit is contained in:
@@ -16,6 +16,11 @@
|
||||
|
||||
package com.android.settings.bluetooth;
|
||||
|
||||
import com.android.internal.app.AlertActivity;
|
||||
import com.android.internal.app.AlertController;
|
||||
import com.android.settings.R;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.content.BroadcastReceiver;
|
||||
@@ -29,10 +34,6 @@ import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.android.internal.app.AlertActivity;
|
||||
import com.android.internal.app.AlertController;
|
||||
import com.android.settings.R;
|
||||
|
||||
/**
|
||||
* RequestPermissionActivity asks the user whether to enable discovery. This is
|
||||
* usually started by an application wanted to start bluetooth and or discovery
|
||||
@@ -47,12 +48,6 @@ public class RequestPermissionActivity extends AlertActivity implements
|
||||
|
||||
private static final int MAX_DISCOVERABLE_TIMEOUT = 300;
|
||||
|
||||
// Result code: Error
|
||||
public static final int RESULT_ERROR = -2;
|
||||
|
||||
// Result code: User rejected the request
|
||||
public static final int RESULT_USER_DENIED = -1;
|
||||
|
||||
// Non-error return code: BT is starting or has started successfully. Used
|
||||
// by this Activity and RequestPermissionHelperActivity
|
||||
/* package */ static final int RESULT_BT_STARTING_OR_STARTED = -1000;
|
||||
@@ -180,7 +175,7 @@ public class RequestPermissionActivity extends AlertActivity implements
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
if (requestCode != REQUEST_CODE_START_BT) {
|
||||
Log.e(TAG, "Unexpected onActivityResult " + requestCode + " " + resultCode);
|
||||
setResult(RESULT_ERROR);
|
||||
setResult(Activity.RESULT_CANCELED);
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
@@ -208,7 +203,7 @@ public class RequestPermissionActivity extends AlertActivity implements
|
||||
break;
|
||||
|
||||
case DialogInterface.BUTTON_NEGATIVE:
|
||||
setResult(RESULT_USER_DENIED);
|
||||
setResult(Activity.RESULT_CANCELED);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -218,14 +213,18 @@ public class RequestPermissionActivity extends AlertActivity implements
|
||||
|
||||
if (mEnableOnly) {
|
||||
// BT enabled. Done
|
||||
returnCode = 0;
|
||||
returnCode = Activity.RESULT_OK;
|
||||
} else if (mLocalManager.getBluetoothAdapter().setScanMode(
|
||||
BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, mTimeout)) {
|
||||
// If already in discoverable mode, this will extend the timeout.
|
||||
persistDiscoverableEndTimestamp(System.currentTimeMillis() + mTimeout * 1000);
|
||||
returnCode = mTimeout;
|
||||
// Activity.RESULT_FIRST_USER should be 1
|
||||
if (returnCode < Activity.RESULT_FIRST_USER) {
|
||||
returnCode = Activity.RESULT_FIRST_USER;
|
||||
}
|
||||
} else {
|
||||
returnCode = RESULT_ERROR;
|
||||
returnCode = Activity.RESULT_CANCELED;
|
||||
}
|
||||
|
||||
setResult(returnCode);
|
||||
@@ -254,14 +253,14 @@ public class RequestPermissionActivity extends AlertActivity implements
|
||||
Log.e(TAG, "Error: this activity may be started only with intent "
|
||||
+ BluetoothAdapter.ACTION_REQUEST_ENABLE + " or "
|
||||
+ BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
|
||||
setResult(RESULT_ERROR);
|
||||
setResult(Activity.RESULT_CANCELED);
|
||||
return true;
|
||||
}
|
||||
|
||||
mLocalManager = LocalBluetoothManager.getInstance(this);
|
||||
if (mLocalManager == null) {
|
||||
Log.e(TAG, "Error: there's a problem starting bluetooth");
|
||||
setResult(RESULT_ERROR);
|
||||
setResult(Activity.RESULT_CANCELED);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -284,7 +283,7 @@ public class RequestPermissionActivity extends AlertActivity implements
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
setResult(RequestPermissionActivity.RESULT_USER_DENIED);
|
||||
setResult(Activity.RESULT_CANCELED);
|
||||
super.onBackPressed();
|
||||
}
|
||||
}
|
||||
|
@@ -16,6 +16,11 @@
|
||||
|
||||
package com.android.settings.bluetooth;
|
||||
|
||||
import com.android.internal.app.AlertActivity;
|
||||
import com.android.internal.app.AlertController;
|
||||
import com.android.settings.R;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
@@ -24,10 +29,6 @@ import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.android.internal.app.AlertActivity;
|
||||
import com.android.internal.app.AlertController;
|
||||
import com.android.settings.R;
|
||||
|
||||
/**
|
||||
* RequestPermissionHelperActivity asks the user whether to enable discovery.
|
||||
* This is usually started by RequestPermissionActivity.
|
||||
@@ -107,12 +108,12 @@ public class RequestPermissionHelperActivity extends AlertActivity implements
|
||||
|| mLocalManager.getBluetoothAdapter().enable()) {
|
||||
returnCode = RequestPermissionActivity.RESULT_BT_STARTING_OR_STARTED;
|
||||
} else {
|
||||
returnCode = RequestPermissionActivity.RESULT_ERROR;
|
||||
returnCode = Activity.RESULT_CANCELED;
|
||||
}
|
||||
break;
|
||||
|
||||
case DialogInterface.BUTTON_NEGATIVE:
|
||||
returnCode = RequestPermissionActivity.RESULT_USER_DENIED;
|
||||
returnCode = Activity.RESULT_CANCELED;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
@@ -131,14 +132,14 @@ public class RequestPermissionHelperActivity extends AlertActivity implements
|
||||
mTimeout = intent.getIntExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
|
||||
BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT);
|
||||
} else {
|
||||
setResult(RequestPermissionActivity.RESULT_ERROR);
|
||||
setResult(Activity.RESULT_CANCELED);
|
||||
return true;
|
||||
}
|
||||
|
||||
mLocalManager = LocalBluetoothManager.getInstance(this);
|
||||
if (mLocalManager == null) {
|
||||
Log.e(TAG, "Error: there's a problem starting bluetooth");
|
||||
setResult(RequestPermissionActivity.RESULT_ERROR);
|
||||
setResult(Activity.RESULT_CANCELED);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -147,7 +148,7 @@ public class RequestPermissionHelperActivity extends AlertActivity implements
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
setResult(RequestPermissionActivity.RESULT_USER_DENIED);
|
||||
setResult(Activity.RESULT_CANCELED);
|
||||
super.onBackPressed();
|
||||
}
|
||||
}
|
||||
|
@@ -17,8 +17,18 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.android.settings.tests">
|
||||
|
||||
<uses-permission android:name="android.permission.BLUETOOTH" />
|
||||
|
||||
<application>
|
||||
<uses-library android:name="android.test.runner" />
|
||||
<activity android:name="BluetoothRequestPermissionTest"
|
||||
android:label="Bluetooth Perm Test" >
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.DEFAULT" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
<instrumentation android:name="SettingsLaunchPerformance"
|
||||
|
46
tests/res/layout/bluetooth_request_permission_test.xml
Normal file
46
tests/res/layout/bluetooth_request_permission_test.xml
Normal file
@@ -0,0 +1,46 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- 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.
|
||||
-->
|
||||
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent">
|
||||
|
||||
<ListView android:id="@+id/msg_container"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="0dip"
|
||||
android:layout_weight="1"
|
||||
android:transcriptMode="normal"/>
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
style="@android:style/ButtonBar">
|
||||
|
||||
<Button android:id="@+id/enable"
|
||||
android:layout_width="0dip"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="@string/enable" />
|
||||
|
||||
<Button android:id="@+id/discover"
|
||||
android:layout_width="0dip"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="@string/discoverable" />
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
23
tests/res/values/strings.xml
Normal file
23
tests/res/values/strings.xml
Normal file
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- 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.
|
||||
-->
|
||||
|
||||
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
|
||||
<!-- Test only. Do not translate. -->
|
||||
<!-- Test only. Do not translate. -->
|
||||
<!-- Test only. Do not translate. -->
|
||||
<string name="enable">Enable</string>
|
||||
<string name="discoverable">Discoverable</string>
|
||||
</resources>
|
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* 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.tests;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.Button;
|
||||
import android.widget.ListView;
|
||||
|
||||
public class BluetoothRequestPermissionTest extends Activity {
|
||||
private static final String TAG = "BluetoothRequestPermissionTest";
|
||||
|
||||
private ArrayAdapter<String> mMsgAdapter;
|
||||
|
||||
private class BtOnClickListener implements OnClickListener {
|
||||
final boolean mEnableOnly; // enable or enable + discoverable
|
||||
|
||||
public BtOnClickListener(boolean enableOnly) {
|
||||
mEnableOnly = enableOnly;
|
||||
}
|
||||
|
||||
public void onClick(View v) {
|
||||
requestPermission(mEnableOnly);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
setContentView(R.layout.bluetooth_request_permission_test);
|
||||
|
||||
Button enable = (Button) findViewById(R.id.enable);
|
||||
enable.setOnClickListener(new BtOnClickListener(true /* enable */));
|
||||
|
||||
Button discover = (Button) findViewById(R.id.discover);
|
||||
discover.setOnClickListener(new BtOnClickListener(false /* enable & discoverable */));
|
||||
|
||||
mMsgAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
|
||||
|
||||
ListView listView = (ListView) findViewById(R.id.msg_container);
|
||||
listView.setAdapter(mMsgAdapter);
|
||||
|
||||
registerReceiver(mReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
|
||||
addMsg("Initialized");
|
||||
}
|
||||
|
||||
void requestPermission(boolean enableOnly) {
|
||||
Intent i = new Intent();
|
||||
if (enableOnly) {
|
||||
addMsg("Starting activity to enable bt");
|
||||
i.setAction(BluetoothAdapter.ACTION_REQUEST_ENABLE);
|
||||
} else {
|
||||
addMsg("Starting activity to enable bt + discovery");
|
||||
i.setAction(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
|
||||
i.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 20);
|
||||
}
|
||||
startActivityForResult(i, 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
if (requestCode != 1) {
|
||||
Log.e(TAG, "Unexpected onActivityResult " + requestCode + " " + resultCode);
|
||||
return;
|
||||
}
|
||||
|
||||
if (resultCode == Activity.RESULT_CANCELED) {
|
||||
addMsg("Result = RESULT_CANCELED");
|
||||
} else if (resultCode == Activity.RESULT_OK) {
|
||||
addMsg("Result = RESULT_OK (not expected for discovery)");
|
||||
} else {
|
||||
addMsg("Result = " + resultCode);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
unregisterReceiver(mReceiver);
|
||||
}
|
||||
|
||||
private void addMsg(String msg) {
|
||||
mMsgAdapter.add(msg);
|
||||
Log.d(TAG, "msg");
|
||||
}
|
||||
|
||||
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (intent == null)
|
||||
return;
|
||||
if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {
|
||||
String stateStr = "???";
|
||||
switch (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothDevice.ERROR)) {
|
||||
case BluetoothAdapter.STATE_OFF:
|
||||
stateStr = "off";
|
||||
break;
|
||||
case BluetoothAdapter.STATE_TURNING_ON:
|
||||
stateStr = "turning on";
|
||||
break;
|
||||
case BluetoothAdapter.STATE_ON:
|
||||
stateStr = "on";
|
||||
break;
|
||||
case BluetoothAdapter.STATE_TURNING_OFF:
|
||||
stateStr = "turning off";
|
||||
break;
|
||||
}
|
||||
addMsg("Bluetooth status = " + stateStr);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user