d1974230bfdb95587c4b779991370f5c730ef937
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / QuickAction.java
1 package eu.alefzero.owncloud;
2
3 import android.content.Context;
4
5 import android.graphics.Rect;
6 import android.graphics.drawable.Drawable;
7
8 import android.widget.ImageView;
9 import android.widget.TextView;
10 import android.widget.LinearLayout;
11 import android.widget.ScrollView;
12
13 import android.view.Gravity;
14 import android.view.LayoutInflater;
15 import android.view.View;
16 import android.view.View.OnClickListener;
17 import android.view.ViewGroup.LayoutParams;
18 import android.view.ViewGroup;
19
20 import java.util.ArrayList;
21
22 /**
23 * Popup window, shows action list as icon and text like the one in Gallery3D app.
24 *
25 * @author Lorensius. W. T
26 */
27 public class QuickAction extends CustomPopup {
28 private final View root;
29 private final ImageView mArrowUp;
30 private final ImageView mArrowDown;
31 private final LayoutInflater inflater;
32 private final Context context;
33
34 protected static final int ANIM_GROW_FROM_LEFT = 1;
35 protected static final int ANIM_GROW_FROM_RIGHT = 2;
36 protected static final int ANIM_GROW_FROM_CENTER = 3;
37 protected static final int ANIM_REFLECT = 4;
38 protected static final int ANIM_AUTO = 5;
39
40 private int animStyle;
41 private ViewGroup mTrack;
42 private ScrollView scroller;
43 private ArrayList<ActionItem> actionList;
44
45 /**
46 * Constructor
47 *
48 * @param anchor {@link View} on where the popup window should be displayed
49 */
50 public QuickAction(View anchor) {
51 super(anchor);
52
53 actionList = new ArrayList<ActionItem>();
54 context = anchor.getContext();
55 inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
56
57 root = (ViewGroup) inflater.inflate(R.layout.popup, null);
58
59 mArrowDown = (ImageView) root.findViewById(R.id.arrow_down);
60 mArrowUp = (ImageView) root.findViewById(R.id.arrow_up);
61
62 setContentView(root);
63
64 mTrack = (ViewGroup) root.findViewById(R.id.tracks);
65 scroller = (ScrollView) root.findViewById(R.id.scroller);
66 animStyle = ANIM_AUTO;
67 }
68
69 /**
70 * Set animation style
71 *
72 * @param animStyle animation style, default is set to ANIM_AUTO
73 */
74 public void setAnimStyle(int animStyle) {
75 this.animStyle = animStyle;
76 }
77
78 /**
79 * Add action item
80 *
81 * @param action {@link ActionItem} object
82 */
83 public void addActionItem(ActionItem action) {
84 actionList.add(action);
85 }
86
87 /**
88 * Show popup window. Popup is automatically positioned, on top or bottom of anchor view.
89 *
90 */
91 public void show () {
92 preShow();
93
94 int xPos, yPos;
95
96 int[] location = new int[2];
97
98 mAnchor.getLocationOnScreen(location);
99
100 Rect anchorRect = new Rect(location[0], location[1], location[0] + mAnchor.getWidth(), location[1]
101 + mAnchor.getHeight());
102
103 createActionList();
104
105 root.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
106 root.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
107
108 int rootHeight = root.getMeasuredHeight();
109 int rootWidth = root.getMeasuredWidth();
110
111 int screenWidth = mWManager.getDefaultDisplay().getWidth();
112 int screenHeight = mWManager.getDefaultDisplay().getHeight();
113
114 //automatically get X coord of popup (top left)
115 if ((anchorRect.left + rootWidth) > screenWidth) {
116 xPos = anchorRect.left - (rootWidth-mAnchor.getWidth());
117 } else {
118 if (mAnchor.getWidth() > rootWidth) {
119 xPos = anchorRect.centerX() - (rootWidth/2);
120 } else {
121 xPos = anchorRect.left;
122 }
123 }
124
125 int dyTop = anchorRect.top;
126 int dyBottom = screenHeight - anchorRect.bottom;
127
128 boolean onTop = (dyTop > dyBottom) ? true : false;
129
130 if (onTop) {
131 if (rootHeight > dyTop) {
132 yPos = 15;
133 LayoutParams l = scroller.getLayoutParams();
134 l.height = dyTop - mAnchor.getHeight();
135 } else {
136 yPos = anchorRect.top - rootHeight;
137 }
138 } else {
139 yPos = anchorRect.bottom;
140
141 if (rootHeight > dyBottom) {
142 LayoutParams l = scroller.getLayoutParams();
143 l.height = dyBottom;
144 }
145 }
146
147 showArrow(((onTop) ? R.id.arrow_down : R.id.arrow_up), anchorRect.centerX()-xPos);
148
149 setAnimationStyle(screenWidth, anchorRect.centerX(), onTop);
150
151 mWindow.showAtLocation(mAnchor, Gravity.NO_GRAVITY, xPos, yPos);
152 }
153
154 /**
155 * Set animation style
156 *
157 * @param screenWidth screen width
158 * @param requestedX distance from left edge
159 * @param onTop flag to indicate where the popup should be displayed. Set TRUE if displayed on top of anchor view
160 * and vice versa
161 */
162 private void setAnimationStyle(int screenWidth, int requestedX, boolean onTop) {
163 int arrowPos = requestedX - mArrowUp.getMeasuredWidth()/2;
164
165 switch (animStyle) {
166 case ANIM_GROW_FROM_LEFT:
167 mWindow.setAnimationStyle((onTop) ? R.style.Animations_PopUpMenu_Left : R.style.Animations_PopDownMenu_Left);
168 break;
169
170 case ANIM_GROW_FROM_RIGHT:
171 mWindow.setAnimationStyle((onTop) ? R.style.Animations_PopUpMenu_Right : R.style.Animations_PopDownMenu_Right);
172 break;
173
174 case ANIM_GROW_FROM_CENTER:
175 mWindow.setAnimationStyle((onTop) ? R.style.Animations_PopUpMenu_Center : R.style.Animations_PopDownMenu_Center);
176 break;
177
178 case ANIM_REFLECT:
179 mWindow.setAnimationStyle((onTop) ? R.style.Animations_PopUpMenu_Reflect : R.style.Animations_PopDownMenu_Reflect);
180 break;
181
182 case ANIM_AUTO:
183 if (arrowPos <= screenWidth/4) {
184 mWindow.setAnimationStyle((onTop) ? R.style.Animations_PopUpMenu_Left : R.style.Animations_PopDownMenu_Left);
185 } else if (arrowPos > screenWidth/4 && arrowPos < 3 * (screenWidth/4)) {
186 mWindow.setAnimationStyle((onTop) ? R.style.Animations_PopUpMenu_Center : R.style.Animations_PopDownMenu_Center);
187 } else {
188 mWindow.setAnimationStyle((onTop) ? R.style.Animations_PopUpMenu_Right : R.style.Animations_PopDownMenu_Right);
189 }
190
191 break;
192 }
193 }
194
195 /**
196 * Create action list
197 */
198 private void createActionList() {
199 View view;
200 String title;
201 Drawable icon;
202 OnClickListener listener;
203
204 for (int i = 0; i < actionList.size(); i++) {
205 title = actionList.get(i).getTitle();
206 icon = actionList.get(i).getIcon();
207 listener = actionList.get(i).getOnClickListerner();
208
209 view = getActionItem(title, icon, listener);
210
211 view.setFocusable(true);
212 view.setClickable(true);
213
214 mTrack.addView(view);
215 }
216 }
217
218 /**
219 * Get action item {@link View}
220 *
221 * @param title action item title
222 * @param icon {@link Drawable} action item icon
223 * @param listener {@link View.OnClickListener} action item listener
224 * @return action item {@link View}
225 */
226 private View getActionItem(String title, Drawable icon, OnClickListener listener) {
227 LinearLayout container = (LinearLayout) inflater.inflate(R.layout.action_item, null);
228
229 ImageView img = (ImageView) container.findViewById(R.id.icon);
230 TextView text = (TextView) container.findViewById(R.id.title);
231
232 if (icon != null) {
233 img.setImageDrawable(icon);
234 }
235
236 if (title != null) {
237 text.setText(title);
238 }
239
240 if (listener != null) {
241 container.setOnClickListener(listener);
242 }
243
244 return container;
245 }
246
247 /**
248 * Show arrow
249 *
250 * @param whichArrow arrow type resource id
251 * @param requestedX distance from left screen
252 */
253 private void showArrow(int whichArrow, int requestedX) {
254 final View showArrow = (whichArrow == R.id.arrow_up) ? mArrowUp : mArrowDown;
255 final View hideArrow = (whichArrow == R.id.arrow_up) ? mArrowDown : mArrowUp;
256
257 final int arrowWidth = mArrowUp.getMeasuredWidth();
258
259 showArrow.setVisibility(View.VISIBLE);
260
261 ViewGroup.MarginLayoutParams param = (ViewGroup.MarginLayoutParams)showArrow.getLayoutParams();
262
263 param.leftMargin = requestedX - arrowWidth / 2;
264
265 hideArrow.setVisibility(View.INVISIBLE);
266 }
267 }