16
16
import com .o3dr .android .client .apis .MissionApi ;
17
17
import com .o3dr .android .client .apis .VehicleApi ;
18
18
import com .o3dr .android .client .interfaces .DroneListener ;
19
+ import com .o3dr .android .client .utils .TxPowerComplianceCountries ;
19
20
import com .o3dr .services .android .lib .coordinate .LatLong ;
20
21
import com .o3dr .services .android .lib .drone .attribute .AttributeEvent ;
21
22
import com .o3dr .services .android .lib .drone .attribute .AttributeType ;
22
23
import com .o3dr .services .android .lib .drone .calibration .magnetometer .MagnetometerCalibrationStatus ;
23
24
import com .o3dr .services .android .lib .drone .companion .solo .SoloAttributes ;
25
+ import com .o3dr .services .android .lib .drone .companion .solo .SoloEventExtras ;
26
+ import com .o3dr .services .android .lib .drone .companion .solo .SoloEvents ;
24
27
import com .o3dr .services .android .lib .drone .connection .ConnectionParameter ;
25
28
import com .o3dr .services .android .lib .drone .connection .ConnectionResult ;
26
29
import com .o3dr .services .android .lib .drone .mission .Mission ;
@@ -132,17 +135,19 @@ void init(ControlTower controlTower, Handler handler) {
132
135
this .droneObserver = new DroneObserver (this );
133
136
}
134
137
135
- Context getContext (){
138
+ Context getContext () {
136
139
return this .context ;
137
140
}
138
141
139
142
synchronized void start () {
140
- if (!serviceMgr .isTowerConnected ())
143
+ if (!serviceMgr .isTowerConnected ()) {
141
144
throw new IllegalStateException ("Service manager must be connected." );
145
+ }
142
146
143
147
IDroneApi droneApi = droneApiRef .get ();
144
- if (isStarted (droneApi ))
148
+ if (isStarted (droneApi )) {
145
149
return ;
150
+ }
146
151
147
152
try {
148
153
droneApi = serviceMgr .get3drServices ().registerDroneApi (this .apiListener , serviceMgr .getApplicationId ());
@@ -151,8 +156,9 @@ synchronized void start() {
151
156
throw new IllegalStateException ("Unable to retrieve a valid drone handle." );
152
157
}
153
158
154
- if (asyncScheduler == null || asyncScheduler .isShutdown ())
159
+ if (asyncScheduler == null || asyncScheduler .isShutdown ()) {
155
160
asyncScheduler = Executors .newFixedThreadPool (1 );
161
+ }
156
162
157
163
addAttributesObserver (droneApi , this .droneObserver );
158
164
resetFlightTimer ();
@@ -185,16 +191,17 @@ synchronized void destroy() {
185
191
private void checkForGroundCollision () {
186
192
Speed speed = getAttribute (AttributeType .SPEED );
187
193
Altitude altitude = getAttribute (AttributeType .ALTITUDE );
188
- if (speed == null || altitude == null )
194
+ if (speed == null || altitude == null ) {
189
195
return ;
196
+ }
190
197
191
198
double verticalSpeed = speed .getVerticalSpeed ();
192
199
double altitudeValue = altitude .getAltitude ();
193
200
194
201
boolean isCollisionImminent = altitudeValue
195
- + (verticalSpeed * COLLISION_SECONDS_BEFORE_COLLISION ) < 0
196
- && verticalSpeed < COLLISION_DANGEROUS_SPEED_METERS_PER_SECOND
197
- && altitudeValue > COLLISION_SAFE_ALTITUDE_METERS ;
202
+ + (verticalSpeed * COLLISION_SECONDS_BEFORE_COLLISION ) < 0
203
+ && verticalSpeed < COLLISION_DANGEROUS_SPEED_METERS_PER_SECOND
204
+ && altitudeValue > COLLISION_SAFE_ALTITUDE_METERS ;
198
205
199
206
Bundle extrasBundle = new Bundle (1 );
200
207
extrasBundle .putBoolean (EXTRA_IS_GROUND_COLLISION_IMMINENT , isCollisionImminent );
@@ -214,8 +221,9 @@ public double getSpeedParameter() {
214
221
Parameters params = getAttribute (AttributeType .PARAMETERS );
215
222
if (params != null ) {
216
223
Parameter speedParam = params .getParameter ("WPNAV_SPEED" );
217
- if (speedParam != null )
224
+ if (speedParam != null ) {
218
225
return speedParam .getValue ();
226
+ }
219
227
}
220
228
221
229
return 0 ;
@@ -227,8 +235,9 @@ public double getSpeedParameter() {
227
235
* @param action Runnabl that will be executed.
228
236
*/
229
237
public void post (Runnable action ) {
230
- if (handler == null || action == null )
238
+ if (handler == null || action == null ) {
231
239
return ;
240
+ }
232
241
233
242
handler .post (action );
234
243
}
@@ -262,8 +271,9 @@ public long getFlightTime() {
262
271
263
272
public <T extends Parcelable > T getAttribute (String type ) {
264
273
final IDroneApi droneApi = droneApiRef .get ();
265
- if (!isStarted (droneApi ) || type == null )
274
+ if (!isStarted (droneApi ) || type == null ) {
266
275
return this .getAttributeDefaultValue (type );
276
+ }
267
277
268
278
T attribute = null ;
269
279
Bundle carrier = null ;
@@ -277,7 +287,7 @@ public <T extends Parcelable> T getAttribute(String type) {
277
287
try {
278
288
carrier .setClassLoader (contextClassLoader );
279
289
attribute = carrier .getParcelable (type );
280
- }catch (Exception e ){
290
+ } catch (Exception e ) {
281
291
Log .e (TAG , e .getMessage (), e );
282
292
}
283
293
}
@@ -287,8 +297,9 @@ public <T extends Parcelable> T getAttribute(String type) {
287
297
288
298
public <T extends Parcelable > void getAttributeAsync (final String attributeType ,
289
299
final OnAttributeRetrievedCallback <T > callback ) {
290
- if (callback == null )
300
+ if (callback == null ) {
291
301
throw new IllegalArgumentException ("Callback must be non-null." );
302
+ }
292
303
293
304
final IDroneApi droneApi = droneApiRef .get ();
294
305
if (!isStarted (droneApi )) {
@@ -309,19 +320,21 @@ public void run() {
309
320
handler .post (new Runnable () {
310
321
@ Override
311
322
public void run () {
312
- if (attribute == null )
323
+ if (attribute == null ) {
313
324
callback .onRetrievalFailed ();
314
- else
325
+ } else {
315
326
callback .onRetrievalSucceed (attribute );
327
+ }
316
328
}
317
329
});
318
330
}
319
331
});
320
332
}
321
333
322
334
private <T extends Parcelable > T getAttributeDefaultValue (String attributeType ) {
323
- if (attributeType == null )
335
+ if (attributeType == null ) {
324
336
return null ;
337
+ }
325
338
326
339
switch (attributeType ) {
327
340
case AttributeType .ALTITUDE :
@@ -475,7 +488,7 @@ private boolean isStarted(IDroneApi droneApi) {
475
488
return droneApi != null && droneApi .asBinder ().pingBinder ();
476
489
}
477
490
478
- public boolean isStarted (){
491
+ public boolean isStarted () {
479
492
return isStarted (droneApiRef .get ());
480
493
}
481
494
@@ -491,11 +504,13 @@ public ConnectionParameter getConnectionParameter() {
491
504
492
505
public <T extends MissionItem > void buildMissionItemsAsync (final MissionItem .ComplexItem <T >[] missionItems ,
493
506
final OnMissionItemsBuiltCallback <T > callback ) {
494
- if (callback == null )
507
+ if (callback == null ) {
495
508
throw new IllegalArgumentException ("Callback must be non-null." );
509
+ }
496
510
497
- if (missionItems == null || missionItems .length == 0 )
511
+ if (missionItems == null || missionItems .length == 0 ) {
498
512
return ;
513
+ }
499
514
500
515
asyncScheduler .execute (new Runnable () {
501
516
@ Override
@@ -514,11 +529,13 @@ public void run() {
514
529
}
515
530
516
531
public void registerDroneListener (DroneListener listener ) {
517
- if (listener == null )
532
+ if (listener == null ) {
518
533
return ;
534
+ }
519
535
520
- if (!droneListeners .contains (listener ))
536
+ if (!droneListeners .contains (listener )) {
521
537
droneListeners .add (listener );
538
+ }
522
539
}
523
540
524
541
private void addAttributesObserver (IDroneApi droneApi , IObserver observer ) {
@@ -554,8 +571,9 @@ public void removeMavlinkObserver(MavlinkObserver observer) {
554
571
}
555
572
556
573
public void unregisterDroneListener (DroneListener listener ) {
557
- if (listener == null )
574
+ if (listener == null ) {
558
575
return ;
576
+ }
559
577
560
578
droneListeners .remove (listener );
561
579
}
@@ -689,13 +707,14 @@ public void loadWaypoints() {
689
707
MissionApi .getApi (this ).loadWaypoints ();
690
708
}
691
709
692
- public Handler getHandler (){
710
+ public Handler getHandler () {
693
711
return handler ;
694
712
}
695
713
696
714
void notifyDroneConnectionFailed (final ConnectionResult result ) {
697
- if (droneListeners .isEmpty ())
715
+ if (droneListeners .isEmpty ()) {
698
716
return ;
717
+ }
699
718
700
719
handler .post (new Runnable () {
701
720
@ Override
@@ -708,38 +727,59 @@ public void run() {
708
727
709
728
void notifyAttributeUpdated (final String attributeEvent , final Bundle extras ) {
710
729
//Update the bundle classloader
711
- if (extras != null )
730
+ if (extras != null ) {
712
731
extras .setClassLoader (contextClassLoader );
732
+ }
713
733
714
- if (AttributeEvent .STATE_UPDATED .equals (attributeEvent )) {
715
- getAttributeAsync (AttributeType .STATE , new OnAttributeRetrievedCallback <State >() {
716
- @ Override
717
- public void onRetrievalSucceed (State state ) {
718
- if (state .isFlying ())
719
- resetFlightTimer ();
720
- else
721
- stopTimer ();
722
- }
734
+ switch (attributeEvent ) {
735
+ case AttributeEvent .STATE_UPDATED :
736
+ getAttributeAsync (AttributeType .STATE , new OnAttributeRetrievedCallback <State >() {
737
+ @ Override
738
+ public void onRetrievalSucceed (State state ) {
739
+ if (state .isFlying ()) {
740
+ resetFlightTimer ();
741
+ } else {
742
+ stopTimer ();
743
+ }
744
+ }
723
745
724
- @ Override
725
- public void onRetrievalFailed () {
726
- stopTimer ();
727
- }
728
- });
729
- } else if (AttributeEvent .SPEED_UPDATED .equals (attributeEvent )) {
730
- checkForGroundCollision ();
746
+ @ Override
747
+ public void onRetrievalFailed () {
748
+ stopTimer ();
749
+ }
750
+ });
751
+ break ;
752
+
753
+ case AttributeEvent .SPEED_UPDATED :
754
+ checkForGroundCollision ();
755
+ break ;
756
+
757
+ //TODO remove this when deprecated methods are deleted in 3.0
758
+ // This ensures that the api is backwards compatible
759
+ case SoloEvents .SOLO_TX_POWER_COMPLIANCE_COUNTRY_UPDATED :
760
+ String compliantCountry = extras .getString (SoloEventExtras .EXTRA_SOLO_TX_POWER_COMPLIANT_COUNTRY );
761
+ final Bundle eventInfo = new Bundle (1 );
762
+ boolean isEUCompliant = !TxPowerComplianceCountries .getDefaultCountry ().name ().equals (compliantCountry );
763
+ eventInfo .putBoolean (SoloEventExtras .EXTRA_SOLO_EU_TX_POWER_COMPLIANT , isEUCompliant );
764
+ sendEventToListeners (SoloEvents .SOLO_EU_TX_POWER_COMPLIANCE_UPDATED , eventInfo );
765
+ break ;
731
766
}
732
767
733
- if (droneListeners .isEmpty ())
768
+ sendEventToListeners (attributeEvent , extras );
769
+ }
770
+
771
+ private void sendEventToListeners (final String attributeEvent , final Bundle extras ) {
772
+ if (droneListeners .isEmpty ()) {
734
773
return ;
774
+ }
735
775
736
776
handler .post (new Runnable () {
737
777
@ Override
738
778
public void run () {
739
779
for (DroneListener listener : droneListeners ) {
740
780
try {
741
781
listener .onDroneEvent (attributeEvent , extras );
742
- }catch (Exception e ){
782
+ } catch (Exception e ) {
743
783
Log .e (TAG , e .getMessage (), e );
744
784
}
745
785
}
@@ -748,8 +788,9 @@ public void run() {
748
788
}
749
789
750
790
void notifyDroneServiceInterrupted (final String errorMsg ) {
751
- if (droneListeners .isEmpty ())
791
+ if (droneListeners .isEmpty ()) {
752
792
return ;
793
+ }
753
794
754
795
handler .post (new Runnable () {
755
796
@ Override
0 commit comments