2018-11-07 12:24:35 -08:00
|
|
|
// Copyright 2013 The Flutter Authors. All rights reserved.
|
2015-01-30 14:05:57 -08:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
2016-04-08 09:39:14 -07:00
|
|
|
package io.flutter.view;
|
2015-01-30 14:05:57 -08:00
|
|
|
|
|
|
|
|
import android.content.Context;
|
2019-04-08 14:46:41 -07:00
|
|
|
import android.os.Handler;
|
2019-05-20 10:08:50 -07:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
|
import android.support.annotation.Nullable;
|
2019-10-29 19:46:21 -07:00
|
|
|
import android.support.annotation.VisibleForTesting;
|
2019-06-20 17:13:58 -07:00
|
|
|
|
2019-10-11 16:15:55 -07:00
|
|
|
import io.flutter.embedding.engine.loader.FlutterLoader;
|
2015-01-30 14:05:57 -08:00
|
|
|
|
|
|
|
|
/**
|
2016-04-08 09:39:14 -07:00
|
|
|
* A class to intialize the Flutter engine.
|
2016-08-18 16:27:16 -07:00
|
|
|
*/
|
2016-04-08 09:39:14 -07:00
|
|
|
public class FlutterMain {
|
2016-08-18 16:27:16 -07:00
|
|
|
|
2017-01-12 15:47:18 -08:00
|
|
|
public static class Settings {
|
|
|
|
|
private String logTag;
|
|
|
|
|
|
2019-05-20 10:08:50 -07:00
|
|
|
@Nullable
|
2017-01-12 15:47:18 -08:00
|
|
|
public String getLogTag() {
|
|
|
|
|
return logTag;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the tag associated with Flutter app log messages.
|
2017-02-03 16:25:25 -08:00
|
|
|
* @param tag Log tag.
|
2017-01-12 15:47:18 -08:00
|
|
|
*/
|
|
|
|
|
public void setLogTag(String tag) {
|
|
|
|
|
logTag = tag;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-30 14:05:57 -08:00
|
|
|
/**
|
2016-03-15 16:00:33 -07:00
|
|
|
* Starts initialization of the native system.
|
2017-02-03 16:25:25 -08:00
|
|
|
* @param applicationContext The Android application context.
|
2016-08-18 16:27:16 -07:00
|
|
|
*/
|
2019-05-20 10:08:50 -07:00
|
|
|
public static void startInitialization(@NonNull Context applicationContext) {
|
2019-10-29 19:46:21 -07:00
|
|
|
if (isRunningInRobolectricTest) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-10-11 16:15:55 -07:00
|
|
|
FlutterLoader.getInstance().startInitialization(applicationContext);
|
2017-01-12 15:47:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Starts initialization of the native system.
|
2019-10-11 16:15:55 -07:00
|
|
|
* <p>
|
|
|
|
|
* This loads the Flutter engine's native library to enable subsequent JNI calls. This also
|
|
|
|
|
* starts locating and unpacking Dart resources packaged in the app's APK.
|
|
|
|
|
* <p>
|
|
|
|
|
* Calling this method multiple times has no effect.
|
|
|
|
|
*
|
2017-02-03 16:25:25 -08:00
|
|
|
* @param applicationContext The Android application context.
|
|
|
|
|
* @param settings Configuration settings.
|
2017-01-12 15:47:18 -08:00
|
|
|
*/
|
2019-05-20 10:08:50 -07:00
|
|
|
public static void startInitialization(@NonNull Context applicationContext, @NonNull Settings settings) {
|
2019-10-29 19:46:21 -07:00
|
|
|
if (isRunningInRobolectricTest) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-10-11 16:15:55 -07:00
|
|
|
FlutterLoader.Settings newSettings = new FlutterLoader.Settings();
|
|
|
|
|
newSettings.setLogTag(settings.getLogTag());
|
|
|
|
|
FlutterLoader.getInstance().startInitialization(applicationContext, newSettings);
|
2016-03-15 16:00:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Blocks until initialization of the native system has completed.
|
2019-10-11 16:15:55 -07:00
|
|
|
* <p>
|
|
|
|
|
* Calling this method multiple times has no effect.
|
|
|
|
|
*
|
2017-02-03 16:25:25 -08:00
|
|
|
* @param applicationContext The Android application context.
|
|
|
|
|
* @param args Flags sent to the Flutter runtime.
|
2016-08-18 16:27:16 -07:00
|
|
|
*/
|
2019-05-20 10:08:50 -07:00
|
|
|
public static void ensureInitializationComplete(@NonNull Context applicationContext, @Nullable String[] args) {
|
2019-10-29 19:46:21 -07:00
|
|
|
if (isRunningInRobolectricTest) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-10-11 16:15:55 -07:00
|
|
|
FlutterLoader.getInstance().ensureInitializationComplete(applicationContext, args);
|
2015-01-30 14:05:57 -08:00
|
|
|
}
|
|
|
|
|
|
2019-04-08 14:46:41 -07:00
|
|
|
/**
|
|
|
|
|
* Same as {@link #ensureInitializationComplete(Context, String[])} but waiting on a background
|
|
|
|
|
* thread, then invoking {@code callback} on the {@code callbackHandler}.
|
|
|
|
|
*/
|
|
|
|
|
public static void ensureInitializationCompleteAsync(
|
2019-05-20 10:08:50 -07:00
|
|
|
@NonNull Context applicationContext,
|
|
|
|
|
@Nullable String[] args,
|
|
|
|
|
@NonNull Handler callbackHandler,
|
|
|
|
|
@NonNull Runnable callback
|
2019-04-08 14:46:41 -07:00
|
|
|
) {
|
2019-10-29 19:46:21 -07:00
|
|
|
if (isRunningInRobolectricTest) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-10-11 16:15:55 -07:00
|
|
|
FlutterLoader.getInstance().ensureInitializationCompleteAsync(
|
|
|
|
|
applicationContext, args, callbackHandler, callback);
|
2016-03-15 16:00:33 -07:00
|
|
|
}
|
|
|
|
|
|
2019-07-30 12:57:01 -07:00
|
|
|
@NonNull
|
|
|
|
|
public static String findAppBundlePath() {
|
2019-10-11 16:15:55 -07:00
|
|
|
return FlutterLoader.getInstance().findAppBundlePath();
|
2019-07-30 12:57:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Deprecated
|
2019-05-20 10:08:50 -07:00
|
|
|
@Nullable
|
|
|
|
|
public static String findAppBundlePath(@NonNull Context applicationContext) {
|
2019-10-11 16:15:55 -07:00
|
|
|
return FlutterLoader.getInstance().findAppBundlePath();
|
2016-07-11 13:44:42 -07:00
|
|
|
}
|
2018-03-16 12:59:57 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the file name for the given asset.
|
|
|
|
|
* The returned file name can be used to access the asset in the APK
|
2018-12-19 09:24:02 -08:00
|
|
|
* through the {@link android.content.res.AssetManager} API.
|
2018-03-16 12:59:57 +01:00
|
|
|
*
|
|
|
|
|
* @param asset the name of the asset. The name can be hierarchical
|
2018-12-19 09:24:02 -08:00
|
|
|
* @return the filename to be used with {@link android.content.res.AssetManager}
|
2018-03-16 12:59:57 +01:00
|
|
|
*/
|
2019-05-20 10:08:50 -07:00
|
|
|
@NonNull
|
|
|
|
|
public static String getLookupKeyForAsset(@NonNull String asset) {
|
2019-10-11 16:15:55 -07:00
|
|
|
return FlutterLoader.getInstance().getLookupKeyForAsset(asset);
|
2018-03-16 12:59:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the file name for the given asset which originates from the
|
|
|
|
|
* specified packageName. The returned file name can be used to access
|
2018-12-19 09:24:02 -08:00
|
|
|
* the asset in the APK through the {@link android.content.res.AssetManager} API.
|
2018-03-16 12:59:57 +01:00
|
|
|
*
|
|
|
|
|
* @param asset the name of the asset. The name can be hierarchical
|
|
|
|
|
* @param packageName the name of the package from which the asset originates
|
2018-12-19 09:24:02 -08:00
|
|
|
* @return the file name to be used with {@link android.content.res.AssetManager}
|
2018-03-16 12:59:57 +01:00
|
|
|
*/
|
2019-05-20 10:08:50 -07:00
|
|
|
@NonNull
|
|
|
|
|
public static String getLookupKeyForAsset(@NonNull String asset, @NonNull String packageName) {
|
2019-10-11 16:15:55 -07:00
|
|
|
return FlutterLoader.getInstance().getLookupKeyForAsset(asset, packageName);
|
2018-03-16 12:59:57 +01:00
|
|
|
}
|
2019-10-29 19:46:21 -07:00
|
|
|
|
|
|
|
|
private static boolean isRunningInRobolectricTest = false;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Indicates whether we are currently running in a Robolectric Test.
|
|
|
|
|
*
|
|
|
|
|
* <p> Flutter cannot be initialized inside a Robolectric environment since it cannot load
|
|
|
|
|
* native libraries.
|
|
|
|
|
*
|
|
|
|
|
* @deprecated Use the new embedding (io.flutter.embedding) instead which provides better
|
|
|
|
|
* modularity for testing.
|
|
|
|
|
*/
|
|
|
|
|
@Deprecated
|
|
|
|
|
@VisibleForTesting
|
|
|
|
|
public static void setIsRunningInRobolectricTest(boolean isRunningInRobolectricTest) {
|
|
|
|
|
FlutterMain.isRunningInRobolectricTest = isRunningInRobolectricTest;
|
|
|
|
|
}
|
2015-01-30 14:05:57 -08:00
|
|
|
}
|