Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 0cc67bb

Browse files
committed
Implementation of loadFlutterAsset on Android
1 parent dba90a5 commit 0cc67bb

21 files changed

+2095
-2174
lines changed

packages/webview_flutter/webview_flutter_android/android/build.gradle

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,4 @@ android {
5858
}
5959
}
6060
}
61-
compileOptions {
62-
sourceCompatibility JavaVersion.VERSION_1_8
63-
targetCompatibility JavaVersion.VERSION_1_8
64-
}
6561
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
package io.flutter.plugins.webviewflutter;
6+
7+
import android.content.res.AssetManager;
8+
import androidx.annotation.NonNull;
9+
import io.flutter.embedding.engine.plugins.FlutterPlugin;
10+
import io.flutter.plugin.common.PluginRegistry;
11+
import java.io.IOException;
12+
13+
/**
14+
* Provides access to the assets registered as part of the App bundle.
15+
*/
16+
abstract class FlutterAssetManager {
17+
final AssetManager assetManager;
18+
19+
/**
20+
* Constructs a new instance of the {@link FlutterAssetManager}.
21+
*
22+
* @param assetManager Instance of Android's {@link AssetManager} used to access assets within the App bundle.
23+
*/
24+
public FlutterAssetManager(AssetManager assetManager) {
25+
this.assetManager = assetManager;
26+
}
27+
28+
/**
29+
* Gets the relative file path to the Flutter asset with the given name, including the file's extension, e.g., "myImage.jpg".
30+
*
31+
* The returned file path is relative to the Android app's standard asset's directory. Therefore,
32+
* the returned path is appropriate to pass to Android's AssetManager, but the path is not
33+
* appropriate to load as an absolute path.
34+
*/
35+
abstract String getAssetFilePathByName(String name);
36+
37+
/**
38+
* Returns a String array of all the assets at the given path.
39+
*
40+
* @param path A relative path within the assets, i.e., "docs/home.html". This value cannot be null.
41+
* @return String[] Array of strings, one for each asset. These file names are relative to 'path'. This value may be null.
42+
* @throws IOException Throws an IOException in case I/O operations were interrupted.
43+
*/
44+
public String[] list(@NonNull String path) throws IOException {
45+
return assetManager.list(path);
46+
}
47+
48+
/**
49+
* Provides access to assets using the {@link PluginRegistry.Registrar} for looking up file paths to Flutter assets.
50+
*
51+
* @deprecated The {@link RegistrarFlutterAssetManager} is for Flutter's v1 embedding. For
52+
* instructions on migrating a plugin from Flutter's v1 Android embedding to v2, visit
53+
* http://flutter.dev/go/android-plugin-migration
54+
*/
55+
@Deprecated
56+
static class RegistrarFlutterAssetManager extends FlutterAssetManager {
57+
final PluginRegistry.Registrar registrar;
58+
59+
/**
60+
* Constructs a new instance of the {@link RegistrarFlutterAssetManager}.
61+
*
62+
* @param assetManager Instance of Android's {@link AssetManager} used to access assets within the App bundle.
63+
* @param registrar Instance of {@link io.flutter.plugin.common.PluginRegistry.Registrar} used to look up file paths to assets registered by Flutter.
64+
*/
65+
RegistrarFlutterAssetManager(AssetManager assetManager, PluginRegistry.Registrar registrar) {
66+
super(assetManager);
67+
this.registrar = registrar;
68+
}
69+
70+
@Override
71+
public String getAssetFilePathByName(String name) {
72+
return registrar.lookupKeyForAsset(name);
73+
}
74+
}
75+
76+
/**
77+
* Provides access to assets using the {@link FlutterPlugin.FlutterAssets} for looking up file paths to Flutter assets.
78+
*/
79+
static class PluginBindingFlutterAssetManager extends FlutterAssetManager {
80+
final FlutterPlugin.FlutterAssets flutterAssets;
81+
82+
/**
83+
* Constructs a new instance of the {@link PluginBindingFlutterAssetManager}.
84+
*
85+
* @param assetManager Instance of Android's {@link AssetManager} used to access assets within the App bundle.
86+
* @param flutterAssets Instance of {@link io.flutter.embedding.engine.plugins.FlutterPlugin.FlutterAssets} used to look up file paths to assets registered by Flutter.
87+
*/
88+
PluginBindingFlutterAssetManager(AssetManager assetManager, FlutterPlugin.FlutterAssets flutterAssets) {
89+
super(assetManager);
90+
this.flutterAssets = flutterAssets;
91+
}
92+
93+
@Override
94+
public String getAssetFilePathByName(String name) {
95+
return flutterAssets.getAssetFilePathByName(name);
96+
}
97+
}
98+
}
99+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
package io.flutter.plugins.webviewflutter;
6+
7+
import android.content.Context;
8+
import android.webkit.WebView;
9+
import io.flutter.plugins.webviewflutter.GeneratedAndroidWebView.FlutterAssetManagerHostApi;
10+
import java.io.IOException;
11+
import java.util.Arrays;
12+
import java.util.List;
13+
14+
/**
15+
* Host api implementation for {@link WebView}.
16+
*
17+
* <p>Handles creating {@link WebView}s that intercommunicate with a paired Dart object.
18+
*/
19+
public class FlutterAssetManagerHostApiImpl implements FlutterAssetManagerHostApi {
20+
final FlutterAssetManager flutterAssetManager;
21+
22+
/**
23+
* Constructs a new instance of {@link FlutterAssetManagerHostApiImpl}.
24+
*/
25+
public FlutterAssetManagerHostApiImpl(FlutterAssetManager flutterAssetManager) {
26+
this.flutterAssetManager = flutterAssetManager;
27+
}
28+
29+
@Override
30+
public List<String> list(String path) {
31+
try {
32+
String[] paths = flutterAssetManager.list(path);
33+
return Arrays.asList(paths);
34+
} catch (IOException ex) {
35+
throw new RuntimeException(ex.getMessage());
36+
}
37+
}
38+
39+
@Override
40+
public String getAssetFilePathByName(String name) {
41+
return flutterAssetManager.getAssetFilePathByName(name);
42+
}
43+
}

0 commit comments

Comments
 (0)