Fix handling context menu swipes

This commit is contained in:
Alexey Kulish 2018-02-03 19:12:55 +03:00
parent 4f776f3268
commit e1748a0c2e

View file

@ -71,7 +71,6 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo
public static final float ZOOM_PADDING_TOP_DP = 4f; public static final float ZOOM_PADDING_TOP_DP = 4f;
public static final float MARKER_PADDING_DP = 20f; public static final float MARKER_PADDING_DP = 20f;
public static final float MARKER_PADDING_X_DP = 50f; public static final float MARKER_PADDING_X_DP = 50f;
public static final float SKIP_HALF_SCREEN_STATE_KOEF = .21f;
public static final int ZOOM_IN_STANDARD = 17; public static final int ZOOM_IN_STANDARD = 17;
public static final int CURRENT_Y_UNDEFINED = Integer.MAX_VALUE; public static final int CURRENT_Y_UNDEFINED = Integer.MAX_VALUE;
@ -128,8 +127,6 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo
private boolean wasDrawerDisabled; private boolean wasDrawerDisabled;
private boolean zoomIn; private boolean zoomIn;
private float skipScreenStateLimit;
private int screenOrientation; private int screenOrientation;
private boolean created; private boolean created;
@ -353,8 +350,8 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo
hasMoved = true; hasMoved = true;
float y = event.getY(); float y = event.getY();
float newY = getViewY() + (y - dy); float newY = getViewY() + (y - dy);
if (menu.isLandscapeLayout() && newY > 0) { if (menu.isLandscapeLayout() && newY > topScreenPosY) {
newY = 0; newY = topScreenPosY;
} }
setViewY((int) newY, false, false); setViewY((int) newY, false, false);
@ -391,8 +388,8 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo
scroller.abortAnimation(); scroller.abortAnimation();
scroller.fling(0, currentY, 0, initialVelocity, 0, 0, scroller.fling(0, currentY, 0, initialVelocity, 0, 0,
viewHeight - menuFullHeightMax, Math.min(viewHeight - menuFullHeightMax, getFullScreenTopPosY()),
minHalfY, screenHeight,
0, 0); 0, 0);
currentY = scroller.getFinalY(); currentY = scroller.getFinalY();
scroller.abortAnimation(); scroller.abortAnimation();
@ -404,8 +401,7 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo
slidingDown = false; slidingDown = false;
} }
boolean skipScreenState = Math.abs(getViewY() - dyMain) > skipScreenStateLimit; changeMenuState(currentY, slidingUp, slidingDown);
changeMenuState(currentY, skipScreenState, slidingUp, slidingDown);
} }
recycleVelocityTracker(); recycleVelocityTracker();
break; break;
@ -740,59 +736,90 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo
private void processScreenHeight(ViewParent parent) { private void processScreenHeight(ViewParent parent) {
View container = (View) parent; View container = (View) parent;
screenHeight = container.getHeight() + AndroidUtils.getStatusBarHeight(getActivity()); screenHeight = container.getHeight() + AndroidUtils.getStatusBarHeight(getActivity());
skipScreenStateLimit = screenHeight * SKIP_HALF_SCREEN_STATE_KOEF;
viewHeight = screenHeight - AndroidUtils.getStatusBarHeight(getMapActivity()); viewHeight = screenHeight - AndroidUtils.getStatusBarHeight(getMapActivity());
} }
public void openMenuFullScreen() { public void openMenuFullScreen() {
changeMenuState(getViewY(), true, true, false); changeMenuState(getMenuStatePosY(MenuState.FULL_SCREEN), false, false);
} }
public void openMenuHeaderOnly() { public void openMenuHeaderOnly() {
changeMenuState(getViewY(), true, false, true); if (!menu.isLandscapeLayout()) {
changeMenuState(getMenuStatePosY(MenuState.HEADER_ONLY), false, false);
}
} }
public void openMenuHalfScreen() { public void openMenuHalfScreen() {
int oldMenuState = menu.getCurrentMenuState(); if (!menu.isLandscapeLayout()) {
if (oldMenuState == MenuState.HEADER_ONLY) { changeMenuState(getMenuStatePosY(MenuState.HALF_SCREEN), false, false);
changeMenuState(getViewY(), false, true, false);
} else if (oldMenuState == MenuState.FULL_SCREEN && !menu.isLandscapeLayout()) {
changeMenuState(getViewY(), false, false, true);
} }
} }
private void changeMenuState(int currentY, boolean skipScreenState, private void changeMenuState(int currentY, boolean slidingUp, boolean slidingDown) {
boolean slidingUp, boolean slidingDown) {
boolean needCloseMenu = false; boolean needCloseMenu = false;
int oldMenuState = menu.getCurrentMenuState(); int currentMenuState = menu.getCurrentMenuState();
if (!menu.isLandscapeLayout()) { if (!menu.isLandscapeLayout()) {
if (slidingDown && oldMenuState == MenuState.FULL_SCREEN && getViewY() < getFullScreenTopPosY()) { int headerDist = Math.abs(currentY - getMenuStatePosY(MenuState.HEADER_ONLY));
int halfDist = Math.abs(currentY - getMenuStatePosY(MenuState.HALF_SCREEN));
int fullDist = Math.abs(currentY - getMenuStatePosY(MenuState.FULL_SCREEN));
int newState;
if (headerDist < halfDist && headerDist < fullDist) {
newState = MenuState.HEADER_ONLY;
} else if (halfDist < headerDist && halfDist < fullDist) {
newState = MenuState.HALF_SCREEN;
} else {
newState = MenuState.FULL_SCREEN;
}
if (slidingDown && currentMenuState == MenuState.FULL_SCREEN && getViewY() < getFullScreenTopPosY()) {
slidingDown = false; slidingDown = false;
newState = MenuState.FULL_SCREEN;
} }
if (menuBottomViewHeight > 0 && slidingUp) { if (menuBottomViewHeight > 0 && slidingUp) {
menu.slideUp(); while (menu.getCurrentMenuState() != newState) {
if (skipScreenState) { if (!menu.slideUp()) {
menu.slideUp(); break;
}
} }
} else if (slidingDown) { } else if (slidingDown) {
needCloseMenu = !menu.slideDown(); if (currentMenuState == MenuState.HEADER_ONLY) {
if (!needCloseMenu && skipScreenState) { needCloseMenu = true;
menu.slideDown(); } else {
while (menu.getCurrentMenuState() != newState) {
if (!menu.slideDown()) {
needCloseMenu = true;
break;
}
}
}
} else {
if (currentMenuState < newState) {
while (menu.getCurrentMenuState() != newState) {
if (!menu.slideUp()) {
break;
}
}
} else {
while (menu.getCurrentMenuState() != newState) {
if (!menu.slideDown()) {
break;
}
}
} }
} }
} }
int newMenuState = menu.getCurrentMenuState(); int newMenuState = menu.getCurrentMenuState();
boolean needMapAdjust = oldMenuState != newMenuState && newMenuState != MenuState.FULL_SCREEN; boolean needMapAdjust = currentMenuState != newMenuState && newMenuState != MenuState.FULL_SCREEN;
if (newMenuState != oldMenuState) { if (newMenuState != currentMenuState) {
restoreCustomMapRatio(); restoreCustomMapRatio();
menu.updateControlsVisibility(true); menu.updateControlsVisibility(true);
doBeforeMenuStateChange(oldMenuState, newMenuState); doBeforeMenuStateChange(currentMenuState, newMenuState);
toggleDetailsHideButton(); toggleDetailsHideButton();
} }
applyPosY(currentY, needCloseMenu, needMapAdjust, oldMenuState, newMenuState, 0); applyPosY(currentY, needCloseMenu, needMapAdjust, currentMenuState, newMenuState, 0);
} }
private void restoreCustomMapRatio() { private void restoreCustomMapRatio() {
@ -1466,6 +1493,22 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo
return -menuTitleHeight + menuButtonsHeight + bottomToolbarPosY; return -menuTitleHeight + menuButtonsHeight + bottomToolbarPosY;
} }
private int getMenuStatePosY(int menuState) {
if (menu.isLandscapeLayout()) {
return topScreenPosY;
}
switch (menuState) {
case MenuState.HEADER_ONLY:
return getHeaderOnlyTopY();
case MenuState.HALF_SCREEN:
return minHalfY;
case MenuState.FULL_SCREEN:
return getFullScreenTopPosY();
default:
return 0;
}
}
private int getPosY() { private int getPosY() {
return getPosY(CURRENT_Y_UNDEFINED, false); return getPosY(CURRENT_Y_UNDEFINED, false);
} }
@ -1491,20 +1534,15 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo
int posY = 0; int posY = 0;
switch (destinationState) { switch (destinationState) {
case MenuState.HEADER_ONLY: case MenuState.HEADER_ONLY:
posY = getHeaderOnlyTopY(); posY = getMenuStatePosY(MenuState.HEADER_ONLY);
break; break;
case MenuState.HALF_SCREEN: case MenuState.HALF_SCREEN:
posY = minHalfY; posY = getMenuStatePosY(MenuState.HALF_SCREEN);
break; break;
case MenuState.FULL_SCREEN: case MenuState.FULL_SCREEN:
if (currentY != CURRENT_Y_UNDEFINED) { if (currentY != CURRENT_Y_UNDEFINED) {
int maxPosY = viewHeight - menuFullHeightMax; int maxPosY = viewHeight - menuFullHeightMax;
int minPosY; int minPosY = getMenuStatePosY(MenuState.FULL_SCREEN);
if (menu.isLandscapeLayout()) {
minPosY = topScreenPosY;
} else {
minPosY = getFullScreenTopPosY();
}
if (maxPosY > minPosY) { if (maxPosY > minPosY) {
maxPosY = minPosY; maxPosY = minPosY;
} }
@ -1516,11 +1554,7 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo
posY = currentY; posY = currentY;
} }
} else { } else {
if (menu.isLandscapeLayout()) { posY = getMenuStatePosY(MenuState.FULL_SCREEN);
posY = topScreenPosY;
} else {
posY = getFullScreenTopPosY();
}
} }
break; break;
default: default: