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