Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.internal.app; |
| 18 | |
jiayuzhou | 21a353b | 2018-08-16 16:09:43 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 20 | import android.annotation.UnsupportedAppUsage; |
Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 21 | import android.content.ComponentName; |
| 22 | import android.content.Context; |
Adrian Roos | 2335bd6 | 2016-08-11 15:42:40 -0700 | [diff] [blame] | 23 | import android.content.pm.ApplicationInfo; |
Jorim Jaggi | 165ce06 | 2015-07-06 16:18:11 -0700 | [diff] [blame] | 24 | import android.content.pm.PackageManager; |
Jorim Jaggi | 165ce06 | 2015-07-06 16:18:11 -0700 | [diff] [blame] | 25 | import android.os.Bundle; |
Dianne Hackborn | 17f6935 | 2015-07-17 18:04:14 -0700 | [diff] [blame] | 26 | import android.os.IBinder; |
Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 27 | import android.os.RemoteException; |
| 28 | import android.os.ServiceManager; |
| 29 | import android.provider.Settings; |
| 30 | import android.util.Log; |
| 31 | |
jiayuzhou | 21a353b | 2018-08-16 16:09:43 -0700 | [diff] [blame] | 32 | import java.util.ArrayList; |
| 33 | import java.util.Set; |
| 34 | |
Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 35 | /** |
| 36 | * Utility method for dealing with the assistant aspects of |
| 37 | * {@link com.android.internal.app.IVoiceInteractionManagerService IVoiceInteractionManagerService}. |
| 38 | */ |
| 39 | public class AssistUtils { |
| 40 | |
| 41 | private static final String TAG = "AssistUtils"; |
| 42 | |
| 43 | private final Context mContext; |
| 44 | private final IVoiceInteractionManagerService mVoiceInteractionManagerService; |
| 45 | |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 46 | @UnsupportedAppUsage |
Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 47 | public AssistUtils(Context context) { |
| 48 | mContext = context; |
| 49 | mVoiceInteractionManagerService = IVoiceInteractionManagerService.Stub.asInterface( |
| 50 | ServiceManager.getService(Context.VOICE_INTERACTION_MANAGER_SERVICE)); |
| 51 | } |
| 52 | |
Dianne Hackborn | 17f6935 | 2015-07-17 18:04:14 -0700 | [diff] [blame] | 53 | public boolean showSessionForActiveService(Bundle args, int sourceFlags, |
| 54 | IVoiceInteractionSessionShowCallback showCallback, IBinder activityToken) { |
Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 55 | try { |
Jorim Jaggi | e446dce | 2015-07-20 14:44:55 -0700 | [diff] [blame] | 56 | if (mVoiceInteractionManagerService != null) { |
| 57 | return mVoiceInteractionManagerService.showSessionForActiveService(args, |
| 58 | sourceFlags, showCallback, activityToken); |
| 59 | } |
Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 60 | } catch (RemoteException e) { |
| 61 | Log.w(TAG, "Failed to call showSessionForActiveService", e); |
| 62 | } |
Dianne Hackborn | 17f6935 | 2015-07-17 18:04:14 -0700 | [diff] [blame] | 63 | return false; |
Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 64 | } |
| 65 | |
jiayuzhou | 21a353b | 2018-08-16 16:09:43 -0700 | [diff] [blame] | 66 | /** |
| 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 Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 90 | public void launchVoiceAssistFromKeyguard() { |
| 91 | try { |
Jorim Jaggi | e446dce | 2015-07-20 14:44:55 -0700 | [diff] [blame] | 92 | if (mVoiceInteractionManagerService != null) { |
| 93 | mVoiceInteractionManagerService.launchVoiceAssistFromKeyguard(); |
| 94 | } |
Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 95 | } 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 Jaggi | e446dce | 2015-07-20 14:44:55 -0700 | [diff] [blame] | 122 | if (mVoiceInteractionManagerService != null) { |
| 123 | return mVoiceInteractionManagerService.getActiveServiceComponentName(); |
| 124 | } else { |
| 125 | return null; |
| 126 | } |
Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 127 | } 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 Jaggi | e446dce | 2015-07-20 14:44:55 -0700 | [diff] [blame] | 145 | if (mVoiceInteractionManagerService != null) { |
| 146 | mVoiceInteractionManagerService.hideCurrentSession(); |
| 147 | } |
Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 148 | } catch (RemoteException e) { |
| 149 | Log.w(TAG, "Failed to call hideCurrentSession", e); |
| 150 | } |
| 151 | } |
| 152 | |
Jorim Jaggi | 19695d9 | 2015-07-20 15:51:40 -0700 | [diff] [blame] | 153 | 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 Chin | ecb9f3e | 2016-06-27 16:01:52 -0700 | [diff] [blame] | 163 | 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 Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 173 | @UnsupportedAppUsage |
Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 174 | 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. Moltmann | 5d89450 | 2019-01-17 10:31:00 -0800 | [diff] [blame] | 179 | } else { |
Steve Elliott | 433c83d | 2018-09-25 15:23:38 +0000 | [diff] [blame] | 180 | return null; |
| 181 | } |
Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Adrian Roos | 2335bd6 | 2016-08-11 15:42:40 -0700 | [diff] [blame] | 184 | 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 Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 221 | } |