11package com .yalantis .contextmenu .lib ;
22
3+ import android .annotation .SuppressLint ;
34import android .annotation .TargetApi ;
45import android .app .Activity ;
56import android .os .Build ;
1819import com .yalantis .contextmenu .lib .interfaces .OnMenuItemClickListener ;
1920import com .yalantis .contextmenu .lib .interfaces .OnMenuItemLongClickListener ;
2021
21- import java .util .ArrayList ;
2222import java .util .List ;
2323
2424public 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 /**
0 commit comments