Skip to content

Commit

Permalink
Fix notification permission
Browse files Browse the repository at this point in the history
Add alert to request permission on setting change
  • Loading branch information
udenr committed Apr 8, 2024
1 parent fbbf9ae commit 377bbeb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@
*/
package org.secuso.privacyfriendlypaindiary.activities

import android.Manifest
import android.app.AlertDialog
import android.content.SharedPreferences
import android.content.SharedPreferences.OnSharedPreferenceChangeListener
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.preference.Preference.OnPreferenceClickListener
import android.preference.PreferenceFragment
import android.preference.PreferenceManager
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
Expand Down Expand Up @@ -56,6 +61,10 @@ class SettingsActivity : BaseActivity() {
}

class GeneralPreferenceFragment : PreferenceFragment(), OnSharedPreferenceChangeListener {
companion object {
const val REQUEST_CODE_POST_NOTIFICATION = 3
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initPreferences()
Expand Down Expand Up @@ -87,6 +96,7 @@ class SettingsActivity : BaseActivity() {
}

override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String?) {
checkPermission()
if (key == KEY_PREF_REMINDER) {
val enabled = sharedPreferences.getBoolean(KEY_PREF_REMINDER, false)
if (enabled) {
Expand All @@ -103,6 +113,25 @@ class SettingsActivity : BaseActivity() {
}
}

private fun checkPermission(): Boolean {
//Check for notification permission and exact alarm permission
if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU
&& ContextCompat.checkSelfPermission(this.context, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED)
) {
AlertDialog.Builder(this.context)
.setMessage(R.string.dialog_need_permission_for_notifications)
.setPositiveButton(android.R.string.ok) { _, _ ->
ActivityCompat.requestPermissions(this.activity, arrayOf(Manifest.permission.POST_NOTIFICATIONS), REQUEST_CODE_POST_NOTIFICATION)
}
.setTitle(R.string.dialog_need_permission_for_notifications_title)
.setCancelable(true)
.create()
.show()
return false
}
return true
}

private fun resetApp() {
runBlocking {
launch(Dispatchers.IO) {
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@
<string name="export_success">PDF wurde im Dokumenten-Ordner gespeichert.</string>
<string name="export_failure">Unbekannter Fehler: PDF konnte nicht gespeichert werden.</string>
<string name="share_caution">Seien Sie vorsichtig, mit wem Sie Ihre medizinischen Informationen teilen.</string>
<string name="dialog_need_permission_for_notifications">Für diese Funktion ist die Berechtigung zum Senden von Benachrichtigungen erforderlich.</string>
<string name="dialog_need_permission_for_notifications_title">Benachrichtigungen aktivieren</string>


</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,7 @@
<string name="export_success">PDF was saved to documents folder.</string>
<string name="export_failure">Unknown Error: PDF could not be saved.</string>
<string name="share_caution">Be careful who you share your medical information with.</string>
<string name="dialog_need_permission_for_notifications">Permission to send notifications is needed for this feature.</string>
<string name="dialog_need_permission_for_notifications_title">Enable notifications</string>

</resources>

0 comments on commit 377bbeb

Please sign in to comment.