1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.support.v4.widget;
18
19import android.view.View;
20import android.view.View.OnTouchListener;
21import android.widget.PopupWindow;
22
23/**
24 * Helper for accessing features in PopupWindow introduced after API level 4
25 * in a backwards compatible fashion.
26 */
27public class PopupWindowCompat {
28    /**
29     * Interface for the full API.
30     */
31    interface PopupWindowImpl {
32        public void showAsDropDown(PopupWindow popup, View anchor, int xoff, int yoff,
33                int gravity);
34    }
35
36    /**
37     * Interface implementation that doesn't use anything above v4 APIs.
38     */
39    static class BasePopupWindowImpl implements PopupWindowImpl {
40        @Override
41        public void showAsDropDown(PopupWindow popup, View anchor, int xoff, int yoff,
42                int gravity) {
43            popup.showAsDropDown(anchor, xoff, yoff);
44        }
45    }
46
47    /**
48     * Interface implementation for devices with at least KitKat APIs.
49     */
50    static class KitKatPopupWindowImpl extends BasePopupWindowImpl {
51        @Override
52        public void showAsDropDown(PopupWindow popup, View anchor, int xoff, int yoff,
53                int gravity) {
54            PopupWindowCompatKitKat.showAsDropDown(popup, anchor, xoff, yoff, gravity);
55        }
56    }
57
58    /**
59     * Select the correct implementation to use for the current platform.
60     */
61    static final PopupWindowImpl IMPL;
62    static {
63        final int version = android.os.Build.VERSION.SDK_INT;
64        if (version >= 19) {
65            IMPL = new KitKatPopupWindowImpl();
66        } else {
67            IMPL = new BasePopupWindowImpl();
68        }
69    }
70
71    private PopupWindowCompat() {
72        // This class is not publicly instantiable.
73    }
74
75    /**
76     * <p>Display the content view in a popup window anchored to the bottom-left
77     * corner of the anchor view offset by the specified x and y coordinates.
78     * If there is not enough room on screen to show
79     * the popup in its entirety, this method tries to find a parent scroll
80     * view to scroll. If no parent scroll view can be scrolled, the bottom-left
81     * corner of the popup is pinned at the top left corner of the anchor view.</p>
82     * <p>If the view later scrolls to move <code>anchor</code> to a different
83     * location, the popup will be moved correspondingly.</p>
84     *
85     * @param popup the PopupWindow to show
86     * @param anchor the view on which to pin the popup window
87     * @param xoff A horizontal offset from the anchor in pixels
88     * @param yoff A vertical offset from the anchor in pixels
89     * @param gravity Alignment of the popup relative to the anchor
90     */
91    public static void showAsDropDown(PopupWindow popup, View anchor, int xoff, int yoff,
92            int gravity) {
93        IMPL.showAsDropDown(popup, anchor, xoff, yoff, gravity);
94    }
95}
96