Flutter Engine
The Flutter Engine
FlutterEngineCache.java
Go to the documentation of this file.
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
5package io.flutter.embedding.engine;
6
7import androidx.annotation.NonNull;
8import androidx.annotation.Nullable;
9import androidx.annotation.VisibleForTesting;
10import java.util.HashMap;
11import java.util.Map;
12
13/**
14 * Static singleton cache that holds {@link io.flutter.embedding.engine.FlutterEngine} instances
15 * identified by {@code String}s.
16 *
17 * <p>The ID of a given {@link io.flutter.embedding.engine.FlutterEngine} can be whatever {@code
18 * String} is desired.
19 *
20 * <p>{@code FlutterEngineCache} is useful for storing pre-warmed {@link
21 * io.flutter.embedding.engine.FlutterEngine} instances. {@link
22 * io.flutter.embedding.android.FlutterActivity} and {@link
23 * io.flutter.embedding.android.FlutterFragment} use the {@code FlutterEngineCache} singleton
24 * internally when instructed to use a cached {@link io.flutter.embedding.engine.FlutterEngine}
25 * based on a given ID. See {@link
26 * io.flutter.embedding.android.FlutterActivity.CachedEngineIntentBuilder} and {@link
27 * io.flutter.embedding.android.FlutterFragment#withCachedEngine(String)} for related APIs.
28 */
29public class FlutterEngineCache {
30 private static FlutterEngineCache instance;
31
32 /**
33 * Returns the static singleton instance of {@code FlutterEngineCache}.
34 *
35 * <p>Creates a new instance if one does not yet exist.
36 */
37 @NonNull
39 if (instance == null) {
41 }
42 return instance;
43 }
44
45 private final Map<String, FlutterEngine> cachedEngines = new HashMap<>();
46
47 @VisibleForTesting
48 /* package */ FlutterEngineCache() {}
49
50 /**
51 * Returns {@code true} if a {@link io.flutter.embedding.engine.FlutterEngine} in this cache is
52 * associated with the given {@code engineId}.
53 */
54 public boolean contains(@NonNull String engineId) {
55 return cachedEngines.containsKey(engineId);
56 }
57
58 /**
59 * Returns the {@link io.flutter.embedding.engine.FlutterEngine} in this cache that is associated
60 * with the given {@code engineId}, or {@code null} is no such {@link
61 * io.flutter.embedding.engine.FlutterEngine} exists.
62 */
63 @Nullable
64 public FlutterEngine get(@NonNull String engineId) {
65 return cachedEngines.get(engineId);
66 }
67
68 /**
69 * Places the given {@link io.flutter.embedding.engine.FlutterEngine} in this cache and associates
70 * it with the given {@code engineId}.
71 *
72 * <p>If a {@link io.flutter.embedding.engine.FlutterEngine} already exists in this cache for the
73 * given {@code engineId}, that {@link io.flutter.embedding.engine.FlutterEngine} is removed from
74 * this cache.
75 */
76 public void put(@NonNull String engineId, @Nullable FlutterEngine engine) {
77 if (engine != null) {
78 cachedEngines.put(engineId, engine);
79 } else {
80 cachedEngines.remove(engineId);
81 }
82 }
83
84 /**
85 * Removes any {@link io.flutter.embedding.engine.FlutterEngine} that is currently in the cache
86 * that is identified by the given {@code engineId}.
87 */
88 public void remove(@NonNull String engineId) {
89 put(engineId, null);
90 }
91
92 /**
93 * Removes all {@link io.flutter.embedding.engine.FlutterEngine}'s that are currently in the
94 * cache.
95 */
96 public void clear() {
97 cachedEngines.clear();
98 }
99}
void put(@NonNull String engineId, @Nullable FlutterEngine engine)
boolean contains(@NonNull String engineId)
FlutterEngine engine
Definition: main.cc:68
VkInstance instance
Definition: main.cc:48