fix: ask for notification permission

This commit is contained in:
J-Jamet
2023-06-18 18:12:17 +02:00
parent 9b4b5914d6
commit 82875ff832
7 changed files with 60 additions and 2 deletions

View File

@@ -9,6 +9,8 @@
android:anyDensity="true" />
<uses-permission
android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission
android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission
android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission

View File

@@ -29,11 +29,15 @@ import android.content.Context.BIND_IMPORTANT
import android.content.Intent
import android.content.IntentFilter
import android.content.ServiceConnection
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.os.IBinder
import android.util.Log
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.ActivityCompat.shouldShowRequestPermissionRationale
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
import com.kunzisoft.keepass.R
@@ -340,7 +344,42 @@ class DatabaseTaskProvider(
}
}
private val tempServiceParameters = mutableListOf<Pair<Bundle?, String>>()
private val requestPermissionLauncher = activity?.registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { _ ->
// Whether or not the user has accepted, the service can be started,
// There just won't be any notification if it's not allowed.
tempServiceParameters.removeFirstOrNull()?.let {
startService(it.first, it.second)
}
}
private fun start(bundle: Bundle? = null, actionTask: String) {
val contextActivity = activity
if (ContextCompat.checkSelfPermission(context, NOTIFICATION_PERMISSION)
== PackageManager.PERMISSION_GRANTED) {
startService(bundle, actionTask)
} else if (contextActivity != null
&& shouldShowRequestPermissionRationale(contextActivity, NOTIFICATION_PERMISSION)) {
// it's not the first time, so the user deliberately chooses not to display the notification
startService(bundle, actionTask)
} else {
AlertDialog.Builder(activity)
.setMessage(R.string.warning_database_notification_permission)
.setNegativeButton(R.string.later) { _, _ ->
// Refuses the notification, so start the service
startService(bundle, actionTask)
}
.setPositiveButton(R.string.ask) { _, _ ->
// Save the temp parameters to ask the permission
tempServiceParameters.add(Pair(bundle, actionTask))
requestPermissionLauncher?.launch(NOTIFICATION_PERMISSION)
}.create().show()
}
}
private fun startService(bundle: Bundle? = null, actionTask: String) {
try {
if (bundle != null)
intentDatabaseTask.putExtras(bundle)
@@ -742,5 +781,7 @@ class DatabaseTaskProvider(
companion object {
private val TAG = DatabaseTaskProvider::class.java.name
private const val NOTIFICATION_PERMISSION = "android.permission.POST_NOTIFICATIONS"
}
}

View File

@@ -281,7 +281,14 @@ class AttachmentFileNotificationService: LockNotificationService() {
AttachmentState.CANCELED,
AttachmentState.ERROR -> {
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_DETACH)
notificationManager?.notify(attachmentNotification.notificationId, builder.build())
try {
notificationManager?.notify(
attachmentNotification.notificationId,
builder.build()
)
} catch (e: SecurityException) {
Log.e(TAG, "Unable to notify the attachment state", e)
}
} else -> {
startForeground(attachmentNotification.notificationId, builder.build())
}

View File

@@ -57,10 +57,10 @@ import com.kunzisoft.keepass.timeout.TimeoutHelper
import com.kunzisoft.keepass.utils.DATABASE_START_TASK_ACTION
import com.kunzisoft.keepass.utils.DATABASE_STOP_TASK_ACTION
import com.kunzisoft.keepass.utils.LOCK_ACTION
import com.kunzisoft.keepass.utils.closeDatabase
import com.kunzisoft.keepass.utils.getParcelableExtraCompat
import com.kunzisoft.keepass.utils.getParcelableList
import com.kunzisoft.keepass.utils.putParcelableList
import com.kunzisoft.keepass.utils.closeDatabase
import com.kunzisoft.keepass.viewmodels.FileDatabaseInfo
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel

View File

@@ -613,6 +613,9 @@
<string name="menu_merge_database">Fusionner les données</string>
<string name="enable_keep_screen_on_summary">Maintiens l\'écran allumé lorsque l\'entrée est visionnée</string>
<string name="warning_keyfile_integrity">Le hachage du fichier n\'est pas garanti car Android peut modifier ses données à la volée. Changez l\'extension du fichier en .bin pour une intégrité correcte.</string>
<string name="later">Plus tard</string>
<string name="ask">Demander</string>
<string name="warning_database_notification_permission">L\'autorisation de notification permet d\'afficher l\'état de la base de données et de la verrouiller à l\'aide d\'un bouton facilement accessible.\n\nSi vous n\'activez pas cette permission, la base de données ouverte en arrière-plan ne sera pas visible si une autre application est en premier plan.</string>
<string name="enable_keep_screen_on_title">Maintenir l\'écran allumé</string>
<string name="show_entry_colors_summary">Affiche les couleurs de premier plan et d\'arrière-plan pour une entrée</string>
<string name="warning_database_info_reloaded">Le rechargement de la base de données supprimera les données modifiées localement.</string>

View File

@@ -373,6 +373,9 @@
<string name="warning_database_revoked">Access to the file revoked by the file manager, close the database and reopen it from its location.</string>
<string name="warning_exact_alarm">You have not allowed the app to use an exact alarm. As a result, the features requiring a timer will not be done with an exact time.</string>
<string name="warning_keyfile_integrity">The hash of the file is not guaranteed because Android can change its data on the fly. Change the file extension to .bin for correct integrity.</string>
<string name="warning_database_notification_permission">The notification permission allows you to display the status of the database and lock it with an easily accessible button.\n\nIf you do not activate this permission, the database open in the background will not be visible if another application is in the foreground.</string>
<string name="later">Later</string>
<string name="ask">Ask</string>
<string name="merge_success">Merge successfully completed</string>
<string name="permission">Permission</string>
<string name="version_label">Version %1$s</string>

View File

@@ -309,12 +309,14 @@
<style name="KeepassDXStyle.Light.Dialog" parent="Theme.MaterialComponents.Light.Dialog.Alert.Bridge">
<item name="colorAccent">@color/orange</item>
<item name="android:textColorPrimary">@color/green</item>
<item name="android:background">@color/white_dark</item>
<item name="background">@color/white_dark</item>
<item name="android:windowSoftInputMode">adjustResize</item>
</style>
<style name="KeepassDXStyle.Night.Dialog" parent="Theme.MaterialComponents.Dialog.Alert.Bridge">
<item name="colorAccent">@color/orange</item>
<item name="android:textColorPrimary">@color/green</item>
<item name="android:background">@color/grey_dark</item>
<item name="background">@color/grey_dark</item>
<item name="android:windowSoftInputMode">adjustResize</item>
</style>