|
5 | 5 | package io.flutter.plugins.firebase.firebaseremoteconfig; |
6 | 6 |
|
7 | 7 | import android.content.Context; |
8 | | -import android.content.SharedPreferences; |
9 | | -import androidx.annotation.NonNull; |
10 | | -import com.google.android.gms.tasks.OnCompleteListener; |
11 | | -import com.google.android.gms.tasks.Task; |
12 | | -import com.google.firebase.remoteconfig.FirebaseRemoteConfig; |
13 | | -import com.google.firebase.remoteconfig.FirebaseRemoteConfigFetchThrottledException; |
14 | | -import com.google.firebase.remoteconfig.FirebaseRemoteConfigInfo; |
15 | | -import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings; |
16 | | -import com.google.firebase.remoteconfig.FirebaseRemoteConfigValue; |
17 | | -import io.flutter.plugin.common.MethodCall; |
| 8 | +import io.flutter.embedding.engine.plugins.FlutterPlugin; |
| 9 | +import io.flutter.plugin.common.BinaryMessenger; |
18 | 10 | import io.flutter.plugin.common.MethodChannel; |
19 | | -import io.flutter.plugin.common.MethodChannel.MethodCallHandler; |
20 | | -import io.flutter.plugin.common.MethodChannel.Result; |
21 | 11 | import io.flutter.plugin.common.PluginRegistry.Registrar; |
22 | | -import java.util.HashMap; |
23 | | -import java.util.HashSet; |
24 | | -import java.util.Map; |
25 | | -import java.util.Set; |
26 | 12 |
|
27 | 13 | /** FirebaseRemoteConfigPlugin */ |
28 | | -public class FirebaseRemoteConfigPlugin implements MethodCallHandler { |
| 14 | +public class FirebaseRemoteConfigPlugin implements FlutterPlugin { |
29 | 15 |
|
30 | | - public static final String TAG = "FirebaseRemoteConfigPlugin"; |
31 | | - public static final String PREFS_NAME = |
| 16 | + static final String TAG = "FirebaseRemoteConfigPlugin"; |
| 17 | + static final String PREFS_NAME = |
32 | 18 | "io.flutter.plugins.firebase.firebaseremoteconfig.FirebaseRemoteConfigPlugin"; |
33 | | - public static final String DEFAULT_PREF_KEY = "default_keys"; |
| 19 | + static final String METHOD_CHANNEL = "plugins.flutter.io/firebase_remote_config"; |
34 | 20 |
|
35 | | - private static SharedPreferences sharedPreferences; |
| 21 | + private MethodChannel channel; |
36 | 22 |
|
37 | 23 | public static void registerWith(Registrar registrar) { |
38 | | - final MethodChannel channel = |
39 | | - new MethodChannel(registrar.messenger(), "plugins.flutter.io/firebase_remote_config"); |
40 | | - channel.setMethodCallHandler(new FirebaseRemoteConfigPlugin()); |
41 | | - sharedPreferences = registrar.context().getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); |
| 24 | + FirebaseRemoteConfigPlugin plugin = new FirebaseRemoteConfigPlugin(); |
| 25 | + plugin.setupChannel(registrar.messenger(), registrar.context()); |
42 | 26 | } |
43 | 27 |
|
44 | 28 | @Override |
45 | | - public void onMethodCall(MethodCall call, final Result result) { |
46 | | - switch (call.method) { |
47 | | - case "RemoteConfig#instance": |
48 | | - { |
49 | | - FirebaseRemoteConfigInfo firebaseRemoteConfigInfo = |
50 | | - FirebaseRemoteConfig.getInstance().getInfo(); |
51 | | - |
52 | | - Map<String, Object> properties = new HashMap<>(); |
53 | | - properties.put("lastFetchTime", firebaseRemoteConfigInfo.getFetchTimeMillis()); |
54 | | - properties.put( |
55 | | - "lastFetchStatus", mapLastFetchStatus(firebaseRemoteConfigInfo.getLastFetchStatus())); |
56 | | - properties.put( |
57 | | - "inDebugMode", firebaseRemoteConfigInfo.getConfigSettings().isDeveloperModeEnabled()); |
58 | | - properties.put("parameters", getConfigParameters()); |
59 | | - result.success(properties); |
60 | | - break; |
61 | | - } |
62 | | - case "RemoteConfig#setConfigSettings": |
63 | | - { |
64 | | - boolean debugMode = call.argument("debugMode"); |
65 | | - final FirebaseRemoteConfig firebaseRemoteConfig = FirebaseRemoteConfig.getInstance(); |
66 | | - FirebaseRemoteConfigSettings settings = |
67 | | - new FirebaseRemoteConfigSettings.Builder().setDeveloperModeEnabled(debugMode).build(); |
68 | | - firebaseRemoteConfig.setConfigSettings(settings); |
69 | | - result.success(null); |
70 | | - break; |
71 | | - } |
72 | | - case "RemoteConfig#fetch": |
73 | | - { |
74 | | - long expiration = ((Number) call.argument("expiration")).longValue(); |
75 | | - final FirebaseRemoteConfig firebaseRemoteConfig = FirebaseRemoteConfig.getInstance(); |
76 | | - firebaseRemoteConfig |
77 | | - .fetch(expiration) |
78 | | - .addOnCompleteListener( |
79 | | - new OnCompleteListener<Void>() { |
80 | | - @Override |
81 | | - public void onComplete(@NonNull Task<Void> task) { |
82 | | - FirebaseRemoteConfigInfo firebaseRemoteConfigInfo = |
83 | | - firebaseRemoteConfig.getInfo(); |
84 | | - Map<String, Object> properties = new HashMap<>(); |
85 | | - properties.put( |
86 | | - "lastFetchTime", firebaseRemoteConfigInfo.getFetchTimeMillis()); |
87 | | - properties.put( |
88 | | - "lastFetchStatus", |
89 | | - mapLastFetchStatus(firebaseRemoteConfigInfo.getLastFetchStatus())); |
90 | | - if (!task.isSuccessful()) { |
91 | | - final Exception exception = task.getException(); |
92 | | - |
93 | | - if (exception instanceof FirebaseRemoteConfigFetchThrottledException) { |
94 | | - properties.put( |
95 | | - "fetchThrottledEnd", |
96 | | - ((FirebaseRemoteConfigFetchThrottledException) exception) |
97 | | - .getThrottleEndTimeMillis()); |
98 | | - String errorMessage = |
99 | | - "Fetch has been throttled. See the error's " |
100 | | - + "FETCH_THROTTLED_END field for throttle end time."; |
101 | | - result.error("fetchFailedThrottled", errorMessage, properties); |
102 | | - } else { |
103 | | - String errorMessage = |
104 | | - "Unable to complete fetch. Reason is unknown " |
105 | | - + "but this could be due to lack of connectivity."; |
106 | | - result.error("fetchFailed", errorMessage, properties); |
107 | | - } |
108 | | - } else { |
109 | | - result.success(properties); |
110 | | - } |
111 | | - } |
112 | | - }); |
113 | | - break; |
114 | | - } |
115 | | - case "RemoteConfig#activate": |
116 | | - { |
117 | | - boolean newConfig = FirebaseRemoteConfig.getInstance().activateFetched(); |
118 | | - Map<String, Object> properties = new HashMap<>(); |
119 | | - properties.put("parameters", getConfigParameters()); |
120 | | - properties.put("newConfig", newConfig); |
121 | | - result.success(properties); |
122 | | - break; |
123 | | - } |
124 | | - case "RemoteConfig#setDefaults": |
125 | | - { |
126 | | - Map<String, Object> defaults = call.argument("defaults"); |
127 | | - FirebaseRemoteConfig.getInstance().setDefaults(defaults); |
128 | | - SharedPreferences.Editor editor = sharedPreferences.edit(); |
129 | | - editor.putStringSet(DEFAULT_PREF_KEY, defaults.keySet()).apply(); |
130 | | - result.success(null); |
131 | | - break; |
132 | | - } |
133 | | - default: |
134 | | - { |
135 | | - result.notImplemented(); |
136 | | - break; |
137 | | - } |
138 | | - } |
| 29 | + public void onAttachedToEngine(FlutterPluginBinding binding) { |
| 30 | + setupChannel(binding.getFlutterEngine().getDartExecutor(), binding.getApplicationContext()); |
139 | 31 | } |
140 | 32 |
|
141 | | - private Map<String, Object> getConfigParameters() { |
142 | | - FirebaseRemoteConfig firebaseRemoteConfig = FirebaseRemoteConfig.getInstance(); |
143 | | - Map<String, Object> parameterMap = new HashMap<>(); |
144 | | - Set<String> keys = firebaseRemoteConfig.getKeysByPrefix(""); |
145 | | - for (String key : keys) { |
146 | | - FirebaseRemoteConfigValue remoteConfigValue = firebaseRemoteConfig.getValue(key); |
147 | | - parameterMap.put(key, createRemoteConfigValueMap(remoteConfigValue)); |
148 | | - } |
149 | | - // Add default parameters if missing since `getKeysByPrefix` does not return default keys. |
150 | | - Set<String> defaultKeys = |
151 | | - sharedPreferences.getStringSet(DEFAULT_PREF_KEY, new HashSet<String>()); |
152 | | - for (String defaultKey : defaultKeys) { |
153 | | - if (!parameterMap.containsKey(defaultKey)) { |
154 | | - FirebaseRemoteConfigValue remoteConfigValue = firebaseRemoteConfig.getValue(defaultKey); |
155 | | - parameterMap.put(defaultKey, createRemoteConfigValueMap(remoteConfigValue)); |
156 | | - } |
157 | | - } |
158 | | - return parameterMap; |
159 | | - } |
160 | | - |
161 | | - private Map<String, Object> createRemoteConfigValueMap( |
162 | | - FirebaseRemoteConfigValue remoteConfigValue) { |
163 | | - Map<String, Object> valueMap = new HashMap<>(); |
164 | | - valueMap.put("value", remoteConfigValue.asByteArray()); |
165 | | - valueMap.put("source", mapValueSource(remoteConfigValue.getSource())); |
166 | | - return valueMap; |
| 33 | + @Override |
| 34 | + public void onDetachedFromEngine(FlutterPluginBinding binding) { |
| 35 | + tearDownChannel(); |
167 | 36 | } |
168 | 37 |
|
169 | | - private String mapLastFetchStatus(int status) { |
170 | | - switch (status) { |
171 | | - case FirebaseRemoteConfig.LAST_FETCH_STATUS_SUCCESS: |
172 | | - return "success"; |
173 | | - case FirebaseRemoteConfig.LAST_FETCH_STATUS_FAILURE: |
174 | | - return "failure"; |
175 | | - case FirebaseRemoteConfig.LAST_FETCH_STATUS_THROTTLED: |
176 | | - return "throttled"; |
177 | | - case FirebaseRemoteConfig.LAST_FETCH_STATUS_NO_FETCH_YET: |
178 | | - return "noFetchYet"; |
179 | | - default: |
180 | | - return "failure"; |
181 | | - } |
| 38 | + private void setupChannel(BinaryMessenger messenger, Context context) { |
| 39 | + MethodCallHandlerImpl handler = |
| 40 | + new MethodCallHandlerImpl(context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)); |
| 41 | + channel = new MethodChannel(messenger, METHOD_CHANNEL); |
| 42 | + channel.setMethodCallHandler(handler); |
182 | 43 | } |
183 | 44 |
|
184 | | - private String mapValueSource(int source) { |
185 | | - switch (source) { |
186 | | - case FirebaseRemoteConfig.VALUE_SOURCE_STATIC: |
187 | | - return "static"; |
188 | | - case FirebaseRemoteConfig.VALUE_SOURCE_DEFAULT: |
189 | | - return "default"; |
190 | | - case FirebaseRemoteConfig.VALUE_SOURCE_REMOTE: |
191 | | - return "remote"; |
192 | | - default: |
193 | | - return "static"; |
194 | | - } |
| 45 | + private void tearDownChannel() { |
| 46 | + channel.setMethodCallHandler(null); |
| 47 | + channel = null; |
195 | 48 | } |
196 | 49 | } |
0 commit comments