Browse files

better support for devices without the Play Store

  • Loading branch information...
1 parent 2bce24e commit 472d67f65fd2dd41dc8458945515d0897fa3fccd @commonsguy committed Dec 17, 2012
View
3 MapsV2/Basic/res/values/strings.xml
@@ -3,5 +3,6 @@
<string name="app_name">MapsV2 Basic Demo</string>
<string name="legal">Legal Notices</string>
-
+ <string name="no_maps">Google Maps V2 not available!</string>
+
</resources>
View
100 MapsV2/Basic/src/com/commonsware/android/mapsv2/basic/AbstractMapActivity.java
@@ -0,0 +1,100 @@
+/***
+ Copyright (c) 2012 CommonsWare, LLC
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ use this file except in compliance with the License. You may obtain a copy
+ of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
+ by applicable law or agreed to in writing, software distributed under the
+ License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
+ OF ANY KIND, either express or implied. See the License for the specific
+ language governing permissions and limitations under the License.
+
+ From _The Busy Coder's Guide to Android Development_
+ http://commonsware.com/Android
+ */
+
+package com.commonsware.android.mapsv2.basic;
+
+import android.app.Dialog;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.os.Bundle;
+import android.support.v4.app.DialogFragment;
+import android.widget.Toast;
+import com.actionbarsherlock.app.SherlockFragmentActivity;
+import com.actionbarsherlock.view.Menu;
+import com.actionbarsherlock.view.MenuItem;
+import com.google.android.gms.common.ConnectionResult;
+import com.google.android.gms.common.GooglePlayServicesUtil;
+
+public class AbstractMapActivity extends SherlockFragmentActivity {
+ protected static final String TAG_ERROR_DIALOG_FRAGMENT="errorDialog";
+
+ @Override
+ public boolean onCreateOptionsMenu(Menu menu) {
+ getSupportMenuInflater().inflate(R.menu.activity_main, menu);
+
+ return(super.onCreateOptionsMenu(menu));
+ }
+
+ @Override
+ public boolean onOptionsItemSelected(MenuItem item) {
+ if (item.getItemId() == R.id.legal) {
+ startActivity(new Intent(this, LegalNoticesActivity.class));
+
+ return(true);
+ }
+
+ return super.onOptionsItemSelected(item);
+ }
+
+ protected boolean readyToGo() {
+ int status=
+ GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
+
+ if (status == ConnectionResult.SUCCESS) {
+ return(true);
+ }
+ else if (GooglePlayServicesUtil.isUserRecoverableError(status)) {
+ ErrorDialogFragment.newInstance(status)
+ .show(getSupportFragmentManager(),
+ TAG_ERROR_DIALOG_FRAGMENT);
+ }
+ else {
+ Toast.makeText(this, R.string.no_maps, Toast.LENGTH_LONG).show();
+ finish();
+ }
+
+ return(false);
+ }
+
+ public static class ErrorDialogFragment extends DialogFragment {
+ static final String ARG_STATUS="status";
+
+ static ErrorDialogFragment newInstance(int status) {
+ Bundle args=new Bundle();
+
+ args.putInt(ARG_STATUS, status);
+
+ ErrorDialogFragment result=new ErrorDialogFragment();
+
+ result.setArguments(args);
+
+ return(result);
+ }
+
+ @Override
+ public Dialog onCreateDialog(Bundle savedInstanceState) {
+ Bundle args=getArguments();
+
+ return GooglePlayServicesUtil.getErrorDialog(args.getInt(ARG_STATUS),
+ getActivity(), 0);
+ }
+
+ @Override
+ public void onDismiss(DialogInterface dlg) {
+ if (getActivity() != null) {
+ getActivity().finish();
+ }
+ }
+ }
+}
View
27 MapsV2/Basic/src/com/commonsware/android/mapsv2/basic/MainActivity.java
@@ -14,34 +14,15 @@
package com.commonsware.android.mapsv2.basic;
-import android.content.Intent;
import android.os.Bundle;
-import com.actionbarsherlock.app.SherlockFragmentActivity;
-import com.actionbarsherlock.view.Menu;
-import com.actionbarsherlock.view.MenuItem;
-public class MainActivity extends SherlockFragmentActivity {
+public class MainActivity extends AbstractMapActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getSupportMenuInflater().inflate(R.menu.activity_main, menu);
-
- return(super.onCreateOptionsMenu(menu));
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- if (item.getItemId() == R.id.legal) {
- startActivity(new Intent(this, LegalNoticesActivity.class));
-
- return(true);
+
+ if (readyToGo()) {
+ setContentView(R.layout.activity_main);
}
-
- return super.onOptionsItemSelected(item);
}
}
View
100 MapsV2/Location/src/com/commonsware/android/mapsv2/location/AbstractMapActivity.java
@@ -0,0 +1,100 @@
+/***
+ Copyright (c) 2012 CommonsWare, LLC
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ use this file except in compliance with the License. You may obtain a copy
+ of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
+ by applicable law or agreed to in writing, software distributed under the
+ License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
+ OF ANY KIND, either express or implied. See the License for the specific
+ language governing permissions and limitations under the License.
+
+ From _The Busy Coder's Guide to Android Development_
+ http://commonsware.com/Android
+ */
+
+package com.commonsware.android.mapsv2.location;
+
+import android.app.Dialog;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.os.Bundle;
+import android.support.v4.app.DialogFragment;
+import android.widget.Toast;
+import com.actionbarsherlock.app.SherlockFragmentActivity;
+import com.actionbarsherlock.view.Menu;
+import com.actionbarsherlock.view.MenuItem;
+import com.google.android.gms.common.ConnectionResult;
+import com.google.android.gms.common.GooglePlayServicesUtil;
+
+public class AbstractMapActivity extends SherlockFragmentActivity {
+ protected static final String TAG_ERROR_DIALOG_FRAGMENT="errorDialog";
+
+ @Override
+ public boolean onCreateOptionsMenu(Menu menu) {
+ getSupportMenuInflater().inflate(R.menu.activity_main, menu);
+
+ return(super.onCreateOptionsMenu(menu));
+ }
+
+ @Override
+ public boolean onOptionsItemSelected(MenuItem item) {
+ if (item.getItemId() == R.id.legal) {
+ startActivity(new Intent(this, LegalNoticesActivity.class));
+
+ return(true);
+ }
+
+ return super.onOptionsItemSelected(item);
+ }
+
+ protected boolean readyToGo() {
+ int status=
+ GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
+
+ if (status == ConnectionResult.SUCCESS) {
+ return(true);
+ }
+ else if (GooglePlayServicesUtil.isUserRecoverableError(status)) {
+ ErrorDialogFragment.newInstance(status)
+ .show(getSupportFragmentManager(),
+ TAG_ERROR_DIALOG_FRAGMENT);
+ }
+ else {
+ Toast.makeText(this, R.string.no_maps, Toast.LENGTH_LONG).show();
+ finish();
+ }
+
+ return(false);
+ }
+
+ public static class ErrorDialogFragment extends DialogFragment {
+ static final String ARG_STATUS="status";
+
+ static ErrorDialogFragment newInstance(int status) {
+ Bundle args=new Bundle();
+
+ args.putInt(ARG_STATUS, status);
+
+ ErrorDialogFragment result=new ErrorDialogFragment();
+
+ result.setArguments(args);
+
+ return(result);
+ }
+
+ @Override
+ public Dialog onCreateDialog(Bundle savedInstanceState) {
+ Bundle args=getArguments();
+
+ return GooglePlayServicesUtil.getErrorDialog(args.getInt(ARG_STATUS),
+ getActivity(), 0);
+ }
+
+ @Override
+ public void onDismiss(DialogInterface dlg) {
+ if (getActivity() != null) {
+ getActivity().finish();
+ }
+ }
+ }
+}
View
41 MapsV2/Location/src/com/commonsware/android/mapsv2/location/MainActivity.java
@@ -14,7 +14,6 @@
package com.commonsware.android.mapsv2.location;
-import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
@@ -26,9 +25,6 @@
import java.util.ArrayList;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
-import com.actionbarsherlock.app.SherlockFragmentActivity;
-import com.actionbarsherlock.view.Menu;
-import com.actionbarsherlock.view.MenuItem;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
@@ -39,7 +35,7 @@
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
-public class MainActivity extends SherlockFragmentActivity implements
+public class MainActivity extends AbstractMapActivity implements
OnNavigationListener, OnInfoWindowClickListener, LocationSource,
LocationListener {
private static final int[] MAP_TYPE_NAMES= { R.string.normal,
@@ -55,20 +51,17 @@
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- SupportMapFragment mapFrag=
- (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
+ if (readyToGo()) {
+ setContentView(R.layout.activity_main);
- initListNav();
+ SupportMapFragment mapFrag=
+ (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
- map=mapFrag.getMap();
+ initListNav();
+
+ map=mapFrag.getMap();
- if (map == null) {
- Toast.makeText(this, R.string.no_maps, Toast.LENGTH_LONG).show();
- finish();
- }
- else {
locMgr=(LocationManager)getSystemService(LOCATION_SERVICE);
crit.setAccuracy(Criteria.ACCURACY_FINE);
@@ -112,24 +105,6 @@ public void onPause() {
}
@Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getSupportMenuInflater().inflate(R.menu.activity_main, menu);
-
- return(super.onCreateOptionsMenu(menu));
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- if (item.getItemId() == R.id.legal) {
- startActivity(new Intent(this, LegalNoticesActivity.class));
-
- return(true);
- }
-
- return super.onOptionsItemSelected(item);
- }
-
- @Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
map.setMapType(MAP_TYPES[itemPosition]);
View
100 MapsV2/Markers/src/com/commonsware/android/mapsv2/markers/AbstractMapActivity.java
@@ -0,0 +1,100 @@
+/***
+ Copyright (c) 2012 CommonsWare, LLC
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ use this file except in compliance with the License. You may obtain a copy
+ of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
+ by applicable law or agreed to in writing, software distributed under the
+ License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
+ OF ANY KIND, either express or implied. See the License for the specific
+ language governing permissions and limitations under the License.
+
+ From _The Busy Coder's Guide to Android Development_
+ http://commonsware.com/Android
+ */
+
+package com.commonsware.android.mapsv2.markers;
+
+import android.app.Dialog;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.os.Bundle;
+import android.support.v4.app.DialogFragment;
+import android.widget.Toast;
+import com.actionbarsherlock.app.SherlockFragmentActivity;
+import com.actionbarsherlock.view.Menu;
+import com.actionbarsherlock.view.MenuItem;
+import com.google.android.gms.common.ConnectionResult;
+import com.google.android.gms.common.GooglePlayServicesUtil;
+
+public class AbstractMapActivity extends SherlockFragmentActivity {
+ protected static final String TAG_ERROR_DIALOG_FRAGMENT="errorDialog";
+
+ @Override
+ public boolean onCreateOptionsMenu(Menu menu) {
+ getSupportMenuInflater().inflate(R.menu.activity_main, menu);
+
+ return(super.onCreateOptionsMenu(menu));
+ }
+
+ @Override
+ public boolean onOptionsItemSelected(MenuItem item) {
+ if (item.getItemId() == R.id.legal) {
+ startActivity(new Intent(this, LegalNoticesActivity.class));
+
+ return(true);
+ }
+
+ return super.onOptionsItemSelected(item);
+ }
+
+ protected boolean readyToGo() {
+ int status=
+ GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
+
+ if (status == ConnectionResult.SUCCESS) {
+ return(true);
+ }
+ else if (GooglePlayServicesUtil.isUserRecoverableError(status)) {
+ ErrorDialogFragment.newInstance(status)
+ .show(getSupportFragmentManager(),
+ TAG_ERROR_DIALOG_FRAGMENT);
+ }
+ else {
+ Toast.makeText(this, R.string.no_maps, Toast.LENGTH_LONG).show();
+ finish();
+ }
+
+ return(false);
+ }
+
+ public static class ErrorDialogFragment extends DialogFragment {
+ static final String ARG_STATUS="status";
+
+ static ErrorDialogFragment newInstance(int status) {
+ Bundle args=new Bundle();
+
+ args.putInt(ARG_STATUS, status);
+
+ ErrorDialogFragment result=new ErrorDialogFragment();
+
+ result.setArguments(args);
+
+ return(result);
+ }
+
+ @Override
+ public Dialog onCreateDialog(Bundle savedInstanceState) {
+ Bundle args=getArguments();
+
+ return GooglePlayServicesUtil.getErrorDialog(args.getInt(ARG_STATUS),
+ getActivity(), 0);
+ }
+
+ @Override
+ public void onDismiss(DialogInterface dlg) {
+ if (getActivity() != null) {
+ getActivity().finish();
+ }
+ }
+ }
+}
View
42 MapsV2/Markers/src/com/commonsware/android/mapsv2/markers/MainActivity.java
@@ -14,25 +14,20 @@
package com.commonsware.android.mapsv2.markers;
-import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.widget.ArrayAdapter;
-import android.widget.Toast;
import java.util.ArrayList;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
-import com.actionbarsherlock.app.SherlockFragmentActivity;
-import com.actionbarsherlock.view.Menu;
-import com.actionbarsherlock.view.MenuItem;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
-public class MainActivity extends SherlockFragmentActivity implements
+public class MainActivity extends AbstractMapActivity implements
OnNavigationListener {
private static final int[] MAP_TYPE_NAMES= { R.string.normal,
R.string.hybrid, R.string.satellite, R.string.terrain };
@@ -44,20 +39,17 @@
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- SupportMapFragment mapFrag=
- (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
+ if (readyToGo()) {
+ setContentView(R.layout.activity_main);
- initListNav();
+ SupportMapFragment mapFrag=
+ (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
- map=mapFrag.getMap();
+ initListNav();
+
+ map=mapFrag.getMap();
- if (map == null) {
- Toast.makeText(this, R.string.no_maps, Toast.LENGTH_LONG).show();
- finish();
- }
- else {
CameraUpdate center=
CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
-73.98180484771729));
@@ -79,24 +71,6 @@ protected void onCreate(Bundle savedInstanceState) {
}
@Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getSupportMenuInflater().inflate(R.menu.activity_main, menu);
-
- return(super.onCreateOptionsMenu(menu));
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- if (item.getItemId() == R.id.legal) {
- startActivity(new Intent(this, LegalNoticesActivity.class));
-
- return(true);
- }
-
- return super.onOptionsItemSelected(item);
- }
-
- @Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
map.setMapType(MAP_TYPES[itemPosition]);
View
100 MapsV2/NooYawk/src/com/commonsware/android/mapsv2/nooyawk/AbstractMapActivity.java
@@ -0,0 +1,100 @@
+/***
+ Copyright (c) 2012 CommonsWare, LLC
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ use this file except in compliance with the License. You may obtain a copy
+ of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
+ by applicable law or agreed to in writing, software distributed under the
+ License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
+ OF ANY KIND, either express or implied. See the License for the specific
+ language governing permissions and limitations under the License.
+
+ From _The Busy Coder's Guide to Android Development_
+ http://commonsware.com/Android
+ */
+
+package com.commonsware.android.mapsv2.nooyawk;
+
+import android.app.Dialog;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.os.Bundle;
+import android.support.v4.app.DialogFragment;
+import android.widget.Toast;
+import com.actionbarsherlock.app.SherlockFragmentActivity;
+import com.actionbarsherlock.view.Menu;
+import com.actionbarsherlock.view.MenuItem;
+import com.google.android.gms.common.ConnectionResult;
+import com.google.android.gms.common.GooglePlayServicesUtil;
+
+public class AbstractMapActivity extends SherlockFragmentActivity {
+ protected static final String TAG_ERROR_DIALOG_FRAGMENT="errorDialog";
+
+ @Override
+ public boolean onCreateOptionsMenu(Menu menu) {
+ getSupportMenuInflater().inflate(R.menu.activity_main, menu);
+
+ return(super.onCreateOptionsMenu(menu));
+ }
+
+ @Override
+ public boolean onOptionsItemSelected(MenuItem item) {
+ if (item.getItemId() == R.id.legal) {
+ startActivity(new Intent(this, LegalNoticesActivity.class));
+
+ return(true);
+ }
+
+ return super.onOptionsItemSelected(item);
+ }
+
+ protected boolean readyToGo() {
+ int status=
+ GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
+
+ if (status == ConnectionResult.SUCCESS) {
+ return(true);
+ }
+ else if (GooglePlayServicesUtil.isUserRecoverableError(status)) {
+ ErrorDialogFragment.newInstance(status)
+ .show(getSupportFragmentManager(),
+ TAG_ERROR_DIALOG_FRAGMENT);
+ }
+ else {
+ Toast.makeText(this, R.string.no_maps, Toast.LENGTH_LONG).show();
+ finish();
+ }
+
+ return(false);
+ }
+
+ public static class ErrorDialogFragment extends DialogFragment {
+ static final String ARG_STATUS="status";
+
+ static ErrorDialogFragment newInstance(int status) {
+ Bundle args=new Bundle();
+
+ args.putInt(ARG_STATUS, status);
+
+ ErrorDialogFragment result=new ErrorDialogFragment();
+
+ result.setArguments(args);
+
+ return(result);
+ }
+
+ @Override
+ public Dialog onCreateDialog(Bundle savedInstanceState) {
+ Bundle args=getArguments();
+
+ return GooglePlayServicesUtil.getErrorDialog(args.getInt(ARG_STATUS),
+ getActivity(), 0);
+ }
+
+ @Override
+ public void onDismiss(DialogInterface dlg) {
+ if (getActivity() != null) {
+ getActivity().finish();
+ }
+ }
+ }
+}
View
42 MapsV2/NooYawk/src/com/commonsware/android/mapsv2/nooyawk/MainActivity.java
@@ -14,24 +14,19 @@
package com.commonsware.android.mapsv2.nooyawk;
-import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.widget.ArrayAdapter;
-import android.widget.Toast;
import java.util.ArrayList;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
-import com.actionbarsherlock.app.SherlockFragmentActivity;
-import com.actionbarsherlock.view.Menu;
-import com.actionbarsherlock.view.MenuItem;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
-public class MainActivity extends SherlockFragmentActivity implements
+public class MainActivity extends AbstractMapActivity implements
OnNavigationListener {
private static final int[] MAP_TYPE_NAMES= { R.string.normal,
R.string.hybrid, R.string.satellite, R.string.terrain };
@@ -43,20 +38,17 @@
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- SupportMapFragment mapFrag=
- (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
+ if (readyToGo()) {
+ setContentView(R.layout.activity_main);
- initListNav();
+ SupportMapFragment mapFrag=
+ (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
- map=mapFrag.getMap();
+ initListNav();
+
+ map=mapFrag.getMap();
- if (map == null) {
- Toast.makeText(this, R.string.no_maps, Toast.LENGTH_LONG).show();
- finish();
- }
- else {
CameraUpdate center=
CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
-73.98180484771729));
@@ -68,24 +60,6 @@ protected void onCreate(Bundle savedInstanceState) {
}
@Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getSupportMenuInflater().inflate(R.menu.activity_main, menu);
-
- return(super.onCreateOptionsMenu(menu));
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- if (item.getItemId() == R.id.legal) {
- startActivity(new Intent(this, LegalNoticesActivity.class));
-
- return(true);
- }
-
- return super.onOptionsItemSelected(item);
- }
-
- @Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
map.setMapType(MAP_TYPES[itemPosition]);
View
100 MapsV2/Popups/src/com/commonsware/android/mapsv2/popups/AbstractMapActivity.java
@@ -0,0 +1,100 @@
+/***
+ Copyright (c) 2012 CommonsWare, LLC
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ use this file except in compliance with the License. You may obtain a copy
+ of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
+ by applicable law or agreed to in writing, software distributed under the
+ License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
+ OF ANY KIND, either express or implied. See the License for the specific
+ language governing permissions and limitations under the License.
+
+ From _The Busy Coder's Guide to Android Development_
+ http://commonsware.com/Android
+ */
+
+package com.commonsware.android.mapsv2.popups;
+
+import android.app.Dialog;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.os.Bundle;
+import android.support.v4.app.DialogFragment;
+import android.widget.Toast;
+import com.actionbarsherlock.app.SherlockFragmentActivity;
+import com.actionbarsherlock.view.Menu;
+import com.actionbarsherlock.view.MenuItem;
+import com.google.android.gms.common.ConnectionResult;
+import com.google.android.gms.common.GooglePlayServicesUtil;
+
+public class AbstractMapActivity extends SherlockFragmentActivity {
+ protected static final String TAG_ERROR_DIALOG_FRAGMENT="errorDialog";
+
+ @Override
+ public boolean onCreateOptionsMenu(Menu menu) {
+ getSupportMenuInflater().inflate(R.menu.activity_main, menu);
+
+ return(super.onCreateOptionsMenu(menu));
+ }
+
+ @Override
+ public boolean onOptionsItemSelected(MenuItem item) {
+ if (item.getItemId() == R.id.legal) {
+ startActivity(new Intent(this, LegalNoticesActivity.class));
+
+ return(true);
+ }
+
+ return super.onOptionsItemSelected(item);
+ }
+
+ protected boolean readyToGo() {
+ int status=
+ GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
+
+ if (status == ConnectionResult.SUCCESS) {
+ return(true);
+ }
+ else if (GooglePlayServicesUtil.isUserRecoverableError(status)) {
+ ErrorDialogFragment.newInstance(status)
+ .show(getSupportFragmentManager(),
+ TAG_ERROR_DIALOG_FRAGMENT);
+ }
+ else {
+ Toast.makeText(this, R.string.no_maps, Toast.LENGTH_LONG).show();
+ finish();
+ }
+
+ return(false);
+ }
+
+ public static class ErrorDialogFragment extends DialogFragment {
+ static final String ARG_STATUS="status";
+
+ static ErrorDialogFragment newInstance(int status) {
+ Bundle args=new Bundle();
+
+ args.putInt(ARG_STATUS, status);
+
+ ErrorDialogFragment result=new ErrorDialogFragment();
+
+ result.setArguments(args);
+
+ return(result);
+ }
+
+ @Override
+ public Dialog onCreateDialog(Bundle savedInstanceState) {
+ Bundle args=getArguments();
+
+ return GooglePlayServicesUtil.getErrorDialog(args.getInt(ARG_STATUS),
+ getActivity(), 0);
+ }
+
+ @Override
+ public void onDismiss(DialogInterface dlg) {
+ if (getActivity() != null) {
+ getActivity().finish();
+ }
+ }
+ }
+}
View
41 MapsV2/Popups/src/com/commonsware/android/mapsv2/popups/MainActivity.java
@@ -14,17 +14,13 @@
package com.commonsware.android.mapsv2.popups;
-import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
-import com.actionbarsherlock.app.SherlockFragmentActivity;
-import com.actionbarsherlock.view.Menu;
-import com.actionbarsherlock.view.MenuItem;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
@@ -34,7 +30,7 @@
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
-public class MainActivity extends SherlockFragmentActivity implements
+public class MainActivity extends AbstractMapActivity implements
OnNavigationListener, OnInfoWindowClickListener {
private static final int[] MAP_TYPE_NAMES= { R.string.normal,
R.string.hybrid, R.string.satellite, R.string.terrain };
@@ -46,20 +42,17 @@
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- SupportMapFragment mapFrag=
- (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
+ if (readyToGo()) {
+ setContentView(R.layout.activity_main);
- initListNav();
+ SupportMapFragment mapFrag=
+ (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
- map=mapFrag.getMap();
+ initListNav();
+
+ map=mapFrag.getMap();
- if (map == null) {
- Toast.makeText(this, R.string.no_maps, Toast.LENGTH_LONG).show();
- finish();
- }
- else {
CameraUpdate center=
CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
-73.98180484771729));
@@ -84,24 +77,6 @@ protected void onCreate(Bundle savedInstanceState) {
}
@Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getSupportMenuInflater().inflate(R.menu.activity_main, menu);
-
- return(super.onCreateOptionsMenu(menu));
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- if (item.getItemId() == R.id.legal) {
- startActivity(new Intent(this, LegalNoticesActivity.class));
-
- return(true);
- }
-
- return super.onOptionsItemSelected(item);
- }
-
- @Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
map.setMapType(MAP_TYPES[itemPosition]);

0 comments on commit 472d67f

Please sign in to comment.