package io.flutter.embedding.android; import android.content.Context; import android.graphics.Region; import android.util.AttributeSet; import android.view.SurfaceHolder; import android.view.SurfaceView; import com.google.android.gms.maps.model.BitmapDescriptorFactory; import io.flutter.Log; import io.flutter.embedding.engine.renderer.FlutterRenderer; import io.flutter.embedding.engine.renderer.FlutterUiDisplayListener; import io.flutter.embedding.engine.renderer.RenderSurface; /* loaded from: classes5.dex */ public class FlutterSurfaceView extends SurfaceView implements RenderSurface { private static final String TAG = "FlutterSurfaceView"; private FlutterRenderer flutterRenderer; private final FlutterUiDisplayListener flutterUiDisplayListener; private boolean isAttachedToFlutterRenderer; private boolean isPaused; private boolean isSurfaceAvailableForRendering; private final boolean renderTransparently; private final SurfaceHolder.Callback surfaceCallback; @Override // io.flutter.embedding.engine.renderer.RenderSurface public FlutterRenderer getAttachedRenderer() { return this.flutterRenderer; } public FlutterSurfaceView(Context context) { this(context, null, false); } public FlutterSurfaceView(Context context, boolean z) { this(context, null, z); } public FlutterSurfaceView(Context context, AttributeSet attributeSet) { this(context, attributeSet, false); } private FlutterSurfaceView(Context context, AttributeSet attributeSet, boolean z) { super(context, attributeSet); this.isSurfaceAvailableForRendering = false; this.isPaused = false; this.isAttachedToFlutterRenderer = false; this.surfaceCallback = new SurfaceHolder.Callback(this) { // from class: io.flutter.embedding.android.FlutterSurfaceView.1 final FlutterSurfaceView this$0; { this.this$0 = this; } @Override // android.view.SurfaceHolder.Callback public void surfaceCreated(SurfaceHolder surfaceHolder) { Log.v(FlutterSurfaceView.TAG, "SurfaceHolder.Callback.startRenderingToSurface()"); this.this$0.isSurfaceAvailableForRendering = true; if (this.this$0.isAttachedToFlutterRenderer) { this.this$0.connectSurfaceToRenderer(); } } @Override // android.view.SurfaceHolder.Callback public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) { Log.v(FlutterSurfaceView.TAG, "SurfaceHolder.Callback.surfaceChanged()"); if (this.this$0.isAttachedToFlutterRenderer) { this.this$0.changeSurfaceSize(i2, i3); } } @Override // android.view.SurfaceHolder.Callback public void surfaceDestroyed(SurfaceHolder surfaceHolder) { Log.v(FlutterSurfaceView.TAG, "SurfaceHolder.Callback.stopRenderingToSurface()"); this.this$0.isSurfaceAvailableForRendering = false; if (this.this$0.isAttachedToFlutterRenderer) { this.this$0.disconnectSurfaceFromRenderer(); } } }; this.flutterUiDisplayListener = new FlutterUiDisplayListener(this) { // from class: io.flutter.embedding.android.FlutterSurfaceView.2 final FlutterSurfaceView this$0; @Override // io.flutter.embedding.engine.renderer.FlutterUiDisplayListener public void onFlutterUiNoLongerDisplayed() { } { this.this$0 = this; } @Override // io.flutter.embedding.engine.renderer.FlutterUiDisplayListener public void onFlutterUiDisplayed() { Log.v(FlutterSurfaceView.TAG, "onFlutterUiDisplayed()"); this.this$0.setAlpha(1.0f); if (this.this$0.flutterRenderer != null) { this.this$0.flutterRenderer.removeIsDisplayingFlutterUiListener(this); } } }; this.renderTransparently = z; init(); } private void init() { if (this.renderTransparently) { getHolder().setFormat(-2); setZOrderOnTop(true); } getHolder().addCallback(this.surfaceCallback); setAlpha(BitmapDescriptorFactory.HUE_RED); } @Override // android.view.SurfaceView, android.view.View public boolean gatherTransparentRegion(Region region) { if (getAlpha() < 1.0f) { return false; } int[] iArr = new int[2]; getLocationInWindow(iArr); int i = iArr[0]; int i2 = iArr[1]; int right = getRight(); int left = getLeft(); int i3 = iArr[1]; int bottom = getBottom(); region.op(i, i2, (right + i) - left, (i3 + bottom) - getTop(), Region.Op.DIFFERENCE); return true; } @Override // io.flutter.embedding.engine.renderer.RenderSurface public void attachToRenderer(FlutterRenderer flutterRenderer) { Log.v(TAG, "Attaching to FlutterRenderer."); if (this.flutterRenderer != null) { Log.v(TAG, "Already connected to a FlutterRenderer. Detaching from old one and attaching to new one."); this.flutterRenderer.stopRenderingToSurface(); this.flutterRenderer.removeIsDisplayingFlutterUiListener(this.flutterUiDisplayListener); } this.flutterRenderer = flutterRenderer; this.isAttachedToFlutterRenderer = true; flutterRenderer.addIsDisplayingFlutterUiListener(this.flutterUiDisplayListener); if (this.isSurfaceAvailableForRendering) { Log.v(TAG, "Surface is available for rendering. Connecting FlutterRenderer to Android surface."); connectSurfaceToRenderer(); } this.isPaused = false; } @Override // io.flutter.embedding.engine.renderer.RenderSurface public void detachFromRenderer() { if (this.flutterRenderer != null) { if (getWindowToken() != null) { Log.v(TAG, "Disconnecting FlutterRenderer from Android surface."); disconnectSurfaceFromRenderer(); } setAlpha(BitmapDescriptorFactory.HUE_RED); this.flutterRenderer.removeIsDisplayingFlutterUiListener(this.flutterUiDisplayListener); this.flutterRenderer = null; this.isAttachedToFlutterRenderer = false; return; } Log.w(TAG, "detachFromRenderer() invoked when no FlutterRenderer was attached."); } @Override // io.flutter.embedding.engine.renderer.RenderSurface public void pause() { if (this.flutterRenderer == null) { Log.w(TAG, "pause() invoked when no FlutterRenderer was attached."); return; } this.flutterRenderer = null; this.isPaused = true; this.isAttachedToFlutterRenderer = false; } /* JADX INFO: Access modifiers changed from: private */ public void connectSurfaceToRenderer() { if (this.flutterRenderer == null || getHolder() == null) { throw new IllegalStateException("connectSurfaceToRenderer() should only be called when flutterRenderer and getHolder() are non-null."); } this.flutterRenderer.startRenderingToSurface(getHolder().getSurface(), this.isPaused); } /* JADX INFO: Access modifiers changed from: private */ public void changeSurfaceSize(int i, int i2) { if (this.flutterRenderer == null) { throw new IllegalStateException("changeSurfaceSize() should only be called when flutterRenderer is non-null."); } Log.v(TAG, "Notifying FlutterRenderer that Android surface size has changed to " + i + " x " + i2); this.flutterRenderer.surfaceChanged(i, i2); } /* JADX INFO: Access modifiers changed from: private */ public void disconnectSurfaceFromRenderer() { FlutterRenderer flutterRenderer = this.flutterRenderer; if (flutterRenderer == null) { throw new IllegalStateException("disconnectSurfaceFromRenderer() should only be called when flutterRenderer is non-null."); } flutterRenderer.stopRenderingToSurface(); } }