1 package eu
.alefzero
.owncloud
;
3 import android
.content
.Context
;
5 import android
.graphics
.Rect
;
6 import android
.graphics
.drawable
.Drawable
;
8 import android
.widget
.ImageView
;
9 import android
.widget
.TextView
;
10 import android
.widget
.LinearLayout
;
11 import android
.widget
.ScrollView
;
13 import android
.view
.Gravity
;
14 import android
.view
.LayoutInflater
;
15 import android
.view
.View
;
16 import android
.view
.View
.OnClickListener
;
17 import android
.view
.ViewGroup
.LayoutParams
;
18 import android
.view
.ViewGroup
;
20 import java
.util
.ArrayList
;
23 * Popup window, shows action list as icon and text like the one in Gallery3D app.
25 * @author Lorensius. W. T
27 public class QuickAction
extends CustomPopup
{
28 private final View root
;
29 private final ImageView mArrowUp
;
30 private final ImageView mArrowDown
;
31 private final LayoutInflater inflater
;
32 private final Context context
;
34 protected static final int ANIM_GROW_FROM_LEFT
= 1;
35 protected static final int ANIM_GROW_FROM_RIGHT
= 2;
36 protected static final int ANIM_GROW_FROM_CENTER
= 3;
37 protected static final int ANIM_REFLECT
= 4;
38 protected static final int ANIM_AUTO
= 5;
40 private int animStyle
;
41 private ViewGroup mTrack
;
42 private ScrollView scroller
;
43 private ArrayList
<ActionItem
> actionList
;
48 * @param anchor {@link View} on where the popup window should be displayed
50 public QuickAction(View anchor
) {
53 actionList
= new ArrayList
<ActionItem
>();
54 context
= anchor
.getContext();
55 inflater
= (LayoutInflater
) context
.getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
57 root
= (ViewGroup
) inflater
.inflate(R
.layout
.popup
, null
);
59 mArrowDown
= (ImageView
) root
.findViewById(R
.id
.arrow_down
);
60 mArrowUp
= (ImageView
) root
.findViewById(R
.id
.arrow_up
);
64 mTrack
= (ViewGroup
) root
.findViewById(R
.id
.tracks
);
65 scroller
= (ScrollView
) root
.findViewById(R
.id
.scroller
);
66 animStyle
= ANIM_AUTO
;
72 * @param animStyle animation style, default is set to ANIM_AUTO
74 public void setAnimStyle(int animStyle
) {
75 this.animStyle
= animStyle
;
81 * @param action {@link ActionItem} object
83 public void addActionItem(ActionItem action
) {
84 actionList
.add(action
);
88 * Show popup window. Popup is automatically positioned, on top or bottom of anchor view.
96 int[] location
= new int[2];
98 mAnchor
.getLocationOnScreen(location
);
100 Rect anchorRect
= new Rect(location
[0], location
[1], location
[0] + mAnchor
.getWidth(), location
[1]
101 + mAnchor
.getHeight());
105 root
.setLayoutParams(new LayoutParams(LayoutParams
.WRAP_CONTENT
, LayoutParams
.WRAP_CONTENT
));
106 root
.measure(LayoutParams
.WRAP_CONTENT
, LayoutParams
.WRAP_CONTENT
);
108 int rootHeight
= root
.getMeasuredHeight();
109 int rootWidth
= root
.getMeasuredWidth();
111 int screenWidth
= mWManager
.getDefaultDisplay().getWidth();
112 int screenHeight
= mWManager
.getDefaultDisplay().getHeight();
114 //automatically get X coord of popup (top left)
115 if ((anchorRect
.left
+ rootWidth
) > screenWidth
) {
116 xPos
= anchorRect
.left
- (rootWidth
-mAnchor
.getWidth());
118 if (mAnchor
.getWidth() > rootWidth
) {
119 xPos
= anchorRect
.centerX() - (rootWidth
/2);
121 xPos
= anchorRect
.left
;
125 int dyTop
= anchorRect
.top
;
126 int dyBottom
= screenHeight
- anchorRect
.bottom
;
128 boolean onTop
= (dyTop
> dyBottom
) ? true
: false
;
131 if (rootHeight
> dyTop
) {
133 LayoutParams l
= scroller
.getLayoutParams();
134 l
.height
= dyTop
- mAnchor
.getHeight();
136 yPos
= anchorRect
.top
- rootHeight
;
139 yPos
= anchorRect
.bottom
;
141 if (rootHeight
> dyBottom
) {
142 LayoutParams l
= scroller
.getLayoutParams();
147 showArrow(((onTop
) ? R
.id
.arrow_down
: R
.id
.arrow_up
), anchorRect
.centerX()-xPos
);
149 setAnimationStyle(screenWidth
, anchorRect
.centerX(), onTop
);
151 mWindow
.showAtLocation(mAnchor
, Gravity
.NO_GRAVITY
, xPos
, yPos
);
155 * Set animation style
157 * @param screenWidth screen width
158 * @param requestedX distance from left edge
159 * @param onTop flag to indicate where the popup should be displayed. Set TRUE if displayed on top of anchor view
162 private void setAnimationStyle(int screenWidth
, int requestedX
, boolean onTop
) {
163 int arrowPos
= requestedX
- mArrowUp
.getMeasuredWidth()/2;
166 case ANIM_GROW_FROM_LEFT
:
167 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Left
: R
.style
.Animations_PopDownMenu_Left
);
170 case ANIM_GROW_FROM_RIGHT
:
171 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Right
: R
.style
.Animations_PopDownMenu_Right
);
174 case ANIM_GROW_FROM_CENTER
:
175 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Center
: R
.style
.Animations_PopDownMenu_Center
);
179 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Reflect
: R
.style
.Animations_PopDownMenu_Reflect
);
183 if (arrowPos
<= screenWidth
/4) {
184 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Left
: R
.style
.Animations_PopDownMenu_Left
);
185 } else if (arrowPos
> screenWidth
/4 && arrowPos
< 3 * (screenWidth
/4)) {
186 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Center
: R
.style
.Animations_PopDownMenu_Center
);
188 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Right
: R
.style
.Animations_PopDownMenu_Right
);
198 private void createActionList() {
202 OnClickListener listener
;
204 for (int i
= 0; i
< actionList
.size(); i
++) {
205 title
= actionList
.get(i
).getTitle();
206 icon
= actionList
.get(i
).getIcon();
207 listener
= actionList
.get(i
).getOnClickListerner();
209 view
= getActionItem(title
, icon
, listener
);
211 view
.setFocusable(true
);
212 view
.setClickable(true
);
214 mTrack
.addView(view
);
219 * Get action item {@link View}
221 * @param title action item title
222 * @param icon {@link Drawable} action item icon
223 * @param listener {@link View.OnClickListener} action item listener
224 * @return action item {@link View}
226 private View
getActionItem(String title
, Drawable icon
, OnClickListener listener
) {
227 LinearLayout container
= (LinearLayout
) inflater
.inflate(R
.layout
.action_item
, null
);
229 ImageView img
= (ImageView
) container
.findViewById(R
.id
.icon
);
230 TextView text
= (TextView
) container
.findViewById(R
.id
.title
);
233 img
.setImageDrawable(icon
);
240 if (listener
!= null
) {
241 container
.setOnClickListener(listener
);
250 * @param whichArrow arrow type resource id
251 * @param requestedX distance from left screen
253 private void showArrow(int whichArrow
, int requestedX
) {
254 final View showArrow
= (whichArrow
== R
.id
.arrow_up
) ? mArrowUp
: mArrowDown
;
255 final View hideArrow
= (whichArrow
== R
.id
.arrow_up
) ? mArrowDown
: mArrowUp
;
257 final int arrowWidth
= mArrowUp
.getMeasuredWidth();
259 showArrow
.setVisibility(View
.VISIBLE
);
261 ViewGroup
.MarginLayoutParams param
= (ViewGroup
.MarginLayoutParams
)showArrow
.getLayoutParams();
263 param
.leftMargin
= requestedX
- arrowWidth
/ 2;
265 hideArrow
.setVisibility(View
.INVISIBLE
);