Skip to content

Commit fc2edbb

Browse files
committed
feat(reminders): replace old notification permission request logic
GSoC 2025: Review Reminders The old code requested notification permissions via MyAccount and requests the permission each time a new user logs in, which makes no sense. Notifications are tied to the phone the app is installed on, not an individual user profile. Rather, we should be requesting notification permissions when the user first triggers a sync. This is what this change accomplishes. Deletes all old logic for requesting notification permissions. Deletes `checkNotificationPermission` as it has been moved to Permissions as `shouldRequestNotificationPermissions`. Requests notification permissions the first time the user triggers a sync.
1 parent a6e20e6 commit fc2edbb

File tree

2 files changed

+9
-49
lines changed

2 files changed

+9
-49
lines changed

AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ import com.ichi2.utils.ImportUtils
184184
import com.ichi2.utils.ImportUtils.ImportResult
185185
import com.ichi2.utils.NetworkUtils
186186
import com.ichi2.utils.NetworkUtils.isActiveNetworkMetered
187+
import com.ichi2.utils.Permissions
187188
import com.ichi2.utils.VersionUtils
188189
import com.ichi2.utils.cancelable
189190
import com.ichi2.utils.checkBoxPrompt
@@ -501,11 +502,6 @@ open class DeckPicker :
501502
}
502503
}
503504

504-
private val notificationPermissionLauncher =
505-
registerForActivityResult(ActivityResultContracts.RequestPermission()) {
506-
Timber.i("notification permission: %b", it)
507-
}
508-
509505
// ----------------------------------------------------------------------------
510506
// ANDROID ACTIVITY METHODS
511507
// ----------------------------------------------------------------------------
@@ -2122,7 +2118,12 @@ open class DeckPicker :
21222118
return
21232119
}
21242120

2125-
MyAccount.checkNotificationPermission(this, notificationPermissionLauncher)
2121+
// Request notification permissions from the user if they have not been requested ever before
2122+
if (!Prefs.syncNotifsRequestShown) {
2123+
Permissions.requestNotificationsPermissionIfNeeded(context = this, supportFragmentManager) {
2124+
Prefs.syncNotifsRequestShown = true
2125+
}
2126+
}
21262127

21272128
/** Nested function that makes the connection to
21282129
* the sync server and starts syncing the data */

AnkiDroid/src/main/java/com/ichi2/anki/MyAccount.kt

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
****************************************************************************************/
1414
package com.ichi2.anki
1515

16-
import android.content.Context
17-
import android.content.pm.PackageManager
1816
import android.content.res.Configuration
1917
import android.os.Build
2018
import android.os.Bundle
@@ -27,10 +25,7 @@ import android.widget.Button
2725
import android.widget.ImageView
2826
import android.widget.TextView
2927
import androidx.activity.OnBackPressedCallback
30-
import androidx.activity.result.ActivityResultLauncher
31-
import androidx.activity.result.contract.ActivityResultContracts
3228
import androidx.appcompat.widget.Toolbar
33-
import androidx.core.content.ContextCompat
3429
import androidx.core.view.isVisible
3530
import com.google.android.material.textfield.TextInputEditText
3631
import com.google.android.material.textfield.TextInputLayout
@@ -43,7 +38,6 @@ import com.ichi2.anki.utils.ext.removeFragmentFromContainer
4338
import com.ichi2.anki.utils.ext.showDialogFragment
4439
import com.ichi2.ui.TextInputEditField
4540
import com.ichi2.utils.AdaptionUtil.isUserATestClient
46-
import com.ichi2.utils.Permissions
4741
import net.ankiweb.rsdroid.exceptions.BackendSyncException
4842
import timber.log.Timber
4943

@@ -98,11 +92,6 @@ open class MyAccount : AnkiActivity() {
9892
}
9993
}
10094

101-
private val notificationPermissionLauncher =
102-
registerForActivityResult(ActivityResultContracts.RequestPermission()) {
103-
Timber.i("notification permission: %b", it)
104-
}
105-
10695
override fun onCreate(savedInstanceState: Bundle?) {
10796
if (showedActivityFailedScreen(savedInstanceState)) {
10897
return
@@ -137,7 +126,7 @@ open class MyAccount : AnkiActivity() {
137126
return
138127
}
139128
Timber.i("Attempting auto-login")
140-
handleNewLogin(username, password, notificationPermissionLauncher)
129+
handleNewLogin(username, password)
141130
}
142131

143132
private fun login() {
@@ -146,7 +135,7 @@ open class MyAccount : AnkiActivity() {
146135
inputMethodManager.hideSoftInputFromWindow(username.windowToken, 0)
147136
val username = username.text.toString().trim() // trim spaces, issue 1586
148137
val password = password.text.toString()
149-
handleNewLogin(username, password, notificationPermissionLauncher)
138+
handleNewLogin(username, password)
150139
}
151140

152141
private fun logout() {
@@ -332,7 +321,6 @@ open class MyAccount : AnkiActivity() {
332321
private fun handleNewLogin(
333322
username: String,
334323
password: String,
335-
resultLauncher: ActivityResultLauncher<String>,
336324
) {
337325
val endpoint = getEndpoint()
338326
launchCatchingTask {
@@ -355,7 +343,6 @@ open class MyAccount : AnkiActivity() {
355343
}
356344
updateLogin(username, auth.hkey)
357345
setResult(RESULT_OK)
358-
checkNotificationPermission(this@MyAccount, resultLauncher)
359346
finish()
360347
}
361348
}
@@ -364,33 +351,5 @@ open class MyAccount : AnkiActivity() {
364351
@KotlinCleanup("change to enum")
365352
internal const val STATE_LOG_IN = 1
366353
internal const val STATE_LOGGED_IN = 2
367-
368-
/**
369-
* Displays a system prompt: "Allow AnkiDroid to send you notifications"
370-
*
371-
* [launcher] receives a callback result (`boolean`) unless:
372-
* * Permissions were already granted
373-
* * We are < API 33
374-
*
375-
* Permissions may permanently be denied, in which case [launcher] immediately
376-
* receives a failure result
377-
*/
378-
fun checkNotificationPermission(
379-
context: Context,
380-
launcher: ActivityResultLauncher<String>,
381-
) {
382-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
383-
return
384-
}
385-
val permission = Permissions.postNotification
386-
if (permission != null &&
387-
ContextCompat.checkSelfPermission(
388-
context,
389-
permission,
390-
) != PackageManager.PERMISSION_GRANTED
391-
) {
392-
launcher.launch(permission)
393-
}
394-
}
395354
}
396355
}

0 commit comments

Comments
 (0)