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 app.
44 * @author Lorensius. W. T
46 public class QuickAction
extends CustomPopup
{
47 private final View root
;
48 private final ImageView mArrowUp
;
49 private final ImageView mArrowDown
;
50 private final LayoutInflater inflater
;
51 private final Context context
;
53 protected static final int ANIM_GROW_FROM_LEFT
= 1;
54 protected static final int ANIM_GROW_FROM_RIGHT
= 2;
55 protected static final int ANIM_GROW_FROM_CENTER
= 3;
56 protected static final int ANIM_REFLECT
= 4;
57 protected static final int ANIM_AUTO
= 5;
59 private int animStyle
;
60 private ViewGroup mTrack
;
61 private ScrollView scroller
;
62 private ArrayList
<ActionItem
> actionList
;
67 * @param anchor {@link View} on where the popup window should be displayed
69 public QuickAction(View anchor
) {
72 actionList
= new ArrayList
<ActionItem
>();
73 context
= anchor
.getContext();
74 inflater
= (LayoutInflater
) context
.getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
76 root
= (ViewGroup
) inflater
.inflate(R
.layout
.popup
, null
);
78 mArrowDown
= (ImageView
) root
.findViewById(R
.id
.arrow_down
);
79 mArrowUp
= (ImageView
) root
.findViewById(R
.id
.arrow_up
);
83 mTrack
= (ViewGroup
) root
.findViewById(R
.id
.tracks
);
84 scroller
= (ScrollView
) root
.findViewById(R
.id
.scroller
);
85 animStyle
= ANIM_AUTO
;
91 * @param animStyle animation style, default is set to ANIM_AUTO
93 public void setAnimStyle(int animStyle
) {
94 this.animStyle
= animStyle
;
100 * @param action {@link ActionItem} object
102 public void addActionItem(ActionItem action
) {
103 actionList
.add(action
);
107 * Show popup window. Popup is automatically positioned, on top or bottom of anchor view.
110 public void show () {
115 int[] location
= new int[2];
117 mAnchor
.getLocationOnScreen(location
);
119 Rect anchorRect
= new Rect(location
[0], location
[1], location
[0] + mAnchor
.getWidth(), location
[1]
120 + mAnchor
.getHeight());
124 root
.setLayoutParams(new LayoutParams(LayoutParams
.WRAP_CONTENT
, LayoutParams
.WRAP_CONTENT
));
125 root
.measure(LayoutParams
.WRAP_CONTENT
, LayoutParams
.WRAP_CONTENT
);
127 int rootHeight
= root
.getMeasuredHeight();
128 int rootWidth
= root
.getMeasuredWidth();
130 int screenWidth
= mWManager
.getDefaultDisplay().getWidth();
131 int screenHeight
= mWManager
.getDefaultDisplay().getHeight();
133 //automatically get X coord of popup (top left)
134 if ((anchorRect
.left
+ rootWidth
) > screenWidth
) {
135 xPos
= anchorRect
.left
- (rootWidth
-mAnchor
.getWidth());
137 if (mAnchor
.getWidth() > rootWidth
) {
138 xPos
= anchorRect
.centerX() - (rootWidth
/2);
140 xPos
= anchorRect
.left
;
144 int dyTop
= anchorRect
.top
;
145 int dyBottom
= screenHeight
- anchorRect
.bottom
;
147 boolean onTop
= (dyTop
> dyBottom
) ? true
: false
;
150 if (rootHeight
> dyTop
) {
152 LayoutParams l
= scroller
.getLayoutParams();
153 l
.height
= dyTop
- mAnchor
.getHeight();
155 yPos
= anchorRect
.top
- rootHeight
;
158 yPos
= anchorRect
.bottom
;
160 if (rootHeight
> dyBottom
) {
161 LayoutParams l
= scroller
.getLayoutParams();
166 showArrow(((onTop
) ? R
.id
.arrow_down
: R
.id
.arrow_up
), anchorRect
.centerX()-xPos
);
168 setAnimationStyle(screenWidth
, anchorRect
.centerX(), onTop
);
170 mWindow
.showAtLocation(mAnchor
, Gravity
.NO_GRAVITY
, xPos
, yPos
);
174 * Set animation style
176 * @param screenWidth screen width
177 * @param requestedX distance from left edge
178 * @param onTop flag to indicate where the popup should be displayed. Set TRUE if displayed on top of anchor view
181 private void setAnimationStyle(int screenWidth
, int requestedX
, boolean onTop
) {
182 int arrowPos
= requestedX
- mArrowUp
.getMeasuredWidth()/2;
185 case ANIM_GROW_FROM_LEFT
:
186 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Left
: R
.style
.Animations_PopDownMenu_Left
);
189 case ANIM_GROW_FROM_RIGHT
:
190 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Right
: R
.style
.Animations_PopDownMenu_Right
);
193 case ANIM_GROW_FROM_CENTER
:
194 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Center
: R
.style
.Animations_PopDownMenu_Center
);
198 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Reflect
: R
.style
.Animations_PopDownMenu_Reflect
);
202 if (arrowPos
<= screenWidth
/4) {
203 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Left
: R
.style
.Animations_PopDownMenu_Left
);
204 } else if (arrowPos
> screenWidth
/4 && arrowPos
< 3 * (screenWidth
/4)) {
205 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Center
: R
.style
.Animations_PopDownMenu_Center
);
207 mWindow
.setAnimationStyle((onTop
) ? R
.style
.Animations_PopUpMenu_Right
: R
.style
.Animations_PopDownMenu_Right
);
217 private void createActionList() {
221 OnClickListener listener
;
223 for (int i
= 0; i
< actionList
.size(); i
++) {
224 title
= actionList
.get(i
).getTitle();
225 icon
= actionList
.get(i
).getIcon();
226 listener
= actionList
.get(i
).getOnClickListerner();
228 view
= getActionItem(title
, icon
, listener
);
230 view
.setFocusable(true
);
231 view
.setClickable(true
);
233 mTrack
.addView(view
);
238 * Get action item {@link View}
240 * @param title action item title
241 * @param icon {@link Drawable} action item icon
242 * @param listener {@link View.OnClickListener} action item listener
243 * @return action item {@link View}
245 private View
getActionItem(String title
, Drawable icon
, OnClickListener listener
) {
246 LinearLayout container
= (LinearLayout
) inflater
.inflate(R
.layout
.action_item
, null
);
248 ImageView img
= (ImageView
) container
.findViewById(R
.id
.icon
);
249 TextView text
= (TextView
) container
.findViewById(R
.id
.title
);
252 img
.setImageDrawable(icon
);
259 if (listener
!= null
) {
260 container
.setOnClickListener(listener
);
269 * @param whichArrow arrow type resource id
270 * @param requestedX distance from left screen
272 private void showArrow(int whichArrow
, int requestedX
) {
273 final View showArrow
= (whichArrow
== R
.id
.arrow_up
) ? mArrowUp
: mArrowDown
;
274 final View hideArrow
= (whichArrow
== R
.id
.arrow_up
) ? mArrowDown
: mArrowUp
;
276 final int arrowWidth
= mArrowUp
.getMeasuredWidth();
278 showArrow
.setVisibility(View
.VISIBLE
);
280 ViewGroup
.MarginLayoutParams param
= (ViewGroup
.MarginLayoutParams
)showArrow
.getLayoutParams();
282 param
.leftMargin
= requestedX
- arrowWidth
/ 2;
284 hideArrow
.setVisibility(View
.INVISIBLE
);