Skip to content

Commit 6acb2eb

Browse files
author
Chris Yang
authored
[firebase_remote_config] Support v2 android embedder. (#282)
1 parent 2513474 commit 6acb2eb

File tree

16 files changed

+338
-192
lines changed

16 files changed

+338
-192
lines changed

packages/firebase_remote_config/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.2.1
2+
3+
* Support Android V2 embedding.
4+
* Migrate to using the new e2e test binding.
5+
16
## 0.2.0+9
27

38
* Updated README instructions for contributing for consistency with other Flutterfire plugins.

packages/firebase_remote_config/android/build.gradle

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,28 @@ android {
3939
}
4040

4141
apply from: file("./user-agent.gradle")
42+
43+
afterEvaluate {
44+
def containsEmbeddingDependencies = false
45+
for (def configuration : configurations.all) {
46+
for (def dependency : configuration.dependencies) {
47+
if (dependency.group == 'io.flutter' &&
48+
dependency.name.startsWith('flutter_embedding') &&
49+
dependency.isTransitive())
50+
{
51+
containsEmbeddingDependencies = true
52+
break
53+
}
54+
}
55+
}
56+
if (!containsEmbeddingDependencies) {
57+
android {
58+
dependencies {
59+
def lifecycle_version = "1.1.1"
60+
compileOnly "android.arch.lifecycle:runtime:$lifecycle_version"
61+
compileOnly "android.arch.lifecycle:common:$lifecycle_version"
62+
compileOnly "android.arch.lifecycle:common-java8:$lifecycle_version"
63+
}
64+
}
65+
}
66+
}

packages/firebase_remote_config/android/src/main/java/io/flutter/plugins/firebase/firebaseremoteconfig/FirebaseRemoteConfigPlugin.java

Lines changed: 22 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -5,192 +5,45 @@
55
package io.flutter.plugins.firebase.firebaseremoteconfig;
66

77
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;
1810
import io.flutter.plugin.common.MethodChannel;
19-
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
20-
import io.flutter.plugin.common.MethodChannel.Result;
2111
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;
2612

2713
/** FirebaseRemoteConfigPlugin */
28-
public class FirebaseRemoteConfigPlugin implements MethodCallHandler {
14+
public class FirebaseRemoteConfigPlugin implements FlutterPlugin {
2915

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 =
3218
"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";
3420

35-
private static SharedPreferences sharedPreferences;
21+
private MethodChannel channel;
3622

3723
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());
4226
}
4327

4428
@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());
13931
}
14032

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();
16736
}
16837

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);
18243
}
18344

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;
19548
}
19649
}

0 commit comments

Comments
 (0)