55package io .flutter .plugins .deviceinfo ;
66
77import android .content .Context ;
8+ import android .util .Log ;
89import io .flutter .embedding .engine .plugins .FlutterPlugin ;
910import io .flutter .plugin .common .BinaryMessenger ;
1011import 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 ;
1116
1217/** DeviceInfoPlugin */
1318public class DeviceInfoPlugin implements FlutterPlugin {
14-
19+ static final String TAG = "DeviceInfoPlugin" ;
1520 MethodChannel channel ;
1621
1722 /** Plugin registration. */
@@ -32,7 +37,24 @@ public void onDetachedFromEngine(FlutterPlugin.FlutterPluginBinding binding) {
3237 }
3338
3439 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+ }
3658 final MethodCallHandlerImpl handler =
3759 new MethodCallHandlerImpl (context .getContentResolver (), context .getPackageManager ());
3860 channel .setMethodCallHandler (handler );
0 commit comments