Home | History | Annotate | Download | only in launcher2
      1 /*
      2  * Copyright (C) 2008 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.launcher2;
     18 
     19 import android.content.Context;
     20 import android.content.res.Resources;
     21 import android.graphics.Bitmap;
     22 import android.graphics.Canvas;
     23 import android.graphics.Rect;
     24 import android.graphics.Region;
     25 import android.graphics.Region.Op;
     26 import android.graphics.drawable.Drawable;
     27 import android.util.AttributeSet;
     28 import android.view.MotionEvent;
     29 import android.widget.TextView;
     30 
     31 /**
     32  * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan
     33  * because we want to make the bubble taller than the text and TextView's clip is
     34  * too aggressive.
     35  */
     36 public class BubbleTextView extends TextView {
     37     static final float CORNER_RADIUS = 4.0f;
     38     static final float SHADOW_LARGE_RADIUS = 4.0f;
     39     static final float SHADOW_SMALL_RADIUS = 1.75f;
     40     static final float SHADOW_Y_OFFSET = 2.0f;
     41     static final int SHADOW_LARGE_COLOUR = 0xDD000000;
     42     static final int SHADOW_SMALL_COLOUR = 0xCC000000;
     43     static final float PADDING_H = 8.0f;
     44     static final float PADDING_V = 3.0f;
     45 
     46     private int mPrevAlpha = -1;
     47 
     48     private final HolographicOutlineHelper mOutlineHelper = new HolographicOutlineHelper();
     49     private final Canvas mTempCanvas = new Canvas();
     50     private final Rect mTempRect = new Rect();
     51     private boolean mDidInvalidateForPressedState;
     52     private Bitmap mPressedOrFocusedBackground;
     53     private int mFocusedOutlineColor;
     54     private int mFocusedGlowColor;
     55     private int mPressedOutlineColor;
     56     private int mPressedGlowColor;
     57 
     58     private boolean mBackgroundSizeChanged;
     59     private Drawable mBackground;
     60 
     61     private boolean mStayPressed;
     62     private CheckLongPressHelper mLongPressHelper;
     63 
     64     public BubbleTextView(Context context) {
     65         super(context);
     66         init();
     67     }
     68 
     69     public BubbleTextView(Context context, AttributeSet attrs) {
     70         super(context, attrs);
     71         init();
     72     }
     73 
     74     public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {
     75         super(context, attrs, defStyle);
     76         init();
     77     }
     78 
     79     private void init() {
     80         mLongPressHelper = new CheckLongPressHelper(this);
     81         mBackground = getBackground();
     82 
     83         final Resources res = getContext().getResources();
     84         mFocusedOutlineColor = mFocusedGlowColor = mPressedOutlineColor = mPressedGlowColor =
     85             res.getColor(android.R.color.white);
     86 
     87         setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
     88     }
     89 
     90     public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
     91         Bitmap b = info.getIcon(iconCache);
     92 
     93         setCompoundDrawablesWithIntrinsicBounds(null,
     94                 new FastBitmapDrawable(b),
     95                 null, null);
     96         setText(info.title);
     97         setTag(info);
     98     }
     99 
    100     @Override
    101     protected boolean setFrame(int left, int top, int right, int bottom) {
    102         if (getLeft() != left || getRight() != right || getTop() != top || getBottom() != bottom) {
    103             mBackgroundSizeChanged = true;
    104         }
    105         return super.setFrame(left, top, right, bottom);
    106     }
    107 
    108     @Override
    109     protected boolean verifyDrawable(Drawable who) {
    110         return who == mBackground || super.verifyDrawable(who);
    111     }
    112 
    113     @Override
    114     public void setTag(Object tag) {
    115         if (tag != null) {
    116             LauncherModel.checkItemInfo((ItemInfo) tag);
    117         }
    118         super.setTag(tag);
    119     }
    120 
    121     @Override
    122     protected void drawableStateChanged() {
    123         if (isPressed()) {
    124             // In this case, we have already created the pressed outline on ACTION_DOWN,
    125             // so we just need to do an invalidate to trigger draw
    126             if (!mDidInvalidateForPressedState) {
    127                 setCellLayoutPressedOrFocusedIcon();
    128             }
    129         } else {
    130             // Otherwise, either clear the pressed/focused background, or create a background
    131             // for the focused state
    132             final boolean backgroundEmptyBefore = mPressedOrFocusedBackground == null;
    133             if (!mStayPressed) {
    134                 mPressedOrFocusedBackground = null;
    135             }
    136             if (isFocused()) {
    137                 if (getLayout() == null) {
    138                     // In some cases, we get focus before we have been layed out. Set the
    139                     // background to null so that it will get created when the view is drawn.
    140                     mPressedOrFocusedBackground = null;
    141                 } else {
    142                     mPressedOrFocusedBackground = createGlowingOutline(
    143                             mTempCanvas, mFocusedGlowColor, mFocusedOutlineColor);
    144                 }
    145                 mStayPressed = false;
    146                 setCellLayoutPressedOrFocusedIcon();
    147             }
    148             final boolean backgroundEmptyNow = mPressedOrFocusedBackground == null;
    149             if (!backgroundEmptyBefore && backgroundEmptyNow) {
    150                 setCellLayoutPressedOrFocusedIcon();
    151             }
    152         }
    153 
    154         Drawable d = mBackground;
    155         if (d != null && d.isStateful()) {
    156             d.setState(getDrawableState());
    157         }
    158         super.drawableStateChanged();
    159     }
    160 
    161     /**
    162      * Draw this BubbleTextView into the given Canvas.
    163      *
    164      * @param destCanvas the canvas to draw on
    165      * @param padding the horizontal and vertical padding to use when drawing
    166      */
    167     private void drawWithPadding(Canvas destCanvas, int padding) {
    168         final Rect clipRect = mTempRect;
    169         getDrawingRect(clipRect);
    170 
    171         // adjust the clip rect so that we don't include the text label
    172         clipRect.bottom =
    173             getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V + getLayout().getLineTop(0);
    174 
    175         // Draw the View into the bitmap.
    176         // The translate of scrollX and scrollY is necessary when drawing TextViews, because
    177         // they set scrollX and scrollY to large values to achieve centered text
    178         destCanvas.save();
    179         destCanvas.scale(getScaleX(), getScaleY(),
    180                 (getWidth() + padding) / 2, (getHeight() + padding) / 2);
    181         destCanvas.translate(-getScrollX() + padding / 2, -getScrollY() + padding / 2);
    182         destCanvas.clipRect(clipRect, Op.REPLACE);
    183         draw(destCanvas);
    184         destCanvas.restore();
    185     }
    186 
    187     /**
    188      * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
    189      * Responsibility for the bitmap is transferred to the caller.
    190      */
    191     private Bitmap createGlowingOutline(Canvas canvas, int outlineColor, int glowColor) {
    192         final int padding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS;
    193         final Bitmap b = Bitmap.createBitmap(
    194                 getWidth() + padding, getHeight() + padding, Bitmap.Config.ARGB_8888);
    195 
    196         canvas.setBitmap(b);
    197         drawWithPadding(canvas, padding);
    198         mOutlineHelper.applyExtraThickExpensiveOutlineWithBlur(b, canvas, glowColor, outlineColor);
    199         canvas.setBitmap(null);
    200 
    201         return b;
    202     }
    203 
    204     @Override
    205     public boolean onTouchEvent(MotionEvent event) {
    206         // Call the superclass onTouchEvent first, because sometimes it changes the state to
    207         // isPressed() on an ACTION_UP
    208         boolean result = super.onTouchEvent(event);
    209 
    210         switch (event.getAction()) {
    211             case MotionEvent.ACTION_DOWN:
    212                 // So that the pressed outline is visible immediately when isPressed() is true,
    213                 // we pre-create it on ACTION_DOWN (it takes a small but perceptible amount of time
    214                 // to create it)
    215                 if (mPressedOrFocusedBackground == null) {
    216                     mPressedOrFocusedBackground = createGlowingOutline(
    217                             mTempCanvas, mPressedGlowColor, mPressedOutlineColor);
    218                 }
    219                 // Invalidate so the pressed state is visible, or set a flag so we know that we
    220                 // have to call invalidate as soon as the state is "pressed"
    221                 if (isPressed()) {
    222                     mDidInvalidateForPressedState = true;
    223                     setCellLayoutPressedOrFocusedIcon();
    224                 } else {
    225                     mDidInvalidateForPressedState = false;
    226                 }
    227 
    228                 mLongPressHelper.postCheckForLongPress();
    229                 break;
    230             case MotionEvent.ACTION_CANCEL:
    231             case MotionEvent.ACTION_UP:
    232                 // If we've touched down and up on an item, and it's still not "pressed", then
    233                 // destroy the pressed outline
    234                 if (!isPressed()) {
    235                     mPressedOrFocusedBackground = null;
    236                 }
    237 
    238                 mLongPressHelper.cancelLongPress();
    239                 break;
    240         }
    241         return result;
    242     }
    243 
    244     void setStayPressed(boolean stayPressed) {
    245         mStayPressed = stayPressed;
    246         if (!stayPressed) {
    247             mPressedOrFocusedBackground = null;
    248         }
    249         setCellLayoutPressedOrFocusedIcon();
    250     }
    251 
    252     void setCellLayoutPressedOrFocusedIcon() {
    253         if (getParent() instanceof ShortcutAndWidgetContainer) {
    254             ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) getParent();
    255             if (parent != null) {
    256                 CellLayout layout = (CellLayout) parent.getParent();
    257                 layout.setPressedOrFocusedIcon((mPressedOrFocusedBackground != null) ? this : null);
    258             }
    259         }
    260     }
    261 
    262     void clearPressedOrFocusedBackground() {
    263         mPressedOrFocusedBackground = null;
    264         setCellLayoutPressedOrFocusedIcon();
    265     }
    266 
    267     Bitmap getPressedOrFocusedBackground() {
    268         return mPressedOrFocusedBackground;
    269     }
    270 
    271     int getPressedOrFocusedBackgroundPadding() {
    272         return HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS / 2;
    273     }
    274 
    275     @Override
    276     public void draw(Canvas canvas) {
    277         final Drawable background = mBackground;
    278         if (background != null) {
    279             final int scrollX = getScrollX();
    280             final int scrollY = getScrollY();
    281 
    282             if (mBackgroundSizeChanged) {
    283                 background.setBounds(0, 0,  getRight() - getLeft(), getBottom() - getTop());
    284                 mBackgroundSizeChanged = false;
    285             }
    286 
    287             if ((scrollX | scrollY) == 0) {
    288                 background.draw(canvas);
    289             } else {
    290                 canvas.translate(scrollX, scrollY);
    291                 background.draw(canvas);
    292                 canvas.translate(-scrollX, -scrollY);
    293             }
    294         }
    295 
    296         // If text is transparent, don't draw any shadow
    297         if (getCurrentTextColor() == getResources().getColor(android.R.color.transparent)) {
    298             getPaint().clearShadowLayer();
    299             super.draw(canvas);
    300             return;
    301         }
    302 
    303         // We enhance the shadow by drawing the shadow twice
    304         getPaint().setShadowLayer(SHADOW_LARGE_RADIUS, 0.0f, SHADOW_Y_OFFSET, SHADOW_LARGE_COLOUR);
    305         super.draw(canvas);
    306         canvas.save(Canvas.CLIP_SAVE_FLAG);
    307         canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(),
    308                 getScrollX() + getWidth(),
    309                 getScrollY() + getHeight(), Region.Op.INTERSECT);
    310         getPaint().setShadowLayer(SHADOW_SMALL_RADIUS, 0.0f, 0.0f, SHADOW_SMALL_COLOUR);
    311         super.draw(canvas);
    312         canvas.restore();
    313     }
    314 
    315     @Override
    316     protected void onAttachedToWindow() {
    317         super.onAttachedToWindow();
    318         if (mBackground != null) mBackground.setCallback(this);
    319     }
    320 
    321     @Override
    322     protected void onDetachedFromWindow() {
    323         super.onDetachedFromWindow();
    324         if (mBackground != null) mBackground.setCallback(null);
    325     }
    326 
    327     @Override
    328     protected boolean onSetAlpha(int alpha) {
    329         if (mPrevAlpha != alpha) {
    330             mPrevAlpha = alpha;
    331             super.onSetAlpha(alpha);
    332         }
    333         return true;
    334     }
    335 
    336     @Override
    337     public void cancelLongPress() {
    338         super.cancelLongPress();
    339 
    340         mLongPressHelper.cancelLongPress();
    341     }
    342 }
    343