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