Home | History | Annotate | Download | only in bluetooth
      1 /*
      2  * Copyright (C) 2011 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.cts.verifier.bluetooth;
     18 
     19 import com.android.cts.verifier.PassFailButtons;
     20 import com.android.cts.verifier.R;
     21 
     22 import android.app.AlertDialog;
     23 import android.bluetooth.BluetoothAdapter;
     24 import android.content.BroadcastReceiver;
     25 import android.content.Context;
     26 import android.content.DialogInterface;
     27 import android.content.Intent;
     28 import android.content.IntentFilter;
     29 import android.os.Bundle;
     30 import android.os.Handler;
     31 import android.os.Message;
     32 import android.provider.Settings;
     33 import android.view.View;
     34 import android.view.View.OnClickListener;
     35 import android.view.Window;
     36 import android.widget.Button;
     37 import android.widget.TextView;
     38 import android.widget.Toast;
     39 
     40 public class ConnectionAccessServerActivity extends PassFailButtons.Activity {
     41 
     42     private static final String ACTION_CONNECTION_ACCESS_REQUEST =
     43             "android.bluetooth.device.action.CONNECTION_ACCESS_REQUEST";
     44 
     45     private static final int ENABLE_BLUETOOTH_REQUEST = 1;
     46 
     47     private BluetoothAdapter mBluetoothAdapter;
     48     private ConnectionAccessRequestReceiver mConnectionAccessRequestReceiver;
     49     private BluetoothChatService mChatService;
     50 
     51     @Override
     52     protected void onCreate(Bundle savedInstanceState) {
     53         super.onCreate(savedInstanceState);
     54         requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
     55         setContentView(R.layout.bt_connection_access);
     56         setInfoResources(R.string.bt_connection_access_server,
     57                 R.string.bt_connection_access_server_info, 0);
     58         setPassFailButtonClickListeners();
     59         getPassButton().setEnabled(false);
     60 
     61         View settings = findViewById(R.id.bt_settings);
     62         settings.setOnClickListener(new OnClickListener() {
     63             @Override
     64             public void onClick(View view) {
     65                 startActivity(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS));
     66             }
     67         });
     68 
     69         Button makeDiscoverableButton = (Button) findViewById(R.id.bt_make_discoverable_button);
     70         makeDiscoverableButton.setVisibility(View.VISIBLE);
     71         makeDiscoverableButton.setOnClickListener(new OnClickListener() {
     72             @Override
     73             public void onClick(View v) {
     74                 makeDiscoverable();
     75             }
     76         });
     77 
     78         mConnectionAccessRequestReceiver = new ConnectionAccessRequestReceiver();
     79         IntentFilter intentFilter = new IntentFilter(ACTION_CONNECTION_ACCESS_REQUEST);
     80         registerReceiver(mConnectionAccessRequestReceiver, intentFilter);
     81 
     82         mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
     83         if (mBluetoothAdapter.isEnabled()) {
     84             startChatService();
     85         } else {
     86             Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
     87             startActivityForResult(intent, ENABLE_BLUETOOTH_REQUEST);
     88         }
     89     }
     90 
     91     @Override
     92     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     93         super.onActivityResult(requestCode, resultCode, data);
     94         switch (requestCode) {
     95             case ENABLE_BLUETOOTH_REQUEST:
     96                 if (resultCode == RESULT_OK) {
     97                     startChatService();
     98                 } else {
     99                     setResult(RESULT_CANCELED);
    100                     finish();
    101                 }
    102                 break;
    103         }
    104     }
    105 
    106     private void startChatService() {
    107         mChatService = new BluetoothChatService(this, new ChatHandler(),
    108                 BluetoothChatService.HANDSFREE_INSECURE_UUID);
    109         boolean secure = false;
    110         mChatService.start(secure);
    111     }
    112 
    113     private void makeDiscoverable() {
    114         if (mBluetoothAdapter.getScanMode() !=
    115                 BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
    116             Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    117             intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 30);
    118             startActivity(intent);
    119         }
    120     }
    121 
    122     private class ChatHandler extends Handler {
    123         @Override
    124         public void handleMessage(Message msg) {
    125             super.handleMessage(msg);
    126             switch (msg.what) {
    127                 case BluetoothChatService.MESSAGE_STATE_CHANGE:
    128                     handleStateChange(msg);
    129                     break;
    130 
    131                 case BluetoothChatService.MESSAGE_TOAST:
    132                     handleToast(msg);
    133                     break;
    134             }
    135         }
    136     }
    137 
    138     private void handleStateChange(Message msg) {
    139         int state = msg.arg1;
    140         switch (state) {
    141             case BluetoothChatService.STATE_LISTEN:
    142                 setProgressBarIndeterminateVisibility(true);
    143                 Toast.makeText(this, R.string.bt_listening, Toast.LENGTH_SHORT).show();
    144                 break;
    145         }
    146     }
    147 
    148     private void handleToast(Message msg) {
    149         String toast = msg.getData().getString(BluetoothChatService.TOAST);
    150         Toast.makeText(this, toast, Toast.LENGTH_LONG).show();
    151     }
    152 
    153     class ConnectionAccessRequestReceiver extends BroadcastReceiver {
    154         @Override
    155         public void onReceive(Context context, Intent intent) {
    156             new AlertDialog.Builder(ConnectionAccessServerActivity.this)
    157                     .setMessage(R.string.bt_ca_dialog)
    158                     .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
    159                         @Override
    160                         public void onClick(DialogInterface dialog, int which) {
    161                             getPassButton().setEnabled(true);
    162                         }
    163                     })
    164                     .setNegativeButton(android.R.string.cancel,
    165                             new DialogInterface.OnClickListener() {
    166                         @Override
    167                         public void onClick(DialogInterface dialog, int which) {
    168                             getPassButton().setEnabled(false);
    169                         }
    170                     })
    171                     .show();
    172         }
    173     }
    174 
    175     @Override
    176     protected void onDestroy() {
    177         super.onDestroy();
    178         if (mChatService != null) {
    179             mChatService.stop();
    180         }
    181         unregisterReceiver(mConnectionAccessRequestReceiver);
    182     }
    183 }
    184