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
;
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
;
69 * {@link View} on where the popup window should be displayed
71 public QuickAction(View anchor
) {
74 actionList
= new ArrayList
<ActionItem
>();
75 context
= anchor
.getContext();
76 inflater
= (LayoutInflater
) context
77 .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
;
95 * animation style, default is set to ANIM_AUTO
97 public void setAnimStyle(int animStyle
) {
98 this.animStyle
= animStyle
;
105 * {@link ActionItem} object
107 public void addActionItem(ActionItem action
) {
108 actionList
.add(action
);
112 * Show popup window. Popup is automatically positioned, on top or bottom of
121 int[] location
= new int[2];
123 mAnchor
.getLocationOnScreen(location
);
125 Rect anchorRect
= new Rect(location
[0], location
[1], location
[0]
126 + mAnchor
.getWidth(), location
[1] + mAnchor
.getHeight());
130 root
.setLayoutParams(new LayoutParams(LayoutParams
.WRAP_CONTENT
,
131 LayoutParams
.WRAP_CONTENT
));
132 root
.measure(LayoutParams
.WRAP_CONTENT
, LayoutParams
.WRAP_CONTENT
);
134 int rootHeight
= root
.getMeasuredHeight();
135 int rootWidth
= root
.getMeasuredWidth();
137 int screenWidth
= mWManager
.getDefaultDisplay().getWidth();
138 int screenHeight
= mWManager
.getDefaultDisplay().getHeight();
140 // automatically get X coord of popup (top left)
141 if ((anchorRect
.left
+ rootWidth
) > screenWidth
) {
142 xPos
= anchorRect
.left
- (rootWidth
- mAnchor
.getWidth());
144 if (mAnchor
.getWidth() > rootWidth
) {
145 xPos
= anchorRect
.centerX() - (rootWidth
/ 2);
147 xPos
= anchorRect
.left
;
151 int dyTop
= anchorRect
.top
;
152 int dyBottom
= screenHeight
- anchorRect
.bottom
;
154 boolean onTop
= (dyTop
> dyBottom
) ? true
: false
;
157 if (rootHeight
> dyTop
) {
159 LayoutParams l
= scroller
.getLayoutParams();
160 l
.height
= dyTop
- mAnchor
.getHeight();
162 yPos
= anchorRect
.top
- rootHeight
;
165 yPos
= anchorRect
.bottom
;
167 if (rootHeight
> dyBottom
) {
168 LayoutParams l
= scroller
.getLayoutParams();
173 showArrow(((onTop
) ? R
.id
.arrow_down
: R
.id
.arrow_up
),
174 anchorRect
.centerX() - xPos
);
176 setAnimationStyle(screenWidth
, anchorRect
.centerX(), onTop
);
178 mWindow
.showAtLocation(mAnchor
, Gravity
.NO_GRAVITY
, xPos
, yPos
);
182 * Set animation style
187 * distance from left edge
189 * flag to indicate where the popup should be displayed. Set TRUE
190 * if displayed on top of anchor view and vice versa
192 private void setAnimationStyle(int screenWidth
, int requestedX
,
194 int arrowPos
= requestedX
- mArrowUp
.getMeasuredWidth() / 2;
197 case ANIM_GROW_FROM_LEFT
:
198 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Left
199 : R
.style
.Animations_PopDownMenu_Left
);
202 case ANIM_GROW_FROM_RIGHT
:
203 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Right
204 : R
.style
.Animations_PopDownMenu_Right
);
207 case ANIM_GROW_FROM_CENTER
:
208 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Center
209 : R
.style
.Animations_PopDownMenu_Center
);
213 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Reflect
214 : R
.style
.Animations_PopDownMenu_Reflect
);
218 if (arrowPos
<= screenWidth
/ 4) {
219 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Left
220 : R
.style
.Animations_PopDownMenu_Left
);
221 } else if (arrowPos
> screenWidth
/ 4
222 && arrowPos
< 3 * (screenWidth
/ 4)) {
223 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Center
224 : R
.style
.Animations_PopDownMenu_Center
);
226 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Right
227 : R
.style
.Animations_PopDownMenu_Right
);
237 private void createActionList() {
241 OnClickListener listener
;
243 for (int i
= 0; i
< actionList
.size(); i
++) {
244 title
= actionList
.get(i
).getTitle();
245 icon
= actionList
.get(i
).getIcon();
246 listener
= actionList
.get(i
).getOnClickListerner();
248 view
= getActionItem(title
, icon
, listener
);
250 view
.setFocusable(true
);
251 view
.setClickable(true
);
253 mTrack
.addView(view
);
258 * Get action item {@link View}
263 * {@link Drawable} action item icon
265 * {@link View.OnClickListener} action item listener
266 * @return action item {@link View}
268 private View
getActionItem(String title
, Drawable icon
,
269 OnClickListener listener
) {
270 LinearLayout container
= (LinearLayout
) inflater
.inflate(
271 R
.layout
.action_item
, null
);
273 ImageView img
= (ImageView
) container
.findViewById(R
.id
.icon
);
274 TextView text
= (TextView
) container
.findViewById(R
.id
.title
);
277 img
.setImageDrawable(icon
);
284 if (listener
!= null
) {
285 container
.setOnClickListener(listener
);
295 * arrow type resource id
297 * distance from left screen
299 private void showArrow(int whichArrow
, int requestedX
) {
300 final View showArrow
= (whichArrow
== R
.id
.arrow_up
) ? mArrowUp
302 final View hideArrow
= (whichArrow
== R
.id
.arrow_up
) ? mArrowDown
305 final int arrowWidth
= mArrowUp
.getMeasuredWidth();
307 showArrow
.setVisibility(View
.VISIBLE
);
309 ViewGroup
.MarginLayoutParams param
= (ViewGroup
.MarginLayoutParams
) showArrow
312 param
.leftMargin
= requestedX
- arrowWidth
/ 2;
314 hideArrow
.setVisibility(View
.INVISIBLE
);