11package org .droidplanner .activities ;
22
3+ import org .droidplanner .R ;
34import org .droidplanner .activities .helpers .SuperUI ;
45import org .droidplanner .drone .Drone ;
56import org .droidplanner .drone .DroneInterfaces .DroneEventsType ;
67import org .droidplanner .drone .DroneInterfaces .OnDroneListener ;
78import org .droidplanner .drone .variables .mission .MissionItem ;
89import org .droidplanner .drone .variables .mission .waypoints .SpatialCoordItem ;
910import org .droidplanner .fragments .FlightActionsFragment ;
11+ import org .droidplanner .fragments .FlightActionsFragment .OnMissionControlInteraction ;
1012import org .droidplanner .fragments .FlightMapFragment ;
1113import org .droidplanner .fragments .RCFragment ;
1214import org .droidplanner .fragments .TelemetryFragment ;
13- import org .droidplanner .fragments .FlightActionsFragment .OnMissionControlInteraction ;
1415import org .droidplanner .fragments .helpers .DroneMap ;
1516import org .droidplanner .fragments .helpers .OnMapInteractionListener ;
1617import org .droidplanner .fragments .mode .ModeAcroFragment ;
2728import org .droidplanner .fragments .mode .ModeStabilizeFragment ;
2829import org .droidplanner .polygon .PolygonPoint ;
2930
31+ import android .app .Dialog ;
32+ import android .content .DialogInterface ;
3033import android .content .Intent ;
3134import android .os .Bundle ;
3235import android .support .v4 .app .Fragment ;
3336import android .support .v4 .app .FragmentManager ;
3437import android .view .View ;
3538
36- import org .droidplanner .R ;
39+ import com .google .android .gms .common .ConnectionResult ;
40+ import com .google .android .gms .common .GooglePlayServicesUtil ;
3741import com .google .android .gms .maps .model .LatLng ;
3842
3943public class FlightActivity extends SuperUI implements
4044 OnMapInteractionListener , OnMissionControlInteraction , OnDroneListener {
4145
46+ private static final int GOOGLE_PLAY_SERVICES_REQUEST_CODE = 101 ;
47+
4248 private FragmentManager fragmentManager ;
4349 private RCFragment rcFragment ;
4450 private View failsafeTextView ;
@@ -61,12 +67,6 @@ public void onCreate(Bundle savedInstanceState) {
6167 fragmentManager .beginTransaction ().add (R .id .modeInfoPanel , modeRtl ).commit ();
6268 }
6369
64- mapFragment = fragmentManager .findFragmentById (R .id .mapFragment );
65- if (mapFragment == null ){
66- mapFragment = new FlightMapFragment ();
67- fragmentManager .beginTransaction ().add (R .id .mapFragment , mapFragment ).commit ();
68- }
69-
7070 Fragment telemetryFragment = fragmentManager .findFragmentById (R .id .telemetryFragment );
7171 if (telemetryFragment == null ){
7272 telemetryFragment = new TelemetryFragment ();
@@ -79,14 +79,61 @@ public void onCreate(Bundle savedInstanceState) {
7979 editorTools = new FlightActionsFragment ();
8080 fragmentManager .beginTransaction ().add (R .id .editorToolsFragment , editorTools ).commit ();
8181 }
82- }
82+
83+ }
84+
85+ /**
86+ * Ensures that the device has the correct version of the Google Play Services.
87+ * @return true if the Google Play Services binary is valid
88+ */
89+ private boolean isGooglePlayServicesValid (boolean showErrorDialog ){
90+ //Check for the google play services is available
91+ final int playStatus = GooglePlayServicesUtil .isGooglePlayServicesAvailable
92+ (getApplicationContext ());
93+ final boolean isValid = playStatus == ConnectionResult .SUCCESS ;
94+
95+ if (!isValid && showErrorDialog ) {
96+ final Dialog errorDialog = GooglePlayServicesUtil .getErrorDialog (playStatus , this ,
97+ GOOGLE_PLAY_SERVICES_REQUEST_CODE , new DialogInterface .OnCancelListener () {
98+ @ Override
99+ public void onCancel (DialogInterface dialog ) {
100+ finish ();
101+ }
102+ });
103+
104+ if (errorDialog != null )
105+ errorDialog .show ();
106+ }
107+
108+ return isValid ;
109+ }
110+
111+ /**
112+ * Used to setup the flight screen map fragment. Before attempting to initialize the map
113+ * fragment, this checks if the Google Play Services binary is installed and up to date.
114+ */
115+ private void setupMapFragment (){
116+ if (mapFragment == null && isGooglePlayServicesValid (true )) {
117+ mapFragment = fragmentManager .findFragmentById (R .id .mapFragment );
118+ if (mapFragment == null ) {
119+ mapFragment = new FlightMapFragment ();
120+ fragmentManager .beginTransaction ().add (R .id .mapFragment , mapFragment ).commit ();
121+ }
122+ }
123+ }
83124
84125 @ Override
85126 protected void onStart () {
86127 super .onStart ();
87128 onModeChanged (drone ); // Update the mode detail panel;
88129 }
89130
131+ @ Override
132+ public void onResume (){
133+ super .onResume ();
134+ setupMapFragment ();
135+ }
136+
90137 @ Override
91138 public void onAddPoint (LatLng point ) {
92139 // TODO Auto-generated method stub
@@ -130,8 +177,11 @@ public void onJoystickSelected() {
130177
131178 @ Override
132179 public void onPlanningSelected () {
133- ((DroneMap ) mapFragment ).saveCameraPosition ();
134- Intent navigationIntent ;
180+ if (mapFragment != null ) {
181+ ((DroneMap ) mapFragment ).saveCameraPosition ();
182+ }
183+
184+ Intent navigationIntent ;
135185 navigationIntent = new Intent (this , EditorActivity .class );
136186 startActivity (navigationIntent );
137187 }
0 commit comments