1 /* 2 * Copyright (C) 2015 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 android.voiceinteraction.service; 18 19 import android.content.Context; 20 import android.os.Bundle; 21 import android.os.RemoteCallback; 22 import android.service.voice.VoiceInteractionSession; 23 import android.service.voice.VoiceInteractionSessionService; 24 import android.util.Log; 25 import android.voiceinteraction.common.Utils; 26 27 import java.lang.reflect.Constructor; 28 import java.lang.reflect.InvocationTargetException; 29 30 public class MainInteractionSessionService extends VoiceInteractionSessionService { 31 32 private static final String TAG = "MainInteractionSessionService"; 33 @Override onNewSession(Bundle args)34 public VoiceInteractionSession onNewSession(Bundle args) { 35 final String className = (args != null) 36 ? args.getString(Utils.VOICE_INTERACTION_KEY_CLASS) : null; 37 String command = args != null ? args.getString(Utils.VOICE_INTERACTION_KEY_COMMAND) : null; 38 final RemoteCallback callback = (args != null) ? args.getParcelable( 39 Utils.VOICE_INTERACTION_KEY_REMOTE_CALLBACK_FOR_NEW_SESSION, RemoteCallback.class) 40 : null; 41 if (callback != null) { 42 // Notify the session is started. The result value is not important 43 callback.sendResult(Bundle.EMPTY); 44 } 45 boolean testStartAssistantActivity = 46 command != null && command.equals("startAssistantActivity"); 47 Log.d(TAG, "testStartAssistantActivity = " + testStartAssistantActivity); 48 if (testStartAssistantActivity) { 49 return new SimpleVoiceInteractionSession(this); 50 } 51 if (className == null) { 52 return new MainInteractionSession(this); 53 } else { 54 try { 55 final Constructor<?> constructor = Class.forName(className) 56 .getConstructor(Context.class); 57 return (VoiceInteractionSession) constructor.newInstance(this); 58 } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException 59 | InstantiationException | InvocationTargetException e) { 60 throw new RuntimeException("Cannot instantiate class: " + className, e); 61 } 62 } 63 } 64 } 65