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    public void jumpDrawablesToCurrentState() {
76        super.jumpDrawablesToCurrentState();
77        if (mBackground != null) mBackground.jumpToCurrentState();
78    }
79
80    @Override
81    protected void drawableStateChanged() {
82        Drawable d = mBackground;
83        if (d != null && d.isStateful()) {
84            d.setState(getDrawableState());
85        }
86        super.drawableStateChanged();
87    }
88
89    @Override
90    public void draw(Canvas canvas) {
91        final Drawable background = mBackground;
92        if (background != null) {
93            if (mBackgroundSizeChanged) {
94                mBackgroundSizeChanged = false;
95                Rect chld = mTmp0;
96                Rect bkg = mTmp1;
97                mBackground.getPadding(bkg);
98                getChildBounds(chld);
99                // This doesn't clamp to this view's bounds, which is what we want,
100                // so that the drawing is clipped.
101                final int top = chld.top - bkg.top;
102                final int bottom = chld.bottom + bkg.bottom;
103                // The background here is a gradient that wants to
104                // extend the full width of the screen (whatever that
105                // may be).
106                int left, right;
107                if (false) {
108                    // This limits the width of the drawable.
109                    left = chld.left - bkg.left;
110                    right = chld.right + bkg.right;
111                } else {
112                    // This expands it to full width.
113                    left = 0;
114                    right = getRight();
115                }
116                background.setBounds(left, top, right, bottom);
117            }
118        }
119        mBackground.draw(canvas);
120
121        if (false) {
122            android.graphics.Paint p = new android.graphics.Paint();
123            p.setColor(0x88ffff00);
124            canvas.drawRect(background.getBounds(), p);
125        }
126        canvas.drawARGB((int)(0.75*0xff), 0, 0, 0);
127
128        super.draw(canvas);
129    }
130
131    @Override
132    protected void onAttachedToWindow() {
133        super.onAttachedToWindow();
134        mBackground.setCallback(this);
135        setWillNotDraw(false);
136    }
137
138    @Override
139    protected void onDetachedFromWindow() {
140        super.onDetachedFromWindow();
141        mBackground.setCallback(null);
142    }
143
144    private void getChildBounds(Rect r) {
145        r.left = r.top = Integer.MAX_VALUE;
146        r.bottom = r.right = Integer.MIN_VALUE;
147        final int N = getChildCount();
148        for (int i=0; i<N; i++) {
149            View v = getChildAt(i);
150            if (v.getVisibility() == View.VISIBLE) {
151                r.left = Math.min(r.left, v.getLeft());
152                r.top = Math.min(r.top, v.getTop());
153                r.right = Math.max(r.right, v.getRight());
154                r.bottom = Math.max(r.bottom, v.getBottom());
155            }
156        }
157    }
158}
159