2 * ownCloud Android client application
4 * @author Lorensius. W. T
5 * Copyright (C) 2011 Bartek Przybylski
6 * Copyright (C) 2015 ownCloud Inc.
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 package com
.owncloud
.android
.ui
;
23 import android
.content
.Context
;
25 import android
.graphics
.Rect
;
26 import android
.graphics
.drawable
.Drawable
;
28 import android
.widget
.ImageView
;
29 import android
.widget
.TextView
;
30 import android
.widget
.LinearLayout
;
31 import android
.widget
.ScrollView
;
33 import android
.view
.Gravity
;
34 import android
.view
.LayoutInflater
;
35 import android
.view
.View
;
36 import android
.view
.View
.OnClickListener
;
37 import android
.view
.ViewGroup
.LayoutParams
;
38 import android
.view
.ViewGroup
;
40 import java
.util
.ArrayList
;
42 import com
.owncloud
.android
.R
;
46 * Popup window, shows action list as icon and text like the one in Gallery3D
49 public class QuickAction
extends CustomPopup
{
50 private final View root
;
51 private final ImageView mArrowUp
;
52 private final ImageView mArrowDown
;
53 private final LayoutInflater inflater
;
54 private final Context context
;
56 protected static final int ANIM_GROW_FROM_LEFT
= 1;
57 protected static final int ANIM_GROW_FROM_RIGHT
= 2;
58 protected static final int ANIM_GROW_FROM_CENTER
= 3;
59 protected static final int ANIM_REFLECT
= 4;
60 protected static final int ANIM_AUTO
= 5;
62 private int animStyle
;
63 private ViewGroup mTrack
;
64 private ScrollView scroller
;
65 private ArrayList
<ActionItem
> actionList
;
70 * @param anchor {@link View} on where the popup window should be displayed
72 public QuickAction(View anchor
) {
75 actionList
= new ArrayList
<ActionItem
>();
76 context
= anchor
.getContext();
77 inflater
= (LayoutInflater
) context
78 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
80 root
= (ViewGroup
) inflater
.inflate(R
.layout
.popup
, null
);
82 mArrowDown
= (ImageView
) root
.findViewById(R
.id
.arrow_down
);
83 mArrowUp
= (ImageView
) root
.findViewById(R
.id
.arrow_up
);
87 mTrack
= (ViewGroup
) root
.findViewById(R
.id
.tracks
);
88 scroller
= (ScrollView
) root
.findViewById(R
.id
.scroller
);
89 animStyle
= ANIM_AUTO
;
95 * @param animStyle animation style, default is set to ANIM_AUTO
97 public void setAnimStyle(int animStyle
) {
98 this.animStyle
= animStyle
;
104 * @param action {@link ActionItem} object
106 public void addActionItem(ActionItem action
) {
107 actionList
.add(action
);
111 * Show popup window. Popup is automatically positioned, on top or bottom of
120 int[] location
= new int[2];
122 mAnchor
.getLocationOnScreen(location
);
124 Rect anchorRect
= new Rect(location
[0], location
[1], location
[0]
125 + mAnchor
.getWidth(), location
[1] + mAnchor
.getHeight());
129 root
.setLayoutParams(new LayoutParams(LayoutParams
.WRAP_CONTENT
,
130 LayoutParams
.WRAP_CONTENT
));
131 root
.measure(LayoutParams
.WRAP_CONTENT
, LayoutParams
.WRAP_CONTENT
);
133 int rootHeight
= root
.getMeasuredHeight();
134 int rootWidth
= root
.getMeasuredWidth();
136 int screenWidth
= mWManager
.getDefaultDisplay().getWidth();
137 int screenHeight
= mWManager
.getDefaultDisplay().getHeight();
139 // automatically get X coord of popup (top left)
140 if ((anchorRect
.left
+ rootWidth
) > screenWidth
) {
141 xPos
= anchorRect
.left
- (rootWidth
- mAnchor
.getWidth());
143 if (mAnchor
.getWidth() > rootWidth
) {
144 xPos
= anchorRect
.centerX() - (rootWidth
/ 2);
146 xPos
= anchorRect
.left
;
150 int dyTop
= anchorRect
.top
;
151 int dyBottom
= screenHeight
- anchorRect
.bottom
;
153 boolean onTop
= (dyTop
> dyBottom
) ? true
: false
;
156 if (rootHeight
> dyTop
) {
158 LayoutParams l
= scroller
.getLayoutParams();
159 l
.height
= dyTop
- mAnchor
.getHeight();
161 yPos
= anchorRect
.top
- rootHeight
;
164 yPos
= anchorRect
.bottom
;
166 if (rootHeight
> dyBottom
) {
167 LayoutParams l
= scroller
.getLayoutParams();
172 showArrow(((onTop
) ? R
.id
.arrow_down
: R
.id
.arrow_up
),
173 anchorRect
.centerX() - xPos
);
175 setAnimationStyle(screenWidth
, anchorRect
.centerX(), onTop
);
177 mWindow
.showAtLocation(mAnchor
, Gravity
.NO_GRAVITY
, xPos
, yPos
);
181 * Set animation style
183 * @param screenWidth screen width
184 * @param requestedX distance from left edge
185 * @param onTop flag to indicate where the popup should be displayed. Set
186 * TRUE if displayed on top of anchor view and vice versa
188 private void setAnimationStyle(int screenWidth
, int requestedX
,
190 int arrowPos
= requestedX
- mArrowUp
.getMeasuredWidth() / 2;
193 case ANIM_GROW_FROM_LEFT
:
194 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Left
195 : R
.style
.Animations_PopDownMenu_Left
);
198 case ANIM_GROW_FROM_RIGHT
:
199 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Right
200 : R
.style
.Animations_PopDownMenu_Right
);
203 case ANIM_GROW_FROM_CENTER
:
204 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Center
205 : R
.style
.Animations_PopDownMenu_Center
);
209 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Reflect
210 : R
.style
.Animations_PopDownMenu_Reflect
);
214 if (arrowPos
<= screenWidth
/ 4) {
215 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Left
216 : R
.style
.Animations_PopDownMenu_Left
);
217 } else if (arrowPos
> screenWidth
/ 4
218 && arrowPos
< 3 * (screenWidth
/ 4)) {
219 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Center
220 : R
.style
.Animations_PopDownMenu_Center
);
222 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Right
223 : R
.style
.Animations_PopDownMenu_Right
);
233 private void createActionList() {
237 OnClickListener listener
;
239 for (int i
= 0; i
< actionList
.size(); i
++) {
240 title
= actionList
.get(i
).getTitle();
241 icon
= actionList
.get(i
).getIcon();
242 listener
= actionList
.get(i
).getOnClickListerner();
244 view
= getActionItem(title
, icon
, listener
);
246 view
.setFocusable(true
);
247 view
.setClickable(true
);
249 mTrack
.addView(view
);
254 * Get action item {@link View}
256 * @param title action item title
257 * @param icon {@link Drawable} action item icon
258 * @param listener {@link View.OnClickListener} action item listener
259 * @return action item {@link View}
261 private View
getActionItem(String title
, Drawable icon
,
262 OnClickListener listener
) {
263 LinearLayout container
= (LinearLayout
) inflater
.inflate(
264 R
.layout
.action_item
, null
);
266 ImageView img
= (ImageView
) container
.findViewById(R
.id
.icon
);
267 TextView text
= (TextView
) container
.findViewById(R
.id
.title
);
270 img
.setImageDrawable(icon
);
277 if (listener
!= null
) {
278 container
.setOnClickListener(listener
);
287 * @param whichArrow arrow type resource id
288 * @param requestedX distance from left screen
290 private void showArrow(int whichArrow
, int requestedX
) {
291 final View showArrow
= (whichArrow
== R
.id
.arrow_up
) ? mArrowUp
293 final View hideArrow
= (whichArrow
== R
.id
.arrow_up
) ? mArrowDown
296 final int arrowWidth
= mArrowUp
.getMeasuredWidth();
298 showArrow
.setVisibility(View
.VISIBLE
);
300 ViewGroup
.MarginLayoutParams param
= (ViewGroup
.MarginLayoutParams
) showArrow
303 param
.leftMargin
= requestedX
- arrowWidth
/ 2;
305 hideArrow
.setVisibility(View
.INVISIBLE
);