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