From 5d505fb59a5267d8eeb894ae76d9969675de9841 Mon Sep 17 00:00:00 2001 From: Hafiz Date: Sun, 8 Jan 2023 20:36:16 -0500 Subject: [PATCH] Check if notifications are disabled (Android 13+) Check if notifications are disabled (Android 13+), and if so show layout w/button that requests access via dialog --- Habitica/res/layout/activity_task_form.xml | 31 +++++++++++ .../ui/activities/TaskFormActivity.kt | 52 +++++++++++++++++-- 2 files changed, 79 insertions(+), 4 deletions(-) diff --git a/Habitica/res/layout/activity_task_form.xml b/Habitica/res/layout/activity_task_form.xml index c0d324029..7ac9f92ab 100644 --- a/Habitica/res/layout/activity_task_form.xml +++ b/Habitica/res/layout/activity_task_form.xml @@ -209,6 +209,37 @@ android:layout_height="wrap_content" android:text="@string/reminders" style="@style/TaskFormSectionheader"/> + + + + + + + + + + if (granted) { + pushNotificationManager.addPushDeviceUsingStoredToken() + } else { + //If user denies notification settings originally - they must manually enable it through notification settings. + val alert = HabiticaAlertDialog(this) + alert.setTitle(R.string.push_notification_system_settings_title) + alert.setMessage(R.string.push_notification_system_settings_description) + alert.addButton(R.string.settings, true, false) { _, _ -> + val notifSettingIntent: Intent = Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS) + .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + .putExtra(Settings.EXTRA_APP_PACKAGE, applicationContext?.packageName) + startActivity(notifSettingIntent) + } + alert.addButton(R.string.cancel, false) { _, _ -> + alert.dismiss() + } + alert.show() + } + } + private var isCreating = true private var isChallengeTask = false private var usesTaskAttributeStats = false @@ -350,6 +375,11 @@ class TaskFormActivity : BaseActivity() { configureForm() } + override fun onResume() { + checkIfShowNotifLayout() + super.onResume() + } + override fun loadTheme(sharedPreferences: SharedPreferences, forced: Boolean) { super.loadTheme(sharedPreferences, forced) val upperTintColor = @@ -548,6 +578,7 @@ class TaskFormActivity : BaseActivity() { task.checklist?.let { binding.checklistContainer.checklistItems = it } binding.remindersContainer.taskType = taskType task.reminders?.let { binding.remindersContainer.reminders = it } + checkIfShowNotifLayout() } task.attribute?.let { viewModel.selectedAttribute.value = it } @@ -612,8 +643,10 @@ class TaskFormActivity : BaseActivity() { thisTask.setWeeksOfMonth(binding.taskSchedulingControls.weeksOfMonth) if (binding.habitAdjustPositiveStreakView.text?.isNotEmpty() == true) thisTask.streak = binding.habitAdjustPositiveStreakView.text.toString().toIntCatchOverflow() + checkIfShowNotifLayout() } else if (taskType == TaskType.TODO) { thisTask.dueDate = binding.taskSchedulingControls.dueDate + checkIfShowNotifLayout() } else if (taskType == TaskType.REWARD) { thisTask.value = binding.rewardValue.value } @@ -729,6 +762,17 @@ class TaskFormActivity : BaseActivity() { alert.show() } + private fun checkIfShowNotifLayout() { + if (!pushNotificationManager.notificationPermissionEnabled() && Build.VERSION.SDK_INT >= 33) { + binding.notificationsDisabledLayout.visibility = View.VISIBLE + binding.enableNotifsButton.setOnClickListener { + notificationPermissionLauncher.launch(android.Manifest.permission.POST_NOTIFICATIONS) + } + } else { + binding.notificationsDisabledLayout.visibility = View.GONE + } + } + private fun showChallengeDeleteTask() { lifecycleScope.launchCatching { val tasks = taskRepository.getTasksForChallenge(task?.challengeID).firstOrNull()