blob: 18ac092e6cb5c91fa9e7aef0d036b21df0a5800e [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;
Winson Chung7d3810d2011-10-07 13:11:56 -070027import android.widget.ImageView;
28
29public class HolographicImageView extends ImageView {
30
31 private final HolographicViewHelper mHolographicHelper;
Cristina Stancud7919982013-08-16 11:15:40 +010032 private boolean mHotwordOn;
33 private boolean mIsPressed;
34 private boolean mIsFocused;
Winson Chung7d3810d2011-10-07 13:11:56 -070035
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 Stancud7919982013-08-16 11:15:40 +010048 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 Chung7d3810d2011-10-07 13:11:56 -070073 }
74
Winson Chungbb185bd2011-11-21 12:31:42 -080075 void invalidatePressedFocusedStates() {
76 mHolographicHelper.invalidatePressedFocusedStates(this);
77 }
78
Winson Chung7d3810d2011-10-07 13:11:56 -070079 @Override
Cristina Stancud7919982013-08-16 11:15:40 +010080 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 Chung7d3810d2011-10-07 13:11:56 -070093 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 Stancud7919982013-08-16 11:15:40 +0100100
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 Chung7d3810d2011-10-07 13:11:56 -0700121}