Browse Source

feat(权限): 改进悬浮窗权限请求流程并添加场景区分

为悬浮窗权限请求添加不同场景的说明文案,包括翻译悬浮窗和录音悬浮条场景
改进Android端权限请求逻辑,增加系统设置跳转的容错处理
添加相关多语言文案支持
weicu
lxm 9 months ago
parent
commit
43ce0daa67
  1. 45
      android/app/src/main/kotlin/com/saitong/voitrans/OverlayPermissionPlugin.kt
  2. 5
      lib/core/services/floating_window_manager.dart
  3. 7
      lib/core/translations/language/en_us.dart
  4. 7
      lib/core/translations/language/zh_cn.dart
  5. 40
      lib/core/utils/overlay_permission_util.dart
  6. 4
      lib/modules/meeting/controllers/meeting_record_controller.dart

45
android/app/src/main/kotlin/com/saitong/voitrans/OverlayPermissionPlugin.kt

@ -4,6 +4,7 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.content.pm.PackageManager
import android.provider.Settings import android.provider.Settings
import io.flutter.embedding.engine.plugins.FlutterPlugin import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.MethodCall
@ -46,12 +47,32 @@ class OverlayPermissionPlugin: FlutterPlugin, MethodCallHandler {
private fun requestOverlayPermission(result: Result) { private fun requestOverlayPermission(result: Result) {
try { try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION).apply { if (hasOverlayPermission()) {
data = Uri.parse("package:${context.packageName}") result.success(true)
flags = Intent.FLAG_ACTIVITY_NEW_TASK return
} }
context.startActivity(intent)
result.success(true) val openedBySystem = startActivitySafe(
Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION).apply {
data = Uri.parse("package:${context.packageName}")
}
)
if (openedBySystem) {
result.success(true)
return
}
val openedByAppDetails = startActivitySafe(
Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply {
data = Uri.parse("package:${context.packageName}")
}
)
if (openedByAppDetails) {
result.success(true)
return
}
result.success(false)
} else { } else {
result.success(true) result.success(true)
} }
@ -60,7 +81,19 @@ class OverlayPermissionPlugin: FlutterPlugin, MethodCallHandler {
} }
} }
private fun startActivitySafe(intent: Intent): Boolean {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
val pm: PackageManager = context.packageManager
if (intent.resolveActivity(pm) == null) return false
return try {
context.startActivity(intent)
true
} catch (_: Exception) {
false
}
}
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) { override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null) channel.setMethodCallHandler(null)
} }
} }

5
lib/core/services/floating_window_manager.dart

@ -91,8 +91,9 @@ class FloatingWindowManager extends GetxController {
/// 启用悬浮窗功能 /// 启用悬浮窗功能
Future<bool> enableFloatingWindow() async { Future<bool> enableFloatingWindow() async {
if (!_hasPermission.value) { if (!_hasPermission.value) {
final bool granted = final bool granted = await OverlayPermissionUtil.requestOverlayPermission(
await OverlayPermissionUtil.requestOverlayPermission(); scene: OverlayPermissionScene.translationFloatingWindow,
);
if (!granted) { if (!granted) {
Get.snackbar('权限不足'.tr, '需要悬浮窗权限才能启用此功能'.tr); Get.snackbar('权限不足'.tr, '需要悬浮窗权限才能启用此功能'.tr);
return false; return false;

7
lib/core/translations/language/en_us.dart

@ -446,6 +446,13 @@ const Map<String, String> enUS = {
'meetingRecordRecordingNotificationTitle': 'Recording in progress', // 录音进行中 'meetingRecordRecordingNotificationTitle': 'Recording in progress', // 录音进行中
'meetingRecordRecordingNotificationBody': 'meetingRecordRecordingNotificationBody':
'Recording in background. Tap to return to the app.', // 正在后台录音,点击返回应用 'Recording in background. Tap to return to the app.', // 正在后台录音,点击返回应用
'overlayPermissionTitle': 'Draw over other apps', // 悬浮窗权限
'overlayPermissionTranslationDesc':
'To show translation results during calls or audio/video translation, we need the overlay permission. This lets you see real-time translations while using other apps.', // 为了在通话或音视频翻译时显示翻译结果,需要悬浮窗权限。这样您就可以在使用其他应用时看到实时翻译。
'overlayPermissionRecordingDesc':
'To show the recording floating bar (timer/pause/resume) during recording, we need the overlay permission.', // 为了在录音过程中显示录音悬浮条(计时/暂停/继续),需要悬浮窗权限。
'overlayPermissionOpenSettingsFailed':
'Unable to open system settings automatically. Please open Settings and enable the overlay permission manually.', // 无法自动跳转到系统设置,请手动前往系统设置开启悬浮窗权限。
'transcribing': 'Transcribing...', 'transcribing': 'Transcribing...',
'summarizing': 'Summarizing...', 'summarizing': 'Summarizing...',
'analyzing': 'Analyzing...', 'analyzing': 'Analyzing...',

7
lib/core/translations/language/zh_cn.dart

@ -421,6 +421,13 @@ const Map<String, String> zhCN = {
'meetingRecordContinueInBackground': '后台继续录音', // 后台继续录音 'meetingRecordContinueInBackground': '后台继续录音', // 后台继续录音
'meetingRecordRecordingNotificationTitle': '录音进行中', // 录音进行中 'meetingRecordRecordingNotificationTitle': '录音进行中', // 录音进行中
'meetingRecordRecordingNotificationBody': '正在后台录音,点击返回应用', // 正在后台录音,点击返回应用 'meetingRecordRecordingNotificationBody': '正在后台录音,点击返回应用', // 正在后台录音,点击返回应用
'overlayPermissionTitle': '悬浮窗权限', // 悬浮窗权限
'overlayPermissionTranslationDesc':
'为了在通话或音视频翻译时显示翻译结果,需要悬浮窗权限。这样您就可以在使用其他应用时看到实时翻译。', // 为了在通话或音视频翻译时显示翻译结果,需要悬浮窗权限。这样您就可以在使用其他应用时看到实时翻译。
'overlayPermissionRecordingDesc':
'为了在录音过程中显示录音悬浮条(计时/暂停/继续),需要悬浮窗权限。', // 为了在录音过程中显示录音悬浮条(计时/暂停/继续),需要悬浮窗权限。
'overlayPermissionOpenSettingsFailed':
'无法自动跳转到系统设置,请手动前往系统设置开启悬浮窗权限。', // 无法自动跳转到系统设置,请手动前往系统设置开启悬浮窗权限。
'transcribing': '正在转写...', 'transcribing': '正在转写...',
'summarizing': '正在总结...', 'summarizing': '正在总结...',
'analyzing': '正在分析...', 'analyzing': '正在分析...',

40
lib/core/utils/overlay_permission_util.dart

@ -4,6 +4,12 @@ import 'package:flutter/services.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'logger.dart'; import 'logger.dart';
/// 悬浮窗权限场景
enum OverlayPermissionScene {
translationFloatingWindow,
recordingFloatingBar,
}
/// 悬浮窗权限管理工具类 /// 悬浮窗权限管理工具类
class OverlayPermissionUtil { class OverlayPermissionUtil {
static const MethodChannel _channel = MethodChannel('overlay_permission'); static const MethodChannel _channel = MethodChannel('overlay_permission');
@ -12,7 +18,8 @@ class OverlayPermissionUtil {
static Future<bool> hasOverlayPermission() async { static Future<bool> hasOverlayPermission() async {
if (Platform.isAndroid) { if (Platform.isAndroid) {
try { try {
final bool hasPermission = await _channel.invokeMethod('hasOverlayPermission'); final bool hasPermission =
await _channel.invokeMethod('hasOverlayPermission');
Logger.info('检查悬浮窗权限: $hasPermission'); Logger.info('检查悬浮窗权限: $hasPermission');
return hasPermission; return hasPermission;
} catch (e) { } catch (e) {
@ -25,26 +32,36 @@ class OverlayPermissionUtil {
} }
/// 请求悬浮窗权限 /// 请求悬浮窗权限
static Future<bool> requestOverlayPermission() async { static Future<bool> requestOverlayPermission({
OverlayPermissionScene scene =
OverlayPermissionScene.translationFloatingWindow,
}) async {
if (Platform.isAndroid) { if (Platform.isAndroid) {
// 首先检查是否已有权限 // 首先检查是否已有权限
if (await hasOverlayPermission()) { if (await hasOverlayPermission()) {
return true; return true;
} }
final String explanationKey = switch (scene) {
OverlayPermissionScene.translationFloatingWindow =>
'overlayPermissionTranslationDesc', // 为了在通话或音视频翻译时显示翻译结果,需要悬浮窗权限。这样您就可以在使用其他应用时看到实时翻译。
OverlayPermissionScene.recordingFloatingBar =>
'overlayPermissionRecordingDesc', // 为了在录音过程中显示录音悬浮条(计时/暂停/继续),需要悬浮窗权限。
};
// 显示说明对话框 // 显示说明对话框
bool? userAgreed = await Get.dialog<bool>( bool? userAgreed = await Get.dialog<bool>(
AlertDialog( AlertDialog(
title: Text('悬浮窗权限'.tr), title: Text('overlayPermissionTitle'.tr), // 悬浮窗权限
content: Text('为了在通话或音视频翻译时显示翻译结果,需要悬浮窗权限。这样您就可以在使用其他应用时看到实时翻译。'.tr), content: Text(explanationKey.tr), // 按场景显示不同说明文案
actions: [ actions: [
TextButton( TextButton(
onPressed: () => Get.back(result: false), onPressed: () => Get.back(result: false),
child: Text('取消'.tr), child: Text('cancel'.tr), // 取消
), ),
ElevatedButton( ElevatedButton(
onPressed: () => Get.back(result: true), onPressed: () => Get.back(result: true),
child: Text('去设置'.tr), child: Text('goToSettings'.tr), // 去设置
), ),
], ],
), ),
@ -54,7 +71,16 @@ class OverlayPermissionUtil {
if (userAgreed == true) { if (userAgreed == true) {
try { try {
// 跳转系统设置页 // 跳转系统设置页
await _channel.invokeMethod('requestOverlayPermission'); final bool opened =
await _channel.invokeMethod('requestOverlayPermission');
if (!opened) {
Get.snackbar(
'permissionRequestError'.tr,
'overlayPermissionOpenSettingsFailed'
.tr, // 无法自动跳转到系统设置,请手动前往系统设置开启悬浮窗权限。
);
return false;
}
// 等待用户操作完成后重新检查权限 // 等待用户操作完成后重新检查权限
await Future.delayed(const Duration(seconds: 1)); await Future.delayed(const Duration(seconds: 1));
return await hasOverlayPermission(); return await hasOverlayPermission();

4
lib/modules/meeting/controllers/meeting_record_controller.dart

@ -135,7 +135,9 @@ class MeetingRecordController extends GetxController
final hasPermission = await OverlayPermissionUtil.hasOverlayPermission(); final hasPermission = await OverlayPermissionUtil.hasOverlayPermission();
if (!hasPermission) { if (!hasPermission) {
final granted = await OverlayPermissionUtil.requestOverlayPermission(); final granted = await OverlayPermissionUtil.requestOverlayPermission(
scene: OverlayPermissionScene.recordingFloatingBar,
);
if (!granted) return; if (!granted) return;
} }

Loading…
Cancel
Save