VoiceInteractionSessionService.java revision 18f0d357f9693fe787a3e3777d8fdf01357a6e3f
1/** 2 * Copyright (C) 2014 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 17package android.service.voice; 18 19import android.app.Service; 20import android.content.Context; 21import android.content.Intent; 22import android.os.Bundle; 23import android.os.IBinder; 24import android.os.Looper; 25import android.os.Message; 26import android.os.RemoteException; 27import android.os.ServiceManager; 28import com.android.internal.app.IVoiceInteractionManagerService; 29import com.android.internal.os.HandlerCaller; 30import com.android.internal.os.SomeArgs; 31 32public abstract class VoiceInteractionSessionService extends Service { 33 34 static final int MSG_NEW_SESSION = 1; 35 36 IVoiceInteractionManagerService mSystemService; 37 38 IVoiceInteractionSessionService mInterface = new IVoiceInteractionSessionService.Stub() { 39 public void newSession(IBinder token, Bundle args) { 40 mHandlerCaller.sendMessage(mHandlerCaller.obtainMessageOO(MSG_NEW_SESSION, 41 token, args)); 42 43 } 44 }; 45 46 HandlerCaller mHandlerCaller; 47 final HandlerCaller.Callback mHandlerCallerCallback = new HandlerCaller.Callback() { 48 @Override 49 public void executeMessage(Message msg) { 50 SomeArgs args = (SomeArgs)msg.obj; 51 switch (msg.what) { 52 case MSG_NEW_SESSION: 53 doNewSession((IBinder)args.arg1, (Bundle)args.arg2); 54 break; 55 } 56 } 57 }; 58 59 @Override 60 public void onCreate() { 61 super.onCreate(); 62 mSystemService = IVoiceInteractionManagerService.Stub.asInterface( 63 ServiceManager.getService(Context.VOICE_INTERACTION_MANAGER_SERVICE)); 64 mHandlerCaller = new HandlerCaller(this, Looper.myLooper(), 65 mHandlerCallerCallback, true); 66 } 67 68 public abstract VoiceInteractionSession onNewSession(Bundle args); 69 70 @Override 71 public IBinder onBind(Intent intent) { 72 return mInterface.asBinder(); 73 } 74 75 void doNewSession(IBinder token, Bundle args) { 76 VoiceInteractionSession session = onNewSession(args); 77 try { 78 mSystemService.deliverNewSession(token, session.mSession, session.mInteractor); 79 } catch (RemoteException e) { 80 } 81 } 82} 83