1/*
2 * Copyright (C) 2011 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.launcher2;
18
19import android.content.Context;
20import android.content.res.TypedArray;
21import android.graphics.Canvas;
22import android.graphics.drawable.Drawable;
23import android.graphics.drawable.StateListDrawable;
24import android.util.AttributeSet;
25import android.widget.ImageView;
26import android.widget.LinearLayout;
27
28import com.android.launcher.R;
29
30public class HolographicLinearLayout extends LinearLayout {
31
32    private final HolographicViewHelper mHolographicHelper;
33    private ImageView mImageView;
34    private int mImageViewId;
35
36    public HolographicLinearLayout(Context context) {
37        this(context, null);
38    }
39
40    public HolographicLinearLayout(Context context, AttributeSet attrs) {
41        this(context, attrs, 0);
42    }
43
44    public HolographicLinearLayout(Context context, AttributeSet attrs, int defStyle) {
45        super(context, attrs, defStyle);
46
47        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HolographicLinearLayout,
48                defStyle, 0);
49        mImageViewId = a.getResourceId(R.styleable.HolographicLinearLayout_sourceImageViewId, -1);
50        a.recycle();
51
52        setWillNotDraw(false);
53        mHolographicHelper = new HolographicViewHelper(context);
54    }
55
56    @Override
57    protected void drawableStateChanged() {
58        super.drawableStateChanged();
59
60        if (mImageView != null) {
61            Drawable d = mImageView.getDrawable();
62            if (d instanceof StateListDrawable) {
63                StateListDrawable sld = (StateListDrawable) d;
64                sld.setState(getDrawableState());
65            }
66        }
67    }
68
69    void invalidatePressedFocusedStates() {
70        mHolographicHelper.invalidatePressedFocusedStates(mImageView);
71        invalidate();
72    }
73
74    @Override
75    protected void onDraw(Canvas canvas) {
76        super.onDraw(canvas);
77
78        // One time call to generate the pressed/focused state -- must be called after
79        // measure/layout
80        if (mImageView == null) {
81            mImageView = (ImageView) findViewById(mImageViewId);
82        }
83        mHolographicHelper.generatePressedFocusedStates(mImageView);
84    }
85}
86