From 2f7320fc16dd1c51765908952e4b863614b56fcb Mon Sep 17 00:00:00 2001 From: James Cotton Date: Sat, 19 Mar 2011 19:58:57 -0500 Subject: [PATCH] Common Activity class that binds to the Telemetry service --- .../androidgcs/ObjectManagerActivity.java | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 androidgcs/src/org/openpilot/androidgcs/ObjectManagerActivity.java diff --git a/androidgcs/src/org/openpilot/androidgcs/ObjectManagerActivity.java b/androidgcs/src/org/openpilot/androidgcs/ObjectManagerActivity.java new file mode 100644 index 000000000..9f57796dc --- /dev/null +++ b/androidgcs/src/org/openpilot/androidgcs/ObjectManagerActivity.java @@ -0,0 +1,97 @@ +package org.openpilot.androidgcs; + +import org.openpilot.androidgcs.OPTelemetryService.LocalBinder; +import org.openpilot.androidgcs.OPTelemetryService.TelemTask; +import org.openpilot.uavtalk.UAVObjectManager; + +import android.app.Activity; +import android.content.BroadcastReceiver; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.content.ServiceConnection; +import android.os.Bundle; +import android.os.IBinder; +import android.util.Log; + +public abstract class ObjectManagerActivity extends Activity { + + private final String TAG = "ObjectManagerActivity"; + private static int LOGLEVEL = 2; +// private static boolean WARN = LOGLEVEL > 1; + private static boolean DEBUG = LOGLEVEL > 0; + + UAVObjectManager objMngr; + boolean mBound = false; + boolean mConnected = false; + LocalBinder binder; + + + /** Called when the activity is first created. */ + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + BroadcastReceiver connectedReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + Log.d(TAG, "Received intent"); + TelemTask task; + if(intent.getAction().compareTo(OPTelemetryService.INTENT_ACTION_CONNECTED) == 0) { + + if(binder == null) + return; + if((task = binder.getTelemTask(0)) == null) + return; + objMngr = task.getObjectManager(); + mConnected = true; + onOPConnected(); + Log.d(TAG, "Connected()"); + } else if (intent.getAction().compareTo(OPTelemetryService.INTENT_ACTION_DISCONNECTED) == 0) { + objMngr = null; + mConnected = false; + onOPDisconnected(); + Log.d(TAG, "Disonnected()"); + } + } + }; + + IntentFilter filter = new IntentFilter(); + filter.addCategory(OPTelemetryService.INTENT_CATEGORY_GCS); + filter.addAction(OPTelemetryService.INTENT_ACTION_CONNECTED); + filter.addAction(OPTelemetryService.INTENT_ACTION_DISCONNECTED); + registerReceiver(connectedReceiver, filter); + } + + void onOPConnected() { + + } + + void onOPDisconnected() { + + } + + @Override + public void onStart() { + super.onStart(); + Intent intent = new Intent(this, OPTelemetryService.class); + bindService(intent, mConnection, Context.BIND_AUTO_CREATE); + } + + /** Defines callbacks for service binding, passed to bindService() */ + private ServiceConnection mConnection = new ServiceConnection() { + public void onServiceConnected(ComponentName arg0, IBinder service) { + // We've bound to LocalService, cast the IBinder and attempt to open a connection + if (DEBUG) Log.d(TAG,"Service bound"); + binder = (LocalBinder) service; + binder.openFakeConnection(); + } + + public void onServiceDisconnected(ComponentName name) { + mBound = false; + mConnected = false; + objMngr = null; + } + }; +}