Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 34be33ef authored by Alejandro Nijamkin's avatar Alejandro Nijamkin Committed by Ale Nijamkin
Browse files

Notification toggle section (2/3).

Section for toggling the "show/hide notifications on the lock screen"
seetting.

Fixes: 265436567
Test: unit tests included
Test: manually verified that it toggles the setting
Test: manually verified that the setting in Settings > Display > Lock
screen > Privacy is changed to match

Change-Id: I76956eba5ae77a6f9bdd551f7043fa725f4b0fe9
parent 8ca34bc0
Loading
Loading
Loading
Loading
+57 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
  ~ 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.
  ~
  -->
<com.android.customization.picker.notifications.ui.view.NotificationSectionView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?selectableItemBackground"
    android:clickable="true"
    android:paddingVertical="@dimen/section_top_padding"
    android:paddingHorizontal="@dimen/section_horizontal_padding"
    android:orientation="horizontal"
    android:gravity="center_vertical">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/notifications_section_title"
            style="@style/SectionTitleTextStyle" />

        <TextView
            android:id="@+id/subtitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/SectionSubtitleTextStyle"/>
    </LinearLayout>

    <Switch
        android:id="@+id/switcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
        style="@style/Switch.SettingsLib"
        tools:ignore="UseSwitchCompatOrMaterialXml" />

</com.android.customization.picker.notifications.ui.view.NotificationSectionView>
+26 −0
Original line number Diff line number Diff line
@@ -347,4 +347,30 @@
    [CHAR LIMIT=60].
    -->
    <string name="keyguard_quick_affordance_none_selected">None</string>

    <!--
    Title for a section in a list of sections in the settings app that allows the user to modify
    how notifications behave whilst the device is locked.

    [CHAR LIMIT=32].
    -->
    <string name="notifications_section_title">Notifications</string>

    <!--
    Summary for a setting that lets the user toggle between showing or hiding notifications on their
    device's lock screen. This one is shown when the user is allowing notifications to show on their
    device's lock screen.

    [CHAR LIMIT=64].
    -->
    <string name="show_notifications_on_lock_screen">Show notifications on the lock screen</string>

    <!--
    Summary for a setting that lets the user toggle between showing or hiding notifications on their
    device's lock screen. This one is shown when the user is not allowing notifications to show on
    their device's lock screen.

    [CHAR LIMIT=64].
    -->
    <string name="hide_notifications_on_lock_screen">Hide notifications on the lock screen</string>
</resources>
+16 −1
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.AbstractSavedStateViewModelFactory;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.ViewModelProvider;

@@ -14,6 +15,8 @@ import com.android.customization.model.grid.GridSectionController;
import com.android.customization.model.mode.DarkModeSectionController;
import com.android.customization.model.themedicon.ThemedIconSectionController;
import com.android.customization.model.themedicon.ThemedIconSwitchProvider;
import com.android.customization.picker.notifications.ui.section.NotificationSectionController;
import com.android.customization.picker.notifications.ui.viewmodel.NotificationSectionViewModel;
import com.android.customization.picker.quickaffordance.domain.interactor.KeyguardQuickAffordancePickerInteractor;
import com.android.customization.picker.quickaffordance.ui.section.KeyguardQuickAffordanceSectionController;
import com.android.customization.picker.quickaffordance.ui.viewmodel.KeyguardQuickAffordancePickerViewModel;
@@ -40,14 +43,17 @@ public final class DefaultCustomizationSections implements CustomizationSections
    private final KeyguardQuickAffordancePickerInteractor mKeyguardQuickAffordancePickerInteractor;
    private final KeyguardQuickAffordancePickerViewModel.Factory
            mKeyguardQuickAffordancePickerViewModelFactory;
    private final AbstractSavedStateViewModelFactory mNotificationSectionViewModelFactory;

    public DefaultCustomizationSections(
            KeyguardQuickAffordancePickerInteractor keyguardQuickAffordancePickerInteractor,
            KeyguardQuickAffordancePickerViewModel.Factory
                    keyguardQuickAffordancePickerViewModelFactory) {
                    keyguardQuickAffordancePickerViewModelFactory,
            AbstractSavedStateViewModelFactory notificationSectionViewModelFactory) {
        mKeyguardQuickAffordancePickerInteractor = keyguardQuickAffordancePickerInteractor;
        mKeyguardQuickAffordancePickerViewModelFactory =
                keyguardQuickAffordancePickerViewModelFactory;
        mNotificationSectionViewModelFactory = notificationSectionViewModelFactory;
    }

    @Override
@@ -100,6 +106,15 @@ public final class DefaultCustomizationSections implements CustomizationSections
                                        mKeyguardQuickAffordancePickerViewModelFactory)
                                        .get(KeyguardQuickAffordancePickerViewModel.class),
                                lifecycleOwner));

                // Notifications section.
                sectionControllers.add(
                        new NotificationSectionController(
                                new ViewModelProvider(
                                        activity,
                                        mNotificationSectionViewModelFactory)
                                        .get(NotificationSectionViewModel.class),
                                lifecycleOwner));
                break;

            case HOME_SCREEN:
+31 −6
Original line number Diff line number Diff line
@@ -15,7 +15,6 @@
 */
package com.android.customization.module

import android.app.Activity
import android.app.NotificationManager
import android.content.ComponentName
import android.content.Context
@@ -25,11 +24,15 @@ import android.os.Bundle
import android.os.Handler
import android.os.UserHandle
import android.view.LayoutInflater
import androidx.activity.ComponentActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.android.customization.model.theme.OverlayManagerCompat
import com.android.customization.model.theme.ThemeBundleProvider
import com.android.customization.model.theme.ThemeManager
import com.android.customization.picker.notifications.data.repository.NotificationsRepository
import com.android.customization.picker.notifications.domain.interactor.NotificationsInteractor
import com.android.customization.picker.notifications.ui.viewmodel.NotificationSectionViewModel
import com.android.customization.picker.quickaffordance.data.repository.KeyguardQuickAffordancePickerRepository
import com.android.customization.picker.quickaffordance.domain.interactor.KeyguardQuickAffordancePickerInteractor
import com.android.customization.picker.quickaffordance.domain.interactor.KeyguardQuickAffordanceSnapshotRestorer
@@ -59,7 +62,8 @@ import com.android.wallpaper.picker.LivePreviewFragment
import com.android.wallpaper.picker.PreviewFragment
import com.android.wallpaper.picker.undo.domain.interactor.SnapshotRestorer
import java.util.concurrent.Executors
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope

open class ThemePickerInjector : WallpaperPicker2Injector(), CustomizationInjector {
    private var customizationSections: CustomizationSections? = null
@@ -76,12 +80,18 @@ open class ThemePickerInjector : WallpaperPicker2Injector(), CustomizationInject
        null
    private var clockRegistry: ClockRegistry? = null
    private var pluginManager: PluginManager? = null
    private var notificationsInteractor: NotificationsInteractor? = null

    override fun getCustomizationSections(activity: Activity): CustomizationSections {
    override fun getCustomizationSections(activity: ComponentActivity): CustomizationSections {
        return customizationSections
            ?: DefaultCustomizationSections(
                    getKeyguardQuickAffordancePickerInteractor(activity),
                    getKeyguardQuickAffordancePickerViewModelFactory(activity)
                    getKeyguardQuickAffordancePickerViewModelFactory(activity),
                    NotificationSectionViewModel.newFactory(
                        owner = activity,
                        defaultArgs = null,
                        interactor = getNotificationsInteractor(activity),
                    ),
                )
                .also { customizationSections = it }
    }
@@ -186,7 +196,7 @@ open class ThemePickerInjector : WallpaperPicker2Injector(), CustomizationInject
    ): KeyguardQuickAffordancePickerInteractor {
        val client = getKeyguardQuickAffordancePickerProviderClient(context)
        return KeyguardQuickAffordancePickerInteractor(
            KeyguardQuickAffordancePickerRepository(client, IO),
            KeyguardQuickAffordancePickerRepository(client, Dispatchers.IO),
            client
        ) { getKeyguardQuickAffordanceSnapshotRestorer(context) }
    }
@@ -195,7 +205,7 @@ open class ThemePickerInjector : WallpaperPicker2Injector(), CustomizationInject
        context: Context
    ): CustomizationProviderClient {
        return customizationProviderClient
            ?: CustomizationProviderClientImpl(context, IO).also {
            ?: CustomizationProviderClientImpl(context, Dispatchers.IO).also {
                customizationProviderClient = it
            }
    }
@@ -285,6 +295,21 @@ open class ThemePickerInjector : WallpaperPicker2Injector(), CustomizationInject
        )
    }

    protected fun getNotificationsInteractor(
        context: Context,
    ): NotificationsInteractor {
        return notificationsInteractor
            ?: NotificationsInteractor(
                    repository =
                        NotificationsRepository(
                            scope = GlobalScope,
                            backgroundDispatcher = Dispatchers.IO,
                            secureSettingsRepository = getSecureSettingsRepository(context),
                        )
                )
                .also { notificationsInteractor = it }
    }

    companion object {
        @JvmStatic
        private val KEY_QUICK_AFFORDANCE_SNAPSHOT_RESTORER =
+72 −0
Original line number Diff line number Diff line
/*
 * 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.customization.picker.notifications.data.repository

import android.provider.Settings
import com.android.customization.picker.notifications.shared.model.NotificationSettingsModel
import com.android.wallpaper.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 NotificationsRepository(
    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,
            )

    /** Updates the setting to show or hide notifications on the lock screen. */
    suspend fun setShowNotificationsOnLockScreenEnabled(isEnabled: Boolean) {
        withContext(backgroundDispatcher) {
            secureSettingsRepository.set(
                name = Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
                value = if (isEnabled) 1 else 0,
            )
        }
    }

    suspend fun isShowNotificationsOnLockScreenEnabled(): Boolean {
        return withContext(backgroundDispatcher) {
            secureSettingsRepository.get(
                name = Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
                defaultValue = 0,
            ) == 1
        }
    }
}
Loading