Context menu - added landscape mode

This commit is contained in:
Alexey Kulish 2015-09-18 20:42:43 +03:00
parent ad04041a56
commit 6d77355e02
6 changed files with 108 additions and 51 deletions

View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-100%p" android:toXDelta="0"
android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>

View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-100%p"
android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="1.0" android:toAlpha="0.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>

View file

@ -1,10 +1,9 @@
package net.osmand.plus.mapcontextmenu; package net.osmand.plus.mapcontextmenu;
import android.app.Activity;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.util.Log;
import android.widget.Toast;
import net.osmand.binary.RouteDataObject; import net.osmand.binary.RouteDataObject;
import net.osmand.data.Amenity; import net.osmand.data.Amenity;
@ -16,7 +15,6 @@ import net.osmand.plus.ContextMenuAdapter;
import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings; import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.mapcontextmenu.sections.AmenityInfoMenuController; import net.osmand.plus.mapcontextmenu.sections.AmenityInfoMenuController;
import net.osmand.plus.mapcontextmenu.sections.MenuController; import net.osmand.plus.mapcontextmenu.sections.MenuController;
@ -184,11 +182,11 @@ public class MapContextMenu {
}.execute(loc); }.execute(loc);
} }
public MenuController getMenuController() { public MenuController getMenuController(Activity activity) {
if (object != null) { if (object != null) {
if (object instanceof Amenity) { if (object instanceof Amenity) {
return new AmenityInfoMenuController(app, (Amenity)object); return new AmenityInfoMenuController(app, activity, (Amenity)object);
} }
} }

View file

@ -21,6 +21,7 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.ViewTreeObserver; import android.view.ViewTreeObserver;
import android.view.animation.DecelerateInterpolator; import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
@ -147,6 +148,12 @@ public class MapContextMenuFragment extends Fragment {
getCtxMenu().restoreMenuState(savedInstanceState); getCtxMenu().restoreMenuState(savedInstanceState);
view = inflater.inflate(R.layout.map_context_menu_fragment, container, false); view = inflater.inflate(R.layout.map_context_menu_fragment, container, false);
mainView = view.findViewById(R.id.context_menu_main);
menuController = getCtxMenu().getMenuController(getActivity());
if (menuController != null && menuController.isLandscapeLayout()) {
mainView.setLayoutParams(new FrameLayout.LayoutParams(dpToPx(menuController.getLandscapeWidthDp()), ViewGroup.LayoutParams.MATCH_PARENT));
}
ViewTreeObserver vto = view.getViewTreeObserver(); ViewTreeObserver vto = view.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@ -196,8 +203,6 @@ public class MapContextMenuFragment extends Fragment {
} }
}); });
mainView = view.findViewById(R.id.context_menu_main);
final View.OnTouchListener slideTouchListener = new View.OnTouchListener() { final View.OnTouchListener slideTouchListener = new View.OnTouchListener() {
private float dy; private float dy;
private float dyMain; private float dyMain;
@ -205,41 +210,28 @@ public class MapContextMenuFragment extends Fragment {
private boolean slidingUp; private boolean slidingUp;
private boolean slidingDown; private boolean slidingDown;
private float velocityX;
private float velocityY; private float velocityY;
private float maxVelocityY; private float maxVelocityY;
private float startX;
private float startY;
private long lastTouchDown;
private final int CLICK_ACTION_THRESHHOLD = 200;
private boolean isClick(float endX, float endY) {
float differenceX = Math.abs(startX - endX);
float differenceY = Math.abs(startY - endY);
if (differenceX > 1 ||
differenceY > 1 ||
Math.abs(velocityX) > 10 ||
Math.abs(velocityY) > 10 ||
System.currentTimeMillis() - lastTouchDown > CLICK_ACTION_THRESHHOLD) {
return false;
}
return true;
}
@Override @Override
public boolean onTouch(View v, MotionEvent event) { public boolean onTouch(View v, MotionEvent event) {
if (singleTapDetector.onTouchEvent(event)) {
OsmandMapTileView mapView = getMapActivity().getMapView();
mapView.getAnimatedDraggingThread().startMoving(getCtxMenu().getPointDescription().getLat(), getCtxMenu().getPointDescription().getLon(),
mapView.getZoom(), true);
return true;
}
if (menuController != null && menuController.isLandscapeLayout()) {
return true;
}
switch (event.getAction()) { switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_DOWN:
startX = event.getX();
startY = event.getY();
lastTouchDown = System.currentTimeMillis();
dy = event.getY(); dy = event.getY();
dyMain = getViewY(); dyMain = getViewY();
velocity = VelocityTracker.obtain(); velocity = VelocityTracker.obtain();
velocityX = 0;
velocityY = 0; velocityY = 0;
maxVelocityY = 0; maxVelocityY = 0;
velocity.addMovement(event); velocity.addMovement(event);
@ -260,7 +252,6 @@ public class MapContextMenuFragment extends Fragment {
velocity.addMovement(event); velocity.addMovement(event);
velocity.computeCurrentVelocity(1000); velocity.computeCurrentVelocity(1000);
velocityX = Math.abs(velocity.getXVelocity());
velocityY = Math.abs(velocity.getYVelocity()); velocityY = Math.abs(velocity.getYVelocity());
if (velocityY > maxVelocityY) if (velocityY > maxVelocityY)
maxVelocityY = velocityY; maxVelocityY = velocityY;
@ -269,9 +260,6 @@ public class MapContextMenuFragment extends Fragment {
case MotionEvent.ACTION_UP: case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_CANCEL:
float endX = event.getX();
float endY = event.getY();
int currentY = getViewY(); int currentY = getViewY();
slidingUp = Math.abs(maxVelocityY) > 500 && (currentY - dyMain) < -50; slidingUp = Math.abs(maxVelocityY) > 500 && (currentY - dyMain) < -50;
@ -316,14 +304,6 @@ public class MapContextMenuFragment extends Fragment {
updateMainViewLayout(posY); updateMainViewLayout(posY);
} }
} }
// OnClick event
if (isClick(endX, endY)) {
OsmandMapTileView mapView = getMapActivity().getMapView();
mapView.getAnimatedDraggingThread().startMoving(getCtxMenu().getPointDescription().getLat(), getCtxMenu().getPointDescription().getLon(),
mapView.getZoom(), true);
}
break; break;
} }
@ -417,7 +397,6 @@ public class MapContextMenuFragment extends Fragment {
}); });
// Menu controller // Menu controller
menuController = getCtxMenu().getMenuController();
bottomView = view.findViewById(R.id.context_menu_bottom_view); bottomView = view.findViewById(R.id.context_menu_bottom_view);
if (menuController != null) { if (menuController != null) {
bottomView.setOnTouchListener(new View.OnTouchListener() { bottomView.setOnTouchListener(new View.OnTouchListener() {
@ -529,9 +508,19 @@ public class MapContextMenuFragment extends Fragment {
} }
public static void showInstance(final MapActivity mapActivity) { public static void showInstance(final MapActivity mapActivity) {
int slideInAnim = R.anim.slide_in_bottom;
int slideOutAnim = R.anim.slide_out_bottom;
MenuController menuController = mapActivity.getContextMenu().getMenuController(mapActivity);
if (menuController != null) {
slideInAnim = menuController.getSlideInAnimation();
slideOutAnim = menuController.getSlideOutAnimation();
}
MapContextMenuFragment fragment = new MapContextMenuFragment(); MapContextMenuFragment fragment = new MapContextMenuFragment();
mapActivity.getSupportFragmentManager().beginTransaction() mapActivity.getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.slide_in_bottom, R.anim.slide_out_bottom, R.anim.slide_in_bottom, R.anim.slide_out_bottom) .setCustomAnimations(slideInAnim, slideOutAnim, slideInAnim, slideOutAnim)
.add(R.id.fragmentContainer, fragment, "MapContextMenuFragment") .add(R.id.fragmentContainer, fragment, "MapContextMenuFragment")
.addToBackStack(null).commit(); .addToBackStack(null).commit();
} }
@ -551,6 +540,12 @@ public class MapContextMenuFragment extends Fragment {
return dm.heightPixels; return dm.heightPixels;
} }
private int getScreenWidth() {
DisplayMetrics dm = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
return dm.widthPixels;
}
private int dpToPx(float dp) { private int dpToPx(float dp) {
Resources r = getActivity().getResources(); Resources r = getActivity().getResources();
return (int) TypedValue.applyDimension( return (int) TypedValue.applyDimension(

View file

@ -1,21 +1,23 @@
package net.osmand.plus.mapcontextmenu.sections; package net.osmand.plus.mapcontextmenu.sections;
import android.app.Activity;
import net.osmand.data.Amenity; import net.osmand.data.Amenity;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
public class AmenityInfoMenuController extends MenuController { public class AmenityInfoMenuController extends MenuController {
public AmenityInfoMenuController(OsmandApplication app, final Amenity amenity) { public AmenityInfoMenuController(OsmandApplication app, Activity activity, final Amenity amenity) {
super(new AmenityInfoMenuBuilder(app, amenity)); super(new AmenityInfoMenuBuilder(app, amenity), activity);
} }
@Override @Override
public int getInitialMenuState() { protected int getInitialMenuStatePortrait() {
return MenuState.HEADER_ONLY; return MenuState.HEADER_ONLY;
} }
@Override @Override
public int getSupportedMenuStates() { protected int getSupportedMenuStatesPortrait() {
return MenuState.HEADER_ONLY | MenuState.HALF_SCREEN | MenuState.FULL_SCREEN; return MenuState.HEADER_ONLY | MenuState.HALF_SCREEN | MenuState.FULL_SCREEN;
} }

View file

@ -1,7 +1,11 @@
package net.osmand.plus.mapcontextmenu.sections; package net.osmand.plus.mapcontextmenu.sections;
import android.app.Activity;
import android.view.View; import android.view.View;
import net.osmand.plus.R;
import net.osmand.plus.helpers.AndroidUiHelper;
public abstract class MenuController { public abstract class MenuController {
public class MenuState { public class MenuState {
@ -12,9 +16,13 @@ public abstract class MenuController {
private MenuBuilder builder; private MenuBuilder builder;
private int currentMenuState; private int currentMenuState;
private boolean portraitMode;
private boolean largeDevice;
public MenuController(MenuBuilder builder) { public MenuController(MenuBuilder builder, Activity activity) {
this.builder = builder; this.builder = builder;
portraitMode = AndroidUiHelper.isOrientationPortrait(activity);
largeDevice = AndroidUiHelper.isXLargeDevice(activity);
this.currentMenuState = getInitialMenuState(); this.currentMenuState = getInitialMenuState();
} }
@ -23,10 +31,50 @@ public abstract class MenuController {
} }
public int getInitialMenuState() { public int getInitialMenuState() {
return MenuState.HEADER_ONLY; if (isLandscapeLayout()) {
return MenuState.FULL_SCREEN;
} else {
return getInitialMenuStatePortrait();
}
}
public boolean isLandscapeLayout() {
return !portraitMode && !largeDevice;
}
public float getLandscapeWidthDp() {
return 350f;
} }
public int getSupportedMenuStates() { public int getSupportedMenuStates() {
if (isLandscapeLayout()) {
return MenuState.FULL_SCREEN;
} else {
return getSupportedMenuStatesPortrait();
}
}
public int getSlideInAnimation() {
if (isLandscapeLayout()) {
return R.anim.slide_in_left;
} else {
return R.anim.slide_in_bottom;
}
}
public int getSlideOutAnimation() {
if (isLandscapeLayout()) {
return R.anim.slide_out_left;
} else {
return R.anim.slide_out_bottom;
}
}
protected int getInitialMenuStatePortrait() {
return MenuState.HEADER_ONLY;
}
protected int getSupportedMenuStatesPortrait() {
return MenuState.HEADER_ONLY | MenuState.HALF_SCREEN | MenuState.FULL_SCREEN; return MenuState.HEADER_ONLY | MenuState.HALF_SCREEN | MenuState.FULL_SCREEN;
} }