5
5
package io .flutter .plugins .deviceinfo ;
6
6
7
7
import android .content .Context ;
8
+ import android .util .Log ;
8
9
import io .flutter .embedding .engine .plugins .FlutterPlugin ;
9
10
import io .flutter .plugin .common .BinaryMessenger ;
10
11
import io .flutter .plugin .common .MethodChannel ;
12
+ import io .flutter .plugin .common .MethodCodec ;
13
+ import io .flutter .plugin .common .StandardMethodCodec ;
14
+ import java .lang .reflect .Constructor ;
15
+ import java .lang .reflect .Method ;
11
16
12
17
/** DeviceInfoPlugin */
13
18
public class DeviceInfoPlugin implements FlutterPlugin {
14
-
19
+ static final String TAG = "DeviceInfoPlugin" ;
15
20
MethodChannel channel ;
16
21
17
22
/** Plugin registration. */
@@ -32,7 +37,24 @@ public void onDetachedFromEngine(FlutterPlugin.FlutterPluginBinding binding) {
32
37
}
33
38
34
39
private void setupMethodChannel (BinaryMessenger messenger , Context context ) {
35
- channel = new MethodChannel (messenger , "plugins.flutter.io/device_info" );
40
+ String channelName = "plugins.flutter.io/device_info" ;
41
+ // TODO(gaaclarke): Remove reflection guard when https://github.com/flutter/engine/pull/29147
42
+ // becomes available on the stable branch.
43
+ try {
44
+ Class methodChannelClass = Class .forName ("io.flutter.plugin.common.MethodChannel" );
45
+ Class taskQueueClass = Class .forName ("io.flutter.plugin.common.BinaryMessenger$TaskQueue" );
46
+ Method makeBackgroundTaskQueue = messenger .getClass ().getMethod ("makeBackgroundTaskQueue" );
47
+ Object taskQueue = makeBackgroundTaskQueue .invoke (messenger );
48
+ Constructor <MethodChannel > constructor =
49
+ methodChannelClass .getConstructor (
50
+ BinaryMessenger .class , String .class , MethodCodec .class , taskQueueClass );
51
+ channel =
52
+ constructor .newInstance (messenger , channelName , StandardMethodCodec .INSTANCE , taskQueue );
53
+ Log .d (TAG , "Use TaskQueues." );
54
+ } catch (Exception ex ) {
55
+ channel = new MethodChannel (messenger , channelName );
56
+ Log .d (TAG , "Don't use TaskQueues." );
57
+ }
36
58
final MethodCallHandlerImpl handler =
37
59
new MethodCallHandlerImpl (context .getContentResolver (), context .getPackageManager ());
38
60
channel .setMethodCallHandler (handler );
0 commit comments