flutter_mehod_channel_util.dart 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import 'dart:io';
  2. import 'package:flutter/services.dart';
  3. import 'package:o2oa_all_platform/common/index.dart';
  4. class O2FlutterMethodChannelUtils {
  5. // 通信通道名称
  6. final String flutterPCNotifyChannelName = "pulgin.pc.o2oa.net/notify";
  7. // Android ios 通道
  8. final String channelName = "o2oa.net/flutter_inner";
  9. // 通信方法名称
  10. final cmdNameNotify = "sendNotify";
  11. // macos 通道
  12. MethodChannel? _methodChannelMacos;
  13. MethodChannel? _methodChannel;
  14. //私有构造函数
  15. O2FlutterMethodChannelUtils._internal();
  16. //保存单例
  17. static final O2FlutterMethodChannelUtils _singleton =
  18. O2FlutterMethodChannelUtils._internal();
  19. //工厂构造函数
  20. factory O2FlutterMethodChannelUtils() => _singleton;
  21. /// 发送 系统通知 目前实现 windows和macos
  22. ///
  23. Future<void> sendIMMsgToPCNotify(String? id, String title, String msgBody) async {
  24. OLogger.d('发送IM消息,title: $title , body: $msgBody');
  25. _methodChannelMacos ??= MethodChannel(flutterPCNotifyChannelName);
  26. final Map<String, String> params = {"title": title, "msgBody": msgBody};
  27. if (id != null) {
  28. params["msgId"] = id;
  29. }
  30. try {
  31. final answer = await _methodChannelMacos?.invokeMethod(
  32. cmdNameNotify, params); // .invokeListMethod<String>(CMD_OPEN_DIALOG, params);
  33. OLogger.i(answer);
  34. } on PlatformException catch (e) {
  35. OLogger.e("发送IM消息出错,${e.message}");
  36. } on MissingPluginException catch (e) {
  37. OLogger.e("发送IM消息出错,${e.message}");
  38. }
  39. }
  40. ///
  41. /// 打开本地文件
  42. /// @param filePath 文件路径
  43. ///
  44. Future<void> openLocalFile(String filePath, {bool shareBtnShow = true}) async {
  45. OLogger.d("打开文件:$filePath");
  46. final Map<String, dynamic> params = {"filePath": filePath, "shareBtnShow": shareBtnShow};
  47. _methodChannel ??= MethodChannel(channelName);
  48. try {
  49. String? result = await _methodChannel?.invokeMethod("openFile", params);
  50. OLogger.i("文件打开完成:$result");
  51. } on PlatformException catch (e) {
  52. OLogger.e("打开文件出错,${e.message}");
  53. } on MissingPluginException catch (e) {
  54. OLogger.e("打开文件出错,${e.message}");
  55. }
  56. }
  57. /// Android 安装 apk
  58. Future<void> installApk(String filePath) async {
  59. if (!Platform.isAndroid) {
  60. OLogger.e('该方法只支持 Android 端');
  61. return;
  62. }
  63. final Map<String, String> params = {"filePath": filePath};
  64. _methodChannel ??= MethodChannel(channelName);
  65. try {
  66. String? result = await _methodChannel?.invokeMethod("installAPK", params);
  67. OLogger.i("安装 apk 完成:$result");
  68. } on PlatformException catch (e) {
  69. OLogger.e("安装 apk 出错,${e.message}");
  70. } on MissingPluginException catch (e) {
  71. OLogger.e("安装 apk 出错,${e.message}");
  72. }
  73. }
  74. /// 原生 清除webview缓存
  75. Future<void> clearCacheNative() async {
  76. OLogger.d('清除缓存,原生方法!');
  77. if (!Platform.isAndroid && !Platform.isIOS) {
  78. OLogger.e('该方法只支持 移动端');
  79. return;
  80. }
  81. _methodChannel ??= MethodChannel(channelName);
  82. try {
  83. String? result = await _methodChannel?.invokeMethod("clearCache", {});
  84. OLogger.i("清除缓存 完成:$result");
  85. } on PlatformException catch (e) {
  86. OLogger.e("清除缓存 出错,${e.message}");
  87. } on MissingPluginException catch (e) {
  88. OLogger.e("清除缓存 出错,${e.message}");
  89. }
  90. }
  91. /// 检查是否 root 或 是否越狱
  92. Future<bool> checkRootNative() async {
  93. OLogger.d('检查是否已经 root 或 越狱!');
  94. if (!Platform.isAndroid && !Platform.isIOS) {
  95. OLogger.e('该方法只支持 移动端');
  96. return false;
  97. }
  98. _methodChannel ??= MethodChannel(channelName);
  99. try {
  100. bool? result = await _methodChannel?.invokeMethod("checkRoot", {});
  101. OLogger.i("checkRoot 结果:$result");
  102. return result == true;
  103. } on PlatformException catch (e) {
  104. OLogger.e("checkRoot 出错,${e.message}");
  105. } on MissingPluginException catch (e) {
  106. OLogger.e("checkRoot 出错,${e.message}");
  107. }
  108. return false;
  109. }
  110. Future<bool> saveToAlbum(String filePath) async {
  111. OLogger.d('保存到相册,原生方法!');
  112. if (!Platform.isAndroid && !Platform.isIOS) {
  113. OLogger.e('该方法只支持 移动端');
  114. return false;
  115. }
  116. _methodChannel ??= MethodChannel(channelName);
  117. try {
  118. bool? result = await _methodChannel?.invokeMethod("saveToAlbum", {"filePath": filePath});
  119. OLogger.i("保存到相册 结果:$result");
  120. return result == true;
  121. } on PlatformException catch (e) {
  122. OLogger.e("保存到相册 出错,${e.message}");
  123. } on MissingPluginException catch (e) {
  124. OLogger.e("保存到相册 出错,${e.message}");
  125. }
  126. return false;
  127. }
  128. /// 是否 pad 设备
  129. Future<bool> isTabletDevice() async {
  130. OLogger.d('检查设备是否是 pad');
  131. if (!Platform.isAndroid && !Platform.isIOS) {
  132. OLogger.e('该方法只支持 移动端');
  133. return false;
  134. }
  135. _methodChannel ??= MethodChannel(channelName);
  136. try {
  137. bool? result = await _methodChannel?.invokeMethod("isTabletDevice", {});
  138. OLogger.i("isTabletDevice 结果:$result");
  139. return result == true;
  140. } on PlatformException catch (e) {
  141. OLogger.e("isTabletDevice 出错,${e.message}");
  142. } on MissingPluginException catch (e) {
  143. OLogger.e("isTabletDevice 出错,${e.message}");
  144. }
  145. return false;
  146. }
  147. }