Refactor zoom buttons for fragments

This commit is contained in:
Vitaliy 2020-07-27 18:53:27 +03:00
parent 36b3fd4b56
commit 158e93a238
4 changed files with 118 additions and 200 deletions

View file

@ -7,34 +7,6 @@
android:layout_gravity="bottom" android:layout_gravity="bottom"
android:orientation="vertical"> android:orientation="vertical">
<!--<net.osmand.plus.quickaction.QuickActionsWidget
android:id="@+id/quick_action_widget"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:visibility="visible"
android:visibility="gone"/>
<FrameLayout
android:id="@+id/map_quick_actions_button_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:padding="8dp"
android:animateLayoutChanges="true">
<ImageButton
android:id="@+id/map_quick_actions_button"
android:layout_width="@dimen/map_button_size"
android:layout_height="@dimen/map_button_size"
android:background="@drawable/btn_circle_blue"
android:contentDescription="@string/zoomIn"
android:layout_gravity="bottom|right"
android:visibility="gone"
tools:visibility="visible"
tools:src="@drawable/ic_action_remove_dark"/>
</FrameLayout>-->
<FrameLayout <FrameLayout
android:id="@+id/bottom_controls_container" android:id="@+id/bottom_controls_container"
android:layout_width="match_parent" android:layout_width="match_parent"

View file

@ -44,7 +44,6 @@ import net.osmand.plus.LockableViewPager;
import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmAndLocationProvider; import net.osmand.plus.OsmAndLocationProvider;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.MapActivityActions; import net.osmand.plus.activities.MapActivityActions;
@ -60,6 +59,9 @@ import net.osmand.plus.routepreparationmenu.cards.PublicTransportCard;
import net.osmand.plus.routing.RouteDirectionInfo; import net.osmand.plus.routing.RouteDirectionInfo;
import net.osmand.plus.routing.RoutingHelper; import net.osmand.plus.routing.RoutingHelper;
import net.osmand.plus.routing.TransportRoutingHelper; import net.osmand.plus.routing.TransportRoutingHelper;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.views.MapControlsLayer;
import net.osmand.plus.views.MapControlsLayer.MapHudButton;
import net.osmand.plus.views.OsmandMapTileView; import net.osmand.plus.views.OsmandMapTileView;
import net.osmand.router.TransportRoutePlanner.TransportRouteResult; import net.osmand.router.TransportRoutePlanner.TransportRouteResult;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
@ -108,6 +110,9 @@ public class ChooseRouteFragment extends BaseOsmAndFragment implements ContextMe
private boolean publicTransportMode; private boolean publicTransportMode;
private boolean needAdjustMap; private boolean needAdjustMap;
private MapHudButton mapZoomIn;
private MapHudButton mapZoomOut;
@Nullable @Nullable
@Override @Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
@ -241,6 +246,18 @@ public class ChooseRouteFragment extends BaseOsmAndFragment implements ContextMe
} }
} }
@Override
public void onDestroyView() {
super.onDestroyView();
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
MapControlsLayer mapControlsLayer = mapActivity.getMapLayers().getMapControlsLayer();
mapControlsLayer.removeHudButton(mapZoomIn);
mapControlsLayer.removeHudButton(mapZoomOut);
}
}
@Override @Override
public int getStatusBarColorId() { public int getStatusBarColorId() {
View view = getView(); View view = getView();
@ -345,30 +362,26 @@ public class ChooseRouteFragment extends BaseOsmAndFragment implements ContextMe
} }
private void buildZoomButtons(@NonNull View view) { private void buildZoomButtons(@NonNull View view) {
OsmandApplication app = requireMyApplication(); MapActivity mapActivity = getMapActivity();
if (mapActivity == null) {
return;
}
// Zoom buttons // Zoom buttons
View zoomButtonsView = view.findViewById(R.id.map_hud_controls); View zoomButtonsView = view.findViewById(R.id.map_hud_controls);
this.zoomButtonsView = zoomButtonsView; this.zoomButtonsView = zoomButtonsView;
ImageButton zoomInButtonView = (ImageButton) view.findViewById(R.id.map_zoom_in_button);
ImageButton zoomOutButtonView = (ImageButton) view.findViewById(R.id.map_zoom_out_button); ImageButton zoomInButtonView = view.findViewById(R.id.map_zoom_in_button);
ImageButton myLocButtonView = (ImageButton) view.findViewById(R.id.map_my_location_button); ImageButton zoomOutButtonView = view.findViewById(R.id.map_zoom_out_button);
OsmandMapTileView mapTileView = mapActivity.getMapView();
View.OnLongClickListener longClickListener = MapControlsLayer.getOnClickMagnifierListener(mapTileView);
MapControlsLayer mapControlsLayer = mapActivity.getMapLayers().getMapControlsLayer();
mapZoomIn = mapControlsLayer.createZoomInButton(zoomInButtonView, longClickListener);
mapZoomOut = mapControlsLayer.createZoomOutButton(zoomOutButtonView, longClickListener);
ImageButton myLocButtonView = view.findViewById(R.id.map_my_location_button);
this.myLocButtonView = myLocButtonView; this.myLocButtonView = myLocButtonView;
AndroidUtils.updateImageButton(app, zoomInButtonView, R.drawable.ic_zoom_in, R.drawable.ic_zoom_in,
R.drawable.btn_circle_trans, R.drawable.btn_circle_night, nightMode);
AndroidUtils.updateImageButton(app, zoomOutButtonView, R.drawable.ic_zoom_out, R.drawable.ic_zoom_out,
R.drawable.btn_circle_trans, R.drawable.btn_circle_night, nightMode);
zoomInButtonView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
doZoomIn();
}
});
zoomOutButtonView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
doZoomOut();
}
});
myLocButtonView.setOnClickListener(new OnClickListener() { myLocButtonView.setOnClickListener(new OnClickListener() {
@Override @Override
@ -379,7 +392,7 @@ public class ChooseRouteFragment extends BaseOsmAndFragment implements ContextMe
mapActivity.getMapViewTrackingUtilities().backToLocationImpl(); mapActivity.getMapViewTrackingUtilities().backToLocationImpl();
} else { } else {
ActivityCompat.requestPermissions(mapActivity, ActivityCompat.requestPermissions(mapActivity,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, new String[] {Manifest.permission.ACCESS_FINE_LOCATION},
OsmAndLocationProvider.REQUEST_LOCATION_PERMISSION); OsmAndLocationProvider.REQUEST_LOCATION_PERMISSION);
} }
} }
@ -420,25 +433,6 @@ public class ChooseRouteFragment extends BaseOsmAndFragment implements ContextMe
} }
} }
public void doZoomIn() {
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
OsmandMapTileView map = mapActivity.getMapView();
if (map.isZooming() && map.hasCustomMapRatio()) {
mapActivity.changeZoom(2, System.currentTimeMillis());
} else {
mapActivity.changeZoom(1, System.currentTimeMillis());
}
}
}
public void doZoomOut() {
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
mapActivity.changeZoom(-1, System.currentTimeMillis());
}
}
private void updateZoomButtonsVisibility(int menuState) { private void updateZoomButtonsVisibility(int menuState) {
View zoomButtonsView = this.zoomButtonsView; View zoomButtonsView = this.zoomButtonsView;
if (zoomButtonsView != null) { if (zoomButtonsView != null) {

View file

@ -1,6 +1,5 @@
package net.osmand.plus.track; package net.osmand.plus.track;
import android.Manifest;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.AsyncTask; import android.os.AsyncTask;
@ -18,20 +17,17 @@ import android.widget.LinearLayout;
import androidx.annotation.ColorInt; import androidx.annotation.ColorInt;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.GPXUtilities.GPXFile; import net.osmand.GPXUtilities.GPXFile;
import net.osmand.Location;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.data.QuadRect; import net.osmand.data.QuadRect;
import net.osmand.data.RotatedTileBox; import net.osmand.data.RotatedTileBox;
import net.osmand.plus.GPXDatabase.GpxDataItem; import net.osmand.plus.GPXDatabase.GpxDataItem;
import net.osmand.plus.GpxSelectionHelper.GpxDisplayGroup; import net.osmand.plus.GpxSelectionHelper.GpxDisplayGroup;
import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile;
import net.osmand.plus.OsmAndLocationProvider;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.UiUtilities; import net.osmand.plus.UiUtilities;
@ -45,6 +41,8 @@ import net.osmand.plus.routepreparationmenu.cards.BaseCard;
import net.osmand.plus.routepreparationmenu.cards.BaseCard.CardListener; import net.osmand.plus.routepreparationmenu.cards.BaseCard.CardListener;
import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.track.SplitTrackAsyncTask.SplitTrackListener; import net.osmand.plus.track.SplitTrackAsyncTask.SplitTrackListener;
import net.osmand.plus.views.MapControlsLayer;
import net.osmand.plus.views.MapControlsLayer.MapHudButton;
import net.osmand.plus.views.OsmandMapTileView; import net.osmand.plus.views.OsmandMapTileView;
import net.osmand.render.RenderingRulesStorage; import net.osmand.render.RenderingRulesStorage;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
@ -81,7 +79,9 @@ public class TrackAppearanceFragment extends ContextMenuFragment implements Card
private ImageView appearanceIcon; private ImageView appearanceIcon;
private View zoomButtonsView; private View zoomButtonsView;
private ImageButton myLocButtonView;
private MapHudButton mapZoomIn;
private MapHudButton mapZoomOut;
@Override @Override
public int getMainLayoutId() { public int getMainLayoutId() {
@ -239,6 +239,13 @@ public class TrackAppearanceFragment extends ContextMenuFragment implements Card
public void onDestroyView() { public void onDestroyView() {
super.onDestroyView(); super.onDestroyView();
exitTrackAppearanceMode(); exitTrackAppearanceMode();
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
MapControlsLayer mapControlsLayer = mapActivity.getMapLayers().getMapControlsLayer();
mapControlsLayer.removeHudButton(mapZoomIn);
mapControlsLayer.removeHudButton(mapZoomOut);
}
} }
private void enterTrackAppearanceMode() { private void enterTrackAppearanceMode() {
@ -334,76 +341,18 @@ public class TrackAppearanceFragment extends ContextMenuFragment implements Card
} }
private void buildZoomButtons(@NonNull View view) { private void buildZoomButtons(@NonNull View view) {
OsmandApplication app = requireMyApplication(); MapActivity mapActivity = requireMapActivity();
this.zoomButtonsView = view.findViewById(R.id.map_hud_controls); zoomButtonsView = view.findViewById(R.id.map_hud_controls);
ImageButton zoomInButtonView = (ImageButton) view.findViewById(R.id.map_zoom_in_button);
ImageButton zoomOutButtonView = (ImageButton) view.findViewById(R.id.map_zoom_out_button);
AndroidUtils.updateImageButton(app, zoomInButtonView, R.drawable.ic_zoom_in, R.drawable.ic_zoom_in,
R.drawable.btn_circle_trans, R.drawable.btn_circle_night, isNightMode());
AndroidUtils.updateImageButton(app, zoomOutButtonView, R.drawable.ic_zoom_out, R.drawable.ic_zoom_out,
R.drawable.btn_circle_trans, R.drawable.btn_circle_night, isNightMode());
zoomInButtonView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
doZoomIn();
}
});
zoomOutButtonView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
doZoomOut();
}
});
myLocButtonView = (ImageButton) view.findViewById(R.id.map_my_location_button); ImageButton zoomInButtonView = view.findViewById(R.id.map_zoom_in_button);
myLocButtonView.setOnClickListener(new View.OnClickListener() { ImageButton zoomOutButtonView = view.findViewById(R.id.map_zoom_out_button);
@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); OsmandMapTileView mapTileView = mapActivity.getMapView();
} View.OnLongClickListener longClickListener = MapControlsLayer.getOnClickMagnifierListener(mapTileView);
MapControlsLayer mapControlsLayer = mapActivity.getMapLayers().getMapControlsLayer();
private void updateMyLocation() { mapZoomIn = mapControlsLayer.createZoomInButton(zoomInButtonView, longClickListener);
MapActivity mapActivity = getMapActivity(); mapZoomOut = mapControlsLayer.createZoomOutButton(zoomOutButtonView, longClickListener);
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, isNightMode(), 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, isNightMode(), R.drawable.btn_circle, R.drawable.btn_circle_night);
} else {
myLocButtonView.setImageResource(R.drawable.ic_my_location);
AndroidUtils.setBackground(app, myLocButtonView, isNightMode(), 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());
}
}
} }
public void updateZoomButtonsPos(@NonNull ContextMenuFragment fragment, int y, boolean animated) { public void updateZoomButtonsPos(@NonNull ContextMenuFragment fragment, int y, boolean animated) {
@ -423,25 +372,6 @@ public class TrackAppearanceFragment extends ContextMenuFragment implements Card
return zoomButtonsView != null ? zoomButtonsView.getHeight() : 0; return zoomButtonsView != null ? zoomButtonsView.getHeight() : 0;
} }
public void doZoomIn() {
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
OsmandMapTileView map = mapActivity.getMapView();
if (map.isZooming() && map.hasCustomMapRatio()) {
mapActivity.changeZoom(2, System.currentTimeMillis());
} else {
mapActivity.changeZoom(1, System.currentTimeMillis());
}
}
}
public void doZoomOut() {
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
mapActivity.changeZoom(-1, System.currentTimeMillis());
}
}
private void updateZoomButtonsVisibility(int menuState) { private void updateZoomButtonsVisibility(int menuState) {
View zoomButtonsView = this.zoomButtonsView; View zoomButtonsView = this.zoomButtonsView;
if (zoomButtonsView != null) { if (zoomButtonsView != null) {

View file

@ -587,29 +587,21 @@ public class MapControlsLayer extends OsmandMapLayer {
} }
private void initZooms() { private void initZooms() {
final OsmandMapTileView view = mapActivity.getMapView(); OsmandMapTileView view = mapActivity.getMapView();
View.OnLongClickListener longClickListener = MapControlsLayer.getOnClickMagnifierListener(view);
View zoomInButton = mapActivity.findViewById(R.id.map_zoom_in_button); View zoomInButton = mapActivity.findViewById(R.id.map_zoom_in_button);
mapZoomIn = createHudButton(zoomInButton, R.drawable.ic_zoom_in, ZOOM_IN_HUD_ID).setRoundTransparent();
controls.add(mapZoomIn);
zoomInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mapActivity.getContextMenu().zoomInPressed()) {
return;
}
if (view.isZooming()) {
mapActivity.changeZoom(2, System.currentTimeMillis());
} else {
mapActivity.changeZoom(1, System.currentTimeMillis());
}
lastZoom = System.currentTimeMillis();
}
});
final View.OnLongClickListener listener = MapControlsLayer.getOnClickMagnifierListener(view);
zoomInButton.setOnLongClickListener(listener);
View zoomOutButton = mapActivity.findViewById(R.id.map_zoom_out_button); View zoomOutButton = mapActivity.findViewById(R.id.map_zoom_out_button);
mapZoomOut = createHudButton(zoomOutButton, R.drawable.ic_zoom_out, ZOOM_OUT_HUD_ID).setRoundTransparent();
controls.add(mapZoomOut); mapZoomIn = createZoomInButton(zoomInButton, longClickListener);
mapZoomOut = createZoomOutButton(zoomOutButton, longClickListener);
}
public MapHudButton createZoomOutButton(View zoomOutButton, View.OnLongClickListener longClickListener) {
MapHudButton mapZoomOutButton = createHudButton(zoomOutButton, R.drawable.ic_zoom_out, ZOOM_OUT_HUD_ID);
mapZoomOutButton.setRoundTransparent();
zoomOutButton.setOnLongClickListener(longClickListener);
zoomOutButton.setOnClickListener(new View.OnClickListener() { zoomOutButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
@ -620,7 +612,37 @@ public class MapControlsLayer extends OsmandMapLayer {
lastZoom = System.currentTimeMillis(); lastZoom = System.currentTimeMillis();
} }
}); });
zoomOutButton.setOnLongClickListener(listener); controls.add(mapZoomOutButton);
return mapZoomOutButton;
}
public MapHudButton createZoomInButton(View zoomInButton, View.OnLongClickListener longClickListener) {
MapHudButton mapZoomInButton = createHudButton(zoomInButton, R.drawable.ic_zoom_in, ZOOM_IN_HUD_ID);
mapZoomInButton.setRoundTransparent();
zoomInButton.setOnLongClickListener(longClickListener);
zoomInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mapActivity.getContextMenu().zoomInPressed()) {
return;
}
if (mapActivity.getMapView().isZooming()) {
mapActivity.changeZoom(2, System.currentTimeMillis());
} else {
mapActivity.changeZoom(1, System.currentTimeMillis());
}
lastZoom = System.currentTimeMillis();
}
});
controls.add(mapZoomInButton);
return mapZoomInButton;
}
public void removeHudButton(MapHudButton hudButton) {
controls.remove(hudButton);
} }
public void showMapControlsIfHidden() { public void showMapControlsIfHidden() {
@ -984,31 +1006,31 @@ public class MapControlsLayer extends OsmandMapLayer {
transparencySetting = null; transparencySetting = null;
} }
private class MapHudButton { public class MapHudButton {
View iv;
int bgDark;
int bgLight;
int resId;
int resLightId;
int resDarkId;
int resClrLight = R.color.map_button_icon_color_light;
int resClrDark = R.color.map_button_icon_color_dark;
String id;
boolean flipIconForRtl;
boolean nightMode = false; private View iv;
boolean f = true; private int bgDark;
boolean compass; private int bgLight;
boolean compassOutside; private int resId;
boolean forceHideCompass; private int resLightId;
ViewPropertyAnimatorCompat hideAnimator; private int resDarkId;
private int resClrLight = R.color.map_button_icon_color_light;
private int resClrDark = R.color.map_button_icon_color_dark;
private String id;
private boolean flipIconForRtl;
private boolean nightMode = false;
private boolean f = true;
private boolean compass;
private boolean compassOutside;
private boolean forceHideCompass;
private ViewPropertyAnimatorCompat hideAnimator;
public MapHudButton setRoundTransparent() { public MapHudButton setRoundTransparent() {
setBg(R.drawable.btn_circle_trans, R.drawable.btn_circle_night); setBg(R.drawable.btn_circle_trans, R.drawable.btn_circle_night);
return this; return this;
} }
public MapHudButton setBg(int dayBg, int nightBg) { public MapHudButton setBg(int dayBg, int nightBg) {
if (bgDark == nightBg && dayBg == bgLight) { if (bgDark == nightBg && dayBg == bgLight) {
return this; return this;