diff options
author | Michael Chan <mchan@android.com> | 2009-11-05 18:25:55 -0800 |
---|---|---|
committer | Michael Chan <mchan@android.com> | 2009-11-05 18:32:09 -0800 |
commit | 834e5993e4f2f34d5aceb3196601b30231d00b07 (patch) | |
tree | 0d80b6dee540faf255eef759f1f97e33c5c72226 /tests | |
parent | 4d004e910732565ddd449c7c525f7d6cbd353392 (diff) | |
download | packages_apps_Settings-834e5993e4f2f34d5aceb3196601b30231d00b07.zip packages_apps_Settings-834e5993e4f2f34d5aceb3196601b30231d00b07.tar.gz packages_apps_Settings-834e5993e4f2f34d5aceb3196601b30231d00b07.tar.bz2 |
b/2234854 Fixed Bluetooth API return codes for requesting permission to enable bluetooth
Also added test app.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/AndroidManifest.xml | 16 | ||||
-rw-r--r-- | tests/res/layout/bluetooth_request_permission_test.xml | 46 | ||||
-rw-r--r-- | tests/res/values/strings.xml | 23 | ||||
-rw-r--r-- | tests/src/com/android/settings/tests/BluetoothRequestPermissionTest.java | 136 |
4 files changed, 218 insertions, 3 deletions
diff --git a/tests/AndroidManifest.xml b/tests/AndroidManifest.xml index cf2e03c..8a0ce21 100644 --- a/tests/AndroidManifest.xml +++ b/tests/AndroidManifest.xml @@ -4,9 +4,9 @@ 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. @@ -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" @@ -26,4 +36,4 @@ android:label="Settings Launch Performance"> </instrumentation> -</manifest> +</manifest> diff --git a/tests/res/layout/bluetooth_request_permission_test.xml b/tests/res/layout/bluetooth_request_permission_test.xml new file mode 100644 index 0000000..8bb0daa --- /dev/null +++ b/tests/res/layout/bluetooth_request_permission_test.xml @@ -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> diff --git a/tests/res/values/strings.xml b/tests/res/values/strings.xml new file mode 100644 index 0000000..b06782f --- /dev/null +++ b/tests/res/values/strings.xml @@ -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> diff --git a/tests/src/com/android/settings/tests/BluetoothRequestPermissionTest.java b/tests/src/com/android/settings/tests/BluetoothRequestPermissionTest.java new file mode 100644 index 0000000..105c98e --- /dev/null +++ b/tests/src/com/android/settings/tests/BluetoothRequestPermissionTest.java @@ -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); + } + } + }; +} |