2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.altusmetrum.AltosDroid;
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.*;
45 * This is the main Activity that displays the current chat session.
47 public class AltosDroid extends Activity {
49 private static final String TAG = "BluetoothChat";
50 private static final boolean D = true;
52 private static final AltosLine q = new AltosLine();
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;
61 // Key names received from the BluetoothChatService Handler
62 public static final String DEVICE_NAME = "device_name";
63 public static final String TOAST = "toast";
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;
71 private TextView mTitle;
72 private ListView mConversationView;
73 private EditText mOutEditText;
74 private Button mSendButton;
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;
89 public void onCreate(Bundle savedInstanceState) {
90 super.onCreate(savedInstanceState);
91 if(D) Log.e(TAG, "+++ ON CREATE +++");
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);
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);
103 // Get local Bluetooth adapter
104 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
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();
115 public void onStart() {
117 if(D) Log.e(TAG, "++ ON START ++");
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
126 if (mChatService == null) setupChat();
131 public synchronized void onResume() {
133 if(D) Log.e(TAG, "+ ON RESUME +");
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();
147 private void setupChat() {
148 Log.d(TAG, "setupChat()");
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);
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);
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);
170 // Initialize the BluetoothChatService to perform bluetooth connections
171 mChatService = new BluetoothChatService(this, mHandler);
173 // Initialize the buffer for outgoing messages
174 mOutStringBuffer = new StringBuffer("");
178 public synchronized void onPause() {
180 if(D) Log.e(TAG, "- ON PAUSE -");
184 public void onStop() {
186 if(D) Log.e(TAG, "-- ON STOP --");
190 public void onDestroy() {
192 // Stop the Bluetooth chat services
193 if (mChatService != null) mChatService.stop();
194 if(D) Log.e(TAG, "--- ON DESTROY ---");
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);
209 * @param message A string of text to send.
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();
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);
224 // Reset out string buffer to zero and clear the edit text field
225 mOutStringBuffer.setLength(0);
226 mOutEditText.setText(mOutStringBuffer);
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);
239 if(D) Log.i(TAG, "END onEditorAction");
244 // The Handler that gets information back from the BluetoothChatService
245 private final Handler mHandler = new Handler() {
247 public void handleMessage(Message msg) {
249 case MESSAGE_STATE_CHANGE:
250 if(D) Log.i(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
252 case BluetoothChatService.STATE_CONNECTED:
253 mTitle.setText(R.string.title_connected_to);
254 mTitle.append(mConnectedDeviceName);
255 mConversationArrayAdapter.clear();
257 case BluetoothChatService.STATE_CONNECTING:
258 mTitle.setText(R.string.title_connecting);
260 case BluetoothChatService.STATE_READY:
261 case BluetoothChatService.STATE_NONE:
262 mTitle.setText(R.string.title_not_connected);
267 byte[] writeBuf = (byte[]) msg.obj;
268 // construct a string from the buffer
269 String writeMessage = new String(writeBuf);
270 mConversationArrayAdapter.add("Me: " + writeMessage);
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);
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();
285 Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST),
286 Toast.LENGTH_SHORT).show();
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);
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);
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
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();
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);
332 public boolean onCreateOptionsMenu(Menu menu) {
333 MenuInflater inflater = getMenuInflater();
334 inflater.inflate(R.menu.option_menu, menu);
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);
346 case R.id.telemetry_service_bind:
347 serverIntent = new Intent(this, TelemetryServiceActivities.Binding.class);
348 startActivity(serverIntent);
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);
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);
360 case R.id.discoverable:
361 // Ensure this device is discoverable by others
362 ensureDiscoverable();