Browse Source

[feat]键盘插件,提供检查是否有悬浮窗权限、跳转去悬浮窗设置页的API给Flutter

hezihao 7 months ago
parent
commit
65e451b29d

+ 14 - 1
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/KeyboardAndroidPlugin.kt

@@ -7,6 +7,7 @@ import com.atmob.keyboard_android.component.base.interceptor.LoginRouteIntercept
 import com.atmob.keyboard_android.component.base.interceptor.RouteInterceptorManager
 import com.atmob.keyboard_android.component.listener.ComponentUpdateLoggingListener
 import com.atmob.keyboard_android.floating.FloatingButtonService
+import com.atmob.keyboard_android.util.FloatingWindowUtil
 import com.atmob.keyboard_android.util.InputMethodUtil
 import com.atmob.keyboard_android.util.LogUtil
 import io.flutter.embedding.engine.plugins.FlutterPlugin
@@ -58,11 +59,23 @@ class KeyboardAndroidPlugin : FlutterPlugin, MethodCallHandler {
 
             // 开启或关闭悬浮窗
             "enableFloatingWindow" -> {
-                val enable = call.argument<Boolean>("enable") ?: false
+                val enable = call.argument<Boolean>("enable") == true
                 enableFloatingWindow(enable)
                 result.success(null)
             }
 
+            // 跳转到系统的悬浮窗设置页
+            "jumpFloatingWindowSetting" -> {
+                FloatingWindowUtil.jumpFloatingWindowSetting(context)
+                result.success(null)
+            }
+
+            // 是否开启了悬浮窗权限
+            "hasFloatingWindowPermission" -> {
+                val hasPermission = FloatingWindowUtil.hasFloatingWindowPermission(context)
+                result.success(hasPermission)
+            }
+
             // 打开输入法设置
             "openInputMethodSettings" -> {
                 InputMethodUtil.openInputMethodSettings(context)

+ 5 - 10
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/floating/FloatingButtonService.kt

@@ -7,17 +7,16 @@ import android.content.Intent
 import android.graphics.PixelFormat
 import android.os.Build
 import android.os.IBinder
-import android.provider.Settings
 import android.view.Gravity
 import android.view.LayoutInflater
 import android.view.MotionEvent
 import android.view.View
 import android.view.WindowManager
 import android.widget.ImageView
-import androidx.core.net.toUri
 import com.atmob.keyboard_android.R
 import com.atmob.keyboard_android.ext.click
 import com.atmob.keyboard_android.keyboard.InputMethodPickerActivity
+import com.atmob.keyboard_android.util.FloatingWindowUtil
 import kotlin.math.abs
 
 /**
@@ -33,15 +32,11 @@ class FloatingButtonService : Service() {
          * 启动服务
          */
         fun start(context: Context) {
-            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(context)) {
-                val intent = Intent(
-                    Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
-                    "package:${context.packageName}".toUri()
-                )
-                // 重要:在非 Activity 里启动 Activity 需要添加 FLAG_ACTIVITY_NEW_TASK
-                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
-                context.startActivity(intent)
+            // 检查是否有悬浮窗权限,没有则跳转到设置页面
+            if (!FloatingWindowUtil.hasFloatingWindowPermission(context)) {
+                FloatingWindowUtil.jumpFloatingWindowSetting(context)
             } else {
+                // 有权限,开启悬浮窗
                 val serviceIntent = Intent(context, FloatingButtonService::class.java)
                 context.startService(serviceIntent)
             }

+ 39 - 0
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/util/FloatingWindowUtil.kt

@@ -0,0 +1,39 @@
+package com.atmob.keyboard_android.util
+
+import android.content.Context
+import android.content.Intent
+import android.os.Build
+import android.provider.Settings
+import androidx.core.net.toUri
+
+/**
+ * 悬浮窗工具类
+ */
+class FloatingWindowUtil {
+    companion object {
+        /**
+         * 检查是否有悬浮窗权限
+         */
+        fun hasFloatingWindowPermission(context: Context): Boolean {
+            // 6.0及其以上,需要授权
+            return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
+                return Settings.canDrawOverlays(context)
+            } else {
+                return true
+            }
+        }
+
+        /**
+         * 跳转到悬浮窗的权限设置页面
+         */
+        fun jumpFloatingWindowSetting(context: Context) {
+            val intent = Intent(
+                Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
+                "package:${context.packageName}".toUri()
+            )
+            // 重要:在非 Activity 里启动 Activity 需要添加 FLAG_ACTIVITY_NEW_TASK
+            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
+            context.startActivity(intent)
+        }
+    }
+}