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 
17 package com.android.launcher2;
18 
19 import android.content.Context;
20 import android.content.res.TypedArray;
21 import android.graphics.Canvas;
22 import android.graphics.drawable.Drawable;
23 import android.graphics.drawable.StateListDrawable;
24 import android.util.AttributeSet;
25 import android.widget.ImageView;
26 import android.widget.LinearLayout;
27 
28 import com.android.launcher.R;
29 
30 public class HolographicLinearLayout extends LinearLayout {
31 
32     private final HolographicViewHelper mHolographicHelper;
33     private ImageView mImageView;
34     private int mImageViewId;
35 
HolographicLinearLayout(Context context)36     public HolographicLinearLayout(Context context) {
37         this(context, null);
38     }
39 
HolographicLinearLayout(Context context, AttributeSet attrs)40     public HolographicLinearLayout(Context context, AttributeSet attrs) {
41         this(context, attrs, 0);
42     }
43 
HolographicLinearLayout(Context context, AttributeSet attrs, int defStyle)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
drawableStateChanged()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 
invalidatePressedFocusedStates()69     void invalidatePressedFocusedStates() {
70         mHolographicHelper.invalidatePressedFocusedStates(mImageView);
71         invalidate();
72     }
73 
74     @Override
onDraw(Canvas canvas)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