[UI] Request notifications permission on API >= 33. (#143)

* Added requesting notifications permission on Android 13 devices

* Move permission check to PermissionManager

* Request permission on home screen
This commit is contained in:
Adam Kasprzycki 2022-10-14 00:24:00 +02:00 committed by GitHub
parent 132729bbd9
commit c983c16907
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 6 deletions

View File

@ -12,7 +12,7 @@
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<!-- PowerPermission uses minSdk 21, it's safe to override as it is used only in >= 23 -->
<uses-sdk tools:overrideLibrary="com.qifan.powerpermission.coroutines, com.qifan.powerpermission.core" />

View File

@ -81,7 +81,8 @@ class HomeFragment : Fragment(), CoroutineScope {
private val job: Job = Job()
override val coroutineContext: CoroutineContext
get() = job + Dispatchers.Main
private val manager
get() = app.permissionManager
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
activity = (getActivity() as MainActivity?) ?: return null
context ?: return null
@ -96,6 +97,10 @@ class HomeFragment : Fragment(), CoroutineScope {
if (!isAdded)
return
if (!manager.isNotificationPermissionGranted) {
manager.requestNotificationsPermission(activity, 0, false){}
}
activity.bottomSheet.prependItems(
BottomSheetPrimaryItem(true)
.withTitle(R.string.menu_add_remove_cards)

View File

@ -23,8 +23,8 @@ import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.*
import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.data.api.*
import pl.szczodrzynski.edziennik.App
import pl.szczodrzynski.edziennik.R
import pl.szczodrzynski.edziennik.databinding.LoginChooserFragmentBinding
import pl.szczodrzynski.edziennik.ext.*
import pl.szczodrzynski.edziennik.ui.dialogs.sync.RegisterUnavailableDialog
@ -51,7 +51,8 @@ class LoginChooserFragment : Fragment(), CoroutineScope {
private val job: Job = Job()
override val coroutineContext: CoroutineContext
get() = job + Dispatchers.Main
private val manager
get() = app.permissionManager
// local/private variables go here
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
@ -67,6 +68,9 @@ class LoginChooserFragment : Fragment(), CoroutineScope {
if (!isAdded) return
val adapter = LoginChooserAdapter(activity, this::onLoginModeClicked)
if (!manager.isNotificationPermissionGranted) {
manager.requestNotificationsPermission(activity, 0, false){}
}
b.versionText.setText(
R.string.login_chooser_version_format,

View File

@ -36,7 +36,13 @@ class PermissionManager(val app: App) : CoroutineScope {
app.checkSelfPermission(name) == PackageManager.PERMISSION_GRANTED
else
true
val isNotificationPermissionGranted by lazy {
if (Build.VERSION.SDK_INT >= 33) {
app.checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED
} else {
true
}
}
private fun openPermissionSettings(activity: AppCompatActivity) {
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
val uri = Uri.fromParts("package", app.packageName, null)
@ -80,6 +86,10 @@ class PermissionManager(val app: App) : CoroutineScope {
.show()
}
result.hasPermanentDenied() -> {
if (!isRequired) {
onSuccess()
return@launch
}
MaterialAlertDialogBuilder(activity)
.setTitle(R.string.permissions_required)
.setMessage(R.string.permissions_denied)
@ -92,6 +102,18 @@ class PermissionManager(val app: App) : CoroutineScope {
}
}
}
fun requestNotificationsPermission(
activity: AppCompatActivity,
@StringRes permissionMessage: Int,
isRequired: Boolean = false,
onSuccess: suspend CoroutineScope.() -> Unit
) = requestPermission(
activity,
permissionMessage,
isRequired,
Manifest.permission.POST_NOTIFICATIONS,
onSuccess
)
fun requestStoragePermission(
activity: AppCompatActivity,