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 | |
Adrian Roos | 2335bd6 | 2016-08-11 15:42:40 -0700 | [diff] [blame] | 19 | import com.android.internal.R; |
| 20 | |
Andrei Onea | eecddd5 | 2019-03-27 10:32:55 +0000 | [diff] [blame] | 21 | import android.annotation.UnsupportedAppUsage; |
Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 22 | import android.app.SearchManager; |
| 23 | import android.content.ComponentName; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
Adrian Roos | 2335bd6 | 2016-08-11 15:42:40 -0700 | [diff] [blame] | 26 | import android.content.pm.ApplicationInfo; |
Jorim Jaggi | 165ce06 | 2015-07-06 16:18:11 -0700 | [diff] [blame] | 27 | import android.content.pm.PackageManager; |
| 28 | import android.content.pm.ResolveInfo; |
| 29 | import android.os.Bundle; |
Dianne Hackborn | 17f6935 | 2015-07-17 18:04:14 -0700 | [diff] [blame] | 30 | import android.os.IBinder; |
Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 31 | import android.os.RemoteException; |
| 32 | import android.os.ServiceManager; |
| 33 | import android.provider.Settings; |
| 34 | import android.util.Log; |
| 35 | |
| 36 | /** |
| 37 | * Utility method for dealing with the assistant aspects of |
| 38 | * {@link com.android.internal.app.IVoiceInteractionManagerService IVoiceInteractionManagerService}. |
| 39 | */ |
| 40 | public class AssistUtils { |
| 41 | |
| 42 | private static final String TAG = "AssistUtils"; |
| 43 | |
| 44 | private final Context mContext; |
| 45 | private final IVoiceInteractionManagerService mVoiceInteractionManagerService; |
| 46 | |
Andrei Onea | eecddd5 | 2019-03-27 10:32:55 +0000 | [diff] [blame] | 47 | @UnsupportedAppUsage |
Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 48 | public AssistUtils(Context context) { |
| 49 | mContext = context; |
| 50 | mVoiceInteractionManagerService = IVoiceInteractionManagerService.Stub.asInterface( |
| 51 | ServiceManager.getService(Context.VOICE_INTERACTION_MANAGER_SERVICE)); |
| 52 | } |
| 53 | |
Dianne Hackborn | 17f6935 | 2015-07-17 18:04:14 -0700 | [diff] [blame] | 54 | public boolean showSessionForActiveService(Bundle args, int sourceFlags, |
| 55 | IVoiceInteractionSessionShowCallback showCallback, IBinder activityToken) { |
Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 56 | try { |
Jorim Jaggi | e446dce | 2015-07-20 14:44:55 -0700 | [diff] [blame] | 57 | if (mVoiceInteractionManagerService != null) { |
| 58 | return mVoiceInteractionManagerService.showSessionForActiveService(args, |
| 59 | sourceFlags, showCallback, activityToken); |
| 60 | } |
Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 61 | } catch (RemoteException e) { |
| 62 | Log.w(TAG, "Failed to call showSessionForActiveService", e); |
| 63 | } |
Dianne Hackborn | 17f6935 | 2015-07-17 18:04:14 -0700 | [diff] [blame] | 64 | return false; |
Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | public void launchVoiceAssistFromKeyguard() { |
| 68 | try { |
Jorim Jaggi | e446dce | 2015-07-20 14:44:55 -0700 | [diff] [blame] | 69 | if (mVoiceInteractionManagerService != null) { |
| 70 | mVoiceInteractionManagerService.launchVoiceAssistFromKeyguard(); |
| 71 | } |
Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 72 | } 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 Jaggi | e446dce | 2015-07-20 14:44:55 -0700 | [diff] [blame] | 99 | if (mVoiceInteractionManagerService != null) { |
| 100 | return mVoiceInteractionManagerService.getActiveServiceComponentName(); |
| 101 | } else { |
| 102 | return null; |
| 103 | } |
Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 104 | } 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 Jaggi | e446dce | 2015-07-20 14:44:55 -0700 | [diff] [blame] | 122 | if (mVoiceInteractionManagerService != null) { |
| 123 | mVoiceInteractionManagerService.hideCurrentSession(); |
| 124 | } |
Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 125 | } catch (RemoteException e) { |
| 126 | Log.w(TAG, "Failed to call hideCurrentSession", e); |
| 127 | } |
| 128 | } |
| 129 | |
Jorim Jaggi | 19695d9 | 2015-07-20 15:51:40 -0700 | [diff] [blame] | 130 | 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 Chin | ecb9f3e | 2016-06-27 16:01:52 -0700 | [diff] [blame] | 140 | 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 Onea | eecddd5 | 2019-03-27 10:32:55 +0000 | [diff] [blame] | 150 | @UnsupportedAppUsage |
Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 151 | 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 Elliott | 730d1d5 | 2018-09-25 15:23:38 +0000 | [diff] [blame] | 158 | final String defaultSetting = mContext.getResources().getString( |
| 159 | R.string.config_defaultAssistantComponentName); |
| 160 | if (defaultSetting != null) { |
| 161 | return ComponentName.unflattenFromString(defaultSetting); |
| 162 | } |
| 163 | |
Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 164 | // Fallback to keep backward compatible behavior when there is no user setting. |
| 165 | if (activeServiceSupportsAssistGesture()) { |
| 166 | return getActiveServiceComponentName(); |
| 167 | } |
Steven Wu | 3aa2cf7 | 2018-05-25 15:20:10 -0400 | [diff] [blame] | 168 | 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 Jaggi | 165ce06 | 2015-07-06 16:18:11 -0700 | [diff] [blame] | 174 | 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 Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 180 | } |
Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 181 | return null; |
| 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 | } |