Home | History | Annotate | Download | only in bubbles
      1 /*
      2  * Copyright (C) 2018 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 package com.android.systemui.bubbles;
     17 
     18 import android.content.Context;
     19 import android.content.res.TypedArray;
     20 import android.graphics.Canvas;
     21 import android.graphics.Point;
     22 import android.graphics.Rect;
     23 import android.util.AttributeSet;
     24 import android.widget.ImageView;
     25 
     26 import com.android.internal.graphics.ColorUtils;
     27 import com.android.systemui.R;
     28 
     29 /**
     30  * View that circle crops its contents and supports displaying a coloured dot on a top corner.
     31  */
     32 public class BadgedImageView extends ImageView {
     33 
     34     private BadgeRenderer mDotRenderer;
     35     private int mIconSize;
     36     private Rect mTempBounds = new Rect();
     37     private Point mTempPoint = new Point();
     38 
     39     private float mDotScale = 0f;
     40     private int mUpdateDotColor;
     41     private boolean mShowUpdateDot;
     42     private boolean mOnLeft;
     43 
     44     public BadgedImageView(Context context) {
     45         this(context, null);
     46     }
     47 
     48     public BadgedImageView(Context context, AttributeSet attrs) {
     49         this(context, attrs, 0);
     50     }
     51 
     52     public BadgedImageView(Context context, AttributeSet attrs, int defStyleAttr) {
     53         this(context, attrs, defStyleAttr, 0);
     54     }
     55 
     56     public BadgedImageView(Context context, AttributeSet attrs, int defStyleAttr,
     57             int defStyleRes) {
     58         super(context, attrs, defStyleAttr, defStyleRes);
     59         mIconSize = getResources().getDimensionPixelSize(R.dimen.individual_bubble_size);
     60         mDotRenderer = new BadgeRenderer(getContext());
     61 
     62         TypedArray ta = context.obtainStyledAttributes(
     63                 new int[] {android.R.attr.colorBackgroundFloating});
     64         ta.recycle();
     65     }
     66 
     67     @Override
     68     public void onDraw(Canvas canvas) {
     69         super.onDraw(canvas);
     70         if (mShowUpdateDot) {
     71             getDrawingRect(mTempBounds);
     72             mTempPoint.set((getWidth() - mIconSize) / 2, getPaddingTop());
     73             mDotRenderer.draw(canvas, mUpdateDotColor, mTempBounds, mDotScale, mTempPoint,
     74                     mOnLeft);
     75         }
     76     }
     77 
     78     /**
     79      * Set whether the dot should appear on left or right side of the view.
     80      */
     81     public void setDotPosition(boolean onLeft) {
     82         mOnLeft = onLeft;
     83         invalidate();
     84     }
     85 
     86     public boolean getDotPosition() {
     87         return mOnLeft;
     88     }
     89 
     90     /**
     91      * Set whether the dot should show or not.
     92      */
     93     public void setShowDot(boolean showBadge) {
     94         mShowUpdateDot = showBadge;
     95         invalidate();
     96     }
     97 
     98     /**
     99      * @return whether the dot is being displayed.
    100      */
    101     public boolean isShowingDot() {
    102         return mShowUpdateDot;
    103     }
    104 
    105     /**
    106      * The colour to use for the dot.
    107      */
    108     public void setDotColor(int color) {
    109         mUpdateDotColor = ColorUtils.setAlphaComponent(color, 255 /* alpha */);
    110         invalidate();
    111     }
    112 
    113     /**
    114      * How big the dot should be, fraction from 0 to 1.
    115      */
    116     public void setDotScale(float fraction) {
    117         mDotScale = fraction;
    118         invalidate();
    119     }
    120 
    121     public float getDotScale() {
    122         return mDotScale;
    123     }
    124 }
    125