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.content.res.Configuration;
23 import android.os.Bundle;
24 import android.os.IBinder;
25 import android.os.Looper;
26 import android.os.Message;
27 import android.os.RemoteException;
28 import android.os.ServiceManager;
29 import com.android.internal.app.IVoiceInteractionManagerService;
30 import com.android.internal.os.HandlerCaller;
31 import com.android.internal.os.SomeArgs;
32 
33 import java.io.FileDescriptor;
34 import java.io.PrintWriter;
35 
36 /**
37  * An active voice interaction session, initiated by a {@link VoiceInteractionService}.
38  */
39 public abstract class VoiceInteractionSessionService extends Service {
40 
41     static final int MSG_NEW_SESSION = 1;
42 
43     IVoiceInteractionManagerService mSystemService;
44     VoiceInteractionSession mSession;
45 
46     IVoiceInteractionSessionService mInterface = new IVoiceInteractionSessionService.Stub() {
47         public void newSession(IBinder token, Bundle args, int startFlags) {
48             mHandlerCaller.sendMessage(mHandlerCaller.obtainMessageIOO(MSG_NEW_SESSION,
49                     startFlags, token, args));
50 
51         }
52     };
53 
54     HandlerCaller mHandlerCaller;
55     final HandlerCaller.Callback mHandlerCallerCallback = new HandlerCaller.Callback() {
56         @Override
57         public void executeMessage(Message msg) {
58             SomeArgs args = (SomeArgs)msg.obj;
59             switch (msg.what) {
60                 case MSG_NEW_SESSION:
61                     doNewSession((IBinder)args.arg1, (Bundle)args.arg2, args.argi1);
62                     break;
63             }
64         }
65     };
66 
67     @Override
onCreate()68     public void onCreate() {
69         super.onCreate();
70         mSystemService = IVoiceInteractionManagerService.Stub.asInterface(
71                 ServiceManager.getService(Context.VOICE_INTERACTION_MANAGER_SERVICE));
72         mHandlerCaller = new HandlerCaller(this, Looper.myLooper(),
73                 mHandlerCallerCallback, true);
74     }
75 
onNewSession(Bundle args)76     public abstract VoiceInteractionSession onNewSession(Bundle args);
77 
78     @Override
onBind(Intent intent)79     public IBinder onBind(Intent intent) {
80         return mInterface.asBinder();
81     }
82 
83     @Override
onConfigurationChanged(Configuration newConfig)84     public void onConfigurationChanged(Configuration newConfig) {
85         super.onConfigurationChanged(newConfig);
86         if (mSession != null) {
87             mSession.onConfigurationChanged(newConfig);
88         }
89     }
90 
91     @Override
onLowMemory()92     public void onLowMemory() {
93         super.onLowMemory();
94         if (mSession != null) {
95             mSession.onLowMemory();
96         }
97     }
98 
99     @Override
onTrimMemory(int level)100     public void onTrimMemory(int level) {
101         super.onTrimMemory(level);
102         if (mSession != null) {
103             mSession.onTrimMemory(level);
104         }
105     }
106 
107     @Override
dump(FileDescriptor fd, PrintWriter writer, String[] args)108     protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
109         if (mSession == null) {
110             writer.println("(no active session)");
111         } else {
112             writer.println("VoiceInteractionSession:");
113             mSession.dump("  ", fd, writer, args);
114         }
115     }
116 
doNewSession(IBinder token, Bundle args, int startFlags)117     void doNewSession(IBinder token, Bundle args, int startFlags) {
118         if (mSession != null) {
119             mSession.doDestroy();
120             mSession = null;
121         }
122         mSession = onNewSession(args);
123         try {
124             mSystemService.deliverNewSession(token, mSession.mSession, mSession.mInteractor);
125             mSession.doCreate(mSystemService, token);
126         } catch (RemoteException e) {
127         }
128     }
129 }
130