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:
parent
6bed9fcb3c
commit
babe4d9f0e
@ -93,9 +93,10 @@ public abstract class ObjectManagerActivity extends Activity {
|
||||
* objectUpdated with the right object type
|
||||
*/
|
||||
final Handler uavobjHandler = new Handler();
|
||||
private class UpdatedObserver implements Observer {
|
||||
private class ActivityUpdatedObserver implements Observer {
|
||||
UAVObject obj;
|
||||
UpdatedObserver(UAVObject obj) { this.obj = obj; };
|
||||
ActivityUpdatedObserver(UAVObject obj) { this.obj = obj; };
|
||||
@Override
|
||||
public void update(Observable observable, Object data) {
|
||||
uavobjHandler.post(new Runnable() {
|
||||
@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
|
||||
@ -110,7 +127,10 @@ public abstract class ObjectManagerActivity extends Activity {
|
||||
* the objectUpdated() method will be called in the original UI thread
|
||||
*/
|
||||
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) {
|
||||
ListIterator<List<UAVObject>> li = objects.listIterator();
|
||||
@ -133,6 +153,7 @@ public abstract class ObjectManagerActivity extends Activity {
|
||||
}
|
||||
|
||||
final Observer telemetryObserver = new Observer() {
|
||||
@Override
|
||||
public void update(Observable observable, Object data) {
|
||||
uavobjHandler.post(new Runnable() {
|
||||
@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 {
|
||||
|
||||
// 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() */
|
||||
private ServiceConnection mConnection = new ServiceConnection() {
|
||||
private final ServiceConnection mConnection = new ServiceConnection() {
|
||||
@Override
|
||||
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");
|
||||
@ -293,6 +315,7 @@ public abstract class ObjectManagerActivity extends Activity {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServiceDisconnected(ComponentName name) {
|
||||
mBound = false;
|
||||
binder = null;
|
||||
|
@ -1,17 +1,11 @@
|
||||
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.UAVObjectManager;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.Fragment;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.util.Log;
|
||||
|
||||
public class ObjectManagerFragment extends Fragment {
|
||||
@ -40,25 +34,29 @@ public class ObjectManagerFragment extends Fragment {
|
||||
super.onAttach(activity);
|
||||
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
|
||||
void onOPConnected(UAVObjectManager objMngr) {
|
||||
protected void onOPConnected(UAVObjectManager objMngr) {
|
||||
this.objMngr = objMngr;
|
||||
if (DEBUG) Log.d(TAG,"onOPConnected");
|
||||
}
|
||||
|
||||
void onOPDisconnected() {
|
||||
protected void onOPDisconnected() {
|
||||
objMngr = null;
|
||||
if (DEBUG) Log.d(TAG,"onOPDisconnected");
|
||||
}
|
||||
|
||||
public void onBind() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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
|
||||
* objectUpdated with the right object type
|
||||
*/
|
||||
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
|
||||
* Register on the activities object monitor handler so that updates
|
||||
* occur within that UI thread. No need to maintain a handler for
|
||||
* each fragment.
|
||||
*/
|
||||
protected void registerObjectUpdates(UAVObject object) {
|
||||
object.addUpdatedObserver(new UpdatedObserver(object));
|
||||
}
|
||||
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());
|
||||
}
|
||||
((ObjectManagerActivity) getActivity()).registerObjectUpdates(object, this);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user