diff --git a/res/layout/notification_ls_minimalism_selector.xml b/res/layout/notification_ls_minimalism_selector.xml new file mode 100644 index 00000000000..d37ad5401a2 --- /dev/null +++ b/res/layout/notification_ls_minimalism_selector.xml @@ -0,0 +1,121 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/res/values/strings.xml b/res/values/strings.xml index 692941d3ec2..0ab4ade6e4b 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -8789,6 +8789,18 @@ Automatically remove previously viewed notifications from the lock screen + + Full list + + + The current default placement is a full shelf and notification stack. + + + Compact + + + New notifications are collapsed into a shelf on your lockscreen. + Notifications on lock screen diff --git a/res/xml/lock_screen_notifications_settings.xml b/res/xml/lock_screen_notifications_settings.xml index 9aaafc6fbb5..b5fb12869e0 100644 --- a/res/xml/lock_screen_notifications_settings.xml +++ b/res/xml/lock_screen_notifications_settings.xml @@ -25,6 +25,25 @@ android:title="@string/switch_on_text" settings:controller="com.android.settings.notification.LockScreenNotificationsGlobalPreferenceController"/> + + + + + + + mButtons = new HashMap<>(); + private Map mIllustrations = new HashMap<>(); + private final Map 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); + } + } +}