altos: Fix .sdcdbrc file for telebt-v0.1
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / AltosDroid.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 android.app.Activity;
20 import android.bluetooth.BluetoothAdapter;
21 import android.bluetooth.BluetoothDevice;
22 import android.content.Intent;
23 import android.os.Bundle;
24 import android.os.Handler;
25 import android.os.Message;
26 import android.util.Log;
27 import android.view.KeyEvent;
28 import android.view.Menu;
29 import android.view.MenuInflater;
30 import android.view.MenuItem;
31 import android.view.View;
32 import android.view.Window;
33 import android.view.View.OnClickListener;
34 import android.view.inputmethod.EditorInfo;
35 import android.widget.ArrayAdapter;
36 import android.widget.Button;
37 import android.widget.EditText;
38 import android.widget.ListView;
39 import android.widget.TextView;
40 import android.widget.Toast;
41 import org.altusmetrum.AltosDroid.R;
42 import org.altusmetrum.AltosLib.*;
43
44 /**
45  * This is the main Activity that displays the current chat session.
46  */
47 public class AltosDroid extends Activity {
48     // Debugging
49     private static final String TAG = "BluetoothChat";
50     private static final boolean D = true;
51
52     private static final AltosLine q = new AltosLine();
53
54     // Message types sent from the BluetoothChatService Handler
55     public static final int MESSAGE_STATE_CHANGE = 1;
56     public static final int MESSAGE_READ = 2;
57     public static final int MESSAGE_WRITE = 3;
58     public static final int MESSAGE_DEVICE_NAME = 4;
59     public static final int MESSAGE_TOAST = 5;
60
61     // Key names received from the BluetoothChatService Handler
62     public static final String DEVICE_NAME = "device_name";
63     public static final String TOAST = "toast";
64
65     // Intent request codes
66     private static final int REQUEST_CONNECT_DEVICE_SECURE = 1;
67     private static final int REQUEST_CONNECT_DEVICE_INSECURE = 2;
68     private static final int REQUEST_ENABLE_BT = 3;
69
70     // Layout Views
71     private TextView mTitle;
72     private ListView mConversationView;
73     private EditText mOutEditText;
74     private Button mSendButton;
75
76     // Name of the connected device
77     private String mConnectedDeviceName = null;
78     // Array adapter for the conversation thread
79     private ArrayAdapter<String> mConversationArrayAdapter;
80     // String buffer for outgoing messages
81     private StringBuffer mOutStringBuffer;
82     // Local Bluetooth adapter
83     private BluetoothAdapter mBluetoothAdapter = null;
84     // Member object for the chat services
85     private BluetoothChatService mChatService = null;
86
87
88     @Override
89     public void onCreate(Bundle savedInstanceState) {
90         super.onCreate(savedInstanceState);
91         if(D) Log.e(TAG, "+++ ON CREATE +++");
92
93         // Set up the window layout
94         requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
95         setContentView(R.layout.main);
96         getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
97
98         // Set up the custom title
99         mTitle = (TextView) findViewById(R.id.title_left_text);
100         mTitle.setText(R.string.app_name);
101         mTitle = (TextView) findViewById(R.id.title_right_text);
102
103         // Get local Bluetooth adapter
104         mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
105
106         // If the adapter is null, then Bluetooth is not supported
107         if (mBluetoothAdapter == null) {
108             Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
109             finish();
110             return;
111         }
112     }
113
114     @Override
115     public void onStart() {
116         super.onStart();
117         if(D) Log.e(TAG, "++ ON START ++");
118
119         // If BT is not on, request that it be enabled.
120         // setupChat() will then be called during onActivityResult
121         if (!mBluetoothAdapter.isEnabled()) {
122             Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
123             startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
124         // Otherwise, setup the chat session
125         } else {
126             if (mChatService == null) setupChat();
127         }
128     }
129
130     @Override
131     public synchronized void onResume() {
132         super.onResume();
133         if(D) Log.e(TAG, "+ ON RESUME +");
134
135         // Performing this check in onResume() covers the case in which BT was
136         // not enabled during onStart(), so we were paused to enable it...
137         // onResume() will be called when ACTION_REQUEST_ENABLE activity returns.
138         if (mChatService != null) {
139             // Only if the state is STATE_NONE, do we know that we haven't started already
140             if (mChatService.getState() == BluetoothChatService.STATE_NONE) {
141               // Start the Bluetooth chat services
142               mChatService.start();
143             }
144         }
145     }
146
147     private void setupChat() {
148         Log.d(TAG, "setupChat()");
149
150         // Initialize the array adapter for the conversation thread
151         mConversationArrayAdapter = new ArrayAdapter<String>(this, R.layout.message);
152         mConversationView = (ListView) findViewById(R.id.in);
153         mConversationView.setAdapter(mConversationArrayAdapter);
154
155         // Initialize the compose field with a listener for the return key
156         mOutEditText = (EditText) findViewById(R.id.edit_text_out);
157         mOutEditText.setOnEditorActionListener(mWriteListener);
158
159         // Initialize the send button with a listener that for click events
160         mSendButton = (Button) findViewById(R.id.button_send);
161         mSendButton.setOnClickListener(new OnClickListener() {
162             public void onClick(View v) {
163                 // Send a message using content of the edit text widget
164                 TextView view = (TextView) findViewById(R.id.edit_text_out);
165                 String message = view.getText().toString();
166                 sendMessage(message);
167             }
168         });
169
170         // Initialize the BluetoothChatService to perform bluetooth connections
171         mChatService = new BluetoothChatService(this, mHandler);
172
173         // Initialize the buffer for outgoing messages
174         mOutStringBuffer = new StringBuffer("");
175     }
176
177     @Override
178     public synchronized void onPause() {
179         super.onPause();
180         if(D) Log.e(TAG, "- ON PAUSE -");
181     }
182
183     @Override
184     public void onStop() {
185         super.onStop();
186         if(D) Log.e(TAG, "-- ON STOP --");
187     }
188
189     @Override
190     public void onDestroy() {
191         super.onDestroy();
192         // Stop the Bluetooth chat services
193         if (mChatService != null) mChatService.stop();
194         if(D) Log.e(TAG, "--- ON DESTROY ---");
195     }
196
197     private void ensureDiscoverable() {
198         if(D) Log.d(TAG, "ensure discoverable");
199         if (mBluetoothAdapter.getScanMode() !=
200             BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
201             Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
202             discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
203             startActivity(discoverableIntent);
204         }
205     }
206
207     /**
208      * Sends a message.
209      * @param message  A string of text to send.
210      */
211     private void sendMessage(String message) {
212         // Check that we're actually connected before trying anything
213         if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) {
214             Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show();
215             return;
216         }
217
218         // Check that there's actually something to send
219         if (message.length() > 0) {
220             // Get the message bytes and tell the BluetoothChatService to write
221             byte[] send = message.getBytes();
222             mChatService.write(send);
223
224             // Reset out string buffer to zero and clear the edit text field
225             mOutStringBuffer.setLength(0);
226             mOutEditText.setText(mOutStringBuffer);
227         }
228     }
229
230     // The action listener for the EditText widget, to listen for the return key
231     private TextView.OnEditorActionListener mWriteListener =
232         new TextView.OnEditorActionListener() {
233         public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
234             // If the action is a key-up event on the return key, send the message
235             if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_UP) {
236                 String message = view.getText().toString();
237                 sendMessage(message);
238             }
239             if(D) Log.i(TAG, "END onEditorAction");
240             return true;
241         }
242     };
243
244     // The Handler that gets information back from the BluetoothChatService
245     private final Handler mHandler = new Handler() {
246         @Override
247         public void handleMessage(Message msg) {
248             switch (msg.what) {
249             case MESSAGE_STATE_CHANGE:
250                 if(D) Log.i(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
251                 switch (msg.arg1) {
252                 case BluetoothChatService.STATE_CONNECTED:
253                     mTitle.setText(R.string.title_connected_to);
254                     mTitle.append(mConnectedDeviceName);
255                     mConversationArrayAdapter.clear();
256                     break;
257                 case BluetoothChatService.STATE_CONNECTING:
258                     mTitle.setText(R.string.title_connecting);
259                     break;
260                 case BluetoothChatService.STATE_READY:
261                 case BluetoothChatService.STATE_NONE:
262                     mTitle.setText(R.string.title_not_connected);
263                     break;
264                 }
265                 break;
266             case MESSAGE_WRITE:
267                 byte[] writeBuf = (byte[]) msg.obj;
268                 // construct a string from the buffer
269                 String writeMessage = new String(writeBuf);
270                 mConversationArrayAdapter.add("Me:  " + writeMessage);
271                 break;
272             case MESSAGE_READ:
273                 byte[] readBuf = (byte[]) msg.obj;
274                 // construct a string from the valid bytes in the buffer
275                 String readMessage = new String(readBuf, 0, msg.arg1);
276                 mConversationArrayAdapter.add(mConnectedDeviceName+":  " + readMessage);
277                 break;
278             case MESSAGE_DEVICE_NAME:
279                 // save the connected device's name
280                 mConnectedDeviceName = msg.getData().getString(DEVICE_NAME);
281                 Toast.makeText(getApplicationContext(), "Connected to "
282                                + mConnectedDeviceName, Toast.LENGTH_SHORT).show();
283                 break;
284             case MESSAGE_TOAST:
285                 Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST),
286                                Toast.LENGTH_SHORT).show();
287                 break;
288             }
289         }
290     };
291
292     public void onActivityResult(int requestCode, int resultCode, Intent data) {
293         if(D) Log.d(TAG, "onActivityResult " + resultCode);
294         switch (requestCode) {
295         case REQUEST_CONNECT_DEVICE_SECURE:
296             // When DeviceListActivity returns with a device to connect
297             if (resultCode == Activity.RESULT_OK) {
298                 connectDevice(data, true);
299             }
300             break;
301         case REQUEST_CONNECT_DEVICE_INSECURE:
302             // When DeviceListActivity returns with a device to connect
303             if (resultCode == Activity.RESULT_OK) {
304                 connectDevice(data, false);
305             }
306             break;
307         case REQUEST_ENABLE_BT:
308             // When the request to enable Bluetooth returns
309             if (resultCode == Activity.RESULT_OK) {
310                 // Bluetooth is now enabled, so set up a chat session
311                 setupChat();
312             } else {
313                 // User did not enable Bluetooth or an error occured
314                 Log.d(TAG, "BT not enabled");
315                 Toast.makeText(this, R.string.bt_not_enabled_leaving, Toast.LENGTH_SHORT).show();
316                 finish();
317             }
318         }
319     }
320
321     private void connectDevice(Intent data, boolean secure) {
322         // Get the device MAC address
323         String address = data.getExtras()
324             .getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
325         // Get the BLuetoothDevice object
326         BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
327         // Attempt to connect to the device
328         mChatService.connect(device, secure);
329     }
330
331     @Override
332     public boolean onCreateOptionsMenu(Menu menu) {
333         MenuInflater inflater = getMenuInflater();
334         inflater.inflate(R.menu.option_menu, menu);
335         return true;
336     }
337
338     @Override
339     public boolean onOptionsItemSelected(MenuItem item) {
340         Intent serverIntent = null;
341         switch (item.getItemId()) {
342         case R.id.telemetry_service_control:
343             serverIntent = new Intent(this, TelemetryServiceActivities.Controller.class);
344             startActivity(serverIntent);
345             return true;
346         case R.id.telemetry_service_bind:
347             serverIntent = new Intent(this, TelemetryServiceActivities.Binding.class);
348             startActivity(serverIntent);
349             return true;
350         case R.id.secure_connect_scan:
351             // Launch the DeviceListActivity to see devices and do scan
352             serverIntent = new Intent(this, DeviceListActivity.class);
353             startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE_SECURE);
354             return true;
355         case R.id.insecure_connect_scan:
356             // Launch the DeviceListActivity to see devices and do scan
357             serverIntent = new Intent(this, DeviceListActivity.class);
358             startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE_INSECURE);
359             return true;
360         case R.id.discoverable:
361             // Ensure this device is discoverable by others
362             ensureDiscoverable();
363             return true;
364         }
365         return false;
366     }
367
368 }