Skip to content

Commit

Permalink
Merge pull request #252 from amberin/prompt-earlier-for-alarm-permission
Browse files Browse the repository at this point in the history
 Ensure the "alarms & reminders" permission is granted before syncing
  • Loading branch information
amberin authored May 19, 2024
2 parents bc854ac + 39f361f commit d44c17a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/src/main/java/com/orgzly/android/sync/SyncState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ data class SyncState(val type: Type, val message: String? = null, val current: I
FAILED_NO_CONNECTION,
FAILED_NO_STORAGE_PERMISSION,
FAILED_NO_BOOKS_FOUND,
FAILED_NO_ALARMS_PERMISSION,
FAILED_EXCEPTION
}

Expand All @@ -33,6 +34,7 @@ data class SyncState(val type: Type, val message: String? = null, val current: I
Type.FAILED_NO_CONNECTION,
Type.FAILED_NO_STORAGE_PERMISSION,
Type.FAILED_NO_BOOKS_FOUND,
Type.FAILED_NO_ALARMS_PERMISSION,
Type.FAILED_EXCEPTION ->
true
else ->
Expand Down Expand Up @@ -83,6 +85,7 @@ data class SyncState(val type: Type, val message: String? = null, val current: I
Type.FAILED_NO_REPOS -> getString(R.string.no_repos)
Type.FAILED_NO_CONNECTION -> getString(R.string.no_connection)
Type.FAILED_NO_STORAGE_PERMISSION -> getString(R.string.storage_permissions_missing)
Type.FAILED_NO_ALARMS_PERMISSION -> getString(R.string.alarms_permissions_missing)
Type.FAILED_NO_BOOKS_FOUND -> getString(R.string.no_books)
Type.FAILED_EXCEPTION -> message
}
Expand Down
30 changes: 30 additions & 0 deletions app/src/main/java/com/orgzly/android/sync/SyncWorker.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.orgzly.android.sync

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.Settings
import androidx.work.CoroutineWorker
import androidx.work.ForegroundInfo
import androidx.work.WorkerParameters
Expand All @@ -15,6 +19,7 @@ import com.orgzly.android.prefs.AppPreferences
import com.orgzly.android.reminders.RemindersScheduler
import com.orgzly.android.repos.*
import com.orgzly.android.ui.notifications.SyncNotifications
import com.orgzly.android.ui.util.getAlarmManager
import com.orgzly.android.ui.util.haveNetworkConnection
import com.orgzly.android.util.AppPermissions
import com.orgzly.android.util.LogMajorEvents
Expand Down Expand Up @@ -162,6 +167,31 @@ class SyncWorker(val context: Context, val params: WorkerParameters) :
}
}

/* Make sure we have permission to set alarms & reminders,
* since this typically happens when new books are parsed.
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (!context.getAlarmManager().canScheduleExactAlarms()) {
if (
AppPreferences.remindersForDeadlineEnabled(context) ||
AppPreferences.remindersForScheduledEnabled(context) ||
AppPreferences.remindersForEventsEnabled(context)
) {
if (App.getCurrentActivity() != null) {
val uri = Uri.parse("package:" + BuildConfig.APPLICATION_ID)
App.getCurrentActivity().startActivity(
Intent(
Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM,
uri
)
)
}
return SyncState.getInstance((SyncState.Type.FAILED_NO_ALARMS_PERMISSION))
}
}
}


return null
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/orgzly/android/ui/CommonFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ open class CommonFragment : Fragment() {
FAILED_NO_REPOS,
FAILED_NO_CONNECTION,
FAILED_NO_STORAGE_PERMISSION,
FAILED_NO_ALARMS_PERMISSION,
FAILED_NO_BOOKS_FOUND,
FAILED_EXCEPTION ->
progressIndicator.visibility = View.GONE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ class SyncFragment : Fragment() {
SyncState.Type.FAILED_NO_REPOS,
SyncState.Type.FAILED_NO_CONNECTION,
SyncState.Type.FAILED_NO_STORAGE_PERMISSION,
SyncState.Type.FAILED_NO_ALARMS_PERMISSION,
SyncState.Type.FAILED_NO_BOOKS_FOUND,
SyncState.Type.FAILED_EXCEPTION -> {
setAnimation(false)
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@
</plurals>

<string name="storage_permissions_missing">No storage permission</string>
<string name="alarms_permissions_missing">No permission to set reminders</string>
<string name="file_does_not_exist">File does not exist: %s</string>

<string name="import_from">Import from “%1$s”?</string>
Expand Down

0 comments on commit d44c17a

Please sign in to comment.