Skip to content

Commit

Permalink
Add POST_NOTIFICATIONS permission to enable background toasts
Browse files Browse the repository at this point in the history
Error message (with `adb logcat *:E`):

    E NotificationService: Suppressing toast from package com.example.whispertoinput by user request.
  • Loading branch information
j3soon committed Jan 27, 2024
1 parent 0a5465a commit 960fe1d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 21 deletions.
1 change: 1 addition & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ import kotlinx.coroutines.withContext
import java.io.File
import java.io.IOException

// 200 and 201 are an arbitrary values, as long as they do not conflict with each other
private const val MICROPHONE_PERMISSION_REQUEST_CODE = 200
private const val NOTIFICATION_PERMISSION_REQUEST_CODE = 201
val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")
val ENDPOINT = stringPreferencesKey("endpoint")
val LANGUAGE_CODE = stringPreferencesKey("language-code")
Expand Down Expand Up @@ -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
)
}
}
}

Expand All @@ -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
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
<string name="start_speech_to_text">Start speech-to-text.</string>
<string name="start_transcribing">Start transcribing.</string>
<string name="grant_microphone_permission">Grant Microphone Permission</string>
<string name="mic_permission_required">Whisper to Input requires microphone usage to work.</string>
<string name="mic_permission_required">Whisper to Input requires microphone permission to work.</string>
<string name="notification_permission_required">Whisper to Input requires notification permission to work.</string>
<string name="microphone">Microphone</string>
<string name="successfully_set">Successfully set!</string>
<string name="backspace_button_hint">Backspace Button</string>
Expand Down

0 comments on commit 960fe1d

Please sign in to comment.