forked from maltaisn/another-notes-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issue related to new Alarm&Reminders permission on A14, closes ma…
- Loading branch information
Showing
8 changed files
with
187 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
app/src/main/kotlin/com/maltaisn/notes/ui/reminder/ReminderPermission.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package com.maltaisn.notes.ui.reminder | ||
|
||
import android.Manifest | ||
import android.app.AlarmManager | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.os.Build | ||
import android.provider.Settings | ||
import androidx.activity.result.ActivityResultLauncher | ||
import androidx.activity.result.contract.ActivityResultContracts | ||
import androidx.fragment.app.Fragment | ||
import com.maltaisn.notes.R | ||
import com.maltaisn.notes.ui.common.ConfirmDialog | ||
|
||
class ReminderPermission( | ||
val fragment: Fragment, | ||
val context: Context | ||
) : ConfirmDialog.Callback { | ||
|
||
private var requestPermissionLauncher: ActivityResultLauncher<String>? = null | ||
private var permissionRequested = false | ||
|
||
/** | ||
* Optional listener called if permission was denied or may have been denied. | ||
*/ | ||
var deniedListener: (() -> Unit)? = null | ||
|
||
init { | ||
requestPermissionLauncher = fragment.registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted -> | ||
if (!isGranted) { | ||
if (permissionRequested) { | ||
// Explanation was just shown, user denied permission. | ||
deniedListener?.invoke() | ||
} else { | ||
// Ask user to go to the app's notification settings to grant the permission. | ||
// Only do this if the permission wasn't requested just before. | ||
ConfirmDialog.newInstance( | ||
message = R.string.reminder_alarm_permission, | ||
btnPositive = R.string.action_ok, | ||
).show(fragment.childFragmentManager, | ||
ReminderPermission.REMINDER_PERMISSION_DENIED_DIALOG | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun request() { | ||
if (Build.VERSION.SDK_INT < 34) { | ||
return | ||
} | ||
val alarmManager: AlarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager | ||
when { | ||
alarmManager.canScheduleExactAlarms() -> { | ||
// OK, permission already granted. | ||
} | ||
|
||
fragment.shouldShowRequestPermissionRationale(Manifest.permission.SCHEDULE_EXACT_ALARM) -> { | ||
// Show dialog explaning permission request. | ||
ConfirmDialog.newInstance( | ||
message = R.string.reminder_alarm_permission, | ||
btnPositive = R.string.action_ok, | ||
).show(fragment.childFragmentManager, | ||
ReminderPermission.REMINDER_PERMISSION_DIALOG | ||
) | ||
permissionRequested = true | ||
} | ||
|
||
else -> { | ||
requestPermissionLauncher?.launch(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM) | ||
} | ||
} | ||
} | ||
|
||
override fun onDialogPositiveButtonClicked(tag: String?) { | ||
if (Build.VERSION.SDK_INT < 34) { | ||
return | ||
} | ||
|
||
when (tag) { | ||
ReminderPermission.REMINDER_PERMISSION_DIALOG -> { | ||
// First time asking, can request normally. | ||
requestPermissionLauncher?.launch(Manifest.permission.SCHEDULE_EXACT_ALARM) | ||
} | ||
|
||
ReminderPermission.REMINDER_PERMISSION_DENIED_DIALOG -> { | ||
// Not first time asking, open notification settings window to let user do it. | ||
val settingsIntent = Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM) | ||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) | ||
.putExtra(Settings.EXTRA_APP_PACKAGE, fragment.requireContext().packageName) | ||
fragment.startActivity(settingsIntent) | ||
// Dismiss immediately since at this point we can't know if user did enable notifications or not. | ||
deniedListener?.invoke() | ||
} | ||
} | ||
} | ||
|
||
override fun onDialogNegativeButtonClicked(tag: String?) { | ||
if (tag == REMINDER_PERMISSION_DIALOG || tag == REMINDER_PERMISSION_DENIED_DIALOG) { | ||
// Notification permission was denied, no point in setting reminder. | ||
deniedListener?.invoke() | ||
} | ||
} | ||
|
||
override fun onDialogCancelled(tag: String?) { | ||
onDialogNegativeButtonClicked(tag) | ||
} | ||
|
||
companion object { | ||
private const val REMINDER_PERMISSION_DIALOG = "alarm-permission-dialog" | ||
private const val REMINDER_PERMISSION_DENIED_DIALOG = "alarm-permission-denied-dialog" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters