1/*
2 * Copyright (C) 2015 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.util.Log;
20import android.widget.PopupWindow;
21
22import java.lang.reflect.Field;
23
24class PopupWindowCompatApi21 {
25
26    private static final String TAG = "PopupWindowCompatApi21";
27
28    private static Field sOverlapAnchorField;
29
30    static {
31        try {
32            sOverlapAnchorField = PopupWindow.class.getDeclaredField("mOverlapAnchor");
33            sOverlapAnchorField.setAccessible(true);
34        } catch (NoSuchFieldException e) {
35            Log.i(TAG, "Could not fetch mOverlapAnchor field from PopupWindow", e);
36        }
37    }
38
39    static void setOverlapAnchor(PopupWindow popupWindow, boolean overlapAnchor) {
40        if (sOverlapAnchorField != null) {
41            try {
42                sOverlapAnchorField.set(popupWindow, overlapAnchor);
43            } catch (IllegalAccessException e) {
44                Log.i(TAG, "Could not set overlap anchor field in PopupWindow", e);
45            }
46        }
47    }
48
49    static boolean getOverlapAnchor(PopupWindow popupWindow) {
50        if (sOverlapAnchorField != null) {
51            try {
52                return (Boolean) sOverlapAnchorField.get(popupWindow);
53            } catch (IllegalAccessException e) {
54                Log.i(TAG, "Could not get overlap anchor field in PopupWindow", e);
55            }
56        }
57        return false;
58    }
59
60}
61