11package org .droidplanner .activities ;
22
3+ import android .app .Dialog ;
4+ import android .content .DialogInterface ;
5+ import android .content .Intent ;
6+ import android .os .Bundle ;
7+ import android .support .v4 .app .Fragment ;
8+ import android .support .v4 .app .FragmentManager ;
9+ import android .view .View ;
10+ import android .widget .Toast ;
11+
12+ import com .google .android .gms .common .ConnectionResult ;
13+ import com .google .android .gms .common .GooglePlayServicesUtil ;
14+ import com .google .android .gms .maps .model .LatLng ;
15+
16+ import org .droidplanner .R ;
317import org .droidplanner .activities .helpers .SuperUI ;
418import org .droidplanner .drone .Drone ;
519import org .droidplanner .drone .DroneInterfaces .DroneEventsType ;
620import org .droidplanner .drone .DroneInterfaces .OnDroneListener ;
721import org .droidplanner .drone .variables .mission .MissionItem ;
822import org .droidplanner .drone .variables .mission .waypoints .SpatialCoordItem ;
923import org .droidplanner .fragments .FlightActionsFragment ;
24+ import org .droidplanner .fragments .FlightActionsFragment .OnMissionControlInteraction ;
1025import org .droidplanner .fragments .FlightMapFragment ;
1126import org .droidplanner .fragments .RCFragment ;
1227import org .droidplanner .fragments .TelemetryFragment ;
13- import org .droidplanner .fragments .FlightActionsFragment .OnMissionControlInteraction ;
1428import org .droidplanner .fragments .helpers .DroneMap ;
1529import org .droidplanner .fragments .helpers .OnMapInteractionListener ;
1630import org .droidplanner .fragments .mode .ModeAcroFragment ;
2741import org .droidplanner .fragments .mode .ModeStabilizeFragment ;
2842import org .droidplanner .polygon .PolygonPoint ;
2943
30- import android .content .Intent ;
31- import android .os .Bundle ;
32- import android .support .v4 .app .Fragment ;
33- import android .support .v4 .app .FragmentManager ;
34- import android .view .View ;
35-
36- import org .droidplanner .R ;
37- import com .google .android .gms .maps .model .LatLng ;
38-
3944public class FlightActivity extends SuperUI implements
4045 OnMapInteractionListener , OnMissionControlInteraction , OnDroneListener {
4146
47+ private static final int GOOGLE_PLAY_SERVICES_REQUEST_CODE = 101 ;
48+
4249 private FragmentManager fragmentManager ;
4350 private RCFragment rcFragment ;
4451 private View failsafeTextView ;
@@ -61,12 +68,6 @@ public void onCreate(Bundle savedInstanceState) {
6168 fragmentManager .beginTransaction ().add (R .id .modeInfoPanel , modeRtl ).commit ();
6269 }
6370
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-
7071 Fragment telemetryFragment = fragmentManager .findFragmentById (R .id .telemetryFragment );
7172 if (telemetryFragment == null ){
7273 telemetryFragment = new TelemetryFragment ();
@@ -79,14 +80,61 @@ public void onCreate(Bundle savedInstanceState) {
7980 editorTools = new FlightActionsFragment ();
8081 fragmentManager .beginTransaction ().add (R .id .editorToolsFragment , editorTools ).commit ();
8182 }
82- }
83+
84+ }
85+
86+ /**
87+ * Ensures that the device has the correct version of the Google Play Services.
88+ * @return true if the Google Play Services binary is valid
89+ */
90+ private boolean isGooglePlayServicesValid (boolean showErrorDialog ){
91+ //Check for the google play services is available
92+ final int playStatus = GooglePlayServicesUtil .isGooglePlayServicesAvailable
93+ (getApplicationContext ());
94+ final boolean isValid = playStatus == ConnectionResult .SUCCESS ;
95+
96+ if (!isValid && showErrorDialog ) {
97+ final Dialog errorDialog = GooglePlayServicesUtil .getErrorDialog (playStatus , this ,
98+ GOOGLE_PLAY_SERVICES_REQUEST_CODE , new DialogInterface .OnCancelListener () {
99+ @ Override
100+ public void onCancel (DialogInterface dialog ) {
101+ finish ();
102+ }
103+ });
104+
105+ if (errorDialog != null )
106+ errorDialog .show ();
107+ }
108+
109+ return isValid ;
110+ }
111+
112+ /**
113+ * Used to setup the flight screen map fragment. Before attempting to initialize the map
114+ * fragment, this checks if the Google Play Services binary is installed and up to date.
115+ */
116+ private void setupMapFragment (){
117+ if (mapFragment == null && isGooglePlayServicesValid (true )) {
118+ mapFragment = fragmentManager .findFragmentById (R .id .mapFragment );
119+ if (mapFragment == null ) {
120+ mapFragment = new FlightMapFragment ();
121+ fragmentManager .beginTransaction ().add (R .id .mapFragment , mapFragment ).commit ();
122+ }
123+ }
124+ }
83125
84126 @ Override
85127 protected void onStart () {
86128 super .onStart ();
87129 onModeChanged (drone ); // Update the mode detail panel;
88130 }
89131
132+ @ Override
133+ public void onResume (){
134+ super .onResume ();
135+ setupMapFragment ();
136+ }
137+
90138 @ Override
91139 public void onAddPoint (LatLng point ) {
92140 // TODO Auto-generated method stub
@@ -130,8 +178,11 @@ public void onJoystickSelected() {
130178
131179 @ Override
132180 public void onPlanningSelected () {
133- ((DroneMap ) mapFragment ).saveCameraPosition ();
134- Intent navigationIntent ;
181+ if (mapFragment != null ) {
182+ ((DroneMap ) mapFragment ).saveCameraPosition ();
183+ }
184+
185+ Intent navigationIntent ;
135186 navigationIntent = new Intent (this , EditorActivity .class );
136187 startActivity (navigationIntent );
137188 }
0 commit comments