1/*
2 * Copyright (C) 2014 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.v7.widget;
18
19import android.annotation.TargetApi;
20import android.content.Context;
21import android.os.Build;
22import android.support.annotation.AttrRes;
23import android.support.annotation.NonNull;
24import android.support.annotation.Nullable;
25import android.support.annotation.StyleRes;
26import android.support.v4.widget.PopupWindowCompat;
27import android.support.v7.appcompat.R;
28import android.util.AttributeSet;
29import android.util.Log;
30import android.view.View;
31import android.view.ViewTreeObserver.OnScrollChangedListener;
32import android.widget.PopupWindow;
33
34import java.lang.ref.WeakReference;
35import java.lang.reflect.Field;
36
37class AppCompatPopupWindow extends PopupWindow {
38
39    private static final String TAG = "AppCompatPopupWindow";
40    private static final boolean COMPAT_OVERLAP_ANCHOR = Build.VERSION.SDK_INT < 21;
41
42    private boolean mOverlapAnchor;
43
44    public AppCompatPopupWindow(@NonNull Context context, @Nullable AttributeSet attrs,
45            @AttrRes int defStyleAttr) {
46        super(context, attrs, defStyleAttr);
47        init(context, attrs, defStyleAttr, 0);
48    }
49
50    @TargetApi(11)
51    public AppCompatPopupWindow(@NonNull Context context, @Nullable AttributeSet attrs,
52            @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
53        super(context, attrs, defStyleAttr, defStyleRes);
54        init(context, attrs, defStyleAttr, defStyleRes);
55    }
56
57    private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
58        TintTypedArray a = TintTypedArray.obtainStyledAttributes(context, attrs,
59                R.styleable.PopupWindow, defStyleAttr, defStyleRes);
60        if (a.hasValue(R.styleable.PopupWindow_overlapAnchor)) {
61            setSupportOverlapAnchor(a.getBoolean(R.styleable.PopupWindow_overlapAnchor, false));
62        }
63        // We re-set this for tinting purposes
64        setBackgroundDrawable(a.getDrawable(R.styleable.PopupWindow_android_popupBackground));
65
66        final int sdk = Build.VERSION.SDK_INT;
67        if (defStyleRes != 0 && sdk < 11) {
68            // If we have a defStyleRes, but we're on < API 11, we need to manually set attributes
69            // from the style
70            if (sdk >= 9) {
71                // android:popupAnimationStyle was added in API 9
72                if (a.hasValue(R.styleable.PopupWindow_android_popupAnimationStyle)) {
73                    setAnimationStyle(a.getResourceId(
74                            R.styleable.PopupWindow_android_popupAnimationStyle, -1));
75                }
76            }
77        }
78
79        a.recycle();
80
81        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
82            // For devices pre-ICS, we need to wrap the internal OnScrollChangedListener
83            // due to NPEs.
84            wrapOnScrollChangedListener(this);
85        }
86    }
87
88    @Override
89    public void showAsDropDown(View anchor, int xoff, int yoff) {
90        if (COMPAT_OVERLAP_ANCHOR && mOverlapAnchor) {
91            // If we're pre-L, emulate overlapAnchor by modifying the yOff
92            yoff -= anchor.getHeight();
93        }
94        super.showAsDropDown(anchor, xoff, yoff);
95    }
96
97    @TargetApi(Build.VERSION_CODES.KITKAT)
98    @Override
99    public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) {
100        if (COMPAT_OVERLAP_ANCHOR && mOverlapAnchor) {
101            // If we're pre-L, emulate overlapAnchor by modifying the yOff
102            yoff -= anchor.getHeight();
103        }
104        super.showAsDropDown(anchor, xoff, yoff, gravity);
105    }
106
107    @Override
108    public void update(View anchor, int xoff, int yoff, int width, int height) {
109        if (COMPAT_OVERLAP_ANCHOR && mOverlapAnchor) {
110            // If we're pre-L, emulate overlapAnchor by modifying the yOff
111            yoff -= anchor.getHeight();
112        }
113        super.update(anchor, xoff, yoff, width, height);
114    }
115
116    private static void wrapOnScrollChangedListener(final PopupWindow popup) {
117        try {
118            final Field fieldAnchor = PopupWindow.class.getDeclaredField("mAnchor");
119            fieldAnchor.setAccessible(true);
120
121            final Field fieldListener = PopupWindow.class
122                    .getDeclaredField("mOnScrollChangedListener");
123            fieldListener.setAccessible(true);
124
125            final OnScrollChangedListener originalListener =
126                    (OnScrollChangedListener) fieldListener.get(popup);
127
128            // Now set a new listener, wrapping the original and only proxying the call when
129            // we have an anchor view.
130            fieldListener.set(popup, new OnScrollChangedListener() {
131                @Override
132                public void onScrollChanged() {
133                    try {
134                        WeakReference<View> mAnchor = (WeakReference<View>) fieldAnchor.get(popup);
135                        if (mAnchor == null || mAnchor.get() == null) {
136                            return;
137                        } else {
138                            originalListener.onScrollChanged();
139                        }
140                    } catch (IllegalAccessException e) {
141                        // Oh well...
142                    }
143                }
144            });
145        } catch (Exception e) {
146            Log.d(TAG, "Exception while installing workaround OnScrollChangedListener", e);
147        }
148    }
149
150    /**
151     * @hide
152     */
153    public void setSupportOverlapAnchor(boolean overlapAnchor) {
154        if (COMPAT_OVERLAP_ANCHOR) {
155            mOverlapAnchor = overlapAnchor;
156        } else {
157            PopupWindowCompat.setOverlapAnchor(this, overlapAnchor);
158        }
159    }
160
161    /**
162     * @hide
163     */
164    public boolean getSupportOverlapAnchor() {
165        if (COMPAT_OVERLAP_ANCHOR) {
166            return mOverlapAnchor;
167        } else {
168            return PopupWindowCompat.getOverlapAnchor(this);
169        }
170    }
171
172}
173