Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      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
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     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.
     15  */
     16 
     17 package com.android.settings.tests;
     18 
     19 import android.app.Activity;
     20 import android.bluetooth.BluetoothAdapter;
     21 import android.bluetooth.BluetoothDevice;
     22 import android.content.BroadcastReceiver;
     23 import android.content.Context;
     24 import android.content.Intent;
     25 import android.content.IntentFilter;
     26 import android.os.Bundle;
     27 import android.util.Log;
     28 import android.view.View;
     29 import android.view.View.OnClickListener;
     30 import android.widget.ArrayAdapter;
     31 import android.widget.Button;
     32 import android.widget.ListView;
     33 import com.android.settings.tests.unit.R;
     34 
     35 public class BluetoothRequestPermissionTest extends Activity {
     36     private static final String TAG = "BluetoothRequestPermissionTest";
     37     BluetoothAdapter mAdapter;
     38     private ArrayAdapter<String> mMsgAdapter;
     39 
     40     // Discoverable button alternates between 20 second timeout and no timeout.
     41     private boolean mDiscoveryWithTimeout = true;
     42 
     43     private class BtOnClickListener implements OnClickListener {
     44         final boolean mEnableOnly; // enable or enable + discoverable
     45 
     46         public BtOnClickListener(boolean enableOnly) {
     47             mEnableOnly = enableOnly;
     48         }
     49 
     50         public void onClick(View v) {
     51             requestPermission(mEnableOnly);
     52         }
     53     }
     54 
     55     private class BtScanOnClickListener implements OnClickListener {
     56         public void onClick(View v) {
     57             Button scanButton = (Button) v;
     58             if (mAdapter.isDiscovering()) {
     59                 mAdapter.cancelDiscovery();
     60                 scanButton.setText(R.string.start_scan);
     61             } else {
     62                 mAdapter.startDiscovery();
     63                 scanButton.setText(R.string.stop_scan);
     64             }
     65         }
     66     }
     67 
     68     @Override
     69     public void onCreate(Bundle icicle) {
     70         super.onCreate(icicle);
     71         setContentView(R.layout.bluetooth_request_permission_test);
     72         mAdapter = BluetoothAdapter.getDefaultAdapter();
     73 
     74         Button enable = (Button) findViewById(R.id.enable);
     75         enable.setOnClickListener(new BtOnClickListener(true /* enable */));
     76 
     77         Button discoverable = (Button) findViewById(R.id.discoverable);
     78         discoverable.setOnClickListener(new BtOnClickListener(false /* enable & discoverable */));
     79 
     80         Button scanButton = (Button) findViewById(R.id.scan);
     81         scanButton.setOnClickListener(new BtScanOnClickListener());
     82         if (mAdapter.isDiscovering()) {
     83             scanButton.setText(R.string.stop_scan);
     84         } else {
     85             scanButton.setText(R.string.start_scan);
     86         }
     87 
     88         mMsgAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
     89 
     90         ListView listView = (ListView) findViewById(R.id.msg_container);
     91         listView.setAdapter(mMsgAdapter);
     92 
     93         IntentFilter filter = new IntentFilter();
     94         filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
     95         filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
     96         filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
     97         filter.addAction(BluetoothDevice.ACTION_FOUND);
     98         registerReceiver(mReceiver, filter);
     99         addMsg("Initialized");
    100     }
    101 
    102     void requestPermission(boolean enableOnly) {
    103         Intent i = new Intent();
    104         if (enableOnly) {
    105             addMsg("Starting activity to enable bt");
    106             i.setAction(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    107         } else {
    108             addMsg("Starting activity to enable bt + discovery");
    109             i.setAction(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    110             // Discoverability duration toggles between 20 seconds and no timeout.
    111             int timeout = (mDiscoveryWithTimeout ? 20 : 0);
    112             i.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, timeout);
    113             mDiscoveryWithTimeout = !mDiscoveryWithTimeout;
    114         }
    115         startActivityForResult(i, 1);
    116     }
    117 
    118     @Override
    119     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    120         if (requestCode != 1) {
    121             Log.e(TAG, "Unexpected onActivityResult " + requestCode + " " + resultCode);
    122             return;
    123         }
    124 
    125         if (resultCode == Activity.RESULT_CANCELED) {
    126             addMsg("Result = RESULT_CANCELED");
    127         } else if (resultCode == Activity.RESULT_OK) {
    128             addMsg("Result = RESULT_OK (not expected for discovery)");
    129         } else {
    130             addMsg("Result = " + resultCode);
    131         }
    132     }
    133 
    134     @Override
    135     protected void onDestroy() {
    136         super.onDestroy();
    137         unregisterReceiver(mReceiver);
    138     }
    139 
    140     private void addMsg(String msg) {
    141         mMsgAdapter.add(msg);
    142         Log.d(TAG, "msg");
    143     }
    144 
    145     private BroadcastReceiver mReceiver = new BroadcastReceiver() {
    146         @Override
    147         public void onReceive(Context context, Intent intent) {
    148             if (intent == null)
    149                 return;
    150             String action = intent.getAction();
    151             if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
    152                 String stateStr = "???";
    153                 switch (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothDevice.ERROR)) {
    154                     case BluetoothAdapter.STATE_OFF:
    155                         stateStr = "off";
    156                         break;
    157                     case BluetoothAdapter.STATE_TURNING_ON:
    158                         stateStr = "turning on";
    159                         break;
    160                     case BluetoothAdapter.STATE_ON:
    161                         stateStr = "on";
    162                         break;
    163                     case BluetoothAdapter.STATE_TURNING_OFF:
    164                         stateStr = "turning off";
    165                         break;
    166                 }
    167                 addMsg("Bluetooth status = " + stateStr);
    168             } else if (action.equals(BluetoothDevice.ACTION_FOUND)) {
    169                 String name = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
    170                 addMsg("Found: " + name);
    171             } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)) {
    172                 addMsg("Scan started...");
    173             } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
    174                 addMsg("Scan ended");
    175             }
    176         }
    177     };
    178 }
    179