summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Shuming Hao <shuminghao@google.com> 2024-05-23 11:59:36 -0700
committer Shuming Hao <shuminghao@google.com> 2024-06-12 15:43:14 -0700
commitfd5265be02883ec6261818fc95d20ab023cf22b5 (patch)
tree0df092cb9ae5e28e09ab76167956832b1e164545
parenteb8cb7fe46b6e5f523305f0ae8df57494a635b3a (diff)
Add close button on notifications
This CL adds close buttons on each notification and notification group parents. Bug: 338456215 Test: manual Change-Id: Ie2531d16189cd39abfed13f43164390e74fee2d4 (cherry picked from commit 696cff2be9262b367bffe3471c43ea4da0555d10)
-rw-r--r--core/java/android/app/Notification.java10
-rw-r--r--core/java/com/android/internal/widget/NotificationCloseButton.java98
-rw-r--r--core/res/res/drawable/notification_close_button_icon.xml28
-rw-r--r--core/res/res/layout/notification_close_button.xml30
-rw-r--r--core/res/res/layout/notification_template_header.xml23
-rw-r--r--core/res/res/layout/notification_template_material_base.xml34
-rw-r--r--core/res/res/values/dimens.xml3
-rw-r--r--core/res/res/values/symbols.xml5
8 files changed, 220 insertions, 11 deletions
diff --git a/core/java/android/app/Notification.java b/core/java/android/app/Notification.java
index fc3bb0288d67..a55d29335f5d 100644
--- a/core/java/android/app/Notification.java
+++ b/core/java/android/app/Notification.java
@@ -6081,6 +6081,7 @@ public class Notification implements Parcelable
bindProfileBadge(contentView, p);
bindAlertedIcon(contentView, p);
bindExpandButton(contentView, p);
+ bindCloseButton(contentView, p);
mN.mUsesStandardHeader = true;
}
@@ -6102,6 +6103,15 @@ public class Notification implements Parcelable
contentView.setInt(R.id.expand_button, "setHighlightPillColor", pillColor);
}
+ private void bindCloseButton(RemoteViews contentView, StandardTemplateParams p) {
+ // set default colors
+ int bgColor = getBackgroundColor(p);
+ int backgroundColor = Colors.flattenAlpha(getColors(p).getProtectionColor(), bgColor);
+ int foregroundColor = Colors.flattenAlpha(getPrimaryTextColor(p), backgroundColor);
+ contentView.setInt(R.id.close_button, "setForegroundColor", foregroundColor);
+ contentView.setInt(R.id.close_button, "setBackgroundColor", backgroundColor);
+ }
+
private void bindHeaderChronometerAndTime(RemoteViews contentView,
StandardTemplateParams p, boolean hasTextToLeft) {
if (!p.mHideTime && showsTimeOrChronometer()) {
diff --git a/core/java/com/android/internal/widget/NotificationCloseButton.java b/core/java/com/android/internal/widget/NotificationCloseButton.java
new file mode 100644
index 000000000000..22be87b2b1ed
--- /dev/null
+++ b/core/java/com/android/internal/widget/NotificationCloseButton.java
@@ -0,0 +1,98 @@
+/*
+ * 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.internal.widget;
+
+import android.annotation.ColorInt;
+import android.annotation.Nullable;
+import android.content.Context;
+import android.content.res.ColorStateList;
+import android.util.AttributeSet;
+import android.view.RemotableViewMethod;
+import android.view.accessibility.AccessibilityNodeInfo;
+import android.widget.Button;
+import android.widget.ImageView;
+import android.widget.RemoteViews;
+
+import com.android.internal.R;
+
+/**
+ * A close button in a notification
+ */
+@RemoteViews.RemoteView
+public class NotificationCloseButton extends ImageView {
+
+ @ColorInt private int mBackgroundColor;
+ @ColorInt private int mForegroundColor;
+
+ public NotificationCloseButton(Context context) {
+ this(context, null, 0, 0);
+ }
+
+ public NotificationCloseButton(Context context, @Nullable AttributeSet attrs) {
+ this(context, attrs, 0, 0);
+ }
+
+ public NotificationCloseButton(Context context, @Nullable AttributeSet attrs,
+ int defStyleAttr) {
+ this(context, attrs, defStyleAttr, 0);
+ }
+
+ public NotificationCloseButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr,
+ int defStyleRes) {
+ super(context, attrs, defStyleAttr, defStyleRes);
+ }
+
+ @Override
+ protected void onFinishInflate() {
+ super.onFinishInflate();
+ setContentDescription(mContext.getText(R.string.close_button_text));
+ }
+
+ @Override
+ public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
+ super.onInitializeAccessibilityNodeInfo(info);
+ info.setClassName(Button.class.getName());
+ }
+
+
+ private void updateColors() {
+ if (mBackgroundColor != 0) {
+ this.setBackgroundTintList(ColorStateList.valueOf(mBackgroundColor));
+ }
+ if (mForegroundColor != 0) {
+ this.setImageTintList(ColorStateList.valueOf(mForegroundColor));
+ }
+ }
+
+ /**
+ * Set the color used for the foreground.
+ */
+ @RemotableViewMethod
+ public void setForegroundColor(@ColorInt int color) {
+ mForegroundColor = color;
+ updateColors();
+ }
+
+ /**
+ * Sets the color used for the background.
+ */
+ @RemotableViewMethod
+ public void setBackgroundColor(@ColorInt int color) {
+ mBackgroundColor = color;
+ updateColors();
+ }
+}
diff --git a/core/res/res/drawable/notification_close_button_icon.xml b/core/res/res/drawable/notification_close_button_icon.xml
new file mode 100644
index 000000000000..947cd5a10525
--- /dev/null
+++ b/core/res/res/drawable/notification_close_button_icon.xml
@@ -0,0 +1,28 @@
+<?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.
+ -->
+
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+ android:width="@dimen/notification_close_button_size"
+ android:height="@dimen/notification_close_button_size"
+ android:viewportWidth="16.0"
+ android:viewportHeight="16.0">
+<path
+ android:fillColor="#FF000000"
+ android:pathData="M 12.6667 4.2733 L 11.7267 3.3333 L 8 7.06 L 4.2734 3.3333 L 3.3334
+4.2733 L 7.06 8 L 3.3334 11.7267 L 4.2734 12.6667 L 8 8.94 L 11.7267 12.6667 L 12.6667
+11.7267 L 8.94 8 L 12.6667 4.2733 Z"/>
+</vector> \ No newline at end of file
diff --git a/core/res/res/layout/notification_close_button.xml b/core/res/res/layout/notification_close_button.xml
new file mode 100644
index 000000000000..5eff84e6981a
--- /dev/null
+++ b/core/res/res/layout/notification_close_button.xml
@@ -0,0 +1,30 @@
+<?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
+ -->
+
+<com.android.internal.widget.NotificationCloseButton
+ xmlns:android="http://schemas.android.com/apk/res/android"
+ android:id="@+id/close_button"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_gravity="top|end"
+ android:contentDescription="@string/close_button_text"
+ android:visibility="gone"
+ android:src="@drawable/notification_close_button_icon"
+ android:padding="2dp"
+ android:scaleType="fitCenter"
+ android:importantForAccessibility="no"
+ >
+</com.android.internal.widget.NotificationCloseButton>
diff --git a/core/res/res/layout/notification_template_header.xml b/core/res/res/layout/notification_template_header.xml
index d80b7657d0b0..e44c7272bc1e 100644
--- a/core/res/res/layout/notification_template_header.xml
+++ b/core/res/res/layout/notification_template_header.xml
@@ -83,11 +83,28 @@
android:focusable="false"
/>
- <include layout="@layout/notification_expand_button"
+ <LinearLayout
+ android:id="@+id/notification_buttons_column"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
- android:layout_centerVertical="true"
- />
+ android:orientation="vertical"
+ >
+
+ <include layout="@layout/notification_close_button"
+ android:layout_width="@dimen/notification_close_button_size"
+ android:layout_height="@dimen/notification_close_button_size"
+ android:layout_gravity="end"
+ android:layout_marginEnd="20dp"
+ />
+
+ <include layout="@layout/notification_expand_button"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_alignParentEnd="true"
+ android:layout_centerVertical="true"
+ />
+
+ </LinearLayout>
</NotificationHeaderView>
diff --git a/core/res/res/layout/notification_template_material_base.xml b/core/res/res/layout/notification_template_material_base.xml
index 452df50484c8..29f14a4a93fc 100644
--- a/core/res/res/layout/notification_template_material_base.xml
+++ b/core/res/res/layout/notification_template_material_base.xml
@@ -157,20 +157,38 @@
android:maxDrawableHeight="@dimen/notification_right_icon_size"
/>
- <FrameLayout
- android:id="@+id/expand_button_touch_container"
+ <LinearLayout
+ android:id="@+id/notification_buttons_column"
android:layout_width="wrap_content"
android:layout_height="match_parent"
- android:minWidth="@dimen/notification_content_margin_end"
+ android:layout_alignParentEnd="true"
+ android:orientation="vertical"
>
- <include layout="@layout/notification_expand_button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical|end"
+ <include layout="@layout/notification_close_button"
+ android:layout_width="@dimen/notification_close_button_size"
+ android:layout_height="@dimen/notification_close_button_size"
+ android:layout_gravity="end"
+ android:layout_marginEnd="20dp"
/>
- </FrameLayout>
+ <FrameLayout
+ android:id="@+id/expand_button_touch_container"
+ android:layout_width="wrap_content"
+ android:layout_height="0dp"
+ android:layout_weight="1"
+ android:minWidth="@dimen/notification_content_margin_end"
+ >
+
+ <include layout="@layout/notification_expand_button"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_gravity="center_vertical|end"
+ />
+
+ </FrameLayout>
+
+ </LinearLayout>
</LinearLayout>
diff --git a/core/res/res/values/dimens.xml b/core/res/res/values/dimens.xml
index a0807ca580a2..7d622275f6f6 100644
--- a/core/res/res/values/dimens.xml
+++ b/core/res/res/values/dimens.xml
@@ -355,6 +355,9 @@
<!-- the padding of the expand icon in the notification header -->
<dimen name="notification_expand_button_icon_padding">2dp</dimen>
+ <!-- the size of the notification close button -->
+ <dimen name="notification_close_button_size">16dp</dimen>
+
<!-- Vertical margin for the headerless notification content, when content has 1 line -->
<!-- 16 * 2 (margins) + 24 (1 line) = 56 (notification) -->
<dimen name="notification_headerless_margin_oneline">16dp</dimen>
diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml
index 639b74681155..070f6c48ef14 100644
--- a/core/res/res/values/symbols.xml
+++ b/core/res/res/values/symbols.xml
@@ -3170,6 +3170,7 @@
<java-symbol type="id" name="header_text_secondary_divider" />
<java-symbol type="drawable" name="ic_expand_notification" />
<java-symbol type="drawable" name="ic_collapse_notification" />
+ <java-symbol type="drawable" name="notification_close_button_icon" />
<java-symbol type="drawable" name="ic_expand_bundle" />
<java-symbol type="drawable" name="ic_collapse_bundle" />
<java-symbol type="drawable" name="ic_notification_summary_auto" />
@@ -3876,6 +3877,7 @@
<java-symbol type="string" name="expand_button_content_description_collapsed" />
<java-symbol type="string" name="expand_button_content_description_expanded" />
+ <java-symbol type="string" name="close_button_text" />
<java-symbol type="string" name="content_description_collapsed" />
<java-symbol type="string" name="content_description_expanded" />
@@ -5040,6 +5042,9 @@
<java-symbol type="string" name="ui_translation_accessibility_translation_finished" />
<java-symbol type="layout" name="notification_expand_button"/>
+ <java-symbol type="id" name="close_button" />
+ <java-symbol type="layout" name="notification_close_button"/>
+ <java-symbol type="id" name="notification_buttons_column" />
<java-symbol type="bool" name="config_supportsMicToggle" />
<java-symbol type="bool" name="config_supportsCamToggle" />