Refactoring: Added comments to every class, as well as a copyright
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / CustomPopup.java
diff --git a/src/eu/alefzero/owncloud/ui/CustomPopup.java b/src/eu/alefzero/owncloud/ui/CustomPopup.java
new file mode 100644 (file)
index 0000000..7709f92
--- /dev/null
@@ -0,0 +1,148 @@
+/* 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
+import android.graphics.Rect;\r
+import android.graphics.drawable.BitmapDrawable;\r
+import android.graphics.drawable.Drawable;\r
+import android.view.Gravity;\r
+import android.view.LayoutInflater;\r
+import android.view.MotionEvent;\r
+import android.view.View;\r
+import android.view.WindowManager;\r
+import android.view.View.OnTouchListener;\r
+import android.view.ViewGroup.LayoutParams;\r
+import android.widget.PopupWindow;\r
+\r
+/**\r
+ * Represents a custom PopupWindows\r
+ * @author Lorensius. W. T\r
+ *\r
+ */\r
+public class CustomPopup {\r
+  protected final View mAnchor;\r
+  protected final PopupWindow mWindow;\r
+  private View root;\r
+  private Drawable background = null;\r
+  protected final WindowManager mWManager;\r
+  \r
+  public CustomPopup(View anchor) {\r
+    mAnchor = anchor;\r
+    mWindow = new PopupWindow(anchor.getContext());\r
+    \r
+    mWindow.setTouchInterceptor(new OnTouchListener() {\r
+      \r
+      public boolean onTouch(View v, MotionEvent event) {\r
+        if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {\r
+          CustomPopup.this.dismiss();\r
+          return true;\r
+        }\r
+        return false;\r
+      }\r
+    });\r
+    \r
+    mWManager = (WindowManager) anchor.getContext().getSystemService(Context.WINDOW_SERVICE);\r
+    onCreate();\r
+  }\r
+  \r
+  \r
+  public void onCreate() {}\r
+  public void onShow() {}\r
+  \r
+  public void preShow() {\r
+    if (root == null) {\r
+      throw new IllegalStateException("setContentView called with a view to display");\r
+    }\r
+    \r
+    onShow();\r
+    \r
+    if (background == null) {\r
+      mWindow.setBackgroundDrawable(new BitmapDrawable());\r
+    } else {\r
+      mWindow.setBackgroundDrawable(background);\r
+    }\r
+    \r
+    mWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);\r
+    mWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);\r
+    mWindow.setTouchable(true);\r
+    mWindow.setFocusable(true);\r
+    mWindow.setOutsideTouchable(true);\r
+    \r
+    mWindow.setContentView(root);\r
+  }\r
+  \r
+  public void setBackgroundDrawable(Drawable background) {\r
+    this.background = background;\r
+  }\r
+  \r
+  public void setContentView(View root) {\r
+    this.root = root;\r
+    mWindow.setContentView(root);\r
+  }\r
+  \r
+  public void setContentView(int layoutResId) {\r
+    LayoutInflater inflater = \r
+      (LayoutInflater) mAnchor.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);\r
+    setContentView(inflater.inflate(layoutResId, null));\r
+  }\r
+  \r
+  public void showDropDown() {\r
+    showDropDown(0, 0);\r
+  }\r
+  \r
+  public void showDropDown(int x, int y) {\r
+    preShow();\r
+    mWindow.setAnimationStyle(android.R.style.Animation_Dialog);\r
+    mWindow.showAsDropDown(mAnchor, x, y);\r
+  }\r
+  \r
+  public void showLikeQuickAction() {\r
+    showLikeQuickAction(0, 0);\r
+  }\r
+  \r
+  public void showLikeQuickAction(int x, int y) {\r
+    preShow();\r
+    \r
+    mWindow.setAnimationStyle(android.R.style.Animation_Dialog);\r
+    int[] location = new int[2];\r
+    mAnchor.getLocationOnScreen(location);\r
+    \r
+    Rect anchorRect = \r
+      new Rect(location[0], location[1], location[0] + mAnchor.getWidth(), location[1] + mAnchor.getHeight());\r
+    \r
+    root.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));\r
+    root.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);\r
+    \r
+    int rootW = root.getWidth(), rootH = root.getHeight();\r
+    int screenW = mWManager.getDefaultDisplay().getWidth();\r
+    \r
+    int xpos = ((screenW-rootW)/2) + x;\r
+    int ypos = anchorRect.top - rootH + y;\r
+    \r
+    if (rootH > anchorRect.top) {\r
+      ypos = anchorRect.bottom + y;\r
+    }\r
+    mWindow.showAtLocation(mAnchor, Gravity.NO_GRAVITY, xpos, ypos);\r
+  }\r
+  \r
+  public void dismiss() {\r
+    mWindow.dismiss();\r
+  }\r
+  \r
+}\r