Move NotificationsInteractor & related repos to shared.
We need a way to access notifications-related secure settings in new
sysui recommended architecture code. WallpaperPicker already had code
for this (in the form of a NotificationsInteractor,
NotificationsRepository and SecureSettingsRepository), so instead of
having multiple sources of truth for the same type of data, I moved
these to the shared package so both SystemUI and WallpaperPicker can
access them.
I also renamed NotificationsInteractor and NotificationsRepository to
NotificationsSettingsInteractor and NotificationSettingsRepository
respectively, to better reflect the kind of data they handle.
Added the fake repository to a new CustomizationTestUtils lib.
This move also includes a very small behavior change - there was a
circular dependency between Notifications[Settings]Interactor and
NotificationsSnapshotRestorer, which deals with restoring setting when
the "Reset" button is pressed in WallpaperPicker. Since the snapshot
restorer only needs to exist in WallpaperPicker code, I split it from
the interactor and made it collect the settings flow from the interactor
directly (see other CLs in topic).
I tested that this works correctly by running WallpaperPickerGoogle and
doing the following steps:
- open Lock screen customization page
- toggle "Show notifications on the lockscreen"
- lock the device to verify that the correct setting is applied
- unlock the device and press "Reset"
- verify that the toggle state is updated
- lock the device to verify that the old setting is applied
Note: The test associated with NotificationsSettingsRepository is
currently in WallpaperPickerTests as opposed to CustomizationTests, and
it's complicated to move it so it will stay there for now. See
b/315806189.
Bug: 293167744
Test: manual (see steps above) + atest
NotificationSettingsRepositoryTest
Flag: NONE
Change-Id: I4d4bacd16ce070823d61de601b03f0d0ec63e822
diff --git a/packages/SystemUI/Android.bp b/packages/SystemUI/Android.bp
index f10ac1b..cc5dfc6 100644
--- a/packages/SystemUI/Android.bp
+++ b/packages/SystemUI/Android.bp
@@ -277,6 +277,7 @@
"SystemUIPluginLib",
"SystemUISharedLib",
"SystemUICustomizationLib",
+ "SystemUICustomizationTestUtils",
"SystemUI-statsd",
"SettingsLib",
"com_android_systemui_flags_lib",
diff --git a/packages/SystemUI/customization/src/com/android/systemui/shared/notifications/data/repository/NotificationSettingsRepository.kt b/packages/SystemUI/customization/src/com/android/systemui/shared/notifications/data/repository/NotificationSettingsRepository.kt
new file mode 100644
index 0000000..0b7c3f9
--- /dev/null
+++ b/packages/SystemUI/customization/src/com/android/systemui/shared/notifications/data/repository/NotificationSettingsRepository.kt
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.systemui.shared.notifications.data.repository
+
+import android.provider.Settings
+import com.android.systemui.shared.notifications.shared.model.NotificationSettingsModel
+import com.android.systemui.shared.settings.data.repository.SecureSettingsRepository
+import kotlinx.coroutines.CoroutineDispatcher
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.flow.SharedFlow
+import kotlinx.coroutines.flow.SharingStarted
+import kotlinx.coroutines.flow.map
+import kotlinx.coroutines.flow.shareIn
+import kotlinx.coroutines.withContext
+
+/** Provides access to state related to notifications. */
+class NotificationSettingsRepository(
+ scope: CoroutineScope,
+ private val backgroundDispatcher: CoroutineDispatcher,
+ private val secureSettingsRepository: SecureSettingsRepository,
+) {
+ /** The current state of the notification setting. */
+ val settings: SharedFlow<NotificationSettingsModel> =
+ secureSettingsRepository
+ .intSetting(
+ name = Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
+ )
+ .map { lockScreenShowNotificationsInt ->
+ NotificationSettingsModel(
+ isShowNotificationsOnLockScreenEnabled = lockScreenShowNotificationsInt == 1,
+ )
+ }
+ .shareIn(
+ scope = scope,
+ started = SharingStarted.WhileSubscribed(),
+ replay = 1,
+ )
+
+ suspend fun getSettings(): NotificationSettingsModel {
+ return withContext(backgroundDispatcher) {
+ NotificationSettingsModel(
+ isShowNotificationsOnLockScreenEnabled =
+ secureSettingsRepository.get(
+ name = Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
+ defaultValue = 0,
+ ) == 1
+ )
+ }
+ }
+
+ suspend fun setSettings(model: NotificationSettingsModel) {
+ withContext(backgroundDispatcher) {
+ secureSettingsRepository.set(
+ name = Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
+ value = if (model.isShowNotificationsOnLockScreenEnabled) 1 else 0,
+ )
+ }
+ }
+}
diff --git a/packages/SystemUI/customization/src/com/android/systemui/shared/notifications/domain/interactor/NotificationSettingsInteractor.kt b/packages/SystemUI/customization/src/com/android/systemui/shared/notifications/domain/interactor/NotificationSettingsInteractor.kt
new file mode 100644
index 0000000..21f3aca
--- /dev/null
+++ b/packages/SystemUI/customization/src/com/android/systemui/shared/notifications/domain/interactor/NotificationSettingsInteractor.kt
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.systemui.shared.notifications.domain.interactor
+
+import com.android.systemui.shared.notifications.data.repository.NotificationSettingsRepository
+import com.android.systemui.shared.notifications.shared.model.NotificationSettingsModel
+import kotlinx.coroutines.flow.Flow
+
+/** Encapsulates business logic for interacting with notification settings. */
+class NotificationSettingsInteractor(
+ private val repository: NotificationSettingsRepository,
+) {
+ /** The current state of the notification setting. */
+ val settings: Flow<NotificationSettingsModel> = repository.settings
+
+ /** Toggles the setting to show or hide notifications on the lock screen. */
+ suspend fun toggleShowNotificationsOnLockScreenEnabled() {
+ val currentModel = repository.getSettings()
+ setSettings(
+ currentModel.copy(
+ isShowNotificationsOnLockScreenEnabled =
+ !currentModel.isShowNotificationsOnLockScreenEnabled,
+ )
+ )
+ }
+
+ suspend fun setSettings(model: NotificationSettingsModel) {
+ repository.setSettings(model)
+ }
+
+ suspend fun getSettings(): NotificationSettingsModel {
+ return repository.getSettings()
+ }
+}
diff --git a/packages/SystemUI/customization/src/com/android/systemui/shared/notifications/shared/model/NotificationSettingsModel.kt b/packages/SystemUI/customization/src/com/android/systemui/shared/notifications/shared/model/NotificationSettingsModel.kt
new file mode 100644
index 0000000..7e35360
--- /dev/null
+++ b/packages/SystemUI/customization/src/com/android/systemui/shared/notifications/shared/model/NotificationSettingsModel.kt
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package com.android.systemui.shared.notifications.shared.model
+
+/** Models notification settings. */
+data class NotificationSettingsModel(
+ /** Whether notifications are shown on the lock screen. */
+ val isShowNotificationsOnLockScreenEnabled: Boolean = false,
+)
diff --git a/packages/SystemUI/customization/src/com/android/systemui/shared/settings/data/repository/SecureSettingsRepository.kt b/packages/SystemUI/customization/src/com/android/systemui/shared/settings/data/repository/SecureSettingsRepository.kt
new file mode 100644
index 0000000..7ef16a8
--- /dev/null
+++ b/packages/SystemUI/customization/src/com/android/systemui/shared/settings/data/repository/SecureSettingsRepository.kt
@@ -0,0 +1,102 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.systemui.shared.settings.data.repository
+
+import android.content.ContentResolver
+import android.database.ContentObserver
+import android.provider.Settings
+import kotlinx.coroutines.CoroutineDispatcher
+import kotlinx.coroutines.channels.awaitClose
+import kotlinx.coroutines.flow.Flow
+import kotlinx.coroutines.flow.callbackFlow
+import kotlinx.coroutines.flow.flowOn
+import kotlinx.coroutines.flow.map
+import kotlinx.coroutines.withContext
+
+/** Defines interface for classes that can provide access to data from [Settings.Secure]. */
+interface SecureSettingsRepository {
+
+ /** Returns a [Flow] tracking the value of a setting as an [Int]. */
+ fun intSetting(
+ name: String,
+ defaultValue: Int = 0,
+ ): Flow<Int>
+
+ /** Updates the value of the setting with the given name. */
+ suspend fun set(
+ name: String,
+ value: Int,
+ )
+
+ suspend fun get(
+ name: String,
+ defaultValue: Int = 0,
+ ): Int
+}
+
+class SecureSettingsRepositoryImpl(
+ private val contentResolver: ContentResolver,
+ private val backgroundDispatcher: CoroutineDispatcher,
+) : SecureSettingsRepository {
+
+ override fun intSetting(
+ name: String,
+ defaultValue: Int,
+ ): Flow<Int> {
+ return callbackFlow {
+ val observer =
+ object : ContentObserver(null) {
+ override fun onChange(selfChange: Boolean) {
+ trySend(Unit)
+ }
+ }
+
+ contentResolver.registerContentObserver(
+ Settings.Secure.getUriFor(name),
+ /* notifyForDescendants= */ false,
+ observer,
+ )
+ send(Unit)
+
+ awaitClose { contentResolver.unregisterContentObserver(observer) }
+ }
+ .map { Settings.Secure.getInt(contentResolver, name, defaultValue) }
+ // The above work is done on the background thread (which is important for accessing
+ // settings through the content resolver).
+ .flowOn(backgroundDispatcher)
+ }
+
+ override suspend fun set(name: String, value: Int) {
+ withContext(backgroundDispatcher) {
+ Settings.Secure.putInt(
+ contentResolver,
+ name,
+ value,
+ )
+ }
+ }
+
+ override suspend fun get(name: String, defaultValue: Int): Int {
+ return withContext(backgroundDispatcher) {
+ Settings.Secure.getInt(
+ contentResolver,
+ name,
+ defaultValue,
+ )
+ }
+ }
+}
diff --git a/packages/SystemUI/customization/tests/utils/Android.bp b/packages/SystemUI/customization/tests/utils/Android.bp
new file mode 100644
index 0000000..6db1410
--- /dev/null
+++ b/packages/SystemUI/customization/tests/utils/Android.bp
@@ -0,0 +1,33 @@
+// Copyright (C) 2023 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package {
+ // See: http://go/android-license-faq
+ // A large-scale-change added 'default_applicable_licenses' to import
+ // all of the 'license_kinds' from "frameworks_base_packages_SystemUI_license"
+ // to get the below license kinds:
+ // SPDX-license-identifier-Apache-2.0
+ default_applicable_licenses: ["frameworks_base_packages_SystemUI_license"],
+}
+
+java_library {
+ name: "SystemUICustomizationTestUtils",
+ srcs: [
+ "src/**/*.java",
+ "src/**/*.kt",
+ ],
+ static_libs: [
+ "SystemUICustomizationLib",
+ ],
+}
diff --git a/packages/SystemUI/customization/tests/utils/src/com/android/systemui/shared/settings/data/repository/FakeSecureSettingsRepository.kt b/packages/SystemUI/customization/tests/utils/src/com/android/systemui/shared/settings/data/repository/FakeSecureSettingsRepository.kt
new file mode 100644
index 0000000..1c86a07
--- /dev/null
+++ b/packages/SystemUI/customization/tests/utils/src/com/android/systemui/shared/settings/data/repository/FakeSecureSettingsRepository.kt
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.systemui.shared.settings.data.repository
+
+import kotlinx.coroutines.flow.Flow
+import kotlinx.coroutines.flow.MutableStateFlow
+import kotlinx.coroutines.flow.map
+
+class FakeSecureSettingsRepository : SecureSettingsRepository {
+
+ private val settings = MutableStateFlow<Map<String, String>>(mutableMapOf())
+
+ override fun intSetting(name: String, defaultValue: Int): Flow<Int> {
+ return settings.map { it.getOrDefault(name, defaultValue.toString()) }.map { it.toInt() }
+ }
+
+ override suspend fun set(name: String, value: Int) {
+ settings.value = settings.value.toMutableMap().apply { this[name] = value.toString() }
+ }
+
+ override suspend fun get(name: String, defaultValue: Int): Int {
+ return settings.value[name]?.toInt() ?: defaultValue
+ }
+}