Context menu - fixed crashes, landscape layout

This commit is contained in:
Alexey Kulish 2015-09-28 18:29:40 +03:00
parent 47bfc3c33e
commit 52d377b009
9 changed files with 358 additions and 41 deletions

View file

@ -0,0 +1,246 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/point_edit_layout"
android:layout_width="350dp"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="fill_parent"
android:layout_height="@dimen/dashboard_map_toolbar"
android:background="?attr/pstsTabBackground"
android:minHeight="@dimen/dashboard_map_toolbar"
android:theme="?attr/toolbar_theme"
app:contentInsetLeft="72dp"
app:contentInsetStart="72dp">
<ImageButton
android:id="@+id/delete_button"
android:layout_width="?attr/actionBarSize"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="@android:color/transparent"
android:src="@drawable/ic_action_delete_dark"/>
<Button
android:id="@+id/save_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="@android:color/transparent"
android:text="@string/shared_string_save"/>
</android.support.v7.widget.Toolbar>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/ctx_menu_info_view_bg"
android:fillViewport="true">
<LinearLayout
android:id="@+id/main_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/title_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/bg_point_editor_view"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp">
<TextView
android:id="@+id/header_caption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_gravity="center_vertical"
android:text="Point info"
android:textColor="?android:textColorPrimary"
android:textSize="@dimen/default_list_text_size"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal">
<LinearLayout
android:layout_width="42dp"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/name_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
android:scaleType="center"
android:src="@drawable/ic_action_building_number"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/name_caption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:text="@string/favourites_edit_dialog_name"
android:textColor="?android:textColorSecondary"
android:textSize="@dimen/default_list_text_size"/>
<EditText
android:id="@+id/name_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginRight="16dp"
android:layout_marginLeft="8dp"
android:textColor="?android:textColorPrimary"
android:textColorHint="?android:textColorSecondary"
android:inputType="text"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal">
<LinearLayout
android:layout_width="42dp"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/category_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
android:scaleType="center"
android:src="@drawable/ic_action_building_number"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/category_caption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:text="@string/favourites_edit_dialog_name"
android:textColor="?android:textColorSecondary"
android:textSize="@dimen/default_list_text_size"/>
<net.osmand.plus.widgets.AutoCompleteTextViewEx
android:id="@+id/category_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginRight="16dp"
android:layout_marginLeft="8dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:background="?attr/ctx_menu_info_view_bg"
android:orientation="horizontal">
<LinearLayout
android:layout_width="42dp"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/description_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
android:layout_marginTop="10dp"
android:scaleType="center"
android:src="@drawable/ic_action_note_dark"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:id="@+id/description_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_marginLeft="8dp"
android:maxLines="8"
android:layout_gravity="center_vertical"
android:textColor="?android:textColorPrimary"
android:textColorHint="?android:textColorSecondary"
android:inputType="textMultiLine"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
</FrameLayout>

View file

@ -27,6 +27,16 @@ public class FavouritePoint implements Serializable, LocationPoint {
this.name = name;
}
public FavouritePoint(FavouritePoint favouritePoint) {
this.latitude = favouritePoint.latitude;
this.longitude = favouritePoint.longitude;
this.category = favouritePoint.category;
this.name = favouritePoint.name;
this.color = favouritePoint.color;
this.description = favouritePoint.description;
this.visible = favouritePoint.visible;
}
public int getColor() {
return color;
}

View file

@ -4,6 +4,7 @@ import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import net.osmand.Location;
import net.osmand.ResultMatcher;
@ -52,7 +53,7 @@ public class MapContextMenu {
private static final String KEY_CTX_MENU_STREET_STR = "key_ctx_menu_street_str";
public boolean isMenuVisible() {
return mapActivity.getSupportFragmentManager().findFragmentByTag("MapContextMenuFragment") != null;
return findMenuFragment() != null;
}
public PointDescription getPointDescription() {
@ -107,9 +108,10 @@ public class MapContextMenu {
public void hide() {
MapContextMenuFragment fragment = findMenuFragment();
if (fragment != null)
if (fragment != null) {
fragment.dismissMenu();
}
}
private boolean needStreetName() {
boolean res = object != null || Algorithms.isEmpty(pointDescription.getName());
@ -138,12 +140,13 @@ public class MapContextMenu {
}
private MapContextMenuFragment findMenuFragment() {
Fragment fragment = mapActivity.getSupportFragmentManager().findFragmentByTag("MapContextMenuFragment");
if (fragment != null)
Fragment fragment = mapActivity.getSupportFragmentManager().findFragmentByTag(MapContextMenuFragment.TAG);
if (fragment != null) {
return (MapContextMenuFragment) fragment;
else
} else {
return null;
}
}
public int getLeftIconId() {
return leftIconId;
@ -335,8 +338,11 @@ public class MapContextMenu {
public void saveMenuState(Bundle bundle) {
if (object != null) {
if (object instanceof Amenity)
if (object instanceof Amenity) {
bundle.putSerializable(KEY_CTX_MENU_OBJECT, (Amenity) object);
} else if (object instanceof FavouritePoint) {
bundle.putSerializable(KEY_CTX_MENU_OBJECT, (FavouritePoint) object);
}
}
bundle.putSerializable(KEY_CTX_MENU_POINT_DESC, pointDescription);
bundle.putSerializable(KEY_CTX_MENU_NAME_STR, nameStr);

View file

@ -10,6 +10,7 @@ import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.GestureDetector;
@ -511,7 +512,7 @@ public class MapContextMenuFragment extends Fragment {
public void dismissMenu() {
FragmentActivity activity = getActivity();
if (activity != null) {
activity.getSupportFragmentManager().popBackStack();
activity.getSupportFragmentManager().popBackStack(TAG, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
}
@ -540,8 +541,8 @@ public class MapContextMenuFragment extends Fragment {
MapContextMenuFragment fragment = new MapContextMenuFragment();
mapActivity.getSupportFragmentManager().beginTransaction()
.setCustomAnimations(slideInAnim, slideOutAnim, slideInAnim, slideOutAnim)
.add(R.id.fragmentContainer, fragment, "MapContextMenuFragment")
.addToBackStack(null).commit();
.add(R.id.fragmentContainer, fragment, TAG)
.addToBackStack(TAG).commit();
}
private MapContextMenu getCtxMenu() {

View file

@ -11,7 +11,8 @@ public class FavoritePointEditor extends PointEditor {
private FavouritePoint favorite;
public static final String FRAGMENT_NAME = "FavoritePointEditorFragment";
public static final String TAG = "FavoritePointEditorFragment";
private static final String KEY_CTX_EDIT_FAV_OBJECT = "key_ctx_edit_fav_object";
public FavoritePointEditor(OsmandApplication app, MapActivity mapActivity) {
super(app, mapActivity);
@ -19,17 +20,21 @@ public class FavoritePointEditor extends PointEditor {
@Override
public void saveState(Bundle bundle) {
bundle.putSerializable(KEY_CTX_EDIT_FAV_OBJECT, favorite);
}
@Override
public void restoreState(Bundle bundle) {
Object object = bundle.getSerializable(KEY_CTX_EDIT_FAV_OBJECT);
if (object != null) {
favorite = (FavouritePoint)object;
}
}
@Override
public String getFragmentName() {
return FRAGMENT_NAME;
public String getFragmentTag() {
return TAG;
}
public FavouritePoint getFavorite() {

View file

@ -6,8 +6,10 @@ import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import net.osmand.data.FavouritePoint;
import net.osmand.data.PointDescription;
import net.osmand.plus.FavouritesDbHelper;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
@ -28,6 +30,11 @@ public class FavoritePointEditorFragment extends PointEditorFragment {
super.onAttach(activity);
helper = getMyApplication().getFavorites();
editor = getMapActivity().getFavoritePointEditor();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
favorite = editor.getFavorite();
}
@ -53,7 +60,7 @@ public class FavoritePointEditorFragment extends PointEditorFragment {
FavoritePointEditorFragment fragment = new FavoritePointEditorFragment();
mapActivity.getSupportFragmentManager().beginTransaction()
//.setCustomAnimations(slideInAnim, slideOutAnim, slideInAnim, slideOutAnim)
.add(R.id.fragmentContainer, fragment, editor.getFragmentName())
.add(R.id.fragmentContainer, fragment, editor.getFragmentTag())
.addToBackStack(null).commit();
}
@ -82,30 +89,31 @@ public class FavoritePointEditorFragment extends PointEditorFragment {
builder.setPositiveButton(R.string.shared_string_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (editor.isNew()) {
doAddFavorite(point.getName(), point.getCategory(), point.getDescription());
} else {
helper.editFavouriteName(favorite, point.getName(), point.getCategory(), point.getDescription());
}
getMapActivity().getMapView().refreshMap(true);
if (needDismiss) {
dismiss(true);
}
doSave(favorite, point.getName(), point.getCategory(), point.getDescription(), needDismiss);
}
});
builder.create().show();
} else {
doSave(favorite, point.getName(), point.getCategory(), point.getDescription(), needDismiss);
}
saved = true;
}
private void doSave(FavouritePoint favorite, String name, String category, String description, boolean needDismiss) {
if (editor.isNew()) {
doAddFavorite(point.getName(), point.getCategory(), point.getDescription());
doAddFavorite(name, category, description);
} else {
helper.editFavouriteName(favorite, point.getName(), point.getCategory(), point.getDescription());
helper.editFavouriteName(favorite, name, category, description);
}
getMapActivity().getMapView().refreshMap(true);
if (needDismiss) {
dismiss(true);
dismiss(false);
}
}
saved = true;
PointDescription pointDescription = favorite.getPointDescription();
pointDescription.setLat(favorite.getLatitude());
pointDescription.setLon(favorite.getLongitude());
getMapActivity().getContextMenu().show(pointDescription, new FavouritePoint(favorite));
}
private void doAddFavorite(String name, String category, String description) {

View file

@ -13,7 +13,6 @@ public abstract class PointEditor {
protected OsmandApplication app;
protected final MapActivity mapActivity;
protected Object object;
protected boolean isNew;
private boolean portraitMode;
@ -53,10 +52,10 @@ public abstract class PointEditor {
public abstract void saveState(Bundle bundle);
public abstract void restoreState(Bundle bundle);
public abstract String getFragmentName();
public abstract String getFragmentTag();
public void hide() {
Fragment fragment = mapActivity.getSupportFragmentManager().findFragmentByTag(getFragmentName());
Fragment fragment = mapActivity.getSupportFragmentManager().findFragmentByTag(getFragmentTag());
if (fragment != null)
((PointEditorFragment)fragment).dismiss();
}

View file

@ -1,12 +1,15 @@
package net.osmand.plus.mapcontextmenu.editors;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
@ -14,8 +17,11 @@ import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import net.osmand.plus.FavouritesDbHelper;
@ -23,11 +29,14 @@ import net.osmand.plus.IconsCache;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.widgets.AutoCompleteTextViewEx;
import net.osmand.util.Algorithms;
import java.util.List;
import static android.util.TypedValue.COMPLEX_UNIT_DIP;
public abstract class PointEditorFragment extends Fragment {
@Override
@ -36,16 +45,26 @@ public abstract class PointEditorFragment extends Fragment {
getEditor().saveState(outState);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null)
getEditor().restoreState(savedInstanceState);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (savedInstanceState != null)
getEditor().restoreState(savedInstanceState);
View view = inflater.inflate(R.layout.point_editor_fragment, container, false);
getActivity().findViewById(R.id.MapHudButtonsOverlay).setVisibility(View.INVISIBLE);
View view;
if (getEditor().isLandscapeLayout()) {
view = inflater.inflate(R.layout.point_editor_fragment_land, container, false);
} else {
view = inflater.inflate(R.layout.point_editor_fragment, container, false);
}
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
toolbar.setTitle(getToolbarTitle());
toolbar.setNavigationIcon(getMyApplication().getIconsCache().getIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha));
@ -128,6 +147,8 @@ public abstract class PointEditorFragment extends Fragment {
save(false);
}
super.onDestroyView();
getActivity().findViewById(R.id.MapHudButtonsOverlay).setVisibility(View.VISIBLE);
}
protected void savePressed() {
@ -212,4 +233,20 @@ public abstract class PointEditorFragment extends Fragment {
return Algorithms.isEmpty(res) ? null : res;
}
// Utils
private int getScreenHeight() {
DisplayMetrics dm = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
return dm.heightPixels;
}
private int dpToPx(float dp) {
Resources r = getActivity().getResources();
return (int) TypedValue.applyDimension(
COMPLEX_UNIT_DIP,
dp,
r.getDisplayMetrics()
);
}
}

View file

@ -0,0 +1,5 @@
package net.osmand.plus.mapcontextmenu.editors.dialogs;
public class CategoryDialogs {
}