Fixed: Rotating screen after viewing a file would return you to the
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / QuickAction.java
index 9e146b8..e1d97d2 100644 (file)
@@ -39,248 +39,267 @@ import java.util.ArrayList;
 import eu.alefzero.owncloud.R;\r
 \r
 /**\r
- * Popup window, shows action list as icon and text like the one in Gallery3D app. \r
+ * Popup window, shows action list as icon and text like the one in Gallery3D\r
+ * 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
+    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\r
+                .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\r
+     * 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]\r
+                + mAnchor.getWidth(), location[1] + mAnchor.getHeight());\r
+\r
+        createActionList();\r
+\r
+        root.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,\r
+                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),\r
+                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\r
+     *            TRUE if displayed on top of anchor view and vice versa\r
+     */\r
+    private void setAnimationStyle(int screenWidth, int requestedX,\r
+            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
+                    : R.style.Animations_PopDownMenu_Left);\r
+            break;\r
+\r
+        case ANIM_GROW_FROM_RIGHT:\r
+            mWindow.setAnimationStyle((onTop) ? R.style.Animations_PopUpMenu_Right\r
+                    : R.style.Animations_PopDownMenu_Right);\r
+            break;\r
+\r
+        case ANIM_GROW_FROM_CENTER:\r
+            mWindow.setAnimationStyle((onTop) ? R.style.Animations_PopUpMenu_Center\r
+                    : R.style.Animations_PopDownMenu_Center);\r
+            break;\r
+\r
+        case ANIM_REFLECT:\r
+            mWindow.setAnimationStyle((onTop) ? R.style.Animations_PopUpMenu_Reflect\r
+                    : 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
+                        : R.style.Animations_PopDownMenu_Left);\r
+            } else if (arrowPos > screenWidth / 4\r
+                    && arrowPos < 3 * (screenWidth / 4)) {\r
+                mWindow.setAnimationStyle((onTop) ? R.style.Animations_PopUpMenu_Center\r
+                        : R.style.Animations_PopDownMenu_Center);\r
+            } else {\r
+                mWindow.setAnimationStyle((onTop) ? R.style.Animations_PopUpMenu_Right\r
+                        : 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,\r
+            OnClickListener listener) {\r
+        LinearLayout container = (LinearLayout) inflater.inflate(\r
+                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\r
+                : mArrowDown;\r
+        final View hideArrow = (whichArrow == R.id.arrow_up) ? mArrowDown\r
+                : 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
+\r
+        ViewGroup.MarginLayoutParams param = (ViewGroup.MarginLayoutParams) showArrow\r
+                .getLayoutParams();\r
+\r
         param.leftMargin = requestedX - arrowWidth / 2;\r
-        \r
+\r
         hideArrow.setVisibility(View.INVISIBLE);\r
     }\r
 }
\ No newline at end of file