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