blob: 5344a7ee9ab2553ad69808d913ad872ff299ee88 [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;
Winson Chung62054222011-11-03 13:12:50 -070027import android.widget.ImageView;
Winson Chung7d3810d2011-10-07 13:11:56 -070028import android.widget.LinearLayout;
29
30public class HolographicLinearLayout extends LinearLayout {
Winson Chung7d3810d2011-10-07 13:11:56 -070031 private final HolographicViewHelper mHolographicHelper;
Winson Chung62054222011-11-03 13:12:50 -070032 private ImageView mImageView;
33 private int mImageViewId;
Winson Chung7d3810d2011-10-07 13:11:56 -070034
Cristina Stancud7919982013-08-16 11:15:40 +010035 private boolean mHotwordOn;
36 private boolean mIsPressed;
37 private boolean mIsFocused;
38
Winson Chung7d3810d2011-10-07 13:11:56 -070039 public HolographicLinearLayout(Context context) {
40 this(context, null);
41 }
42
43 public HolographicLinearLayout(Context context, AttributeSet attrs) {
44 this(context, attrs, 0);
45 }
46
47 public HolographicLinearLayout(Context context, AttributeSet attrs, int defStyle) {
48 super(context, attrs, defStyle);
49
Winson Chung62054222011-11-03 13:12:50 -070050 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HolographicLinearLayout,
51 defStyle, 0);
52 mImageViewId = a.getResourceId(R.styleable.HolographicLinearLayout_sourceImageViewId, -1);
Cristina Stancud7919982013-08-16 11:15:40 +010053 mHotwordOn = a.getBoolean(R.styleable.HolographicLinearLayout_stateHotwordOn, false);
Winson Chung62054222011-11-03 13:12:50 -070054 a.recycle();
55
Cristina Stancud7919982013-08-16 11:15:40 +010056
Winson Chung7d3810d2011-10-07 13:11:56 -070057 setWillNotDraw(false);
58 mHolographicHelper = new HolographicViewHelper(context);
Cristina Stancud7919982013-08-16 11:15:40 +010059
60 setOnTouchListener(new OnTouchListener() {
61 @Override
62 public boolean onTouch(View v, MotionEvent event) {
63 if (isPressed() != mIsPressed) {
64 mIsPressed = isPressed();
65 refreshDrawableState();
66 }
67 return false;
68 }
69 });
70
71 setOnFocusChangeListener(new OnFocusChangeListener() {
72 @Override
73 public void onFocusChange(View v, boolean hasFocus) {
74 if (isFocused() != mIsFocused) {
75 mIsFocused = isFocused();
76 refreshDrawableState();
77 }
78 }
79 });
Winson Chung7d3810d2011-10-07 13:11:56 -070080 }
81
82 @Override
Winson Chung62054222011-11-03 13:12:50 -070083 protected void drawableStateChanged() {
84 super.drawableStateChanged();
85
86 if (mImageView != null) {
Cristina Stancud7919982013-08-16 11:15:40 +010087 mHolographicHelper.generatePressedFocusedStates(mImageView);
Winson Chung62054222011-11-03 13:12:50 -070088 Drawable d = mImageView.getDrawable();
89 if (d instanceof StateListDrawable) {
90 StateListDrawable sld = (StateListDrawable) d;
91 sld.setState(getDrawableState());
Cristina Stancud7919982013-08-16 11:15:40 +010092 sld.invalidateSelf();
Winson Chung62054222011-11-03 13:12:50 -070093 }
94 }
95 }
96
Winson Chungbb185bd2011-11-21 12:31:42 -080097 void invalidatePressedFocusedStates() {
98 mHolographicHelper.invalidatePressedFocusedStates(mImageView);
Winson Chung7cc3c7f2012-06-07 15:18:57 -070099 invalidate();
Winson Chungbb185bd2011-11-21 12:31:42 -0800100 }
101
Winson Chung62054222011-11-03 13:12:50 -0700102 @Override
Winson Chung7d3810d2011-10-07 13:11:56 -0700103 protected void onDraw(Canvas canvas) {
104 super.onDraw(canvas);
105
106 // One time call to generate the pressed/focused state -- must be called after
107 // measure/layout
Winson Chung62054222011-11-03 13:12:50 -0700108 if (mImageView == null) {
109 mImageView = (ImageView) findViewById(mImageViewId);
110 }
111 mHolographicHelper.generatePressedFocusedStates(mImageView);
Winson Chung7d3810d2011-10-07 13:11:56 -0700112 }
Cristina Stancud7919982013-08-16 11:15:40 +0100113
114 private boolean isHotwordOn() {
115 return mHotwordOn;
116 }
117
118 public void setHotwordState(boolean on) {
119 if (on == mHotwordOn) {
120 return;
121 }
122 mHotwordOn = on;
123 refreshDrawableState();
124 }
125
126 @Override
127 public int[] onCreateDrawableState(int extraSpace) {
128 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
129 if (isHotwordOn()) {
130 mergeDrawableStates(drawableState, new int[] {R.attr.stateHotwordOn});
131 }
132 return drawableState;
133 }
Winson Chung7d3810d2011-10-07 13:11:56 -0700134}