MD5 校验值:d555a6389a0a865314e2706996812829
FlutterActivityAndFragmentDelegate.java 文件包含反编译后的源代码,请注意,该内容仅供学习和参考使用,不得用于非法用途。
package ko.flutter.embedding.android; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.ViewTreeObserver; import androidx.lifecycle.Lifecycle; import java.util.Arrays; import java.util.List; import ko.flutter.FlutterInjector; import ko.flutter.Log; import ko.flutter.embedding.engine.FlutterEngine; import ko.flutter.embedding.engine.FlutterEngineCache; import ko.flutter.embedding.engine.FlutterShellArgs; import ko.flutter.embedding.engine.dart.DartExecutor; import ko.flutter.embedding.engine.renderer.FlutterUiDisplayListener; import ko.flutter.plugin.platform.PlatformPlugin; import ko.flutter.util.ViewUtils; class FlutterActivityAndFragmentDelegate implements ExclusiveAppComponent<Activity> { private static final int FLUTTER_SPLASH_VIEW_FALLBACK_ID = 486947586; private static final String FRAMEWORK_RESTORATION_BUNDLE_KEY = "framework"; private static final String PLUGINS_RESTORATION_BUNDLE_KEY = "plugins"; private static final String TAG = "FlutterActivityAndFragmentDelegate"; ViewTreeObserver.OnPreDrawListener activePreDrawListener; private FlutterEngine flutterEngine; FlutterView flutterView; private Host host; private boolean isAttached; private boolean isFlutterEngineFromHost; private boolean isFlutterUiDisplayed; private PlatformPlugin platformPlugin; private final FlutterUiDisplayListener flutterUiDisplayListener = new FlutterUiDisplayListener() { @Override public void onFlutterUiDisplayed() { FlutterActivityAndFragmentDelegate.this.host.onFlutterUiDisplayed(); FlutterActivityAndFragmentDelegate.this.isFlutterUiDisplayed = true; FlutterActivityAndFragmentDelegate.this.isFirstFrameRendered = true; } @Override public void onFlutterUiNoLongerDisplayed() { FlutterActivityAndFragmentDelegate.this.host.onFlutterUiNoLongerDisplayed(); FlutterActivityAndFragmentDelegate.this.isFlutterUiDisplayed = false; } }; private boolean isFirstFrameRendered = false; public interface DelegateFactory { FlutterActivityAndFragmentDelegate createDelegate(Host host); } public interface Host extends SplashScreenProvider, FlutterEngineProvider, FlutterEngineConfigurator, PlatformPlugin.PlatformPluginDelegate { void cleanUpFlutterEngine(FlutterEngine flutterEngine); void configureFlutterEngine(FlutterEngine flutterEngine); void detachFromFlutterEngine(); Activity getActivity(); String getAppBundlePath(); String getCachedEngineId(); Context getContext(); List<String> getDartEntrypointArgs(); String getDartEntrypointFunctionName(); String getDartEntrypointLibraryUri(); ExclusiveAppComponent<Activity> getExclusiveAppComponent(); FlutterShellArgs getFlutterShellArgs(); String getInitialRoute(); Lifecycle getLifecycle(); RenderMode getRenderMode(); TransparencyMode getTransparencyMode(); void onFlutterSurfaceViewCreated(FlutterSurfaceView flutterSurfaceView); void onFlutterTextureViewCreated(FlutterTextureView flutterTextureView); void onFlutterUiDisplayed(); void onFlutterUiNoLongerDisplayed(); FlutterEngine provideFlutterEngine(Context context); PlatformPlugin providePlatformPlugin(Activity activity, FlutterEngine flutterEngine); @Override SplashScreen provideSplashScreen(); boolean shouldAttachEngineToActivity(); boolean shouldDestroyEngineWithHost(); boolean shouldDispatchAppLifecycleState(); boolean shouldHandleDeeplinking(); boolean shouldRestoreAndSaveState(); void updateSystemUiOverlays(); } public FlutterActivityAndFragmentDelegate(Host host) { this.host = host; } public void release() { this.host = null; this.flutterEngine = null; this.flutterView = null; this.platformPlugin = null; } public FlutterEngine getFlutterEngine() { return this.flutterEngine; } public boolean isFlutterEngineFromHost() { return this.isFlutterEngineFromHost; } public boolean isAttached() { return this.isAttached; } public void onAttach(Context context) { ensureAlive(); if (this.flutterEngine == null) { setupFlutterEngine(); } if (this.host.shouldAttachEngineToActivity()) { Log.v(TAG, "Attaching FlutterEngine to the Activity that owns this delegate."); this.flutterEngine.getActivityControlSurface().attachToActivity(this, this.host.getLifecycle()); } Host host = this.host; this.platformPlugin = host.providePlatformPlugin(host.getActivity(), this.flutterEngine); this.host.configureFlutterEngine(this.flutterEngine); this.isAttached = true; } @Override public Activity getAppComponent() { Activity activity = this.host.getActivity(); if (activity != null) { return activity; } throw new AssertionError("FlutterActivityAndFragmentDelegate's getAppComponent should only be queried after onAttach, when the host's activity should always be non-null"); } void setupFlutterEngine() { Log.v(TAG, "Setting up FlutterEngine."); String cachedEngineId = this.host.getCachedEngineId(); if (cachedEngineId != null) { FlutterEngine flutterEngine = FlutterEngineCache.getInstance().get(cachedEngineId); this.flutterEngine = flutterEngine; this.isFlutterEngineFromHost = true; if (flutterEngine != null) { return; } throw new IllegalStateException("The requested cached FlutterEngine did not exist in the FlutterEngineCache: '" + cachedEngineId + "'"); } Host host = this.host; FlutterEngine provideFlutterEngine = host.provideFlutterEngine(host.getContext()); this.flutterEngine = provideFlutterEngine; if (provideFlutterEngine != null) { this.isFlutterEngineFromHost = true; return; } Log.v(TAG, "No preferred FlutterEngine was provided. Creating a new FlutterEngine for this FlutterFragment."); this.flutterEngine = new FlutterEngine(this.host.getContext(), this.host.getFlutterShellArgs().toArray(), false, this.host.shouldRestoreAndSaveState()); this.isFlutterEngineFromHost = false; } public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle, int i, boolean z) { Log.v(TAG, "Creating FlutterView."); ensureAlive(); if (this.host.getRenderMode() == RenderMode.surface) { FlutterSurfaceView flutterSurfaceView = new FlutterSurfaceView(this.host.getContext(), this.host.getTransparencyMode() == TransparencyMode.transparent); this.host.onFlutterSurfaceViewCreated(flutterSurfaceView); this.flutterView = new FlutterView(this.host.getContext(), flutterSurfaceView); } else { FlutterTextureView flutterTextureView = new FlutterTextureView(this.host.getContext()); flutterTextureView.setOpaque(this.host.getTransparencyMode() == TransparencyMode.opaque); this.host.onFlutterTextureViewCreated(flutterTextureView); this.flutterView = new FlutterView(this.host.getContext(), flutterTextureView); } this.flutterView.addOnFirstFrameRenderedListener(this.flutterUiDisplayListener); Log.v(TAG, "Attaching FlutterEngine to FlutterView."); this.flutterView.attachToFlutterEngine(this.flutterEngine); this.flutterView.setId(i); SplashScreen provideSplashScreen = this.host.provideSplashScreen(); if (provideSplashScreen != null) { Log.w(TAG, "A splash screen was provided to Flutter, but this is deprecated. See flutter.dev/go/android-splash-migration for migration steps."); FlutterSplashView flutterSplashView = new FlutterSplashView(this.host.getContext()); flutterSplashView.setId(ViewUtils.generateViewId(FLUTTER_SPLASH_VIEW_FALLBACK_ID)); flutterSplashView.displayFlutterViewWithSplash(this.flutterView, provideSplashScreen); return flutterSplashView; } if (z) { delayFirstAndroidViewDraw(this.flutterView); } return this.flutterView; } public void onRestoreInstanceState(Bundle bundle) { Bundle bundle2; Log.v(TAG, "onRestoreInstanceState. Giving framework and plugins an opportunity to restore state."); ensureAlive(); byte[] bArr = null; if (bundle != null) { Bundle bundle3 = bundle.getBundle(PLUGINS_RESTORATION_BUNDLE_KEY); bArr = bundle.getByteArray(FRAMEWORK_RESTORATION_BUNDLE_KEY); bundle2 = bundle3; } else { bundle2 = null; } if (this.host.shouldRestoreAndSaveState()) { this.flutterEngine.getRestorationChannel().setRestorationData(bArr); } if (this.host.shouldAttachEngineToActivity()) { this.flutterEngine.getActivityControlSurface().onRestoreInstanceState(bundle2); } } public void onStart() { Log.v(TAG, "onStart()"); ensureAlive(); doInitialFlutterViewRun(); this.flutterView.setVisibility(0); } private void doInitialFlutterViewRun() { String str; DartExecutor.DartEntrypoint dartEntrypoint; if (this.host.getCachedEngineId() == null && !this.flutterEngine.getDartExecutor().isExecutingDart()) { String initialRoute = this.host.getInitialRoute(); if (initialRoute == null && (initialRoute = maybeGetInitialRouteFromIntent(this.host.getActivity().getIntent())) == null) { initialRoute = "/"; } String dartEntrypointLibraryUri = this.host.getDartEntrypointLibraryUri(); if (("Executing Dart entrypoint: " + this.host.getDartEntrypointFunctionName() + ", library uri: " + dartEntrypointLibraryUri) == null) { str = "\"\""; } else { str = dartEntrypointLibraryUri + ", and sending initial route: " + initialRoute; } Log.v(TAG, str); this.flutterEngine.getNavigationChannel().setInitialRoute(initialRoute); String appBundlePath = this.host.getAppBundlePath(); if (appBundlePath == null || appBundlePath.isEmpty()) { appBundlePath = FlutterInjector.instance().flutterLoader().findAppBundlePath(); } if (dartEntrypointLibraryUri == null) { dartEntrypoint = new DartExecutor.DartEntrypoint(appBundlePath, this.host.getDartEntrypointFunctionName()); } else { dartEntrypoint = new DartExecutor.DartEntrypoint(appBundlePath, dartEntrypointLibraryUri, this.host.getDartEntrypointFunctionName()); } this.flutterEngine.getDartExecutor().executeDartEntrypoint(dartEntrypoint, this.host.getDartEntrypointArgs()); } } private String maybeGetInitialRouteFromIntent(Intent intent) { Uri data; String path; if (!this.host.shouldHandleDeeplinking() || (data = intent.getData()) == null || (path = data.getPath()) == null || path.isEmpty()) { return null; } if (data.getQuery() != null && !data.getQuery().isEmpty()) { path = path + "?" + data.getQuery(); } if (data.getFragment() == null || data.getFragment().isEmpty()) { return path; } return path + "#" + data.getFragment(); } private void delayFirstAndroidViewDraw(final FlutterView flutterView) { if (this.host.getRenderMode() != RenderMode.surface) { throw new IllegalArgumentException("Cannot delay the first Android view draw when the render mode is not set to `RenderMode.surface`."); } if (this.activePreDrawListener != null) { flutterView.getViewTreeObserver().removeOnPreDrawListener(this.activePreDrawListener); } this.activePreDrawListener = new ViewTreeObserver.OnPreDrawListener() { @Override public boolean onPreDraw() { if (FlutterActivityAndFragmentDelegate.this.isFlutterUiDisplayed && FlutterActivityAndFragmentDelegate.this.activePreDrawListener != null) { flutterView.getViewTreeObserver().removeOnPreDrawListener(this); FlutterActivityAndFragmentDelegate.this.activePreDrawListener = null; } return FlutterActivityAndFragmentDelegate.this.isFlutterUiDisplayed; } }; flutterView.getViewTreeObserver().addOnPreDrawListener(this.activePreDrawListener); } public void onResume() { Log.v(TAG, "onResume()"); ensureAlive(); if (this.host.shouldDispatchAppLifecycleState()) { this.flutterEngine.getLifecycleChannel().appIsResumed(); } } public void onPostResume() { Log.v(TAG, "onPostResume()"); ensureAlive(); if (this.flutterEngine != null) { updateSystemUiOverlays(); } else { Log.w(TAG, "onPostResume() invoked before FlutterFragment was attached to an Activity."); } } public void updateSystemUiOverlays() { PlatformPlugin platformPlugin = this.platformPlugin; if (platformPlugin != null) { platformPlugin.updateSystemUiOverlays(); } } public void onPause() { Log.v(TAG, "onPause()"); ensureAlive(); if (this.host.shouldDispatchAppLifecycleState()) { this.flutterEngine.getLifecycleChannel().appIsInactive(); } } public void onStop() { Log.v(TAG, "onStop()"); ensureAlive(); if (this.host.shouldDispatchAppLifecycleState()) { this.flutterEngine.getLifecycleChannel().appIsPaused(); } this.flutterView.setVisibility(8); } public void onDestroyView() { Log.v(TAG, "onDestroyView()"); ensureAlive(); if (this.activePreDrawListener != null) { this.flutterView.getViewTreeObserver().removeOnPreDrawListener(this.activePreDrawListener); this.activePreDrawListener = null; } this.flutterView.detachFromFlutterEngine(); this.flutterView.removeOnFirstFrameRenderedListener(this.flutterUiDisplayListener); } public void onSaveInstanceState(Bundle bundle) { Log.v(TAG, "onSaveInstanceState. Giving framework and plugins an opportunity to save state."); ensureAlive(); if (this.host.shouldRestoreAndSaveState()) { bundle.putByteArray(FRAMEWORK_RESTORATION_BUNDLE_KEY, this.flutterEngine.getRestorationChannel().getRestorationData()); } if (this.host.shouldAttachEngineToActivity()) { Bundle bundle2 = new Bundle(); this.flutterEngine.getActivityControlSurface().onSaveInstanceState(bundle2); bundle.putBundle(PLUGINS_RESTORATION_BUNDLE_KEY, bundle2); } } @Override public void detachFromFlutterEngine() { if (this.host.shouldDestroyEngineWithHost()) { throw new AssertionError("The internal FlutterEngine created by " + this.host + " has been attached to by another activity. To persist a FlutterEngine beyond the ownership of this activity, explicitly create a FlutterEngine"); } this.host.detachFromFlutterEngine(); } public void onDetach() { Log.v(TAG, "onDetach()"); ensureAlive(); this.host.cleanUpFlutterEngine(this.flutterEngine); if (this.host.shouldAttachEngineToActivity()) { Log.v(TAG, "Detaching FlutterEngine from the Activity that owns this Fragment."); if (this.host.getActivity().isChangingConfigurations()) { this.flutterEngine.getActivityControlSurface().detachFromActivityForConfigChanges(); } else { this.flutterEngine.getActivityControlSurface().detachFromActivity(); } } PlatformPlugin platformPlugin = this.platformPlugin; if (platformPlugin != null) { platformPlugin.destroy(); this.platformPlugin = null; } if (this.host.shouldDispatchAppLifecycleState()) { this.flutterEngine.getLifecycleChannel().appIsDetached(); } if (this.host.shouldDestroyEngineWithHost()) { this.flutterEngine.destroy(); if (this.host.getCachedEngineId() != null) { FlutterEngineCache.getInstance().remove(this.host.getCachedEngineId()); } this.flutterEngine = null; } this.isAttached = false; } public void onBackPressed() { ensureAlive(); if (this.flutterEngine != null) { Log.v(TAG, "Forwarding onBackPressed() to FlutterEngine."); this.flutterEngine.getNavigationChannel().popRoute(); } else { Log.w(TAG, "Invoked onBackPressed() before FlutterFragment was attached to an Activity."); } } public void onRequestPermissionsResult(int i, String[] strArr, int[] iArr) { ensureAlive(); if (this.flutterEngine != null) { Log.v(TAG, "Forwarding onRequestPermissionsResult() to FlutterEngine:\nrequestCode: " + i + "\npermissions: " + Arrays.toString(strArr) + "\ngrantResults: " + Arrays.toString(iArr)); this.flutterEngine.getActivityControlSurface().onRequestPermissionsResult(i, strArr, iArr); return; } Log.w(TAG, "onRequestPermissionResult() invoked before FlutterFragment was attached to an Activity."); } public void onNewIntent(Intent intent) { ensureAlive(); if (this.flutterEngine != null) { Log.v(TAG, "Forwarding onNewIntent() to FlutterEngine and sending pushRoute message."); this.flutterEngine.getActivityControlSurface().onNewIntent(intent); String maybeGetInitialRouteFromIntent = maybeGetInitialRouteFromIntent(intent); if (maybeGetInitialRouteFromIntent == null || maybeGetInitialRouteFromIntent.isEmpty()) { return; } this.flutterEngine.getNavigationChannel().pushRoute(maybeGetInitialRouteFromIntent); return; } Log.w(TAG, "onNewIntent() invoked before FlutterFragment was attached to an Activity."); } public void onActivityResult(int i, int i2, Intent intent) { ensureAlive(); if (this.flutterEngine != null) { Log.v(TAG, "Forwarding onActivityResult() to FlutterEngine:\nrequestCode: " + i + "\nresultCode: " + i2 + "\ndata: " + intent); this.flutterEngine.getActivityControlSurface().onActivityResult(i, i2, intent); return; } Log.w(TAG, "onActivityResult() invoked before FlutterFragment was attached to an Activity."); } public void onUserLeaveHint() { ensureAlive(); if (this.flutterEngine != null) { Log.v(TAG, "Forwarding onUserLeaveHint() to FlutterEngine."); this.flutterEngine.getActivityControlSurface().onUserLeaveHint(); } else { Log.w(TAG, "onUserLeaveHint() invoked before FlutterFragment was attached to an Activity."); } } public void onTrimMemory(int i) { ensureAlive(); FlutterEngine flutterEngine = this.flutterEngine; if (flutterEngine != null) { if (this.isFirstFrameRendered && i >= 10) { flutterEngine.getDartExecutor().notifyLowMemoryWarning(); this.flutterEngine.getSystemChannel().sendMemoryPressureWarning(); } this.flutterEngine.getRenderer().onTrimMemory(i); } } private void ensureAlive() { if (this.host == null) { throw new IllegalStateException("Cannot execute method on a destroyed FlutterActivityAndFragmentDelegate."); } } }