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