blob: fde01dd0deea30ab7da2b4a0900404477b9fcbe5 [file] [log] [blame]
Adrian Roose9175082015-06-15 13:23:14 -07001/*
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
Adrian Roos2335bd62016-08-11 15:42:40 -070019import com.android.internal.R;
20
Andrei Oneaeecddd52019-03-27 10:32:55 +000021import android.annotation.UnsupportedAppUsage;
Adrian Roose9175082015-06-15 13:23:14 -070022import android.app.SearchManager;
23import android.content.ComponentName;
24import android.content.Context;
25import android.content.Intent;
Adrian Roos2335bd62016-08-11 15:42:40 -070026import android.content.pm.ApplicationInfo;
Jorim Jaggi165ce062015-07-06 16:18:11 -070027import android.content.pm.PackageManager;
28import android.content.pm.ResolveInfo;
29import android.os.Bundle;
Dianne Hackborn17f69352015-07-17 18:04:14 -070030import android.os.IBinder;
Adrian Roose9175082015-06-15 13:23:14 -070031import android.os.RemoteException;
32import android.os.ServiceManager;
33import android.provider.Settings;
34import android.util.Log;
35
36/**
37 * Utility method for dealing with the assistant aspects of
38 * {@link com.android.internal.app.IVoiceInteractionManagerService IVoiceInteractionManagerService}.
39 */
40public class AssistUtils {
41
42 private static final String TAG = "AssistUtils";
43
44 private final Context mContext;
45 private final IVoiceInteractionManagerService mVoiceInteractionManagerService;
46
Andrei Oneaeecddd52019-03-27 10:32:55 +000047 @UnsupportedAppUsage
Adrian Roose9175082015-06-15 13:23:14 -070048 public AssistUtils(Context context) {
49 mContext = context;
50 mVoiceInteractionManagerService = IVoiceInteractionManagerService.Stub.asInterface(
51 ServiceManager.getService(Context.VOICE_INTERACTION_MANAGER_SERVICE));
52 }
53
Dianne Hackborn17f69352015-07-17 18:04:14 -070054 public boolean showSessionForActiveService(Bundle args, int sourceFlags,
55 IVoiceInteractionSessionShowCallback showCallback, IBinder activityToken) {
Adrian Roose9175082015-06-15 13:23:14 -070056 try {
Jorim Jaggie446dce2015-07-20 14:44:55 -070057 if (mVoiceInteractionManagerService != null) {
58 return mVoiceInteractionManagerService.showSessionForActiveService(args,
59 sourceFlags, showCallback, activityToken);
60 }
Adrian Roose9175082015-06-15 13:23:14 -070061 } catch (RemoteException e) {
62 Log.w(TAG, "Failed to call showSessionForActiveService", e);
63 }
Dianne Hackborn17f69352015-07-17 18:04:14 -070064 return false;
Adrian Roose9175082015-06-15 13:23:14 -070065 }
66
67 public void launchVoiceAssistFromKeyguard() {
68 try {
Jorim Jaggie446dce2015-07-20 14:44:55 -070069 if (mVoiceInteractionManagerService != null) {
70 mVoiceInteractionManagerService.launchVoiceAssistFromKeyguard();
71 }
Adrian Roose9175082015-06-15 13:23:14 -070072 } catch (RemoteException e) {
73 Log.w(TAG, "Failed to call launchVoiceAssistFromKeyguard", e);
74 }
75 }
76
77 public boolean activeServiceSupportsAssistGesture() {
78 try {
79 return mVoiceInteractionManagerService != null
80 && mVoiceInteractionManagerService.activeServiceSupportsAssist();
81 } catch (RemoteException e) {
82 Log.w(TAG, "Failed to call activeServiceSupportsAssistGesture", e);
83 return false;
84 }
85 }
86
87 public boolean activeServiceSupportsLaunchFromKeyguard() {
88 try {
89 return mVoiceInteractionManagerService != null
90 && mVoiceInteractionManagerService.activeServiceSupportsLaunchFromKeyguard();
91 } catch (RemoteException e) {
92 Log.w(TAG, "Failed to call activeServiceSupportsLaunchFromKeyguard", e);
93 return false;
94 }
95 }
96
97 public ComponentName getActiveServiceComponentName() {
98 try {
Jorim Jaggie446dce2015-07-20 14:44:55 -070099 if (mVoiceInteractionManagerService != null) {
100 return mVoiceInteractionManagerService.getActiveServiceComponentName();
101 } else {
102 return null;
103 }
Adrian Roose9175082015-06-15 13:23:14 -0700104 } catch (RemoteException e) {
105 Log.w(TAG, "Failed to call getActiveServiceComponentName", e);
106 return null;
107 }
108 }
109
110 public boolean isSessionRunning() {
111 try {
112 return mVoiceInteractionManagerService != null
113 && mVoiceInteractionManagerService.isSessionRunning();
114 } catch (RemoteException e) {
115 Log.w(TAG, "Failed to call isSessionRunning", e);
116 return false;
117 }
118 }
119
120 public void hideCurrentSession() {
121 try {
Jorim Jaggie446dce2015-07-20 14:44:55 -0700122 if (mVoiceInteractionManagerService != null) {
123 mVoiceInteractionManagerService.hideCurrentSession();
124 }
Adrian Roose9175082015-06-15 13:23:14 -0700125 } catch (RemoteException e) {
126 Log.w(TAG, "Failed to call hideCurrentSession", e);
127 }
128 }
129
Jorim Jaggi19695d92015-07-20 15:51:40 -0700130 public void onLockscreenShown() {
131 try {
132 if (mVoiceInteractionManagerService != null) {
133 mVoiceInteractionManagerService.onLockscreenShown();
134 }
135 } catch (RemoteException e) {
136 Log.w(TAG, "Failed to call onLockscreenShown", e);
137 }
138 }
139
Annie Chinecb9f3e2016-06-27 16:01:52 -0700140 public void registerVoiceInteractionSessionListener(IVoiceInteractionSessionListener listener) {
141 try {
142 if (mVoiceInteractionManagerService != null) {
143 mVoiceInteractionManagerService.registerVoiceInteractionSessionListener(listener);
144 }
145 } catch (RemoteException e) {
146 Log.w(TAG, "Failed to register voice interaction listener", e);
147 }
148 }
149
Andrei Oneaeecddd52019-03-27 10:32:55 +0000150 @UnsupportedAppUsage
Adrian Roose9175082015-06-15 13:23:14 -0700151 public ComponentName getAssistComponentForUser(int userId) {
152 final String setting = Settings.Secure.getStringForUser(mContext.getContentResolver(),
153 Settings.Secure.ASSISTANT, userId);
154 if (setting != null) {
155 return ComponentName.unflattenFromString(setting);
156 }
157
Steve Elliott730d1d52018-09-25 15:23:38 +0000158 final String defaultSetting = mContext.getResources().getString(
159 R.string.config_defaultAssistantComponentName);
160 if (defaultSetting != null) {
161 return ComponentName.unflattenFromString(defaultSetting);
162 }
163
Adrian Roose9175082015-06-15 13:23:14 -0700164 // Fallback to keep backward compatible behavior when there is no user setting.
165 if (activeServiceSupportsAssistGesture()) {
166 return getActiveServiceComponentName();
167 }
Steven Wu3aa2cf72018-05-25 15:20:10 -0400168 final SearchManager searchManager =
169 (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE);
170 if (searchManager == null) {
171 return null;
172 }
173 final Intent intent = searchManager.getAssistIntent(false);
Jorim Jaggi165ce062015-07-06 16:18:11 -0700174 PackageManager pm = mContext.getPackageManager();
175 ResolveInfo info = pm.resolveActivityAsUser(intent, PackageManager.MATCH_DEFAULT_ONLY,
176 userId);
177 if (info != null) {
178 return new ComponentName(info.activityInfo.applicationInfo.packageName,
179 info.activityInfo.name);
Adrian Roose9175082015-06-15 13:23:14 -0700180 }
Adrian Roose9175082015-06-15 13:23:14 -0700181 return null;
182 }
183
Adrian Roos2335bd62016-08-11 15:42:40 -0700184 public static boolean isPreinstalledAssistant(Context context, ComponentName assistant) {
185 if (assistant == null) {
186 return false;
187 }
188 ApplicationInfo applicationInfo;
189 try {
190 applicationInfo = context.getPackageManager().getApplicationInfo(
191 assistant.getPackageName(), 0);
192 } catch (PackageManager.NameNotFoundException e) {
193 return false;
194 }
195 return applicationInfo.isSystemApp() || applicationInfo.isUpdatedSystemApp();
196 }
197
198 private static boolean isDisclosureEnabled(Context context) {
199 return Settings.Secure.getInt(context.getContentResolver(),
200 Settings.Secure.ASSIST_DISCLOSURE_ENABLED, 0) != 0;
201 }
202
203 /**
204 * @return if the disclosure animation should trigger for the given assistant.
205 *
206 * Third-party assistants will always need to disclose, while the user can configure this for
207 * pre-installed assistants.
208 */
209 public static boolean shouldDisclose(Context context, ComponentName assistant) {
210 if (!allowDisablingAssistDisclosure(context)) {
211 return true;
212 }
213
214 return isDisclosureEnabled(context) || !isPreinstalledAssistant(context, assistant);
215 }
216
217 public static boolean allowDisablingAssistDisclosure(Context context) {
218 return context.getResources().getBoolean(
219 com.android.internal.R.bool.config_allowDisablingAssistDisclosure);
220 }
Adrian Roose9175082015-06-15 13:23:14 -0700221}