Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | |
Lyn Han | cd6465e | 2019-12-09 15:14:17 -0800 | [diff] [blame] | 17 | package com.android.systemui.bubbles; |
| 18 | |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 19 | import static com.android.systemui.bubbles.BubbleDebugConfig.DEBUG_OVERFLOW; |
| 20 | import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_BUBBLES; |
| 21 | import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_WITH_CLASS_NAME; |
| 22 | |
Lyn Han | cd6465e | 2019-12-09 15:14:17 -0800 | [diff] [blame] | 23 | import android.app.Activity; |
Lyn Han | e570696 | 2020-05-05 13:45:13 -0700 | [diff] [blame] | 24 | import android.app.Notification; |
Lyn Han | 2cf5d90 | 2020-04-27 14:40:51 -0700 | [diff] [blame] | 25 | import android.content.Context; |
Lyn Han | e1cf3b2 | 2020-04-01 13:39:43 -0700 | [diff] [blame] | 26 | import android.content.res.Configuration; |
Lyn Han | 4dccbc6 | 2020-03-16 23:31:24 -0700 | [diff] [blame] | 27 | import android.content.res.Resources; |
Lyn Han | cd6465e | 2019-12-09 15:14:17 -0800 | [diff] [blame] | 28 | import android.content.res.TypedArray; |
| 29 | import android.graphics.Color; |
| 30 | import android.os.Bundle; |
Lyn Han | 4dccbc6 | 2020-03-16 23:31:24 -0700 | [diff] [blame] | 31 | import android.util.DisplayMetrics; |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 32 | import android.util.Log; |
| 33 | import android.view.LayoutInflater; |
Lyn Han | 05ea3f8 | 2020-02-05 17:53:08 -0800 | [diff] [blame] | 34 | import android.view.View; |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 35 | import android.view.ViewGroup; |
Lyn Han | e570696 | 2020-05-05 13:45:13 -0700 | [diff] [blame] | 36 | import android.view.accessibility.AccessibilityNodeInfo; |
Lyn Han | e1cf3b2 | 2020-04-01 13:39:43 -0700 | [diff] [blame] | 37 | import android.widget.ImageView; |
Lyn Han | 05ea3f8 | 2020-02-05 17:53:08 -0800 | [diff] [blame] | 38 | import android.widget.LinearLayout; |
Lyn Han | 4dccbc6 | 2020-03-16 23:31:24 -0700 | [diff] [blame] | 39 | import android.widget.TextView; |
Lyn Han | cd6465e | 2019-12-09 15:14:17 -0800 | [diff] [blame] | 40 | |
| 41 | import androidx.recyclerview.widget.GridLayoutManager; |
| 42 | import androidx.recyclerview.widget.RecyclerView; |
| 43 | |
Lyn Han | 6e6a52d | 2020-05-06 13:37:31 -0700 | [diff] [blame] | 44 | import com.android.internal.util.ContrastColorUtil; |
Lyn Han | cd6465e | 2019-12-09 15:14:17 -0800 | [diff] [blame] | 45 | import com.android.systemui.R; |
| 46 | |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 47 | import java.util.ArrayList; |
| 48 | import java.util.List; |
| 49 | import java.util.function.Consumer; |
| 50 | |
Lyn Han | 6cdc706 | 2020-01-15 13:32:17 -0800 | [diff] [blame] | 51 | import javax.inject.Inject; |
| 52 | |
Lyn Han | cd6465e | 2019-12-09 15:14:17 -0800 | [diff] [blame] | 53 | /** |
| 54 | * Activity for showing aged out bubbles. |
| 55 | * Must be public to be accessible to androidx...AppComponentFactory |
| 56 | */ |
| 57 | public class BubbleOverflowActivity extends Activity { |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 58 | private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleOverflowActivity" : TAG_BUBBLES; |
| 59 | |
Lyn Han | 05ea3f8 | 2020-02-05 17:53:08 -0800 | [diff] [blame] | 60 | private LinearLayout mEmptyState; |
Lyn Han | e1cf3b2 | 2020-04-01 13:39:43 -0700 | [diff] [blame] | 61 | private ImageView mEmptyStateImage; |
Lyn Han | 6cdc706 | 2020-01-15 13:32:17 -0800 | [diff] [blame] | 62 | private BubbleController mBubbleController; |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 63 | private BubbleOverflowAdapter mAdapter; |
| 64 | private RecyclerView mRecyclerView; |
| 65 | private List<Bubble> mOverflowBubbles = new ArrayList<>(); |
Lyn Han | 6cdc706 | 2020-01-15 13:32:17 -0800 | [diff] [blame] | 66 | |
Lyn Han | 2cf5d90 | 2020-04-27 14:40:51 -0700 | [diff] [blame] | 67 | private class NoScrollGridLayoutManager extends GridLayoutManager { |
| 68 | NoScrollGridLayoutManager(Context context, int columns) { |
| 69 | super(context, columns); |
| 70 | } |
| 71 | @Override |
| 72 | public boolean canScrollVertically() { |
| 73 | return false; |
| 74 | } |
| 75 | } |
| 76 | |
Lyn Han | 6cdc706 | 2020-01-15 13:32:17 -0800 | [diff] [blame] | 77 | @Inject |
| 78 | public BubbleOverflowActivity(BubbleController controller) { |
| 79 | mBubbleController = controller; |
| 80 | } |
Lyn Han | cd6465e | 2019-12-09 15:14:17 -0800 | [diff] [blame] | 81 | |
| 82 | @Override |
| 83 | public void onCreate(Bundle savedInstanceState) { |
| 84 | super.onCreate(savedInstanceState); |
| 85 | setContentView(R.layout.bubble_overflow_activity); |
| 86 | setBackgroundColor(); |
| 87 | |
Lyn Han | 05ea3f8 | 2020-02-05 17:53:08 -0800 | [diff] [blame] | 88 | mEmptyState = findViewById(R.id.bubble_overflow_empty_state); |
Lyn Han | cd6465e | 2019-12-09 15:14:17 -0800 | [diff] [blame] | 89 | mRecyclerView = findViewById(R.id.bubble_overflow_recycler); |
Lyn Han | e1cf3b2 | 2020-04-01 13:39:43 -0700 | [diff] [blame] | 90 | mEmptyStateImage = findViewById(R.id.bubble_overflow_empty_state_image); |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 91 | |
Lyn Han | 4dccbc6 | 2020-03-16 23:31:24 -0700 | [diff] [blame] | 92 | Resources res = getResources(); |
| 93 | final int columns = res.getInteger(R.integer.bubbles_overflow_columns); |
| 94 | mRecyclerView.setLayoutManager( |
Lyn Han | 2cf5d90 | 2020-04-27 14:40:51 -0700 | [diff] [blame] | 95 | new NoScrollGridLayoutManager(getApplicationContext(), columns)); |
Lyn Han | 4dccbc6 | 2020-03-16 23:31:24 -0700 | [diff] [blame] | 96 | |
| 97 | DisplayMetrics displayMetrics = new DisplayMetrics(); |
| 98 | getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); |
Lyn Han | 0b6ca73 | 2020-03-31 00:11:04 -0700 | [diff] [blame] | 99 | final int recyclerViewWidth = (displayMetrics.widthPixels |
| 100 | - res.getDimensionPixelSize(R.dimen.bubble_overflow_padding)); |
| 101 | final int viewWidth = recyclerViewWidth / columns; |
Lyn Han | 4dccbc6 | 2020-03-16 23:31:24 -0700 | [diff] [blame] | 102 | |
| 103 | final int maxOverflowBubbles = res.getInteger(R.integer.bubbles_max_overflow); |
| 104 | final int rows = (int) Math.ceil((double) maxOverflowBubbles / columns); |
Lyn Han | 0b6ca73 | 2020-03-31 00:11:04 -0700 | [diff] [blame] | 105 | final int recyclerViewHeight = res.getDimensionPixelSize(R.dimen.bubble_overflow_height) |
| 106 | - res.getDimensionPixelSize(R.dimen.bubble_overflow_padding); |
| 107 | final int viewHeight = recyclerViewHeight / rows; |
Lyn Han | 4dccbc6 | 2020-03-16 23:31:24 -0700 | [diff] [blame] | 108 | |
Lyn Han | e570696 | 2020-05-05 13:45:13 -0700 | [diff] [blame] | 109 | mAdapter = new BubbleOverflowAdapter(getApplicationContext(), mOverflowBubbles, |
Lyn Han | 4dccbc6 | 2020-03-16 23:31:24 -0700 | [diff] [blame] | 110 | mBubbleController::promoteBubbleFromOverflow, viewWidth, viewHeight); |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 111 | mRecyclerView.setAdapter(mAdapter); |
Mady Mellor | 1d08202 | 2020-05-12 16:35:39 +0000 | [diff] [blame] | 112 | onDataChanged(mBubbleController.getOverflowBubbles()); |
| 113 | mBubbleController.setOverflowCallback(() -> { |
| 114 | onDataChanged(mBubbleController.getOverflowBubbles()); |
| 115 | }); |
| 116 | onThemeChanged(); |
Lyn Han | e1cf3b2 | 2020-04-01 13:39:43 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Handle theme changes. |
| 121 | */ |
| 122 | void onThemeChanged() { |
| 123 | final int mode = |
| 124 | getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; |
| 125 | switch (mode) { |
| 126 | case Configuration.UI_MODE_NIGHT_NO: |
| 127 | if (DEBUG_OVERFLOW) { |
| 128 | Log.d(TAG, "Set overflow UI to light mode"); |
| 129 | } |
| 130 | mEmptyStateImage.setImageDrawable( |
| 131 | getResources().getDrawable(R.drawable.ic_empty_bubble_overflow_light)); |
| 132 | break; |
| 133 | case Configuration.UI_MODE_NIGHT_YES: |
| 134 | if (DEBUG_OVERFLOW) { |
| 135 | Log.d(TAG, "Set overflow UI to dark mode"); |
| 136 | } |
| 137 | mEmptyStateImage.setImageDrawable( |
| 138 | getResources().getDrawable(R.drawable.ic_empty_bubble_overflow_dark)); |
| 139 | break; |
| 140 | } |
Lyn Han | cd6465e | 2019-12-09 15:14:17 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void setBackgroundColor() { |
| 144 | final TypedArray ta = getApplicationContext().obtainStyledAttributes( |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 145 | new int[]{android.R.attr.colorBackgroundFloating}); |
Lyn Han | cd6465e | 2019-12-09 15:14:17 -0800 | [diff] [blame] | 146 | int bgColor = ta.getColor(0, Color.WHITE); |
| 147 | ta.recycle(); |
| 148 | findViewById(android.R.id.content).setBackgroundColor(bgColor); |
| 149 | } |
| 150 | |
Mady Mellor | 1d08202 | 2020-05-12 16:35:39 +0000 | [diff] [blame] | 151 | void onDataChanged(List<Bubble> bubbles) { |
| 152 | mOverflowBubbles.clear(); |
| 153 | mOverflowBubbles.addAll(bubbles); |
| 154 | mAdapter.notifyDataSetChanged(); |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 155 | |
Mady Mellor | 1d08202 | 2020-05-12 16:35:39 +0000 | [diff] [blame] | 156 | if (mOverflowBubbles.isEmpty()) { |
| 157 | mEmptyState.setVisibility(View.VISIBLE); |
| 158 | } else { |
| 159 | mEmptyState.setVisibility(View.GONE); |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 160 | } |
Mady Mellor | 1d08202 | 2020-05-12 16:35:39 +0000 | [diff] [blame] | 161 | |
| 162 | if (DEBUG_OVERFLOW) { |
| 163 | Log.d(TAG, "Updated overflow bubbles:\n" + BubbleDebugConfig.formatBubblesString( |
| 164 | mOverflowBubbles, /*selected*/ null)); |
| 165 | } |
| 166 | } |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 167 | |
Lyn Han | cd6465e | 2019-12-09 15:14:17 -0800 | [diff] [blame] | 168 | @Override |
| 169 | public void onStart() { |
| 170 | super.onStart(); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public void onRestart() { |
| 175 | super.onRestart(); |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public void onResume() { |
| 180 | super.onResume(); |
Lyn Han | e1cf3b2 | 2020-04-01 13:39:43 -0700 | [diff] [blame] | 181 | onThemeChanged(); |
Lyn Han | cd6465e | 2019-12-09 15:14:17 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public void onPause() { |
| 186 | super.onPause(); |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public void onStop() { |
| 191 | super.onStop(); |
| 192 | } |
| 193 | |
| 194 | public void onDestroy() { |
Lyn Han | 6cdc706 | 2020-01-15 13:32:17 -0800 | [diff] [blame] | 195 | super.onDestroy(); |
Lyn Han | cd6465e | 2019-12-09 15:14:17 -0800 | [diff] [blame] | 196 | } |
| 197 | } |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 198 | |
| 199 | class BubbleOverflowAdapter extends RecyclerView.Adapter<BubbleOverflowAdapter.ViewHolder> { |
Lyn Han | e570696 | 2020-05-05 13:45:13 -0700 | [diff] [blame] | 200 | private Context mContext; |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 201 | private Consumer<Bubble> mPromoteBubbleFromOverflow; |
| 202 | private List<Bubble> mBubbles; |
Lyn Han | 4dccbc6 | 2020-03-16 23:31:24 -0700 | [diff] [blame] | 203 | private int mWidth; |
| 204 | private int mHeight; |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 205 | |
Lyn Han | e570696 | 2020-05-05 13:45:13 -0700 | [diff] [blame] | 206 | public BubbleOverflowAdapter(Context context, List<Bubble> list, Consumer<Bubble> promoteBubble, |
| 207 | int width, int height) { |
| 208 | mContext = context; |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 209 | mBubbles = list; |
| 210 | mPromoteBubbleFromOverflow = promoteBubble; |
Lyn Han | 4dccbc6 | 2020-03-16 23:31:24 -0700 | [diff] [blame] | 211 | mWidth = width; |
| 212 | mHeight = height; |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public BubbleOverflowAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, |
| 217 | int viewType) { |
Lyn Han | 6e6a52d | 2020-05-06 13:37:31 -0700 | [diff] [blame] | 218 | |
| 219 | // Set layout for overflow bubble view. |
Lyn Han | 4dccbc6 | 2020-03-16 23:31:24 -0700 | [diff] [blame] | 220 | LinearLayout overflowView = (LinearLayout) LayoutInflater.from(parent.getContext()) |
| 221 | .inflate(R.layout.bubble_overflow_view, parent, false); |
Lyn Han | cd4f87e | 2020-02-19 20:33:45 -0800 | [diff] [blame] | 222 | LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( |
| 223 | LinearLayout.LayoutParams.WRAP_CONTENT, |
Lyn Han | 4dccbc6 | 2020-03-16 23:31:24 -0700 | [diff] [blame] | 224 | LinearLayout.LayoutParams.WRAP_CONTENT); |
| 225 | params.width = mWidth; |
| 226 | params.height = mHeight; |
| 227 | overflowView.setLayoutParams(params); |
Lyn Han | 6e6a52d | 2020-05-06 13:37:31 -0700 | [diff] [blame] | 228 | |
| 229 | // Ensure name has enough contrast. |
| 230 | final TypedArray ta = mContext.obtainStyledAttributes( |
| 231 | new int[]{android.R.attr.colorBackgroundFloating, android.R.attr.textColorPrimary}); |
| 232 | final int bgColor = ta.getColor(0, Color.WHITE); |
| 233 | int textColor = ta.getColor(1, Color.BLACK); |
| 234 | textColor = ContrastColorUtil.ensureTextContrast(textColor, bgColor, true); |
| 235 | ta.recycle(); |
| 236 | |
| 237 | TextView viewName = overflowView.findViewById(R.id.bubble_view_name); |
| 238 | viewName.setTextColor(textColor); |
| 239 | |
Lyn Han | 4dccbc6 | 2020-03-16 23:31:24 -0700 | [diff] [blame] | 240 | return new ViewHolder(overflowView); |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | @Override |
| 244 | public void onBindViewHolder(ViewHolder vh, int index) { |
Lyn Han | 4dccbc6 | 2020-03-16 23:31:24 -0700 | [diff] [blame] | 245 | Bubble b = mBubbles.get(index); |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 246 | |
Joshua Tsuji | 2ed260e | 2020-03-26 14:26:01 -0400 | [diff] [blame] | 247 | vh.iconView.setRenderedBubble(b); |
Lyn Han | 2480d9c | 2020-04-26 07:19:00 -0700 | [diff] [blame] | 248 | vh.iconView.removeDotSuppressionFlag(BadgedImageView.SuppressionFlag.FLYOUT_VISIBLE); |
Lyn Han | 4dccbc6 | 2020-03-16 23:31:24 -0700 | [diff] [blame] | 249 | vh.iconView.setOnClickListener(view -> { |
| 250 | mBubbles.remove(b); |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 251 | notifyDataSetChanged(); |
Lyn Han | 4dccbc6 | 2020-03-16 23:31:24 -0700 | [diff] [blame] | 252 | mPromoteBubbleFromOverflow.accept(b); |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 253 | }); |
Lyn Han | 4dccbc6 | 2020-03-16 23:31:24 -0700 | [diff] [blame] | 254 | |
Lyn Han | e570696 | 2020-05-05 13:45:13 -0700 | [diff] [blame] | 255 | final CharSequence titleCharSeq = |
| 256 | b.getEntry().getSbn().getNotification().extras.getCharSequence( |
| 257 | Notification.EXTRA_TITLE); |
| 258 | String titleStr = mContext.getResources().getString(R.string.notification_bubble_title); |
| 259 | if (titleCharSeq != null) { |
| 260 | titleStr = titleCharSeq.toString(); |
| 261 | } |
| 262 | vh.iconView.setContentDescription(mContext.getResources().getString( |
| 263 | R.string.bubble_content_description_single, titleStr, b.getAppName())); |
| 264 | |
| 265 | vh.iconView.setAccessibilityDelegate( |
| 266 | new View.AccessibilityDelegate() { |
| 267 | @Override |
| 268 | public void onInitializeAccessibilityNodeInfo(View host, |
| 269 | AccessibilityNodeInfo info) { |
| 270 | super.onInitializeAccessibilityNodeInfo(host, info); |
| 271 | // Talkback prompts "Double tap to add back to stack" |
| 272 | // instead of the default "Double tap to activate" |
| 273 | info.addAction( |
| 274 | new AccessibilityNodeInfo.AccessibilityAction( |
| 275 | AccessibilityNodeInfo.ACTION_CLICK, |
| 276 | mContext.getResources().getString( |
| 277 | R.string.bubble_accessibility_action_add_back))); |
| 278 | } |
| 279 | }); |
| 280 | |
Lyn Han | 4dccbc6 | 2020-03-16 23:31:24 -0700 | [diff] [blame] | 281 | Bubble.FlyoutMessage message = b.getFlyoutMessage(); |
| 282 | if (message != null && message.senderName != null) { |
| 283 | vh.textView.setText(message.senderName); |
| 284 | } else { |
| 285 | vh.textView.setText(b.getAppName()); |
| 286 | } |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | @Override |
| 290 | public int getItemCount() { |
| 291 | return mBubbles.size(); |
| 292 | } |
| 293 | |
| 294 | public static class ViewHolder extends RecyclerView.ViewHolder { |
Lyn Han | 4dccbc6 | 2020-03-16 23:31:24 -0700 | [diff] [blame] | 295 | public BadgedImageView iconView; |
| 296 | public TextView textView; |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 297 | |
Lyn Han | 4dccbc6 | 2020-03-16 23:31:24 -0700 | [diff] [blame] | 298 | public ViewHolder(LinearLayout v) { |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 299 | super(v); |
Lyn Han | 4dccbc6 | 2020-03-16 23:31:24 -0700 | [diff] [blame] | 300 | iconView = v.findViewById(R.id.bubble_view); |
| 301 | textView = v.findViewById(R.id.bubble_view_name); |
Lyn Han | b58c756 | 2020-01-07 14:29:20 -0800 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | } |