5b48d5710d73d17f8a13600d0f2f5569785a376b
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / AltosDroid.java
1 /*
2  * Copyright © 2012 Mike Beattie <mike@ethernal.org>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package org.altusmetrum.AltosDroid;
19
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.*;
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 = "AltosDroid";
50         private static final boolean D = true;
51
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;
58
59     // Key names received from the BluetoothChatService Handler
60     public static final String DEVICE_NAME = "device_name";
61     public static final String TOAST = "toast";
62
63
64
65         // Intent request codes
66         private static final int REQUEST_CONNECT_DEVICE = 1;
67         private static final int REQUEST_ENABLE_BT      = 2;
68
69         // Layout Views
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;
78
79         @Override
80         public void onCreate(Bundle savedInstanceState) {
81                 super.onCreate(savedInstanceState);
82                 if(D) Log.e(TAG, "+++ ON CREATE +++");
83
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);
88
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);
93
94                 mSerialView = (TextView) findViewById(R.id.in);
95                 mSerialView.setMovementMethod(new ScrollingMovementMethod());
96                 mSerialView.setClickable(false);
97                 mSerialView.setLongClickable(false);
98
99                 // Get local Bluetooth adapter
100                 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
101
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();
105                         finish();
106                         return;
107                 }
108
109         }
110
111         @Override
112         public void onStart() {
113                 super.onStart();
114                 if(D) Log.e(TAG, "++ ON START ++");
115
116                 if (!mBluetoothAdapter.isEnabled()) {
117                         Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
118                         startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
119                 } else {
120                         //if (mChatService == null) setupChat();
121                 }
122         }
123
124         @Override
125         public synchronized void onResume() {
126                 super.onResume();
127                 if(D) Log.e(TAG, "+ ON RESUME +");
128
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();
137                         //}
138                 //}
139         }
140
141         @Override
142         public synchronized void onPause() {
143                 super.onPause();
144                 if(D) Log.e(TAG, "- ON PAUSE -");
145         }
146
147         @Override
148         public void onStop() {
149                 super.onStop();
150                 if(D) Log.e(TAG, "-- ON STOP --");
151         }
152
153         @Override
154         public void onDestroy() {
155                 super.onDestroy();
156                 if(D) Log.e(TAG, "--- ON DESTROY ---");
157         }
158
159
160
161 /*
162         private void setupChat() {
163                 Log.d(TAG, "setupChat()");
164
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);
168
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);
177                         }
178                 });
179
180                 // Initialize the BluetoothChatService to perform bluetooth connections
181                 mChatService = new BluetoothChatService(this, mHandler);
182
183                 // Initialize the buffer for outgoing messages
184                 mOutStringBuffer = new StringBuffer("");
185         }
186 */
187
188         /**
189          * Sends a message.
190          * @param message  A string of text to send.
191          */
192         /*
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();
197                         return;
198                 }
199
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);
205
206                         // Reset out string buffer to zero and clear the edit text field
207                         mOutStringBuffer.setLength(0);
208                         mOutEditText.setText(mOutStringBuffer);
209                 }
210         }
211
212
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);
221                         }
222                         if(D) Log.i(TAG, "END onEditorAction");
223                         return true;
224                 }
225         };
226         */
227
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) {
234                                 connectDevice(data);
235                         }
236                         break;
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
241                                 //setupChat();
242                         } else {
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();
247                                 finish();
248                         }
249                 }
250         }
251
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
258         }
259
260
261         @Override
262         public boolean onCreateOptionsMenu(Menu menu) {
263                 MenuInflater inflater = getMenuInflater();
264                 inflater.inflate(R.menu.option_menu, menu);
265                 return true;
266         }
267
268         @Override
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);
276                         return true;
277                 }
278                 return false;
279         }
280
281 }