desktop_shortcut_utils.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:app_links/app_links.dart';
  4. import 'package:electronic_assistant/base/base_controller.dart';
  5. import 'package:electronic_assistant/main.dart';
  6. import 'package:electronic_assistant/module/record/view.dart';
  7. import 'package:electronic_assistant/router/app_pages.dart';
  8. import 'package:electronic_assistant/utils/launcher_url_util.dart';
  9. import 'package:electronic_assistant/utils/mmkv_util.dart';
  10. import 'package:electronic_assistant/utils/toast_util.dart';
  11. import 'package:flutter/material.dart';
  12. import 'package:flutter/services.dart';
  13. import 'package:get/get.dart';
  14. import 'package:get/get_core/src/get_main.dart';
  15. import 'package:url_launcher/url_launcher.dart';
  16. import '../dialog/desktop_shortcut_dialog.dart';
  17. import '../dialog/ios_shortcut_guide_dialog.dart';
  18. import '../module/shortcut/view.dart';
  19. import 'android_shortcut.dart';
  20. class DesktopShortcutUtils {
  21. DesktopShortcutUtils._();
  22. static const String routePath = 'route_path';
  23. static const String _showMaxTimeTag = 'showMaxTime';
  24. static const int _showMaxFrequency = 1;
  25. static Map<String, dynamic>? intentMap;
  26. static StreamSubscription<Uri>? linkSubscription;
  27. static AppLinks appLinks = AppLinks();
  28. static MethodChannel methodChannel =
  29. const MethodChannel('assistant_method_channel');
  30. static Future<void> registerDesktopShortcut() async {
  31. if (Platform.isAndroid) {
  32. androidShortCut.register();
  33. } else if (Platform.isIOS) {
  34. //TODO IOS
  35. registerIosDeeplink();
  36. }
  37. }
  38. // Future<void> setMethodChannelCallHandler() async {}
  39. static Future<void> registerIosDeeplink() async {
  40. methodChannel.setMethodCallHandler((call) async {
  41. DesktopShortcutUtils.setLaunchAction(LaunchAction.recordAudioAction);
  42. Get.toNamed(RoutePath.record);
  43. });
  44. // Handle links
  45. linkSubscription = appLinks.uriLinkStream.listen((uri) {
  46. debugPrint('onAppLink: $uri');
  47. if (uri.scheme == "xiaoting") {
  48. DesktopShortcutUtils.setLaunchAction(LaunchAction.recordAudioAction);
  49. DesktopShortcutUtils.setRouteAction(LaunchAction.recordAudioAction);
  50. }
  51. });
  52. }
  53. static void isShowTipsDialog(void Function() nextCallback) {
  54. if (!_isShow()) {
  55. nextCallback();
  56. return;
  57. }
  58. if (Platform.isAndroid) {
  59. showAddDesktopShortcutDialog(onConfirm: () {
  60. showAddDesktopShortcutTipsDialog(onConfirm: () {
  61. androidShortCut.addRecordShortcut();
  62. }, onDismiss: () {
  63. nextCallback();
  64. });
  65. }, onCancel: () {
  66. nextCallback();
  67. });
  68. } else if (Platform.isAndroid) {
  69. showIosShortcutGuideDialog(
  70. onConfirm: () async {
  71. await ShortCutPage.start();
  72. nextCallback();
  73. },
  74. onCancel: () {
  75. nextCallback();
  76. },
  77. );
  78. }
  79. _setShowOnce();
  80. }
  81. static Future<void> _launchUrl() async {
  82. LauncherUrlUtil.launchHttpUrl('https://xiaoting.atmob.com/record',
  83. mode: LaunchMode.externalApplication);
  84. }
  85. static bool _isShow() {
  86. return KVUtil.getInt(_showMaxTimeTag, 0) < _showMaxFrequency;
  87. }
  88. static void _setShowOnce() {
  89. int maxFrequency = KVUtil.getInt(_showMaxTimeTag, 0);
  90. maxFrequency++;
  91. KVUtil.putInt(_showMaxTimeTag, maxFrequency);
  92. }
  93. static void requestAddDesktopShortcut() {
  94. if (Platform.isAndroid) {
  95. showAddDesktopShortcutTipsDialog(
  96. onConfirm: () {
  97. androidShortCut.addRecordShortcut();
  98. },
  99. onDismiss: () {});
  100. } else if (Platform.isIOS) {
  101. showIosShortcutGuideDialog(
  102. onConfirm: () {
  103. ShortCutPage.start();
  104. },
  105. );
  106. }
  107. _setShowOnce();
  108. }
  109. static void setLaunchAction(String action) {
  110. intentMap ??= {};
  111. intentMap?[LaunchAction.key] = action;
  112. }
  113. static Map<String, dynamic>? getRouteMap() {
  114. return intentMap;
  115. }
  116. static void clearRouteMap() {
  117. intentMap = null;
  118. }
  119. static void setRouteAction(String recordAudioAction) {
  120. if (recordAudioAction == LaunchAction.recordAudioAction) {
  121. RecordPage.start(fromType: RecordFromType.shortcutIcon);
  122. }
  123. }
  124. }
  125. class LaunchAction {
  126. static const String key = 'launchAction';
  127. static const String recordAudioAction = 'record_audio';
  128. }