MD5 校验值:d555a6389a0a865314e2706996812829
FlutterEngineConnectionRegistry.java 文件包含反编译后的源代码,请注意,该内容仅供学习和参考使用,不得用于非法用途。
package ho.flutter.embedding.engine; import android.app.Activity; import android.app.Service; import android.content.BroadcastReceiver; import android.content.ContentProvider; import android.content.Context; import android.content.Intent; import android.os.Bundle; import androidx.lifecycle.Lifecycle; import ho.flutter.Log; import ho.flutter.embedding.android.ExclusiveAppComponent; import ho.flutter.embedding.engine.loader.FlutterLoader; import ho.flutter.embedding.engine.plugins.FlutterPlugin; import ho.flutter.embedding.engine.plugins.PluginRegistry; import ho.flutter.embedding.engine.plugins.activity.ActivityAware; import ho.flutter.embedding.engine.plugins.activity.ActivityControlSurface; import ho.flutter.embedding.engine.plugins.activity.ActivityPluginBinding; import ho.flutter.embedding.engine.plugins.broadcastreceiver.BroadcastReceiverAware; import ho.flutter.embedding.engine.plugins.broadcastreceiver.BroadcastReceiverControlSurface; import ho.flutter.embedding.engine.plugins.broadcastreceiver.BroadcastReceiverPluginBinding; import ho.flutter.embedding.engine.plugins.contentprovider.ContentProviderAware; import ho.flutter.embedding.engine.plugins.contentprovider.ContentProviderControlSurface; import ho.flutter.embedding.engine.plugins.contentprovider.ContentProviderPluginBinding; import ho.flutter.embedding.engine.plugins.lifecycle.HiddenLifecycleReference; import ho.flutter.embedding.engine.plugins.service.ServiceAware; import ho.flutter.embedding.engine.plugins.service.ServiceControlSurface; import ho.flutter.embedding.engine.plugins.service.ServicePluginBinding; import ho.flutter.plugin.common.PluginRegistry; import ho.flutter.util.TraceSection; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Set; public class FlutterEngineConnectionRegistry implements PluginRegistry, ActivityControlSurface, ServiceControlSurface, BroadcastReceiverControlSurface, ContentProviderControlSurface { private static final String TAG = "FlutterEngineCxnRegstry"; private FlutterEngineActivityPluginBinding activityPluginBinding; private BroadcastReceiver broadcastReceiver; private FlutterEngineBroadcastReceiverPluginBinding broadcastReceiverPluginBinding; private ContentProvider contentProvider; private FlutterEngineContentProviderPluginBinding contentProviderPluginBinding; private ExclusiveAppComponent<Activity> exclusiveActivity; private final FlutterEngine flutterEngine; private final FlutterPlugin.FlutterPluginBinding pluginBinding; private Service service; private FlutterEngineServicePluginBinding servicePluginBinding; private final Map<Class<? extends FlutterPlugin>, FlutterPlugin> plugins = new HashMap(); private final Map<Class<? extends FlutterPlugin>, ActivityAware> activityAwarePlugins = new HashMap(); private boolean isWaitingForActivityReattachment = false; private final Map<Class<? extends FlutterPlugin>, ServiceAware> serviceAwarePlugins = new HashMap(); private final Map<Class<? extends FlutterPlugin>, BroadcastReceiverAware> broadcastReceiverAwarePlugins = new HashMap(); private final Map<Class<? extends FlutterPlugin>, ContentProviderAware> contentProviderAwarePlugins = new HashMap(); public FlutterEngineConnectionRegistry(Context context, FlutterEngine flutterEngine, FlutterLoader flutterLoader) { this.flutterEngine = flutterEngine; this.pluginBinding = new FlutterPlugin.FlutterPluginBinding(context, flutterEngine, flutterEngine.getDartExecutor(), flutterEngine.getRenderer(), flutterEngine.getPlatformViewsController().getRegistry(), new DefaultFlutterAssets(flutterLoader)); } public void destroy() { Log.v(TAG, "Destroying."); detachFromAppComponent(); removeAll(); } @Override public void add(FlutterPlugin flutterPlugin) { TraceSection.begin("FlutterEngineConnectionRegistry#add " + flutterPlugin.getClass().getSimpleName()); try { if (has(flutterPlugin.getClass())) { Log.w(TAG, "Attempted to register plugin (" + flutterPlugin + ") but it was already registered with this FlutterEngine (" + this.flutterEngine + ")."); return; } Log.v(TAG, "Adding plugin: " + flutterPlugin); this.plugins.put(flutterPlugin.getClass(), flutterPlugin); flutterPlugin.onAttachedToEngine(this.pluginBinding); if (flutterPlugin instanceof ActivityAware) { ActivityAware activityAware = (ActivityAware) flutterPlugin; this.activityAwarePlugins.put(flutterPlugin.getClass(), activityAware); if (isAttachedToActivity()) { activityAware.onAttachedToActivity(this.activityPluginBinding); } } if (flutterPlugin instanceof ServiceAware) { ServiceAware serviceAware = (ServiceAware) flutterPlugin; this.serviceAwarePlugins.put(flutterPlugin.getClass(), serviceAware); if (isAttachedToService()) { serviceAware.onAttachedToService(this.servicePluginBinding); } } if (flutterPlugin instanceof BroadcastReceiverAware) { BroadcastReceiverAware broadcastReceiverAware = (BroadcastReceiverAware) flutterPlugin; this.broadcastReceiverAwarePlugins.put(flutterPlugin.getClass(), broadcastReceiverAware); if (isAttachedToBroadcastReceiver()) { broadcastReceiverAware.onAttachedToBroadcastReceiver(this.broadcastReceiverPluginBinding); } } if (flutterPlugin instanceof ContentProviderAware) { ContentProviderAware contentProviderAware = (ContentProviderAware) flutterPlugin; this.contentProviderAwarePlugins.put(flutterPlugin.getClass(), contentProviderAware); if (isAttachedToContentProvider()) { contentProviderAware.onAttachedToContentProvider(this.contentProviderPluginBinding); } } } finally { TraceSection.end(); } } @Override public void add(Set<FlutterPlugin> set) { Iterator<FlutterPlugin> it = set.iterator(); while (it.hasNext()) { add(it.next()); } } @Override public boolean has(Class<? extends FlutterPlugin> cls) { return this.plugins.containsKey(cls); } @Override public FlutterPlugin get(Class<? extends FlutterPlugin> cls) { return this.plugins.get(cls); } @Override public void remove(Class<? extends FlutterPlugin> cls) { FlutterPlugin flutterPlugin = this.plugins.get(cls); if (flutterPlugin == null) { return; } TraceSection.begin("FlutterEngineConnectionRegistry#remove " + cls.getSimpleName()); try { if (flutterPlugin instanceof ActivityAware) { if (isAttachedToActivity()) { ((ActivityAware) flutterPlugin).onDetachedFromActivity(); } this.activityAwarePlugins.remove(cls); } if (flutterPlugin instanceof ServiceAware) { if (isAttachedToService()) { ((ServiceAware) flutterPlugin).onDetachedFromService(); } this.serviceAwarePlugins.remove(cls); } if (flutterPlugin instanceof BroadcastReceiverAware) { if (isAttachedToBroadcastReceiver()) { ((BroadcastReceiverAware) flutterPlugin).onDetachedFromBroadcastReceiver(); } this.broadcastReceiverAwarePlugins.remove(cls); } if (flutterPlugin instanceof ContentProviderAware) { if (isAttachedToContentProvider()) { ((ContentProviderAware) flutterPlugin).onDetachedFromContentProvider(); } this.contentProviderAwarePlugins.remove(cls); } flutterPlugin.onDetachedFromEngine(this.pluginBinding); this.plugins.remove(cls); } finally { TraceSection.end(); } } @Override public void remove(Set<Class<? extends FlutterPlugin>> set) { Iterator<Class<? extends FlutterPlugin>> it = set.iterator(); while (it.hasNext()) { remove(it.next()); } } @Override public void removeAll() { remove(new HashSet(this.plugins.keySet())); this.plugins.clear(); } private void detachFromAppComponent() { if (isAttachedToActivity()) { detachFromActivity(); return; } if (isAttachedToService()) { detachFromService(); } else if (isAttachedToBroadcastReceiver()) { detachFromBroadcastReceiver(); } else if (isAttachedToContentProvider()) { detachFromContentProvider(); } } private boolean isAttachedToActivity() { return this.exclusiveActivity != null; } private Activity attachedActivity() { ExclusiveAppComponent<Activity> exclusiveAppComponent = this.exclusiveActivity; if (exclusiveAppComponent != null) { return exclusiveAppComponent.getAppComponent(); } return null; } @Override public void attachToActivity(ExclusiveAppComponent<Activity> exclusiveAppComponent, Lifecycle lifecycle) { TraceSection.begin("FlutterEngineConnectionRegistry#attachToActivity"); try { ExclusiveAppComponent<Activity> exclusiveAppComponent2 = this.exclusiveActivity; if (exclusiveAppComponent2 != null) { exclusiveAppComponent2.detachFromFlutterEngine(); } detachFromAppComponent(); this.exclusiveActivity = exclusiveAppComponent; attachToActivityInternal(exclusiveAppComponent.getAppComponent(), lifecycle); } finally { TraceSection.end(); } } private void attachToActivityInternal(Activity activity, Lifecycle lifecycle) { this.activityPluginBinding = new FlutterEngineActivityPluginBinding(activity, lifecycle); this.flutterEngine.getPlatformViewsController().setSoftwareRendering(activity.getIntent().getBooleanExtra("enable-software-rendering", false)); this.flutterEngine.getPlatformViewsController().attach(activity, this.flutterEngine.getRenderer(), this.flutterEngine.getDartExecutor()); for (ActivityAware activityAware : this.activityAwarePlugins.values()) { if (this.isWaitingForActivityReattachment) { activityAware.onReattachedToActivityForConfigChanges(this.activityPluginBinding); } else { activityAware.onAttachedToActivity(this.activityPluginBinding); } } this.isWaitingForActivityReattachment = false; } @Override public void detachFromActivityForConfigChanges() { if (isAttachedToActivity()) { TraceSection.begin("FlutterEngineConnectionRegistry#detachFromActivityForConfigChanges"); try { this.isWaitingForActivityReattachment = true; Iterator<ActivityAware> it = this.activityAwarePlugins.values().iterator(); while (it.hasNext()) { it.next().onDetachedFromActivityForConfigChanges(); } detachFromActivityInternal(); return; } finally { TraceSection.end(); } } Log.e(TAG, "Attempted to detach plugins from an Activity when no Activity was attached."); } @Override public void detachFromActivity() { if (isAttachedToActivity()) { TraceSection.begin("FlutterEngineConnectionRegistry#detachFromActivity"); try { Iterator<ActivityAware> it = this.activityAwarePlugins.values().iterator(); while (it.hasNext()) { it.next().onDetachedFromActivity(); } detachFromActivityInternal(); return; } finally { TraceSection.end(); } } Log.e(TAG, "Attempted to detach plugins from an Activity when no Activity was attached."); } private void detachFromActivityInternal() { this.flutterEngine.getPlatformViewsController().detach(); this.exclusiveActivity = null; this.activityPluginBinding = null; } @Override public boolean onRequestPermissionsResult(int i, String[] strArr, int[] iArr) { if (isAttachedToActivity()) { TraceSection.begin("FlutterEngineConnectionRegistry#onRequestPermissionsResult"); try { return this.activityPluginBinding.onRequestPermissionsResult(i, strArr, iArr); } finally { TraceSection.end(); } } Log.e(TAG, "Attempted to notify ActivityAware plugins of onRequestPermissionsResult, but no Activity was attached."); return false; } @Override public boolean onActivityResult(int i, int i2, Intent intent) { if (isAttachedToActivity()) { TraceSection.begin("FlutterEngineConnectionRegistry#onActivityResult"); try { return this.activityPluginBinding.onActivityResult(i, i2, intent); } finally { TraceSection.end(); } } Log.e(TAG, "Attempted to notify ActivityAware plugins of onActivityResult, but no Activity was attached."); return false; } @Override public void onNewIntent(Intent intent) { if (isAttachedToActivity()) { TraceSection.begin("FlutterEngineConnectionRegistry#onNewIntent"); try { this.activityPluginBinding.onNewIntent(intent); return; } finally { TraceSection.end(); } } Log.e(TAG, "Attempted to notify ActivityAware plugins of onNewIntent, but no Activity was attached."); } @Override public void onUserLeaveHint() { if (isAttachedToActivity()) { TraceSection.begin("FlutterEngineConnectionRegistry#onUserLeaveHint"); try { this.activityPluginBinding.onUserLeaveHint(); return; } finally { TraceSection.end(); } } Log.e(TAG, "Attempted to notify ActivityAware plugins of onUserLeaveHint, but no Activity was attached."); } @Override public void onSaveInstanceState(Bundle bundle) { if (isAttachedToActivity()) { TraceSection.begin("FlutterEngineConnectionRegistry#onSaveInstanceState"); try { this.activityPluginBinding.onSaveInstanceState(bundle); return; } finally { TraceSection.end(); } } Log.e(TAG, "Attempted to notify ActivityAware plugins of onSaveInstanceState, but no Activity was attached."); } @Override public void onRestoreInstanceState(Bundle bundle) { if (isAttachedToActivity()) { TraceSection.begin("FlutterEngineConnectionRegistry#onRestoreInstanceState"); try { this.activityPluginBinding.onRestoreInstanceState(bundle); return; } finally { TraceSection.end(); } } Log.e(TAG, "Attempted to notify ActivityAware plugins of onRestoreInstanceState, but no Activity was attached."); } private boolean isAttachedToService() { return this.service != null; } @Override public void attachToService(Service service, Lifecycle lifecycle, boolean z) { TraceSection.begin("FlutterEngineConnectionRegistry#attachToService"); try { detachFromAppComponent(); this.service = service; this.servicePluginBinding = new FlutterEngineServicePluginBinding(service, lifecycle); Iterator<ServiceAware> it = this.serviceAwarePlugins.values().iterator(); while (it.hasNext()) { it.next().onAttachedToService(this.servicePluginBinding); } } finally { TraceSection.end(); } } @Override public void detachFromService() { if (isAttachedToService()) { TraceSection.begin("FlutterEngineConnectionRegistry#detachFromService"); try { Iterator<ServiceAware> it = this.serviceAwarePlugins.values().iterator(); while (it.hasNext()) { it.next().onDetachedFromService(); } this.service = null; this.servicePluginBinding = null; return; } finally { TraceSection.end(); } } Log.e(TAG, "Attempted to detach plugins from a Service when no Service was attached."); } @Override public void onMoveToForeground() { if (isAttachedToService()) { TraceSection.begin("FlutterEngineConnectionRegistry#onMoveToForeground"); try { this.servicePluginBinding.onMoveToForeground(); } finally { TraceSection.end(); } } } @Override public void onMoveToBackground() { if (isAttachedToService()) { TraceSection.begin("FlutterEngineConnectionRegistry#onMoveToBackground"); try { this.servicePluginBinding.onMoveToBackground(); } finally { TraceSection.end(); } } } private boolean isAttachedToBroadcastReceiver() { return this.broadcastReceiver != null; } @Override public void attachToBroadcastReceiver(BroadcastReceiver broadcastReceiver, Lifecycle lifecycle) { TraceSection.begin("FlutterEngineConnectionRegistry#attachToBroadcastReceiver"); try { detachFromAppComponent(); this.broadcastReceiver = broadcastReceiver; this.broadcastReceiverPluginBinding = new FlutterEngineBroadcastReceiverPluginBinding(broadcastReceiver); Iterator<BroadcastReceiverAware> it = this.broadcastReceiverAwarePlugins.values().iterator(); while (it.hasNext()) { it.next().onAttachedToBroadcastReceiver(this.broadcastReceiverPluginBinding); } } finally { TraceSection.end(); } } @Override public void detachFromBroadcastReceiver() { if (isAttachedToBroadcastReceiver()) { TraceSection.begin("FlutterEngineConnectionRegistry#detachFromBroadcastReceiver"); try { Iterator<BroadcastReceiverAware> it = this.broadcastReceiverAwarePlugins.values().iterator(); while (it.hasNext()) { it.next().onDetachedFromBroadcastReceiver(); } return; } finally { TraceSection.end(); } } Log.e(TAG, "Attempted to detach plugins from a BroadcastReceiver when no BroadcastReceiver was attached."); } private boolean isAttachedToContentProvider() { return this.contentProvider != null; } @Override public void attachToContentProvider(ContentProvider contentProvider, Lifecycle lifecycle) { TraceSection.begin("FlutterEngineConnectionRegistry#attachToContentProvider"); try { detachFromAppComponent(); this.contentProvider = contentProvider; this.contentProviderPluginBinding = new FlutterEngineContentProviderPluginBinding(contentProvider); Iterator<ContentProviderAware> it = this.contentProviderAwarePlugins.values().iterator(); while (it.hasNext()) { it.next().onAttachedToContentProvider(this.contentProviderPluginBinding); } } finally { TraceSection.end(); } } @Override public void detachFromContentProvider() { if (isAttachedToContentProvider()) { TraceSection.begin("FlutterEngineConnectionRegistry#detachFromContentProvider"); try { Iterator<ContentProviderAware> it = this.contentProviderAwarePlugins.values().iterator(); while (it.hasNext()) { it.next().onDetachedFromContentProvider(); } return; } finally { TraceSection.end(); } } Log.e(TAG, "Attempted to detach plugins from a ContentProvider when no ContentProvider was attached."); } private static class DefaultFlutterAssets implements FlutterPlugin.FlutterAssets { final FlutterLoader flutterLoader; private DefaultFlutterAssets(FlutterLoader flutterLoader) { this.flutterLoader = flutterLoader; } @Override public String getAssetFilePathByName(String str) { return this.flutterLoader.getLookupKeyForAsset(str); } @Override public String getAssetFilePathByName(String str, String str2) { return this.flutterLoader.getLookupKeyForAsset(str, str2); } @Override public String getAssetFilePathBySubpath(String str) { return this.flutterLoader.getLookupKeyForAsset(str); } @Override public String getAssetFilePathBySubpath(String str, String str2) { return this.flutterLoader.getLookupKeyForAsset(str, str2); } } public static class FlutterEngineActivityPluginBinding implements ActivityPluginBinding { private final Activity activity; private final HiddenLifecycleReference hiddenLifecycleReference; private final Set<PluginRegistry.RequestPermissionsResultListener> onRequestPermissionsResultListeners = new HashSet(); private final Set<PluginRegistry.ActivityResultListener> onActivityResultListeners = new HashSet(); private final Set<PluginRegistry.NewIntentListener> onNewIntentListeners = new HashSet(); private final Set<PluginRegistry.UserLeaveHintListener> onUserLeaveHintListeners = new HashSet(); private final Set<ActivityPluginBinding.OnSaveInstanceStateListener> onSaveInstanceStateListeners = new HashSet(); public FlutterEngineActivityPluginBinding(Activity activity, Lifecycle lifecycle) { this.activity = activity; this.hiddenLifecycleReference = new HiddenLifecycleReference(lifecycle); } @Override public Activity getActivity() { return this.activity; } @Override public Object getLifecycle() { return this.hiddenLifecycleReference; } @Override public void addRequestPermissionsResultListener(PluginRegistry.RequestPermissionsResultListener requestPermissionsResultListener) { this.onRequestPermissionsResultListeners.add(requestPermissionsResultListener); } @Override public void removeRequestPermissionsResultListener(PluginRegistry.RequestPermissionsResultListener requestPermissionsResultListener) { this.onRequestPermissionsResultListeners.remove(requestPermissionsResultListener); } boolean onRequestPermissionsResult(int i, String[] strArr, int[] iArr) { Iterator<PluginRegistry.RequestPermissionsResultListener> it = this.onRequestPermissionsResultListeners.iterator(); while (true) { boolean z = false; while (it.hasNext()) { if (it.next().onRequestPermissionsResult(i, strArr, iArr) || z) { z = true; } } return z; } } @Override public void addActivityResultListener(PluginRegistry.ActivityResultListener activityResultListener) { this.onActivityResultListeners.add(activityResultListener); } @Override public void removeActivityResultListener(PluginRegistry.ActivityResultListener activityResultListener) { this.onActivityResultListeners.remove(activityResultListener); } boolean onActivityResult(int i, int i2, Intent intent) { Iterator it = new HashSet(this.onActivityResultListeners).iterator(); while (true) { boolean z = false; while (it.hasNext()) { if (((PluginRegistry.ActivityResultListener) it.next()).onActivityResult(i, i2, intent) || z) { z = true; } } return z; } } @Override public void addOnNewIntentListener(PluginRegistry.NewIntentListener newIntentListener) { this.onNewIntentListeners.add(newIntentListener); } @Override public void removeOnNewIntentListener(PluginRegistry.NewIntentListener newIntentListener) { this.onNewIntentListeners.remove(newIntentListener); } void onNewIntent(Intent intent) { Iterator<PluginRegistry.NewIntentListener> it = this.onNewIntentListeners.iterator(); while (it.hasNext()) { it.next().onNewIntent(intent); } } @Override public void addOnUserLeaveHintListener(PluginRegistry.UserLeaveHintListener userLeaveHintListener) { this.onUserLeaveHintListeners.add(userLeaveHintListener); } @Override public void removeOnUserLeaveHintListener(PluginRegistry.UserLeaveHintListener userLeaveHintListener) { this.onUserLeaveHintListeners.remove(userLeaveHintListener); } @Override public void addOnSaveStateListener(ActivityPluginBinding.OnSaveInstanceStateListener onSaveInstanceStateListener) { this.onSaveInstanceStateListeners.add(onSaveInstanceStateListener); } @Override public void removeOnSaveStateListener(ActivityPluginBinding.OnSaveInstanceStateListener onSaveInstanceStateListener) { this.onSaveInstanceStateListeners.remove(onSaveInstanceStateListener); } void onUserLeaveHint() { Iterator<PluginRegistry.UserLeaveHintListener> it = this.onUserLeaveHintListeners.iterator(); while (it.hasNext()) { it.next().onUserLeaveHint(); } } void onSaveInstanceState(Bundle bundle) { Iterator<ActivityPluginBinding.OnSaveInstanceStateListener> it = this.onSaveInstanceStateListeners.iterator(); while (it.hasNext()) { it.next().onSaveInstanceState(bundle); } } void onRestoreInstanceState(Bundle bundle) { Iterator<ActivityPluginBinding.OnSaveInstanceStateListener> it = this.onSaveInstanceStateListeners.iterator(); while (it.hasNext()) { it.next().onRestoreInstanceState(bundle); } } } public static class FlutterEngineServicePluginBinding implements ServicePluginBinding { private final HiddenLifecycleReference hiddenLifecycleReference; private final Set<ServiceAware.OnModeChangeListener> onModeChangeListeners = new HashSet(); private final Service service; FlutterEngineServicePluginBinding(Service service, Lifecycle lifecycle) { this.service = service; this.hiddenLifecycleReference = lifecycle != null ? new HiddenLifecycleReference(lifecycle) : null; } @Override public Service getService() { return this.service; } @Override public Object getLifecycle() { return this.hiddenLifecycleReference; } @Override public void addOnModeChangeListener(ServiceAware.OnModeChangeListener onModeChangeListener) { this.onModeChangeListeners.add(onModeChangeListener); } @Override public void removeOnModeChangeListener(ServiceAware.OnModeChangeListener onModeChangeListener) { this.onModeChangeListeners.remove(onModeChangeListener); } void onMoveToForeground() { Iterator<ServiceAware.OnModeChangeListener> it = this.onModeChangeListeners.iterator(); while (it.hasNext()) { it.next().onMoveToForeground(); } } void onMoveToBackground() { Iterator<ServiceAware.OnModeChangeListener> it = this.onModeChangeListeners.iterator(); while (it.hasNext()) { it.next().onMoveToBackground(); } } } public static class FlutterEngineBroadcastReceiverPluginBinding implements BroadcastReceiverPluginBinding { private final BroadcastReceiver broadcastReceiver; FlutterEngineBroadcastReceiverPluginBinding(BroadcastReceiver broadcastReceiver) { this.broadcastReceiver = broadcastReceiver; } @Override public BroadcastReceiver getBroadcastReceiver() { return this.broadcastReceiver; } } public static class FlutterEngineContentProviderPluginBinding implements ContentProviderPluginBinding { private final ContentProvider contentProvider; FlutterEngineContentProviderPluginBinding(ContentProvider contentProvider) { this.contentProvider = contentProvider; } @Override public ContentProvider getContentProvider() { return this.contentProvider; } } }