1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.notification; 18 19 import static android.provider.Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS; 20 import static android.provider.Settings.Secure.LOCK_SCREEN_SHOW_ONLY_UNSEEN_NOTIFICATIONS; 21 22 import android.content.Context; 23 import android.provider.Settings; 24 25 import androidx.annotation.VisibleForTesting; 26 27 import com.android.server.notification.Flags; 28 import com.android.settings.R; 29 import com.android.settings.core.TogglePreferenceController; 30 31 public class ShowOnlyUnseenNotificationsOnLockscreenPreferenceController 32 extends TogglePreferenceController { 33 34 // This is the default value for phones, before notification minimalism, this setting is 35 // unavailable to phones, we use this value to hide the toggle on phones. 36 private static final int UNSET_UNAVAILABLE = 0; 37 @VisibleForTesting 38 static final int ON = 1; 39 @VisibleForTesting 40 static final int OFF = 2; 41 ShowOnlyUnseenNotificationsOnLockscreenPreferenceController( Context context, String preferenceKey)42 public ShowOnlyUnseenNotificationsOnLockscreenPreferenceController( 43 Context context, 44 String preferenceKey) { 45 super(context, preferenceKey); 46 } 47 48 @Override isChecked()49 public boolean isChecked() { 50 return Settings.Secure.getInt(mContext.getContentResolver(), 51 LOCK_SCREEN_SHOW_ONLY_UNSEEN_NOTIFICATIONS, UNSET_UNAVAILABLE) == ON; 52 } 53 54 @Override setChecked(boolean isChecked)55 public boolean setChecked(boolean isChecked) { 56 return Settings.Secure.putInt(mContext.getContentResolver(), 57 LOCK_SCREEN_SHOW_ONLY_UNSEEN_NOTIFICATIONS, isChecked ? ON : OFF); 58 } 59 60 @Override getAvailabilityStatus()61 public int getAvailabilityStatus() { 62 // Hide when the notifications on lock screen page flag is enabled. 63 if (Flags.notificationLockScreenSettings()) { 64 return CONDITIONALLY_UNAVAILABLE; 65 } 66 if (Flags.notificationMinimalism()) { 67 if (!isNotifOnLockScreenEnabled()) { 68 return DISABLED_DEPENDENT_SETTING; 69 } 70 // We want to show the switch when the lock screen notification minimalism flag is on. 71 return AVAILABLE; 72 } 73 int setting = Settings.Secure.getInt(mContext.getContentResolver(), 74 LOCK_SCREEN_SHOW_ONLY_UNSEEN_NOTIFICATIONS, UNSET_UNAVAILABLE); 75 if (setting == UNSET_UNAVAILABLE) { 76 return CONDITIONALLY_UNAVAILABLE; 77 } else { 78 return AVAILABLE; 79 } 80 } 81 82 @Override getSliceHighlightMenuRes()83 public int getSliceHighlightMenuRes() { 84 return R.string.menu_key_notifications; 85 } 86 isNotifOnLockScreenEnabled()87 private boolean isNotifOnLockScreenEnabled() { 88 return Settings.Secure.getInt(mContext.getContentResolver(), 89 LOCK_SCREEN_SHOW_NOTIFICATIONS, 0) == 1; 90 } 91 } 92