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