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