2 * Copyright © 2012 Mike Beattie <mike@ethernal.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18 package org.altusmetrum.AltosDroid;
20 import android.app.Activity;
21 import android.bluetooth.BluetoothAdapter;
22 import android.bluetooth.BluetoothDevice;
23 import android.content.Intent;
24 import android.os.Bundle;
25 import android.os.Handler;
26 import android.os.Message;
27 import android.text.method.ScrollingMovementMethod;
28 import android.util.Log;
29 //import android.view.KeyEvent;
30 import android.view.Menu;
31 import android.view.MenuInflater;
32 import android.view.MenuItem;
33 //import android.view.View;
34 import android.view.Window;
35 //import android.view.View.OnClickListener;
36 //import android.view.inputmethod.EditorInfo;
37 //import android.widget.Button;
38 //import android.widget.EditText;
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 = "AltosDroid";
50 private static final boolean D = true;
52 // Message types sent from the BluetoothChatService Handler
53 public static final int MESSAGE_STATE_CHANGE = 1;
54 public static final int MESSAGE_READ = 2;
55 public static final int MESSAGE_WRITE = 3;
56 public static final int MESSAGE_DEVICE_NAME = 4;
57 public static final int MESSAGE_TOAST = 5;
59 // Key names received from the BluetoothChatService Handler
60 public static final String DEVICE_NAME = "device_name";
61 public static final String TOAST = "toast";
65 // Intent request codes
66 private static final int REQUEST_CONNECT_DEVICE = 1;
67 private static final int REQUEST_ENABLE_BT = 2;
70 private TextView mTitle;
71 private TextView mSerialView;
72 //private EditText mOutEditText;
73 //private Button mSendButton;
74 // Name of the connected device
75 private String mConnectedDeviceName = null;
76 // Local Bluetooth adapter
77 private BluetoothAdapter mBluetoothAdapter = null;
80 public void onCreate(Bundle savedInstanceState) {
81 super.onCreate(savedInstanceState);
82 if(D) Log.e(TAG, "+++ ON CREATE +++");
84 // Set up the window layout
85 requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
86 setContentView(R.layout.main);
87 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
89 // Set up the custom title
90 mTitle = (TextView) findViewById(R.id.title_left_text);
91 mTitle.setText(R.string.app_name);
92 mTitle = (TextView) findViewById(R.id.title_right_text);
94 mSerialView = (TextView) findViewById(R.id.in);
95 mSerialView.setMovementMethod(new ScrollingMovementMethod());
96 mSerialView.setClickable(false);
97 mSerialView.setLongClickable(false);
99 // Get local Bluetooth adapter
100 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
102 // If the adapter is null, then Bluetooth is not supported
103 if (mBluetoothAdapter == null) {
104 Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
112 public void onStart() {
114 if(D) Log.e(TAG, "++ ON START ++");
116 if (!mBluetoothAdapter.isEnabled()) {
117 Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
118 startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
120 //if (mChatService == null) setupChat();
125 public synchronized void onResume() {
127 if(D) Log.e(TAG, "+ ON RESUME +");
129 // Performing this check in onResume() covers the case in which BT was
130 // not enabled during onStart(), so we were paused to enable it...
131 // onResume() will be called when ACTION_REQUEST_ENABLE activity returns.
132 //if (mChatService != null) {
133 // Only if the state is STATE_NONE, do we know that we haven't started already
134 //if (mChatService.getState() == BluetoothChatService.STATE_NONE) {
135 // Start the Bluetooth chat services
136 //mChatService.start();
142 public synchronized void onPause() {
144 if(D) Log.e(TAG, "- ON PAUSE -");
148 public void onStop() {
150 if(D) Log.e(TAG, "-- ON STOP --");
154 public void onDestroy() {
156 if(D) Log.e(TAG, "--- ON DESTROY ---");
162 private void setupChat() {
163 Log.d(TAG, "setupChat()");
165 // Initialize the compose field with a listener for the return key
166 mOutEditText = (EditText) findViewById(R.id.edit_text_out);
167 mOutEditText.setOnEditorActionListener(mWriteListener);
169 // Initialize the send button with a listener that for click events
170 mSendButton = (Button) findViewById(R.id.button_send);
171 mSendButton.setOnClickListener(new OnClickListener() {
172 public void onClick(View v) {
173 // Send a message using content of the edit text widget
174 TextView view = (TextView) findViewById(R.id.edit_text_out);
175 String message = view.getText().toString();
176 sendMessage(message);
180 // Initialize the BluetoothChatService to perform bluetooth connections
181 mChatService = new BluetoothChatService(this, mHandler);
183 // Initialize the buffer for outgoing messages
184 mOutStringBuffer = new StringBuffer("");
190 * @param message A string of text to send.
193 private void sendMessage(String message) {
194 // Check that we're actually connected before trying anything
195 if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) {
196 Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show();
200 // Check that there's actually something to send
201 if (message.length() > 0) {
202 // Get the message bytes and tell the BluetoothChatService to write
203 byte[] send = message.getBytes();
204 mChatService.write(send);
206 // Reset out string buffer to zero and clear the edit text field
207 mOutStringBuffer.setLength(0);
208 mOutEditText.setText(mOutStringBuffer);
213 // The action listener for the EditText widget, to listen for the return key
214 private TextView.OnEditorActionListener mWriteListener =
215 new TextView.OnEditorActionListener() {
216 public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
217 // If the action is a key-up event on the return key, send the message
218 if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_UP) {
219 String message = view.getText().toString();
220 sendMessage(message);
222 if(D) Log.i(TAG, "END onEditorAction");
228 public void onActivityResult(int requestCode, int resultCode, Intent data) {
229 if(D) Log.d(TAG, "onActivityResult " + resultCode);
230 switch (requestCode) {
231 case REQUEST_CONNECT_DEVICE:
232 // When DeviceListActivity returns with a device to connect to
233 if (resultCode == Activity.RESULT_OK) {
237 case REQUEST_ENABLE_BT:
238 // When the request to enable Bluetooth returns
239 if (resultCode == Activity.RESULT_OK) {
240 // Bluetooth is now enabled, so set up a chat session
243 // User did not enable Bluetooth or an error occured
244 Log.d(TAG, "BT not enabled");
245 stopService(new Intent(AltosDroid.this, TelemetryService.class));
246 Toast.makeText(this, R.string.bt_not_enabled_leaving, Toast.LENGTH_SHORT).show();
252 private void connectDevice(Intent data) {
253 // Get the device MAC address
254 String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
255 // Get the BLuetoothDevice object
256 BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
257 // Attempt to connect to the device
262 public boolean onCreateOptionsMenu(Menu menu) {
263 MenuInflater inflater = getMenuInflater();
264 inflater.inflate(R.menu.option_menu, menu);
269 public boolean onOptionsItemSelected(MenuItem item) {
270 Intent serverIntent = null;
271 switch (item.getItemId()) {
272 case R.id.connect_scan:
273 // Launch the DeviceListActivity to see devices and do scan
274 serverIntent = new Intent(this, DeviceListActivity.class);
275 startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);