|
24 | 24 | import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
|
25 | 25 | import io.flutter.plugin.common.MethodChannel.Result;
|
26 | 26 |
|
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 | + |
35 | 29 | import static com.optimizely.optimizely_flutter_sdk.helper_classes.Constants.*;
|
36 |
| -import static com.optimizely.optimizely_flutter_sdk.helper_classes.Utils.convertKeysCamelCaseToSnakeCase; |
37 | 30 |
|
38 |
| -import java.util.Collections; |
39 |
| -import java.util.HashMap; |
40 |
| -import java.util.List; |
41 | 31 | import java.util.Map;
|
42 | 32 |
|
43 | 33 | import io.flutter.embedding.engine.plugins.activity.ActivityAware;
|
44 | 34 |
|
45 | 35 | /** OptimizelyFlutterSdkPlugin */
|
46 | 36 | public class OptimizelyFlutterSdkPlugin extends OptimizelyFlutterClient implements FlutterPlugin, ActivityAware, MethodCallHandler {
|
47 | 37 |
|
48 |
| - private static MethodChannel channel; |
| 38 | + public static MethodChannel channel; |
49 | 39 |
|
50 | 40 | @Override
|
51 | 41 | public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
|
52 | 42 | 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); |
58 | 44 | switch (call.method) {
|
59 | 45 | case APIs.INITIALIZE: {
|
60 |
| - initializeOptimizely(sdkKey, result); |
| 46 | + initializeOptimizely(argumentsParser, result); |
61 | 47 | break;
|
62 | 48 | }
|
63 | 49 | 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); |
68 | 51 | break;
|
69 | 52 | }
|
70 | 53 | 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); |
75 | 55 | break;
|
76 | 56 | }
|
77 | 57 | case APIs.GET_OPTIMIZELY_CONFIG: {
|
78 |
| - getOptimizelyConfig(sdkKey, result); |
| 58 | + getOptimizelyConfig(argumentsParser, result); |
79 | 59 | break;
|
80 | 60 | }
|
81 | 61 | 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); |
86 | 63 | break;
|
87 | 64 | }
|
88 | 65 | 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); |
92 | 67 | break;
|
93 | 68 | }
|
94 | 69 | 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); |
99 | 71 | break;
|
100 | 72 | }
|
101 | 73 | 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); |
106 | 75 | break;
|
107 | 76 | }
|
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); |
136 | 79 | break;
|
137 | 80 | }
|
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); |
149 | 83 | break;
|
150 | 84 | }
|
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); |
163 | 87 | break;
|
164 | 88 | }
|
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); |
173 | 91 | break;
|
174 | 92 | }
|
175 | 93 | default:
|
176 |
| - result.success(createResponse(false, ErrorMessage.INVALID_PARAMS)); |
| 94 | + result.notImplemented(); |
177 | 95 | }
|
178 | 96 | }
|
179 | 97 |
|
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 |
| - |
189 | 98 | @Override
|
190 | 99 | public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
|
191 | 100 | channel = new MethodChannel(binding.getBinaryMessenger(), "optimizely_flutter_sdk");
|
|
0 commit comments