Commit bc34068b authored by Yacumima's avatar Yacumima

dev

parent 5045f501
...@@ -2,17 +2,24 @@ package com.idlefish.flutterboost; ...@@ -2,17 +2,24 @@ package com.idlefish.flutterboost;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.view.Surface;
import com.idlefish.flutterboost.interfaces.IContainerRecord; import com.idlefish.flutterboost.interfaces.IContainerRecord;
import com.idlefish.flutterboost.interfaces.IStateListener; import com.idlefish.flutterboost.interfaces.IStateListener;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import io.flutter.app.FlutterPluginRegistry; import io.flutter.app.FlutterPluginRegistry;
import io.flutter.embedding.engine.FlutterEngine; import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterJNI;
import io.flutter.embedding.engine.dart.DartExecutor; import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.embedding.engine.renderer.FlutterRenderer;
import io.flutter.embedding.engine.renderer.OnFirstFrameRenderedListener;
import io.flutter.plugin.common.BinaryMessenger; import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.PluginRegistry; import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.platform.PlatformViewRegistry; import io.flutter.plugin.platform.PlatformViewRegistry;
...@@ -26,28 +33,41 @@ public class BoostFlutterEngine extends FlutterEngine { ...@@ -26,28 +33,41 @@ public class BoostFlutterEngine extends FlutterEngine {
protected final DartExecutor.DartEntrypoint mEntrypoint; protected final DartExecutor.DartEntrypoint mEntrypoint;
protected final String mInitRoute; protected final String mInitRoute;
private final FakeRender mFakeRender;
protected WeakReference<Activity> mCurrentActivityRef; protected WeakReference<Activity> mCurrentActivityRef;
public BoostFlutterEngine(@NonNull Context context) { public BoostFlutterEngine(@NonNull Context context) {
this(context,null,null); this(context, null, null);
} }
public BoostFlutterEngine(@NonNull Context context,DartExecutor.DartEntrypoint entrypoint,String initRoute) { public BoostFlutterEngine(@NonNull Context context, DartExecutor.DartEntrypoint entrypoint, String initRoute) {
super(context); super(context);
mContext = context.getApplicationContext(); mContext = context.getApplicationContext();
mBoostPluginRegistry = new BoostPluginRegistry(this,context); mBoostPluginRegistry = new BoostPluginRegistry(this, context);
if(entrypoint != null) { if (entrypoint != null) {
mEntrypoint = entrypoint; mEntrypoint = entrypoint;
}else{ } else {
mEntrypoint = defaultDartEntrypoint(context); mEntrypoint = defaultDartEntrypoint(context);
} }
if(initRoute != null) { if (initRoute != null) {
mInitRoute = initRoute; mInitRoute = initRoute;
}else{ } else {
mInitRoute = defaultInitialRoute(context); mInitRoute = defaultInitialRoute(context);
} }
FlutterJNI flutterJNI = null;
try {
Field field = FlutterEngine.class.getDeclaredField("flutterJNI");
field.setAccessible(true);
flutterJNI = (FlutterJNI) field.get(this);
} catch (Throwable t) {
Debuger.exception(t);
}
mFakeRender = new FakeRender(flutterJNI);
} }
public void startRun(@Nullable Activity activity) { public void startRun(@Nullable Activity activity) {
...@@ -61,7 +81,7 @@ public class BoostFlutterEngine extends FlutterEngine { ...@@ -61,7 +81,7 @@ public class BoostFlutterEngine extends FlutterEngine {
getDartExecutor().executeDartEntrypoint(mEntrypoint); getDartExecutor().executeDartEntrypoint(mEntrypoint);
final IStateListener stateListener = FlutterBoost.sInstance.mStateListener; final IStateListener stateListener = FlutterBoost.sInstance.mStateListener;
if(stateListener != null) { if (stateListener != null) {
stateListener.onEngineStarted(this); stateListener.onEngineStarted(this);
} }
...@@ -69,25 +89,45 @@ public class BoostFlutterEngine extends FlutterEngine { ...@@ -69,25 +89,45 @@ public class BoostFlutterEngine extends FlutterEngine {
} }
} }
protected DartExecutor.DartEntrypoint defaultDartEntrypoint(Context context){ protected DartExecutor.DartEntrypoint defaultDartEntrypoint(Context context) {
return new DartExecutor.DartEntrypoint( return new DartExecutor.DartEntrypoint(
context.getResources().getAssets(), context.getResources().getAssets(),
FlutterMain.findAppBundlePath(context), FlutterMain.findAppBundlePath(context),
"main"); "main");
} }
protected String defaultInitialRoute(Context context){ protected String defaultInitialRoute(Context context) {
return "/"; return "/";
} }
public BoostPluginRegistry getBoostPluginRegistry(){ public BoostPluginRegistry getBoostPluginRegistry() {
return mBoostPluginRegistry; return mBoostPluginRegistry;
} }
public boolean isRunning(){ public boolean isRunning() {
return getDartExecutor().isExecutingDart(); return getDartExecutor().isExecutingDart();
} }
@NonNull
@Override
public FlutterRenderer getRenderer() {
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
boolean hit = false;
for (StackTraceElement st : stackTrace) {
if (st.getMethodName().equals("sendViewportMetricsToFlutter")) {
hit = true;
break;
}
}
if (hit) {
return mFakeRender;
} else {
return super.getRenderer();
}
}
public class BoostPluginRegistry extends FlutterPluginRegistry { public class BoostPluginRegistry extends FlutterPluginRegistry {
private final FlutterEngine mEngine; private final FlutterEngine mEngine;
...@@ -97,7 +137,7 @@ public class BoostFlutterEngine extends FlutterEngine { ...@@ -97,7 +137,7 @@ public class BoostFlutterEngine extends FlutterEngine {
} }
public PluginRegistry.Registrar registrarFor(String pluginKey) { public PluginRegistry.Registrar registrarFor(String pluginKey) {
return new BoostRegistrar(mEngine,super.registrarFor(pluginKey)); return new BoostRegistrar(mEngine, super.registrarFor(pluginKey));
} }
} }
...@@ -117,21 +157,21 @@ public class BoostFlutterEngine extends FlutterEngine { ...@@ -117,21 +157,21 @@ public class BoostFlutterEngine extends FlutterEngine {
IContainerRecord record; IContainerRecord record;
record = FlutterBoost.singleton().containerManager().getCurrentTopRecord(); record = FlutterBoost.singleton().containerManager().getCurrentTopRecord();
if(record == null) { if (record == null) {
record = FlutterBoost.singleton().containerManager().getLastGenerateRecord(); record = FlutterBoost.singleton().containerManager().getLastGenerateRecord();
} }
if(record == null){ if (record == null) {
activity = FlutterBoost.singleton().currentActivity(); activity = FlutterBoost.singleton().currentActivity();
}else{ } else {
activity = record.getContainer().getContextActivity(); activity = record.getContainer().getContextActivity();
} }
if(activity == null && mCurrentActivityRef != null) { if (activity == null && mCurrentActivityRef != null) {
activity = mCurrentActivityRef.get(); activity = mCurrentActivityRef.get();
} }
if(activity == null) { if (activity == null) {
throw new RuntimeException("current has no valid Activity yet"); throw new RuntimeException("current has no valid Activity yet");
} }
...@@ -175,7 +215,7 @@ public class BoostFlutterEngine extends FlutterEngine { ...@@ -175,7 +215,7 @@ public class BoostFlutterEngine extends FlutterEngine {
@Override @Override
public String lookupKeyForAsset(String s, String s1) { public String lookupKeyForAsset(String s, String s1) {
return mRegistrar.lookupKeyForAsset(s,s1); return mRegistrar.lookupKeyForAsset(s, s1);
} }
@Override @Override
...@@ -208,4 +248,110 @@ public class BoostFlutterEngine extends FlutterEngine { ...@@ -208,4 +248,110 @@ public class BoostFlutterEngine extends FlutterEngine {
return mRegistrar.addViewDestroyListener(viewDestroyListener); return mRegistrar.addViewDestroyListener(viewDestroyListener);
} }
} }
private boolean viewportMetricsEqual(FlutterRenderer.ViewportMetrics a, FlutterRenderer.ViewportMetrics b) {
return a != null && b != null &&
a.height == b.height &&
a.width == b.width &&
a.devicePixelRatio == b.devicePixelRatio &&
a.paddingBottom == b.paddingBottom &&
a.paddingLeft == b.paddingLeft &&
a.paddingRight == b.paddingRight &&
a.paddingTop == b.paddingTop &&
a.viewInsetLeft == b.viewInsetLeft &&
a.viewInsetRight == b.viewInsetRight &&
a.viewInsetTop == b.viewInsetTop &&
a.viewInsetBottom == b.viewInsetBottom;
}
class FakeRender extends FlutterRenderer {
private ViewportMetrics last;
public FakeRender(FlutterJNI flutterJNI) {
super(flutterJNI);
}
@Override
public void setViewportMetrics(@NonNull ViewportMetrics viewportMetrics) {
if (viewportMetrics.width > 0 && viewportMetrics.height > 0 && !viewportMetricsEqual(last, viewportMetrics)) {
last = viewportMetrics;
Debuger.log("setViewportMetrics w:" + viewportMetrics.width + " h:" + viewportMetrics.height);
super.setViewportMetrics(viewportMetrics);
}
}
@Override
public void attachToRenderSurface(@NonNull RenderSurface renderSurface) {
Debuger.exception("should never called!");
}
@Override
public void detachFromRenderSurface() {
Debuger.exception("should never called!");
}
@Override
public void addOnFirstFrameRenderedListener(@NonNull OnFirstFrameRenderedListener listener) {
Debuger.exception("should never called!");
}
@Override
public void removeOnFirstFrameRenderedListener(@NonNull OnFirstFrameRenderedListener listener) {
Debuger.exception("should never called!");
}
@Override
public SurfaceTextureEntry createSurfaceTexture() {
Debuger.exception("should never called!");
return null;
}
@Override
public void surfaceCreated(Surface surface) {
Debuger.exception("should never called!");
}
@Override
public void surfaceChanged(int width, int height) {
Debuger.exception("should never called!");
}
@Override
public void surfaceDestroyed() {
Debuger.exception("should never called!");
}
@Override
public Bitmap getBitmap() {
Debuger.exception("should never called!");
return null;
}
@Override
public void dispatchPointerDataPacket(ByteBuffer buffer, int position) {
Debuger.exception("should never called!");
}
@Override
public boolean isSoftwareRenderingEnabled() {
Debuger.exception("should never called!");
return false;
}
@Override
public void setAccessibilityFeatures(int flags) {
Debuger.exception("should never called!");
}
@Override
public void setSemanticsEnabled(boolean enabled) {
Debuger.exception("should never called!");
}
@Override
public void dispatchSemanticsAction(int id, int action, ByteBuffer args, int argsPosition) {
Debuger.exception("should never called!");
}
}
} }
...@@ -26,24 +26,20 @@ package com.idlefish.flutterboost; ...@@ -26,24 +26,20 @@ package com.idlefish.flutterboost;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color; import android.graphics.Color;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment;
import android.os.SystemClock;
import android.support.v4.view.ViewCompat; import android.support.v4.view.ViewCompat;
import android.view.Gravity; import android.view.Gravity;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import com.idlefish.flutterboost.interfaces.IStateListener; import com.idlefish.flutterboost.interfaces.IStateListener;
import java.io.File; import java.lang.reflect.Method;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
...@@ -138,6 +134,19 @@ public class BoostFlutterView extends FrameLayout { ...@@ -138,6 +134,19 @@ public class BoostFlutterView extends FrameLayout {
if(stateListener != null) { if(stateListener != null) {
stateListener.onFlutterViewInited(mFlutterEngine,this); stateListener.onFlutterViewInited(mFlutterEngine,this);
} }
checkAssert();
}
private void checkAssert(){
try {
Method method = FlutterView.class.getDeclaredMethod("sendViewportMetricsToFlutter");
if(method == null) {
throw new Exception("method: FlutterView.sendViewportMetricsToFlutter not found!");
}
}catch (Throwable t){
Debuger.exception(t);
}
} }
protected View createRenderingProgressCorver(){ protected View createRenderingProgressCorver(){
......
...@@ -108,6 +108,9 @@ public class ContainerRecord implements IContainerRecord { ...@@ -108,6 +108,9 @@ public class ContainerRecord implements IContainerRecord {
mState = STATE_DISAPPEAR; mState = STATE_DISAPPEAR;
mProxy.disappear(); mProxy.disappear();
if(getContainer().getContextActivity().isFinishing()) {
mProxy.destroy();
}
mContainer.getBoostFlutterView().onDetach(); mContainer.getBoostFlutterView().onDetach();
......
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