Commit 08956ada authored by yangwu.jia's avatar yangwu.jia

泄漏问题解决

parent f0d3355e
package com.idlefish.flutterboost; package com.idlefish.flutterboost;
import android.app.Activity;
import android.content.Context; import io.flutter.Log;
import android.support.annotation.Nullable;
import com.idlefish.flutterboost.interfaces.IContainerRecord;
import io.flutter.app.FlutterPluginRegistry;
import io.flutter.embedding.engine.FlutterEngine; import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.plugins.shim.ShimPluginRegistry; import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.BinaryMessenger; import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.PluginRegistry; import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.platform.PlatformViewRegistry;
import io.flutter.view.FlutterView;
import io.flutter.view.TextureRegistry;
import java.lang.ref.WeakReference; import java.util.*;
public class BoostPluginRegistry extends ShimPluginRegistry {
protected WeakReference<Activity> mCurrentActivityRef;
private FlutterEngine mEngine; public class BoostPluginRegistry implements PluginRegistry {
private Context mContext; private static final String TAG = "ShimPluginRegistry";
public BoostPluginRegistry(FlutterEngine engine, Context context) { private final FlutterEngine flutterEngine;
super(engine); private final Map<String, Object> pluginMap = new HashMap();
mEngine = engine; private final BoostRegistrarAggregate shimRegistrarAggregate;
mContext=context;
}
public BoostRegistrarAggregate getRegistrarAggregate() {
return shimRegistrarAggregate;
}
public BoostPluginRegistry(FlutterEngine flutterEngine) {
this.flutterEngine = flutterEngine;
this.shimRegistrarAggregate = new BoostRegistrarAggregate();
this.flutterEngine.getPlugins().add(this.shimRegistrarAggregate);
}
public PluginRegistry.Registrar registrarFor(String pluginKey) { public Registrar registrarFor(String pluginKey) {
return new BoostRegistrar(mEngine, super.registrarFor(pluginKey)); Log.v("ShimPluginRegistry", "Creating plugin Registrar for '" + pluginKey + "'");
if (this.pluginMap.containsKey(pluginKey)) {
throw new IllegalStateException("Plugin key " + pluginKey + " is already in use");
} else {
this.pluginMap.put(pluginKey, (Object) null);
BoostRegistrar registrar = new BoostRegistrar(pluginKey, this.pluginMap);
this.shimRegistrarAggregate.addPlugin(registrar);
return registrar;
} }
}
public void currentActivity(@Nullable Activity activity) { public boolean hasPlugin(String pluginKey) {
mCurrentActivityRef = new WeakReference<>(activity); return this.pluginMap.containsKey(pluginKey);
} }
public class BoostRegistrar implements PluginRegistry.Registrar { public Object valuePublishedByPlugin(String pluginKey) {
return this.pluginMap.get(pluginKey);
}
private final PluginRegistry.Registrar mRegistrar; public static class BoostRegistrarAggregate implements FlutterPlugin, ActivityAware {
private final FlutterEngine mEngine; private final Set<BoostRegistrar> shimRegistrars;
private FlutterPluginBinding flutterPluginBinding;
private ActivityPluginBinding activityPluginBinding;
BoostRegistrar(FlutterEngine engine, PluginRegistry.Registrar registrar) { public ActivityPluginBinding getActivityPluginBinding() {
mRegistrar = registrar; return activityPluginBinding;
mEngine = engine;
} }
@Override private BoostRegistrarAggregate() {
public Activity activity() { this.shimRegistrars = new HashSet();
Activity activity; }
IContainerRecord record;
record = NewFlutterBoost.instance().containerManager().getCurrentTopRecord(); public void addPlugin(BoostRegistrar shimRegistrar) {
if (record == null) { this.shimRegistrars.add(shimRegistrar);
record = NewFlutterBoost.instance().containerManager().getLastGenerateRecord(); if (this.flutterPluginBinding != null) {
shimRegistrar.onAttachedToEngine(this.flutterPluginBinding);
} }
if (record == null) { if (this.activityPluginBinding != null) {
activity = NewFlutterBoost.instance().currentActivity(); shimRegistrar.onAttachedToActivity(this.activityPluginBinding);
} else {
activity = record.getContainer().getContextActivity();
} }
if (activity == null && mCurrentActivityRef != null) { }
activity = mCurrentActivityRef.get();
} public void onAttachedToEngine(FlutterPluginBinding binding) {
this.flutterPluginBinding = binding;
Iterator var2 = this.shimRegistrars.iterator();
if (activity == null) { while (var2.hasNext()) {
throw new RuntimeException("current has no valid Activity yet"); BoostRegistrar shimRegistrar = (BoostRegistrar) var2.next();
shimRegistrar.onAttachedToEngine(binding);
} }
return activity;
} }
@Override public void onDetachedFromEngine(FlutterPluginBinding binding) {
public Context context() { Iterator var2 = this.shimRegistrars.iterator();
return BoostPluginRegistry.this.mContext;
}
@Override while (var2.hasNext()) {
public Context activeContext() { BoostRegistrar shimRegistrar = (BoostRegistrar) var2.next();
return BoostPluginRegistry.this.mContext; shimRegistrar.onDetachedFromEngine(binding);
} }
@Override this.flutterPluginBinding = null;
public BinaryMessenger messenger() {
return mEngine.getDartExecutor();
} }
@Override public void onAttachedToActivity(ActivityPluginBinding binding) {
public TextureRegistry textures() { this.activityPluginBinding = binding;
return mEngine.getRenderer(); Iterator var2 = this.shimRegistrars.iterator();
}
@Override while (var2.hasNext()) {
public PlatformViewRegistry platformViewRegistry() { BoostRegistrar shimRegistrar = (BoostRegistrar) var2.next();
return mEngine.getPlatformViewsController().getRegistry(); shimRegistrar.onAttachedToActivity(binding);
} }
@Override
public FlutterView view() {
throw new RuntimeException("should not use!!!");
} }
@Override public void onDetachedFromActivityForConfigChanges() {
public String lookupKeyForAsset(String s) { Iterator var1 = this.shimRegistrars.iterator();
return mRegistrar.lookupKeyForAsset(s);
}
@Override while (var1.hasNext()) {
public String lookupKeyForAsset(String s, String s1) { BoostRegistrar shimRegistrar = (BoostRegistrar) var1.next();
return mRegistrar.lookupKeyForAsset(s, s1); shimRegistrar.onDetachedFromActivity();
} }
@Override this.activityPluginBinding = null;
public PluginRegistry.Registrar publish(Object o) {
return mRegistrar.publish(o);
} }
@Override public void onReattachedToActivityForConfigChanges(ActivityPluginBinding binding) {
public PluginRegistry.Registrar addRequestPermissionsResultListener(PluginRegistry.RequestPermissionsResultListener requestPermissionsResultListener) { Iterator var2 = this.shimRegistrars.iterator();
return mRegistrar.addRequestPermissionsResultListener(requestPermissionsResultListener);
}
@Override while (var2.hasNext()) {
public PluginRegistry.Registrar addActivityResultListener(PluginRegistry.ActivityResultListener activityResultListener) { BoostRegistrar shimRegistrar = (BoostRegistrar) var2.next();
return mRegistrar.addActivityResultListener(activityResultListener); shimRegistrar.onReattachedToActivityForConfigChanges(binding);
} }
@Override
public PluginRegistry.Registrar addNewIntentListener(PluginRegistry.NewIntentListener newIntentListener) {
return mRegistrar.addNewIntentListener(newIntentListener);
} }
@Override public void onDetachedFromActivity() {
public PluginRegistry.Registrar addUserLeaveHintListener(PluginRegistry.UserLeaveHintListener userLeaveHintListener) { Iterator var1 = this.shimRegistrars.iterator();
return mRegistrar.addUserLeaveHintListener(userLeaveHintListener);
} while (var1.hasNext()) {
BoostRegistrar shimRegistrar = (BoostRegistrar) var1.next();
shimRegistrar.onDetachedFromActivity();
}
@Override
public PluginRegistry.Registrar addViewDestroyListener(PluginRegistry.ViewDestroyListener viewDestroyListener) {
return mRegistrar.addViewDestroyListener(viewDestroyListener);
} }
} }
} }
package com.idlefish.flutterboost;
import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import io.flutter.Log;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.FlutterPlugin.FlutterPluginBinding;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.PluginRegistry.ActivityResultListener;
import io.flutter.plugin.common.PluginRegistry.NewIntentListener;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import io.flutter.plugin.common.PluginRegistry.RequestPermissionsResultListener;
import io.flutter.plugin.common.PluginRegistry.UserLeaveHintListener;
import io.flutter.plugin.common.PluginRegistry.ViewDestroyListener;
import io.flutter.plugin.platform.PlatformViewRegistry;
import io.flutter.view.FlutterMain;
import io.flutter.view.FlutterNativeView;
import io.flutter.view.FlutterView;
import io.flutter.view.TextureRegistry;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
class BoostRegistrar implements Registrar, FlutterPlugin, ActivityAware {
private static final String TAG = "ShimRegistrar";
private final Map<String, Object> globalRegistrarMap;
private final String pluginId;
private final Set<ViewDestroyListener> viewDestroyListeners = new HashSet();
private final Set<RequestPermissionsResultListener> requestPermissionsResultListeners = new HashSet();
private final Set<ActivityResultListener> activityResultListeners = new HashSet();
private final Set<NewIntentListener> newIntentListeners = new HashSet();
private final Set<UserLeaveHintListener> userLeaveHintListeners = new HashSet();
private FlutterPluginBinding pluginBinding;
private ActivityPluginBinding activityPluginBinding;
public BoostRegistrar(@NonNull String pluginId, @NonNull Map<String, Object> globalRegistrarMap) {
this.pluginId = pluginId;
this.globalRegistrarMap = globalRegistrarMap;
}
public Activity activity() {
return this.activityPluginBinding != null ? this.activityPluginBinding.getActivity() : null;
}
public Context context() {
return this.pluginBinding != null ? this.pluginBinding.getApplicationContext() : null;
}
public Context activeContext() {
return (Context)(this.activityPluginBinding == null ? this.context() : this.activity());
}
public BinaryMessenger messenger() {
return this.pluginBinding != null ? this.pluginBinding.getFlutterEngine().getDartExecutor() : null;
}
public TextureRegistry textures() {
return this.pluginBinding != null ? this.pluginBinding.getFlutterEngine().getRenderer() : null;
}
public PlatformViewRegistry platformViewRegistry() {
return this.pluginBinding != null ? this.pluginBinding.getFlutterEngine().getPlatformViewsController().getRegistry() : null;
}
public FlutterView view() {
throw new UnsupportedOperationException("The new embedding does not support the old FlutterView.");
}
public String lookupKeyForAsset(String asset) {
return FlutterMain.getLookupKeyForAsset(asset);
}
public String lookupKeyForAsset(String asset, String packageName) {
return FlutterMain.getLookupKeyForAsset(asset, packageName);
}
public Registrar publish(Object value) {
this.globalRegistrarMap.put(this.pluginId, value);
return this;
}
public Registrar addRequestPermissionsResultListener(RequestPermissionsResultListener listener) {
this.requestPermissionsResultListeners.add(listener);
if (this.activityPluginBinding != null) {
this.activityPluginBinding.addRequestPermissionsResultListener(listener);
}
return this;
}
public Registrar addActivityResultListener(ActivityResultListener listener) {
this.activityResultListeners.add(listener);
if (this.activityPluginBinding != null) {
this.activityPluginBinding.addActivityResultListener(listener);
}
return this;
}
public Registrar addNewIntentListener(NewIntentListener listener) {
this.newIntentListeners.add(listener);
if (this.activityPluginBinding != null) {
this.activityPluginBinding.addOnNewIntentListener(listener);
}
return this;
}
public Registrar addUserLeaveHintListener(UserLeaveHintListener listener) {
this.userLeaveHintListeners.add(listener);
if (this.activityPluginBinding != null) {
this.activityPluginBinding.addOnUserLeaveHintListener(listener);
}
return this;
}
@NonNull
public Registrar addViewDestroyListener(@NonNull ViewDestroyListener listener) {
this.viewDestroyListeners.add(listener);
return this;
}
public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
Log.v("ShimRegistrar", "Attached to FlutterEngine.");
this.pluginBinding = binding;
}
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
Log.v("ShimRegistrar", "Detached from FlutterEngine.");
Iterator var2 = this.viewDestroyListeners.iterator();
while(var2.hasNext()) {
ViewDestroyListener listener = (ViewDestroyListener)var2.next();
listener.onViewDestroy((FlutterNativeView)null);
}
this.pluginBinding = null;
}
public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
Log.v("ShimRegistrar", "Attached to an Activity.");
this.activityPluginBinding = binding;
this.addExistingListenersToActivityPluginBinding();
}
public void onDetachedFromActivityForConfigChanges() {
Log.v("ShimRegistrar", "Detached from an Activity for config changes.");
this.activityPluginBinding = null;
}
public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {
Log.v("ShimRegistrar", "Reconnected to an Activity after config changes.");
this.activityPluginBinding = binding;
this.addExistingListenersToActivityPluginBinding();
}
public void onDetachedFromActivity() {
Log.v("ShimRegistrar", "Detached from an Activity.");
this.activityPluginBinding = null;
}
private void addExistingListenersToActivityPluginBinding() {
Iterator var1 = this.requestPermissionsResultListeners.iterator();
while(var1.hasNext()) {
RequestPermissionsResultListener listener = (RequestPermissionsResultListener)var1.next();
this.activityPluginBinding.addRequestPermissionsResultListener(listener);
}
var1 = this.activityResultListeners.iterator();
while(var1.hasNext()) {
ActivityResultListener listener = (ActivityResultListener)var1.next();
this.activityPluginBinding.addActivityResultListener(listener);
}
var1 = this.newIntentListeners.iterator();
while(var1.hasNext()) {
NewIntentListener listener = (NewIntentListener)var1.next();
this.activityPluginBinding.addOnNewIntentListener(listener);
}
var1 = this.userLeaveHintListeners.iterator();
while(var1.hasNext()) {
UserLeaveHintListener listener = (UserLeaveHintListener)var1.next();
this.activityPluginBinding.addOnUserLeaveHintListener(listener);
}
}
}
...@@ -156,9 +156,9 @@ public class NewFlutterBoost { ...@@ -156,9 +156,9 @@ public class NewFlutterBoost {
); );
flutterEngine.getDartExecutor().executeDartEntrypoint(entrypoint); flutterEngine.getDartExecutor().executeDartEntrypoint(entrypoint);
mRegistry = new BoostPluginRegistry(createEngine(),mPlatform.getApplication()); mRegistry = new BoostPluginRegistry(createEngine());
((BoostPluginRegistry) mRegistry).currentActivity(mCurrentActiveActivity); // ((BoostPluginRegistry) mRegistry).currentActivity(mCurrentActiveActivity);
mPlatform.registerPlugins(mRegistry); mPlatform.registerPlugins(mRegistry);
......
...@@ -19,6 +19,7 @@ import java.util.HashMap; ...@@ -19,6 +19,7 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.idlefish.flutterboost.BoostPluginRegistry;
import com.idlefish.flutterboost.NewFlutterBoost; import com.idlefish.flutterboost.NewFlutterBoost;
import com.idlefish.flutterboost.Utils; import com.idlefish.flutterboost.Utils;
import com.idlefish.flutterboost.XFlutterView; import com.idlefish.flutterboost.XFlutterView;
...@@ -29,6 +30,7 @@ import io.flutter.app.FlutterActivity; ...@@ -29,6 +30,7 @@ import io.flutter.app.FlutterActivity;
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.FlutterShellArgs; import io.flutter.embedding.engine.FlutterShellArgs;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.platform.PlatformPlugin; import io.flutter.plugin.platform.PlatformPlugin;
import io.flutter.view.FlutterMain; import io.flutter.view.FlutterMain;
...@@ -98,24 +100,6 @@ public class FlutterActivityAndFragmentDelegate implements IFlutterViewContaine ...@@ -98,24 +100,6 @@ public class FlutterActivityAndFragmentDelegate implements IFlutterViewContaine
// use-cases. // use-cases.
platformPlugin = host.providePlatformPlugin(host.getActivity(), flutterEngine); platformPlugin = host.providePlatformPlugin(host.getActivity(), flutterEngine);
if (host.shouldAttachEngineToActivity()) {
// Notify any plugins that are currently attached to our FlutterEngine that they
// are now attached to an Activity.
//
// Passing this Fragment's Lifecycle should be sufficient because as long as this Fragment
// is attached to its Activity, the lifecycles should be in sync. Once this Fragment is
// detached from its Activity, that Activity will be detached from the FlutterEngine, too,
// which means there shouldn't be any possibility for the Fragment Lifecycle to get out of
// sync with the Activity. We use the Fragment's Lifecycle because it is possible that the
// attached Activity is not a LifecycleOwner.
Log.d(TAG, "Attaching FlutterEngine to the Activity that owns this Fragment.");
flutterEngine.getActivityControlSurface().attachToActivity(
host.getActivity(),
host.getLifecycle()
);
}
host.configureFlutterEngine(flutterEngine); host.configureFlutterEngine(flutterEngine);
} }
...@@ -188,6 +172,11 @@ public class FlutterActivityAndFragmentDelegate implements IFlutterViewContaine ...@@ -188,6 +172,11 @@ public class FlutterActivityAndFragmentDelegate implements IFlutterViewContaine
Log.v(TAG, "onResume()"); Log.v(TAG, "onResume()");
ensureAlive(); ensureAlive();
flutterEngine.getLifecycleChannel().appIsResumed(); flutterEngine.getLifecycleChannel().appIsResumed();
flutterEngine.getActivityControlSurface().attachToActivity(
host.getActivity(),
host.getLifecycle()
);
} }
...@@ -211,7 +200,8 @@ public class FlutterActivityAndFragmentDelegate implements IFlutterViewContaine ...@@ -211,7 +200,8 @@ public class FlutterActivityAndFragmentDelegate implements IFlutterViewContaine
void onStop() { void onStop() {
Log.v(TAG, "onStop()"); Log.v(TAG, "onStop()");
ensureAlive(); ensureAlive();
// flutterView.detachFromFlutterEngine();
} }
void onDestroyView() { void onDestroyView() {
...@@ -219,6 +209,13 @@ public class FlutterActivityAndFragmentDelegate implements IFlutterViewContaine ...@@ -219,6 +209,13 @@ public class FlutterActivityAndFragmentDelegate implements IFlutterViewContaine
mSyncer.onDestroy(); mSyncer.onDestroy();
ensureAlive(); ensureAlive();
BoostPluginRegistry registry= (BoostPluginRegistry)NewFlutterBoost.instance().getPluginRegistry();
ActivityPluginBinding binding=registry.getRegistrarAggregate().getActivityPluginBinding();
if(binding!=null&&(binding.getActivity()==this.host.getActivity())){
registry.getRegistrarAggregate().onDetachedFromActivityForConfigChanges();
flutterEngine.getActivityControlSurface().detachFromActivityForConfigChanges();
}
} }
...@@ -226,15 +223,7 @@ public class FlutterActivityAndFragmentDelegate implements IFlutterViewContaine ...@@ -226,15 +223,7 @@ public class FlutterActivityAndFragmentDelegate implements IFlutterViewContaine
Log.v(TAG, "onDetach()"); Log.v(TAG, "onDetach()");
ensureAlive(); ensureAlive();
if (host.shouldAttachEngineToActivity()) {
// Notify plugins that they are no longer attached to an Activity.
Log.d(TAG, "Detaching FlutterEngine from the Activity that owns this Fragment.");
if (host.getActivity().isChangingConfigurations()) {
flutterEngine.getActivityControlSurface().detachFromActivityForConfigChanges();
} else {
flutterEngine.getActivityControlSurface().detachFromActivity();
}
}
// Null out the platformPlugin to avoid a possible retain cycle between the plugin, this Fragment, // Null out the platformPlugin to avoid a possible retain cycle between the plugin, this Fragment,
// and this Fragment's Activity. // and this Fragment's Activity.
......
...@@ -71,7 +71,6 @@ public class FlutterSplashView extends FrameLayout { ...@@ -71,7 +71,6 @@ public class FlutterSplashView extends FrameLayout {
public void run() { public void run() {
removeView(splashScreenView); removeView(splashScreenView);
previousCompletedSplashIsolate = transitioningIsolateId; previousCompletedSplashIsolate = transitioningIsolateId;
int i=0;
} }
}; };
......
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