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 17 package com.android.launcher3; 18 19 import android.content.Context; 20 import android.content.res.TypedArray; 21 import android.graphics.Canvas; 22 import android.graphics.drawable.Drawable; 23 import android.graphics.drawable.StateListDrawable; 24 import android.util.AttributeSet; 25 import android.view.MotionEvent; 26 import android.view.View; 27 import android.widget.ImageView; 28 29 public class HolographicImageView extends ImageView { 30 31 private final HolographicViewHelper mHolographicHelper; 32 private boolean mHotwordOn; 33 private boolean mIsPressed; 34 private boolean mIsFocused; 35 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); 48 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 }); 73 } 74 75 void invalidatePressedFocusedStates() { 76 mHolographicHelper.invalidatePressedFocusedStates(this); 77 } 78 79 @Override 80 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 93 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 } 100 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 } 121 } 122