Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request post notification permission #430

Merged
merged 1 commit into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion main/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:installLocation="auto">

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.INTERNET" />
Expand Down Expand Up @@ -70,4 +71,4 @@

</application>

</manifest>
</manifest>
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -35,7 +43,7 @@ class PermissionCaptureScreenFragment : Fragment() {
setViews()

if (ScreenExtractor.isGranted) {
startService()
requestNotificationPermissionOrStartService()
}
}

Expand All @@ -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())
Expand Down
4 changes: 3 additions & 1 deletion main/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
<string name="msg_image_reader_format_unmatched">发现暂时性错误,请再试一次(1) (Image reader format)</string>
<string name="msg_translator_provider_does_not_support_the_ocr_lang">此翻译引擎不支援选择的辨识语言</string>

<string name="msg_grant_posting_notification_permission_rationale">授予通知权限以快速显示/隐藏浮动视窗</string>
<string name="btn_request_again">重新请求</string>
<string name="title_version_history">版本资讯</string>
<string name="title_error">错误</string>
<string name="title_guide">使用说明</string>
Expand Down Expand Up @@ -112,4 +114,4 @@
<string name="pref_summary_on_keep_media_projection_resources">维持相关资源直到重启程式</string>
<string name="pref_summary_off_keep_media_projection_resources">每次使用后释放相关资源</string>
<string name="pref_screenshot">萤幕截图</string>
</resources>
</resources>
4 changes: 3 additions & 1 deletion main/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
<string name="msg_image_reader_format_unmatched">發現暫時性錯誤,請再試一次(1) (Image reader format)</string>
<string name="msg_translator_provider_does_not_support_the_ocr_lang">此翻譯引擎不支援選擇的辨識語言</string>

<string name="msg_grant_posting_notification_permission_rationale">授予通知權限以快速顯示/隱藏浮動視窗</string>
<string name="btn_request_again">重新請求</string>
<string name="title_version_history">版本資訊</string>
<string name="title_error">錯誤</string>
<string name="title_guide">使用說明</string>
Expand Down Expand Up @@ -112,4 +114,4 @@
<string name="pref_summary_on_keep_media_projection_resources">維持相關資源直到重啟程式</string>
<string name="pref_summary_off_keep_media_projection_resources">每次使用後釋放相關資源</string>
<string name="pref_screenshot">螢幕截圖</string>
</resources>
</resources>
4 changes: 3 additions & 1 deletion main/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<string name="pref_screenshot">Screenshot</string>
<string name="pref_summary_on_keep_media_projection_resources">Keep the resources until restart</string>
<string name="pref_summary_off_keep_media_projection_resources">Release the resources after used</string>
<string name="pref_title_keep_media_projection_resources">Keep MediaProjection resources</string>

<string name="menu_setting">Setting</string>
<string name="menu_privacy_policy">Privacy Policy</string>
Expand Down Expand Up @@ -86,6 +87,8 @@
<string name="msg_image_reader_format_unmatched">Found temporary error, please try it again. (Image reader format)</string>
<string name="msg_translator_provider_does_not_support_the_ocr_lang">This translation provider does not support the selected recognition language.</string>
<string name="msg_please_restart_the_app_to_take_effect">Please restart the app to take effect</string>
<string name="msg_grant_posting_notification_permission_rationale">Grant posting notification permission to restore/hide the app easier</string>
<string name="btn_request_again">Request Again</string>

<string name="title_version_history">Version History</string>
<string name="title_error">Error</string>
Expand Down Expand Up @@ -117,5 +120,4 @@
<item>Space</item>
<item>None</item>
</string-array>
<string name="pref_title_keep_media_projection_resources">Keep MediaProjection resources</string>
</resources>
Loading