blob: f8483091ba2106568366e58696d9077bc649acc7 [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
jiayuzhou21a353b2018-08-16 16:09:43 -070019import android.annotation.NonNull;
Andrei Onea15884392019-03-22 17:28:11 +000020import android.annotation.UnsupportedAppUsage;
Adrian Roose9175082015-06-15 13:23:14 -070021import android.content.ComponentName;
22import android.content.Context;
Adrian Roos2335bd62016-08-11 15:42:40 -070023import android.content.pm.ApplicationInfo;
Jorim Jaggi165ce062015-07-06 16:18:11 -070024import android.content.pm.PackageManager;
Jorim Jaggi165ce062015-07-06 16:18:11 -070025import android.os.Bundle;
Dianne Hackborn17f69352015-07-17 18:04:14 -070026import android.os.IBinder;
Adrian Roose9175082015-06-15 13:23:14 -070027import android.os.RemoteException;
28import android.os.ServiceManager;
29import android.provider.Settings;
30import android.util.Log;
31
jiayuzhou21a353b2018-08-16 16:09:43 -070032import java.util.ArrayList;
33import java.util.Set;
34
Adrian Roose9175082015-06-15 13:23:14 -070035/**
36 * Utility method for dealing with the assistant aspects of
37 * {@link com.android.internal.app.IVoiceInteractionManagerService IVoiceInteractionManagerService}.
38 */
39public class AssistUtils {
40
41 private static final String TAG = "AssistUtils";
42
43 private final Context mContext;
44 private final IVoiceInteractionManagerService mVoiceInteractionManagerService;
45
Andrei Onea15884392019-03-22 17:28:11 +000046 @UnsupportedAppUsage
Adrian Roose9175082015-06-15 13:23:14 -070047 public AssistUtils(Context context) {
48 mContext = context;
49 mVoiceInteractionManagerService = IVoiceInteractionManagerService.Stub.asInterface(
50 ServiceManager.getService(Context.VOICE_INTERACTION_MANAGER_SERVICE));
51 }
52
Dianne Hackborn17f69352015-07-17 18:04:14 -070053 public boolean showSessionForActiveService(Bundle args, int sourceFlags,
54 IVoiceInteractionSessionShowCallback showCallback, IBinder activityToken) {
Adrian Roose9175082015-06-15 13:23:14 -070055 try {
Jorim Jaggie446dce2015-07-20 14:44:55 -070056 if (mVoiceInteractionManagerService != null) {
57 return mVoiceInteractionManagerService.showSessionForActiveService(args,
58 sourceFlags, showCallback, activityToken);
59 }
Adrian Roose9175082015-06-15 13:23:14 -070060 } catch (RemoteException e) {
61 Log.w(TAG, "Failed to call showSessionForActiveService", e);
62 }
Dianne Hackborn17f69352015-07-17 18:04:14 -070063 return false;
Adrian Roose9175082015-06-15 13:23:14 -070064 }
65
jiayuzhou21a353b2018-08-16 16:09:43 -070066 /**
67 * Checks the availability of a set of voice actions for the current active voice service.
68 *
69 * @param voiceActions A set of supported voice actions to be checked.
70 * @param callback The callback which will deliver a set of supported voice actions. If
71 * no voice actions are supported for the given voice action set, then null
72 * or empty set is provided.
73 */
74 public void getActiveServiceSupportedActions(@NonNull Set<String> voiceActions,
75 @NonNull IVoiceActionCheckCallback callback) {
76 try {
77 if (mVoiceInteractionManagerService != null) {
78 mVoiceInteractionManagerService
79 .getActiveServiceSupportedActions(new ArrayList<>(voiceActions), callback);
80 }
81 } catch (RemoteException e) {
82 Log.w(TAG, "Failed to call activeServiceSupportedActions", e);
83 try {
84 callback.onComplete(null);
85 } catch (RemoteException re) {
86 }
87 }
88 }
89
Adrian Roose9175082015-06-15 13:23:14 -070090 public void launchVoiceAssistFromKeyguard() {
91 try {
Jorim Jaggie446dce2015-07-20 14:44:55 -070092 if (mVoiceInteractionManagerService != null) {
93 mVoiceInteractionManagerService.launchVoiceAssistFromKeyguard();
94 }
Adrian Roose9175082015-06-15 13:23:14 -070095 } catch (RemoteException e) {
96 Log.w(TAG, "Failed to call launchVoiceAssistFromKeyguard", e);
97 }
98 }
99
100 public boolean activeServiceSupportsAssistGesture() {
101 try {
102 return mVoiceInteractionManagerService != null
103 && mVoiceInteractionManagerService.activeServiceSupportsAssist();
104 } catch (RemoteException e) {
105 Log.w(TAG, "Failed to call activeServiceSupportsAssistGesture", e);
106 return false;
107 }
108 }
109
110 public boolean activeServiceSupportsLaunchFromKeyguard() {
111 try {
112 return mVoiceInteractionManagerService != null
113 && mVoiceInteractionManagerService.activeServiceSupportsLaunchFromKeyguard();
114 } catch (RemoteException e) {
115 Log.w(TAG, "Failed to call activeServiceSupportsLaunchFromKeyguard", e);
116 return false;
117 }
118 }
119
120 public ComponentName getActiveServiceComponentName() {
121 try {
Jorim Jaggie446dce2015-07-20 14:44:55 -0700122 if (mVoiceInteractionManagerService != null) {
123 return mVoiceInteractionManagerService.getActiveServiceComponentName();
124 } else {
125 return null;
126 }
Adrian Roose9175082015-06-15 13:23:14 -0700127 } catch (RemoteException e) {
128 Log.w(TAG, "Failed to call getActiveServiceComponentName", e);
129 return null;
130 }
131 }
132
133 public boolean isSessionRunning() {
134 try {
135 return mVoiceInteractionManagerService != null
136 && mVoiceInteractionManagerService.isSessionRunning();
137 } catch (RemoteException e) {
138 Log.w(TAG, "Failed to call isSessionRunning", e);
139 return false;
140 }
141 }
142
143 public void hideCurrentSession() {
144 try {
Jorim Jaggie446dce2015-07-20 14:44:55 -0700145 if (mVoiceInteractionManagerService != null) {
146 mVoiceInteractionManagerService.hideCurrentSession();
147 }
Adrian Roose9175082015-06-15 13:23:14 -0700148 } catch (RemoteException e) {
149 Log.w(TAG, "Failed to call hideCurrentSession", e);
150 }
151 }
152
Jorim Jaggi19695d92015-07-20 15:51:40 -0700153 public void onLockscreenShown() {
154 try {
155 if (mVoiceInteractionManagerService != null) {
156 mVoiceInteractionManagerService.onLockscreenShown();
157 }
158 } catch (RemoteException e) {
159 Log.w(TAG, "Failed to call onLockscreenShown", e);
160 }
161 }
162
Annie Chinecb9f3e2016-06-27 16:01:52 -0700163 public void registerVoiceInteractionSessionListener(IVoiceInteractionSessionListener listener) {
164 try {
165 if (mVoiceInteractionManagerService != null) {
166 mVoiceInteractionManagerService.registerVoiceInteractionSessionListener(listener);
167 }
168 } catch (RemoteException e) {
169 Log.w(TAG, "Failed to register voice interaction listener", e);
170 }
171 }
172
Andrei Onea15884392019-03-22 17:28:11 +0000173 @UnsupportedAppUsage
Adrian Roose9175082015-06-15 13:23:14 -0700174 public ComponentName getAssistComponentForUser(int userId) {
175 final String setting = Settings.Secure.getStringForUser(mContext.getContentResolver(),
176 Settings.Secure.ASSISTANT, userId);
177 if (setting != null) {
178 return ComponentName.unflattenFromString(setting);
Philip P. Moltmann5d894502019-01-17 10:31:00 -0800179 } else {
Steve Elliott433c83d2018-09-25 15:23:38 +0000180 return null;
181 }
Adrian Roose9175082015-06-15 13:23:14 -0700182 }
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
Rajeev Kumarcd497ef2019-08-06 12:02:47 -0700198 public static boolean isDisclosureEnabled(Context context) {
Adrian Roos2335bd62016-08-11 15:42:40 -0700199 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}