Refactor current location button for fragments
This commit is contained in:
parent
158e93a238
commit
7d69d47897
3 changed files with 45 additions and 84 deletions
|
@ -1,6 +1,5 @@
|
|||
package net.osmand.plus.routepreparationmenu;
|
||||
|
||||
import android.Manifest;
|
||||
import android.animation.Animator;
|
||||
import android.animation.AnimatorListenerAdapter;
|
||||
import android.content.Context;
|
||||
|
@ -28,7 +27,6 @@ import androidx.annotation.Nullable;
|
|||
import androidx.appcompat.view.ContextThemeWrapper;
|
||||
import androidx.appcompat.widget.AppCompatImageButton;
|
||||
import androidx.appcompat.widget.AppCompatImageView;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
import androidx.fragment.app.FragmentPagerAdapter;
|
||||
|
@ -36,13 +34,11 @@ import androidx.viewpager.widget.ViewPager;
|
|||
|
||||
import net.osmand.AndroidUtils;
|
||||
import net.osmand.GPXUtilities;
|
||||
import net.osmand.Location;
|
||||
import net.osmand.data.LatLon;
|
||||
import net.osmand.data.PointDescription;
|
||||
import net.osmand.plus.GpxSelectionHelper.GpxDisplayItem;
|
||||
import net.osmand.plus.LockableViewPager;
|
||||
import net.osmand.plus.OsmAndFormatter;
|
||||
import net.osmand.plus.OsmAndLocationProvider;
|
||||
import net.osmand.plus.OsmandApplication;
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.activities.MapActivity;
|
||||
|
@ -92,12 +88,9 @@ public class ChooseRouteFragment extends BaseOsmAndFragment implements ContextMe
|
|||
|
||||
@Nullable
|
||||
private View solidToolbarView;
|
||||
private int solidToolbarHeight;
|
||||
@Nullable
|
||||
private View zoomButtonsView;
|
||||
@Nullable
|
||||
private ImageButton myLocButtonView;
|
||||
@Nullable
|
||||
private ViewGroup pagesView;
|
||||
|
||||
private boolean portrait;
|
||||
|
@ -112,6 +105,7 @@ public class ChooseRouteFragment extends BaseOsmAndFragment implements ContextMe
|
|||
|
||||
private MapHudButton mapZoomIn;
|
||||
private MapHudButton mapZoomOut;
|
||||
private MapHudButton myLocationButton;
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
|
@ -144,7 +138,6 @@ public class ChooseRouteFragment extends BaseOsmAndFragment implements ContextMe
|
|||
AndroidUtils.addStatusBarPadding21v(mapActivity, view);
|
||||
View solidToolbarView = view.findViewById(R.id.toolbar_layout);
|
||||
this.solidToolbarView = solidToolbarView;
|
||||
solidToolbarHeight = getResources().getDimensionPixelSize(R.dimen.dashboard_map_toolbar);
|
||||
LockableViewPager viewPager = view.findViewById(R.id.pager);
|
||||
this.viewPager = viewPager;
|
||||
if (!portrait) {
|
||||
|
@ -255,6 +248,7 @@ public class ChooseRouteFragment extends BaseOsmAndFragment implements ContextMe
|
|||
MapControlsLayer mapControlsLayer = mapActivity.getMapLayers().getMapControlsLayer();
|
||||
mapControlsLayer.removeHudButton(mapZoomIn);
|
||||
mapControlsLayer.removeHudButton(mapZoomOut);
|
||||
mapControlsLayer.removeHudButton(myLocationButton);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -372,6 +366,7 @@ public class ChooseRouteFragment extends BaseOsmAndFragment implements ContextMe
|
|||
|
||||
ImageButton zoomInButtonView = view.findViewById(R.id.map_zoom_in_button);
|
||||
ImageButton zoomOutButtonView = view.findViewById(R.id.map_zoom_out_button);
|
||||
ImageButton myLocButtonView = view.findViewById(R.id.map_my_location_button);
|
||||
|
||||
OsmandMapTileView mapTileView = mapActivity.getMapView();
|
||||
View.OnLongClickListener longClickListener = MapControlsLayer.getOnClickMagnifierListener(mapTileView);
|
||||
|
@ -379,58 +374,9 @@ public class ChooseRouteFragment extends BaseOsmAndFragment implements ContextMe
|
|||
|
||||
mapZoomIn = mapControlsLayer.createZoomInButton(zoomInButtonView, longClickListener);
|
||||
mapZoomOut = mapControlsLayer.createZoomOutButton(zoomOutButtonView, longClickListener);
|
||||
myLocationButton = mapControlsLayer.createMyLocationButton(myLocButtonView);
|
||||
|
||||
ImageButton myLocButtonView = view.findViewById(R.id.map_my_location_button);
|
||||
this.myLocButtonView = myLocButtonView;
|
||||
|
||||
myLocButtonView.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
MapActivity mapActivity = getMapActivity();
|
||||
if (mapActivity != null) {
|
||||
if (OsmAndLocationProvider.isLocationPermissionAvailable(mapActivity)) {
|
||||
mapActivity.getMapViewTrackingUtilities().backToLocationImpl();
|
||||
} else {
|
||||
ActivityCompat.requestPermissions(mapActivity,
|
||||
new String[] {Manifest.permission.ACCESS_FINE_LOCATION},
|
||||
OsmAndLocationProvider.REQUEST_LOCATION_PERMISSION);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
updateMyLocation();
|
||||
|
||||
zoomButtonsView.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
private void updateMyLocation() {
|
||||
MapActivity mapActivity = getMapActivity();
|
||||
if (mapActivity == null) {
|
||||
return;
|
||||
}
|
||||
OsmandApplication app = mapActivity.getMyApplication();
|
||||
Location lastKnownLocation = app.getLocationProvider().getLastKnownLocation();
|
||||
boolean enabled = lastKnownLocation != null;
|
||||
boolean tracked = mapActivity.getMapViewTrackingUtilities().isMapLinkedToLocation();
|
||||
|
||||
ImageButton myLocButtonView = this.myLocButtonView;
|
||||
if (myLocButtonView != null) {
|
||||
if (!enabled) {
|
||||
myLocButtonView.setImageDrawable(getIcon(R.drawable.ic_my_location, R.color.icon_color_default_light));
|
||||
AndroidUtils.setBackground(app, myLocButtonView, nightMode, R.drawable.btn_circle, R.drawable.btn_circle_night);
|
||||
myLocButtonView.setContentDescription(mapActivity.getString(R.string.unknown_location));
|
||||
} else if (tracked) {
|
||||
myLocButtonView.setImageDrawable(getIcon(R.drawable.ic_my_location, R.color.color_myloc_distance));
|
||||
AndroidUtils.setBackground(app, myLocButtonView, nightMode, R.drawable.btn_circle, R.drawable.btn_circle_night);
|
||||
} else {
|
||||
myLocButtonView.setImageResource(R.drawable.ic_my_location);
|
||||
AndroidUtils.setBackground(app, myLocButtonView, nightMode, R.drawable.btn_circle_blue, R.drawable.btn_circle_blue);
|
||||
myLocButtonView.setContentDescription(mapActivity.getString(R.string.map_widget_back_to_loc));
|
||||
}
|
||||
if (app.accessibilityEnabled()) {
|
||||
myLocButtonView.setClickable(enabled && !tracked && app.getRoutingHelper().isFollowingMode());
|
||||
}
|
||||
}
|
||||
AndroidUiHelper.updateVisibility(zoomButtonsView, true);
|
||||
}
|
||||
|
||||
private void updateZoomButtonsVisibility(int menuState) {
|
||||
|
|
|
@ -82,6 +82,7 @@ public class TrackAppearanceFragment extends ContextMenuFragment implements Card
|
|||
|
||||
private MapHudButton mapZoomIn;
|
||||
private MapHudButton mapZoomOut;
|
||||
private MapHudButton myLocationButton;
|
||||
|
||||
@Override
|
||||
public int getMainLayoutId() {
|
||||
|
@ -245,6 +246,7 @@ public class TrackAppearanceFragment extends ContextMenuFragment implements Card
|
|||
MapControlsLayer mapControlsLayer = mapActivity.getMapLayers().getMapControlsLayer();
|
||||
mapControlsLayer.removeHudButton(mapZoomIn);
|
||||
mapControlsLayer.removeHudButton(mapZoomOut);
|
||||
mapControlsLayer.removeHudButton(myLocationButton);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -346,6 +348,7 @@ public class TrackAppearanceFragment extends ContextMenuFragment implements Card
|
|||
|
||||
ImageButton zoomInButtonView = view.findViewById(R.id.map_zoom_in_button);
|
||||
ImageButton zoomOutButtonView = view.findViewById(R.id.map_zoom_out_button);
|
||||
ImageButton myLocButtonView = view.findViewById(R.id.map_my_location_button);
|
||||
|
||||
OsmandMapTileView mapTileView = mapActivity.getMapView();
|
||||
View.OnLongClickListener longClickListener = MapControlsLayer.getOnClickMagnifierListener(mapTileView);
|
||||
|
@ -353,6 +356,7 @@ public class TrackAppearanceFragment extends ContextMenuFragment implements Card
|
|||
|
||||
mapZoomIn = mapControlsLayer.createZoomInButton(zoomInButtonView, longClickListener);
|
||||
mapZoomOut = mapControlsLayer.createZoomOutButton(zoomOutButtonView, longClickListener);
|
||||
myLocationButton = mapControlsLayer.createMyLocationButton(myLocButtonView);
|
||||
}
|
||||
|
||||
public void updateZoomButtonsPos(@NonNull ContextMenuFragment fragment, int y, boolean animated) {
|
||||
|
|
|
@ -354,23 +354,8 @@ public class MapControlsLayer extends OsmandMapLayer {
|
|||
|
||||
private void initControls() {
|
||||
View backToLocation = mapActivity.findViewById(R.id.map_my_location_button);
|
||||
backToLocationControl = createHudButton(backToLocation, R.drawable.ic_my_location, BACK_TO_LOC_HUD_ID)
|
||||
.setIconColorId(R.color.map_button_icon_color_light, R.color.map_button_icon_color_dark)
|
||||
.setBg(R.drawable.btn_circle_blue);
|
||||
controls.add(backToLocationControl);
|
||||
backToLocationControl = createMyLocationButton(backToLocation);
|
||||
|
||||
backToLocation.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (OsmAndLocationProvider.isLocationPermissionAvailable(mapActivity)) {
|
||||
mapActivity.getMapViewTrackingUtilities().backToLocationImpl();
|
||||
} else {
|
||||
ActivityCompat.requestPermissions(mapActivity,
|
||||
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
|
||||
OsmAndLocationProvider.REQUEST_LOCATION_PERMISSION);
|
||||
}
|
||||
}
|
||||
});
|
||||
View backToMenuButton = mapActivity.findViewById(R.id.map_menu_button);
|
||||
|
||||
final boolean dash = settings.SHOW_DASHBOARD_ON_MAP_SCREEN.get();
|
||||
|
@ -407,6 +392,28 @@ public class MapControlsLayer extends OsmandMapLayer {
|
|||
});
|
||||
}
|
||||
|
||||
public MapHudButton createMyLocationButton(View backToLocation) {
|
||||
MapHudButton backToLocationButton = createHudButton(backToLocation, R.drawable.ic_my_location, BACK_TO_LOC_HUD_ID)
|
||||
.setIconColorId(R.color.map_button_icon_color_light, R.color.map_button_icon_color_dark)
|
||||
.setBg(R.drawable.btn_circle_blue);
|
||||
|
||||
backToLocation.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (OsmAndLocationProvider.isLocationPermissionAvailable(mapActivity)) {
|
||||
mapActivity.getMapViewTrackingUtilities().backToLocationImpl();
|
||||
} else {
|
||||
ActivityCompat.requestPermissions(mapActivity,
|
||||
new String[] {Manifest.permission.ACCESS_FINE_LOCATION},
|
||||
OsmAndLocationProvider.REQUEST_LOCATION_PERMISSION);
|
||||
}
|
||||
}
|
||||
});
|
||||
controls.add(backToLocationButton);
|
||||
|
||||
return backToLocationButton;
|
||||
}
|
||||
|
||||
public void doRoute(boolean hasTargets) {
|
||||
this.hasTargets = hasTargets;
|
||||
onNavigationClick();
|
||||
|
@ -786,14 +793,12 @@ public class MapControlsLayer extends OsmandMapLayer {
|
|||
return AndroidUiHelper.isOrientationPortrait(mapActivity);
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private void updateControls(@NonNull RotatedTileBox tileBox, DrawSettings drawSettings) {
|
||||
boolean isNight = drawSettings != null && drawSettings.isNightMode();
|
||||
boolean portrait = isPotrait();
|
||||
// int shadw = isNight ? mapActivity.getResources().getColor(R.color.widgettext_shadow_night) :
|
||||
// mapActivity.getResources().getColor(R.color.widgettext_shadow_day);
|
||||
int textColor = isNight ? mapActivity.getResources().getColor(R.color.widgettext_night) :
|
||||
mapActivity.getResources().getColor(R.color.widgettext_day);
|
||||
int textColor = ContextCompat.getColor(mapActivity, isNight ? R.color.widgettext_night : R.color.widgettext_day);
|
||||
// TODOnightMode
|
||||
// updatextColor(textColor, shadw, rulerControl, zoomControls, mapMenuControls);
|
||||
// default buttons
|
||||
|
@ -811,7 +816,7 @@ public class MapControlsLayer extends OsmandMapLayer {
|
|||
boolean showRouteCalculationControls = routePlanningMode ||
|
||||
((app.accessibilityEnabled() || (System.currentTimeMillis() - touchEvent < TIMEOUT_TO_SHOW_BUTTONS)) && routeFollowingMode);
|
||||
boolean routeDialogOpened = mapRouteInfoMenu.isVisible() || (showRouteCalculationControls && mapRouteInfoMenu.needShowMenu());
|
||||
updateMyLocation(rh, routeDialogOpened || contextMenuOpened);
|
||||
updateMyLocationVisibility(backToLocationControl, rh, routeDialogOpened || contextMenuOpened);
|
||||
boolean showButtons = (showRouteCalculationControls || !routeFollowingMode)
|
||||
&& !isInMovingMarkerMode() && !isInGpxDetailsMode() && !isInMeasurementToolMode()
|
||||
&& !isInPlanRouteMode() && !contextMenuOpened && !isInChoosingRoutesMode()
|
||||
|
@ -869,6 +874,9 @@ public class MapControlsLayer extends OsmandMapLayer {
|
|||
}
|
||||
|
||||
for (MapHudButton mc : controls) {
|
||||
if (BACK_TO_LOC_HUD_ID.equals(mc.id)) {
|
||||
updateMyLocation(mc);
|
||||
}
|
||||
mc.update(mapActivity.getMyApplication(), isNight);
|
||||
}
|
||||
}
|
||||
|
@ -908,8 +916,7 @@ public class MapControlsLayer extends OsmandMapLayer {
|
|||
return new CompassDrawable(originalDrawable);
|
||||
}
|
||||
|
||||
private void updateMyLocation(RoutingHelper rh, boolean dialogOpened) {
|
||||
boolean portrait = isPotrait();
|
||||
private void updateMyLocation(MapHudButton backToLocationControl) {
|
||||
Location lastKnownLocation = mapActivity.getMyApplication().getLocationProvider().getLastKnownLocation();
|
||||
boolean enabled = lastKnownLocation != null;
|
||||
boolean tracked = mapActivity.getMapViewTrackingUtilities().isMapLinkedToLocation();
|
||||
|
@ -927,14 +934,18 @@ public class MapControlsLayer extends OsmandMapLayer {
|
|||
backToLocationControl.setBg(R.drawable.btn_circle_blue);
|
||||
backToLocationControl.iv.setContentDescription(mapActivity.getString(R.string.map_widget_back_to_loc));
|
||||
}
|
||||
boolean visible = !(tracked && rh.isFollowingMode());
|
||||
backToLocationControl.updateVisibility(visible && !dialogOpened && !isInPlanRouteMode()
|
||||
&& !isInTrackAppearanceMode() && (!isInChoosingRoutesMode() || !isInWaypointsChoosingMode() || !portrait));
|
||||
if (app.accessibilityEnabled()) {
|
||||
boolean visible = backToLocationControl.iv.getVisibility() == View.VISIBLE;
|
||||
backToLocationControl.iv.setClickable(enabled && visible);
|
||||
}
|
||||
}
|
||||
|
||||
public void updateMyLocationVisibility(MapHudButton backToLocationControl, RoutingHelper rh, boolean dialogOpened) {
|
||||
boolean tracked = mapActivity.getMapViewTrackingUtilities().isMapLinkedToLocation();
|
||||
boolean visible = !(tracked && rh.isFollowingMode());
|
||||
backToLocationControl.updateVisibility(visible && !dialogOpened && !isInPlanRouteMode()
|
||||
&& !isInTrackAppearanceMode() && (!isInChoosingRoutesMode() || !isInWaypointsChoosingMode() || !isPotrait()));
|
||||
}
|
||||
|
||||
public boolean onSingleTap(PointF point, RotatedTileBox tileBox) {
|
||||
return mapRouteInfoMenu.onSingleTap(point, tileBox);
|
||||
|
|
Loading…
Reference in a new issue