Skip to content

Commit

Permalink
better support for devices without the Play Store
Browse files Browse the repository at this point in the history
  • Loading branch information
commonsguy committed Dec 17, 2012
1 parent 2bce24e commit 472d67f
Show file tree
Hide file tree
Showing 11 changed files with 538 additions and 158 deletions.
3 changes: 2 additions & 1 deletion MapsV2/Basic/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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,
Expand All @@ -55,20 +51,17 @@ public class MainActivity extends SherlockFragmentActivity implements
@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);

Expand Down Expand Up @@ -111,24 +104,6 @@ public void onPause() {
super.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]);
Expand Down
Loading

0 comments on commit 472d67f

Please sign in to comment.