1 /* 2 * Copyright (C) 2017 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.google.android.car.kitchensink.assistant; 17 18 import static android.service.voice.VoiceInteractionSession.SHOW_SOURCE_PUSH_TO_TALK; 19 20 import android.app.ActivityManager; 21 import android.content.Context; 22 import android.os.Bundle; 23 import android.view.HapticFeedbackConstants; 24 import android.view.LayoutInflater; 25 import android.view.View; 26 import android.view.ViewGroup; 27 import android.widget.ImageButton; 28 import android.widget.Toast; 29 30 import androidx.annotation.Nullable; 31 import androidx.fragment.app.Fragment; 32 33 import com.android.internal.app.AssistUtils; 34 import com.android.internal.app.IVoiceInteractionSessionShowCallback; 35 36 import com.google.android.car.kitchensink.R; 37 38 public class CarAssistantFragment extends Fragment { 39 40 private static final String EXTRA_CAR_PUSH_TO_TALK = 41 "com.android.car.input.EXTRA_CAR_PUSH_TO_TALK"; 42 43 @Override onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)44 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 45 @Nullable Bundle savedInstanceState) { 46 return inflater.inflate(R.layout.car_assistant, container, false); 47 } 48 49 @Override onViewCreated(View view, Bundle savedInstanceState)50 public void onViewCreated(View view, Bundle savedInstanceState) { 51 ImageButton micButton = (ImageButton) view.findViewById(R.id.voice_button_service); 52 Context context = getContext(); 53 54 IVoiceInteractionSessionShowCallback showCallback = 55 new IVoiceInteractionSessionShowCallback.Stub() { 56 @Override 57 public void onFailed() { 58 Toast.makeText(context, "Failed to show VoiceInteractionSession", 59 Toast.LENGTH_SHORT).show(); 60 } 61 62 @Override 63 public void onShown() {} 64 }; 65 66 micButton.setOnClickListener(v1 -> { 67 v1.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY); 68 69 AssistUtils assistUtils = new AssistUtils(context); 70 71 if (assistUtils.getAssistComponentForUser(ActivityManager.getCurrentUser()) == null) { 72 Toast.makeText(context, "Unable to retrieve assist component for current user", 73 Toast.LENGTH_SHORT).show(); 74 return; 75 } 76 77 Bundle args = new Bundle(); 78 args.putBoolean(EXTRA_CAR_PUSH_TO_TALK, true); 79 80 boolean success = assistUtils.showSessionForActiveService(args, 81 SHOW_SOURCE_PUSH_TO_TALK, showCallback, /*activityToken=*/ null); 82 if (!success) { 83 Toast.makeText(context, 84 "Assistant app is not available.", Toast.LENGTH_SHORT).show(); 85 } 86 }); 87 } 88 } 89