1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
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 eu
.alefzero
.owncloud
.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 eu
.alefzero
.owncloud
.R
;
40 import eu
.alefzero
.owncloud
.R
.id
;
41 import eu
.alefzero
.owncloud
.R
.layout
;
42 import eu
.alefzero
.owncloud
.R
.style
;
45 * Popup window, shows action list as icon and text like the one in Gallery3D app.
47 * @author Lorensius. W. T
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
.getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
79 root
= (ViewGroup
) inflater
.inflate(R
.layout
.popup
, null
);
81 mArrowDown
= (ImageView
) root
.findViewById(R
.id
.arrow_down
);
82 mArrowUp
= (ImageView
) root
.findViewById(R
.id
.arrow_up
);
86 mTrack
= (ViewGroup
) root
.findViewById(R
.id
.tracks
);
87 scroller
= (ScrollView
) root
.findViewById(R
.id
.scroller
);
88 animStyle
= ANIM_AUTO
;
94 * @param animStyle animation style, default is set to ANIM_AUTO
96 public void setAnimStyle(int animStyle
) {
97 this.animStyle
= animStyle
;
103 * @param action {@link ActionItem} object
105 public void addActionItem(ActionItem action
) {
106 actionList
.add(action
);
110 * Show popup window. Popup is automatically positioned, on top or bottom of anchor view.
113 public void show () {
118 int[] location
= new int[2];
120 mAnchor
.getLocationOnScreen(location
);
122 Rect anchorRect
= new Rect(location
[0], location
[1], location
[0] + mAnchor
.getWidth(), location
[1]
123 + mAnchor
.getHeight());
127 root
.setLayoutParams(new LayoutParams(LayoutParams
.WRAP_CONTENT
, LayoutParams
.WRAP_CONTENT
));
128 root
.measure(LayoutParams
.WRAP_CONTENT
, LayoutParams
.WRAP_CONTENT
);
130 int rootHeight
= root
.getMeasuredHeight();
131 int rootWidth
= root
.getMeasuredWidth();
133 int screenWidth
= mWManager
.getDefaultDisplay().getWidth();
134 int screenHeight
= mWManager
.getDefaultDisplay().getHeight();
136 //automatically get X coord of popup (top left)
137 if ((anchorRect
.left
+ rootWidth
) > screenWidth
) {
138 xPos
= anchorRect
.left
- (rootWidth
-mAnchor
.getWidth());
140 if (mAnchor
.getWidth() > rootWidth
) {
141 xPos
= anchorRect
.centerX() - (rootWidth
/2);
143 xPos
= anchorRect
.left
;
147 int dyTop
= anchorRect
.top
;
148 int dyBottom
= screenHeight
- anchorRect
.bottom
;
150 boolean onTop
= (dyTop
> dyBottom
) ? true
: false
;
153 if (rootHeight
> dyTop
) {
155 LayoutParams l
= scroller
.getLayoutParams();
156 l
.height
= dyTop
- mAnchor
.getHeight();
158 yPos
= anchorRect
.top
- rootHeight
;
161 yPos
= anchorRect
.bottom
;
163 if (rootHeight
> dyBottom
) {
164 LayoutParams l
= scroller
.getLayoutParams();
169 showArrow(((onTop
) ? R
.id
.arrow_down
: R
.id
.arrow_up
), anchorRect
.centerX()-xPos
);
171 setAnimationStyle(screenWidth
, anchorRect
.centerX(), onTop
);
173 mWindow
.showAtLocation(mAnchor
, Gravity
.NO_GRAVITY
, xPos
, yPos
);
177 * Set animation style
179 * @param screenWidth screen width
180 * @param requestedX distance from left edge
181 * @param onTop flag to indicate where the popup should be displayed. Set TRUE if displayed on top of anchor view
184 private void setAnimationStyle(int screenWidth
, int requestedX
, boolean onTop
) {
185 int arrowPos
= requestedX
- mArrowUp
.getMeasuredWidth()/2;
188 case ANIM_GROW_FROM_LEFT
:
189 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Left
: R
.style
.Animations_PopDownMenu_Left
);
192 case ANIM_GROW_FROM_RIGHT
:
193 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Right
: R
.style
.Animations_PopDownMenu_Right
);
196 case ANIM_GROW_FROM_CENTER
:
197 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Center
: R
.style
.Animations_PopDownMenu_Center
);
201 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Reflect
: R
.style
.Animations_PopDownMenu_Reflect
);
205 if (arrowPos
<= screenWidth
/4) {
206 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Left
: R
.style
.Animations_PopDownMenu_Left
);
207 } else if (arrowPos
> screenWidth
/4 && arrowPos
< 3 * (screenWidth
/4)) {
208 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Center
: R
.style
.Animations_PopDownMenu_Center
);
210 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Right
: R
.style
.Animations_PopDownMenu_Right
);
220 private void createActionList() {
224 OnClickListener listener
;
226 for (int i
= 0; i
< actionList
.size(); i
++) {
227 title
= actionList
.get(i
).getTitle();
228 icon
= actionList
.get(i
).getIcon();
229 listener
= actionList
.get(i
).getOnClickListerner();
231 view
= getActionItem(title
, icon
, listener
);
233 view
.setFocusable(true
);
234 view
.setClickable(true
);
236 mTrack
.addView(view
);
241 * Get action item {@link View}
243 * @param title action item title
244 * @param icon {@link Drawable} action item icon
245 * @param listener {@link View.OnClickListener} action item listener
246 * @return action item {@link View}
248 private View
getActionItem(String title
, Drawable icon
, OnClickListener listener
) {
249 LinearLayout container
= (LinearLayout
) inflater
.inflate(R
.layout
.action_item
, null
);
251 ImageView img
= (ImageView
) container
.findViewById(R
.id
.icon
);
252 TextView text
= (TextView
) container
.findViewById(R
.id
.title
);
255 img
.setImageDrawable(icon
);
262 if (listener
!= null
) {
263 container
.setOnClickListener(listener
);
272 * @param whichArrow arrow type resource id
273 * @param requestedX distance from left screen
275 private void showArrow(int whichArrow
, int requestedX
) {
276 final View showArrow
= (whichArrow
== R
.id
.arrow_up
) ? mArrowUp
: mArrowDown
;
277 final View hideArrow
= (whichArrow
== R
.id
.arrow_up
) ? mArrowDown
: mArrowUp
;
279 final int arrowWidth
= mArrowUp
.getMeasuredWidth();
281 showArrow
.setVisibility(View
.VISIBLE
);
283 ViewGroup
.MarginLayoutParams param
= (ViewGroup
.MarginLayoutParams
)showArrow
.getLayoutParams();
285 param
.leftMargin
= requestedX
- arrowWidth
/ 2;
287 hideArrow
.setVisibility(View
.INVISIBLE
);