altos: spiradio debug serial is port 1, not port 0
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / DeviceListActivity.java
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 org.altusmetrum.AltosDroid;
18
19 import java.util.Set;
20 import org.altusmetrum.AltosDroid.R;
21
22 import android.app.Activity;
23 import android.bluetooth.BluetoothAdapter;
24 import android.bluetooth.BluetoothDevice;
25 import android.content.BroadcastReceiver;
26 import android.content.Context;
27 import android.content.Intent;
28 import android.content.IntentFilter;
29 import android.os.Bundle;
30 import android.util.Log;
31 import android.view.View;
32 import android.view.Window;
33 import android.view.View.OnClickListener;
34 import android.widget.AdapterView;
35 import android.widget.ArrayAdapter;
36 import android.widget.Button;
37 import android.widget.ListView;
38 import android.widget.TextView;
39 import android.widget.AdapterView.OnItemClickListener;
40
41 /**
42  * This Activity appears as a dialog. It lists any paired devices and
43  * devices detected in the area after discovery. When a device is chosen
44  * by the user, the MAC address of the device is sent back to the parent
45  * Activity in the result Intent.
46  */
47 public class DeviceListActivity extends Activity {
48     // Debugging
49     private static final String TAG = "DeviceListActivity";
50     private static final boolean D = true;
51
52     // Return Intent extra
53     public static String EXTRA_DEVICE_ADDRESS = "device_address";
54
55     // Member fields
56     private BluetoothAdapter mBtAdapter;
57     private ArrayAdapter<String> mPairedDevicesArrayAdapter;
58     private ArrayAdapter<String> mNewDevicesArrayAdapter;
59
60     @Override
61     protected void onCreate(Bundle savedInstanceState) {
62         super.onCreate(savedInstanceState);
63
64         // Setup the window
65         requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
66         setContentView(R.layout.device_list);
67
68         // Set result CANCELED incase the user backs out
69         setResult(Activity.RESULT_CANCELED);
70
71         // Initialize the button to perform device discovery
72         Button scanButton = (Button) findViewById(R.id.button_scan);
73         scanButton.setOnClickListener(new OnClickListener() {
74             public void onClick(View v) {
75                 doDiscovery();
76                 v.setVisibility(View.GONE);
77             }
78         });
79
80         // Initialize array adapters. One for already paired devices and
81         // one for newly discovered devices
82         mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);
83         mNewDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);
84
85         // Find and set up the ListView for paired devices
86         ListView pairedListView = (ListView) findViewById(R.id.paired_devices);
87         pairedListView.setAdapter(mPairedDevicesArrayAdapter);
88         pairedListView.setOnItemClickListener(mDeviceClickListener);
89
90         // Find and set up the ListView for newly discovered devices
91         ListView newDevicesListView = (ListView) findViewById(R.id.new_devices);
92         newDevicesListView.setAdapter(mNewDevicesArrayAdapter);
93         newDevicesListView.setOnItemClickListener(mDeviceClickListener);
94
95         // Register for broadcasts when a device is discovered
96         IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
97         this.registerReceiver(mReceiver, filter);
98
99         // Register for broadcasts when discovery has finished
100         filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
101         this.registerReceiver(mReceiver, filter);
102
103         // Get the local Bluetooth adapter
104         mBtAdapter = BluetoothAdapter.getDefaultAdapter();
105
106         // Get a set of currently paired devices
107         Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();
108
109         // If there are paired devices, add each one to the ArrayAdapter
110         if (pairedDevices.size() > 0) {
111             findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
112             for (BluetoothDevice device : pairedDevices) {
113                 mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
114             }
115         } else {
116             String noDevices = getResources().getText(R.string.none_paired).toString();
117             mPairedDevicesArrayAdapter.add(noDevices);
118         }
119     }
120
121     @Override
122     protected void onDestroy() {
123         super.onDestroy();
124
125         // Make sure we're not doing discovery anymore
126         if (mBtAdapter != null) {
127             mBtAdapter.cancelDiscovery();
128         }
129
130         // Unregister broadcast listeners
131         this.unregisterReceiver(mReceiver);
132     }
133
134     /**
135      * Start device discover with the BluetoothAdapter
136      */
137     private void doDiscovery() {
138         if (D) Log.d(TAG, "doDiscovery()");
139
140         // Indicate scanning in the title
141         setProgressBarIndeterminateVisibility(true);
142         setTitle(R.string.scanning);
143
144         // Turn on sub-title for new devices
145         findViewById(R.id.title_new_devices).setVisibility(View.VISIBLE);
146
147         // If we're already discovering, stop it
148         if (mBtAdapter.isDiscovering()) {
149             mBtAdapter.cancelDiscovery();
150         }
151
152         // Request discover from BluetoothAdapter
153         mBtAdapter.startDiscovery();
154     }
155
156     // The on-click listener for all devices in the ListViews
157     private OnItemClickListener mDeviceClickListener = new OnItemClickListener() {
158         public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
159             // Cancel discovery because it's costly and we're about to connect
160             mBtAdapter.cancelDiscovery();
161
162             // Get the device MAC address, which is the last 17 chars in the View
163             String info = ((TextView) v).getText().toString();
164             String address = info.substring(info.length() - 17);
165
166             // Create the result Intent and include the MAC address
167             Intent intent = new Intent();
168             intent.putExtra(EXTRA_DEVICE_ADDRESS, address);
169
170             // Set result and finish this Activity
171             setResult(Activity.RESULT_OK, intent);
172             finish();
173         }
174     };
175
176     // The BroadcastReceiver that listens for discovered devices and
177     // changes the title when discovery is finished
178     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
179         @Override
180         public void onReceive(Context context, Intent intent) {
181             String action = intent.getAction();
182
183             // When discovery finds a device
184             if (BluetoothDevice.ACTION_FOUND.equals(action)) {
185                 // Get the BluetoothDevice object from the Intent
186                 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
187                 // If it's already paired, skip it, because it's been listed already
188                 if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
189                     mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
190                 }
191             // When discovery is finished, change the Activity title
192             } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
193                 setProgressBarIndeterminateVisibility(false);
194                 setTitle(R.string.select_device);
195                 if (mNewDevicesArrayAdapter.getCount() == 0) {
196                     String noDevices = getResources().getText(R.string.none_found).toString();
197                     mNewDevicesArrayAdapter.add(noDevices);
198                 }
199             }
200         }
201     };
202
203 }