blob: ce50db1047b76a3fb19865e5ae482c3696db7ff3 [file] [log] [blame]
Winson Chung7d3810d2011-10-07 13:11:56 -07001/*
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 Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung7d3810d2011-10-07 13:11:56 -070018
19import android.content.Context;
Cristina Stancud7919982013-08-16 11:15:40 +010020import android.content.res.TypedArray;
Winson Chung7d3810d2011-10-07 13:11:56 -070021import android.graphics.Canvas;
Cristina Stancud7919982013-08-16 11:15:40 +010022import android.graphics.drawable.Drawable;
23import android.graphics.drawable.StateListDrawable;
Winson Chung7d3810d2011-10-07 13:11:56 -070024import android.util.AttributeSet;
Cristina Stancud7919982013-08-16 11:15:40 +010025import android.view.MotionEvent;
26import android.view.View;
27import android.view.View.OnFocusChangeListener;
28import android.view.View.OnTouchListener;
Winson Chung7d3810d2011-10-07 13:11:56 -070029import android.widget.ImageView;
30
31public class HolographicImageView extends ImageView {
32
33 private final HolographicViewHelper mHolographicHelper;
Cristina Stancud7919982013-08-16 11:15:40 +010034 private boolean mHotwordOn;
35 private boolean mIsPressed;
36 private boolean mIsFocused;
Winson Chung7d3810d2011-10-07 13:11:56 -070037
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 Stancud7919982013-08-16 11:15:40 +010050 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 Chung7d3810d2011-10-07 13:11:56 -070075 }
76
Winson Chungbb185bd2011-11-21 12:31:42 -080077 void invalidatePressedFocusedStates() {
78 mHolographicHelper.invalidatePressedFocusedStates(this);
79 }
80
Winson Chung7d3810d2011-10-07 13:11:56 -070081 @Override
Cristina Stancud7919982013-08-16 11:15:40 +010082 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 Chung7d3810d2011-10-07 13:11:56 -070095 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 Stancud7919982013-08-16 11:15:40 +0100102
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 Chung7d3810d2011-10-07 13:11:56 -0700123}