17d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung/*
27d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung * Copyright (C) 2011 The Android Open Source Project
37d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung *
47d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung * Licensed under the Apache License, Version 2.0 (the "License");
57d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung * you may not use this file except in compliance with the License.
67d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung * You may obtain a copy of the License at
77d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung *
87d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung *      http://www.apache.org/licenses/LICENSE-2.0
97d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung *
107d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung * Unless required by applicable law or agreed to in writing, software
117d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung * distributed under the License is distributed on an "AS IS" BASIS,
127d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung * See the License for the specific language governing permissions and
147d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung * limitations under the License.
157d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung */
167d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung
177d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chungpackage com.android.launcher2;
187d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung
197d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chungimport android.content.Context;
20620542254c9b63cd17d2deefb07ffd39d8a54623Winson Chungimport android.content.res.TypedArray;
217d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chungimport android.graphics.Canvas;
22620542254c9b63cd17d2deefb07ffd39d8a54623Winson Chungimport android.graphics.drawable.Drawable;
23620542254c9b63cd17d2deefb07ffd39d8a54623Winson Chungimport android.graphics.drawable.StateListDrawable;
247d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chungimport android.util.AttributeSet;
25620542254c9b63cd17d2deefb07ffd39d8a54623Winson Chungimport android.widget.ImageView;
267d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chungimport android.widget.LinearLayout;
277d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung
28620542254c9b63cd17d2deefb07ffd39d8a54623Winson Chungimport com.android.launcher.R;
29620542254c9b63cd17d2deefb07ffd39d8a54623Winson Chung
307d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chungpublic class HolographicLinearLayout extends LinearLayout {
317d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung
327d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung    private final HolographicViewHelper mHolographicHelper;
33620542254c9b63cd17d2deefb07ffd39d8a54623Winson Chung    private ImageView mImageView;
34620542254c9b63cd17d2deefb07ffd39d8a54623Winson Chung    private int mImageViewId;
357d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung
367d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung    public HolographicLinearLayout(Context context) {
377d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung        this(context, null);
387d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung    }
397d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung
407d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung    public HolographicLinearLayout(Context context, AttributeSet attrs) {
417d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung        this(context, attrs, 0);
427d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung    }
437d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung
447d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung    public HolographicLinearLayout(Context context, AttributeSet attrs, int defStyle) {
457d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung        super(context, attrs, defStyle);
467d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung
47620542254c9b63cd17d2deefb07ffd39d8a54623Winson Chung        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HolographicLinearLayout,
48620542254c9b63cd17d2deefb07ffd39d8a54623Winson Chung                defStyle, 0);
49620542254c9b63cd17d2deefb07ffd39d8a54623Winson Chung        mImageViewId = a.getResourceId(R.styleable.HolographicLinearLayout_sourceImageViewId, -1);
50620542254c9b63cd17d2deefb07ffd39d8a54623Winson Chung        a.recycle();
51620542254c9b63cd17d2deefb07ffd39d8a54623Winson Chung
527d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung        setWillNotDraw(false);
537d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung        mHolographicHelper = new HolographicViewHelper(context);
547d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung    }
557d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung
567d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung    @Override
57620542254c9b63cd17d2deefb07ffd39d8a54623Winson Chung    protected void drawableStateChanged() {
58620542254c9b63cd17d2deefb07ffd39d8a54623Winson Chung        super.drawableStateChanged();
59620542254c9b63cd17d2deefb07ffd39d8a54623Winson Chung
60620542254c9b63cd17d2deefb07ffd39d8a54623Winson Chung        if (mImageView != null) {
61620542254c9b63cd17d2deefb07ffd39d8a54623Winson Chung            Drawable d = mImageView.getDrawable();
62620542254c9b63cd17d2deefb07ffd39d8a54623Winson Chung            if (d instanceof StateListDrawable) {
63620542254c9b63cd17d2deefb07ffd39d8a54623Winson Chung                StateListDrawable sld = (StateListDrawable) d;
64620542254c9b63cd17d2deefb07ffd39d8a54623Winson Chung                sld.setState(getDrawableState());
65620542254c9b63cd17d2deefb07ffd39d8a54623Winson Chung            }
66620542254c9b63cd17d2deefb07ffd39d8a54623Winson Chung        }
67620542254c9b63cd17d2deefb07ffd39d8a54623Winson Chung    }
68620542254c9b63cd17d2deefb07ffd39d8a54623Winson Chung
69bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung    void invalidatePressedFocusedStates() {
70bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung        mHolographicHelper.invalidatePressedFocusedStates(mImageView);
717cc3c7f88a5f16b5592333d5be5ba57aa71be8d9Winson Chung        invalidate();
72bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung    }
73bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung
74620542254c9b63cd17d2deefb07ffd39d8a54623Winson Chung    @Override
757d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung    protected void onDraw(Canvas canvas) {
767d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung        super.onDraw(canvas);
777d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung
787d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung        // One time call to generate the pressed/focused state -- must be called after
797d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung        // measure/layout
80620542254c9b63cd17d2deefb07ffd39d8a54623Winson Chung        if (mImageView == null) {
81620542254c9b63cd17d2deefb07ffd39d8a54623Winson Chung            mImageView = (ImageView) findViewById(mImageViewId);
82620542254c9b63cd17d2deefb07ffd39d8a54623Winson Chung        }
83620542254c9b63cd17d2deefb07ffd39d8a54623Winson Chung        mHolographicHelper.generatePressedFocusedStates(mImageView);
847d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung    }
857d3810d8337e4f030796b566a2d562fcf79b0a22Winson Chung}
86