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

Ensure the "alarms & reminders" permission is granted before syncing #252

Merged
Show file tree
Hide file tree
Changes from all 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
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