1212import android .util .Log ;
1313import android .widget .Toast ;
1414
15+ import com .o3dr .android .client .ControlTower ;
1516import com .o3dr .android .client .Drone ;
16- import com .o3dr .android .client .ServiceManager ;
1717import com .o3dr .android .client .interfaces .DroneListener ;
18- import com .o3dr .android .client .interfaces .ServiceListener ;
18+ import com .o3dr .android .client .interfaces .TowerListener ;
1919import com .o3dr .services .android .lib .drone .attribute .AttributeEvent ;
2020import com .o3dr .services .android .lib .drone .connection .ConnectionParameter ;
2121import com .o3dr .services .android .lib .drone .connection .ConnectionResult ;
3333import java .util .ArrayList ;
3434import java .util .List ;
3535
36- public class DroidPlannerApp extends Application implements DroneListener , ServiceListener {
36+ public class DroidPlannerApp extends Application implements DroneListener , TowerListener {
3737
3838 private static final long DELAY_TO_DISCONNECTION = 30000l ; // ms
3939
@@ -73,24 +73,27 @@ public void onReceive(Context context, Intent intent) {
7373 };
7474
7575 @ Override
76- public void onServiceConnected () {
76+ public void onTowerConnected () {
7777 if (notificationHandler == null ) {
7878 notificationHandler = new NotificationHandler (getApplicationContext (), drone );
7979 }
8080
81- if (!drone .isStarted ()) {
82- this .drone .start ();
83- this .drone .registerDroneListener (this );
84- }
81+ drone .unregisterDroneListener (this );
82+ controlTower .registerDrone (drone , handler );
83+ drone .registerDroneListener (this );
8584
8685 notifyApiConnected ();
8786 }
8887
8988 @ Override
90- public void onServiceInterrupted () {
89+ public void onTowerDisconnected () {
9190 notifyApiDisconnected ();
9291 }
9392
93+ public DroidPlannerPrefs getAppPreferences () {
94+ return dpPrefs ;
95+ }
96+
9497 public interface ApiListener {
9598 void onApiConnected ();
9699
@@ -100,8 +103,8 @@ public interface ApiListener {
100103 private final Runnable disconnectionTask = new Runnable () {
101104 @ Override
102105 public void run () {
103- drone . destroy ( );
104- serviceMgr .disconnect ();
106+ controlTower . unregisterDrone ( drone );
107+ controlTower .disconnect ();
105108
106109 if (notificationHandler != null ) {
107110 notificationHandler .terminate ();
@@ -125,7 +128,7 @@ public void uncaughtException(Thread thread, Throwable ex) {
125128
126129 private Thread .UncaughtExceptionHandler exceptionHandler ;
127130
128- private ServiceManager serviceMgr ;
131+ private ControlTower controlTower ;
129132 private Drone drone ;
130133
131134 private MissionProxy missionProxy ;
@@ -141,8 +144,8 @@ public void onCreate() {
141144 dpPrefs = new DroidPlannerPrefs (context );
142145 lbm = LocalBroadcastManager .getInstance (context );
143146
144- serviceMgr = new ServiceManager (context );
145- drone = new Drone (serviceMgr , handler );
147+ controlTower = new ControlTower (context );
148+ drone = new Drone ();
146149 missionProxy = new MissionProxy (context , this .drone );
147150
148151 exceptionHandler = Thread .getDefaultUncaughtExceptionHandler ();
@@ -162,13 +165,13 @@ public void addApiListener(ApiListener listener) {
162165 return ;
163166
164167 handler .removeCallbacks (disconnectionTask );
165- boolean isServiceConnected = serviceMgr . isServiceConnected ();
166- if (isServiceConnected )
168+ boolean isTowerConnected = controlTower . isTowerConnected ();
169+ if (isTowerConnected )
167170 listener .onApiConnected ();
168171
169- if (!isServiceConnected ) {
172+ if (!isTowerConnected ) {
170173 try {
171- serviceMgr .connect (this );
174+ controlTower .connect (this );
172175 } catch (IllegalStateException e ) {
173176 //Ignore
174177 }
@@ -330,7 +333,7 @@ public void onDroneEvent(String event, Bundle extras) {
330333
331334 @ Override
332335 public void onDroneServiceInterrupted (String errorMsg ) {
333- drone . destroy ( );
336+ controlTower . unregisterDrone ( drone );
334337 if (notificationHandler != null ) {
335338 notificationHandler .terminate ();
336339 notificationHandler = null ;
0 commit comments