1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-02-21 11:54:15 +01:00

AndroidGCS: Make the fragment object updated callbacks register in the main

activity method and use the main activity handler to consolidate things into
one place.
This commit is contained in:
James Cotton 2012-08-08 08:55:58 -05:00
parent 6bed9fcb3c
commit babe4d9f0e
2 changed files with 76 additions and 79 deletions

View File

@ -93,9 +93,10 @@ public abstract class ObjectManagerActivity extends Activity {
* objectUpdated with the right object type * objectUpdated with the right object type
*/ */
final Handler uavobjHandler = new Handler(); final Handler uavobjHandler = new Handler();
private class UpdatedObserver implements Observer { private class ActivityUpdatedObserver implements Observer {
UAVObject obj; UAVObject obj;
UpdatedObserver(UAVObject obj) { this.obj = obj; }; ActivityUpdatedObserver(UAVObject obj) { this.obj = obj; };
@Override
public void update(Observable observable, Object data) { public void update(Observable observable, Object data) {
uavobjHandler.post(new Runnable() { uavobjHandler.post(new Runnable() {
@Override @Override
@ -103,6 +104,22 @@ public abstract class ObjectManagerActivity extends Activity {
}); });
} }
}; };
private class FragmentUpdatedObserver implements Observer {
UAVObject obj;
ObjectManagerFragment frag;
FragmentUpdatedObserver(UAVObject obj, ObjectManagerFragment frag) {
this.obj = obj;
this.frag = frag;
};
@Override
public void update(Observable observable, Object data) {
uavobjHandler.post(new Runnable() {
@Override
public void run() { frag.objectUpdated(obj); }
});
}
};
/** /**
* Register an activity to receive updates from this object * Register an activity to receive updates from this object
@ -110,7 +127,10 @@ public abstract class ObjectManagerActivity extends Activity {
* the objectUpdated() method will be called in the original UI thread * the objectUpdated() method will be called in the original UI thread
*/ */
protected void registerObjectUpdates(UAVObject object) { protected void registerObjectUpdates(UAVObject object) {
object.addUpdatedObserver(new UpdatedObserver(object)); object.addUpdatedObserver(new ActivityUpdatedObserver(object));
}
protected void registerObjectUpdates(UAVObject object, ObjectManagerFragment frag) {
object.addUpdatedObserver(new FragmentUpdatedObserver(object, frag));
} }
protected void registerObjectUpdates(List<List<UAVObject>> objects) { protected void registerObjectUpdates(List<List<UAVObject>> objects) {
ListIterator<List<UAVObject>> li = objects.listIterator(); ListIterator<List<UAVObject>> li = objects.listIterator();
@ -133,6 +153,7 @@ public abstract class ObjectManagerActivity extends Activity {
} }
final Observer telemetryObserver = new Observer() { final Observer telemetryObserver = new Observer() {
@Override
public void update(Observable observable, Object data) { public void update(Observable observable, Object data) {
uavobjHandler.post(new Runnable() { uavobjHandler.post(new Runnable() {
@Override @Override
@ -248,7 +269,7 @@ public abstract class ObjectManagerActivity extends Activity {
} }
} }
}; };
private ConnectionObserver connectionListeners = new ConnectionObserver(); private final ConnectionObserver connectionListeners = new ConnectionObserver();
public class OnConnectionListener implements Observer { public class OnConnectionListener implements Observer {
// Local reference of the fragment to notify, store in constructor // Local reference of the fragment to notify, store in constructor
@ -275,7 +296,8 @@ public abstract class ObjectManagerActivity extends Activity {
/** Defines callbacks for service binding, passed to bindService() */ /** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() { private final ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName arg0, IBinder service) { public void onServiceConnected(ComponentName arg0, IBinder service) {
// We've bound to LocalService, cast the IBinder and attempt to open a connection // We've bound to LocalService, cast the IBinder and attempt to open a connection
if (DEBUG) Log.d(TAG,"Service bound"); if (DEBUG) Log.d(TAG,"Service bound");
@ -293,6 +315,7 @@ public abstract class ObjectManagerActivity extends Activity {
} }
} }
@Override
public void onServiceDisconnected(ComponentName name) { public void onServiceDisconnected(ComponentName name) {
mBound = false; mBound = false;
binder = null; binder = null;

View File

@ -1,17 +1,11 @@
package org.openpilot.androidgcs; package org.openpilot.androidgcs;
import java.util.List;
import java.util.ListIterator;
import java.util.Observable;
import java.util.Observer;
import org.openpilot.uavtalk.UAVObject; import org.openpilot.uavtalk.UAVObject;
import org.openpilot.uavtalk.UAVObjectManager; import org.openpilot.uavtalk.UAVObjectManager;
import android.app.Activity; import android.app.Activity;
import android.app.Fragment; import android.app.Fragment;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.util.Log; import android.util.Log;
public class ObjectManagerFragment extends Fragment { public class ObjectManagerFragment extends Fragment {
@ -40,25 +34,29 @@ public class ObjectManagerFragment extends Fragment {
super.onAttach(activity); super.onAttach(activity);
if (DEBUG) Log.d(TAG,"onAttach"); if (DEBUG) Log.d(TAG,"onAttach");
((ObjectManagerActivity)activity).addOnConnectionListenerFragment(this); ObjectManagerActivity castActivity = null;
try {
castActivity = (ObjectManagerActivity)activity;
} catch (ClassCastException e) {
throw new android.app.Fragment.InstantiationException(
"Attaching a ObjectManagerFragment to an activity failed because the parent activity is not a ObjectManagerActivity",
e);
}
castActivity.addOnConnectionListenerFragment(this);
} }
// The below methods should all be called by the parent activity at the appropriate times // The below methods should all be called by the parent activity at the appropriate times
void onOPConnected(UAVObjectManager objMngr) { protected void onOPConnected(UAVObjectManager objMngr) {
this.objMngr = objMngr; this.objMngr = objMngr;
if (DEBUG) Log.d(TAG,"onOPConnected"); if (DEBUG) Log.d(TAG,"onOPConnected");
} }
void onOPDisconnected() { protected void onOPDisconnected() {
objMngr = null; objMngr = null;
if (DEBUG) Log.d(TAG,"onOPDisconnected"); if (DEBUG) Log.d(TAG,"onOPDisconnected");
} }
public void onBind() {
}
/** /**
* Called whenever any objects subscribed to via registerObjects * Called whenever any objects subscribed to via registerObjects
*/ */
@ -67,36 +65,12 @@ public class ObjectManagerFragment extends Fragment {
} }
/** /**
* A message handler and a custom Observer to use it which calls * Register on the activities object monitor handler so that updates
* objectUpdated with the right object type * occur within that UI thread. No need to maintain a handler for
*/ * each fragment.
final Handler uavobjHandler = new Handler();
private class UpdatedObserver implements Observer {
UAVObject obj;
UpdatedObserver(UAVObject obj) { this.obj = obj; };
public void update(Observable observable, Object data) {
uavobjHandler.post(new Runnable() {
@Override
public void run() { objectUpdated(obj); }
});
}
};
/**
* Register an activity to receive updates from this object
*
* the objectUpdated() method will be called in the original UI thread
*/ */
protected void registerObjectUpdates(UAVObject object) { protected void registerObjectUpdates(UAVObject object) {
object.addUpdatedObserver(new UpdatedObserver(object)); ((ObjectManagerActivity) getActivity()).registerObjectUpdates(object, this);
}
protected void registerObjectUpdates(List<List<UAVObject>> objects) {
ListIterator<List<UAVObject>> li = objects.listIterator();
while(li.hasNext()) {
ListIterator<UAVObject> li2 = li.next().listIterator();
while(li2.hasNext())
registerObjectUpdates(li2.next());
}
} }
} }