From c983c16907312a75d058abab251d34b86e9f24d2 Mon Sep 17 00:00:00 2001 From: Adam Kasprzycki <66315787+santoni0@users.noreply.github.com> Date: Fri, 14 Oct 2022 00:24:00 +0200 Subject: [PATCH] [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 --- app/src/main/AndroidManifest.xml | 2 +- .../edziennik/ui/home/HomeFragment.kt | 7 +++++- .../ui/login/LoginChooserFragment.kt | 10 +++++--- .../utils/managers/PermissionManager.kt | 24 ++++++++++++++++++- 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c5c273b4..4119bf65 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -12,7 +12,7 @@ - + diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/home/HomeFragment.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/home/HomeFragment.kt index 54f94433..635c7b56 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/home/HomeFragment.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/home/HomeFragment.kt @@ -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) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/login/LoginChooserFragment.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/login/LoginChooserFragment.kt index 1bfe0abd..a4367dee 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/login/LoginChooserFragment.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/login/LoginChooserFragment.kt @@ -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, diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/utils/managers/PermissionManager.kt b/app/src/main/java/pl/szczodrzynski/edziennik/utils/managers/PermissionManager.kt index 65430f60..e99f9e3e 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/utils/managers/PermissionManager.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/utils/managers/PermissionManager.kt @@ -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,