Skip to content

Commit

Permalink
Enabled push notifications by default (#245)
Browse files Browse the repository at this point in the history
* Enabled push notifications by default

* Updated libraries
  • Loading branch information
nunommts authored Oct 24, 2023
1 parent 1994bb6 commit 247ac99
Show file tree
Hide file tree
Showing 18 changed files with 16 additions and 101 deletions.
5 changes: 3 additions & 2 deletions README-CHANGES.xml
Original file line number Diff line number Diff line change
Expand Up @@ -337,15 +337,16 @@
<c:change date="2023-10-16T00:00:00+00:00" summary="Fixed crash when creating a library card."/>
</c:changes>
</c:release>
<c:release date="2023-10-23T19:27:04+00:00" is-open="true" ticket-system="org.nypl.jira" version="1.7.0">
<c:release date="2023-10-24T09:32:44+00:00" is-open="true" ticket-system="org.nypl.jira" version="1.7.0">
<c:changes>
<c:change date="2023-10-19T00:00:00+00:00" summary="Adjusted feed tabs navigation to keep the Catalog on the top of the screen."/>
<c:change date="2023-10-23T00:00:00+00:00" summary="Use a new Material 3 theme."/>
<c:change date="2023-10-23T19:27:04+00:00" summary="Fix a missing version name.">
<c:change date="2023-10-23T00:00:00+00:00" summary="Fix a missing version name.">
<c:tickets>
<c:ticket id="PP-610"/>
</c:tickets>
</c:change>
<c:change date="2023-10-24T09:32:44+00:00" summary="Enabled push notifications by default."/>
</c:changes>
</c:release>
</c:releases>
Expand Down
2 changes: 1 addition & 1 deletion org.thepalaceproject.android.platform
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,7 @@ class ProfileAccountLoginTask(
this.runDeviceActivation()
this.account.setLoginState(AccountLoggedIn(this.credentials))
notificationTokenHttpCalls.registerFCMTokenForProfileAccount(
account = account,
areNotificationsEnabled = profile.preferences().areNotificationsEnabled
account = account
)
this.steps.finishSuccess(Unit)
}
Expand Down Expand Up @@ -318,8 +317,7 @@ class ProfileAccountLoginTask(
this.runDeviceActivation()
this.account.setLoginState(AccountLoggedIn(this.credentials))
notificationTokenHttpCalls.registerFCMTokenForProfileAccount(
account = account,
areNotificationsEnabled = profile.preferences().areNotificationsEnabled
account = account
)
this.steps.finishSuccess(Unit)
}
Expand Down Expand Up @@ -365,8 +363,7 @@ class ProfileAccountLoginTask(
this.runDeviceActivation()
this.account.setLoginState(AccountLoggedIn(this.credentials))
notificationTokenHttpCalls.registerFCMTokenForProfileAccount(
account = account,
areNotificationsEnabled = profile.preferences().areNotificationsEnabled
account = account
)
return this.steps.finishSuccess(Unit)
}
Expand Down Expand Up @@ -407,8 +404,7 @@ class ProfileAccountLoginTask(
this.runDeviceActivation()
this.account.setLoginState(AccountLoggedIn(this.credentials))
notificationTokenHttpCalls.registerFCMTokenForProfileAccount(
account = account,
areNotificationsEnabled = profile.preferences().areNotificationsEnabled
account = account
)
return this.steps.finishSuccess(Unit)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ class ProfileAccountLogoutTask(
this.debug("running fcm token deletion")

notificationTokenHttpCalls.deleteFCMTokenForProfileAccount(
account = account,
areNotificationsEnabled = profile.preferences().areNotificationsEnabled
account = account
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,6 @@ class MainActivity : AppCompatActivity(R.layout.main_host) {

private fun askForNotificationsPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
val services =
Services.serviceDirectory()
val profiles =
services.requireService(ProfilesControllerType::class.java)

if (!profiles.profileCurrent().preferences().areNotificationsEnabled) {
logger.debug("Notifications are currently disabled, so we won't be asking for permissions")
return
}

ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.POST_NOTIFICATIONS),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,14 @@ class NotificationTokenHTTPCalls(
override fun registerFCMTokenForProfileAccounts(profile: ProfileReadableType) {
profile.accounts().values.forEach { account ->
registerFCMTokenForProfileAccount(
account = account,
areNotificationsEnabled = profile.preferences().areNotificationsEnabled
account = account
)
}
}

override fun registerFCMTokenForProfileAccount(
account: AccountType,
areNotificationsEnabled: Boolean
account: AccountType
) {
if (!areNotificationsEnabled) {
logger.debug("Notifications are currently disabled, so we won't be registering a token")
return
}

firebaseInstance.token
.addOnSuccessListener { token ->
logger.debug("Success fetching FCM Token: {}", token)
Expand Down Expand Up @@ -105,14 +98,8 @@ class NotificationTokenHTTPCalls(
}

override fun deleteFCMTokenForProfileAccount(
account: AccountType,
areNotificationsEnabled: Boolean
account: AccountType
) {
if (!areNotificationsEnabled) {
logger.debug("Notifications are currently disabled, so we won't be deleting the token")
return
}

firebaseInstance.token
.addOnSuccessListener { token ->
logger.debug("Success fetching FCM Token: {}", token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.nypl.simplified.profiles.api.ProfileReadableType
interface NotificationTokenHTTPCallsType {
fun registerFCMTokenForProfileAccounts(profile: ProfileReadableType)

fun registerFCMTokenForProfileAccount(account: AccountType, areNotificationsEnabled: Boolean)
fun registerFCMTokenForProfileAccount(account: AccountType)

fun deleteFCMTokenForProfileAccount(account: AccountType, areNotificationsEnabled: Boolean)
fun deleteFCMTokenForProfileAccount(account: AccountType)
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ data class ProfilePreferences(

val isManualLCPPassphraseEnabled: Boolean = false,

/** @return `true` if the push notifications are enabled. */

val areNotificationsEnabled: Boolean = false,

/** @return `true` if the debug settings should be visible. */

val showDebugSettings: Boolean = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,6 @@ object ProfileDescriptionJSON {
val isManualLCPPassphraseEnabled =
JSONParserUtilities.getBooleanDefault(objectNode, "isManualLCPPassphraseEnabled", false)

val areNotificationsEnabled =
JSONParserUtilities.getBooleanDefault(objectNode, "areNotificationsEnabled", false)

val mostRecentAccount =
JSONParserUtilities.getStringOrNull(objectNode, "mostRecentAccount")
?.let { AccountID(UUID.fromString(it)) }
Expand All @@ -224,7 +221,6 @@ object ProfileDescriptionJSON {
showDebugSettings = showDebugSettings,
playbackRates = playbackRates,
sleepTimers = sleepTimers,
areNotificationsEnabled = areNotificationsEnabled,
isManualLCPPassphraseEnabled = isManualLCPPassphraseEnabled
)
}
Expand Down Expand Up @@ -261,9 +257,6 @@ object ProfileDescriptionJSON {
val isManualLCPPassphraseEnabled =
JSONParserUtilities.getBooleanDefault(objectNode, "isManualLCPPassphraseEnabled", false)

val areNotificationsEnabled =
JSONParserUtilities.getBooleanDefault(objectNode, "areNotificationsEnabled", false)

val mostRecentAccount =
JSONParserUtilities.getStringOrNull(objectNode, "mostRecentAccount")
?.let { AccountID(UUID.fromString(it)) }
Expand All @@ -277,7 +270,6 @@ object ProfileDescriptionJSON {
hasSeenLibrarySelectionScreen = true,
playbackRates = playbackRates,
sleepTimers = sleepTimers,
areNotificationsEnabled = areNotificationsEnabled,
isManualLCPPassphraseEnabled = isManualLCPPassphraseEnabled
)
}
Expand Down Expand Up @@ -338,9 +330,6 @@ object ProfileDescriptionJSON {
val isManualLCPPassphraseEnabled =
JSONParserUtilities.getBooleanDefault(preferencesNode, "isManualLCPPassphraseEnabled", false)

val areNotificationsEnabled =
JSONParserUtilities.getBooleanDefault(preferencesNode, "areNotificationsEnabled", false)

val preferences =
ProfilePreferences(
dateOfBirth = this.someOrNull(dateOfBirth),
Expand All @@ -350,7 +339,6 @@ object ProfileDescriptionJSON {
hasSeenLibrarySelectionScreen = true,
playbackRates = playbackRates,
sleepTimers = sleepTimers,
areNotificationsEnabled = areNotificationsEnabled,
isManualLCPPassphraseEnabled = isManualLCPPassphraseEnabled
)

Expand Down Expand Up @@ -496,7 +484,6 @@ object ProfileDescriptionJSON {
): ObjectNode {
val output = objectMapper.createObjectNode()
output.put("showTestingLibraries", preferences.showTestingLibraries)
output.put("areNotificationsEnabled", preferences.areNotificationsEnabled)
output.put("isManualLCPPassphraseEnabled", preferences.isManualLCPPassphraseEnabled)
output.put("hasSeenLibrarySelectionScreen", preferences.hasSeenLibrarySelectionScreen)
output.put("showDebugSettings", preferences.showDebugSettings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,6 @@ object ProfilesDatabases {
hasSeenLibrarySelectionScreen = false,
playbackRates = hashMapOf(),
sleepTimers = hashMapOf(),
areNotificationsEnabled = false,
isManualLCPPassphraseEnabled = false
),
attributes = ProfileAttributes(sortedMapOf())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,6 @@ abstract class ProfileAccountLoginTaskContract {
this.server = MockWebServer()
this.server.start()

val preferences = Mockito.mock(ProfilePreferences::class.java)
Mockito.`when`(this.profile.preferences()).thenReturn(preferences)
Mockito.`when`(preferences.areNotificationsEnabled).thenReturn(true)

this.profileWithoutDRM = """
{
"simplified:authorization_identifier": "6120696828384",
Expand Down Expand Up @@ -724,7 +720,7 @@ abstract class ProfileAccountLoginTaskContract {
assertEquals(1, this.server.requestCount)

Mockito.verify(tokenHttp, Mockito.times(1))
.registerFCMTokenForProfileAccount(account, true)
.registerFCMTokenForProfileAccount(account)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,6 @@ abstract class ProfileAccountLogoutTaskContract {

this.server = MockWebServer()
this.server.start()

val preferences = Mockito.mock(ProfilePreferences::class.java)
Mockito.`when`(this.profile.preferences()).thenReturn(preferences)
Mockito.`when`(preferences.areNotificationsEnabled).thenReturn(true)
}

@AfterEach
Expand Down Expand Up @@ -288,7 +284,7 @@ abstract class ProfileAccountLogoutTaskContract {
this.bookRegistry.books().values.all { it.status is BookStatus.Loaned.LoanedNotDownloaded }
)

Mockito.verify(tokenHttp, Mockito.times(1)).deleteFCMTokenForProfileAccount(account, true)
Mockito.verify(tokenHttp, Mockito.times(1)).deleteFCMTokenForProfileAccount(account)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ class ProfileDescriptionJSONTest {
mostRecentAccount = AccountID.generate(),
playbackRates = hashMapOf(),
sleepTimers = hashMapOf(),
areNotificationsEnabled = false,
isManualLCPPassphraseEnabled = false
),
attributes = ProfileAttributes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class MockProfile(
mostRecentAccount = this.accounts.firstKey(),
playbackRates = hashMapOf(),
sleepTimers = hashMapOf(),
areNotificationsEnabled = false,
isManualLCPPassphraseEnabled = false
),
attributes = ProfileAttributes(sortedMapOf())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ class SettingsDebugFragment : Fragment(R.layout.settings_debug) {
private lateinit var syncAccountsButton: Button
private lateinit var enableOpenEBooksQA: Button
private lateinit var toolbar: PalaceToolbar
private lateinit var areNotificationsEnabled: SwitchCompat
private lateinit var isManualLCPPassphraseEnabled: SwitchCompat

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -99,8 +98,6 @@ class SettingsDebugFragment : Fragment(R.layout.settings_debug) {
view.findViewById(R.id.settingsVersionDevSeenLibrarySelectionScreen)
this.isManualLCPPassphraseEnabled =
view.findViewById(R.id.settingsVersionDevIsManualLCPPassphraseEnabled)
this.areNotificationsEnabled =
view.findViewById(R.id.settingsVersionDevAreNotificationsEnabled)
this.cardCreatorFakeLocation =
view.findViewById(R.id.settingsVersionDevCardCreatorLocationSwitch)
this.showOnlySupportedBooks =
Expand Down Expand Up @@ -141,8 +138,6 @@ class SettingsDebugFragment : Fragment(R.layout.settings_debug) {
this.viewModel.cardCreatorFakeLocation
this.isManualLCPPassphraseEnabled.isChecked =
this.viewModel.isManualLCPPassphraseEnabled
this.areNotificationsEnabled.isChecked =
this.viewModel.areNotificationsEnabled
this.showOnlySupportedBooks.isChecked =
this.viewModel.showOnlySupportedBooks
this.crashlyticsId.text =
Expand Down Expand Up @@ -246,9 +241,6 @@ class SettingsDebugFragment : Fragment(R.layout.settings_debug) {
this.isManualLCPPassphraseEnabled.setOnCheckedChangeListener { _, isChecked ->
this.viewModel.isManualLCPPassphraseEnabled = isChecked
}
this.areNotificationsEnabled.setOnCheckedChangeListener { _, isChecked ->
this.viewModel.areNotificationsEnabled = isChecked
}

/*
* Configure the custom OPDS button.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,6 @@ class SettingsDebugViewModel(application: Application) : AndroidViewModel(applic
}
}

var areNotificationsEnabled: Boolean
get() =
this.profilesController
.profileCurrent()
.preferences()
.areNotificationsEnabled
set(value) {
this.profilesController.profileUpdate { description ->
description.copy(preferences = description.preferences.copy(areNotificationsEnabled = value))
}
}

var isBootFailureEnabled: Boolean
get() =
BootFailureTesting.isBootFailureEnabled(getApplication())
Expand Down
9 changes: 0 additions & 9 deletions simplified-ui-settings/src/main/res/layout/settings_debug.xml
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,6 @@
android:enabled="true"
android:text="@string/settingsDevEnableManualLCPPassphrase" />

<androidx.appcompat.widget.SwitchCompat
android:id="@+id/settingsVersionDevAreNotificationsEnabled"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:checked="false"
android:enabled="true"
android:text="@string/settingsDevEnablePushNotifications" />

<androidx.appcompat.widget.SwitchCompat
android:id="@+id/settingsVersionDevProductionLibrariesSwitch"
android:layout_width="match_parent"
Expand Down
1 change: 0 additions & 1 deletion simplified-ui-settings/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
<string name="settingsDevDrmSupport">DRM Support</string>
<string name="settingsDevEnableHiddenLibraries">Enable Hidden Libraries</string>
<string name="settingsDevEnableManualLCPPassphrase">Enable Manual LCP Passphrase</string>
<string name="settingsDevEnablePushNotifications">Enable Push Notifications</string>
<string name="settingsDevEnableTimeTracking">Enable Time Tracking</string>
<string name="settingsDevFailNextStartup">Cause the next application startup to fail</string>
<string name="settingsDevForgetAllAnnouncements">Forget All Announcements</string>
Expand Down

0 comments on commit 247ac99

Please sign in to comment.