Use a dialog fragment to show map layer

This commit is contained in:
Jimmy Shih
2014-01-16 14:03:12 -08:00
parent a4cf0518c4
commit 9f45405d89
8 changed files with 134 additions and 72 deletions
@@ -301,6 +301,12 @@ public class ChartFragment extends Fragment implements TrackDataListener {
}
return false;
}
@Override
public boolean onMapTypeChanged(int mapType) {
// We don't care.
return false;
}
/**
* Checks the chart settings.
@@ -0,0 +1,71 @@
/*
* Copyright 2014 Google Inc.
*
* 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.
*/
package com.google.android.apps.mytracks.fragments;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.maps.mytracks.R;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
/**
* A DialogFragment to select a map layer.
*
* @author Jimmy Shih
*/
public class MapLayerDialogFragment extends AbstractMyTracksDialogFragment {
public static final String MAP_LAYER_DIALOG_TAG = "mapLayer";
private static final int[] LAYERS = { R.string.menu_map, R.string.menu_satellite,
R.string.menu_satellite_with_streets, R.string.menu_terrain };
private static final int[] MAP_TYPES = { GoogleMap.MAP_TYPE_NORMAL, GoogleMap.MAP_TYPE_SATELLITE,
GoogleMap.MAP_TYPE_HYBRID, GoogleMap.MAP_TYPE_TERRAIN };
@Override
protected Dialog createDialog() {
String[] choices = new String[LAYERS.length];
for (int i = 0; i < LAYERS.length; i++) {
choices[i] = getString(LAYERS[i]);
}
int mapType = PreferencesUtils.getInt(
getActivity(), R.string.map_type_key, PreferencesUtils.MAP_TYPE_DEFAUlT);
return new AlertDialog.Builder(getActivity()).setNegativeButton(R.string.generic_cancel, null)
.setPositiveButton(R.string.generic_ok, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int position = ((AlertDialog) dialog).getListView().getCheckedItemPosition();
PreferencesUtils.setInt(getActivity(), R.string.map_type_key, MAP_TYPES[position]);
}
}).setSingleChoiceItems(choices, getPositionFromMapType(mapType), null)
.setTitle(R.string.menu_map_layer).create();
}
private int getPositionFromMapType(int mapType) {
for (int i = 0; i < MAP_TYPES.length; i++) {
if (MAP_TYPES[i] == mapType) {
return i;
}
}
return 0;
}
}
@@ -276,12 +276,6 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
if (googleMap != null) {
// Update map type
int mapType = PreferencesUtils.getInt(
getActivity(), R.string.map_type_key, PreferencesUtils.MAP_TYPE_DEFAUlT);
googleMap.setMapType(mapType);
ApiAdapterFactory.getApiAdapter().invalidMenu(getActivity());
// Disable my location if gps is disabled
googleMap.setMyLocationEnabled(isGpsProviderEnabled);
}
@@ -329,59 +323,13 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
TrackIconUtils.setMenuIconColor(menu);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
if (googleMap != null) {
int id;
switch (googleMap.getMapType()) {
case GoogleMap.MAP_TYPE_NORMAL:
id = R.id.menu_map;
break;
case GoogleMap.MAP_TYPE_SATELLITE:
id = R.id.menu_satellite;
break;
case GoogleMap.MAP_TYPE_HYBRID:
id = R.id.menu_satellite_with_streets;
break;
case GoogleMap.MAP_TYPE_TERRAIN:
id = R.id.menu_terrain;
break;
default:
id = R.id.menu_map;
}
MenuItem menuItem = menu.findItem(id);
if (menuItem != null) {
menuItem.setChecked(true);
}
}
super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
int type = GoogleMap.MAP_TYPE_NORMAL;
switch (menuItem.getItemId()) {
case R.id.menu_map:
type = GoogleMap.MAP_TYPE_NORMAL;
break;
case R.id.menu_satellite:
type = GoogleMap.MAP_TYPE_SATELLITE;
break;
case R.id.menu_satellite_with_streets:
type = GoogleMap.MAP_TYPE_HYBRID;
break;
case R.id.menu_terrain:
type = GoogleMap.MAP_TYPE_TERRAIN;
break;
default:
return super.onOptionsItemSelected(menuItem);
if (menuItem.getItemId() == R.id.menu_map_layer) {
((TrackDetailActivity) getActivity()).showMapLayerDialog();
return true;
}
if (googleMap != null) {
googleMap.setMapType(type);
menuItem.setChecked(true);
PreferencesUtils.setInt(getActivity(), R.string.map_type_key, type);
}
return true;
return super.onOptionsItemSelected(menuItem);
}
@Override
@@ -499,6 +447,20 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
// We don't care.
return false;
}
@Override
public boolean onMapTypeChanged(final int mapType) {
if (isResumed()) {
getActivity().runOnUiThread(new Runnable() {
public void run() {
if (isResumed() && googleMap != null) {
googleMap.setMapType(mapType);
}
}
});
}
return false;
}
/**
* Resumes the trackDataHub. Needs to be synchronized because the trackDataHub
@@ -251,6 +251,12 @@ public class StatsFragment extends Fragment implements TrackDataListener {
return false;
}
@Override
public boolean onMapTypeChanged(int mapType) {
// We don't care
return false;
}
/**
* Resumes the trackDataHub. Needs to be synchronized because trackDataHub can
* be accessed by multiple threads.