Commit f9f4bf29 authored by yangwu.jia's avatar yangwu.jia

适配1.12

parent 2f523fd6
package com.idlefish.flutterboost;
import android.content.Context;
import android.graphics.SurfaceTexture;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Surface;
import android.view.TextureView;
import io.flutter.Log;
import io.flutter.embedding.engine.renderer.FlutterRenderer;
import io.flutter.embedding.engine.renderer.OnFirstFrameRenderedListener;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class XFlutterTextureView extends TextureView implements FlutterRenderer.RenderSurface {
private static final String TAG = "XFlutterTextureView";
private boolean isSurfaceAvailableForRendering;
private boolean isAttachedToFlutterRenderer;
@Nullable
private FlutterRenderer flutterRenderer;
@NonNull
private Set<OnFirstFrameRenderedListener> onFirstFrameRenderedListeners;
private final SurfaceTextureListener surfaceTextureListener;
public XFlutterTextureView(@NonNull Context context) {
this(context, (AttributeSet)null);
}
public XFlutterTextureView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
this.isSurfaceAvailableForRendering = false;
this.isAttachedToFlutterRenderer = false;
this.onFirstFrameRenderedListeners = new HashSet();
this.surfaceTextureListener = new SurfaceTextureListener() {
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
Log.v("FlutterTextureView", "SurfaceTextureListener.onSurfaceTextureAvailable()");
XFlutterTextureView.this.isSurfaceAvailableForRendering = true;
if (XFlutterTextureView.this.isAttachedToFlutterRenderer) {
XFlutterTextureView.this.connectSurfaceToRenderer();
}
}
public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height) {
Log.v("FlutterTextureView", "SurfaceTextureListener.onSurfaceTextureSizeChanged()");
if (XFlutterTextureView.this.isAttachedToFlutterRenderer) {
XFlutterTextureView.this.changeSurfaceSize(width, height);
}
}
public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {
}
public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) {
Log.v("FlutterTextureView", "SurfaceTextureListener.onSurfaceTextureDestroyed()");
XFlutterTextureView.this.isSurfaceAvailableForRendering = false;
if (XFlutterTextureView.this.isAttachedToFlutterRenderer) {
XFlutterTextureView.this.disconnectSurfaceFromRenderer();
}
return true;
}
};
this.init();
}
private void init() {
this.setSurfaceTextureListener(this.surfaceTextureListener);
}
public void attachToRenderer(@NonNull FlutterRenderer flutterRenderer) {
Log.v("FlutterTextureView", "Attaching to FlutterRenderer.");
if (this.flutterRenderer != null) {
Log.v("FlutterTextureView", "Already connected to a FlutterRenderer. Detaching from old one and attaching to new one.");
this.flutterRenderer.detachFromRenderSurface();
}
this.flutterRenderer = flutterRenderer;
this.isAttachedToFlutterRenderer = true;
if (this.isSurfaceAvailableForRendering) {
Log.v("FlutterTextureView", "Surface is available for rendering. Connecting FlutterRenderer to Android surface.");
this.connectSurfaceToRenderer();
}
}
public void detachFromRenderer() {
if (this.flutterRenderer != null) {
if (this.getWindowToken() != null) {
Log.v("FlutterTextureView", "Disconnecting FlutterRenderer from Android surface.");
this.disconnectSurfaceFromRenderer();
}
this.flutterRenderer = null;
this.isAttachedToFlutterRenderer = false;
} else {
Log.w("FlutterTextureView", "detachFromRenderer() invoked when no FlutterRenderer was attached.");
}
}
private void connectSurfaceToRenderer() {
if (this.flutterRenderer != null && this.getSurfaceTexture() != null) {
Surface surface= new Surface(this.getSurfaceTexture());
this.flutterRenderer.surfaceCreated(surface);
surface.release();
} else {
throw new IllegalStateException("connectSurfaceToRenderer() should only be called when flutterRenderer and getSurfaceTexture() are non-null.");
}
}
private void changeSurfaceSize(int width, int height) {
if (this.flutterRenderer == null) {
throw new IllegalStateException("changeSurfaceSize() should only be called when flutterRenderer is non-null.");
} else {
Log.v("FlutterTextureView", "Notifying FlutterRenderer that Android surface size has changed to " + width + " x " + height);
this.flutterRenderer.surfaceChanged(width, height);
}
}
private void disconnectSurfaceFromRenderer() {
if (this.flutterRenderer == null) {
throw new IllegalStateException("disconnectSurfaceFromRenderer() should only be called when flutterRenderer is non-null.");
} else {
this.flutterRenderer.surfaceDestroyed();
}
}
public void addOnFirstFrameRenderedListener(@NonNull OnFirstFrameRenderedListener listener) {
this.onFirstFrameRenderedListeners.add(listener);
}
public void removeOnFirstFrameRenderedListener(@NonNull OnFirstFrameRenderedListener listener) {
this.onFirstFrameRenderedListeners.remove(listener);
}
public void onFirstFrameRendered() {
Log.v("FlutterTextureView", "onFirstFrameRendered()");
Iterator var1 = this.onFirstFrameRenderedListeners.iterator();
while(var1.hasNext()) {
OnFirstFrameRenderedListener listener = (OnFirstFrameRenderedListener)var1.next();
listener.onFirstFrameRendered();
}
}
}
\ No newline at end of file
...@@ -36,7 +36,8 @@ import io.flutter.Log; ...@@ -36,7 +36,8 @@ import io.flutter.Log;
import io.flutter.embedding.android.*; import io.flutter.embedding.android.*;
import io.flutter.embedding.engine.FlutterEngine; import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.renderer.FlutterRenderer; import io.flutter.embedding.engine.renderer.FlutterRenderer;
import io.flutter.embedding.engine.renderer.OnFirstFrameRenderedListener; import io.flutter.embedding.engine.renderer.FlutterUiDisplayListener;
import io.flutter.embedding.engine.renderer.RenderSurface;
import io.flutter.embedding.engine.systemchannels.TextInputChannel; import io.flutter.embedding.engine.systemchannels.TextInputChannel;
import io.flutter.plugin.editing.TextInputPlugin; import io.flutter.plugin.editing.TextInputPlugin;
import io.flutter.plugin.platform.PlatformViewsController; import io.flutter.plugin.platform.PlatformViewsController;
...@@ -74,8 +75,10 @@ public class XFlutterView extends FrameLayout { ...@@ -74,8 +75,10 @@ public class XFlutterView extends FrameLayout {
// Internal view hierarchy references. // Internal view hierarchy references.
@Nullable @Nullable
private FlutterRenderer.RenderSurface renderSurface; private RenderSurface renderSurface;
private final Set<OnFirstFrameRenderedListener> onFirstFrameRenderedListeners = new HashSet<>();
private final Set<FlutterUiDisplayListener> flutterUiDisplayListeners = new HashSet<>();
private boolean didRenderFirstFrame; private boolean didRenderFirstFrame;
// Connections to a Flutter execution context. // Connections to a Flutter execution context.
...@@ -100,6 +103,9 @@ public class XFlutterView extends FrameLayout { ...@@ -100,6 +103,9 @@ public class XFlutterView extends FrameLayout {
private boolean hasAddFirstFrameRenderedListener=false; private boolean hasAddFirstFrameRenderedListener=false;
private boolean isFlutterUiDisplayed;
// Directly implemented View behavior that communicates with Flutter. // Directly implemented View behavior that communicates with Flutter.
private final FlutterRenderer.ViewportMetrics viewportMetrics = new FlutterRenderer.ViewportMetrics(); private final FlutterRenderer.ViewportMetrics viewportMetrics = new FlutterRenderer.ViewportMetrics();
...@@ -110,23 +116,32 @@ public class XFlutterView extends FrameLayout { ...@@ -110,23 +116,32 @@ public class XFlutterView extends FrameLayout {
} }
}; };
private final OnFirstFrameRenderedListener onFirstFrameRenderedListener = new OnFirstFrameRenderedListener() {
private final FlutterUiDisplayListener flutterUiDisplayListener = new FlutterUiDisplayListener() {
@Override @Override
public void onFirstFrameRendered() { public void onFlutterUiDisplayed() {
didRenderFirstFrame = true; isFlutterUiDisplayed = true;
for (OnFirstFrameRenderedListener listener : onFirstFrameRenderedListeners) { for (FlutterUiDisplayListener listener : flutterUiDisplayListeners) {
listener.onFirstFrameRendered(); listener.onFlutterUiDisplayed();
}
}
@Override
public void onFlutterUiNoLongerDisplayed() {
isFlutterUiDisplayed = false;
for (FlutterUiDisplayListener listener : flutterUiDisplayListeners) {
listener.onFlutterUiNoLongerDisplayed();
} }
} }
}; };
/** /**
* Constructs a {@code FlutterView} programmatically, without any XML attributes. * Constructs a {@code FlutterView} programmatically, without any XML attributes.
* <p> * <p>
* <ul> * <ul>
* <li>{@link #renderMode} defaults to {@link RenderMode#surface}.</li>
* <li>{@link #transparencyMode} defaults to {@link TransparencyMode#opaque}.</li>
* </ul> * </ul>
* {@code FlutterView} requires an {@code Activity} instead of a generic {@code Context} * {@code FlutterView} requires an {@code Activity} instead of a generic {@code Context}
* to be compatible with {@link PlatformViewsController}. * to be compatible with {@link PlatformViewsController}.
...@@ -202,7 +217,7 @@ public class XFlutterView extends FrameLayout { ...@@ -202,7 +217,7 @@ public class XFlutterView extends FrameLayout {
break; break;
case texture: case texture:
Log.v(TAG, "Internally using a FlutterTextureView."); Log.v(TAG, "Internally using a FlutterTextureView.");
XFlutterTextureView flutterTextureView = new XFlutterTextureView(getContext()); FlutterTextureView flutterTextureView = new FlutterTextureView(getContext());
renderSurface = flutterTextureView; renderSurface = flutterTextureView;
addView(flutterTextureView); addView(flutterTextureView);
break; break;
...@@ -233,20 +248,20 @@ public class XFlutterView extends FrameLayout { ...@@ -233,20 +248,20 @@ public class XFlutterView extends FrameLayout {
return didRenderFirstFrame; return didRenderFirstFrame;
} }
/**
* Adds the given {@code listener} to this {@code FlutterView}, to be notified upon Flutter's
* first rendered frame.
*/
public void addOnFirstFrameRenderedListener(@NonNull OnFirstFrameRenderedListener listener) { public void addOnFirstFrameRenderedListener(@NonNull FlutterUiDisplayListener listener) {
onFirstFrameRenderedListeners.add(listener); flutterUiDisplayListeners.add(listener);
} }
/** /**
* Removes the given {@code listener}, which was previously added with * Removes the given {@code listener}, which was previously added with
* {@link #addOnFirstFrameRenderedListener(OnFirstFrameRenderedListener)}. * {@link #addOnFirstFrameRenderedListener(FlutterUiDisplayListener)}.
*/ */
public void removeOnFirstFrameRenderedListener(@NonNull OnFirstFrameRenderedListener listener) { public void removeOnFirstFrameRenderedListener(@NonNull FlutterUiDisplayListener listener) {
onFirstFrameRenderedListeners.remove(listener); flutterUiDisplayListeners.remove(listener);
} }
//------- Start: Process View configuration that Flutter cares about. ------ //------- Start: Process View configuration that Flutter cares about. ------
...@@ -587,17 +602,11 @@ public class XFlutterView extends FrameLayout { ...@@ -587,17 +602,11 @@ public class XFlutterView extends FrameLayout {
} }
this.flutterEngine = flutterEngine; this.flutterEngine = flutterEngine;
// Instruct our FlutterRenderer that we are now its designated RenderSurface.
FlutterRenderer flutterRenderer = this.flutterEngine.getRenderer(); FlutterRenderer flutterRenderer = this.flutterEngine.getRenderer();
didRenderFirstFrame = flutterRenderer.hasRenderedFirstFrame(); isFlutterUiDisplayed = flutterRenderer.isDisplayingFlutterUi();
if(!hasAddFirstFrameRenderedListener){ renderSurface.attachToRenderer(flutterRenderer);
flutterRenderer.addOnFirstFrameRenderedListener(onFirstFrameRenderedListener); flutterRenderer.addIsDisplayingFlutterUiListener(flutterUiDisplayListener);
hasAddFirstFrameRenderedListener=true;
}
flutterRenderer.attachToRenderSurface(renderSurface);
// Initialize various components that know how to process Android View I/O
// in a way that Flutter understands.
if(textInputPlugin==null){ if(textInputPlugin==null){
...@@ -693,11 +702,17 @@ public class XFlutterView extends FrameLayout { ...@@ -693,11 +702,17 @@ public class XFlutterView extends FrameLayout {
// TODO(mattcarroll): once this is proven to work, move this line ot TextInputPlugin // TODO(mattcarroll): once this is proven to work, move this line ot TextInputPlugin
// Instruct our FlutterRenderer that we are no longer interested in being its RenderSurface. // Instruct our FlutterRenderer that we are no longer interested in being its RenderSurface.
FlutterRenderer flutterRenderer = flutterEngine.getRenderer(); FlutterRenderer flutterRenderer = flutterEngine.getRenderer();
// didRenderFirstFrame = false; isFlutterUiDisplayed = false;
flutterRenderer.removeOnFirstFrameRenderedListener(onFirstFrameRenderedListener); flutterRenderer.removeIsDisplayingFlutterUiListener(flutterUiDisplayListener);
flutterRenderer.detachFromRenderSurface(); flutterRenderer.stopRenderingToSurface();
flutterRenderer.setSemanticsEnabled(false);
renderSurface.detachFromRenderer();
flutterEngine = null; flutterEngine = null;
} }
public void release(){ public void release(){
if(textInputPlugin!=null){ if(textInputPlugin!=null){
......
...@@ -458,6 +458,11 @@ public class BoostFlutterActivity extends Activity ...@@ -458,6 +458,11 @@ public class BoostFlutterActivity extends Activity
// No-op. Hook for subclasses. // No-op. Hook for subclasses.
} }
@Override
public void cleanUpFlutterEngine(@NonNull FlutterEngine flutterEngine) {
}
@Override @Override
public boolean shouldAttachEngineToActivity() { public boolean shouldAttachEngineToActivity() {
......
...@@ -482,6 +482,11 @@ public class FlutterFragment extends Fragment implements FlutterActivityAndFragm ...@@ -482,6 +482,11 @@ public class FlutterFragment extends Fragment implements FlutterActivityAndFragm
} }
} }
@Override
public void cleanUpFlutterEngine(@NonNull FlutterEngine flutterEngine) {
}
/** /**
* See {@link NewEngineFragmentBuilder#shouldAttachEngineToActivity()} and * See {@link NewEngineFragmentBuilder#shouldAttachEngineToActivity()} and
* <p> * <p>
......
...@@ -4,8 +4,6 @@ import android.content.Context; ...@@ -4,8 +4,6 @@ import android.content.Context;
import android.graphics.Color; import android.graphics.Color;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.util.AttributeSet; import android.util.AttributeSet;
...@@ -17,9 +15,8 @@ import io.flutter.Log; ...@@ -17,9 +15,8 @@ import io.flutter.Log;
import io.flutter.embedding.android.FlutterView; import io.flutter.embedding.android.FlutterView;
import io.flutter.embedding.android.SplashScreen; import io.flutter.embedding.android.SplashScreen;
import io.flutter.embedding.engine.FlutterEngine; import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.renderer.OnFirstFrameRenderedListener; import io.flutter.embedding.engine.renderer.FlutterUiDisplayListener;
import java.util.Date;
/** /**
* {@code View} that displays a {@link SplashScreen} until a given {@link FlutterView} * {@code View} that displays a {@link SplashScreen} until a given {@link FlutterView}
...@@ -57,39 +54,20 @@ public class FlutterSplashView extends FrameLayout { ...@@ -57,39 +54,20 @@ public class FlutterSplashView extends FrameLayout {
}; };
@NonNull @NonNull
private final OnFirstFrameRenderedListener onFirstFrameRenderedListener = new OnFirstFrameRenderedListener() { private final FlutterUiDisplayListener onFirstFrameRenderedListener = new FlutterUiDisplayListener() {
int i = 0;
@Override @Override
public void onFirstFrameRendered() { public void onFlutterUiDisplayed() {
if (splashScreen != null) {
if (FlutterBoost.instance().platform().whenEngineStart() == FlutterBoost.ConfigBuilder.FLUTTER_ACTIVITY_CREATED) { transitionToFlutter();
long now = new Date().getTime();
long flutterPostFrameCallTime = FlutterBoost.instance().getFlutterPostFrameCallTime();
if (flutterPostFrameCallTime != 0 && (now - flutterPostFrameCallTime) > 800) {
if (splashScreen != null) {
transitionToFlutter();
}
return;
}
handler.postDelayed(new Runnable() {
@Override
public void run() {
onFirstFrameRenderedListener.onFirstFrameRendered();
}
}, 200);
} else {
if (splashScreen != null) {
transitionToFlutter();
}
} }
}
@Override
public void onFlutterUiNoLongerDisplayed() {
} }
}; };
@NonNull @NonNull
......
org.gradle.jvmargs=-Xmx1536M org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment