1/*
2 * Copyright (C) 2015 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 com.android.internal.app;
18
19import android.app.SearchManager;
20import android.content.ComponentName;
21import android.content.Context;
22import android.content.Intent;
23import android.content.pm.PackageManager;
24import android.content.pm.ResolveInfo;
25import android.os.Bundle;
26import android.os.IBinder;
27import android.os.RemoteException;
28import android.os.ServiceManager;
29import android.provider.Settings;
30import android.util.Log;
31
32/**
33 * Utility method for dealing with the assistant aspects of
34 * {@link com.android.internal.app.IVoiceInteractionManagerService IVoiceInteractionManagerService}.
35 */
36public class AssistUtils {
37
38    private static final String TAG = "AssistUtils";
39
40    private final Context mContext;
41    private final IVoiceInteractionManagerService mVoiceInteractionManagerService;
42
43    public AssistUtils(Context context) {
44        mContext = context;
45        mVoiceInteractionManagerService = IVoiceInteractionManagerService.Stub.asInterface(
46                ServiceManager.getService(Context.VOICE_INTERACTION_MANAGER_SERVICE));
47    }
48
49    public boolean showSessionForActiveService(Bundle args, int sourceFlags,
50            IVoiceInteractionSessionShowCallback showCallback, IBinder activityToken) {
51        try {
52            if (mVoiceInteractionManagerService != null) {
53                return mVoiceInteractionManagerService.showSessionForActiveService(args,
54                        sourceFlags, showCallback, activityToken);
55            }
56        } catch (RemoteException e) {
57            Log.w(TAG, "Failed to call showSessionForActiveService", e);
58        }
59        return false;
60    }
61
62    public void launchVoiceAssistFromKeyguard() {
63        try {
64            if (mVoiceInteractionManagerService != null) {
65                mVoiceInteractionManagerService.launchVoiceAssistFromKeyguard();
66            }
67        } catch (RemoteException e) {
68            Log.w(TAG, "Failed to call launchVoiceAssistFromKeyguard", e);
69        }
70    }
71
72    public boolean activeServiceSupportsAssistGesture() {
73        try {
74            return mVoiceInteractionManagerService != null
75                    && mVoiceInteractionManagerService.activeServiceSupportsAssist();
76        } catch (RemoteException e) {
77            Log.w(TAG, "Failed to call activeServiceSupportsAssistGesture", e);
78            return false;
79        }
80    }
81
82    public boolean activeServiceSupportsLaunchFromKeyguard() {
83        try {
84            return mVoiceInteractionManagerService != null
85                    && mVoiceInteractionManagerService.activeServiceSupportsLaunchFromKeyguard();
86        } catch (RemoteException e) {
87            Log.w(TAG, "Failed to call activeServiceSupportsLaunchFromKeyguard", e);
88            return false;
89        }
90    }
91
92    public ComponentName getActiveServiceComponentName() {
93        try {
94            if (mVoiceInteractionManagerService != null) {
95                return mVoiceInteractionManagerService.getActiveServiceComponentName();
96            } else {
97                return null;
98            }
99        } catch (RemoteException e) {
100            Log.w(TAG, "Failed to call getActiveServiceComponentName", e);
101            return null;
102        }
103    }
104
105    public boolean isSessionRunning() {
106        try {
107            return mVoiceInteractionManagerService != null
108                    && mVoiceInteractionManagerService.isSessionRunning();
109        } catch (RemoteException e) {
110            Log.w(TAG, "Failed to call isSessionRunning", e);
111            return false;
112        }
113    }
114
115    public void hideCurrentSession() {
116        try {
117            if (mVoiceInteractionManagerService != null) {
118                mVoiceInteractionManagerService.hideCurrentSession();
119            }
120        } catch (RemoteException e) {
121            Log.w(TAG, "Failed to call hideCurrentSession", e);
122        }
123    }
124
125    public void onLockscreenShown() {
126        try {
127            if (mVoiceInteractionManagerService != null) {
128                mVoiceInteractionManagerService.onLockscreenShown();
129            }
130        } catch (RemoteException e) {
131            Log.w(TAG, "Failed to call onLockscreenShown", e);
132        }
133    }
134
135    public ComponentName getAssistComponentForUser(int userId) {
136        final String setting = Settings.Secure.getStringForUser(mContext.getContentResolver(),
137                Settings.Secure.ASSISTANT, userId);
138        if (setting != null) {
139            return ComponentName.unflattenFromString(setting);
140        }
141
142        // Fallback to keep backward compatible behavior when there is no user setting.
143        if (activeServiceSupportsAssistGesture()) {
144            return getActiveServiceComponentName();
145        }
146
147        Intent intent = ((SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE))
148                .getAssistIntent(false);
149        PackageManager pm = mContext.getPackageManager();
150        ResolveInfo info = pm.resolveActivityAsUser(intent, PackageManager.MATCH_DEFAULT_ONLY,
151                userId);
152        if (info != null) {
153            return new ComponentName(info.activityInfo.applicationInfo.packageName,
154                    info.activityInfo.name);
155        }
156        return null;
157    }
158
159}
160