fix: check permission

This commit is contained in:
J-Jamet
2023-06-18 23:02:09 +02:00
parent 5a883b83a5
commit dfb7e6f3bd
4 changed files with 76 additions and 26 deletions

View File

@@ -67,9 +67,9 @@ import com.kunzisoft.keepass.settings.PreferencesUtil
import com.kunzisoft.keepass.tasks.ActionRunnable
import com.kunzisoft.keepass.tasks.AttachmentFileBinderManager
import com.kunzisoft.keepass.timeout.TimeoutHelper
import com.kunzisoft.keepass.utils.getParcelableExtraCompat
import com.kunzisoft.keepass.utils.UriUtil.openUrl
import com.kunzisoft.keepass.utils.UuidUtil
import com.kunzisoft.keepass.utils.getParcelableExtraCompat
import com.kunzisoft.keepass.view.changeControlColor
import com.kunzisoft.keepass.view.changeTitleColor
import com.kunzisoft.keepass.view.hideByFading
@@ -234,7 +234,7 @@ class EntryActivity : DatabaseLockActivity() {
// Manage entry copy to start notification if allowed (at the first start)
if (savedInstanceState == null) {
// Manage entry to launch copying notification if allowed
ClipboardEntryNotificationService.launchNotificationIfAllowed(this, entryInfo)
ClipboardEntryNotificationService.checkAndLaunchNotification(this, entryInfo)
// Manage entry to populate Magikeyboard and launch keyboard notification if allowed
if (PreferencesUtil.isKeyboardEntrySelectionEnable(this)) {
MagikeyboardService.addEntryAndLaunchNotificationIfAllowed(this, entryInfo)

View File

@@ -19,6 +19,7 @@
*/
package com.kunzisoft.keepass.database
import android.Manifest
import android.app.AlertDialog
import android.content.BroadcastReceiver
import android.content.ComponentName
@@ -31,6 +32,7 @@ import android.content.IntentFilter
import android.content.ServiceConnection
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.IBinder
import android.util.Log
@@ -356,12 +358,17 @@ class DatabaseTaskProvider(
}
private fun start(bundle: Bundle? = null, actionTask: String) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
val contextActivity = activity
if (ContextCompat.checkSelfPermission(context, NOTIFICATION_PERMISSION)
== PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS)
== PackageManager.PERMISSION_GRANTED
) {
startService(bundle, actionTask)
} else if (contextActivity != null
&& shouldShowRequestPermissionRationale(contextActivity, NOTIFICATION_PERMISSION)) {
} else if (contextActivity != null && shouldShowRequestPermissionRationale(
contextActivity,
Manifest.permission.POST_NOTIFICATIONS
)
) {
// it's not the first time, so the user deliberately chooses not to display the notification
startService(bundle, actionTask)
} else {
@@ -374,9 +381,12 @@ class DatabaseTaskProvider(
.setPositiveButton(R.string.ask) { _, _ ->
// Save the temp parameters to ask the permission
tempServiceParameters.add(Pair(bundle, actionTask))
requestPermissionLauncher?.launch(NOTIFICATION_PERMISSION)
requestPermissionLauncher?.launch(Manifest.permission.POST_NOTIFICATIONS)
}.create().show()
}
} else {
startService(bundle, actionTask)
}
}
private fun startService(bundle: Bundle? = null, actionTask: String) {
@@ -781,7 +791,5 @@ class DatabaseTaskProvider(
companion object {
private val TAG = DatabaseTaskProvider::class.java.name
private const val NOTIFICATION_PERMISSION = "android.permission.POST_NOTIFICATIONS"
}
}

View File

@@ -19,11 +19,16 @@
*/
package com.kunzisoft.keepass.services
import android.Manifest
import android.app.Activity
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.util.Log
import android.widget.Toast
import androidx.core.content.ContextCompat
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.model.EntryInfo
import com.kunzisoft.keepass.otp.OtpEntryFields.OTP_TOKEN_FIELD
@@ -148,8 +153,11 @@ class ClipboardEntryNotificationService : LockNotificationService() {
getCopyPendingIntent(fieldToAdd, fieldsToAdd))
}
}
checkNotificationsPermission {
notificationManager?.notify(notificationId, builder.build())
}
}
private fun copyField(fieldToCopy: ClipboardEntryNotificationField, nextFields: MutableList<ClipboardEntryNotificationField>) {
mTimerJob?.cancel()
@@ -207,14 +215,25 @@ class ClipboardEntryNotificationService : LockNotificationService() {
}
} else {
// No timer
checkNotificationsPermission {
notificationManager?.notify(notificationId, builder.build())
}
}
} catch (e: Exception) {
Log.e(TAG, "Clipboard can't be populate", e)
}
}
private fun checkNotificationsPermission(action: () -> Unit) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)
== PackageManager.PERMISSION_GRANTED) {
action.invoke()
} else {
showPermissionError(this)
}
}
override fun onTaskRemoved(rootIntent: Intent?) {
clipboardHelper?.cleanClipboard()
super.onTaskRemoved(rootIntent)
@@ -236,7 +255,29 @@ class ClipboardEntryNotificationService : LockNotificationService() {
const val EXTRA_CLIPBOARD_FIELDS = "EXTRA_CLIPBOARD_FIELDS"
const val ACTION_CLEAN_CLIPBOARD = "ACTION_CLEAN_CLIPBOARD"
fun launchNotificationIfAllowed(context: Context, entry: EntryInfo) {
private fun showPermissionError(context: Context) {
Toast.makeText(context, R.string.warning_copy_permission, Toast.LENGTH_LONG).show()
}
fun checkAndLaunchNotification(
activity: Activity,
entry: EntryInfo
) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (ContextCompat.checkSelfPermission(
activity,
Manifest.permission.POST_NOTIFICATIONS
) == PackageManager.PERMISSION_GRANTED) {
launchNotificationIfAllowed(activity, entry)
} else {
showPermissionError(activity)
}
} else {
launchNotificationIfAllowed(activity, entry)
}
}
private fun launchNotificationIfAllowed(context: Context, entry: EntryInfo) {
val containsUsernameToCopy = entry.username.isNotEmpty()
val containsPasswordToCopy = entry.password.isNotEmpty()

View File

@@ -374,6 +374,7 @@
<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="warning_copy_permission">The notification permission is needed to use the clipboard notification feature.</string>
<string name="later">Later</string>
<string name="ask">Ask</string>
<string name="merge_success">Merge successfully completed</string>