[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.VIBRATE" />
<uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <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 --> <!-- 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" /> <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() private val job: Job = Job()
override val coroutineContext: CoroutineContext override val coroutineContext: CoroutineContext
get() = job + Dispatchers.Main get() = job + Dispatchers.Main
private val manager
get() = app.permissionManager
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
activity = (getActivity() as MainActivity?) ?: return null activity = (getActivity() as MainActivity?) ?: return null
context ?: return null context ?: return null
@ -96,6 +97,10 @@ class HomeFragment : Fragment(), CoroutineScope {
if (!isAdded) if (!isAdded)
return return
if (!manager.isNotificationPermissionGranted) {
manager.requestNotificationsPermission(activity, 0, false){}
}
activity.bottomSheet.prependItems( activity.bottomSheet.prependItems(
BottomSheetPrimaryItem(true) BottomSheetPrimaryItem(true)
.withTitle(R.string.menu_add_remove_cards) .withTitle(R.string.menu_add_remove_cards)

View File

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

View File

@ -36,7 +36,13 @@ class PermissionManager(val app: App) : CoroutineScope {
app.checkSelfPermission(name) == PackageManager.PERMISSION_GRANTED app.checkSelfPermission(name) == PackageManager.PERMISSION_GRANTED
else else
true 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) { private fun openPermissionSettings(activity: AppCompatActivity) {
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
val uri = Uri.fromParts("package", app.packageName, null) val uri = Uri.fromParts("package", app.packageName, null)
@ -80,6 +86,10 @@ class PermissionManager(val app: App) : CoroutineScope {
.show() .show()
} }
result.hasPermanentDenied() -> { result.hasPermanentDenied() -> {
if (!isRequired) {
onSuccess()
return@launch
}
MaterialAlertDialogBuilder(activity) MaterialAlertDialogBuilder(activity)
.setTitle(R.string.permissions_required) .setTitle(R.string.permissions_required)
.setMessage(R.string.permissions_denied) .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( fun requestStoragePermission(
activity: AppCompatActivity, activity: AppCompatActivity,