--- /dev/null
+/* ownCloud Android client application\r
+ * Copyright (C) 2011 Bartek Przybylski\r
+ *\r
+ * This program is free software: you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation, either version 3 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.\r
+ *\r
+ */\r
+package eu.alefzero.owncloud.ui;\r
+\r
+import android.content.Context;\r
+\r
+import android.graphics.Rect;\r
+import android.graphics.drawable.Drawable;\r
+\r
+import android.widget.ImageView;\r
+import android.widget.TextView;\r
+import android.widget.LinearLayout;\r
+import android.widget.ScrollView;\r
+\r
+import android.view.Gravity;\r
+import android.view.LayoutInflater;\r
+import android.view.View;\r
+import android.view.View.OnClickListener;\r
+import android.view.ViewGroup.LayoutParams;\r
+import android.view.ViewGroup;\r
+\r
+import java.util.ArrayList;\r
+\r
+import eu.alefzero.owncloud.R;\r
+import eu.alefzero.owncloud.R.id;\r
+import eu.alefzero.owncloud.R.layout;\r
+import eu.alefzero.owncloud.R.style;\r
+\r
+/**\r
+ * Popup window, shows action list as icon and text like the one in Gallery3D app. \r
+ * \r
+ * @author Lorensius. W. T\r
+ */\r
+public class QuickAction extends CustomPopup {\r
+ private final View root;\r
+ private final ImageView mArrowUp;\r
+ private final ImageView mArrowDown;\r
+ private final LayoutInflater inflater;\r
+ private final Context context;\r
+ \r
+ protected static final int ANIM_GROW_FROM_LEFT = 1;\r
+ protected static final int ANIM_GROW_FROM_RIGHT = 2;\r
+ protected static final int ANIM_GROW_FROM_CENTER = 3;\r
+ protected static final int ANIM_REFLECT = 4;\r
+ protected static final int ANIM_AUTO = 5;\r
+\r
+ private int animStyle;\r
+ private ViewGroup mTrack;\r
+ private ScrollView scroller;\r
+ private ArrayList<ActionItem> actionList;\r
+ \r
+ /**\r
+ * Constructor\r
+ * \r
+ * @param anchor {@link View} on where the popup window should be displayed\r
+ */\r
+ public QuickAction(View anchor) {\r
+ super(anchor);\r
+ \r
+ actionList = new ArrayList<ActionItem>();\r
+ context = anchor.getContext();\r
+ inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);\r
+ \r
+ root = (ViewGroup) inflater.inflate(R.layout.popup, null);\r
+ \r
+ mArrowDown = (ImageView) root.findViewById(R.id.arrow_down);\r
+ mArrowUp = (ImageView) root.findViewById(R.id.arrow_up);\r
+ \r
+ setContentView(root);\r
+ \r
+ mTrack = (ViewGroup) root.findViewById(R.id.tracks);\r
+ scroller = (ScrollView) root.findViewById(R.id.scroller);\r
+ animStyle = ANIM_AUTO;\r
+ }\r
+\r
+ /**\r
+ * Set animation style\r
+ * \r
+ * @param animStyle animation style, default is set to ANIM_AUTO\r
+ */\r
+ public void setAnimStyle(int animStyle) {\r
+ this.animStyle = animStyle;\r
+ }\r
+\r
+ /**\r
+ * Add action item\r
+ * \r
+ * @param action {@link ActionItem} object\r
+ */\r
+ public void addActionItem(ActionItem action) {\r
+ actionList.add(action); \r
+ }\r
+ \r
+ /**\r
+ * Show popup window. Popup is automatically positioned, on top or bottom of anchor view.\r
+ * \r
+ */\r
+ public void show () {\r
+ preShow();\r
+ \r
+ int xPos, yPos;\r
+ \r
+ int[] location = new int[2];\r
+ \r
+ mAnchor.getLocationOnScreen(location);\r
+\r
+ Rect anchorRect = new Rect(location[0], location[1], location[0] + mAnchor.getWidth(), location[1] \r
+ + mAnchor.getHeight());\r
+\r
+ createActionList();\r
+ \r
+ root.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));\r
+ root.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);\r
+ \r
+ int rootHeight = root.getMeasuredHeight();\r
+ int rootWidth = root.getMeasuredWidth();\r
+ \r
+ int screenWidth = mWManager.getDefaultDisplay().getWidth();\r
+ int screenHeight = mWManager.getDefaultDisplay().getHeight();\r
+ \r
+ //automatically get X coord of popup (top left)\r
+ if ((anchorRect.left + rootWidth) > screenWidth) {\r
+ xPos = anchorRect.left - (rootWidth-mAnchor.getWidth());\r
+ } else {\r
+ if (mAnchor.getWidth() > rootWidth) {\r
+ xPos = anchorRect.centerX() - (rootWidth/2);\r
+ } else {\r
+ xPos = anchorRect.left;\r
+ }\r
+ }\r
+ \r
+ int dyTop = anchorRect.top;\r
+ int dyBottom = screenHeight - anchorRect.bottom;\r
+\r
+ boolean onTop = (dyTop > dyBottom) ? true : false;\r
+\r
+ if (onTop) {\r
+ if (rootHeight > dyTop) {\r
+ yPos = 15;\r
+ LayoutParams l = scroller.getLayoutParams();\r
+ l.height = dyTop - mAnchor.getHeight();\r
+ } else {\r
+ yPos = anchorRect.top - rootHeight;\r
+ }\r
+ } else {\r
+ yPos = anchorRect.bottom;\r
+ \r
+ if (rootHeight > dyBottom) { \r
+ LayoutParams l = scroller.getLayoutParams();\r
+ l.height = dyBottom;\r
+ }\r
+ }\r
+ \r
+ showArrow(((onTop) ? R.id.arrow_down : R.id.arrow_up), anchorRect.centerX()-xPos);\r
+ \r
+ setAnimationStyle(screenWidth, anchorRect.centerX(), onTop);\r
+ \r
+ mWindow.showAtLocation(mAnchor, Gravity.NO_GRAVITY, xPos, yPos);\r
+ }\r
+ \r
+ /**\r
+ * Set animation style\r
+ * \r
+ * @param screenWidth screen width\r
+ * @param requestedX distance from left edge\r
+ * @param onTop flag to indicate where the popup should be displayed. Set TRUE if displayed on top of anchor view\r
+ * and vice versa\r
+ */\r
+ private void setAnimationStyle(int screenWidth, int requestedX, boolean onTop) {\r
+ int arrowPos = requestedX - mArrowUp.getMeasuredWidth()/2;\r
+\r
+ switch (animStyle) {\r
+ case ANIM_GROW_FROM_LEFT:\r
+ mWindow.setAnimationStyle((onTop) ? R.style.Animations_PopUpMenu_Left : R.style.Animations_PopDownMenu_Left);\r
+ break;\r
+ \r
+ case ANIM_GROW_FROM_RIGHT:\r
+ mWindow.setAnimationStyle((onTop) ? R.style.Animations_PopUpMenu_Right : R.style.Animations_PopDownMenu_Right);\r
+ break;\r
+ \r
+ case ANIM_GROW_FROM_CENTER:\r
+ mWindow.setAnimationStyle((onTop) ? R.style.Animations_PopUpMenu_Center : R.style.Animations_PopDownMenu_Center);\r
+ break;\r
+ \r
+ case ANIM_REFLECT:\r
+ mWindow.setAnimationStyle((onTop) ? R.style.Animations_PopUpMenu_Reflect : R.style.Animations_PopDownMenu_Reflect);\r
+ break;\r
+ \r
+ case ANIM_AUTO:\r
+ if (arrowPos <= screenWidth/4) {\r
+ mWindow.setAnimationStyle((onTop) ? R.style.Animations_PopUpMenu_Left : R.style.Animations_PopDownMenu_Left);\r
+ } else if (arrowPos > screenWidth/4 && arrowPos < 3 * (screenWidth/4)) {\r
+ mWindow.setAnimationStyle((onTop) ? R.style.Animations_PopUpMenu_Center : R.style.Animations_PopDownMenu_Center);\r
+ } else {\r
+ mWindow.setAnimationStyle((onTop) ? R.style.Animations_PopUpMenu_Right : R.style.Animations_PopDownMenu_Right);\r
+ }\r
+ \r
+ break;\r
+ }\r
+ }\r
+ \r
+ /**\r
+ * Create action list\r
+ */\r
+ private void createActionList() {\r
+ View view;\r
+ String title;\r
+ Drawable icon;\r
+ OnClickListener listener;\r
+ \r
+ for (int i = 0; i < actionList.size(); i++) {\r
+ title = actionList.get(i).getTitle();\r
+ icon = actionList.get(i).getIcon();\r
+ listener = actionList.get(i).getOnClickListerner();\r
+ \r
+ view = getActionItem(title, icon, listener);\r
+ \r
+ view.setFocusable(true);\r
+ view.setClickable(true);\r
+ \r
+ mTrack.addView(view);\r
+ }\r
+ }\r
+ \r
+ /**\r
+ * Get action item {@link View}\r
+ * \r
+ * @param title action item title\r
+ * @param icon {@link Drawable} action item icon\r
+ * @param listener {@link View.OnClickListener} action item listener\r
+ * @return action item {@link View}\r
+ */\r
+ private View getActionItem(String title, Drawable icon, OnClickListener listener) {\r
+ LinearLayout container = (LinearLayout) inflater.inflate(R.layout.action_item, null);\r
+ \r
+ ImageView img = (ImageView) container.findViewById(R.id.icon);\r
+ TextView text = (TextView) container.findViewById(R.id.title);\r
+ \r
+ if (icon != null) {\r
+ img.setImageDrawable(icon);\r
+ }\r
+ \r
+ if (title != null) { \r
+ text.setText(title);\r
+ }\r
+ \r
+ if (listener != null) {\r
+ container.setOnClickListener(listener);\r
+ }\r
+\r
+ return container;\r
+ }\r
+ \r
+ /**\r
+ * Show arrow\r
+ * \r
+ * @param whichArrow arrow type resource id\r
+ * @param requestedX distance from left screen\r
+ */\r
+ private void showArrow(int whichArrow, int requestedX) {\r
+ final View showArrow = (whichArrow == R.id.arrow_up) ? mArrowUp : mArrowDown;\r
+ final View hideArrow = (whichArrow == R.id.arrow_up) ? mArrowDown : mArrowUp;\r
+\r
+ final int arrowWidth = mArrowUp.getMeasuredWidth();\r
+\r
+ showArrow.setVisibility(View.VISIBLE);\r
+ \r
+ ViewGroup.MarginLayoutParams param = (ViewGroup.MarginLayoutParams)showArrow.getLayoutParams();\r
+ \r
+ param.leftMargin = requestedX - arrowWidth / 2;\r
+ \r
+ hideArrow.setVisibility(View.INVISIBLE);\r
+ }\r
+}
\ No newline at end of file