RecentApplicationsBackground.java revision be81f4f15dad6d690efcab1973d1e174ce3b001b
1/* 2 * Copyright (C) 2010 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 17package com.android.internal.policy.impl; 18 19import android.content.Context; 20import android.graphics.Canvas; 21import android.graphics.Rect; 22import android.graphics.drawable.Drawable; 23import android.util.AttributeSet; 24import android.util.Log; 25import android.view.Gravity; 26import android.view.View; 27import android.widget.LinearLayout; 28 29/** 30 * A vertical linear layout. However, instead of drawing the background 31 * behnd the items, it draws the background outside the items based on the 32 * padding. If there isn't enough room to draw both, it clips the background 33 * instead of the contents. 34 */ 35public class RecentApplicationsBackground extends LinearLayout { 36 private static final String TAG = "RecentApplicationsBackground"; 37 38 private boolean mBackgroundSizeChanged; 39 private Drawable mBackground; 40 private Rect mTmp0 = new Rect(); 41 private Rect mTmp1 = new Rect(); 42 43 public RecentApplicationsBackground(Context context) { 44 this(context, null); 45 init(); 46 } 47 48 public RecentApplicationsBackground(Context context, AttributeSet attrs) { 49 super(context, attrs); 50 init(); 51 } 52 53 private void init() { 54 mBackground = getBackground(); 55 setBackgroundDrawable(null); 56 setPadding(0, 0, 0, 0); 57 setGravity(Gravity.CENTER); 58 } 59 60 @Override 61 protected boolean setFrame(int left, int top, int right, int bottom) { 62 setWillNotDraw(false); 63 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) { 64 mBackgroundSizeChanged = true; 65 } 66 return super.setFrame(left, top, right, bottom); 67 } 68 69 @Override 70 protected boolean verifyDrawable(Drawable who) { 71 return who == mBackground || super.verifyDrawable(who); 72 } 73 74 @Override 75 protected void drawableStateChanged() { 76 Drawable d = mBackground; 77 if (d != null && d.isStateful()) { 78 d.setState(getDrawableState()); 79 } 80 super.drawableStateChanged(); 81 } 82 83 @Override 84 public void draw(Canvas canvas) { 85 final Drawable background = mBackground; 86 if (background != null) { 87 if (mBackgroundSizeChanged) { 88 mBackgroundSizeChanged = false; 89 Rect chld = mTmp0; 90 Rect bkg = mTmp1; 91 mBackground.getPadding(bkg); 92 getChildBounds(chld); 93 // This doesn't clamp to this view's bounds, which is what we want, 94 // so that the drawing is clipped. 95 final int top = chld.top - bkg.top; 96 final int bottom = chld.bottom + bkg.bottom; 97 // The background here is a gradient that wants to 98 // extend the full width of the screen (whatever that 99 // may be). 100 int left, right; 101 if (false) { 102 // This limits the width of the drawable. 103 left = chld.left - bkg.left; 104 right = chld.right + bkg.right; 105 } else { 106 // This expands it to full width. 107 left = 0; 108 right = getRight(); 109 } 110 background.setBounds(left, top, right, bottom); 111 } 112 } 113 mBackground.draw(canvas); 114 115 if (false) { 116 android.graphics.Paint p = new android.graphics.Paint(); 117 p.setColor(0x88ffff00); 118 canvas.drawRect(background.getBounds(), p); 119 } 120 canvas.drawARGB((int)(0.75*0xff), 0, 0, 0); 121 122 super.draw(canvas); 123 } 124 125 @Override 126 protected void onAttachedToWindow() { 127 super.onAttachedToWindow(); 128 mBackground.setCallback(this); 129 setWillNotDraw(false); 130 } 131 132 @Override 133 protected void onDetachedFromWindow() { 134 super.onDetachedFromWindow(); 135 mBackground.setCallback(null); 136 } 137 138 private void getChildBounds(Rect r) { 139 r.left = r.top = Integer.MAX_VALUE; 140 r.bottom = r.right = Integer.MIN_VALUE; 141 final int N = getChildCount(); 142 for (int i=0; i<N; i++) { 143 View v = getChildAt(i); 144 if (v.getVisibility() == View.VISIBLE) { 145 r.left = Math.min(r.left, v.getLeft()); 146 r.top = Math.min(r.top, v.getTop()); 147 r.right = Math.max(r.right, v.getRight()); 148 r.bottom = Math.max(r.bottom, v.getBottom()); 149 } 150 } 151 } 152} 153