1 /** 2 * Copyright (C) 2021 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 package com.android.car.voicecontrol; 17 18 import android.content.Context; 19 import android.os.Handler; 20 import android.util.Log; 21 import android.view.LayoutInflater; 22 import android.view.View; 23 import android.widget.TextView; 24 25 import androidx.annotation.UiThread; 26 27 /** 28 * Controller of voice plate UI 29 */ 30 public class VoicePlateController { 31 private static final String TAG = "Mica.VoicePlatePresenter"; 32 private final Context mContext; 33 private View mVoicePlate; 34 private View mContainer; 35 private TextView mRecognizedText; 36 private TextView mState; 37 private Handler mHandler = new Handler(); 38 39 public enum State { 40 IDLE, 41 LISTENING, 42 LISTENING_ACTIVE, 43 LISTENING_STREAMING, 44 PROCESSING, 45 DELIVERING, 46 } 47 VoicePlateController(Context context, LayoutInflater inflater)48 public VoicePlateController(Context context, 49 LayoutInflater inflater) { 50 mContext = context; 51 mVoicePlate = inflater.inflate(R.layout.voice_plate, null); 52 mState = mVoicePlate.findViewById(R.id.state); 53 mContainer = mVoicePlate.findViewById(R.id.container); 54 mRecognizedText = mVoicePlate.findViewById(R.id.recognized_text); 55 } 56 57 /** 58 * @return root view of voice plate 59 */ getContentView()60 public View getContentView() { 61 return mVoicePlate; 62 } 63 64 /** 65 * Updates voice place state 66 */ updateState(State state)67 public void updateState(State state) { 68 mHandler.post(() -> doUpdateState(state)); 69 } 70 71 @UiThread doUpdateState(State state)72 private void doUpdateState(State state) { 73 Log.d(TAG, "UpdateState: " + state); 74 mState.setText(state.toString()); 75 switch (state) { 76 case IDLE: 77 mVoicePlate.setVisibility(View.GONE); 78 break; 79 case LISTENING: 80 mVoicePlate.setVisibility(View.VISIBLE); 81 mContainer.setBackgroundColor(mContext 82 .getColor(R.color.voice_plate_listening)); 83 break; 84 case LISTENING_ACTIVE: 85 mVoicePlate.setVisibility(View.VISIBLE); 86 mContainer.setBackgroundColor(mContext 87 .getColor(R.color.voice_plate_listening_active)); 88 break; 89 case LISTENING_STREAMING: 90 mVoicePlate.setVisibility(View.VISIBLE); 91 mContainer.setBackgroundColor(mContext 92 .getColor(R.color.voice_plate_partial_recognition)); 93 break; 94 case PROCESSING: 95 mVoicePlate.setVisibility(View.VISIBLE); 96 mContainer.setBackgroundColor(mContext 97 .getColor(R.color.voice_plate_processing)); 98 break; 99 case DELIVERING: 100 mVoicePlate.setVisibility(View.VISIBLE); 101 mContainer.setBackgroundColor(mContext 102 .getColor(R.color.voice_plate_delivering)); 103 break; 104 } 105 } 106 107 /** 108 * Updates recognized text view 109 */ updateRecognizedText(String text)110 public void updateRecognizedText(String text) { 111 Log.d(TAG, "UpdateRecognizedText: " + text); 112 mHandler.post(() -> doUpdateRecognizedText(text)); 113 } 114 115 @UiThread doUpdateRecognizedText(String text)116 private void doUpdateRecognizedText(String text) { 117 mRecognizedText.setText(text); 118 } 119 } 120