altosdroid: excise BluetoothChatService example code
[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     private EditText mOutEditText;
65     private Button mSendButton;
66
67         // Intent request codes
68         private static final int REQUEST_CONNECT_DEVICE = 1;
69         private static final int REQUEST_ENABLE_BT      = 2;
70
71         // Layout Views
72         private TextView mTitle;
73         private TextView mSerialView;
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         // Get local Bluetooth adapter
90         mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
91                 // Set up the custom title
92                 mTitle = (TextView) findViewById(R.id.title_left_text);
93                 mTitle.setText(R.string.app_name);
94                 mTitle = (TextView) findViewById(R.id.title_right_text);
95
96                 // If the adapter is null, then Bluetooth is not supported
97                 if (mBluetoothAdapter == null) {
98                         Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
99                         finish();
100                         return;
101                 }
102
103         }
104
105         @Override
106         public void onStart() {
107                 super.onStart();
108                 if(D) Log.e(TAG, "++ ON START ++");
109
110                 if (!mBluetoothAdapter.isEnabled()) {
111                         Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
112                         startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
113                 } else {
114                         //if (mChatService == null) setupChat();
115                 }
116         }
117
118         @Override
119         public synchronized void onResume() {
120                 super.onResume();
121                 if(D) Log.e(TAG, "+ ON RESUME +");
122
123                 // Performing this check in onResume() covers the case in which BT was
124                 // not enabled during onStart(), so we were paused to enable it...
125                 // onResume() will be called when ACTION_REQUEST_ENABLE activity returns.
126                 //if (mChatService != null) {
127                         // Only if the state is STATE_NONE, do we know that we haven't started already
128                         //if (mChatService.getState() == BluetoothChatService.STATE_NONE) {
129                         // Start the Bluetooth chat services
130                         //mChatService.start();
131                         //}
132                 //}
133         }
134
135         @Override
136         public synchronized void onPause() {
137                 super.onPause();
138                 if(D) Log.e(TAG, "- ON PAUSE -");
139         }
140
141         @Override
142         public void onStop() {
143                 super.onStop();
144                 if(D) Log.e(TAG, "-- ON STOP --");
145         }
146
147         @Override
148         public void onDestroy() {
149                 super.onDestroy();
150                 if(D) Log.e(TAG, "--- ON DESTROY ---");
151         }
152
153
154
155 /*
156         private void setupChat() {
157                 Log.d(TAG, "setupChat()");
158
159         mSerialView = (TextView) findViewById(R.id.in);
160         mSerialView.setMovementMethod(new ScrollingMovementMethod());
161         mSerialView.setClickable(false);
162         mSerialView.setLongClickable(false);
163
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);
167
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);
176                         }
177                 });
178
179                 // Initialize the BluetoothChatService to perform bluetooth connections
180                 mChatService = new BluetoothChatService(this, mHandler);
181
182                 // Initialize the buffer for outgoing messages
183                 mOutStringBuffer = new StringBuffer("");
184         }
185 */
186
187         /**
188          * Sends a message.
189          * @param message  A string of text to send.
190          */
191         /*
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();
196                         return;
197                 }
198
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);
204
205                         // Reset out string buffer to zero and clear the edit text field
206                         mOutStringBuffer.setLength(0);
207                         mOutEditText.setText(mOutStringBuffer);
208                 }
209         }
210
211
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);
220                         }
221                         if(D) Log.i(TAG, "END onEditorAction");
222                         return true;
223                 }
224         };
225         */
226
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) {
233                                 connectDevice(data);
234                         }
235                         break;
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
240                                 //setupChat();
241                         } else {
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();
246                                 finish();
247                         }
248                 }
249         }
250
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
257         }
258
259
260         @Override
261         public boolean onCreateOptionsMenu(Menu menu) {
262                 MenuInflater inflater = getMenuInflater();
263                 inflater.inflate(R.menu.option_menu, menu);
264                 return true;
265         }
266
267         @Override
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);
275                         return true;
276                 }
277                 return false;
278         }
279
280 }