Skip to content

Commit c6aca59

Browse files
author
aleksandr.zaycev
committed
Merge branch 'release/v1.0.4'
2 parents 3246039 + 171fedd commit c6aca59

File tree

6 files changed

+209
-72
lines changed

6 files changed

+209
-72
lines changed

app/src/main/java/com/yalantis/contextmenu/sample/MainActivity.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.yalantis.contextmenu.R;
2121
import com.yalantis.contextmenu.lib.ContextMenuDialogFragment;
2222
import com.yalantis.contextmenu.lib.MenuObject;
23+
import com.yalantis.contextmenu.lib.MenuParams;
2324
import com.yalantis.contextmenu.lib.interfaces.OnMenuItemClickListener;
2425
import com.yalantis.contextmenu.lib.interfaces.OnMenuItemLongClickListener;
2526

@@ -38,10 +39,18 @@ protected void onCreate(Bundle savedInstanceState) {
3839
setContentView(R.layout.activity_main);
3940
fragmentManager = getSupportFragmentManager();
4041
initToolbar();
41-
mMenuDialogFragment = ContextMenuDialogFragment.newInstance((int) getResources().getDimension(R.dimen.tool_bar_height), getMenuObjects());
42+
initMenuFragment();
4243
addFragment(new MainFragment(), true, R.id.container);
4344
}
4445

46+
private void initMenuFragment() {
47+
MenuParams menuParams = new MenuParams();
48+
menuParams.setActionBarSize((int) getResources().getDimension(R.dimen.tool_bar_height));
49+
menuParams.setMenuObjects(getMenuObjects());
50+
menuParams.setClosableOutside(false);
51+
mMenuDialogFragment = ContextMenuDialogFragment.newInstance(menuParams);
52+
}
53+
4554
private List<MenuObject> getMenuObjects() {
4655
// You can use any [resource, bitmap, drawable, color] as image:
4756
// item.setResource(...)
@@ -143,10 +152,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
143152

144153
@Override
145154
public void onBackPressed() {
146-
if (fragmentManager.getBackStackEntryCount() == 1) {
155+
if (mMenuDialogFragment != null && mMenuDialogFragment.isAdded()) {
156+
mMenuDialogFragment.dismiss();
157+
} else{
147158
finish();
148-
} else {
149-
super.onBackPressed();
150159
}
151160
}
152161

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
1818
# org.gradle.parallel=true
1919

20-
VERSION_NAME=1.0.3
21-
VERSION_CODE=4
20+
VERSION_NAME=1.0.4
21+
VERSION_CODE=5
2222
GROUP=com.yalantis
2323

2424
POM_DESCRIPTION=Android Library to display awesome context menu

lib/src/main/java/com/yalantis/contextmenu/lib/ContextMenuDialogFragment.java

Lines changed: 55 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.yalantis.contextmenu.lib;
22

3+
import android.annotation.SuppressLint;
34
import android.annotation.TargetApi;
45
import android.app.Activity;
56
import android.os.Build;
@@ -18,76 +19,68 @@
1819
import com.yalantis.contextmenu.lib.interfaces.OnMenuItemClickListener;
1920
import com.yalantis.contextmenu.lib.interfaces.OnMenuItemLongClickListener;
2021

21-
import java.util.ArrayList;
2222
import java.util.List;
2323

2424
public class ContextMenuDialogFragment extends DialogFragment implements OnItemClickListener, OnItemLongClickListener {
2525

2626
public static final String TAG = ContextMenuDialogFragment.class.getSimpleName();
27-
private static final String ACTION_BAR_SIZE = "action_bar_size";
28-
private static final String MENU_OBJECTS = "menu_objects";
29-
private static final String ANIMATION_DELAY = "animation_delay";
30-
private static final String ANIMATION_DURATION = "animation_duration";
31-
private static final String FITS_SYSTEM_WINDOW = "fits_system_window";
32-
private static final String CLIP_TO_PADDING = "clip_to_padding";
27+
private static final String BUNDLE_MENU_PARAMS = "BUNDLE_MENU_PARAMS";
3328

3429
private LinearLayout mWrapperButtons;
3530
private LinearLayout mWrapperText;
3631
private MenuAdapter mDropDownMenuAdapter;
37-
private ArrayList<MenuObject> mMenuObjects;
38-
private int mActionBarHeight;
3932
private OnMenuItemClickListener mItemClickListener;
4033
private OnMenuItemLongClickListener mItemLongClickListener;
41-
/**
42-
* Delay after opening and before closing {@link com.yalantis.contextmenu.lib.ContextMenuDialogFragment}
43-
*/
44-
private int mAnimationDelay = 0;
45-
private int mAnimationDuration;
34+
private MenuParams mMenuParams;
4635

36+
@Deprecated
4737
public static ContextMenuDialogFragment newInstance(int actionBarSize, List<MenuObject> menuObjects) {
48-
ContextMenuDialogFragment contextMenuDialogFragment = new ContextMenuDialogFragment();
49-
Bundle args = new Bundle();
50-
args.putInt(ACTION_BAR_SIZE, actionBarSize);
51-
args.putParcelableArrayList(MENU_OBJECTS, new ArrayList<>(menuObjects));
52-
contextMenuDialogFragment.setArguments(args);
53-
return contextMenuDialogFragment;
38+
MenuParams params = new MenuParams();
39+
params.setActionBarSize(actionBarSize);
40+
params.setMenuObjects(menuObjects);
41+
return newInstance(params);
5442
}
5543

44+
@Deprecated
5645
public static ContextMenuDialogFragment newInstance(int actionBarSize, List<MenuObject> menuObjects, int animationDelay) {
57-
ContextMenuDialogFragment contextMenuDialogFragment = new ContextMenuDialogFragment();
58-
Bundle args = new Bundle();
59-
args.putInt(ACTION_BAR_SIZE, actionBarSize);
60-
args.putParcelableArrayList(MENU_OBJECTS, new ArrayList<>(menuObjects));
61-
args.putInt(ANIMATION_DELAY, animationDelay);
62-
contextMenuDialogFragment.setArguments(args);
63-
return contextMenuDialogFragment;
46+
MenuParams params = new MenuParams();
47+
params.setActionBarSize(actionBarSize);
48+
params.setMenuObjects(menuObjects);
49+
params.setAnimationDelay(animationDelay);
50+
return newInstance(params);
6451
}
6552

53+
@Deprecated
6654
public static ContextMenuDialogFragment newInstance(int actionBarSize, List<MenuObject> menuObjects, int animationDelay, int animationDuration) {
67-
ContextMenuDialogFragment contextMenuDialogFragment = new ContextMenuDialogFragment();
68-
Bundle args = new Bundle();
69-
args.putInt(ACTION_BAR_SIZE, actionBarSize);
70-
args.putParcelableArrayList(MENU_OBJECTS, new ArrayList<>(menuObjects));
71-
args.putInt(ANIMATION_DELAY, animationDelay);
72-
args.putInt(ANIMATION_DURATION, animationDuration);
73-
contextMenuDialogFragment.setArguments(args);
74-
return contextMenuDialogFragment;
55+
MenuParams params = new MenuParams();
56+
params.setActionBarSize(actionBarSize);
57+
params.setMenuObjects(menuObjects);
58+
params.setAnimationDelay(animationDelay);
59+
params.setAnimationDuration(animationDuration);
60+
return newInstance(params);
7561
}
7662

63+
@Deprecated
7764
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
7865
public static ContextMenuDialogFragment newInstance(int actionBarSize, List<MenuObject> menuObjects,
7966
int animationDelay, int animationDuration,
8067
boolean fitsSystemWindow, boolean clipToPadding) {
81-
ContextMenuDialogFragment contextMenuDialogFragment = new ContextMenuDialogFragment();
68+
MenuParams params = new MenuParams();
69+
params.setActionBarSize(actionBarSize);
70+
params.setMenuObjects(menuObjects);
71+
params.setAnimationDelay(animationDelay);
72+
params.setAnimationDuration(animationDuration);
73+
params.setFitsSystemWindow(fitsSystemWindow);
74+
params.setClipToPadding(clipToPadding);
75+
return newInstance(params);
76+
}
77+
78+
public static ContextMenuDialogFragment newInstance(MenuParams menuParams) {
79+
ContextMenuDialogFragment fragment = new ContextMenuDialogFragment();
8280
Bundle args = new Bundle();
83-
args.putInt(ACTION_BAR_SIZE, actionBarSize);
84-
args.putParcelableArrayList(MENU_OBJECTS, new ArrayList<>(menuObjects));
85-
args.putInt(ANIMATION_DELAY, animationDelay);
86-
args.putInt(ANIMATION_DURATION, animationDuration);
87-
args.putBoolean(FITS_SYSTEM_WINDOW, fitsSystemWindow);
88-
args.putBoolean(CLIP_TO_PADDING, clipToPadding);
89-
contextMenuDialogFragment.setArguments(args);
90-
return contextMenuDialogFragment;
81+
args.putParcelable(BUNDLE_MENU_PARAMS, menuParams);
82+
fragment.setArguments(args);
83+
return fragment;
9184
}
9285

9386
@Override
@@ -112,26 +105,16 @@ public void onCreate(Bundle savedInstanceState) {
112105
super.onCreate(savedInstanceState);
113106
setStyle(STYLE_NO_FRAME, R.style.MenuFragmentStyle);
114107
if (getArguments() != null) {
115-
mActionBarHeight = getArguments().getInt(ACTION_BAR_SIZE);
116-
mMenuObjects = getArguments().getParcelableArrayList(MENU_OBJECTS);
117-
if(getArguments().containsKey(ANIMATION_DELAY)){
118-
mAnimationDelay = getArguments().getInt(ANIMATION_DELAY);
119-
}
120-
mAnimationDuration = (getArguments().containsKey(ANIMATION_DURATION))?
121-
getArguments().getInt(ANIMATION_DURATION): MenuAdapter.ANIMATION_DURATION_MILLIS;
108+
mMenuParams = getArguments().getParcelable(BUNDLE_MENU_PARAMS);
122109
}
123110
}
124111

112+
@SuppressLint("NewApi")
125113
@Override
126114
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
127115
View rootView = inflater.inflate(R.layout.fragment_menu, container, false);
128-
129-
if (getArguments().containsKey(FITS_SYSTEM_WINDOW)) {
130-
rootView.setFitsSystemWindows(getArguments().getBoolean(FITS_SYSTEM_WINDOW));
131-
}
132-
if (getArguments().containsKey(CLIP_TO_PADDING)) {
133-
((ViewGroup) rootView).setClipToPadding(getArguments().getBoolean(CLIP_TO_PADDING));
134-
}
116+
rootView.setFitsSystemWindows(mMenuParams.isFitsSystemWindow());
117+
((ViewGroup) rootView).setClipToPadding(mMenuParams.isClipToPadding());
135118

136119
initViews(rootView);
137120
getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
@@ -141,7 +124,16 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
141124
public void run() {
142125
mDropDownMenuAdapter.menuToggle();
143126
}
144-
},mAnimationDelay);
127+
}, mMenuParams.getAnimationDelay());
128+
129+
if (mMenuParams.isClosableOutside()) {
130+
rootView.findViewById(R.id.root).setOnClickListener(new View.OnClickListener() {
131+
@Override
132+
public void onClick(View v) {
133+
getActivity().onBackPressed();
134+
}
135+
});
136+
}
145137
return rootView;
146138
}
147139

@@ -151,10 +143,11 @@ private void initViews(View view) {
151143
}
152144

153145
private void initDropDownMenuAdapter() {
154-
mDropDownMenuAdapter = new MenuAdapter(getActivity(), mWrapperButtons, mWrapperText, mMenuObjects, mActionBarHeight);
146+
mDropDownMenuAdapter = new MenuAdapter(getActivity(), mWrapperButtons, mWrapperText,
147+
mMenuParams.getMenuObjects(), mMenuParams.getActionBarSize());
155148
mDropDownMenuAdapter.setOnItemClickListener(this);
156149
mDropDownMenuAdapter.setOnItemLongClickListener(this);
157-
mDropDownMenuAdapter.setAnimationDuration(mAnimationDuration);
150+
mDropDownMenuAdapter.setAnimationDuration(mMenuParams.getAnimationDuration());
158151
}
159152

160153
private void close() {
@@ -163,7 +156,7 @@ private void close() {
163156
public void run() {
164157
dismiss();
165158
}
166-
},mAnimationDelay);
159+
}, mMenuParams.getAnimationDelay());
167160
}
168161

169162
/**

lib/src/main/java/com/yalantis/contextmenu/lib/MenuObject.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,12 @@ public int describeContents() {
173173
@Override
174174
public void writeToParcel(Parcel dest, int flags) {
175175
dest.writeString(this.mTitle);
176-
dest.writeParcelable(((BitmapDrawable) this.mBgDrawable).getBitmap(), flags);
176+
dest.writeParcelable(mBgDrawable == null ? null :
177+
((BitmapDrawable) this.mBgDrawable).getBitmap(), flags);
177178
dest.writeInt(this.mBgColor);
178179
dest.writeInt(this.mBgResource);
179-
dest.writeParcelable(((BitmapDrawable) this.mDrawable).getBitmap(), flags);
180+
dest.writeParcelable(mDrawable == null ? null :
181+
((BitmapDrawable) this.mDrawable).getBitmap(), flags);
180182
dest.writeInt(this.mColor);
181183
dest.writeParcelable(this.mBitmap, 0);
182184
dest.writeInt(this.mResource);
@@ -188,10 +190,16 @@ public void writeToParcel(Parcel dest, int flags) {
188190

189191
private MenuObject(Parcel in) {
190192
this.mTitle = in.readString();
191-
this.mBgDrawable = new BitmapDrawable((Bitmap) in.readParcelable(getClass().getClassLoader()));
193+
Bitmap bitmapBgDrawable = in.readParcelable(Bitmap.class.getClassLoader());
194+
if (bitmapBgDrawable != null) {
195+
this.mBgDrawable = new BitmapDrawable(bitmapBgDrawable);
196+
}
192197
this.mBgColor = in.readInt();
193198
this.mBgResource = in.readInt();
194-
this.mDrawable = new BitmapDrawable((Bitmap) in.readParcelable(Drawable.class.getClassLoader()));
199+
Bitmap bitmapDrawable = in.readParcelable(Bitmap.class.getClassLoader());
200+
if (bitmapDrawable != null) {
201+
this.mDrawable = new BitmapDrawable(bitmapDrawable);
202+
}
195203
this.mColor = in.readInt();
196204
this.mBitmap = in.readParcelable(Bitmap.class.getClassLoader());
197205
this.mResource = in.readInt();

0 commit comments

Comments
 (0)