Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't show the notification permission request as the first screen #3350

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package au.com.shiftyjelly.pocketcasts.ui

import android.Manifest
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.content.res.Configuration
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
Expand All @@ -21,7 +17,6 @@ import androidx.appcompat.widget.Toolbar
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.platform.ComposeView
import androidx.core.content.ContextCompat
import androidx.core.view.doOnLayout
import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams
Expand Down Expand Up @@ -142,6 +137,7 @@ import au.com.shiftyjelly.pocketcasts.ui.helper.StatusBarColor
import au.com.shiftyjelly.pocketcasts.ui.theme.Theme
import au.com.shiftyjelly.pocketcasts.ui.theme.ThemeColor
import au.com.shiftyjelly.pocketcasts.utils.Network
import au.com.shiftyjelly.pocketcasts.utils.NotificationPermissionHelper
import au.com.shiftyjelly.pocketcasts.utils.featureflag.Feature
import au.com.shiftyjelly.pocketcasts.utils.featureflag.FeatureFlag
import au.com.shiftyjelly.pocketcasts.utils.log.LogBuffer
Expand Down Expand Up @@ -311,43 +307,6 @@ class MainActivity :

private val deepLinkFactory = DeepLinkFactory()

@SuppressLint("WrongConstant") // for custom snackbar duration constant
private fun checkForNotificationPermission(onPermissionGranted: () -> Unit = {}) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
when {
ContextCompat.checkSelfPermission(
this, Manifest.permission.POST_NOTIFICATIONS,
) == PackageManager.PERMISSION_GRANTED -> {
onPermissionGranted()
}
shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS) -> {
if (settings.isNotificationsDisabledMessageShown()) return
Snackbar.make(
findViewById(R.id.root),
getString(LR.string.notifications_blocked_warning),
EXTRA_LONG_SNACKBAR_DURATION_MS,
).setAction(
getString(LR.string.notifications_blocked_warning_snackbar_action)
.uppercase(Locale.getDefault()),
) {
// Responds to click on the action
val intent = Intent(AndroidProviderSettings.ACTION_APPLICATION_DETAILS_SETTINGS)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
val uri: Uri = Uri.fromParts("package", packageName, null)
intent.data = uri
startActivity(intent)
}.show()
settings.setNotificationsDisabledMessageShown(true)
}
else -> {
notificationPermissionLauncher.launch(
Manifest.permission.POST_NOTIFICATIONS,
)
}
}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
Timber.d("Main Activity onCreate")
super.onCreate(savedInstanceState)
Expand All @@ -364,7 +323,6 @@ class MainActivity :
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
checkForNotificationPermission()

binding.bottomNavigation.doOnLayout {
val miniPlayerHeight = miniPlayerHeight
Expand Down Expand Up @@ -1597,7 +1555,33 @@ class MainActivity :
}

override fun checkNotificationPermission(onPermissionGranted: () -> Unit) {
checkForNotificationPermission(onPermissionGranted)
NotificationPermissionHelper.checkForNotificationPermission(
this,
notificationPermissionLauncher,
onShowRequestPermissionRationale = {
if (settings.isNotificationsDisabledMessageShown()) return@checkForNotificationPermission

Snackbar.make(
findViewById(R.id.root),
getString(LR.string.notifications_blocked_warning),
EXTRA_LONG_SNACKBAR_DURATION_MS,
).setAction(
getString(LR.string.notifications_blocked_warning_snackbar_action)
.uppercase(Locale.getDefault()),
) {
// Responds to click on the action
val intent = Intent(AndroidProviderSettings.ACTION_APPLICATION_DETAILS_SETTINGS)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
val uri: Uri = Uri.fromParts("package", packageName, null)
intent.data = uri
startActivity(intent)
}.show()

settings.setNotificationsDisabledMessageShown(true)
},
onPermissionGranted = onPermissionGranted,
onPermissionHandlingNotRequired = {},
)
}

private fun showPlayerBookmarks() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package au.com.shiftyjelly.pocketcasts.podcasts.view.podcast

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.Parcelable
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.core.os.BundleCompat
Expand Down Expand Up @@ -67,6 +70,7 @@ import au.com.shiftyjelly.pocketcasts.ui.helper.FragmentHostListener
import au.com.shiftyjelly.pocketcasts.ui.helper.StatusBarColor
import au.com.shiftyjelly.pocketcasts.ui.images.CoilManager
import au.com.shiftyjelly.pocketcasts.ui.theme.ThemeColor
import au.com.shiftyjelly.pocketcasts.utils.NotificationPermissionHelper
import au.com.shiftyjelly.pocketcasts.utils.extensions.dpToPx
import au.com.shiftyjelly.pocketcasts.utils.featureflag.Feature
import au.com.shiftyjelly.pocketcasts.utils.featureflag.FeatureFlag
Expand All @@ -84,7 +88,9 @@ import au.com.shiftyjelly.pocketcasts.views.helper.UiUtil
import au.com.shiftyjelly.pocketcasts.views.multiselect.MultiSelectBookmarksHelper.NavigationState
import au.com.shiftyjelly.pocketcasts.views.multiselect.MultiSelectHelper
import au.com.shiftyjelly.pocketcasts.views.multiselect.MultiSelectToolbar
import com.google.android.material.snackbar.Snackbar
import dagger.hilt.android.AndroidEntryPoint
import java.util.Locale
import javax.inject.Inject
import kotlin.time.Duration.Companion.seconds
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -166,6 +172,14 @@ class PodcastFragment : BaseFragment(), Toolbar.OnMenuItemClickListener {

private var listState: Parcelable? = null

private val notificationPermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission(),
) { isGranted ->
if (isGranted) {
viewModel.toggleNotifications(requireContext())
}
}

private val onScrollListener = object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {}

Expand Down Expand Up @@ -484,8 +498,29 @@ class PodcastFragment : BaseFragment(), Toolbar.OnMenuItemClickListener {
}

private val onNotificationsClicked: () -> Unit = {
context?.let {
viewModel.toggleNotifications(it)
context?.let { context ->
NotificationPermissionHelper.checkForNotificationPermission(
requireActivity(),
launcher = notificationPermissionLauncher,
onShowRequestPermissionRationale = {
(activity as? FragmentHostListener)?.snackBarView()?.let { snackBarView ->
Snackbar.make(snackBarView, getString(LR.string.notifications_blocked_warning), Snackbar.LENGTH_LONG)
.setAction(
getString(LR.string.notifications_blocked_warning_snackbar_action)
.uppercase(Locale.getDefault()),
) {
// Open app settings for the user to enable permissions
val intent = Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
val uri: Uri = Uri.fromParts("package", requireContext().packageName, null)
intent.data = uri
startActivity(intent)
}.show()
}
},
onPermissionGranted = { viewModel.toggleNotifications(context) },
onPermissionHandlingNotRequired = { viewModel.toggleNotifications(context) },
)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package au.com.shiftyjelly.pocketcasts.utils

import android.Manifest
import android.app.Activity
import android.content.pm.PackageManager
import android.os.Build
import androidx.activity.result.ActivityResultLauncher
import androidx.core.content.ContextCompat

object NotificationPermissionHelper {

fun checkForNotificationPermission(
activity: Activity,
launcher: ActivityResultLauncher<String>,
onShowRequestPermissionRationale: () -> Unit = {},
onPermissionGranted: () -> Unit = {},
onPermissionHandlingNotRequired: () -> Unit = {},
Comment on lines +17 to +18
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you ever anticipate to these two callbacks do different things? I think for a user perspective it doesn't matter whether if they granted the permission or if it was already granted by default.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally I put just one callback, the onPermissionGranted one. But I noticed that in MainActivity we were not doing anything for the case we don't need to require permission when call checkForNotificationPermission, so I decided to do this to avoid any potential issue

) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
when {
ContextCompat.checkSelfPermission(
activity, Manifest.permission.POST_NOTIFICATIONS,
) == PackageManager.PERMISSION_GRANTED -> {
onPermissionGranted()
}

activity.shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS) -> {
onShowRequestPermissionRationale()
}

else -> {
launcher.launch(Manifest.permission.POST_NOTIFICATIONS)
}
}
} else {
onPermissionHandlingNotRequired()
}
}
}
Loading