Skip to content

Commit

Permalink
Properly handle the POST_NOTIFICATIONS permission
Browse files Browse the repository at this point in the history
This is new in API 33. It is "dangerous" and must be explicitly enabled
by the user.
  • Loading branch information
amberin committed May 5, 2024
1 parent c20cda7 commit 67ccf2f
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 7 deletions.
4 changes: 4 additions & 0 deletions app/src/androidTest/java/com/orgzly/android/OrgzlyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public OrgzlyTest() {
this.grantPermissionRule =
GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
InstrumentationRegistry.getInstrumentation().getUiAutomation().grantRuntimePermission(App.getProcessName(),
Manifest.permission.POST_NOTIFICATIONS);
}
}

@Before
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<!-- Initially added for checking available connections (for sync). -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package com.orgzly.android.reminders
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import com.orgzly.BuildConfig
import com.orgzly.android.App
import com.orgzly.android.AppIntent
import com.orgzly.android.data.DataRepository
import com.orgzly.android.data.logs.AppLogsRepository
import com.orgzly.android.prefs.AppPreferences
import com.orgzly.android.ui.util.userFriendlyPeriod
import com.orgzly.android.util.AppPermissions
import com.orgzly.android.util.LogMajorEvents
import com.orgzly.android.util.LogUtils
import com.orgzly.android.util.async
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import android.os.Build
import android.os.SystemClock
import android.util.Log
import androidx.annotation.RequiresApi
import com.orgzly.android.App
import com.orgzly.android.AppIntent
import com.orgzly.android.data.logs.AppLogsRepository
import com.orgzly.android.prefs.AppPreferences
import com.orgzly.android.ui.util.ActivityUtils
import com.orgzly.android.ui.util.getAlarmManager
import com.orgzly.android.util.AppPermissions
import com.orgzly.android.util.LogMajorEvents
import org.joda.time.DateTime
import javax.inject.Inject
Expand Down Expand Up @@ -75,6 +77,15 @@ class RemindersScheduler @Inject constructor(val context: Application, val logs:
private fun schedule(intent: PendingIntent, inMs: Long, hasTime: Boolean, origin: String) {
val alarmManager = context.getAlarmManager()

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (!AppPermissions.isGrantedOrRequest(
App.getCurrentActivity(), AppPermissions.Usage.POST_NOTIFICATIONS
)
) {
return
}
}

// TODO: Add preferences to control *how* to schedule the alarms
if (hasTime) {
if (AppPreferences.remindersUseAlarmClockForTodReminders(context)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP
getString(R.string.pref_key_ongoing_notification),
getString(R.string.pref_key_ongoing_notification_priority) -> {
if (AppPreferences.newNoteNotification(context)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
AppPermissions.isGrantedOrRequest(
activity, AppPermissions.Usage.POST_NOTIFICATIONS)
}
Notifications.showOngoingNotification(context)
} else {
Notifications.cancelNewNoteNotification(context)
Expand Down Expand Up @@ -303,17 +307,44 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP
context?.sendBroadcast(intent)
}

// Reminders for scheduled notes - reset last run time
getString(R.string.pref_key_use_reminders_for_scheduled_times) ->
// Reminders for scheduled notes
getString(R.string.pref_key_use_reminders_for_scheduled_times) -> {
// Reset last run time
AppPreferences.reminderLastRunForScheduled(context, 0L)
if (AppPreferences.remindersForScheduledEnabled(context)) {
// Ensure notifications permission
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
AppPermissions.isGrantedOrRequest(
activity, AppPermissions.Usage.POST_NOTIFICATIONS)
}
}
}

// Reminders for deadlines - reset last run time
getString(R.string.pref_key_use_reminders_for_deadline_times) ->
// Reminders for deadlines
getString(R.string.pref_key_use_reminders_for_deadline_times) -> {
// Reset last run time
AppPreferences.reminderLastRunForDeadline(context, 0L)
if (AppPreferences.remindersForDeadlineEnabled(context)) {
// Ensure notifications permission
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
AppPermissions.isGrantedOrRequest(
activity, AppPermissions.Usage.POST_NOTIFICATIONS)
}
}
}

// Reminders for events - reset last run time
getString(R.string.pref_key_use_reminders_for_event_times) ->
// Reminders for events
getString(R.string.pref_key_use_reminders_for_event_times) -> {
// Reset last run time
AppPreferences.reminderLastRunForEvents(context, 0L)
if (AppPreferences.remindersForEventsEnabled(context)) {
// Ensure notifications permission
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
AppPermissions.isGrantedOrRequest(
activity, AppPermissions.Usage.POST_NOTIFICATIONS)
}
}
}

// Display images inline enabled - request permission
getString(R.string.pref_key_images_enabled) -> {
Expand All @@ -324,6 +355,16 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP
}
}
}

// Notification on failed sync - request permission
getString(R.string.pref_key_show_sync_notifications) -> {
if (AppPreferences.showSyncNotifications(context)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
AppPermissions.isGrantedOrRequest(
activity, AppPermissions.Usage.POST_NOTIFICATIONS)
}
}
}
}

updateRemindersScreen()
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/com/orgzly/android/util/AppPermissions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ object AppPermissions {
Usage.SYNC_START -> Manifest.permission.WRITE_EXTERNAL_STORAGE
Usage.SAVED_SEARCHES_EXPORT_IMPORT -> Manifest.permission.WRITE_EXTERNAL_STORAGE
Usage.EXTERNAL_FILES_ACCESS -> Manifest.permission.READ_EXTERNAL_STORAGE
Usage.POST_NOTIFICATIONS -> Manifest.permission.POST_NOTIFICATIONS
}
}

Expand All @@ -82,6 +83,7 @@ object AppPermissions {
Usage.SYNC_START -> R.string.permissions_rationale_for_sync_start
Usage.SAVED_SEARCHES_EXPORT_IMPORT -> R.string.storage_permissions_missing
Usage.EXTERNAL_FILES_ACCESS -> R.string.permissions_rationale_for_external_files_access
Usage.POST_NOTIFICATIONS -> R.string.permissions_rationale_for_post_notifications
}
}

Expand All @@ -90,6 +92,7 @@ object AppPermissions {
BOOK_EXPORT,
SYNC_START,
SAVED_SEARCHES_EXPORT_IMPORT,
EXTERNAL_FILES_ACCESS
EXTERNAL_FILES_ACCESS,
POST_NOTIFICATIONS
}
}
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 @@ -277,6 +277,7 @@
<string name="permissions_rationale_for_book_export">Exporting notebook requires storage permission</string>
<string name="permissions_rationale_for_sync_start">Syncing with local repositories requires storage permission</string>
<string name="permissions_rationale_for_external_files_access">Accessing external files requires storage permission</string>
<string name="permissions_rationale_for_post_notifications">Permission is required to post notifications</string>

<string name="cycle_visibility">Fold/Unfold All</string>
<string name="running_database_update">Upgrading database…</string>
Expand Down

0 comments on commit 67ccf2f

Please sign in to comment.