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

Commit 81d39096 authored by Yining Liu's avatar Yining Liu
Browse files

Implement the preference switch for minimalism on ls settings page

Implement the preference switch for notification minimalism on the
notifications on lock screen settings page. The illustrations and
summaries will also update when switching the preference. This
preference will hide when the main toggle is unset or when the lock
screen notification minimalism feature flag is disabled.

Bug: 367455695
Flag: com.android.server.notification.notification_lock_screen_settings
Test: adb shell settings get secure \
 lock_screen_notification_minimalism <1|0>
Change-Id: Ie6596e518bd1aa2e828587d7d174b57256aec45b
parent bd6561ac
Loading
Loading
Loading
Loading
+121 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
  ~ Copyright (C) 2024 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.
  -->

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:androidprv="http://schemas.android.com/apk/prv/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/notif_ls_style_desc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingVertical="@dimen/settingslib_illustration_padding"
        android:paddingEnd="48dp"
        android:paddingStart="48dp"
        android:layout_gravity="center_horizontal"
        android:gravity="center"
        android:focusable="true"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="?android:attr/textColorSecondary"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:orientation="horizontal">

        <Space
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"/>

        <LinearLayout
            android:id="@+id/button_compact"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <FrameLayout
                android:layout_width="@dimen/contrast_button_total_size"
                android:layout_height="@dimen/contrast_button_total_size"
                android:background="@drawable/accessibility_contrast_button_background">

                <ImageView
                    android:layout_gravity="center"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:contentDescription="@null"
                    android:src="@drawable/ic_contrast_standard"/>
            </FrameLayout>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/contrast_button_text_spacing"
                android:gravity="center_horizontal|top"
                android:ellipsize="end"
                android:singleLine="true"
                android:textSize="@dimen/contrast_button_text_size"
                android:text="@string/lock_screen_notifs_show_compact"
                android:textColor="?androidprv:attr/materialColorOnSurface"/>
        </LinearLayout>

        <Space
            android:layout_width="@dimen/contrast_button_horizontal_spacing"
            android:layout_height="match_parent" />

        <LinearLayout
            android:id="@+id/button_full"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <FrameLayout
                android:layout_width="@dimen/contrast_button_total_size"
                android:layout_height="@dimen/contrast_button_total_size"
                android:background="@drawable/accessibility_contrast_button_background">

                <ImageView
                    android:layout_gravity="center"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:contentDescription="@null"
                    android:src="@drawable/ic_contrast_high"/>
            </FrameLayout>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/contrast_button_text_spacing"
                android:gravity="center_horizontal|top"
                android:ellipsize="end"
                android:singleLine="true"
                android:textSize="@dimen/contrast_button_text_size"
                android:text="@string/lock_screen_notifs_show_full_list"
                android:textColor="?androidprv:attr/materialColorOnSurface"/>
        </LinearLayout>

        <Space
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
    </LinearLayout>

</LinearLayout>
+12 −0
Original line number Diff line number Diff line
@@ -8789,6 +8789,18 @@
    <!-- Configure notifications: Summary for option of showing only new notifications on the lock screen. [CHAR LIMIT=100] -->
    <string name="unseen_notifs_lock_screen_summary">Automatically remove previously viewed notifications from the lock screen</string>
    <!-- Configure notifications: Value for lockscreen notifications: show all notifications [CHAR LIMIT=60] -->
    <string name="lock_screen_notifs_show_full_list">Full list</string>
    <!-- Configure notifications: Summary for lockscreen notifications: show all notifications [CHAR LIMIT=100] -->
    <string name="lock_screen_notifs_full_list_desc">The current default placement is a full shelf and notification stack.</string>
    <!-- Configure notifications: Value for lockscreen notifications: show compact notifications (minimalism on) [CHAR LIMIT=60] -->
    <string name="lock_screen_notifs_show_compact">Compact</string>
    <!-- Configure notifications: Summary for lockscreen notifications: show compact notifications (minimalism on) [CHAR LIMIT=60] -->
    <string name="lock_screen_notifs_compact_desc">New notifications are collapsed into a shelf on your lockscreen.</string>
    <!-- Configure notifications: Title for determining which notifications appear on the lock screen [CHAR LIMIT=60] -->
    <string name="lock_screen_notifs_title">Notifications on lock screen</string>
+19 −0
Original line number Diff line number Diff line
@@ -25,6 +25,25 @@
        android:title="@string/switch_on_text"
        settings:controller="com.android.settings.notification.LockScreenNotificationsGlobalPreferenceController"/>

<!--    TODO: replace res with actual illustrations when we have -->
    <com.android.settingslib.widget.IllustrationPreference
        android:key="compact_illustration"
        settings:searchable="false"
        android:selectable="false"
        settings:dynamicColor="true"/>

    <com.android.settingslib.widget.IllustrationPreference
        android:key="full_list_illustration"
        settings:searchable="false"
        android:selectable="false"
        settings:dynamicColor="true"/>

    <com.android.settingslib.widget.LayoutPreference
        android:key="ls_minimalism"
        android:selectable="false"
        android:layout="@layout/notification_ls_minimalism_selector"
        settings:controller="com.android.settings.notification.lockscreen.MinimalismPreferenceController" />

    <com.android.settingslib.RestrictedSwitchPreference
        android:key="lock_screen_notification_show_sensitive_toggle"
        android:title="@string/lock_screen_notifications_summary_hide"
+196 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.settings.notification.lockscreen;

import static android.provider.Settings.Secure.LOCK_SCREEN_NOTIFICATION_MINIMALISM;
import static android.provider.Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS;

import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceScreen;

import com.android.server.notification.Flags;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.widget.IllustrationPreference;
import com.android.settingslib.widget.LayoutPreference;

import java.util.HashMap;
import java.util.Map;

public class MinimalismPreferenceController
        extends BasePreferenceController
        implements LifecycleEventObserver {

    private static final int LS_SHOW_NOTIF_ON = 1;
    private static final int LS_SHOW_NOTIF_OFF = 0;
    private static final int LS_MINIMALISM_OFF = 0;
    private static final int LS_MINIMALISM_ON = 1;
    private static final String KEY_MINIMALISM_PREFERENCE = "ls_minimalism";
    private static final String KEY_FULL_LIST_ILLUSTRATION = "full_list_illustration";
    private static final String KEY_COMPACT_ILLUSTRATION = "compact_illustration";
    private static final Uri URI_LOCK_SCREEN_NOTIFICATION_MINIMALISM =
            Settings.Secure.getUriFor(LOCK_SCREEN_NOTIFICATION_MINIMALISM);
    private static final Uri URI_LOCK_SCREEN_SHOW_NOTIFICATIONS =
            Settings.Secure.getUriFor(LOCK_SCREEN_SHOW_NOTIFICATIONS);

    @Nullable private LayoutPreference mPreference;
    @Nullable private TextView mDescView;
    private Map<Integer, LinearLayout> mButtons = new HashMap<>();
    private Map<Integer, IllustrationPreference> mIllustrations = new HashMap<>();
    private final Map<Integer, Integer> mDescriptionTexts = Map.ofEntries(
            Map.entry(LS_MINIMALISM_OFF, R.string.lock_screen_notifs_full_list_desc),
            Map.entry(LS_MINIMALISM_ON, R.string.lock_screen_notifs_compact_desc)
    );

    private final ContentResolver mContentResolver;

    final ContentObserver mContentObserver = new ContentObserver(
            new Handler(Looper.getMainLooper())) {
        @Override
        public void onChange(boolean selfChange, @Nullable Uri uri) {
            refreshState(uri);
        }
    };

    public MinimalismPreferenceController(@NonNull Context context, @NonNull String preferenceKey) {
        super(context, preferenceKey);
        mContentResolver = context.getContentResolver();
    }

    @Override
    public void onStateChanged(@NonNull LifecycleOwner lifecycleOwner,
            @NonNull Lifecycle.Event event) {
        if (event == Lifecycle.Event.ON_RESUME) {
            mContentResolver.registerContentObserver(
                    URI_LOCK_SCREEN_NOTIFICATION_MINIMALISM,
                    /* notifyForDescendants= */ false,
                    mContentObserver
            );
            mContentResolver.registerContentObserver(
                    URI_LOCK_SCREEN_SHOW_NOTIFICATIONS,
                    /* notifyForDescendants= */ false,
                    mContentObserver
            );
        } else if (event == Lifecycle.Event.ON_PAUSE) {
            mContentResolver.unregisterContentObserver(mContentObserver);
        }
    }

    @Override
    public int getAvailabilityStatus() {
        if (!Flags.notificationMinimalism()) {
            return CONDITIONALLY_UNAVAILABLE;
        }
        if (!lockScreenShowNotification()) {
            return CONDITIONALLY_UNAVAILABLE;
        }
        return AVAILABLE;
    }

    /**
     * @return Whether showing notifications on the lockscreen is enabled.
     */
    private boolean lockScreenShowNotification() {
        return Settings.Secure.getInt(
                mContext.getContentResolver(),
                Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
                LS_SHOW_NOTIF_OFF
        ) == LS_SHOW_NOTIF_ON;
    }

    @Override
    public void displayPreference(@NonNull PreferenceScreen screen) {
        super.displayPreference(screen);
        mPreference = screen.findPreference(KEY_MINIMALISM_PREFERENCE);
        mDescView = mPreference.findViewById(R.id.notif_ls_style_desc);

        mButtons = Map.ofEntries(
                Map.entry(LS_MINIMALISM_OFF,
                        mPreference.findViewById(R.id.button_full)),
                Map.entry(LS_MINIMALISM_ON,
                        mPreference.findViewById(R.id.button_compact))
        );

        mIllustrations = Map.ofEntries(
                Map.entry(LS_MINIMALISM_OFF,
                        screen.findPreference(KEY_FULL_LIST_ILLUSTRATION)),
                Map.entry(LS_MINIMALISM_ON,
                        screen.findPreference(KEY_COMPACT_ILLUSTRATION))
        );
        mButtons.forEach((value, button) -> button.setOnClickListener(v ->
                Settings.Secure.putInt(
                        mContext.getContentResolver(),
                        Settings.Secure.LOCK_SCREEN_NOTIFICATION_MINIMALISM,
                        value
                )
        ));

        refreshState(URI_LOCK_SCREEN_NOTIFICATION_MINIMALISM);
    }

    private void highlightButton(int currentValue) {
        mButtons.forEach((value, button) -> button.setSelected(currentValue == value));
    }

    private void highlightIllustration(int currentValue) {
        mIllustrations.forEach((value, preference)
                -> preference.setVisible(currentValue == value));
    }

    private void highlightDescription(int value) {
        if (mDescView == null) return;
        Integer descStringId = mDescriptionTexts.get(value);
        if (descStringId != null) {
            mDescView.setText(descStringId);
        }
    }

    private int getCurrentMinimalismValue() {
        return Settings.Secure.getInt(mContext.getContentResolver(),
                LOCK_SCREEN_NOTIFICATION_MINIMALISM, LS_MINIMALISM_ON);
    }

    private void refreshState(@Nullable Uri uri) {
        if (mPreference == null) return;
        if (URI_LOCK_SCREEN_SHOW_NOTIFICATIONS.equals(uri) && !lockScreenShowNotification()) {
            // hide all preferences when showing notifications on lock screen is disabled
            mIllustrations.forEach((value, preference)
                    -> preference.setVisible(false));
            mPreference.setVisible(false);
        } else {
            mPreference.setVisible(isAvailable());
            int currentValue = getCurrentMinimalismValue();
            highlightButton(currentValue);
            highlightIllustration(currentValue);
            highlightDescription(currentValue);
        }
    }
}