summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/settings/tests/BluetoothRequestPermissionTest.java
blob: 8064e3a4989ac61647afe8e33ea91cd3876b0fb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * 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";
    BluetoothAdapter mAdapter;
    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);
        }
    }

    private class BtScanOnClickListener implements OnClickListener {
        public void onClick(View v) {
            Button scanButton = (Button) v;
            if (mAdapter.isDiscovering()) {
                mAdapter.cancelDiscovery();
                scanButton.setText(R.string.start_scan);
            } else {
                mAdapter.startDiscovery();
                scanButton.setText(R.string.stop_scan);
            }
        }
    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.bluetooth_request_permission_test);
        mAdapter = BluetoothAdapter.getDefaultAdapter();

        Button enable = (Button) findViewById(R.id.enable);
        enable.setOnClickListener(new BtOnClickListener(true /* enable */));

        Button discoverable = (Button) findViewById(R.id.discoverable);
        discoverable.setOnClickListener(new BtOnClickListener(false /* enable & discoverable */));

        Button scanButton = (Button) findViewById(R.id.scan);
        scanButton.setOnClickListener(new BtScanOnClickListener());
        if (mAdapter.isDiscovering()) {
            scanButton.setText(R.string.stop_scan);
        } else {
            scanButton.setText(R.string.start_scan);
        }

        mMsgAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);

        ListView listView = (ListView) findViewById(R.id.msg_container);
        listView.setAdapter(mMsgAdapter);

        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        registerReceiver(mReceiver, filter);
        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;
            String action = intent.getAction();
            if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
                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);
            } else if (action.equals(BluetoothDevice.ACTION_FOUND)) {
                String name = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
                addMsg("Found: " + name);
            } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)) {
                addMsg("Scan started...");
            } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
                addMsg("Scan ended");
            }
        }
    };
}