altosdroid: Add service start/bind/unbind to AltosDroid
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / AltosDroid.java
index 521588fbe7b929ebdf7ec4f85bc1ab055dde995e..54b61c67ca793849d19e0c3707e49977d7165d93 100644 (file)
@@ -21,25 +21,30 @@ import android.app.Activity;
 import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.BluetoothDevice;
 import android.content.Intent;
+import android.content.Context;
+import android.content.ComponentName;
+import android.content.ServiceConnection;
+import android.os.IBinder;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.Message;
+import android.os.Messenger;
+import android.os.RemoteException;
 import android.text.method.ScrollingMovementMethod;
 import android.util.Log;
-import android.view.KeyEvent;
+//import android.view.KeyEvent;
 import android.view.Menu;
 import android.view.MenuInflater;
 import android.view.MenuItem;
-import android.view.View;
+//import android.view.View;
 import android.view.Window;
-import android.view.View.OnClickListener;
-import android.view.inputmethod.EditorInfo;
-import android.widget.Button;
-import android.widget.EditText;
+//import android.view.View.OnClickListener;
+//import android.view.inputmethod.EditorInfo;
+//import android.widget.Button;
+//import android.widget.EditText;
 import android.widget.TextView;
 import android.widget.Toast;
 import org.altusmetrum.AltosDroid.R;
-import org.altusmetrum.AltosLib.*;
 
 /**
  * This is the main Activity that displays the current chat session.
@@ -61,8 +66,6 @@ public class AltosDroid extends Activity {
     public static final String TOAST = "toast";
 
 
-    private EditText mOutEditText;
-    private Button mSendButton;
 
        // Intent request codes
        private static final int REQUEST_CONNECT_DEVICE = 1;
@@ -71,11 +74,32 @@ public class AltosDroid extends Activity {
        // Layout Views
        private TextView mTitle;
        private TextView mSerialView;
+       //private EditText mOutEditText;
+       //private Button mSendButton;
+
+       private boolean mIsBound;
+       Messenger mService = null;
+
        // Name of the connected device
        private String mConnectedDeviceName = null;
        // Local Bluetooth adapter
        private BluetoothAdapter mBluetoothAdapter = null;
 
+       };
+
+
+       private ServiceConnection mConnection = new ServiceConnection() {
+               public void onServiceConnected(ComponentName className, IBinder service) {
+                       mService = new Messenger(service);
+               }
+
+               public void onServiceDisconnected(ComponentName className) {
+                       // This is called when the connection with the service has been unexpectedly disconnected - process crashed.
+                       mService = null;
+               }
+       };
+
+
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
@@ -86,13 +110,19 @@ public class AltosDroid extends Activity {
                setContentView(R.layout.main);
                getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
 
-        // Get local Bluetooth adapter
-        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                // Set up the custom title
                mTitle = (TextView) findViewById(R.id.title_left_text);
                mTitle.setText(R.string.app_name);
                mTitle = (TextView) findViewById(R.id.title_right_text);
 
+               mSerialView = (TextView) findViewById(R.id.in);
+               mSerialView.setMovementMethod(new ScrollingMovementMethod());
+               mSerialView.setClickable(false);
+               mSerialView.setLongClickable(false);
+
+               // Get local Bluetooth adapter
+               mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
+
                // If the adapter is null, then Bluetooth is not supported
                if (mBluetoothAdapter == null) {
                        Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
@@ -100,6 +130,10 @@ public class AltosDroid extends Activity {
                        return;
                }
 
+               // Start Telemetry Service
+               startService(new Intent(AltosDroid.this, TelemetryService.class));
+
+               doBindService();
        }
 
        @Override
@@ -147,6 +181,9 @@ public class AltosDroid extends Activity {
        @Override
        public void onDestroy() {
                super.onDestroy();
+
+               doUnbindService();
+
                if(D) Log.e(TAG, "--- ON DESTROY ---");
        }
 
@@ -156,11 +193,6 @@ public class AltosDroid extends Activity {
        private void setupChat() {
                Log.d(TAG, "setupChat()");
 
-        mSerialView = (TextView) findViewById(R.id.in);
-        mSerialView.setMovementMethod(new ScrollingMovementMethod());
-        mSerialView.setClickable(false);
-        mSerialView.setLongClickable(false);
-
                // Initialize the compose field with a listener for the return key
                mOutEditText = (EditText) findViewById(R.id.edit_text_out);
                mOutEditText.setOnEditorActionListener(mWriteListener);
@@ -277,4 +309,19 @@ public class AltosDroid extends Activity {
                return false;
        }
 
+
+       void doBindService() {
+               bindService(new Intent(this, TelemetryService.class), mConnection, Context.BIND_AUTO_CREATE);
+               mIsBound = true;
+       }
+
+       void doUnbindService() {
+               if (mIsBound) {
+                       // If we have received the service, and hence registered with it, then now is the time to unregister.
+                       // Detach our existing connection.
+                       unbindService(mConnection);
+                       mIsBound = false;
+               }
+       }
+
 }