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

Commit 8b94618d authored by Yining Liu's avatar Yining Liu Committed by Android (Google) Code Review
Browse files

Merge "Notification on lockscreen settings: add WhatToShowController" into main

parents c0e9a84e 50577a6c
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -46,7 +46,8 @@

    <PreferenceCategory
        android:key="lockscreen_notification_what_to_show"
        android:title="@string/lockscreen_notification_what_to_show_title">
        android:title="@string/lockscreen_notification_what_to_show_title"
        settings:controller="com.android.settings.notification.LockScreenWhatToShowController">

        <SwitchPreferenceCompat
            android:key="lock_screen_notification_show_seen_toggle"
+101 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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;

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 androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;

import com.android.settings.widget.PreferenceCategoryController;

public class LockScreenWhatToShowController extends PreferenceCategoryController implements
        LifecycleEventObserver {

    @Nullable
    private Preference mPreference;
    private final ContentResolver mContentResolver;

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

    @Override
    public void updateState(@Nullable Preference preference) {
        super.updateState(preference);
        if (preference == null) return;
        preference.setVisible(isAvailable());
    }

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

    @Override
    public void displayPreference(@NonNull PreferenceScreen screen) {
        super.displayPreference(screen);
        mPreference = screen.findPreference(getPreferenceKey());
    }

    @Override
    public int getAvailabilityStatus() {
        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,
                LockScreenNotificationsGlobalPreferenceController.OFF
        ) == LockScreenNotificationsGlobalPreferenceController.ON;
    }

    @Override
    public void onStateChanged(@NonNull LifecycleOwner lifecycleOwner,
            @NonNull Lifecycle.Event event) {
        if (event == Lifecycle.Event.ON_RESUME) {
            mContentResolver.registerContentObserver(
                    Settings.Secure.getUriFor(Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS),
                    /* notifyForDescendants= */ false, mContentObserver);
        } else if (event == Lifecycle.Event.ON_PAUSE) {
            mContentResolver.unregisterContentObserver(mContentObserver);
        }
    }
}
+100 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import android.content.Context;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings;

import androidx.annotation.Nullable;
import androidx.preference.Preference;
import androidx.test.core.app.ApplicationProvider;

import com.android.server.notification.Flags;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;

@RunWith(RobolectricTestRunner.class)
@EnableFlags(Flags.FLAG_NOTIFICATION_LOCK_SCREEN_SETTINGS)
public class LockScreenWhatToShowControllerTest {

    @Rule
    public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();

    private static final String PREFERENCE_KEY = "lockscreen_notification_what_to_show";

    private LockScreenWhatToShowController mController;
    private Context mContext;
    @Nullable
    private Preference mPreference;

    @Before
    public void setUp() {
        mContext = ApplicationProvider.getApplicationContext();
        MockitoAnnotations.initMocks(this);
        mController = new LockScreenWhatToShowController(
                RuntimeEnvironment.application,
                PREFERENCE_KEY);
        mPreference = new Preference(mContext);
        mPreference.setKey(PREFERENCE_KEY);
    }

    @Test
    public void updateState_preferenceVisibleWhenSettingIsOn() {
        // Before: the show LOCK_SCREEN_SHOW_NOTIFICATIONS setting is on
        Settings.Secure.putInt(
                mContext.getContentResolver(),
                Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
                LockScreenNotificationsGlobalPreferenceController.ON
        );

        // When: update state
        mController.updateState(mPreference);

        // Then: the preference is visible
        assertNotNull(mPreference);
        assertTrue(mPreference.isVisible());
    }

    @Test
    public void updateState_preferenceInvisibleWhenSettingIsOff() {
        // Before: the show LOCK_SCREEN_SHOW_NOTIFICATIONS setting is off
        Settings.Secure.putInt(
                mContext.getContentResolver(),
                Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
                LockScreenNotificationsGlobalPreferenceController.OFF
        );

        // When: update state
        mController.updateState(mPreference);

        // Then: the preference is not visible
        assertNotNull(mPreference);
        assertFalse(mPreference.isVisible());
    }
}