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