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;
44 * This is the main Activity that displays the current chat session.
46 public class AltosDroid extends Activity {
48 private static final String TAG = "AltosDroid";
49 private static final boolean D = true;
51 // Message types sent from the BluetoothChatService Handler
52 public static final int MESSAGE_STATE_CHANGE = 1;
53 public static final int MESSAGE_READ = 2;
54 public static final int MESSAGE_WRITE = 3;
55 public static final int MESSAGE_DEVICE_NAME = 4;
56 public static final int MESSAGE_TOAST = 5;
58 // Key names received from the BluetoothChatService Handler
59 public static final String DEVICE_NAME = "device_name";
60 public static final String TOAST = "toast";
64 // Intent request codes
65 private static final int REQUEST_CONNECT_DEVICE = 1;
66 private static final int REQUEST_ENABLE_BT = 2;
69 private TextView mTitle;
70 private TextView mSerialView;
71 //private EditText mOutEditText;
72 //private Button mSendButton;
73 // Name of the connected device
74 private String mConnectedDeviceName = null;
75 // Local Bluetooth adapter
76 private BluetoothAdapter mBluetoothAdapter = null;
79 public void onCreate(Bundle savedInstanceState) {
80 super.onCreate(savedInstanceState);
81 if(D) Log.e(TAG, "+++ ON CREATE +++");
83 // Set up the window layout
84 requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
85 setContentView(R.layout.main);
86 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
88 // Set up the custom title
89 mTitle = (TextView) findViewById(R.id.title_left_text);
90 mTitle.setText(R.string.app_name);
91 mTitle = (TextView) findViewById(R.id.title_right_text);
93 mSerialView = (TextView) findViewById(R.id.in);
94 mSerialView.setMovementMethod(new ScrollingMovementMethod());
95 mSerialView.setClickable(false);
96 mSerialView.setLongClickable(false);
98 // Get local Bluetooth adapter
99 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
101 // If the adapter is null, then Bluetooth is not supported
102 if (mBluetoothAdapter == null) {
103 Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
111 public void onStart() {
113 if(D) Log.e(TAG, "++ ON START ++");
115 if (!mBluetoothAdapter.isEnabled()) {
116 Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
117 startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
119 //if (mChatService == null) setupChat();
124 public synchronized void onResume() {
126 if(D) Log.e(TAG, "+ ON RESUME +");
128 // Performing this check in onResume() covers the case in which BT was
129 // not enabled during onStart(), so we were paused to enable it...
130 // onResume() will be called when ACTION_REQUEST_ENABLE activity returns.
131 //if (mChatService != null) {
132 // Only if the state is STATE_NONE, do we know that we haven't started already
133 //if (mChatService.getState() == BluetoothChatService.STATE_NONE) {
134 // Start the Bluetooth chat services
135 //mChatService.start();
141 public synchronized void onPause() {
143 if(D) Log.e(TAG, "- ON PAUSE -");
147 public void onStop() {
149 if(D) Log.e(TAG, "-- ON STOP --");
153 public void onDestroy() {
155 if(D) Log.e(TAG, "--- ON DESTROY ---");
161 private void setupChat() {
162 Log.d(TAG, "setupChat()");
164 // Initialize the compose field with a listener for the return key
165 mOutEditText = (EditText) findViewById(R.id.edit_text_out);
166 mOutEditText.setOnEditorActionListener(mWriteListener);
168 // Initialize the send button with a listener that for click events
169 mSendButton = (Button) findViewById(R.id.button_send);
170 mSendButton.setOnClickListener(new OnClickListener() {
171 public void onClick(View v) {
172 // Send a message using content of the edit text widget
173 TextView view = (TextView) findViewById(R.id.edit_text_out);
174 String message = view.getText().toString();
175 sendMessage(message);
179 // Initialize the BluetoothChatService to perform bluetooth connections
180 mChatService = new BluetoothChatService(this, mHandler);
182 // Initialize the buffer for outgoing messages
183 mOutStringBuffer = new StringBuffer("");
189 * @param message A string of text to send.
192 private void sendMessage(String message) {
193 // Check that we're actually connected before trying anything
194 if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) {
195 Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show();
199 // Check that there's actually something to send
200 if (message.length() > 0) {
201 // Get the message bytes and tell the BluetoothChatService to write
202 byte[] send = message.getBytes();
203 mChatService.write(send);
205 // Reset out string buffer to zero and clear the edit text field
206 mOutStringBuffer.setLength(0);
207 mOutEditText.setText(mOutStringBuffer);
212 // The action listener for the EditText widget, to listen for the return key
213 private TextView.OnEditorActionListener mWriteListener =
214 new TextView.OnEditorActionListener() {
215 public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
216 // If the action is a key-up event on the return key, send the message
217 if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_UP) {
218 String message = view.getText().toString();
219 sendMessage(message);
221 if(D) Log.i(TAG, "END onEditorAction");
227 public void onActivityResult(int requestCode, int resultCode, Intent data) {
228 if(D) Log.d(TAG, "onActivityResult " + resultCode);
229 switch (requestCode) {
230 case REQUEST_CONNECT_DEVICE:
231 // When DeviceListActivity returns with a device to connect to
232 if (resultCode == Activity.RESULT_OK) {
236 case REQUEST_ENABLE_BT:
237 // When the request to enable Bluetooth returns
238 if (resultCode == Activity.RESULT_OK) {
239 // Bluetooth is now enabled, so set up a chat session
242 // User did not enable Bluetooth or an error occured
243 Log.d(TAG, "BT not enabled");
244 stopService(new Intent(AltosDroid.this, TelemetryService.class));
245 Toast.makeText(this, R.string.bt_not_enabled_leaving, Toast.LENGTH_SHORT).show();
251 private void connectDevice(Intent data) {
252 // Get the device MAC address
253 String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
254 // Get the BLuetoothDevice object
255 BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
256 // Attempt to connect to the device
261 public boolean onCreateOptionsMenu(Menu menu) {
262 MenuInflater inflater = getMenuInflater();
263 inflater.inflate(R.menu.option_menu, menu);
268 public boolean onOptionsItemSelected(MenuItem item) {
269 Intent serverIntent = null;
270 switch (item.getItemId()) {
271 case R.id.connect_scan:
272 // Launch the DeviceListActivity to see devices and do scan
273 serverIntent = new Intent(this, DeviceListActivity.class);
274 startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);