1 /* 2 * Copyright (C) 2020 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.systemui.car.voicerecognition; 18 19 import android.bluetooth.BluetoothHeadsetClient; 20 import android.content.BroadcastReceiver; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.IntentFilter; 24 import android.os.Handler; 25 import android.os.UserHandle; 26 import android.util.Log; 27 import android.widget.Toast; 28 29 import com.android.internal.annotations.VisibleForTesting; 30 import com.android.systemui.R; 31 import com.android.systemui.SysUIToast; 32 import com.android.systemui.SystemUI; 33 import com.android.systemui.dagger.qualifiers.Main; 34 35 import javax.inject.Inject; 36 37 /** 38 * Controller responsible for showing toast message when voice recognition over bluetooth device 39 * getting activated. 40 */ 41 public class ConnectedDeviceVoiceRecognitionNotifier extends SystemUI { 42 43 private static final String TAG = "CarVoiceRecognition"; 44 @VisibleForTesting 45 static final int INVALID_VALUE = -1; 46 @VisibleForTesting 47 static final int VOICE_RECOGNITION_STARTED = 1; 48 49 private Handler mHandler; 50 51 private final BroadcastReceiver mVoiceRecognitionReceiver = new BroadcastReceiver() { 52 @Override 53 public void onReceive(Context context, Intent intent) { 54 if (Log.isLoggable(TAG, Log.DEBUG)) { 55 Log.d(TAG, "Voice recognition received an intent!"); 56 } 57 if (intent == null 58 || intent.getAction() == null 59 || !BluetoothHeadsetClient.ACTION_AG_EVENT.equals(intent.getAction()) 60 || !intent.hasExtra(BluetoothHeadsetClient.EXTRA_VOICE_RECOGNITION)) { 61 return; 62 } 63 64 int voiceRecognitionState = intent.getIntExtra( 65 BluetoothHeadsetClient.EXTRA_VOICE_RECOGNITION, INVALID_VALUE); 66 67 if (voiceRecognitionState == VOICE_RECOGNITION_STARTED) { 68 showToastMessage(); 69 } 70 } 71 }; 72 showToastMessage()73 private void showToastMessage() { 74 mHandler.post(() -> SysUIToast.makeText(mContext, R.string.voice_recognition_toast, 75 Toast.LENGTH_LONG).show()); 76 } 77 78 @Inject ConnectedDeviceVoiceRecognitionNotifier(Context context, @Main Handler handler)79 public ConnectedDeviceVoiceRecognitionNotifier(Context context, @Main Handler handler) { 80 super(context); 81 mHandler = handler; 82 } 83 84 @Override start()85 public void start() { 86 } 87 88 @Override onBootCompleted()89 protected void onBootCompleted() { 90 IntentFilter filter = new IntentFilter(); 91 filter.addAction(BluetoothHeadsetClient.ACTION_AG_EVENT); 92 mContext.registerReceiverAsUser(mVoiceRecognitionReceiver, UserHandle.ALL, filter, 93 /* broadcastPermission= */ null, /* scheduler= */ null); 94 } 95 } 96