-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathSurveysApi.java
172 lines (149 loc) · 5.63 KB
/
SurveysApi.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package com.instabug.flutter.modules;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import com.instabug.flutter.generated.SurveysPigeon;
import com.instabug.flutter.util.Reflection;
import com.instabug.flutter.util.ThreadManager;
import com.instabug.library.Feature;
import com.instabug.library.Platform;
import com.instabug.survey.Survey;
import com.instabug.survey.Surveys;
import com.instabug.survey.callbacks.OnDismissCallback;
import com.instabug.survey.callbacks.OnShowCallback;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import io.flutter.plugin.common.BinaryMessenger;
public class SurveysApi implements SurveysPigeon.SurveysHostApi {
private final String TAG = InstabugApi.class.getName();
private final SurveysPigeon.SurveysFlutterApi flutterApi;
public static void init(BinaryMessenger messenger) {
final SurveysPigeon.SurveysFlutterApi flutterApi = new SurveysPigeon.SurveysFlutterApi(messenger);
final SurveysApi api = new SurveysApi(flutterApi);
SurveysPigeon.SurveysHostApi.setup(messenger, api);
}
public SurveysApi(SurveysPigeon.SurveysFlutterApi flutterApi) {
this.flutterApi = flutterApi;
}
@Override
public void setEnabled(@NonNull Boolean isEnabled) {
if (isEnabled) {
Surveys.setState(Feature.State.ENABLED);
} else {
Surveys.setState(Feature.State.DISABLED);
}
}
@Override
public void showSurveyIfAvailable() {
Surveys.showSurveyIfAvailable();
}
/**
* Displays a survey using reflection, eliminating the need to call it on a background thread.
* This variant does not return a boolean indicating the existence of the survey.
* Invoked through reflection.
*/
@VisibleForTesting
public void showSurveyCP(@NonNull String surveyToken) {
try {
Method method = Reflection.getMethod(Class.forName("com.instabug.survey.Surveys"), "showSurveyCP", String.class);
if (method != null) {
method.invoke(null, surveyToken);
} else {
Log.e(TAG, "showSurveyCP was not found by reflection");
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void showSurvey(@NonNull String surveyToken) {
showSurveyCP(surveyToken);
}
@Override
public void setAutoShowingEnabled(@NonNull Boolean isEnabled) {
Surveys.setAutoShowingEnabled(isEnabled);
}
@Override
public void setShouldShowWelcomeScreen(@NonNull Boolean shouldShowWelcomeScreen) {
Surveys.setShouldShowWelcomeScreen(shouldShowWelcomeScreen);
}
@Override
public void setAppStoreURL(@NonNull String appStoreURL) {
// iOS Only
}
@Override
public void hasRespondedToSurvey(@NonNull String surveyToken, SurveysPigeon.Result<Boolean> result) {
ThreadManager.runOnBackground(
new Runnable() {
@Override
public void run() {
final boolean hasResponded = Surveys.hasRespondToSurvey(surveyToken);
ThreadManager.runOnMainThread(new Runnable() {
@Override
public void run() {
result.success(hasResponded);
}
});
}
}
);
}
@Override
public void getAvailableSurveys(SurveysPigeon.Result<List<String>> result) {
ThreadManager.runOnBackground(
new Runnable() {
@Override
public void run() {
List<Survey> surveys = Surveys.getAvailableSurveys();
ArrayList<String> titles = new ArrayList<>();
for (Survey survey : surveys != null ? surveys : new ArrayList<Survey>()) {
titles.add(survey.getTitle());
}
ThreadManager.runOnMainThread(new Runnable() {
@Override
public void run() {
result.success(titles);
}
});
}
}
);
}
@Override
public void bindOnShowSurveyCallback() {
Surveys.setOnShowCallback(new OnShowCallback() {
@Override
public void onShow() {
ThreadManager.runOnMainThread(new Runnable() {
@Override
public void run() {
flutterApi.onShowSurvey(new SurveysPigeon.SurveysFlutterApi.Reply<Void>() {
@Override
public void reply(Void reply) {
}
});
}
});
}
});
}
@Override
public void bindOnDismissSurveyCallback() {
Surveys.setOnDismissCallback(new OnDismissCallback() {
@Override
public void onDismiss() {
ThreadManager.runOnMainThread(new Runnable() {
@Override
public void run() {
flutterApi.onDismissSurvey(new SurveysPigeon.SurveysFlutterApi.Reply<Void>() {
@Override
public void reply(Void reply) {
}
});
}
});
}
});
}
}