forked from upstream-mirrors/quik
Ensure exact alarm permission before sending scheduled/delayed messages
This commit is contained in:
@@ -19,6 +19,7 @@
|
|||||||
package com.moez.QKSMS.manager
|
package com.moez.QKSMS.manager
|
||||||
|
|
||||||
import android.Manifest
|
import android.Manifest
|
||||||
|
import android.app.AlarmManager
|
||||||
import android.app.NotificationManager
|
import android.app.NotificationManager
|
||||||
import android.app.role.RoleManager
|
import android.app.role.RoleManager
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
@@ -71,6 +72,15 @@ class PermissionManagerImpl @Inject constructor(private val context: Context) :
|
|||||||
return hasPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
return hasPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun hasExactAlarms(): Boolean {
|
||||||
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
val alarmManager = context.getSystemService(AlarmManager::class.java)
|
||||||
|
return alarmManager.canScheduleExactAlarms()
|
||||||
|
}
|
||||||
|
|
||||||
private fun hasPermission(permission: String): Boolean {
|
private fun hasPermission(permission: String): Boolean {
|
||||||
return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED
|
return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,4 +36,6 @@ interface PermissionManager {
|
|||||||
|
|
||||||
fun hasStorage(): Boolean
|
fun hasStorage(): Boolean
|
||||||
|
|
||||||
|
fun hasExactAlarms(): Boolean
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -309,4 +309,12 @@ class Navigator @Inject constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun showExactAlarmsSettings() {
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||||
|
val intent = Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM)
|
||||||
|
.setData(Uri.parse("package:${context.packageName}"))
|
||||||
|
startActivity(intent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -634,23 +634,36 @@ class ComposeViewModel @Inject constructor(
|
|||||||
|
|
||||||
// Send a message when the send button is clicked, and disable editing mode if it's enabled
|
// Send a message when the send button is clicked, and disable editing mode if it's enabled
|
||||||
view.sendIntent
|
view.sendIntent
|
||||||
.filter { permissionManager.isDefaultSms().also { if (!it) view.requestDefaultSms() } }
|
.withLatestFrom(view.textChangedIntent) { _, body -> body.toString() }
|
||||||
.filter { permissionManager.hasSendSms().also { if (!it) view.requestSmsPermission() } }
|
|
||||||
.withLatestFrom(view.textChangedIntent) { _, body -> body }
|
|
||||||
.map { body -> body.toString() }
|
|
||||||
.withLatestFrom(state, attachments, conversation, selectedChips) { body, state, attachments,
|
.withLatestFrom(state, attachments, conversation, selectedChips) { body, state, attachments,
|
||||||
conversation, chips ->
|
conversation, chips ->
|
||||||
val subId = state.subscription?.subscriptionId ?: -1
|
if (!permissionManager.isDefaultSms()) {
|
||||||
val addresses = when (conversation.recipients.isNotEmpty()) {
|
view.requestDefaultSms()
|
||||||
true -> conversation.recipients.map { it.address }
|
return@withLatestFrom
|
||||||
false -> chips.map { chip -> chip.address }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!permissionManager.hasSendSms()) {
|
||||||
|
view.requestSmsPermission()
|
||||||
|
return@withLatestFrom
|
||||||
|
}
|
||||||
|
|
||||||
val delay = when (prefs.sendDelay.get()) {
|
val delay = when (prefs.sendDelay.get()) {
|
||||||
Preferences.SEND_DELAY_SHORT -> 3000
|
Preferences.SEND_DELAY_SHORT -> 3000
|
||||||
Preferences.SEND_DELAY_MEDIUM -> 5000
|
Preferences.SEND_DELAY_MEDIUM -> 5000
|
||||||
Preferences.SEND_DELAY_LONG -> 10000
|
Preferences.SEND_DELAY_LONG -> 10000
|
||||||
else -> 0
|
else -> 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ((delay != 0 || state.scheduled != 0L) && !permissionManager.hasExactAlarms()) {
|
||||||
|
navigator.showExactAlarmsSettings()
|
||||||
|
return@withLatestFrom
|
||||||
|
}
|
||||||
|
|
||||||
|
val subId = state.subscription?.subscriptionId ?: -1
|
||||||
|
val addresses = when (conversation.recipients.isNotEmpty()) {
|
||||||
|
true -> conversation.recipients.map { it.address }
|
||||||
|
false -> chips.map { chip -> chip.address }
|
||||||
|
}
|
||||||
val sendAsGroup = !state.editingMode || state.sendAsGroup
|
val sendAsGroup = !state.editingMode || state.sendAsGroup
|
||||||
|
|
||||||
when {
|
when {
|
||||||
|
|||||||
Reference in New Issue
Block a user