|
9 | 9 | import com.sensorsdata.abtest.OnABTestReceivedData; |
10 | 10 | import com.sensorsdata.abtest.SensorsABTest; |
11 | 11 | import com.sensorsdata.abtest.SensorsABTestConfigOptions; |
| 12 | +import com.sensorsdata.abtest.SensorsABTestExperiment; |
12 | 13 |
|
13 | 14 | import org.json.JSONObject; |
14 | 15 |
|
|
27 | 28 |
|
28 | 29 | /** AbtestingSdkFlutterPlugin */ |
29 | 30 | public class AbtestingSdkFlutterPlugin implements FlutterPlugin, MethodCallHandler { |
30 | | - /// The MethodChannel that will the communication between Flutter and native Android |
| 31 | + /// The MethodChannel that will the communication between Flutter and native |
| 32 | + /// Android |
31 | 33 | /// |
32 | | - /// This local reference serves to register the plugin with the Flutter Engine and unregister it |
| 34 | + /// This local reference serves to register the plugin with the Flutter Engine |
| 35 | + /// and unregister it |
33 | 36 | /// when the Flutter Engine is detached from the Activity |
34 | 37 | private MethodChannel channel; |
35 | 38 | private Context applicationContext; |
@@ -61,15 +64,38 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { |
61 | 64 | case "fastFetchABTest": |
62 | 65 | fastFetchABTest(call, result); |
63 | 66 | break; |
| 67 | + case "setCustomIDs": |
| 68 | + setCustomIDs(call, result); |
| 69 | + break; |
| 70 | + case "setCustomProperties": |
| 71 | + setCustomProperties(call, result); |
| 72 | + break; |
| 73 | + case "fastFetchABTestWithExperiment": |
| 74 | + fastFetchABTestWithExperiment(call, result); |
| 75 | + break; |
| 76 | + case "asyncFetchABTestWithExperiment": |
| 77 | + asyncFetchABTestWithExperiment(call, result); |
| 78 | + break; |
64 | 79 | default: |
65 | 80 | result.notImplemented(); |
66 | 81 | break; |
67 | 82 | } |
68 | 83 | } |
69 | 84 |
|
70 | 85 | private void startWithConfigOptions(MethodCall call, Result result) { |
71 | | - String url = (String) call.arguments; |
72 | | - SensorsABTestConfigOptions abTestConfigOptions = new SensorsABTestConfigOptions(url); |
| 86 | + Map<String, Object> args = call.arguments(); |
| 87 | + |
| 88 | + String urlString = (String) args.get("urlString"); |
| 89 | + Object customObj = args.get("customProperties"); |
| 90 | + Map<String, Object> customProperties = null; |
| 91 | + if (customObj instanceof Map) { |
| 92 | + customProperties = (Map<String, Object>) customObj; |
| 93 | + } |
| 94 | + |
| 95 | + SensorsABTestConfigOptions abTestConfigOptions = new SensorsABTestConfigOptions(urlString); |
| 96 | + if (customProperties != null) { |
| 97 | + abTestConfigOptions.setCustomProperties(assertMapToJSON(customProperties)); |
| 98 | + } |
73 | 99 | SensorsABTest.startWithConfigOptions(applicationContext, abTestConfigOptions); |
74 | 100 | result.success(null); |
75 | 101 | } |
@@ -127,26 +153,139 @@ public void run() { |
127 | 153 | }); |
128 | 154 | } |
129 | 155 |
|
130 | | - private void checkResult(Object abResult, Result result) { |
131 | | - if (abResult instanceof JSONObject) { |
132 | | - JSONObject jsonObject = (JSONObject) abResult; |
133 | | - result.success(jsonObject.toString()); |
| 156 | + private void fastFetchABTestWithExperiment(MethodCall call, final Result result) { |
| 157 | + List list = (List) call.arguments; |
| 158 | + if (list == null || list.size() == 0) { |
| 159 | + result.success(null); |
134 | 160 | return; |
135 | 161 | } |
136 | | - result.success(abResult); |
| 162 | + |
| 163 | + Map<String, Object> experimenMap = (Map<String, Object>) list.get(0); |
| 164 | + // 构建最终的 Experiment |
| 165 | + SensorsABTestExperiment<Object> experiment = buildExperimentFromMap(experimenMap); |
| 166 | + |
| 167 | + SensorsABTest.shareInstance().fastFetchABTest(experiment, new OnABTestReceivedData() { |
| 168 | + @Override |
| 169 | + public void onResult(final Object value) { |
| 170 | + uiThreadHandler.post(new Runnable() { |
| 171 | + @Override |
| 172 | + public void run() { |
| 173 | + checkResult(value, result); |
| 174 | + } |
| 175 | + }); |
| 176 | + } |
| 177 | + }); |
137 | 178 | } |
138 | 179 |
|
139 | | - private static Map<String, Object> toMap(JSONObject obj) { |
140 | | - if (obj == null) { |
141 | | - return null; |
| 180 | + private void asyncFetchABTestWithExperiment(MethodCall call, final Result result) { |
| 181 | + List list = (List) call.arguments; |
| 182 | + if (list == null || list.size() == 0) { |
| 183 | + result.success(null); |
| 184 | + return; |
142 | 185 | } |
143 | | - Map<String, Object> data = new HashMap<>(); |
144 | | - Iterator<String> it = obj.keys(); |
145 | | - while (it.hasNext()) { |
146 | | - String key = it.next(); |
147 | | - data.put(key, obj.opt(key)); |
| 186 | + |
| 187 | + Map<String, Object> experimenMap = (Map<String, Object>) list.get(0); |
| 188 | + |
| 189 | + // 构建最终的 Experiment |
| 190 | + SensorsABTestExperiment<Object> experiment = buildExperimentFromMap(experimenMap); |
| 191 | + |
| 192 | + SensorsABTest.shareInstance().asyncFetchABTest(experiment, new OnABTestReceivedData() { |
| 193 | + @Override |
| 194 | + public void onResult(final Object value) { |
| 195 | + uiThreadHandler.post(new Runnable() { |
| 196 | + @Override |
| 197 | + public void run() { |
| 198 | + checkResult(value, result); |
| 199 | + } |
| 200 | + }); |
| 201 | + } |
| 202 | + }); |
| 203 | + } |
| 204 | + |
| 205 | + private void setCustomIDs(MethodCall call, Result result) { |
| 206 | + Map<String, String> customIDs = call.arguments(); |
| 207 | + SensorsABTest.shareInstance().setCustomIDs(customIDs); |
| 208 | + result.success(null); |
| 209 | + } |
| 210 | + |
| 211 | + private void setCustomProperties(MethodCall call, Result result) { |
| 212 | + Map<String, Object> customProperties = call.arguments(); |
| 213 | + SensorsABTest.shareInstance().setCustomProperties(assertMapToJSON(customProperties)); |
| 214 | + result.success(null); |
| 215 | + } |
| 216 | + |
| 217 | + // 从 Map 构建 Experiment 对象 |
| 218 | + private SensorsABTestExperiment<Object> buildExperimentFromMap(Map<String, Object> experimentDic) { |
| 219 | + String paramName = (String) experimentDic.get("paramName"); |
| 220 | + Object defaultValue = experimentDic.get("defaultValue"); |
| 221 | + if (defaultValue instanceof Map) { |
| 222 | + defaultValue = new JSONObject((Map) defaultValue); |
| 223 | + } |
| 224 | + |
| 225 | + SensorsABTestExperiment.ExperimentBuilder<Object> builder = SensorsABTestExperiment.newBuilder(paramName, |
| 226 | + defaultValue); |
| 227 | + Object timeoutObj = experimentDic.get("timeoutInterval"); |
| 228 | + if (timeoutObj instanceof Number) { |
| 229 | + int timeout = ((Number) timeoutObj).intValue(); // 毫秒 |
| 230 | + builder.setTimeoutMillSeconds(timeout); |
| 231 | + } |
| 232 | + |
| 233 | + Map<String, Object> properties = (Map<String, Object>) experimentDic.get("properties"); |
| 234 | + if (properties != null) { |
| 235 | + for (String key : properties.keySet()) { |
| 236 | + Object value = properties.get(key); |
| 237 | + |
| 238 | + if (value == null) { |
| 239 | + builder.addProperty(key, (String) null); |
| 240 | + } else if (value instanceof String) { |
| 241 | + builder.addProperty(key, (String) value); |
| 242 | + } else if (value instanceof Boolean) { |
| 243 | + builder.addProperty(key, (Boolean) value); |
| 244 | + } else if (value instanceof Integer) { |
| 245 | + builder.addProperty(key, (Integer) value); |
| 246 | + } else if (value instanceof Long) { |
| 247 | + builder.addProperty(key, (Long) value); |
| 248 | + } else if (value instanceof Double) { |
| 249 | + builder.addProperty(key, (Double) value); |
| 250 | + } else if (value instanceof Float) { |
| 251 | + builder.addProperty(key, ((Float) value).doubleValue()); |
| 252 | + } else if (value instanceof List) { |
| 253 | + try { |
| 254 | + builder.addProperty(key, (List<String>) value); |
| 255 | + } catch (Exception e) { |
| 256 | + } |
| 257 | + } else { |
| 258 | + // 兜底处理,直接转字符串 |
| 259 | + builder.addProperty(key, String.valueOf(value)); |
| 260 | + } |
| 261 | + } |
| 262 | + } |
| 263 | + |
| 264 | + // 构建最终的 Experiment |
| 265 | + SensorsABTestExperiment<Object> experiment = builder.create(); |
| 266 | + return experiment; |
| 267 | + } |
| 268 | + |
| 269 | + private void checkResult(Object abResult, Result result) { |
| 270 | + try { |
| 271 | + if (abResult instanceof JSONObject) { |
| 272 | + JSONObject jsonObject = (JSONObject) abResult; |
| 273 | + result.success(jsonObject.toString()); |
| 274 | + return; |
| 275 | + } |
| 276 | + result.success(abResult); |
| 277 | + } catch (java.lang.Exception e) { |
| 278 | + // ignored |
| 279 | + } |
| 280 | + } |
| 281 | + |
| 282 | + private static JSONObject assertMapToJSON(Map<String, Object> map) { |
| 283 | + if (map != null) { |
| 284 | + return new JSONObject(map); |
| 285 | + } else { |
| 286 | + // SALog.d(TAG, "传入的属性为空"); |
| 287 | + return null; |
148 | 288 | } |
149 | | - return data; |
150 | 289 | } |
151 | 290 |
|
152 | 291 | @Override |
|
0 commit comments