| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package com.android.wm.shell.bubbles; |
| 17 |
|
| 18 |
import android.annotation.DrawableRes; |
| 19 |
import android.annotation.Nullable; |
| 20 |
import android.content.Context; |
| 21 |
import android.content.res.TypedArray; |
| 22 |
import android.graphics.Bitmap; |
| 23 |
import android.graphics.Canvas; |
| 24 |
import android.graphics.Outline; |
| 25 |
import android.graphics.Path; |
| 26 |
import android.graphics.Rect; |
| 27 |
import android.graphics.drawable.Drawable; |
| 28 |
import android.util.AttributeSet; |
| 29 |
import android.util.PathParser; |
| 30 |
import android.view.LayoutInflater; |
| 31 |
import android.view.View; |
| 32 |
import android.view.ViewOutlineProvider; |
| 33 |
import android.widget.ImageView; |
| 34 |
|
| 35 |
import androidx.constraintlayout.widget.ConstraintLayout; |
| 36 |
|
| 37 |
import com.android.launcher3.icons.DotRenderer; |
| 38 |
import com.android.launcher3.icons.IconNormalizer; |
| 39 |
import com.android.wm.shell.R; |
| 40 |
import com.android.wm.shell.animation.Interpolators; |
| 41 |
|
| 42 |
import java.util.EnumSet; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public class BadgedImageView extends ConstraintLayout { |
| 52 |
|
| 53 |
|
| 54 |
public static final float WHITE_SCRIM_ALPHA = 0.54f; |
| 55 |
|
| 56 |
public static final int DEFAULT_PATH_SIZE = 100; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
enum SuppressionFlag { |
| 63 |
|
| 64 |
FLYOUT_VISIBLE, |
| 65 |
|
| 66 |
BEHIND_STACK, |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
private final EnumSet<SuppressionFlag> mDotSuppressionFlags = |
| 74 |
EnumSet.of(SuppressionFlag.FLYOUT_VISIBLE); |
| 75 |
|
| 76 |
private final ImageView mBubbleIcon; |
| 77 |
private final ImageView mAppIcon; |
| 78 |
|
| 79 |
private float mDotScale = 0f; |
| 80 |
private float mAnimatingToDotScale = 0f; |
| 81 |
private boolean mDotIsAnimating = false; |
| 82 |
|
| 83 |
private BubbleViewProvider mBubble; |
| 84 |
private BubblePositioner mPositioner; |
| 85 |
private boolean mOnLeft; |
| 86 |
|
| 87 |
private DotRenderer mDotRenderer; |
| 88 |
private DotRenderer.DrawParams mDrawParams; |
| 89 |
private int mDotColor; |
| 90 |
|
| 91 |
private Rect mTempBounds = new Rect(); |
| 92 |
|
| 93 |
public BadgedImageView(Context context) { |
| 94 |
this(context, null); |
| 95 |
} |
| 96 |
|
| 97 |
public BadgedImageView(Context context, AttributeSet attrs) { |
| 98 |
this(context, attrs, 0); |
| 99 |
} |
| 100 |
|
| 101 |
public BadgedImageView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 102 |
this(context, attrs, defStyleAttr, 0); |
| 103 |
} |
| 104 |
|
| 105 |
public BadgedImageView(Context context, AttributeSet attrs, int defStyleAttr, |
| 106 |
int defStyleRes) { |
| 107 |
super(context, attrs, defStyleAttr, defStyleRes); |
| 108 |
|
| 109 |
setLayoutDirection(LAYOUT_DIRECTION_LTR); |
| 110 |
|
| 111 |
LayoutInflater.from(context).inflate(R.layout.badged_image_view, this); |
| 112 |
|
| 113 |
mBubbleIcon = findViewById(R.id.icon_view); |
| 114 |
mAppIcon = findViewById(R.id.app_icon_view); |
| 115 |
|
| 116 |
final TypedArray ta = mContext.obtainStyledAttributes(attrs, new int[]{android.R.attr.src}, |
| 117 |
defStyleAttr, defStyleRes); |
| 118 |
mBubbleIcon.setImageResource(ta.getResourceId(0, 0)); |
| 119 |
ta.recycle(); |
| 120 |
|
| 121 |
mDrawParams = new DotRenderer.DrawParams(); |
| 122 |
|
| 123 |
setFocusable(true); |
| 124 |
setClickable(true); |
| 125 |
setOutlineProvider(new ViewOutlineProvider() { |
| 126 |
@Override |
| 127 |
public void getOutline(View view, Outline outline) { |
| 128 |
BadgedImageView.this.getOutline(outline); |
| 129 |
} |
| 130 |
}); |
| 131 |
} |
| 132 |
|
| 133 |
private void getOutline(Outline outline) { |
| 134 |
final int bubbleSize = mPositioner.getBubbleSize(); |
| 135 |
final int normalizedSize = IconNormalizer.getNormalizedCircleSize(bubbleSize); |
| 136 |
final int inset = (bubbleSize - normalizedSize) / 2; |
| 137 |
outline.setOval(inset, inset, inset + normalizedSize, inset + normalizedSize); |
| 138 |
} |
| 139 |
|
| 140 |
public void initialize(BubblePositioner positioner) { |
| 141 |
mPositioner = positioner; |
| 142 |
|
| 143 |
Path iconPath = PathParser.createPathFromPathData( |
| 144 |
getResources().getString(com.android.internal.R.string.config_icon_mask)); |
| 145 |
mDotRenderer = new DotRenderer(mPositioner.getBubbleSize(), |
| 146 |
iconPath, DEFAULT_PATH_SIZE); |
| 147 |
} |
| 148 |
|
| 149 |
public void showDotAndBadge(boolean onLeft) { |
| 150 |
removeDotSuppressionFlag(BadgedImageView.SuppressionFlag.BEHIND_STACK); |
| 151 |
animateDotBadgePositions(onLeft); |
| 152 |
} |
| 153 |
|
| 154 |
public void hideDotAndBadge(boolean onLeft) { |
| 155 |
addDotSuppressionFlag(BadgedImageView.SuppressionFlag.BEHIND_STACK); |
| 156 |
mOnLeft = onLeft; |
| 157 |
hideBadge(); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public void setRenderedBubble(BubbleViewProvider bubble) { |
| 164 |
mBubble = bubble; |
| 165 |
mBubbleIcon.setImageBitmap(bubble.getBubbleIcon()); |
| 166 |
mAppIcon.setImageBitmap(bubble.getAppBadge()); |
| 167 |
if (mDotSuppressionFlags.contains(SuppressionFlag.BEHIND_STACK)) { |
| 168 |
hideBadge(); |
| 169 |
} else { |
| 170 |
showBadge(); |
| 171 |
} |
| 172 |
mDotColor = bubble.getDotColor(); |
| 173 |
drawDot(bubble.getDotPath()); |
| 174 |
} |
| 175 |
|
| 176 |
@Override |
| 177 |
public void dispatchDraw(Canvas canvas) { |
| 178 |
super.dispatchDraw(canvas); |
| 179 |
|
| 180 |
if (!shouldDrawDot()) { |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
getDrawingRect(mTempBounds); |
| 185 |
|
| 186 |
mDrawParams.dotColor = mDotColor; |
| 187 |
mDrawParams.iconBounds = mTempBounds; |
| 188 |
mDrawParams.leftAlign = mOnLeft; |
| 189 |
mDrawParams.scale = mDotScale; |
| 190 |
|
| 191 |
mDotRenderer.draw(canvas, mDrawParams); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public void setIconImageResource(@DrawableRes int drawable) { |
| 198 |
mBubbleIcon.setImageResource(drawable); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public Drawable getIconDrawable() { |
| 205 |
return mBubbleIcon.getDrawable(); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
void addDotSuppressionFlag(SuppressionFlag flag) { |
| 210 |
if (mDotSuppressionFlags.add(flag)) { |
| 211 |
|
| 212 |
updateDotVisibility(flag == SuppressionFlag.BEHIND_STACK ); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
void removeDotSuppressionFlag(SuppressionFlag flag) { |
| 218 |
if (mDotSuppressionFlags.remove(flag)) { |
| 219 |
|
| 220 |
updateDotVisibility(flag == SuppressionFlag.BEHIND_STACK); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
void updateDotVisibility(boolean animate) { |
| 226 |
final float targetScale = shouldDrawDot() ? 1f : 0f; |
| 227 |
|
| 228 |
if (animate) { |
| 229 |
animateDotScale(targetScale, null ); |
| 230 |
} else { |
| 231 |
mDotScale = targetScale; |
| 232 |
mAnimatingToDotScale = targetScale; |
| 233 |
invalidate(); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
void drawDot(Path iconPath) { |
| 241 |
mDotRenderer = new DotRenderer(mPositioner.getBubbleSize(), |
| 242 |
iconPath, DEFAULT_PATH_SIZE); |
| 243 |
invalidate(); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
void setDotScale(float fraction) { |
| 250 |
mDotScale = fraction; |
| 251 |
invalidate(); |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
boolean getDotOnLeft() { |
| 258 |
return mOnLeft; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
float[] getDotCenter() { |
| 265 |
float[] dotPosition; |
| 266 |
if (mOnLeft) { |
| 267 |
dotPosition = mDotRenderer.getLeftDotPosition(); |
| 268 |
} else { |
| 269 |
dotPosition = mDotRenderer.getRightDotPosition(); |
| 270 |
} |
| 271 |
getDrawingRect(mTempBounds); |
| 272 |
float dotCenterX = mTempBounds.width() * dotPosition[0]; |
| 273 |
float dotCenterY = mTempBounds.height() * dotPosition[1]; |
| 274 |
return new float[]{dotCenterX, dotCenterY}; |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
@Nullable |
| 281 |
public String getKey() { |
| 282 |
return (mBubble != null) ? mBubble.getKey() : null; |
| 283 |
} |
| 284 |
|
| 285 |
int getDotColor() { |
| 286 |
return mDotColor; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
void animateDotBadgePositions(boolean onLeft) { |
| 291 |
mOnLeft = onLeft; |
| 292 |
|
| 293 |
if (onLeft != getDotOnLeft() && shouldDrawDot()) { |
| 294 |
animateDotScale(0f , () -> { |
| 295 |
invalidate(); |
| 296 |
animateDotScale(1.0f, null ); |
| 297 |
}); |
| 298 |
} |
| 299 |
|
| 300 |
showBadge(); |
| 301 |
|
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
void setDotBadgeOnLeft(boolean onLeft) { |
| 306 |
mOnLeft = onLeft; |
| 307 |
invalidate(); |
| 308 |
showBadge(); |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
private boolean shouldDrawDot() { |
| 313 |
|
| 314 |
|
| 315 |
return mDotIsAnimating || (mBubble.showDot() && mDotSuppressionFlags.isEmpty()); |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
private void animateDotScale(float toScale, @Nullable Runnable after) { |
| 322 |
mDotIsAnimating = true; |
| 323 |
|
| 324 |
|
| 325 |
if (mAnimatingToDotScale == toScale || !shouldDrawDot()) { |
| 326 |
mDotIsAnimating = false; |
| 327 |
return; |
| 328 |
} |
| 329 |
|
| 330 |
mAnimatingToDotScale = toScale; |
| 331 |
|
| 332 |
final boolean showDot = toScale > 0f; |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
clearAnimation(); |
| 337 |
animate() |
| 338 |
.setDuration(200) |
| 339 |
.setInterpolator(Interpolators.FAST_OUT_SLOW_IN) |
| 340 |
.setUpdateListener((valueAnimator) -> { |
| 341 |
float fraction = valueAnimator.getAnimatedFraction(); |
| 342 |
fraction = showDot ? fraction : 1f - fraction; |
| 343 |
setDotScale(fraction); |
| 344 |
}).withEndAction(() -> { |
| 345 |
setDotScale(showDot ? 1f : 0f); |
| 346 |
mDotIsAnimating = false; |
| 347 |
if (after != null) { |
| 348 |
after.run(); |
| 349 |
} |
| 350 |
}).start(); |
| 351 |
} |
| 352 |
|
| 353 |
void showBadge() { |
| 354 |
Bitmap appBadgeBitmap = mBubble.getAppBadge(); |
| 355 |
if (appBadgeBitmap == null) { |
| 356 |
mAppIcon.setVisibility(GONE); |
| 357 |
return; |
| 358 |
} |
| 359 |
|
| 360 |
int translationX; |
| 361 |
if (mOnLeft) { |
| 362 |
translationX = -(mBubble.getBubbleIcon().getWidth() - appBadgeBitmap.getWidth()); |
| 363 |
} else { |
| 364 |
translationX = 0; |
| 365 |
} |
| 366 |
|
| 367 |
mAppIcon.setTranslationX(translationX); |
| 368 |
mAppIcon.setVisibility(VISIBLE); |
| 369 |
} |
| 370 |
|
| 371 |
void hideBadge() { |
| 372 |
mAppIcon.setVisibility(GONE); |
| 373 |
} |
| 374 |
|
| 375 |
@Override |
| 376 |
public String toString() { |
| 377 |
return "BadgedImageView{" + mBubble + "}"; |
| 378 |
} |
| 379 |
} |