Lyn Han | 3cd75d7 | 2020-02-15 19:10:12 -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 | |
| 17 | package com.android.systemui.bubbles; |
| 18 | |
Lyn Han | 9f66c3b | 2020-03-05 23:59:29 -0800 | [diff] [blame] | 19 | import static android.view.Display.INVALID_DISPLAY; |
Lyn Han | 3cd75d7 | 2020-02-15 19:10:12 -0800 | [diff] [blame] | 20 | import static android.view.View.GONE; |
Lyn Han | cd4f87e | 2020-02-19 20:33:45 -0800 | [diff] [blame] | 21 | |
| 22 | import static com.android.systemui.bubbles.BadgedImageView.DEFAULT_PATH_SIZE; |
Lyn Han | 3cd75d7 | 2020-02-15 19:10:12 -0800 | [diff] [blame] | 23 | |
| 24 | import android.content.Context; |
| 25 | import android.content.res.TypedArray; |
Lyn Han | cd4f87e | 2020-02-19 20:33:45 -0800 | [diff] [blame] | 26 | import android.graphics.Bitmap; |
Lyn Han | 3cd75d7 | 2020-02-15 19:10:12 -0800 | [diff] [blame] | 27 | import android.graphics.Color; |
Lyn Han | cd4f87e | 2020-02-19 20:33:45 -0800 | [diff] [blame] | 28 | import android.graphics.Matrix; |
| 29 | import android.graphics.Path; |
Lyn Han | 3cd75d7 | 2020-02-15 19:10:12 -0800 | [diff] [blame] | 30 | import android.graphics.drawable.AdaptiveIconDrawable; |
| 31 | import android.graphics.drawable.ColorDrawable; |
| 32 | import android.graphics.drawable.InsetDrawable; |
Lyn Han | cd4f87e | 2020-02-19 20:33:45 -0800 | [diff] [blame] | 33 | import android.util.PathParser; |
| 34 | import android.util.TypedValue; |
Lyn Han | 3cd75d7 | 2020-02-15 19:10:12 -0800 | [diff] [blame] | 35 | import android.view.LayoutInflater; |
| 36 | import android.view.View; |
| 37 | import android.view.ViewGroup; |
Lyn Han | 3cd75d7 | 2020-02-15 19:10:12 -0800 | [diff] [blame] | 38 | import android.widget.ImageView; |
| 39 | |
| 40 | import com.android.systemui.R; |
| 41 | |
| 42 | /** |
| 43 | * Class for showing aged out bubbles. |
| 44 | */ |
| 45 | public class BubbleOverflow implements BubbleViewProvider { |
Lyn Han | cd4f87e | 2020-02-19 20:33:45 -0800 | [diff] [blame] | 46 | public static final String KEY = "Overflow"; |
Lyn Han | 3cd75d7 | 2020-02-15 19:10:12 -0800 | [diff] [blame] | 47 | |
Lyn Han | cd4f87e | 2020-02-19 20:33:45 -0800 | [diff] [blame] | 48 | private BadgedImageView mOverflowBtn; |
Lyn Han | 9f66c3b | 2020-03-05 23:59:29 -0800 | [diff] [blame] | 49 | private BubbleExpandedView mExpandedView; |
Lyn Han | 3cd75d7 | 2020-02-15 19:10:12 -0800 | [diff] [blame] | 50 | private LayoutInflater mInflater; |
| 51 | private Context mContext; |
Lyn Han | cd4f87e | 2020-02-19 20:33:45 -0800 | [diff] [blame] | 52 | private Bitmap mIcon; |
| 53 | private Path mPath; |
| 54 | private int mBitmapSize; |
| 55 | private int mIconBitmapSize; |
| 56 | private int mDotColor; |
Lyn Han | 3cd75d7 | 2020-02-15 19:10:12 -0800 | [diff] [blame] | 57 | |
| 58 | public BubbleOverflow(Context context) { |
| 59 | mContext = context; |
| 60 | mInflater = LayoutInflater.from(context); |
Lyn Han | cd4f87e | 2020-02-19 20:33:45 -0800 | [diff] [blame] | 61 | mBitmapSize = mContext.getResources().getDimensionPixelSize(R.dimen.bubble_bitmap_size); |
| 62 | mIconBitmapSize = mContext.getResources().getDimensionPixelSize( |
| 63 | R.dimen.bubble_overflow_icon_bitmap_size); |
Lyn Han | 3cd75d7 | 2020-02-15 19:10:12 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Mady Mellor | 0122cc9 | 2020-02-27 12:15:39 -0800 | [diff] [blame] | 66 | void setUpOverflow(ViewGroup parentViewGroup, BubbleStackView stackView) { |
Lyn Han | 9f66c3b | 2020-03-05 23:59:29 -0800 | [diff] [blame] | 67 | mExpandedView = (BubbleExpandedView) mInflater.inflate( |
Lyn Han | 3cd75d7 | 2020-02-15 19:10:12 -0800 | [diff] [blame] | 68 | R.layout.bubble_expanded_view, parentViewGroup /* root */, |
| 69 | false /* attachToRoot */); |
Lyn Han | 9f66c3b | 2020-03-05 23:59:29 -0800 | [diff] [blame] | 70 | mExpandedView.setOverflow(true); |
| 71 | mExpandedView.setStackView(stackView); |
Lyn Han | 3cd75d7 | 2020-02-15 19:10:12 -0800 | [diff] [blame] | 72 | |
Lyn Han | cd4f87e | 2020-02-19 20:33:45 -0800 | [diff] [blame] | 73 | updateIcon(mContext, parentViewGroup); |
| 74 | } |
| 75 | |
Lyn Han | cd4f87e | 2020-02-19 20:33:45 -0800 | [diff] [blame] | 76 | void updateIcon(Context context, ViewGroup parentViewGroup) { |
| 77 | mInflater = LayoutInflater.from(context); |
| 78 | mOverflowBtn = (BadgedImageView) mInflater.inflate(R.layout.bubble_overflow_button, |
Lyn Han | 3cd75d7 | 2020-02-15 19:10:12 -0800 | [diff] [blame] | 79 | parentViewGroup /* root */, |
| 80 | false /* attachToRoot */); |
Lyn Han | e570696 | 2020-05-05 13:45:13 -0700 | [diff] [blame] | 81 | mOverflowBtn.setContentDescription(mContext.getResources().getString( |
| 82 | R.string.bubble_overflow_button_content_description)); |
Lyn Han | 3cd75d7 | 2020-02-15 19:10:12 -0800 | [diff] [blame] | 83 | |
Lyn Han | cd4f87e | 2020-02-19 20:33:45 -0800 | [diff] [blame] | 84 | TypedArray ta = mContext.obtainStyledAttributes( |
| 85 | new int[]{android.R.attr.colorBackgroundFloating}); |
| 86 | int bgColor = ta.getColor(0, Color.WHITE /* default */); |
| 87 | ta.recycle(); |
| 88 | |
| 89 | TypedValue typedValue = new TypedValue(); |
| 90 | context.getTheme().resolveAttribute(android.R.attr.colorAccent, typedValue, true); |
| 91 | int colorAccent = mContext.getColor(typedValue.resourceId); |
| 92 | mOverflowBtn.getDrawable().setTint(colorAccent); |
| 93 | mDotColor = colorAccent; |
| 94 | |
| 95 | ColorDrawable bg = new ColorDrawable(bgColor); |
| 96 | InsetDrawable fg = new InsetDrawable(mOverflowBtn.getDrawable(), |
| 97 | mBitmapSize - mIconBitmapSize /* inset */); |
| 98 | AdaptiveIconDrawable adaptiveIconDrawable = new AdaptiveIconDrawable(bg, fg); |
| 99 | |
| 100 | BubbleIconFactory iconFactory = new BubbleIconFactory(context); |
| 101 | mIcon = iconFactory.createBadgedIconBitmap(adaptiveIconDrawable, |
| 102 | null /* user */, |
| 103 | true /* shrinkNonAdaptiveIcons */).icon; |
| 104 | |
| 105 | float scale = iconFactory.getNormalizer().getScale(mOverflowBtn.getDrawable(), |
| 106 | null /* outBounds */, null /* path */, null /* outMaskShape */); |
| 107 | float radius = DEFAULT_PATH_SIZE / 2f; |
| 108 | mPath = PathParser.createPathFromPathData( |
| 109 | context.getResources().getString(com.android.internal.R.string.config_icon_mask)); |
| 110 | Matrix matrix = new Matrix(); |
| 111 | matrix.setScale(scale /* x scale */, scale /* y scale */, radius /* pivot x */, |
| 112 | radius /* pivot y */); |
| 113 | mPath.transform(matrix); |
| 114 | |
Lyn Han | 3cd75d7 | 2020-02-15 19:10:12 -0800 | [diff] [blame] | 115 | mOverflowBtn.setVisibility(GONE); |
Joshua Tsuji | 2ed260e | 2020-03-26 14:26:01 -0400 | [diff] [blame] | 116 | mOverflowBtn.setRenderedBubble(this); |
Lyn Han | 3cd75d7 | 2020-02-15 19:10:12 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | ImageView getBtn() { |
| 120 | return mOverflowBtn; |
| 121 | } |
| 122 | |
| 123 | void setBtnVisible(int visible) { |
| 124 | mOverflowBtn.setVisibility(visible); |
| 125 | } |
| 126 | |
Lyn Han | cd4f87e | 2020-02-19 20:33:45 -0800 | [diff] [blame] | 127 | @Override |
Lyn Han | 3cd75d7 | 2020-02-15 19:10:12 -0800 | [diff] [blame] | 128 | public BubbleExpandedView getExpandedView() { |
Lyn Han | 9f66c3b | 2020-03-05 23:59:29 -0800 | [diff] [blame] | 129 | return mExpandedView; |
Lyn Han | 3cd75d7 | 2020-02-15 19:10:12 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Lyn Han | cd4f87e | 2020-02-19 20:33:45 -0800 | [diff] [blame] | 132 | @Override |
| 133 | public int getDotColor() { |
| 134 | return mDotColor; |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public Bitmap getBadgedImage() { |
| 139 | return mIcon; |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public boolean showDot() { |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public Path getDotPath() { |
| 149 | return mPath; |
| 150 | } |
| 151 | |
| 152 | @Override |
Lyn Han | 3cd75d7 | 2020-02-15 19:10:12 -0800 | [diff] [blame] | 153 | public void setContentVisibility(boolean visible) { |
Lyn Han | 9f66c3b | 2020-03-05 23:59:29 -0800 | [diff] [blame] | 154 | mExpandedView.setContentVisibility(visible); |
Lyn Han | 3cd75d7 | 2020-02-15 19:10:12 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Lyn Han | cd4f87e | 2020-02-19 20:33:45 -0800 | [diff] [blame] | 157 | @Override |
Lyn Han | 3cd75d7 | 2020-02-15 19:10:12 -0800 | [diff] [blame] | 158 | public void logUIEvent(int bubbleCount, int action, float normalX, float normalY, |
| 159 | int index) { |
| 160 | // TODO(b/149133814) Log overflow UI events. |
| 161 | } |
| 162 | |
Lyn Han | cd4f87e | 2020-02-19 20:33:45 -0800 | [diff] [blame] | 163 | @Override |
Lyn Han | 3cd75d7 | 2020-02-15 19:10:12 -0800 | [diff] [blame] | 164 | public View getIconView() { |
| 165 | return mOverflowBtn; |
| 166 | } |
| 167 | |
Lyn Han | cd4f87e | 2020-02-19 20:33:45 -0800 | [diff] [blame] | 168 | @Override |
Lyn Han | 3cd75d7 | 2020-02-15 19:10:12 -0800 | [diff] [blame] | 169 | public String getKey() { |
Lyn Han | cd4f87e | 2020-02-19 20:33:45 -0800 | [diff] [blame] | 170 | return BubbleOverflow.KEY; |
Lyn Han | 3cd75d7 | 2020-02-15 19:10:12 -0800 | [diff] [blame] | 171 | } |
Lyn Han | 9f66c3b | 2020-03-05 23:59:29 -0800 | [diff] [blame] | 172 | |
| 173 | @Override |
| 174 | public int getDisplayId() { |
| 175 | return mExpandedView != null ? mExpandedView.getVirtualDisplayId() : INVALID_DISPLAY; |
| 176 | } |
Lyn Han | 3cd75d7 | 2020-02-15 19:10:12 -0800 | [diff] [blame] | 177 | } |