Skip to content

Commit abfbb1c

Browse files
Merge pull request #2 from guptaShantanu/android
android-integration to base
2 parents 01e87ea + 0a75aa7 commit abfbb1c

File tree

3 files changed

+129
-8
lines changed

3 files changed

+129
-8
lines changed

android/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,7 @@ android {
3131
disable 'InvalidPackage'
3232
}
3333
}
34+
35+
dependencies{
36+
implementation 'io.split.client:android-client:2.7.0'
37+
}
Lines changed: 124 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,149 @@
11
package com.imumz.flutter_split;
22

3+
import android.content.Context;
4+
35
import androidx.annotation.NonNull;
46

7+
import java.io.IOException;
8+
import java.net.URISyntaxException;
9+
import java.util.HashMap;
10+
import java.util.List;
11+
import java.util.Map;
12+
import java.util.concurrent.TimeoutException;
13+
514
import io.flutter.embedding.engine.plugins.FlutterPlugin;
615
import io.flutter.plugin.common.MethodCall;
716
import io.flutter.plugin.common.MethodChannel;
817
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
918
import io.flutter.plugin.common.MethodChannel.Result;
1019
import io.flutter.plugin.common.PluginRegistry.Registrar;
20+
import io.split.android.client.SplitClient;
21+
import io.split.android.client.SplitClientConfig;
22+
import io.split.android.client.SplitFactory;
23+
import io.split.android.client.SplitFactoryBuilder;
24+
import io.split.android.client.SplitResult;
25+
import io.split.android.client.api.Key;
26+
import io.split.android.client.events.SplitEvent;
27+
import io.split.android.client.events.SplitEventTask;
1128

1229
/** FlutterSplitPlugin */
1330
public class FlutterSplitPlugin implements FlutterPlugin, MethodCallHandler {
14-
/// The MethodChannel that will the communication between Flutter and native Android
15-
///
16-
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
17-
/// when the Flutter Engine is detached from the Activity
31+
1832
private MethodChannel channel;
33+
private Context appContext;
34+
35+
36+
private String apikey;
37+
private String userId;
38+
39+
//Split
40+
private SplitClient client;
41+
42+
// error codes
43+
private static String SDK_NOT_INITIALIZED = "SNI001";
44+
1945

2046
@Override
2147
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
2248
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "flutter_split");
49+
this.appContext = flutterPluginBinding.getApplicationContext();
2350
channel.setMethodCallHandler(this);
2451
}
2552

2653
@Override
2754
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
28-
if (call.method.equals("getPlatformVersion")) {
29-
result.success("Android " + android.os.Build.VERSION.RELEASE);
30-
} else {
55+
if(call.method.equals("initializeSdk")){
56+
57+
this.apikey = call.argument("appKey");
58+
SplitClientConfig config = SplitClientConfig.builder().build();
59+
60+
// Create a new user key to be evaluated
61+
this.userId = call.argument("userId");
62+
Key k = new Key(this.userId);
63+
// Create factory
64+
SplitFactory splitFactory = null;
65+
try {
66+
splitFactory = SplitFactoryBuilder.build(this.apikey, k, config, this.appContext);
67+
} catch (IOException e) {
68+
e.printStackTrace();
69+
} catch (InterruptedException e) {
70+
e.printStackTrace();
71+
} catch (TimeoutException e) {
72+
e.printStackTrace();
73+
} catch (URISyntaxException e) {
74+
e.printStackTrace();
75+
}
76+
// Get Split Client instance
77+
this.client = splitFactory.client();
78+
79+
}else if(call.method.equals("trackEvent")){
80+
String eventType = call.argument("eventName");
81+
HashMap<String,Object> map = new HashMap<String,Object>();
82+
if(this.client!=null){
83+
boolean res = client.track(eventType,map);
84+
if(res){
85+
result.success(true);
86+
}else{
87+
88+
}
89+
}else{
90+
result.error(SDK_NOT_INITIALIZED,"Sdk is not initialized","");
91+
}
92+
}else if(call.method.equals("getTreatment")){
93+
String key = call.argument("key");
94+
HashMap<String,Object> attr = call.argument("attributes");
95+
if(this.client!=null){
96+
String treatment = client.getTreatment(key,attr);
97+
result.success(treatment);
98+
}else{
99+
result.error(SDK_NOT_INITIALIZED,"Sdk is not initialized","");
100+
}
101+
}else if(call.method.equals("getTreatmentWithConfig")){
102+
String key = call.argument("key");
103+
HashMap<String,Object> attr = call.argument("attributes");
104+
if(this.client!=null){
105+
SplitResult treatment = client.getTreatmentWithConfig(key,attr);
106+
Map<String,Object> map = new HashMap<>();
107+
map.put("config",treatment.config());
108+
map.put("treatment",treatment.treatment());
109+
result.success(map);
110+
}else{
111+
result.error(SDK_NOT_INITIALIZED,"Sdk is not initialized","");
112+
}
113+
}else if(call.method.equals("getTreatments")){
114+
List<String> keys = call.argument("keys");
115+
HashMap<String,Object> attr = call.argument("attributes");
116+
if(this.client!=null){
117+
Map<String, String> treatment = client.getTreatments(keys,attr);
118+
result.success(treatment);
119+
}else{
120+
result.error(SDK_NOT_INITIALIZED,"Sdk is not initialized","");
121+
}
122+
}else if(call.method.equals("getTreatmentsWithConfig")){
123+
List<String> keys = call.argument("keys");
124+
HashMap<String,Object> attr = call.argument("attributes");
125+
if(this.client!=null){
126+
Map<String, SplitResult> treatment = client.getTreatmentsWithConfig(keys,attr);
127+
Map<String,Map<String,Object>> finalResult = new HashMap<>();
128+
for (Map.Entry<String,SplitResult> entry : treatment.entrySet()){
129+
Map<String,Object> map = new HashMap<>();
130+
map.put("config",entry.getValue().config());
131+
map.put("treatment",entry.getValue().treatment());
132+
finalResult.put(entry.getKey(),map);
133+
}
134+
result.success(finalResult);
135+
}else{
136+
result.error(SDK_NOT_INITIALIZED,"Sdk is not initialized","");
137+
}
138+
}else if(call.method.equals("dispose")){
139+
if(this.client!=null){
140+
this.client.destroy();
141+
result.success(true);
142+
}else{
143+
result.error(SDK_NOT_INITIALIZED,"Sdk is not initialized","");
144+
}
145+
}
146+
else {
31147
result.notImplemented();
32148
}
33149
}
@@ -36,4 +152,4 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
36152
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
37153
channel.setMethodCallHandler(null);
38154
}
39-
}
155+
}

example/android/gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
org.gradle.jvmargs=-Xmx1536M
22
android.useAndroidX=true
33
android.enableJetifier=true
4+
android.enableR8=true

0 commit comments

Comments
 (0)