blob: cdf43812af1667b0b74fb68ae7382d62ce1de6f2 [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;
Winson Chung62054222011-11-03 13:12:50 -070020import android.content.res.TypedArray;
Winson Chung7d3810d2011-10-07 13:11:56 -070021import android.graphics.Canvas;
Winson Chung62054222011-11-03 13:12:50 -070022import 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 Chung62054222011-11-03 13:12:50 -070029import android.widget.ImageView;
Winson Chung7d3810d2011-10-07 13:11:56 -070030import android.widget.LinearLayout;
31
32public class HolographicLinearLayout extends LinearLayout {
Winson Chung7d3810d2011-10-07 13:11:56 -070033 private final HolographicViewHelper mHolographicHelper;
Winson Chung62054222011-11-03 13:12:50 -070034 private ImageView mImageView;
35 private int mImageViewId;
Winson Chung7d3810d2011-10-07 13:11:56 -070036
Cristina Stancud7919982013-08-16 11:15:40 +010037 private boolean mHotwordOn;
38 private boolean mIsPressed;
39 private boolean mIsFocused;
40
Winson Chung7d3810d2011-10-07 13:11:56 -070041 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 Chung62054222011-11-03 13:12:50 -070052 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HolographicLinearLayout,
53 defStyle, 0);
54 mImageViewId = a.getResourceId(R.styleable.HolographicLinearLayout_sourceImageViewId, -1);
Cristina Stancud7919982013-08-16 11:15:40 +010055 mHotwordOn = a.getBoolean(R.styleable.HolographicLinearLayout_stateHotwordOn, false);
Winson Chung62054222011-11-03 13:12:50 -070056 a.recycle();
57
Cristina Stancud7919982013-08-16 11:15:40 +010058
Winson Chung7d3810d2011-10-07 13:11:56 -070059 setWillNotDraw(false);
60 mHolographicHelper = new HolographicViewHelper(context);
Cristina Stancud7919982013-08-16 11:15:40 +010061
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 Chung7d3810d2011-10-07 13:11:56 -070082 }
83
84 @Override
Winson Chung62054222011-11-03 13:12:50 -070085 protected void drawableStateChanged() {
86 super.drawableStateChanged();
87
88 if (mImageView != null) {
Cristina Stancud7919982013-08-16 11:15:40 +010089 mHolographicHelper.generatePressedFocusedStates(mImageView);
Winson Chung62054222011-11-03 13:12:50 -070090 Drawable d = mImageView.getDrawable();
91 if (d instanceof StateListDrawable) {
92 StateListDrawable sld = (StateListDrawable) d;
93 sld.setState(getDrawableState());
Cristina Stancud7919982013-08-16 11:15:40 +010094 sld.invalidateSelf();
Winson Chung62054222011-11-03 13:12:50 -070095 }
96 }
97 }
98
Winson Chungbb185bd2011-11-21 12:31:42 -080099 void invalidatePressedFocusedStates() {
100 mHolographicHelper.invalidatePressedFocusedStates(mImageView);
Winson Chung7cc3c7f2012-06-07 15:18:57 -0700101 invalidate();
Winson Chungbb185bd2011-11-21 12:31:42 -0800102 }
103
Winson Chung62054222011-11-03 13:12:50 -0700104 @Override
Winson Chung7d3810d2011-10-07 13:11:56 -0700105 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 Chung62054222011-11-03 13:12:50 -0700110 if (mImageView == null) {
111 mImageView = (ImageView) findViewById(mImageViewId);
112 }
113 mHolographicHelper.generatePressedFocusedStates(mImageView);
Winson Chung7d3810d2011-10-07 13:11:56 -0700114 }
Cristina Stancud7919982013-08-16 11:15:40 +0100115
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 Chung7d3810d2011-10-07 13:11:56 -0700136}