Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[firebase_remote_config] Support v2 android embedder. #282

Merged
merged 28 commits into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/firebase_remote_config/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.2.1

* Support Android V2 embedding.
* Migrate to using the new e2e test binding.

## 0.2.0+9

* Updated README instructions for contributing for consistency with other Flutterfire plugins.
Expand Down
25 changes: 25 additions & 0 deletions packages/firebase_remote_config/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,28 @@ android {
}

apply from: file("./user-agent.gradle")

afterEvaluate {
def containsEmbeddingDependencies = false
for (def configuration : configurations.all) {
for (def dependency : configuration.dependencies) {
if (dependency.group == 'io.flutter' &&
dependency.name.startsWith('flutter_embedding') &&
dependency.isTransitive())
{
containsEmbeddingDependencies = true
break
}
}
}
if (!containsEmbeddingDependencies) {
android {
dependencies {
def lifecycle_version = "1.1.1"
compileOnly "android.arch.lifecycle:runtime:$lifecycle_version"
compileOnly "android.arch.lifecycle:common:$lifecycle_version"
compileOnly "android.arch.lifecycle:common-java8:$lifecycle_version"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,192 +5,45 @@
package io.flutter.plugins.firebase.firebaseremoteconfig;

import android.content.Context;
import android.content.SharedPreferences;
import androidx.annotation.NonNull;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.remoteconfig.FirebaseRemoteConfig;
import com.google.firebase.remoteconfig.FirebaseRemoteConfigFetchThrottledException;
import com.google.firebase.remoteconfig.FirebaseRemoteConfigInfo;
import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings;
import com.google.firebase.remoteconfig.FirebaseRemoteConfigValue;
import io.flutter.plugin.common.MethodCall;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/** FirebaseRemoteConfigPlugin */
public class FirebaseRemoteConfigPlugin implements MethodCallHandler {
public class FirebaseRemoteConfigPlugin implements FlutterPlugin {

public static final String TAG = "FirebaseRemoteConfigPlugin";
public static final String PREFS_NAME =
static final String TAG = "FirebaseRemoteConfigPlugin";
static final String PREFS_NAME =
"io.flutter.plugins.firebase.firebaseremoteconfig.FirebaseRemoteConfigPlugin";
public static final String DEFAULT_PREF_KEY = "default_keys";
static final String METHOD_CHANNEL = "plugins.flutter.io/firebase_remote_config";

private static SharedPreferences sharedPreferences;
private MethodChannel channel;

public static void registerWith(Registrar registrar) {
final MethodChannel channel =
new MethodChannel(registrar.messenger(), "plugins.flutter.io/firebase_remote_config");
channel.setMethodCallHandler(new FirebaseRemoteConfigPlugin());
sharedPreferences = registrar.context().getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
FirebaseRemoteConfigPlugin plugin = new FirebaseRemoteConfigPlugin();
plugin.setupChannel(registrar.messenger(), registrar.context());
}

@Override
public void onMethodCall(MethodCall call, final Result result) {
switch (call.method) {
case "RemoteConfig#instance":
{
FirebaseRemoteConfigInfo firebaseRemoteConfigInfo =
FirebaseRemoteConfig.getInstance().getInfo();

Map<String, Object> properties = new HashMap<>();
properties.put("lastFetchTime", firebaseRemoteConfigInfo.getFetchTimeMillis());
properties.put(
"lastFetchStatus", mapLastFetchStatus(firebaseRemoteConfigInfo.getLastFetchStatus()));
properties.put(
"inDebugMode", firebaseRemoteConfigInfo.getConfigSettings().isDeveloperModeEnabled());
properties.put("parameters", getConfigParameters());
result.success(properties);
break;
}
case "RemoteConfig#setConfigSettings":
{
boolean debugMode = call.argument("debugMode");
final FirebaseRemoteConfig firebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
FirebaseRemoteConfigSettings settings =
new FirebaseRemoteConfigSettings.Builder().setDeveloperModeEnabled(debugMode).build();
firebaseRemoteConfig.setConfigSettings(settings);
result.success(null);
break;
}
case "RemoteConfig#fetch":
{
long expiration = ((Number) call.argument("expiration")).longValue();
final FirebaseRemoteConfig firebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
firebaseRemoteConfig
.fetch(expiration)
.addOnCompleteListener(
new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
FirebaseRemoteConfigInfo firebaseRemoteConfigInfo =
firebaseRemoteConfig.getInfo();
Map<String, Object> properties = new HashMap<>();
properties.put(
"lastFetchTime", firebaseRemoteConfigInfo.getFetchTimeMillis());
properties.put(
"lastFetchStatus",
mapLastFetchStatus(firebaseRemoteConfigInfo.getLastFetchStatus()));
if (!task.isSuccessful()) {
final Exception exception = task.getException();

if (exception instanceof FirebaseRemoteConfigFetchThrottledException) {
properties.put(
"fetchThrottledEnd",
((FirebaseRemoteConfigFetchThrottledException) exception)
.getThrottleEndTimeMillis());
String errorMessage =
"Fetch has been throttled. See the error's "
+ "FETCH_THROTTLED_END field for throttle end time.";
result.error("fetchFailedThrottled", errorMessage, properties);
} else {
String errorMessage =
"Unable to complete fetch. Reason is unknown "
+ "but this could be due to lack of connectivity.";
result.error("fetchFailed", errorMessage, properties);
}
} else {
result.success(properties);
}
}
});
break;
}
case "RemoteConfig#activate":
{
boolean newConfig = FirebaseRemoteConfig.getInstance().activateFetched();
Map<String, Object> properties = new HashMap<>();
properties.put("parameters", getConfigParameters());
properties.put("newConfig", newConfig);
result.success(properties);
break;
}
case "RemoteConfig#setDefaults":
{
Map<String, Object> defaults = call.argument("defaults");
FirebaseRemoteConfig.getInstance().setDefaults(defaults);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putStringSet(DEFAULT_PREF_KEY, defaults.keySet()).apply();
result.success(null);
break;
}
default:
{
result.notImplemented();
break;
}
}
public void onAttachedToEngine(FlutterPluginBinding binding) {
setupChannel(binding.getFlutterEngine().getDartExecutor(), binding.getApplicationContext());
}

private Map<String, Object> getConfigParameters() {
FirebaseRemoteConfig firebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
Map<String, Object> parameterMap = new HashMap<>();
Set<String> keys = firebaseRemoteConfig.getKeysByPrefix("");
for (String key : keys) {
FirebaseRemoteConfigValue remoteConfigValue = firebaseRemoteConfig.getValue(key);
parameterMap.put(key, createRemoteConfigValueMap(remoteConfigValue));
}
// Add default parameters if missing since `getKeysByPrefix` does not return default keys.
Set<String> defaultKeys =
sharedPreferences.getStringSet(DEFAULT_PREF_KEY, new HashSet<String>());
for (String defaultKey : defaultKeys) {
if (!parameterMap.containsKey(defaultKey)) {
FirebaseRemoteConfigValue remoteConfigValue = firebaseRemoteConfig.getValue(defaultKey);
parameterMap.put(defaultKey, createRemoteConfigValueMap(remoteConfigValue));
}
}
return parameterMap;
}

private Map<String, Object> createRemoteConfigValueMap(
FirebaseRemoteConfigValue remoteConfigValue) {
Map<String, Object> valueMap = new HashMap<>();
valueMap.put("value", remoteConfigValue.asByteArray());
valueMap.put("source", mapValueSource(remoteConfigValue.getSource()));
return valueMap;
@Override
public void onDetachedFromEngine(FlutterPluginBinding binding) {
tearDownChannel();
}

private String mapLastFetchStatus(int status) {
switch (status) {
case FirebaseRemoteConfig.LAST_FETCH_STATUS_SUCCESS:
return "success";
case FirebaseRemoteConfig.LAST_FETCH_STATUS_FAILURE:
return "failure";
case FirebaseRemoteConfig.LAST_FETCH_STATUS_THROTTLED:
return "throttled";
case FirebaseRemoteConfig.LAST_FETCH_STATUS_NO_FETCH_YET:
return "noFetchYet";
default:
return "failure";
}
private void setupChannel(BinaryMessenger messenger, Context context) {
MethodCallHandlerImpl handler =
new MethodCallHandlerImpl(context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE));
channel = new MethodChannel(messenger, METHOD_CHANNEL);
channel.setMethodCallHandler(handler);
}

private String mapValueSource(int source) {
switch (source) {
case FirebaseRemoteConfig.VALUE_SOURCE_STATIC:
return "static";
case FirebaseRemoteConfig.VALUE_SOURCE_DEFAULT:
return "default";
case FirebaseRemoteConfig.VALUE_SOURCE_REMOTE:
return "remote";
default:
return "static";
}
private void tearDownChannel() {
channel.setMethodCallHandler(null);
channel = null;
}
}
Loading