From 960fe1d47b10aa24dc2a31c72d466e4c76e5700a Mon Sep 17 00:00:00 2001 From: Johnson Sun Date: Sat, 27 Jan 2024 17:00:49 +0800 Subject: [PATCH] Add POST_NOTIFICATIONS permission to enable background toasts Error message (with `adb logcat *:E`): E NotificationService: Suppressing toast from package com.example.whispertoinput by user request. --- android/app/src/main/AndroidManifest.xml | 1 + .../example/whispertoinput/MainActivity.kt | 47 +++++++++++-------- .../recorder/RecorderManager.kt | 5 +- android/app/src/main/res/values/strings.xml | 3 +- 4 files changed, 35 insertions(+), 21 deletions(-) diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index fb1c509..e3f2eae 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -24,6 +24,7 @@ + by preferencesDataStore(name = "settings") val ENDPOINT = stringPreferencesKey("endpoint") val LANGUAGE_CODE = stringPreferencesKey("language-code") @@ -92,19 +94,25 @@ class MainActivity : AppCompatActivity() { // Checks whether permissions are granted. If not, automatically make a request. private fun checkPermissions() { - if (ContextCompat.checkSelfPermission( - this, - Manifest.permission.RECORD_AUDIO - ) == PackageManager.PERMISSION_DENIED - ) { - // Shows a popup for permission request. - // If the permission has been previously (hard-)denied, the popup will not show. - // onRequestPermissionsResult will be called in either case. - ActivityCompat.requestPermissions( - this, - RecorderManager.requiredPermissions(), - MICROPHONE_PERMISSION_REQUEST_CODE - ) + val permission_and_code = arrayOf( + Pair(Manifest.permission.RECORD_AUDIO, MICROPHONE_PERMISSION_REQUEST_CODE), + Pair(Manifest.permission.POST_NOTIFICATIONS, NOTIFICATION_PERMISSION_REQUEST_CODE), + ) + for ((permission, code) in permission_and_code) { + if (ContextCompat.checkSelfPermission( + this, + permission + ) == PackageManager.PERMISSION_DENIED + ) { + // Shows a popup for permission request. + // If the permission has been previously (hard-)denied, the popup will not show. + // onRequestPermissionsResult will be called in either case. + ActivityCompat.requestPermissions( + this, + arrayOf(permission), + code + ) + } } } @@ -115,20 +123,21 @@ class MainActivity : AppCompatActivity() { grantResults: IntArray ) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) + var msg: String; // Only handles requests marked with the unique code. - if (requestCode != MICROPHONE_PERMISSION_REQUEST_CODE) { + if (requestCode == MICROPHONE_PERMISSION_REQUEST_CODE) { + msg = getString(R.string.mic_permission_required) + } else if (requestCode == NOTIFICATION_PERMISSION_REQUEST_CODE) { + msg = getString(R.string.notification_permission_required) + } else { return } // All permissions should be granted. for (result in grantResults) { if (result != PackageManager.PERMISSION_GRANTED) { - Toast.makeText( - this, - getString(R.string.mic_permission_required), - Toast.LENGTH_SHORT - ).show() + Toast.makeText(this, msg, Toast.LENGTH_SHORT).show() return } } diff --git a/android/app/src/main/java/com/example/whispertoinput/recorder/RecorderManager.kt b/android/app/src/main/java/com/example/whispertoinput/recorder/RecorderManager.kt index dca42b8..c758405 100644 --- a/android/app/src/main/java/com/example/whispertoinput/recorder/RecorderManager.kt +++ b/android/app/src/main/java/com/example/whispertoinput/recorder/RecorderManager.kt @@ -38,7 +38,10 @@ private const val MEDIA_RECORDER_CONSTRUCTOR_DEPRECATION_API_LEVEL = 31 class RecorderManager(context: Context) { companion object { - fun requiredPermissions() = arrayOf(Manifest.permission.RECORD_AUDIO) + fun requiredPermissions() = arrayOf( + Manifest.permission.RECORD_AUDIO, + Manifest.permission.POST_NOTIFICATIONS, + ) } private var recorder: MediaRecorder? = null diff --git a/android/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xml index 5d6e868..be95259 100644 --- a/android/app/src/main/res/values/strings.xml +++ b/android/app/src/main/res/values/strings.xml @@ -12,7 +12,8 @@ Start speech-to-text. Start transcribing. Grant Microphone Permission - Whisper to Input requires microphone usage to work. + Whisper to Input requires microphone permission to work. + Whisper to Input requires notification permission to work. Microphone Successfully set! Backspace Button