mobile/android/base/widget/ArrowPopup.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 package org.mozilla.gecko.widget;
michael@0 7
michael@0 8 import org.mozilla.gecko.GeckoApp;
michael@0 9 import org.mozilla.gecko.R;
michael@0 10 import org.mozilla.gecko.util.HardwareUtils;
michael@0 11
michael@0 12 import android.content.res.Resources;
michael@0 13 import android.graphics.drawable.BitmapDrawable;
michael@0 14 import android.os.Build;
michael@0 15 import android.view.Gravity;
michael@0 16 import android.view.LayoutInflater;
michael@0 17 import android.view.View;
michael@0 18 import android.view.ViewGroup;
michael@0 19 import android.widget.ImageView;
michael@0 20 import android.widget.LinearLayout;
michael@0 21 import android.widget.PopupWindow;
michael@0 22 import android.widget.RelativeLayout;
michael@0 23
michael@0 24 public class ArrowPopup extends PopupWindow {
michael@0 25 protected final GeckoApp mActivity;
michael@0 26
michael@0 27 private View mAnchor;
michael@0 28 private ImageView mArrow;
michael@0 29
michael@0 30 private int mArrowWidth;
michael@0 31 private int mYOffset;
michael@0 32
michael@0 33 protected LinearLayout mContent;
michael@0 34 protected boolean mInflated;
michael@0 35
michael@0 36 public ArrowPopup(GeckoApp aActivity) {
michael@0 37 this(aActivity, null);
michael@0 38 }
michael@0 39
michael@0 40 public ArrowPopup(GeckoApp activity, View anchor) {
michael@0 41 super(activity);
michael@0 42 mActivity = activity;
michael@0 43 mAnchor = anchor;
michael@0 44
michael@0 45 mInflated = false;
michael@0 46
michael@0 47 final Resources res = activity.getResources();
michael@0 48 mArrowWidth = res.getDimensionPixelSize(R.dimen.menu_popup_arrow_width);
michael@0 49 mYOffset = res.getDimensionPixelSize(R.dimen.menu_popup_arrow_offset);
michael@0 50
michael@0 51 setAnimationStyle(R.style.PopupAnimation);
michael@0 52 }
michael@0 53
michael@0 54 public void setAnchor(View anchor) {
michael@0 55 mAnchor = anchor;
michael@0 56 }
michael@0 57
michael@0 58 protected void init() {
michael@0 59 setBackgroundDrawable(new BitmapDrawable());
michael@0 60 setOutsideTouchable(true);
michael@0 61
michael@0 62 setWindowLayoutMode(HardwareUtils.isTablet() ? ViewGroup.LayoutParams.WRAP_CONTENT : ViewGroup.LayoutParams.FILL_PARENT,
michael@0 63 ViewGroup.LayoutParams.WRAP_CONTENT);
michael@0 64
michael@0 65 LayoutInflater inflater = LayoutInflater.from(mActivity);
michael@0 66 RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.arrow_popup, null);
michael@0 67 setContentView(layout);
michael@0 68
michael@0 69 mArrow = (ImageView) layout.findViewById(R.id.arrow);
michael@0 70 mContent = (LinearLayout) layout.findViewById(R.id.content);
michael@0 71
michael@0 72 mInflated = true;
michael@0 73 }
michael@0 74
michael@0 75 /*
michael@0 76 * Shows the popup with the arrow pointing to the center of the anchor view. If an anchor hasn't
michael@0 77 * been set or isn't visible, the popup will just be shown at the top of the gecko app view.
michael@0 78 */
michael@0 79 public void show() {
michael@0 80 int[] anchorLocation = new int[2];
michael@0 81 if (mAnchor != null)
michael@0 82 mAnchor.getLocationInWindow(anchorLocation);
michael@0 83
michael@0 84 // If there's no anchor or the anchor is out of the window bounds,
michael@0 85 // just show the popup at the top of the gecko app view.
michael@0 86 if (mAnchor == null || anchorLocation[1] < 0) {
michael@0 87 final View view = mActivity.getView();
michael@0 88
michael@0 89 // Bug in android code causes the window layout parameters to be ignored
michael@0 90 // when using showAtLocation() in Gingerbread phones.
michael@0 91 if (Build.VERSION.SDK_INT < 11) {
michael@0 92 setWidth(view.getWidth());
michael@0 93 setHeight(view.getHeight());
michael@0 94 }
michael@0 95
michael@0 96 showAtLocation(view, Gravity.TOP, 0, 0);
michael@0 97 return;
michael@0 98 }
michael@0 99
michael@0 100 // Remove padding from the width of the anchor when calculating the arrow offset.
michael@0 101 int anchorWidth = mAnchor.getWidth() - mAnchor.getPaddingLeft() - mAnchor.getPaddingRight();
michael@0 102 // This is the difference between the edge of the anchor view and the edge of the arrow view.
michael@0 103 // We're making an assumption here that the anchor view is wider than the arrow view.
michael@0 104 int arrowOffset = (anchorWidth - mArrowWidth)/2 + mAnchor.getPaddingLeft();
michael@0 105
michael@0 106 // The horizontal offset of the popup window, relative to the left side of the anchor view.
michael@0 107 int offset = 0;
michael@0 108
michael@0 109 RelativeLayout.LayoutParams arrowLayoutParams = (RelativeLayout.LayoutParams) mArrow.getLayoutParams();
michael@0 110
michael@0 111 if (HardwareUtils.isTablet()) {
michael@0 112 // On tablets, the popup has a fixed width, so we use a horizontal offset to position it.
michael@0 113 // The arrow's left margin is set by the arrow_popup.xml layout file.
michael@0 114 // This assumes that anchor is not too close to the right side of the screen.
michael@0 115 offset = arrowOffset - arrowLayoutParams.leftMargin;
michael@0 116 } else {
michael@0 117 // On phones, the popup takes up the width of the screen, so we set the arrow's left
michael@0 118 // margin to make it line up with the anchor.
michael@0 119 int leftMargin = anchorLocation[0] + arrowOffset;
michael@0 120 arrowLayoutParams.setMargins(leftMargin, 0, 0, 0);
michael@0 121 }
michael@0 122
michael@0 123 if (isShowing()) {
michael@0 124 update(mAnchor, offset, -mYOffset, -1, -1);
michael@0 125 } else {
michael@0 126 showAsDropDown(mAnchor, offset, -mYOffset);
michael@0 127 }
michael@0 128 }
michael@0 129 }

mercurial