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