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