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