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