what-the-bank/sources/io/flutter/plugin/platform/PlatformViewWrapper.java

289 lines
10 KiB
Java
Raw Normal View History

2024-07-27 18:17:47 +07:00
package io.flutter.plugin.platform;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.SurfaceTexture;
import android.os.Build;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.ViewTreeObserver;
import android.view.accessibility.AccessibilityEvent;
import android.widget.FrameLayout;
import io.flutter.Log;
import io.flutter.embedding.android.AndroidTouchProcessor;
import io.flutter.util.ViewUtils;
import io.flutter.view.TextureRegistry;
import java.util.concurrent.atomic.AtomicLong;
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes6.dex */
public class PlatformViewWrapper extends FrameLayout {
private static final String TAG = "PlatformViewWrapper";
ViewTreeObserver.OnGlobalFocusChangeListener activeFocusListener;
private int bufferHeight;
private int bufferWidth;
private final TextureRegistry.OnFrameConsumedListener frameConsumedListener;
private int left;
private final AtomicLong pendingFramesCount;
private int prevLeft;
private int prevTop;
private boolean shouldRecreateSurfaceForLowMemory;
private Surface surface;
private int top;
private AndroidTouchProcessor touchProcessor;
private final TextureRegistry.OnTrimMemoryListener trimMemoryListener;
private SurfaceTexture tx;
public int getBufferHeight() {
return this.bufferHeight;
}
public int getBufferWidth() {
return this.bufferWidth;
}
public SurfaceTexture getTexture() {
return this.tx;
}
@Override // android.view.ViewGroup
public boolean onInterceptTouchEvent(MotionEvent motionEvent) {
return true;
}
public void setTouchProcessor(AndroidTouchProcessor androidTouchProcessor) {
this.touchProcessor = androidTouchProcessor;
}
private void onFrameProduced() {
if (Build.VERSION.SDK_INT == 29) {
this.pendingFramesCount.incrementAndGet();
}
}
private void recreateSurfaceIfNeeded() {
if (this.shouldRecreateSurfaceForLowMemory) {
Surface surface = this.surface;
if (surface != null) {
surface.release();
}
this.surface = createSurface(this.tx);
this.shouldRecreateSurfaceForLowMemory = false;
}
}
private boolean shouldDrawToSurfaceNow() {
return Build.VERSION.SDK_INT != 29 || this.pendingFramesCount.get() <= 0;
}
public PlatformViewWrapper(Context context) {
super(context);
this.pendingFramesCount = new AtomicLong(0L);
this.frameConsumedListener = new TextureRegistry.OnFrameConsumedListener(this) { // from class: io.flutter.plugin.platform.PlatformViewWrapper.1
final PlatformViewWrapper this$0;
{
this.this$0 = this;
}
@Override // io.flutter.view.TextureRegistry.OnFrameConsumedListener
public void onFrameConsumed() {
if (Build.VERSION.SDK_INT == 29) {
this.this$0.pendingFramesCount.decrementAndGet();
}
}
};
this.shouldRecreateSurfaceForLowMemory = false;
this.trimMemoryListener = new TextureRegistry.OnTrimMemoryListener(this) { // from class: io.flutter.plugin.platform.PlatformViewWrapper.2
final PlatformViewWrapper this$0;
{
this.this$0 = this;
}
@Override // io.flutter.view.TextureRegistry.OnTrimMemoryListener
public void onTrimMemory(int i) {
if (i != 80 || Build.VERSION.SDK_INT < 29) {
return;
}
this.this$0.shouldRecreateSurfaceForLowMemory = true;
}
};
setWillNotDraw(false);
}
public PlatformViewWrapper(Context context, TextureRegistry.SurfaceTextureEntry surfaceTextureEntry) {
this(context);
surfaceTextureEntry.setOnFrameConsumedListener(this.frameConsumedListener);
surfaceTextureEntry.setOnTrimMemoryListener(this.trimMemoryListener);
setTexture(surfaceTextureEntry.surfaceTexture());
}
public void setTexture(SurfaceTexture surfaceTexture) {
int i;
this.tx = surfaceTexture;
int i2 = this.bufferWidth;
if (i2 > 0 && (i = this.bufferHeight) > 0) {
surfaceTexture.setDefaultBufferSize(i2, i);
}
Surface surface = this.surface;
if (surface != null) {
surface.release();
}
Surface createSurface = createSurface(surfaceTexture);
this.surface = createSurface;
Canvas lockHardwareCanvas = createSurface.lockHardwareCanvas();
try {
lockHardwareCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
onFrameProduced();
} finally {
this.surface.unlockCanvasAndPost(lockHardwareCanvas);
}
}
protected Surface createSurface(SurfaceTexture surfaceTexture) {
return new Surface(surfaceTexture);
}
public void setLayoutParams(FrameLayout.LayoutParams layoutParams) {
super.setLayoutParams((ViewGroup.LayoutParams) layoutParams);
this.left = layoutParams.leftMargin;
this.top = layoutParams.topMargin;
}
public void setBufferSize(int i, int i2) {
this.bufferWidth = i;
this.bufferHeight = i2;
SurfaceTexture surfaceTexture = this.tx;
if (surfaceTexture != null) {
surfaceTexture.setDefaultBufferSize(i, i2);
}
}
public void release() {
this.tx = null;
Surface surface = this.surface;
if (surface != null) {
surface.release();
this.surface = null;
}
}
@Override // android.view.ViewGroup, android.view.ViewParent
public boolean requestSendAccessibilityEvent(View view, AccessibilityEvent accessibilityEvent) {
View childAt = getChildAt(0);
if (childAt == null || childAt.getImportantForAccessibility() != 4) {
return super.requestSendAccessibilityEvent(view, accessibilityEvent);
}
return false;
}
@Override // android.view.ViewGroup, android.view.ViewParent
public void onDescendantInvalidated(View view, View view2) {
super.onDescendantInvalidated(view, view2);
invalidate();
}
@Override // android.view.ViewGroup, android.view.ViewParent
public ViewParent invalidateChildInParent(int[] iArr, Rect rect) {
invalidate();
return super.invalidateChildInParent(iArr, rect);
}
@Override // android.view.View
public void draw(Canvas canvas) {
Surface surface = this.surface;
if (surface == null) {
super.draw(canvas);
Log.e(TAG, "Platform view cannot be composed without a surface.");
return;
}
if (!surface.isValid()) {
Log.e(TAG, "Invalid surface. The platform view cannot be displayed.");
return;
}
SurfaceTexture surfaceTexture = this.tx;
if (surfaceTexture == null || surfaceTexture.isReleased()) {
Log.e(TAG, "Invalid texture. The platform view cannot be displayed.");
return;
}
if (!shouldDrawToSurfaceNow()) {
invalidate();
return;
}
recreateSurfaceIfNeeded();
Canvas lockHardwareCanvas = this.surface.lockHardwareCanvas();
try {
lockHardwareCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
super.draw(lockHardwareCanvas);
onFrameProduced();
} finally {
this.surface.unlockCanvasAndPost(lockHardwareCanvas);
}
}
@Override // android.view.View
public boolean onTouchEvent(MotionEvent motionEvent) {
if (this.touchProcessor == null) {
return super.onTouchEvent(motionEvent);
}
Matrix matrix = new Matrix();
int action = motionEvent.getAction();
if (action == 0) {
int i = this.left;
this.prevLeft = i;
int i2 = this.top;
this.prevTop = i2;
matrix.postTranslate(i, i2);
} else if (action == 2) {
matrix.postTranslate(this.prevLeft, this.prevTop);
this.prevLeft = this.left;
this.prevTop = this.top;
} else {
matrix.postTranslate(this.left, this.top);
}
return this.touchProcessor.onTouchEvent(motionEvent, matrix);
}
public void setOnDescendantFocusChangeListener(View.OnFocusChangeListener onFocusChangeListener) {
unsetOnDescendantFocusChangeListener();
ViewTreeObserver viewTreeObserver = getViewTreeObserver();
if (viewTreeObserver.isAlive() && this.activeFocusListener == null) {
ViewTreeObserver.OnGlobalFocusChangeListener onGlobalFocusChangeListener = new ViewTreeObserver.OnGlobalFocusChangeListener(this, onFocusChangeListener) { // from class: io.flutter.plugin.platform.PlatformViewWrapper.3
final PlatformViewWrapper this$0;
final View.OnFocusChangeListener val$userFocusListener;
{
this.this$0 = this;
this.val$userFocusListener = onFocusChangeListener;
}
@Override // android.view.ViewTreeObserver.OnGlobalFocusChangeListener
public void onGlobalFocusChanged(View view, View view2) {
View.OnFocusChangeListener onFocusChangeListener2 = this.val$userFocusListener;
PlatformViewWrapper platformViewWrapper = this.this$0;
onFocusChangeListener2.onFocusChange(platformViewWrapper, ViewUtils.childHasFocus(platformViewWrapper));
}
};
this.activeFocusListener = onGlobalFocusChangeListener;
viewTreeObserver.addOnGlobalFocusChangeListener(onGlobalFocusChangeListener);
}
}
public void unsetOnDescendantFocusChangeListener() {
ViewTreeObserver.OnGlobalFocusChangeListener onGlobalFocusChangeListener;
ViewTreeObserver viewTreeObserver = getViewTreeObserver();
if (!viewTreeObserver.isAlive() || (onGlobalFocusChangeListener = this.activeFocusListener) == null) {
return;
}
this.activeFocusListener = null;
viewTreeObserver.removeOnGlobalFocusChangeListener(onGlobalFocusChangeListener);
}
}