Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
Winson Chung | 6205422 | 2011-11-03 13:12:50 -0700 | [diff] [blame] | 20 | import android.content.res.TypedArray; |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 21 | import android.graphics.Canvas; |
Winson Chung | 6205422 | 2011-11-03 13:12:50 -0700 | [diff] [blame] | 22 | import android.graphics.drawable.Drawable; |
| 23 | import android.graphics.drawable.StateListDrawable; |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 24 | import android.util.AttributeSet; |
Cristina Stancu | d791998 | 2013-08-16 11:15:40 +0100 | [diff] [blame] | 25 | import android.view.MotionEvent; |
| 26 | import android.view.View; |
Winson Chung | 6205422 | 2011-11-03 13:12:50 -0700 | [diff] [blame] | 27 | import android.widget.ImageView; |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 28 | import android.widget.LinearLayout; |
| 29 | |
| 30 | public class HolographicLinearLayout extends LinearLayout { |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 31 | private final HolographicViewHelper mHolographicHelper; |
Winson Chung | 6205422 | 2011-11-03 13:12:50 -0700 | [diff] [blame] | 32 | private ImageView mImageView; |
| 33 | private int mImageViewId; |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 34 | |
Cristina Stancu | d791998 | 2013-08-16 11:15:40 +0100 | [diff] [blame] | 35 | private boolean mHotwordOn; |
| 36 | private boolean mIsPressed; |
| 37 | private boolean mIsFocused; |
| 38 | |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 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 | |
Winson Chung | 6205422 | 2011-11-03 13:12:50 -0700 | [diff] [blame] | 50 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HolographicLinearLayout, |
| 51 | defStyle, 0); |
| 52 | mImageViewId = a.getResourceId(R.styleable.HolographicLinearLayout_sourceImageViewId, -1); |
Cristina Stancu | d791998 | 2013-08-16 11:15:40 +0100 | [diff] [blame] | 53 | mHotwordOn = a.getBoolean(R.styleable.HolographicLinearLayout_stateHotwordOn, false); |
Winson Chung | 6205422 | 2011-11-03 13:12:50 -0700 | [diff] [blame] | 54 | a.recycle(); |
| 55 | |
Cristina Stancu | d791998 | 2013-08-16 11:15:40 +0100 | [diff] [blame] | 56 | |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 57 | setWillNotDraw(false); |
| 58 | mHolographicHelper = new HolographicViewHelper(context); |
Cristina Stancu | d791998 | 2013-08-16 11:15:40 +0100 | [diff] [blame] | 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 | }); |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
Winson Chung | 6205422 | 2011-11-03 13:12:50 -0700 | [diff] [blame] | 83 | protected void drawableStateChanged() { |
| 84 | super.drawableStateChanged(); |
| 85 | |
| 86 | if (mImageView != null) { |
Cristina Stancu | d791998 | 2013-08-16 11:15:40 +0100 | [diff] [blame] | 87 | mHolographicHelper.generatePressedFocusedStates(mImageView); |
Winson Chung | 6205422 | 2011-11-03 13:12:50 -0700 | [diff] [blame] | 88 | Drawable d = mImageView.getDrawable(); |
| 89 | if (d instanceof StateListDrawable) { |
| 90 | StateListDrawable sld = (StateListDrawable) d; |
| 91 | sld.setState(getDrawableState()); |
Cristina Stancu | d791998 | 2013-08-16 11:15:40 +0100 | [diff] [blame] | 92 | sld.invalidateSelf(); |
Winson Chung | 6205422 | 2011-11-03 13:12:50 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
Winson Chung | bb185bd | 2011-11-21 12:31:42 -0800 | [diff] [blame] | 97 | void invalidatePressedFocusedStates() { |
| 98 | mHolographicHelper.invalidatePressedFocusedStates(mImageView); |
Winson Chung | 7cc3c7f | 2012-06-07 15:18:57 -0700 | [diff] [blame] | 99 | invalidate(); |
Winson Chung | bb185bd | 2011-11-21 12:31:42 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Winson Chung | 6205422 | 2011-11-03 13:12:50 -0700 | [diff] [blame] | 102 | @Override |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 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 |
Winson Chung | 6205422 | 2011-11-03 13:12:50 -0700 | [diff] [blame] | 108 | if (mImageView == null) { |
| 109 | mImageView = (ImageView) findViewById(mImageViewId); |
| 110 | } |
| 111 | mHolographicHelper.generatePressedFocusedStates(mImageView); |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 112 | } |
Cristina Stancu | d791998 | 2013-08-16 11:15:40 +0100 | [diff] [blame] | 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 | } |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 134 | } |