1
0
Эх сурвалжийг харах

0.0.48.beta16
- 修复 FullScreenDialog 在输入法弹起情况下启动时可能引发的背景、对话框高度和位置异常问题;
- 修复 BaseDialog 可能存在的空指针异常风险;
- 修复部分处于安卓11、10的小米设备且使用 DialogFragment 模式时对话框无法正常显示的问题;

Kongzue 2 жил өмнө
parent
commit
2965b80b8f

+ 29 - 23
DialogX/src/main/java/com/kongzue/dialogx/dialogs/FullScreenDialog.java

@@ -144,6 +144,8 @@ public class FullScreenDialog extends BaseDialog {
             bkg = convertView.findViewById(R.id.bkg);
             boxCustom = convertView.findViewById(R.id.box_custom);
 
+            imgZoomActivity.bindDialog(FullScreenDialog.this);
+
             if (hideZoomBackground) {
                 dialogView.setBackgroundResource(R.color.black20);
                 imgZoomActivity.setVisibility(View.GONE);
@@ -231,6 +233,7 @@ public class FullScreenDialog extends BaseDialog {
             boxRoot.setOnSafeInsetsChangeListener(new OnSafeInsetsChangeListener() {
                 @Override
                 public void onChange(Rect unsafeRect) {
+                    makeEnterY();
                     bkg.setY(getEnterY());
                 }
             });
@@ -253,12 +256,19 @@ public class FullScreenDialog extends BaseDialog {
             onDialogInit();
         }
 
-        private void showEnterAnim(int customViewHeight) {
-            if (getMaxHeight() != 0) {
-                customViewHeight = Math.min(getMaxHeight() - boxRoot.getUnsafePlace().bottom, customViewHeight);
+        private boolean isMatchParentHeightCustomView() {
+            if (onBindView != null && onBindView.getCustomView() != null) {
+                ViewGroup.LayoutParams lp = onBindView.getCustomView().getLayoutParams();
+                if (lp != null) {
+                    return lp.height == MATCH_PARENT;
+                }
             }
-            enterY = customViewHeight;
-            bkgEnterAimY = boxRoot.getSafeHeight() - customViewHeight;
+            return false;
+        }
+
+        private void showEnterAnim() {
+            makeEnterY();
+            bkgEnterAimY = boxRoot.getSafeHeight() - enterY;
             if (bkgEnterAimY < 0) bkgEnterAimY = 0;
             ObjectAnimator enterAnim = ObjectAnimator.ofFloat(bkg, "y", boxRoot.getHeight(), bkgEnterAimY);
             enterAnim.setDuration(enterAnimDurationTemp);
@@ -278,6 +288,19 @@ public class FullScreenDialog extends BaseDialog {
             bkgAlpha.start();
         }
 
+        private void makeEnterY() {
+            int customViewHeight = boxCustom.getHeight();
+
+            if (customViewHeight == 0 || isMatchParentHeightCustomView()) {
+                customViewHeight = ((int) boxRoot.getSafeHeight());
+            }
+            if (getMaxHeight() != 0) {
+                enterY = Math.min(getMaxHeight() - boxRoot.getUnsafePlace().bottom, customViewHeight);
+            } else {
+                enterY = customViewHeight;
+            }
+        }
+
         @Override
         public void refreshView() {
             if (boxRoot == null || getOwnActivity() == null) {
@@ -393,24 +416,7 @@ public class FullScreenDialog extends BaseDialog {
                 dialogXAnimImpl = new DialogXAnimInterface<FullScreenDialog>() {
                     @Override
                     public void doShowAnim(FullScreenDialog dialog, ObjectRunnable<Float> animProgress) {
-                        int customViewHeight = boxCustom.getHeight();
-                        if (customViewHeight == 0 || isMatchParentHeightCustomView()) {
-                            //实测在 Android 10 中,离屏情况下 View可能无法得到正确高度(恒 0),此时直接按照全屏高度处理
-                            //其他版本 Android 未发现此问题
-                            showEnterAnim((int) boxRoot.getSafeHeight());
-                        } else {
-                            showEnterAnim(customViewHeight);
-                        }
-                    }
-
-                    private boolean isMatchParentHeightCustomView() {
-                        if (onBindView != null && onBindView.getCustomView() != null) {
-                            ViewGroup.LayoutParams lp = onBindView.getCustomView().getLayoutParams();
-                            if (lp != null) {
-                                return lp.height == MATCH_PARENT;
-                            }
-                        }
-                        return false;
+                        showEnterAnim();
                     }
 
                     @Override

+ 21 - 8
DialogX/src/main/java/com/kongzue/dialogx/interfaces/BaseDialog.java

@@ -185,7 +185,7 @@ public abstract class BaseDialog implements LifecycleOwner {
                         public void run(Activity activity) {
                             baseDialog.floatingWindowActivity = new WeakReference<>((DialogXFloatingWindowActivity) activity);
                             baseDialog.floatingWindowActivity.get().setFromActivity(baseDialog.getOwnActivity());
-                            final FrameLayout activityRootView = (FrameLayout) activity.getWindow().getDecorView();
+                            final FrameLayout activityRootView = getDecorView(activity);
                             if (activityRootView == null) {
                                 return;
                             }
@@ -214,7 +214,7 @@ public abstract class BaseDialog implements LifecycleOwner {
                         intent.addFlags(FLAG_ACTIVITY_NEW_TASK);
                     }
                     intent.putExtra("dialogXKey", baseDialog.dialogKey());
-                    intent.putExtra("fromActivityUiStatus", baseDialog.getOwnActivity() == null ? 0 : baseDialog.getOwnActivity().getWindow().getDecorView().getSystemUiVisibility());
+                    intent.putExtra("fromActivityUiStatus", baseDialog.getOwnActivity() == null ? 0 : (getDecorView(baseDialog.getOwnActivity()) == null ? 0 : getDecorView(baseDialog.getOwnActivity()).getSystemUiVisibility()));
                     intent.putExtra("from", getContext().hashCode());
                     getContext().startActivity(intent);
                     int version = Integer.valueOf(Build.VERSION.SDK_INT);
@@ -304,7 +304,7 @@ public abstract class BaseDialog implements LifecycleOwner {
                         public void run(Activity activity) {
                             baseDialog.floatingWindowActivity = new WeakReference<>((DialogXFloatingWindowActivity) activity);
                             baseDialog.floatingWindowActivity.get().setFromActivity(baseDialog.getOwnActivity());
-                            final FrameLayout activityRootView = (FrameLayout) activity.getWindow().getDecorView();
+                            final FrameLayout activityRootView = getDecorView(activity);
                             if (activityRootView == null) {
                                 return;
                             }
@@ -331,7 +331,7 @@ public abstract class BaseDialog implements LifecycleOwner {
                     Intent intent = new Intent(activity, DialogXFloatingWindowActivity.class);
                     intent.putExtra("dialogXKey", baseDialog.dialogKey());
                     intent.putExtra("from", activity.hashCode());
-                    intent.putExtra("fromActivityUiStatus", activity.getWindow().getDecorView().getSystemUiVisibility());
+                    intent.putExtra("fromActivityUiStatus", getDecorView(activity) == null ? 0 : getDecorView(activity).getSystemUiVisibility());
                     activity.startActivity(intent);
                     int version = Integer.valueOf(Build.VERSION.SDK_INT);
                     if (version > 5) {
@@ -339,7 +339,7 @@ public abstract class BaseDialog implements LifecycleOwner {
                     }
                     break;
                 default:
-                    final FrameLayout activityRootView = (FrameLayout) activity.getWindow().getDecorView();
+                    final FrameLayout activityRootView = getDecorView(activity);
                     if (activityRootView == null) {
                         return;
                     }
@@ -387,7 +387,7 @@ public abstract class BaseDialog implements LifecycleOwner {
                 break;
             case FLOATING_ACTIVITY:
                 if (baseDialog.floatingWindowActivity != null && baseDialog.floatingWindowActivity.get() != null) {
-                    FrameLayout rootView = ((FrameLayout) baseDialog.floatingWindowActivity.get().getWindow().getDecorView());
+                    FrameLayout rootView = getDecorView(baseDialog.floatingWindowActivity.get());
                     if (rootView != null) {
                         rootView.removeView(dialogView);
                     }
@@ -603,7 +603,11 @@ public abstract class BaseDialog implements LifecycleOwner {
         if (activity == null) {
             activity = getTopActivity();
         }
-        rootFrameLayout = new WeakReference<>((FrameLayout) activity.getWindow().getDecorView());
+        FrameLayout decorView = getDecorView(activity);
+        if (decorView == null) {
+            return null;
+        }
+        rootFrameLayout = new WeakReference<>(decorView);
         return rootFrameLayout.get();
     }
 
@@ -717,7 +721,10 @@ public abstract class BaseDialog implements LifecycleOwner {
     }
 
     public Activity getOwnActivity() {
-        return ownActivity == null ? getTopActivity() : ownActivity.get();
+        if (ownActivity == null || ownActivity.get() == null) {
+            setOwnActivity(getTopActivity());
+        }
+        return ownActivity.get();
     }
 
     protected void cleanActivityContext() {
@@ -914,4 +921,10 @@ public abstract class BaseDialog implements LifecycleOwner {
         } catch (Exception e) {
         }
     }
+
+    protected static FrameLayout getDecorView(Activity activity) {
+        if (activity == null || activity.getWindow() == null || !(activity.getWindow().getDecorView() instanceof FrameLayout))
+            return null;
+        return (FrameLayout) activity.getWindow().getDecorView();
+    }
 }

+ 49 - 48
DialogX/src/main/java/com/kongzue/dialogx/util/views/ActivityScreenShotImageView.java

@@ -9,18 +9,17 @@ import android.graphics.Path;
 import android.graphics.Rect;
 import android.os.Build;
 import android.util.AttributeSet;
+import android.util.Log;
 import android.view.View;
 import android.view.ViewGroup;
-import android.widget.FrameLayout;
 import android.widget.ImageView;
 
-import androidx.appcompat.widget.AppCompatImageView;
-
 import com.kongzue.dialogx.impl.ActivityLifecycleImpl;
 import com.kongzue.dialogx.interfaces.BaseDialog;
 import com.kongzue.dialogx.util.DialogXFloatingWindowActivity;
 
 import java.lang.ref.WeakReference;
+import java.util.Objects;
 
 /**
  * @author: Kongzue
@@ -29,43 +28,43 @@ import java.lang.ref.WeakReference;
  * @mail: myzcxhh@live.cn
  * @createTime: 2019/11/17 23:53
  */
-public class ActivityScreenShotImageView extends ImageView {
-    
+public class ActivityScreenShotImageView extends androidx.appcompat.widget.AppCompatImageView {
+
     float width, height, mRadius;
-    
+
     public ActivityScreenShotImageView(Context context) {
         super(context);
         init(null);
     }
-    
+
     public ActivityScreenShotImageView(Context context, AttributeSet attrs) {
         super(context, attrs);
         init(attrs);
     }
-    
+
     public ActivityScreenShotImageView(Context context, AttributeSet attrs, int defStyleAttr) {
         super(context, attrs, defStyleAttr);
         init(attrs);
     }
-    
+
     private void init(AttributeSet attrs) {
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
             setLayerType(LAYER_TYPE_HARDWARE, null);
         }
     }
-    
+
     public void setRadius(float mRadius) {
         this.mRadius = mRadius;
         invalidate();
     }
-    
+
     @Override
     protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
         super.onLayout(changed, left, top, right, bottom);
         width = getWidth();
         height = getHeight();
     }
-    
+
     @Override
     protected void onDraw(Canvas canvas) {
         if (width >= mRadius && height > mRadius) {
@@ -82,7 +81,7 @@ public class ActivityScreenShotImageView extends ImageView {
             path.quadTo(0, height, 0, height - mRadius);
             path.lineTo(0, mRadius);
             path.quadTo(0, 0, mRadius, 0);
-            
+
             canvas.clipPath(path);
         }
         try {
@@ -91,16 +90,15 @@ public class ActivityScreenShotImageView extends ImageView {
         } catch (Exception e) {
         }
     }
-    
-    
+
     @Override
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
         if (isAttachedToWindow() && !isScreenshotSuccess) refreshImage();
     }
-    
+
     private int screenWidth, screenHeight;
-    
+
     private void refreshImage() {
         if (screenWidth != getMeasuredWidth() || screenHeight != getMeasuredHeight()) {
             screenWidth = getMeasuredWidth();
@@ -108,12 +106,17 @@ public class ActivityScreenShotImageView extends ImageView {
             doScreenshotActivityAndZoom();
         }
     }
-    
+
     private void doScreenshotActivityAndZoom() {
-        View contentView = getContentView();
-        if (contentView == null) {
-            return;
-        }
+        ViewGroup decorView = getDecorView();
+        if (decorView == null) return;
+//        View lastChildView = decorView.getChildAt(decorView.getChildCount() - 1);
+//        Log.e(">>>", "lastChildView: \t"+lastChildView );
+//        Log.e(">>>", "dialog.getDialogView: \t"+dialog.getDialogView() );
+//        if (lastChildView == dialog.getDialogView()) {
+//            decorView = (ViewGroup) decorView.getChildAt(0);
+//        }
+        View contentView = decorView;
         //先执行一次绘制,防止出现闪屏问题
         if (!inited) drawViewImage(contentView);
         contentView.post(new Runnable() {
@@ -125,39 +128,30 @@ public class ActivityScreenShotImageView extends ImageView {
             }
         });
     }
-    
-    private View getContentView() {
-        Activity topActivity = ActivityLifecycleImpl.getTopActivity();
-        if (topActivity == null) {
-            return null;
+
+    private ViewGroup getDecorView() {
+        if (dialog != null) {
+            Activity ownActivity = dialog.getOwnActivity();
+            return (ViewGroup) ownActivity.getWindow().getDecorView();
         }
-//        for (int i = BaseDialog.getRunningDialogList().size() - 1; i >= 0; i--) {
-//            BaseDialog baseDialog = BaseDialog.getRunningDialogList().get(i);
-//            if (!(baseDialog instanceof PopTip) && baseDialog.getActivity() == activity) {
-//                if (baseDialog.getDialogView() == null) {
-//                    return false;
-//                }
-//                return baseDialog.getDialogView().dispatchTouchEvent(event);
-//            }
-//        }
-        
-        if (getContext() instanceof Activity) {
-            return ((Activity) getContext()).getWindow().getDecorView();
-        }else{
+        Activity topActivity = ActivityLifecycleImpl.getTopActivity();
+        if (topActivity != null) {
             if (topActivity instanceof DialogXFloatingWindowActivity) {
-                return ((DialogXFloatingWindowActivity) topActivity).getFromActivity().getWindow().getDecorView();
+                return (ViewGroup) ((DialogXFloatingWindowActivity) topActivity).getFromActivity().getWindow().getDecorView();
             }
-            return topActivity.getWindow().getDecorView();
+            return (ViewGroup) topActivity.getWindow().getDecorView();
         }
+        return null;
     }
-    
+
     private boolean inited = false;
     private boolean isScreenshotSuccess;
     private WeakReference<View> contentView;
     public static boolean hideContentView = false;
-    
+
     private void drawViewImage(View view) {
         if (view.getWidth() == 0 || view.getHeight() == 0) return;
+        dialog.getDialogView().setVisibility(GONE);
         view.buildDrawingCache();
         Rect rect = new Rect();
         view.getWindowVisibleDisplayFrame(rect);
@@ -168,13 +162,14 @@ public class ActivityScreenShotImageView extends ImageView {
             if (contentView != null && contentView.get() != null) {
                 contentView.get().setVisibility(VISIBLE);
             }
-            View childView = ((ViewGroup) view).getChildAt(0);
-            childView.setVisibility(GONE);
-            contentView = new WeakReference<>(childView);
+            view.setVisibility(GONE);
+            contentView = new WeakReference<>(view);
         }
         isScreenshotSuccess = true;
+        dialog.getDialogView().setVisibility(VISIBLE);
+        dialog.getDialogView().requestFocus();
     }
-    
+
     @Override
     protected void onDetachedFromWindow() {
         super.onDetachedFromWindow();
@@ -183,4 +178,10 @@ public class ActivityScreenShotImageView extends ImageView {
             contentView.clear();
         }
     }
+
+    BaseDialog dialog;
+
+    public void bindDialog(BaseDialog dialog) {
+        this.dialog = dialog;
+    }
 }

+ 1 - 2
DialogX/src/main/java/com/kongzue/dialogx/util/views/DialogXBaseRelativeLayout.java

@@ -1,6 +1,5 @@
 package com.kongzue.dialogx.util.views;
 
-import android.app.Activity;
 import android.content.Context;
 import android.content.res.Configuration;
 import android.content.res.TypedArray;
@@ -10,7 +9,6 @@ import android.graphics.drawable.Drawable;
 import android.os.Build;
 import android.util.AttributeSet;
 import android.util.DisplayMetrics;
-import android.util.Log;
 import android.view.KeyEvent;
 import android.view.MotionEvent;
 import android.view.View;
@@ -257,6 +255,7 @@ public class DialogXBaseRelativeLayout extends RelativeLayout {
         Rect rect = new Rect();
         View decorView = (View) getParent();
         decorView.getWindowVisibleDisplayFrame(rect);
+        if (rect.left==0 && rect.top==0 && rect.right==0 && rect.bottom==0) return;
         paddingView(rect.left, rect.top, displayMetrics.widthPixels - rect.right, displayMetrics.heightPixels - rect.bottom);
     }
 

+ 2 - 2
app/release/output-metadata.json

@@ -11,8 +11,8 @@
       "type": "SINGLE",
       "filters": [],
       "attributes": [],
-      "versionCode": 46,
-      "versionName": "0.0.47.beta20",
+      "versionCode": 47,
+      "versionName": "0.0.48.beta15",
       "outputFile": "app-release.apk"
     }
   ],

+ 0 - 4
app/src/main/java/com/kongzue/dialogxdemo/activity/MainActivity.java

@@ -87,11 +87,7 @@ import com.kongzue.dialogx.style.IOSStyle;
 import com.kongzue.dialogx.style.KongzueStyle;
 import com.kongzue.dialogx.style.MIUIStyle;
 import com.kongzue.dialogx.style.MaterialStyle;
-import com.kongzue.dialogx.util.InputInfo;
-import com.kongzue.dialogx.util.ObjectRunnable;
 import com.kongzue.dialogx.util.TextInfo;
-import com.kongzue.dialogx.util.views.ActivityScreenShotImageView;
-import com.kongzue.dialogxdemo.App;
 import com.kongzue.dialogxdemo.BuildConfig;
 import com.kongzue.dialogxdemo.R;
 import com.kongzue.dialogxdemo.custom.recycleview.CustomRecycleViewAdapter;

+ 1 - 1
gradle.properties

@@ -19,6 +19,6 @@ android.useAndroidX=true
 # Automatically convert third-party libraries to use AndroidX
 android.enableJetifier=true
 
-BUILD_VERSION=0.0.48.beta15
+BUILD_VERSION=0.0.48.beta16
 BUILD_VERSION_INT=47
 DIALOGX_STYLE_VERSION=5