SpeechOrbView.java revision 49bd8747a667ae5f45a132d803cc18a80e963545
1package android.support.v17.leanback.widget; 2 3import android.content.Context; 4import android.content.res.Resources; 5import android.graphics.Color; 6import android.support.v17.leanback.R; 7import android.util.AttributeSet; 8 9public class SpeechOrbView extends SearchOrbView { 10 private final float mSoundLevelMaxZoom; 11 private final Colors mListeningOrbColors; 12 private final Colors mNotListeningOrbColors; 13 14 private int mCurrentLevel = 0; 15 private boolean mListening = false; 16 17 public SpeechOrbView(Context context) { 18 this(context, null); 19 } 20 21 public SpeechOrbView(Context context, AttributeSet attrs) { 22 this(context, attrs, 0); 23 } 24 25 public SpeechOrbView(Context context, AttributeSet attrs, int defStyle) { 26 super(context, attrs, defStyle); 27 28 Resources resources = context.getResources(); 29 mSoundLevelMaxZoom = 30 resources.getFraction(R.fraction.lb_search_bar_speech_orb_max_level_zoom, 1, 1); 31 32 mNotListeningOrbColors = new Colors(resources.getColor(R.color.lb_speech_orb_not_recording), 33 resources.getColor(R.color.lb_speech_orb_not_recording_pulsed), 34 resources.getColor(R.color.lb_speech_orb_not_recording_icon)); 35 mListeningOrbColors = new Colors(resources.getColor(R.color.lb_speech_orb_recording), 36 resources.getColor(R.color.lb_speech_orb_recording), 37 Color.TRANSPARENT); 38 39 showNotListening(); 40 } 41 42 @Override 43 int getLayoutResourceId() { 44 return R.layout.lb_speech_orb; 45 } 46 47 public void showListening() { 48 setOrbColors(mListeningOrbColors); 49 setOrbIcon(getResources().getDrawable(R.drawable.lb_ic_search_mic)); 50 // Assume focused 51 animateOnFocus(true); 52 enableOrbColorAnimation(false); 53 scaleOrbViewOnly(1f); 54 mListening = true; 55 } 56 57 public void showNotListening() { 58 setOrbColors(mNotListeningOrbColors); 59 setOrbIcon(getResources().getDrawable(R.drawable.lb_ic_search_mic_out)); 60 animateOnFocus(hasFocus()); 61 scaleOrbViewOnly(1f); 62 mListening = false; 63 } 64 65 public void setSoundLevel(int level) { 66 if (!mListening) return; 67 68 // Either ease towards the target level, or decay away from it depending on whether 69 // its higher or lower than the current. 70 if (level > mCurrentLevel) { 71 mCurrentLevel = mCurrentLevel + ((level - mCurrentLevel) / 2); 72 } else { 73 mCurrentLevel = (int) (mCurrentLevel * 0.7f); 74 } 75 76 float zoom = getFocusedZoom() + 77 ((mSoundLevelMaxZoom - getFocusedZoom()) * mCurrentLevel) / 100; 78 79 scaleOrbViewOnly(zoom); 80 } 81} 82