Home | History | Annotate | Download | only in launcher3
      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.launcher3;
     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.view.MotionEvent;
     26 import android.view.View;
     27 import android.widget.ImageView;
     28 import android.widget.LinearLayout;
     29 
     30 public class HolographicLinearLayout extends LinearLayout {
     31     private final HolographicViewHelper mHolographicHelper;
     32     private ImageView mImageView;
     33     private int mImageViewId;
     34 
     35     private boolean mHotwordOn;
     36     private boolean mIsPressed;
     37     private boolean mIsFocused;
     38 
     39     public HolographicLinearLayout(Context context) {
     40         this(context, null);
     41     }
     42 
     43     public HolographicLinearLayout(Context context, AttributeSet attrs) {
     44         this(context, attrs, 0);
     45     }
     46 
     47     public HolographicLinearLayout(Context context, AttributeSet attrs, int defStyle) {
     48         super(context, attrs, defStyle);
     49 
     50         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HolographicLinearLayout,
     51                 defStyle, 0);
     52         mImageViewId = a.getResourceId(R.styleable.HolographicLinearLayout_sourceImageViewId, -1);
     53         mHotwordOn = a.getBoolean(R.styleable.HolographicLinearLayout_stateHotwordOn, false);
     54         a.recycle();
     55 
     56 
     57         setWillNotDraw(false);
     58         mHolographicHelper = new HolographicViewHelper(context);
     59 
     60         setOnTouchListener(new OnTouchListener() {
     61             @Override
     62             public boolean onTouch(View v, MotionEvent event) {
     63                 if (isPressed() != mIsPressed) {
     64                     mIsPressed = isPressed();
     65                     refreshDrawableState();
     66                 }
     67                 return false;
     68             }
     69         });
     70 
     71         setOnFocusChangeListener(new OnFocusChangeListener() {
     72             @Override
     73             public void onFocusChange(View v, boolean hasFocus) {
     74                 if (isFocused() != mIsFocused) {
     75                     mIsFocused = isFocused();
     76                     refreshDrawableState();
     77                 }
     78             }
     79         });
     80     }
     81 
     82     @Override
     83     protected void drawableStateChanged() {
     84         super.drawableStateChanged();
     85 
     86         if (mImageView != null) {
     87             mHolographicHelper.generatePressedFocusedStates(mImageView);
     88             Drawable d = mImageView.getDrawable();
     89             if (d instanceof StateListDrawable) {
     90                 StateListDrawable sld = (StateListDrawable) d;
     91                 sld.setState(getDrawableState());
     92                 sld.invalidateSelf();
     93             }
     94         }
     95     }
     96 
     97     void invalidatePressedFocusedStates() {
     98         mHolographicHelper.invalidatePressedFocusedStates(mImageView);
     99         invalidate();
    100     }
    101 
    102     @Override
    103     protected void onDraw(Canvas canvas) {
    104         super.onDraw(canvas);
    105 
    106         // One time call to generate the pressed/focused state -- must be called after
    107         // measure/layout
    108         if (mImageView == null) {
    109             mImageView = (ImageView) findViewById(mImageViewId);
    110         }
    111         mHolographicHelper.generatePressedFocusedStates(mImageView);
    112     }
    113 
    114     private boolean isHotwordOn() {
    115         return mHotwordOn;
    116     }
    117 
    118     public void setHotwordState(boolean on) {
    119         if (on == mHotwordOn) {
    120             return;
    121         }
    122         mHotwordOn = on;
    123         refreshDrawableState();
    124     }
    125 
    126     @Override
    127     public int[] onCreateDrawableState(int extraSpace) {
    128         final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
    129         if (isHotwordOn()) {
    130             mergeDrawableStates(drawableState, new int[] {R.attr.stateHotwordOn});
    131         }
    132         return drawableState;
    133     }
    134 }
    135