Commit dfded887 authored by alep's avatar alep

handle permission android well

parent 74d782e7
This diff is collapsed.
// OverlayPermissionModule.kt
package com.whoscall package com.whoscall
import com.facebook.react.bridge.ReactApplicationContext import android.content.Intent
import com.facebook.react.bridge.ReactContextBaseJavaModule import android.net.Uri
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.Promise
import android.os.Build import android.os.Build
import android.provider.Settings import android.provider.Settings
import com.facebook.react.bridge.*
class OverlayPermissionModule(reactContext: ReactApplicationContext) : class OverlayPermissionModule(private val reactContext: ReactApplicationContext)
ReactContextBaseJavaModule(reactContext) { : ReactContextBaseJavaModule(reactContext) {
override fun getName(): String { override fun getName(): String = "OverlayPermission"
return "OverlayPermission"
}
@ReactMethod @ReactMethod
fun hasPermission(promise: Promise) { fun hasOverlayPermission(promise: Promise) {
val canDraw = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { try {
Settings.canDrawOverlays(reactApplicationContext) val allowed = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
} else { Settings.canDrawOverlays(reactContext)
true } else true
} promise.resolve(allowed)
promise.resolve(canDraw) } catch (e: Exception) {
promise.reject("OVERLAY_CHECK_ERROR", e)
} }
}
@ReactMethod
fun openOverlaySettings() {
try {
val ctx = reactContext.currentActivity ?: reactContext
val intent = Intent(
Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:${reactContext.packageName}")
).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
ctx.startActivity(intent)
} catch (_: Exception) {
// best-effort
}
}
} }
...@@ -7,7 +7,10 @@ import com.facebook.react.uimanager.ViewManager ...@@ -7,7 +7,10 @@ import com.facebook.react.uimanager.ViewManager
class StoragePackage : ReactPackage { class StoragePackage : ReactPackage {
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> { override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
return listOf(StorageModule(reactContext)) return listOf(
StorageModule(reactContext),
OverlayPermissionModule(reactContext) // 👈 add this
)
} }
override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> { override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment