Skip to content

Commit db91b19

Browse files
authored
Merge branch 'master' into mnoman/readme
2 parents 7740e69 + cae9cdd commit db91b19

16 files changed

+866
-280
lines changed

android/src/main/java/com/optimizely/optimizely_flutter_sdk/OptimizelyDecisionResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class OptimizelyDecisionResponse {
2929

3030
private final Map variables;
3131

32-
private final String ruleKey;
32+
private final String ruleKey;
3333

3434
private final String flagKey;
3535

android/src/main/java/com/optimizely/optimizely_flutter_sdk/OptimizelyFlutterClient.java

Lines changed: 259 additions & 11 deletions
Large diffs are not rendered by default.

android/src/main/java/com/optimizely/optimizely_flutter_sdk/OptimizelyFlutterSdkPlugin.java

Lines changed: 21 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -24,168 +24,77 @@
2424
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
2525
import io.flutter.plugin.common.MethodChannel.Result;
2626

27-
import com.fasterxml.jackson.databind.ObjectMapper;
28-
29-
import com.optimizely.ab.android.sdk.OptimizelyClient;
30-
import com.optimizely.ab.event.LogEvent;
31-
import com.optimizely.ab.notification.DecisionNotification;
32-
import com.optimizely.ab.notification.TrackNotification;
33-
import com.optimizely.ab.notification.UpdateConfigNotification;
34-
import com.optimizely.ab.optimizelydecision.OptimizelyDecideOption;
27+
import com.optimizely.optimizely_flutter_sdk.helper_classes.ArgumentsParser;
28+
3529
import static com.optimizely.optimizely_flutter_sdk.helper_classes.Constants.*;
36-
import static com.optimizely.optimizely_flutter_sdk.helper_classes.Utils.convertKeysCamelCaseToSnakeCase;
3730

38-
import java.util.Collections;
39-
import java.util.HashMap;
40-
import java.util.List;
4131
import java.util.Map;
4232

4333
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
4434

4535
/** OptimizelyFlutterSdkPlugin */
4636
public class OptimizelyFlutterSdkPlugin extends OptimizelyFlutterClient implements FlutterPlugin, ActivityAware, MethodCallHandler {
4737

48-
private static MethodChannel channel;
38+
public static MethodChannel channel;
4939

5040
@Override
5141
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
5242
Map<String, ?> arguments = call.arguments();
53-
String sdkKey = (String) arguments.get(RequestParameterKey.SDK_KEY);
54-
if (sdkKey == null) {
55-
result.success(createResponse(false, ErrorMessage.INVALID_PARAMS));
56-
return;
57-
}
43+
ArgumentsParser argumentsParser = new ArgumentsParser(arguments);
5844
switch (call.method) {
5945
case APIs.INITIALIZE: {
60-
initializeOptimizely(sdkKey, result);
46+
initializeOptimizely(argumentsParser, result);
6147
break;
6248
}
6349
case APIs.ADD_NOTIFICATION_LISTENER: {
64-
Integer id = (Integer) arguments.get(RequestParameterKey.NOTIFICATION_ID);
65-
String type = (String) arguments.get(RequestParameterKey.NOTIFICATION_TYPE);
66-
67-
addNotificationListener(sdkKey, id, type, result);
50+
addNotificationListener(argumentsParser, result);
6851
break;
6952
}
7053
case APIs.REMOVE_NOTIFICATION_LISTENER: {
71-
Integer id = (Integer) arguments.get(RequestParameterKey.NOTIFICATION_ID);
72-
String type = (String) arguments.get(RequestParameterKey.NOTIFICATION_TYPE);
73-
74-
removeNotificationListener(sdkKey, id, type, result);
54+
removeNotificationListener(argumentsParser, result);
7555
break;
7656
}
7757
case APIs.GET_OPTIMIZELY_CONFIG: {
78-
getOptimizelyConfig(sdkKey, result);
58+
getOptimizelyConfig(argumentsParser, result);
7959
break;
8060
}
8161
case APIs.CREATE_USER_CONTEXT: {
82-
String userId = (String) arguments.get(RequestParameterKey.USER_ID);
83-
Map<String, Object> attributes = (Map<String, Object>) arguments.get(RequestParameterKey.ATTRIBUTES);
84-
85-
createUserContext(sdkKey, userId, attributes, result);
62+
createUserContext(argumentsParser, result);
8663
break;
8764
}
8865
case APIs.SET_ATTRIBUTES: {
89-
Map<String, Object> attributes = (Map<String, Object>) arguments.get(RequestParameterKey.ATTRIBUTES);
90-
91-
setAttribute(sdkKey, attributes, result);
66+
setAttribute(argumentsParser, result);
9267
break;
9368
}
9469
case APIs.TRACK_EVENT: {
95-
String eventKey = (String) arguments.get(RequestParameterKey.EVENT_KEY);
96-
Map<String, Object> eventTags = (Map<String, Object>) arguments.get(RequestParameterKey.EVENT_TAGS);
97-
98-
trackEvent(sdkKey, eventKey, eventTags, result);
70+
trackEvent(argumentsParser, result);
9971
break;
10072
}
10173
case APIs.DECIDE: {
102-
List<String> decideKeys = (List<String>) arguments.get(RequestParameterKey.DECIDE_KEYS);
103-
List<OptimizelyDecideOption> decideOptions = (List<OptimizelyDecideOption>) arguments.get(RequestParameterKey.DECIDE_OPTIONS);
104-
105-
decide(sdkKey, decideKeys, decideOptions, result);
74+
decide(argumentsParser, result);
10675
break;
10776
}
108-
default:
109-
result.notImplemented();
110-
}
111-
}
112-
113-
protected void addNotificationListener(String sdkKey, Integer id, String type, @NonNull Result result) {
114-
OptimizelyClient optimizelyClient = getOptimizelyClient(sdkKey);
115-
if (optimizelyClient == null) {
116-
result.success(createResponse(false, ErrorMessage.OPTIMIZELY_CLIENT_NOT_FOUND));
117-
return;
118-
}
119-
120-
if (id == null || type == null) {
121-
result.success(createResponse(false, ErrorMessage.INVALID_PARAMS));
122-
return;
123-
}
124-
switch (type) {
125-
case NotificationType.DECISION: {
126-
int notificationId = optimizelyClient.getNotificationCenter().addNotificationHandler(DecisionNotification.class, decisionNotification -> {
127-
Map<String, Object> notificationMap = new HashMap<>();
128-
notificationMap.put(DecisionListenerKeys.TYPE, decisionNotification.getType());
129-
notificationMap.put(DecisionListenerKeys.USER_ID, decisionNotification.getUserId());
130-
notificationMap.put(DecisionListenerKeys.ATTRIBUTES, decisionNotification.getAttributes());
131-
notificationMap.put(DecisionListenerKeys.DECISION_INFO, convertKeysCamelCaseToSnakeCase(decisionNotification.getDecisionInfo()));
132-
invokeNotification(id, NotificationType.DECISION, notificationMap);
133-
});
134-
notificationIdsTracker.put(id, notificationId);
135-
result.success(createResponse(true, SuccessMessage.LISTENER_ADDED));
77+
case APIs.SET_FORCED_DECISION: {
78+
setForcedDecision(argumentsParser, result);
13679
break;
13780
}
138-
case NotificationType.TRACK: {
139-
Map<String, Object> notificationMap = new HashMap<>();
140-
int notificationId = optimizelyClient.getNotificationCenter().addNotificationHandler(TrackNotification.class, trackNotification -> {
141-
notificationMap.put(TrackListenerKeys.EVENT_KEY, trackNotification.getEventKey());
142-
notificationMap.put(TrackListenerKeys.USER_ID, trackNotification.getUserId());
143-
notificationMap.put(TrackListenerKeys.ATTRIBUTES, trackNotification.getAttributes());
144-
notificationMap.put(TrackListenerKeys.EVENT_TAGS, trackNotification.getEventTags());
145-
invokeNotification(id, NotificationType.TRACK, notificationMap);
146-
});
147-
notificationIdsTracker.put(id, notificationId);
148-
result.success(createResponse(true, SuccessMessage.LISTENER_ADDED));
81+
case APIs.GET_FORCED_DECISION: {
82+
getForcedDecision(argumentsParser, result);
14983
break;
15084
}
151-
case NotificationType.LOG_EVENT: {
152-
int notificationId = optimizelyClient.getNotificationCenter().addNotificationHandler(LogEvent.class, logEvent -> {
153-
ObjectMapper mapper = new ObjectMapper();
154-
Map<String, Object> eventParams = mapper.readValue(logEvent.getBody(), Map.class);
155-
Map<String, Object> listenerMap = new HashMap<>();
156-
listenerMap.put(LogEventListenerKeys.URL, logEvent.getEndpointUrl());
157-
listenerMap.put(LogEventListenerKeys.HTTP_VERB, logEvent.getRequestMethod());
158-
listenerMap.put(LogEventListenerKeys.PARAMS, eventParams);
159-
invokeNotification(id, NotificationType.LOG_EVENT, listenerMap);
160-
});
161-
notificationIdsTracker.put(id, notificationId);
162-
result.success(createResponse(true, SuccessMessage.LISTENER_ADDED));
85+
case APIs.REMOVE_FORCED_DECISION: {
86+
removeForcedDecision(argumentsParser, result);
16387
break;
16488
}
165-
case NotificationType.CONFIG_UPDATE: {
166-
int notificationId = optimizelyClient.getNotificationCenter().addNotificationHandler(UpdateConfigNotification.class, configUpdate -> {
167-
Map<String, Object> listenerMap = new HashMap<>();
168-
listenerMap.put("Config-update", Collections.emptyMap());
169-
invokeNotification(id, NotificationType.CONFIG_UPDATE, listenerMap);
170-
});
171-
notificationIdsTracker.put(id, notificationId);
172-
result.success(createResponse(true, SuccessMessage.LISTENER_ADDED));
89+
case APIs.REMOVE_ALL_FORCED_DECISIONS: {
90+
removeAllForcedDecisions(argumentsParser, result);
17391
break;
17492
}
17593
default:
176-
result.success(createResponse(false, ErrorMessage.INVALID_PARAMS));
94+
result.notImplemented();
17795
}
17896
}
17997

180-
private void invokeNotification(int id, String notificationType, Map notificationMap) {
181-
Map<String, Object> listenerResponse = new HashMap<>();
182-
listenerResponse.put(RequestParameterKey.NOTIFICATION_ID, id);
183-
listenerResponse.put(RequestParameterKey.NOTIFICATION_TYPE, notificationType);
184-
listenerResponse.put(RequestParameterKey.NOTIFICATION_PAYLOAD, notificationMap);
185-
Map<String, Object> listenerUnmodifiable = Collections.unmodifiableMap(listenerResponse);
186-
OptimizelyFlutterSdkPlugin.channel.invokeMethod("callbackListener", listenerUnmodifiable);
187-
}
188-
18998
@Override
19099
public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
191100
channel = new MethodChannel(binding.getBinaryMessenger(), "optimizely_flutter_sdk");
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/****************************************************************************
2+
* Copyright 2022, Optimizely, Inc. and contributors *
3+
* *
4+
* Licensed under the Apache License, Version 2.0 (the "License"); *
5+
* you may not use this file except in compliance with the License. *
6+
* You may obtain a copy of the License at *
7+
* *
8+
* https://www.apache.org/licenses/LICENSE-2.0 *
9+
* *
10+
* Unless required by applicable law or agreed to in writing, software *
11+
* distributed under the License is distributed on an "AS IS" BASIS, *
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13+
* See the License for the specific language governing permissions and *
14+
* limitations under the License. *
15+
***************************************************************************/
16+
package com.optimizely.optimizely_flutter_sdk.helper_classes;
17+
18+
import com.optimizely.ab.optimizelydecision.OptimizelyDecideOption;
19+
20+
import java.util.List;
21+
import java.util.Map;
22+
23+
public class ArgumentsParser {
24+
private final Map<String, ?> arguments;
25+
26+
public ArgumentsParser(Map<String, ?> arguments) {
27+
this.arguments = arguments;
28+
}
29+
30+
public String getSdkKey() {
31+
return (String) arguments.get(Constants.RequestParameterKey.SDK_KEY);
32+
}
33+
34+
public Integer getNotificaitonID() {
35+
return (Integer) arguments.get(Constants.RequestParameterKey.NOTIFICATION_ID);
36+
}
37+
38+
public String getNotificationType() {
39+
return (String) arguments.get(Constants.RequestParameterKey.NOTIFICATION_TYPE);
40+
}
41+
42+
public String getUserID() {
43+
return (String) arguments.get(Constants.RequestParameterKey.USER_ID);
44+
}
45+
46+
public Map<String, Object> getAttributes() {
47+
return (Map<String, Object>) arguments.get(Constants.RequestParameterKey.ATTRIBUTES);
48+
}
49+
50+
public String getEventKey() {
51+
return (String) arguments.get(Constants.RequestParameterKey.EVENT_KEY);
52+
}
53+
54+
public Map<String, Object> getEventTags() {
55+
return (Map<String, Object>) arguments.get(Constants.RequestParameterKey.EVENT_TAGS);
56+
}
57+
58+
public List<String> getDecideKeys() {
59+
return (List<String>) arguments.get(Constants.RequestParameterKey.DECIDE_KEYS);
60+
}
61+
62+
public List<OptimizelyDecideOption> getDecideOptions() {
63+
return (List<OptimizelyDecideOption>) arguments.get(Constants.RequestParameterKey.DECIDE_OPTIONS);
64+
}
65+
66+
public String getFlagKey() {
67+
return (String) arguments.get(Constants.RequestParameterKey.FLAG_KEY);
68+
}
69+
70+
public String getRuleKey() {
71+
return (String) arguments.get(Constants.RequestParameterKey.RULE_KEY);
72+
}
73+
74+
public String getVariationKey() {
75+
return (String) arguments.get(Constants.RequestParameterKey.VARIATION_KEY);
76+
}
77+
}

android/src/main/java/com/optimizely/optimizely_flutter_sdk/helper_classes/Constants.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@ public class Constants {
1919
public static class APIs {
2020
public static final String INITIALIZE = "initialize";
2121
public static final String GET_OPTIMIZELY_CONFIG = "getOptimizelyConfig";
22-
public static final String CREATE_USER_CONTEXT="createUserContext";
22+
public static final String CREATE_USER_CONTEXT = "createUserContext";
2323
public static final String SET_ATTRIBUTES="setAttributes";
24-
public static final String TRACK_EVENT="trackEvent";
25-
public static final String DECIDE="decide";
26-
public static final String ADD_NOTIFICATION_LISTENER="addNotificationListener";
27-
public static final String REMOVE_NOTIFICATION_LISTENER ="removeNotificationListener";
24+
public static final String GET_FORCED_DECISION = "getForcedDecision";
25+
public static final String REMOVE_FORCED_DECISION = "removeForcedDecision";
26+
public static final String REMOVE_ALL_FORCED_DECISIONS = "removeAllForcedDecisions";
27+
public static final String SET_FORCED_DECISION = "setForcedDecision";
28+
public static final String TRACK_EVENT = "trackEvent";
29+
public static final String DECIDE = "decide";
30+
public static final String ADD_NOTIFICATION_LISTENER = "addNotificationListener";
31+
public static final String REMOVE_NOTIFICATION_LISTENER = "removeNotificationListener";
2832
}
2933

3034
public static class NotificationType {
@@ -35,16 +39,19 @@ public static class NotificationType {
3539
}
3640

3741
public static class RequestParameterKey {
38-
public static final String SDK_KEY = "sdk_key";
39-
public static final String USER_ID = "user_id";
42+
public static final String SDK_KEY = "sdkKey";
43+
public static final String USER_ID = "userID";
4044
public static final String NOTIFICATION_ID = "id";
4145
public static final String NOTIFICATION_TYPE = "type";
4246
public static final String NOTIFICATION_PAYLOAD = "payload";
4347
public static final String ATTRIBUTES = "attributes";
4448
public static final String DECIDE_KEYS = "keys";
45-
public static final String DECIDE_OPTIONS = "optimizely_decide_option";
46-
public static final String EVENT_KEY= "event_key";
47-
public static final String EVENT_TAGS= "event_tags";
49+
public static final String DECIDE_OPTIONS = "optimizelyDecideOption";
50+
public static final String EVENT_KEY = "eventKey";
51+
public static final String EVENT_TAGS = "eventTags";
52+
public static final String FLAG_KEY = "flagKey";
53+
public static final String RULE_KEY = "ruleKey";
54+
public static final String VARIATION_KEY = "variationKey";
4855
}
4956

5057
public static class ErrorMessage {
@@ -62,6 +69,9 @@ public static class SuccessMessage {
6269
public static final String LISTENER_ADDED = "Listener added successfully.";
6370
public static final String ATTRIBUTES_ADDED = "Attributes added successfully.";
6471
public static final String EVENT_TRACKED = "Event Tracked successfully.";
72+
public static final String FORCED_DECISION_SET = "Forced decision set successfully.";
73+
public static final String REMOVED_FORCED_DECISION = "Forced decision removed successfully.";
74+
public static final String REMOVED_ALL_FORCED_DECISION = "All Forced decisions removed successfully.";
6575
}
6676

6777
public static class DecisionListenerKeys {
@@ -83,4 +93,10 @@ public static class LogEventListenerKeys {
8393
public static final String HTTP_VERB = "http_verb";
8494
public static final String PARAMS = "params";
8595
}
96+
97+
public static class ResponseKey {
98+
public static final String RESULT = "result";
99+
public static final String REASON = "reason";
100+
public static final String SUCCESS = "success";
101+
}
86102
}

0 commit comments

Comments
 (0)