From 8998a64cb95ca08a8c606e34bc48c4f72f5b1d5d Mon Sep 17 00:00:00 2001 From: firemaples Date: Sun, 4 Feb 2024 00:11:31 +0900 Subject: [PATCH] Request post notification permission --- main/src/main/AndroidManifest.xml | 3 +- .../PermissionCaptureScreenFragment.kt | 57 ++++++++++++++++++- main/src/main/res/values-zh-rCN/strings.xml | 4 +- main/src/main/res/values-zh-rTW/strings.xml | 4 +- main/src/main/res/values/strings.xml | 4 +- 5 files changed, 67 insertions(+), 5 deletions(-) diff --git a/main/src/main/AndroidManifest.xml b/main/src/main/AndroidManifest.xml index 8282b884..e18ade1f 100644 --- a/main/src/main/AndroidManifest.xml +++ b/main/src/main/AndroidManifest.xml @@ -3,6 +3,7 @@ xmlns:tools="http://schemas.android.com/tools" android:installLocation="auto"> + @@ -70,4 +71,4 @@ - \ No newline at end of file + diff --git a/main/src/main/java/tw/firemaples/onscreenocr/pages/launch/permissions/PermissionCaptureScreenFragment.kt b/main/src/main/java/tw/firemaples/onscreenocr/pages/launch/permissions/PermissionCaptureScreenFragment.kt index 9a1a4637..cc63b4a9 100644 --- a/main/src/main/java/tw/firemaples/onscreenocr/pages/launch/permissions/PermissionCaptureScreenFragment.kt +++ b/main/src/main/java/tw/firemaples/onscreenocr/pages/launch/permissions/PermissionCaptureScreenFragment.kt @@ -1,14 +1,22 @@ package tw.firemaples.onscreenocr.pages.launch.permissions +import android.Manifest import android.app.Activity import android.content.Context +import android.content.pm.PackageManager import android.media.projection.MediaProjectionManager +import android.os.Build import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.activity.result.contract.ActivityResultContracts +import androidx.annotation.RequiresApi +import androidx.appcompat.app.AlertDialog +import androidx.core.app.ActivityCompat +import androidx.core.content.ContextCompat import androidx.fragment.app.Fragment +import tw.firemaples.onscreenocr.R import tw.firemaples.onscreenocr.databinding.PermissionCaptureScreenFragmentBinding import tw.firemaples.onscreenocr.floatings.ViewHolderService import tw.firemaples.onscreenocr.pages.setting.SettingManager @@ -35,7 +43,7 @@ class PermissionCaptureScreenFragment : Fragment() { setViews() if (ScreenExtractor.isGranted) { - startService() + requestNotificationPermissionOrStartService() } } @@ -59,9 +67,56 @@ class PermissionCaptureScreenFragment : Fragment() { intent = intent, keepMediaProjection = SettingManager.keepMediaProjectionResources, ) + requestNotificationPermissionOrStartService() + } + } + + private fun requestNotificationPermissionOrStartService() { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { + startService() + } else { + requestNotificationPermission { startService() } } + } + + private val notificationResultLauncher = + registerForActivityResult(ActivityResultContracts.RequestPermission()) { _ -> + startService() + } + + @RequiresApi(Build.VERSION_CODES.TIRAMISU) + private fun requestNotificationPermission( + onResult: () -> Unit, + ) { + val permission = Manifest.permission.POST_NOTIFICATIONS + when { + ContextCompat.checkSelfPermission( + requireContext(), permission + ) == PackageManager.PERMISSION_GRANTED -> { + onResult.invoke() + } + + ActivityCompat.shouldShowRequestPermissionRationale(requireActivity(), permission) -> { + AlertDialog.Builder(requireContext()) + .setMessage(getString(R.string.msg_grant_posting_notification_permission_rationale)) + .setPositiveButton(getString(R.string.btn_request_again)) { _, _ -> + notificationResultLauncher.launch(permission) + } + .setNegativeButton(android.R.string.cancel) { _, _ -> + onResult.invoke() + } + .show() + } + + else -> { + // You can directly ask for the permission. + // The registered ActivityResultCallback gets the result of this request. + notificationResultLauncher.launch(permission) + } + } + } private fun startService() { ViewHolderService.showViews(requireActivity()) diff --git a/main/src/main/res/values-zh-rCN/strings.xml b/main/src/main/res/values-zh-rCN/strings.xml index 4465ca4d..cd11c22d 100644 --- a/main/src/main/res/values-zh-rCN/strings.xml +++ b/main/src/main/res/values-zh-rCN/strings.xml @@ -66,6 +66,8 @@ 发现暂时性错误,请再试一次(1) (Image reader format) 此翻译引擎不支援选择的辨识语言 + 授予通知权限以快速显示/隐藏浮动视窗 + 重新请求 版本资讯 错误 使用说明 @@ -112,4 +114,4 @@ 维持相关资源直到重启程式 每次使用后释放相关资源 萤幕截图 - \ No newline at end of file + diff --git a/main/src/main/res/values-zh-rTW/strings.xml b/main/src/main/res/values-zh-rTW/strings.xml index ede4603a..9e504b5a 100644 --- a/main/src/main/res/values-zh-rTW/strings.xml +++ b/main/src/main/res/values-zh-rTW/strings.xml @@ -66,6 +66,8 @@ 發現暫時性錯誤,請再試一次(1) (Image reader format) 此翻譯引擎不支援選擇的辨識語言 + 授予通知權限以快速顯示/隱藏浮動視窗 + 重新請求 版本資訊 錯誤 使用說明 @@ -112,4 +114,4 @@ 維持相關資源直到重啟程式 每次使用後釋放相關資源 螢幕截圖 - \ No newline at end of file + diff --git a/main/src/main/res/values/strings.xml b/main/src/main/res/values/strings.xml index 723ce1e2..7dfac8a1 100644 --- a/main/src/main/res/values/strings.xml +++ b/main/src/main/res/values/strings.xml @@ -50,6 +50,7 @@ Screenshot Keep the resources until restart Release the resources after used + Keep MediaProjection resources Setting Privacy Policy @@ -86,6 +87,8 @@ Found temporary error, please try it again. (Image reader format) This translation provider does not support the selected recognition language. Please restart the app to take effect + Grant posting notification permission to restore/hide the app easier + Request Again Version History Error @@ -117,5 +120,4 @@ Space None - Keep MediaProjection resources