1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.ui
;
20 import android
.content
.Context
;
22 import android
.graphics
.Rect
;
23 import android
.graphics
.drawable
.Drawable
;
25 import android
.widget
.ImageView
;
26 import android
.widget
.TextView
;
27 import android
.widget
.LinearLayout
;
28 import android
.widget
.ScrollView
;
30 import android
.view
.Gravity
;
31 import android
.view
.LayoutInflater
;
32 import android
.view
.View
;
33 import android
.view
.View
.OnClickListener
;
34 import android
.view
.ViewGroup
.LayoutParams
;
35 import android
.view
.ViewGroup
;
37 import java
.util
.ArrayList
;
39 import com
.owncloud
.android
.R
;
42 * Popup window, shows action list as icon and text like the one in Gallery3D
45 * @author Lorensius. W. T
47 public class QuickAction
extends CustomPopup
{
48 private final View root
;
49 private final ImageView mArrowUp
;
50 private final ImageView mArrowDown
;
51 private final LayoutInflater inflater
;
52 private final Context context
;
54 protected static final int ANIM_GROW_FROM_LEFT
= 1;
55 protected static final int ANIM_GROW_FROM_RIGHT
= 2;
56 protected static final int ANIM_GROW_FROM_CENTER
= 3;
57 protected static final int ANIM_REFLECT
= 4;
58 protected static final int ANIM_AUTO
= 5;
60 private int animStyle
;
61 private ViewGroup mTrack
;
62 private ScrollView scroller
;
63 private ArrayList
<ActionItem
> actionList
;
68 * @param anchor {@link View} on where the popup window should be displayed
70 public QuickAction(View anchor
) {
73 actionList
= new ArrayList
<ActionItem
>();
74 context
= anchor
.getContext();
75 inflater
= (LayoutInflater
) context
76 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
78 root
= (ViewGroup
) inflater
.inflate(R
.layout
.popup
, null
);
80 mArrowDown
= (ImageView
) root
.findViewById(R
.id
.arrow_down
);
81 mArrowUp
= (ImageView
) root
.findViewById(R
.id
.arrow_up
);
85 mTrack
= (ViewGroup
) root
.findViewById(R
.id
.tracks
);
86 scroller
= (ScrollView
) root
.findViewById(R
.id
.scroller
);
87 animStyle
= ANIM_AUTO
;
93 * @param animStyle animation style, default is set to ANIM_AUTO
95 public void setAnimStyle(int animStyle
) {
96 this.animStyle
= animStyle
;
102 * @param action {@link ActionItem} object
104 public void addActionItem(ActionItem action
) {
105 actionList
.add(action
);
109 * Show popup window. Popup is automatically positioned, on top or bottom of
118 int[] location
= new int[2];
120 mAnchor
.getLocationOnScreen(location
);
122 Rect anchorRect
= new Rect(location
[0], location
[1], location
[0]
123 + mAnchor
.getWidth(), location
[1] + mAnchor
.getHeight());
127 root
.setLayoutParams(new LayoutParams(LayoutParams
.WRAP_CONTENT
,
128 LayoutParams
.WRAP_CONTENT
));
129 root
.measure(LayoutParams
.WRAP_CONTENT
, LayoutParams
.WRAP_CONTENT
);
131 int rootHeight
= root
.getMeasuredHeight();
132 int rootWidth
= root
.getMeasuredWidth();
134 int screenWidth
= mWManager
.getDefaultDisplay().getWidth();
135 int screenHeight
= mWManager
.getDefaultDisplay().getHeight();
137 // automatically get X coord of popup (top left)
138 if ((anchorRect
.left
+ rootWidth
) > screenWidth
) {
139 xPos
= anchorRect
.left
- (rootWidth
- mAnchor
.getWidth());
141 if (mAnchor
.getWidth() > rootWidth
) {
142 xPos
= anchorRect
.centerX() - (rootWidth
/ 2);
144 xPos
= anchorRect
.left
;
148 int dyTop
= anchorRect
.top
;
149 int dyBottom
= screenHeight
- anchorRect
.bottom
;
151 boolean onTop
= (dyTop
> dyBottom
) ? true
: false
;
154 if (rootHeight
> dyTop
) {
156 LayoutParams l
= scroller
.getLayoutParams();
157 l
.height
= dyTop
- mAnchor
.getHeight();
159 yPos
= anchorRect
.top
- rootHeight
;
162 yPos
= anchorRect
.bottom
;
164 if (rootHeight
> dyBottom
) {
165 LayoutParams l
= scroller
.getLayoutParams();
170 showArrow(((onTop
) ? R
.id
.arrow_down
: R
.id
.arrow_up
),
171 anchorRect
.centerX() - xPos
);
173 setAnimationStyle(screenWidth
, anchorRect
.centerX(), onTop
);
175 mWindow
.showAtLocation(mAnchor
, Gravity
.NO_GRAVITY
, xPos
, yPos
);
179 * Set animation style
181 * @param screenWidth screen width
182 * @param requestedX distance from left edge
183 * @param onTop flag to indicate where the popup should be displayed. Set
184 * TRUE if displayed on top of anchor view and vice versa
186 private void setAnimationStyle(int screenWidth
, int requestedX
,
188 int arrowPos
= requestedX
- mArrowUp
.getMeasuredWidth() / 2;
191 case ANIM_GROW_FROM_LEFT
:
192 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Left
193 : R
.style
.Animations_PopDownMenu_Left
);
196 case ANIM_GROW_FROM_RIGHT
:
197 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Right
198 : R
.style
.Animations_PopDownMenu_Right
);
201 case ANIM_GROW_FROM_CENTER
:
202 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Center
203 : R
.style
.Animations_PopDownMenu_Center
);
207 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Reflect
208 : R
.style
.Animations_PopDownMenu_Reflect
);
212 if (arrowPos
<= screenWidth
/ 4) {
213 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Left
214 : R
.style
.Animations_PopDownMenu_Left
);
215 } else if (arrowPos
> screenWidth
/ 4
216 && arrowPos
< 3 * (screenWidth
/ 4)) {
217 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Center
218 : R
.style
.Animations_PopDownMenu_Center
);
220 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Right
221 : R
.style
.Animations_PopDownMenu_Right
);
231 private void createActionList() {
235 OnClickListener listener
;
237 for (int i
= 0; i
< actionList
.size(); i
++) {
238 title
= actionList
.get(i
).getTitle();
239 icon
= actionList
.get(i
).getIcon();
240 listener
= actionList
.get(i
).getOnClickListerner();
242 view
= getActionItem(title
, icon
, listener
);
244 view
.setFocusable(true
);
245 view
.setClickable(true
);
247 mTrack
.addView(view
);
252 * Get action item {@link View}
254 * @param title action item title
255 * @param icon {@link Drawable} action item icon
256 * @param listener {@link View.OnClickListener} action item listener
257 * @return action item {@link View}
259 private View
getActionItem(String title
, Drawable icon
,
260 OnClickListener listener
) {
261 LinearLayout container
= (LinearLayout
) inflater
.inflate(
262 R
.layout
.action_item
, null
);
264 ImageView img
= (ImageView
) container
.findViewById(R
.id
.icon
);
265 TextView text
= (TextView
) container
.findViewById(R
.id
.title
);
268 img
.setImageDrawable(icon
);
275 if (listener
!= null
) {
276 container
.setOnClickListener(listener
);
285 * @param whichArrow arrow type resource id
286 * @param requestedX distance from left screen
288 private void showArrow(int whichArrow
, int requestedX
) {
289 final View showArrow
= (whichArrow
== R
.id
.arrow_up
) ? mArrowUp
291 final View hideArrow
= (whichArrow
== R
.id
.arrow_up
) ? mArrowDown
294 final int arrowWidth
= mArrowUp
.getMeasuredWidth();
296 showArrow
.setVisibility(View
.VISIBLE
);
298 ViewGroup
.MarginLayoutParams param
= (ViewGroup
.MarginLayoutParams
) showArrow
301 param
.leftMargin
= requestedX
- arrowWidth
/ 2;
303 hideArrow
.setVisibility(View
.INVISIBLE
);