Files
engine/shell/platform/android/io/flutter/view/FlutterMain.java
T

155 lines
5.4 KiB
Java
Raw Normal View History

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.
package io.flutter.view;
2015-01-30 14:05:57 -08:00
import android.content.Context;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import io.flutter.embedding.engine.loader.FlutterLoader;
2015-01-30 14:05:57 -08:00
/**
* A class to intialize the Flutter engine.
*/
public class FlutterMain {
public static class Settings {
private String logTag;
@Nullable
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.
*/
public void setLogTag(String tag) {
logTag = tag;
}
}
2015-01-30 14:05:57 -08:00
/**
* Starts initialization of the native system.
2017-02-03 16:25:25 -08:00
* @param applicationContext The Android application context.
*/
public static void startInitialization(@NonNull Context applicationContext) {
if (isRunningInRobolectricTest) {
return;
}
FlutterLoader.getInstance().startInitialization(applicationContext);
}
/**
* Starts initialization of the native system.
* <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.
*/
public static void startInitialization(@NonNull Context applicationContext, @NonNull Settings settings) {
if (isRunningInRobolectricTest) {
return;
}
FlutterLoader.Settings newSettings = new FlutterLoader.Settings();
newSettings.setLogTag(settings.getLogTag());
FlutterLoader.getInstance().startInitialization(applicationContext, newSettings);
}
/**
* Blocks until initialization of the native system has completed.
* <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.
*/
public static void ensureInitializationComplete(@NonNull Context applicationContext, @Nullable String[] args) {
if (isRunningInRobolectricTest) {
return;
}
FlutterLoader.getInstance().ensureInitializationComplete(applicationContext, args);
2015-01-30 14:05:57 -08: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(
@NonNull Context applicationContext,
@Nullable String[] args,
@NonNull Handler callbackHandler,
@NonNull Runnable callback
) {
if (isRunningInRobolectricTest) {
return;
}
FlutterLoader.getInstance().ensureInitializationCompleteAsync(
applicationContext, args, callbackHandler, callback);
}
@NonNull
public static String findAppBundlePath() {
return FlutterLoader.getInstance().findAppBundlePath();
}
@Deprecated
@Nullable
public static String findAppBundlePath(@NonNull Context applicationContext) {
return FlutterLoader.getInstance().findAppBundlePath();
}
/**
* 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.
*
* @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}
*/
@NonNull
public static String getLookupKeyForAsset(@NonNull String asset) {
return FlutterLoader.getInstance().getLookupKeyForAsset(asset);
}
/**
* 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.
*
* @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}
*/
@NonNull
public static String getLookupKeyForAsset(@NonNull String asset, @NonNull String packageName) {
return FlutterLoader.getInstance().getLookupKeyForAsset(asset, packageName);
}
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
}