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
+1 -16
View File
@@ -17,24 +17,9 @@ limitations under the License.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:icon="@drawable/ic_menu_map_layer"
android:id="@+id/menu_map_layer"
android:orderInCategory="7"
android:showAsAction="ifRoom"
android:title="@string/menu_map_layer">
<menu>
<group android:checkableBehavior="single" >
<item
android:id="@+id/menu_map"
android:title="@string/menu_map"/>
<item
android:id="@+id/menu_satellite"
android:title="@string/menu_satellite"/>
<item
android:id="@+id/menu_satellite_with_streets"
android:title="@string/menu_satellite_with_streets"/>
<item
android:id="@+id/menu_terrain"
android:title="@string/menu_terrain"/>
</group>
</menu>
</item>
</menu>
@@ -29,6 +29,7 @@ import com.google.android.apps.mytracks.fragments.ExportDialogFragment;
import com.google.android.apps.mytracks.fragments.ExportDialogFragment.ExportCaller;
import com.google.android.apps.mytracks.fragments.ExportDialogFragment.ExportType;
import com.google.android.apps.mytracks.fragments.FrequencyDialogFragment;
import com.google.android.apps.mytracks.fragments.MapLayerDialogFragment;
import com.google.android.apps.mytracks.fragments.MyTracksMapFragment;
import com.google.android.apps.mytracks.fragments.PlayMultipleDialogFragment;
import com.google.android.apps.mytracks.fragments.PlayMultipleDialogFragment.PlayMultipleCaller;
@@ -631,4 +632,12 @@ public class TrackDetailActivity extends AbstractSendToGoogleActivity
public void onPlayMultipleDone(long[] trackIds) {
playTracks(trackIds);
}
/**
* Shows the map layer dialog.
*/
public void showMapLayerDialog() {
new MapLayerDialogFragment().show(
getSupportFragmentManager(), MapLayerDialogFragment.MAP_LAYER_DIALOG_TAG);
}
}
@@ -74,6 +74,7 @@ public class TrackDataHub implements DataSourceListener {
private boolean reportSpeed;
private int recordingGpsAccuracy;
private int recordingDistanceInterval;
private int mapType;
// Track points sampling state
private int numLoadedPoints;
@@ -334,6 +335,18 @@ public class TrackDataHub implements DataSourceListener {
}
}
}
if (key == null || key.equals(PreferencesUtils.getKey(context, R.string.map_type_key))) {
mapType = PreferencesUtils.getInt(
context, R.string.map_type_key, PreferencesUtils.MAP_TYPE_DEFAUlT);
if (key != null) {
for (TrackDataListener trackDataListener :
trackDataManager.getListeners(TrackDataType.PREFERENCE)) {
if (trackDataListener.onMapTypeChanged(mapType)) {
loadDataForListener(trackDataListener);
}
}
}
}
}
});
}
@@ -353,6 +366,7 @@ public class TrackDataHub implements DataSourceListener {
trackDataListener.onReportSpeedChanged(reportSpeed);
trackDataListener.onRecordingGpsAccuracy(recordingGpsAccuracy);
trackDataListener.onRecordingDistanceIntervalChanged(recordingDistanceInterval);
trackDataListener.onMapTypeChanged(mapType);
}
notifyTracksTableUpdate(trackDataManager.getListeners(TrackDataType.TRACKS_TABLE));
@@ -381,6 +395,7 @@ public class TrackDataHub implements DataSourceListener {
trackDataListener.onReportSpeedChanged(reportSpeed);
trackDataListener.onRecordingGpsAccuracy(recordingGpsAccuracy);
trackDataListener.onRecordingDistanceIntervalChanged(recordingDistanceInterval);
trackDataListener.onMapTypeChanged(mapType);
}
if (trackDataTypes.contains(TrackDataType.TRACKS_TABLE)) {
@@ -123,4 +123,12 @@ public interface TrackDataListener {
* @return true to reload all the data, false otherwise.
*/
public boolean onRecordingDistanceIntervalChanged(int recordingDistanceInterval);
/**
* Called when the map type preference value is changed.
*
* @param mapType the new value
* @return true to reload all the data, false otherwise.
*/
public boolean onMapTypeChanged(int mapType);
}
@@ -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.