11
11
12
12
import com .fm .openinstall .Configuration ;
13
13
import com .fm .openinstall .OpenInstall ;
14
- import com .fm .openinstall .listener .AppInstallAdapter ;
15
14
import com .fm .openinstall .listener .AppInstallListener ;
16
15
import com .fm .openinstall .listener .AppInstallRetryAdapter ;
17
16
import com .fm .openinstall .listener .AppWakeUpAdapter ;
20
19
import com .fm .openinstall .model .AppData ;
21
20
import com .fm .openinstall .model .Error ;
22
21
23
- import java .util .Collections ;
24
22
import java .util .HashMap ;
25
23
import java .util .Map ;
26
24
@@ -45,6 +43,7 @@ public class OpeninstallFlutterPlugin implements FlutterPlugin, MethodCallHandle
45
43
@ Deprecated
46
44
private static final String METHOD_WAKEUP = "registerWakeup" ;
47
45
46
+ private static final String METHOD_DEBUG = "setDebug" ;
48
47
private static final String METHOD_CONFIG = "config" ;
49
48
private static final String METHOD_CLIPBOARD_ENABLED = "clipBoardEnabled" ;
50
49
private static final String METHOD_SERIAL_ENABLED = "serialEnabled" ;
@@ -54,6 +53,8 @@ public class OpeninstallFlutterPlugin implements FlutterPlugin, MethodCallHandle
54
53
private static final String METHOD_REGISTER = "reportRegister" ;
55
54
private static final String METHOD_EFFECT_POINT = "reportEffectPoint" ;
56
55
private static final String METHOD_SHARE = "reportShare" ;
56
+ private static final String METHOD_OPID = "getOpid" ;
57
+ private static final String METHOD_CHANNEL = "setChannel" ;
57
58
58
59
private static final String METHOD_WAKEUP_NOTIFICATION = "onWakeupNotification" ;
59
60
private static final String METHOD_INSTALL_NOTIFICATION = "onInstallNotification" ;
@@ -66,13 +67,15 @@ public class OpeninstallFlutterPlugin implements FlutterPlugin, MethodCallHandle
66
67
private Configuration configuration = null ;
67
68
68
69
private boolean alwaysCallback = false ;
70
+ private boolean debuggable = true ;
69
71
70
72
71
73
@ Override
72
74
public void onAttachedToEngine (@ NonNull FlutterPluginBinding binding ) {
73
75
flutterPluginBinding = binding ;
74
76
channel = new MethodChannel (flutterPluginBinding .getBinaryMessenger (), "openinstall_flutter_plugin" );
75
77
channel .setMethodCallHandler (this );
78
+ OpenInstall .preInit (flutterPluginBinding .getApplicationContext ());
76
79
}
77
80
78
81
@ Override
@@ -91,8 +94,13 @@ public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBindin
91
94
92
95
@ Override
93
96
public void onMethodCall (MethodCall call , @ NonNull final Result result ) {
94
- Log .d (TAG , "invoke " + call .method );
95
- if (METHOD_CONFIG .equalsIgnoreCase (call .method )) {
97
+ debugLog ("invoke " + call .method );
98
+ if (METHOD_DEBUG .equalsIgnoreCase (call .method )) {
99
+ Boolean enabled = call .argument ("enabled" );
100
+ debuggable = enabled == null ? true : enabled ;
101
+ OpenInstall .setDebug (debuggable );
102
+ result .success ("OK" );
103
+ } else if (METHOD_CONFIG .equalsIgnoreCase (call .method )) {
96
104
config (call );
97
105
result .success ("OK" );
98
106
} else if (METHOD_CLIPBOARD_ENABLED .equalsIgnoreCase (call .method )) {
@@ -120,21 +128,24 @@ public void onMethodCall(MethodCall call, @NonNull final Result result) {
120
128
OpenInstall .getInstall (new AppInstallListener () {
121
129
@ Override
122
130
public void onInstallFinish (AppData appData , Error error ) {
123
- Map <String , String > data = data2Map (appData );
124
- boolean shouldRetry = error !=null && error .shouldRetry ();
125
- data .put ("shouldRetry" , String .valueOf (shouldRetry ));
126
- channel .invokeMethod (METHOD_INSTALL_NOTIFICATION ,data );
131
+ Map <String , Object > data = data2Map (appData );
132
+ boolean shouldRetry = error != null && error .shouldRetry ();
133
+ data .put ("shouldRetry" , shouldRetry );
134
+ if (error != null ) {
135
+ data .put ("message" , error .getErrorMsg ());
136
+ }
137
+ channel .invokeMethod (METHOD_INSTALL_NOTIFICATION , data );
127
138
}
128
139
}, seconds == null ? 0 : seconds );
129
140
result .success ("OK" );
130
141
} else if (METHOD_INSTALL_RETRY .equalsIgnoreCase (call .method )) {
131
142
Integer seconds = call .argument ("seconds" );
132
143
OpenInstall .getInstallCanRetry (new AppInstallRetryAdapter () {
133
144
@ Override
134
- public void onInstall (AppData appData , boolean retry ) {
135
- Map <String , String > data = data2Map (appData );
136
- data .put ("retry" , String .valueOf (retry ));
137
- data .put ("shouldRetry" , String . valueOf ( retry ) ); // 以后保存统一
145
+ public void onInstall (AppData appData , boolean shouldRetry ) {
146
+ Map <String , Object > data = data2Map (appData );
147
+ data .put ("retry" , String .valueOf (shouldRetry )); // 2.4.0 之前的版本返回
148
+ data .put ("shouldRetry" , shouldRetry ); // 以后保存统一
138
149
channel .invokeMethod (METHOD_INSTALL_NOTIFICATION , data );
139
150
}
140
151
}, seconds == null ? 0 : seconds );
@@ -145,35 +156,42 @@ public void onInstall(AppData appData, boolean retry) {
145
156
} else if (METHOD_EFFECT_POINT .equalsIgnoreCase (call .method )) {
146
157
String pointId = call .argument ("pointId" );
147
158
Integer pointValue = call .argument ("pointValue" );
148
- if (TextUtils .isEmpty (pointId ) || pointValue == null ){
159
+ if (TextUtils .isEmpty (pointId ) || pointValue == null ) {
149
160
Log .w (TAG , "pointId is empty or pointValue is null" );
150
161
// result.error("ERROR", "pointId is empty or pointValue is null", null);
151
- }else {
162
+ } else {
152
163
Map <String , String > extraMap = call .argument ("extras" );
153
164
OpenInstall .reportEffectPoint (pointId , pointValue , extraMap );
154
165
}
155
166
result .success ("OK" );
156
167
} else if (METHOD_SHARE .equalsIgnoreCase (call .method )) {
157
168
String shareCode = call .argument ("shareCode" );
158
169
String sharePlatform = call .argument ("platform" );
159
- final Map <String , String > data = new HashMap <>();
160
- if (TextUtils .isEmpty (shareCode ) || TextUtils .isEmpty (sharePlatform )){
161
- data .put ("message" , "shareCode or platform is empty" );
162
- data .put ("shouldRetry" , String . valueOf ( false ) );
170
+ final Map <String , Object > data = new HashMap <>();
171
+ if (TextUtils .isEmpty (shareCode ) || TextUtils .isEmpty (sharePlatform )) {
172
+ data .put ("message" , "shareCode or platform is empty" );
173
+ data .put ("shouldRetry" , false );
163
174
result .success (data );
164
- }else {
175
+ } else {
165
176
OpenInstall .reportShare (shareCode , sharePlatform , new ResultCallback <Void >() {
166
177
@ Override
167
178
public void onResult (@ Nullable Void v , @ Nullable Error error ) {
168
- boolean shouldRetry = error != null && error .shouldRetry ();
169
- data .put ("shouldRetry" , String . valueOf ( shouldRetry ) );
170
- if (error != null ) {
179
+ boolean shouldRetry = error != null && error .shouldRetry ();
180
+ data .put ("shouldRetry" , shouldRetry );
181
+ if (error != null ) {
171
182
data .put ("message" , error .getErrorMsg ());
172
183
}
173
184
result .success (data );
174
185
}
175
186
});
176
187
}
188
+ } else if (METHOD_OPID .equalsIgnoreCase (call .method )) {
189
+ String opid = OpenInstall .getOpid ();
190
+ result .success (opid );
191
+ } else if (METHOD_CHANNEL .equalsIgnoreCase (call .method )) {
192
+ String channelCode = call .argument ("channelCode" );
193
+ OpenInstall .setChannel (channelCode );
194
+ result .success ("OK" );
177
195
} else {
178
196
result .notImplemented ();
179
197
}
@@ -203,7 +221,7 @@ private void config(MethodCall call) {
203
221
String gaid = call .argument ("gaid" );
204
222
builder .gaid (gaid );
205
223
}
206
- if (call .hasArgument ("imeiDisabled" )){
224
+ if (call .hasArgument ("imeiDisabled" )) {
207
225
Boolean imeiDisabled = call .argument ("imeiDisabled" );
208
226
if (checkBoolean (imeiDisabled )) {
209
227
builder .imeiDisabled ();
@@ -213,7 +231,7 @@ private void config(MethodCall call) {
213
231
String imei = call .argument ("imei" );
214
232
builder .imei (imei );
215
233
}
216
- if (call .hasArgument ("macDisabled" )){
234
+ if (call .hasArgument ("macDisabled" )) {
217
235
Boolean macDisabled = call .argument ("macDisabled" );
218
236
if (checkBoolean (macDisabled )) {
219
237
builder .macDisabled ();
@@ -225,7 +243,7 @@ private void config(MethodCall call) {
225
243
}
226
244
227
245
configuration = builder .build ();
228
- // Log.d(TAG, String.format("Configuration: adEnabled=%s, oaid=%s, gaid=%s, macDisabled=%s, imeiDisabled=%s, "
246
+ // debugLog( String.format("Configuration: adEnabled=%s, oaid=%s, gaid=%s, macDisabled=%s, imeiDisabled=%s, "
229
247
// + "androidId=%s, serialNumber=%s, imei=%s, mac=%s",
230
248
// configuration.isAdEnabled(), configuration.getOaid(), configuration.getGaid(),
231
249
// configuration.isMacDisabled(), configuration.isImeiDisabled(),
@@ -241,56 +259,48 @@ private boolean checkBoolean(Boolean bool) {
241
259
242
260
private void init () {
243
261
Context context = flutterPluginBinding .getApplicationContext ();
244
- if (context != null ) {
245
- OpenInstall .init (context , configuration );
246
- initialized = true ;
247
- if (intentHolder != null ) {
248
- wakeup (intentHolder );
249
- intentHolder = null ;
250
- }
251
- } else {
252
- Log .d (TAG , "Context is null, can't init" );
262
+ OpenInstall .init (context , configuration );
263
+ initialized = true ;
264
+ if (intentHolder != null ) {
265
+ wakeup (intentHolder );
266
+ intentHolder = null ;
253
267
}
254
268
}
255
269
256
270
@ Deprecated
257
271
private void initWithPermission () {
258
272
Activity activity = activityPluginBinding .getActivity ();
259
- if (activity == null ) {
260
- Log .d (TAG , "Activity is null, can't initWithPermission, replace with init" );
261
- init ();
262
- } else {
263
- activityPluginBinding .addRequestPermissionsResultListener (permissionsResultListener );
264
- OpenInstall .initWithPermission (activity , configuration , new Runnable () {
265
- @ Override
266
- public void run () {
267
- activityPluginBinding .removeRequestPermissionsResultListener (permissionsResultListener );
268
- initialized = true ;
269
- if (intentHolder != null ) {
270
- wakeup (intentHolder );
271
- intentHolder = null ;
272
- }
273
+ activityPluginBinding .addRequestPermissionsResultListener (permissionsResultListener );
274
+ OpenInstall .initWithPermission (activity , configuration , new Runnable () {
275
+ @ Override
276
+ public void run () {
277
+ activityPluginBinding .removeRequestPermissionsResultListener (permissionsResultListener );
278
+ initialized = true ;
279
+ if (intentHolder != null ) {
280
+ wakeup (intentHolder );
281
+ intentHolder = null ;
273
282
}
274
- });
275
- }
283
+ }
284
+ });
276
285
}
277
286
278
287
@ Override
279
288
public boolean onNewIntent (Intent intent ) {
289
+ debugLog ("onNewIntent" );
280
290
wakeup (intent );
281
291
return false ;
282
292
}
283
293
284
294
285
295
private void wakeup (Intent intent ) {
286
296
if (initialized ) {
287
- Log . d ( TAG , "getWakeUp : alwaysCallback=" + alwaysCallback );
297
+ debugLog ( "getWakeUp : alwaysCallback=" + alwaysCallback );
288
298
if (alwaysCallback ) {
289
299
OpenInstall .getWakeUpAlwaysCallback (intent , new AppWakeUpListener () {
290
300
@ Override
291
301
public void onWakeUpFinish (AppData appData , Error error ) {
292
302
if (error != null ) { // 可忽略,仅调试使用
293
- Log . d ( TAG , "getWakeUpAlwaysCallback : " + error .getErrorMsg ());
303
+ debugLog ( "getWakeUpAlwaysCallback : " + error .getErrorMsg ());
294
304
}
295
305
channel .invokeMethod (METHOD_WAKEUP_NOTIFICATION , data2Map (appData ));
296
306
}
@@ -318,15 +328,21 @@ public boolean onRequestPermissionsResult(int requestCode, String[] permissions,
318
328
}
319
329
};
320
330
321
- private static Map <String , String > data2Map (AppData data ) {
322
- Map <String , String > result = new HashMap <>();
331
+ private static Map <String , Object > data2Map (AppData data ) {
332
+ Map <String , Object > result = new HashMap <>();
323
333
if (data != null ) {
324
334
result .put ("channelCode" , data .getChannel ());
325
335
result .put ("bindData" , data .getData ());
326
336
}
327
337
return result ;
328
338
}
329
339
340
+ private void debugLog (String message ) {
341
+ if (debuggable ) {
342
+ Log .d (TAG , message );
343
+ }
344
+ }
345
+
330
346
@ Override
331
347
public void onDetachedFromEngine (@ NonNull FlutterPluginBinding binding ) {
332
348
0 commit comments