| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- /*
- * Tencent is pleased to support the open source community by making QMUI_Android available.
- *
- * Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved.
- *
- * Licensed under the MIT License (the "License"); you may not use this file except in
- * compliance with the License. You may obtain a copy of the License at
- *
- * http://opensource.org/licenses/MIT
- *
- * Unless required by applicable law or agreed to in writing, software distributed under the License is
- * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
- * either express or implied. See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package com.qmuiteam.qmui.widget.popup;
- import android.content.Context;
- import android.graphics.Color;
- import android.graphics.drawable.ColorDrawable;
- import android.os.Build;
- import android.view.Gravity;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.WindowManager;
- import android.widget.PopupWindow;
- import androidx.annotation.NonNull;
- import androidx.core.view.ViewCompat;
- import com.adealink.frame.util.EdgeUtilKt;
- import java.lang.ref.WeakReference;
- public abstract class QMUIBasePopup<T extends QMUIBasePopup> {
- public static final float DIM_AMOUNT_NOT_EXIST = -1f;
- public static final int NOT_SET = -1;
- protected final PopupWindow mWindow;
- protected WindowManager mWindowManager;
- protected Context mContext;
- protected WeakReference<View> mAttachedViewRf;
- private float mDimAmount = DIM_AMOUNT_NOT_EXIST;
- private int mDimAmountAttr = 0;
- private PopupWindow.OnDismissListener mDismissListener;
- private final View.OnAttachStateChangeListener mOnAttachStateChangeListener = new View.OnAttachStateChangeListener() {
- @Override
- public void onViewAttachedToWindow(View v) {
- }
- @Override
- public void onViewDetachedFromWindow(View v) {
- dismiss();
- }
- };
- private final View.OnTouchListener mOutsideTouchDismissListener = new View.OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
- mWindow.dismiss();
- return true;
- }
- return false;
- }
- };
- public QMUIBasePopup(Context context) {
- mContext = context;
- mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
- mWindow = new PopupWindow(context);
- mWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
- mWindow.setFocusable(true);
- mWindow.setTouchable(true);
- mWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
- @Override
- public void onDismiss() {
- removeOldAttachStateChangeListener();
- mAttachedViewRf = null;
- QMUIBasePopup.this.onDismiss();
- if (mDismissListener != null) {
- mDismissListener.onDismiss();
- }
- }
- });
- dismissIfOutsideTouch(true);
- }
- protected void onSkinChange(int oldSkin, int newSkin){
- }
- public T dimAmount(float dimAmount) {
- mDimAmount = dimAmount;
- return (T) this;
- }
- public T dimAmountAttr(int dimAmountAttr) {
- mDimAmountAttr = dimAmountAttr;
- return (T) this;
- }
- public T setTouchable(boolean touchable){
- mWindow.setTouchable(true);
- return (T) this;
- }
- public T setFocusable(boolean focusable){
- mWindow.setFocusable(focusable);
- return (T) this;
- }
- public T dismissIfOutsideTouch(boolean dismissIfOutsideTouch) {
- mWindow.setOutsideTouchable(dismissIfOutsideTouch);
- if (dismissIfOutsideTouch) {
- mWindow.setTouchInterceptor(mOutsideTouchDismissListener);
- } else {
- mWindow.setTouchInterceptor(null);
- }
- return (T) this;
- }
- public T onDismiss(PopupWindow.OnDismissListener listener) {
- mDismissListener = listener;
- return (T) this;
- }
- private void removeOldAttachStateChangeListener() {
- if (mAttachedViewRf != null) {
- View oldAttachedView = mAttachedViewRf.get();
- if (oldAttachedView != null) {
- oldAttachedView.removeOnAttachStateChangeListener(mOnAttachStateChangeListener);
- }
- }
- }
- public View getDecorView() {
- View decorView = null;
- try {
- if (mWindow.getBackground() == null) {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- decorView = (View) mWindow.getContentView().getParent();
- } else {
- decorView = mWindow.getContentView();
- }
- } else {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- decorView = (View) mWindow.getContentView().getParent().getParent();
- } else {
- decorView = (View) mWindow.getContentView().getParent();
- }
- }
- } catch (Exception ignore) {
- }
- return decorView;
- }
- protected void showAtLocation(@NonNull View parent, int x, int y) {
- if (!ViewCompat.isAttachedToWindow(parent)) {
- return;
- }
- removeOldAttachStateChangeListener();
- parent.addOnAttachStateChangeListener(mOnAttachStateChangeListener);
- mAttachedViewRf = new WeakReference<>(parent);
- mWindow.showAtLocation(parent, Gravity.NO_GRAVITY, x, y);
- if (mDimAmount != DIM_AMOUNT_NOT_EXIST) {
- updateDimAmount(mDimAmount);
- }
- try {
- EdgeUtilKt.fitPopupEdgeToEdge(mWindow);
- } catch (Exception e) {}
- }
- private void updateDimAmount(float dimAmount) {
- View decorView = getDecorView();
- if (decorView != null) {
- WindowManager.LayoutParams p = (WindowManager.LayoutParams) decorView.getLayoutParams();
- p.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
- p.dimAmount = dimAmount;
- modifyWindowLayoutParams(p);
- mWindowManager.updateViewLayout(decorView, p);
- }
- }
- protected void modifyWindowLayoutParams(WindowManager.LayoutParams lp) {
- }
- protected void onDismiss() {
- }
- public final void dismiss() {
- mWindow.dismiss();
- }
- }
|