| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.app; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.compat.annotation.UnsupportedAppUsage; |
| 21 |
import android.content.ComponentName; |
| 22 |
import android.content.Context; |
| 23 |
import android.content.pm.ApplicationInfo; |
| 24 |
import android.content.pm.PackageManager; |
| 25 |
import android.os.Bundle; |
| 26 |
import android.os.IBinder; |
| 27 |
import android.os.RemoteException; |
| 28 |
import android.os.ServiceManager; |
| 29 |
import android.provider.Settings; |
| 30 |
import android.util.Log; |
| 31 |
|
| 32 |
import java.util.ArrayList; |
| 33 |
import java.util.Set; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class AssistUtils { |
| 40 |
|
| 41 |
private static final String TAG = "AssistUtils"; |
| 42 |
|
| 43 |
|
| 44 |
public static final String INVOCATION_TYPE_KEY = "invocation_type"; |
| 45 |
|
| 46 |
public static final int INVOCATION_TYPE_UNKNOWN = 0; |
| 47 |
|
| 48 |
public static final int INVOCATION_TYPE_GESTURE = 1; |
| 49 |
|
| 50 |
public static final int INVOCATION_TYPE_PHYSICAL_GESTURE = 2; |
| 51 |
|
| 52 |
public static final int INVOCATION_TYPE_VOICE = 3; |
| 53 |
|
| 54 |
public static final int INVOCATION_TYPE_QUICK_SEARCH_BAR = 4; |
| 55 |
|
| 56 |
public static final int INVOCATION_TYPE_HOME_BUTTON_LONG_PRESS = 5; |
| 57 |
|
| 58 |
public static final int INVOCATION_TYPE_POWER_BUTTON_LONG_PRESS = 6; |
| 59 |
|
| 60 |
private final Context mContext; |
| 61 |
private final IVoiceInteractionManagerService mVoiceInteractionManagerService; |
| 62 |
|
| 63 |
@UnsupportedAppUsage |
| 64 |
public AssistUtils(Context context) { |
| 65 |
mContext = context; |
| 66 |
mVoiceInteractionManagerService = IVoiceInteractionManagerService.Stub.asInterface( |
| 67 |
ServiceManager.getService(Context.VOICE_INTERACTION_MANAGER_SERVICE)); |
| 68 |
} |
| 69 |
|
| 70 |
public boolean showSessionForActiveService(Bundle args, int sourceFlags, |
| 71 |
IVoiceInteractionSessionShowCallback showCallback, IBinder activityToken) { |
| 72 |
try { |
| 73 |
if (mVoiceInteractionManagerService != null) { |
| 74 |
return mVoiceInteractionManagerService.showSessionForActiveService(args, |
| 75 |
sourceFlags, showCallback, activityToken); |
| 76 |
} |
| 77 |
} catch (RemoteException e) { |
| 78 |
Log.w(TAG, "Failed to call showSessionForActiveService", e); |
| 79 |
} |
| 80 |
return false; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public void getActiveServiceSupportedActions(@NonNull Set<String> voiceActions, |
| 92 |
@NonNull IVoiceActionCheckCallback callback) { |
| 93 |
try { |
| 94 |
if (mVoiceInteractionManagerService != null) { |
| 95 |
mVoiceInteractionManagerService |
| 96 |
.getActiveServiceSupportedActions(new ArrayList<>(voiceActions), callback); |
| 97 |
} |
| 98 |
} catch (RemoteException e) { |
| 99 |
Log.w(TAG, "Failed to call activeServiceSupportedActions", e); |
| 100 |
try { |
| 101 |
callback.onComplete(null); |
| 102 |
} catch (RemoteException re) { |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
public void launchVoiceAssistFromKeyguard() { |
| 108 |
try { |
| 109 |
if (mVoiceInteractionManagerService != null) { |
| 110 |
mVoiceInteractionManagerService.launchVoiceAssistFromKeyguard(); |
| 111 |
} |
| 112 |
} catch (RemoteException e) { |
| 113 |
Log.w(TAG, "Failed to call launchVoiceAssistFromKeyguard", e); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
public boolean activeServiceSupportsAssistGesture() { |
| 118 |
try { |
| 119 |
return mVoiceInteractionManagerService != null |
| 120 |
&& mVoiceInteractionManagerService.activeServiceSupportsAssist(); |
| 121 |
} catch (RemoteException e) { |
| 122 |
Log.w(TAG, "Failed to call activeServiceSupportsAssistGesture", e); |
| 123 |
return false; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
public boolean activeServiceSupportsLaunchFromKeyguard() { |
| 128 |
try { |
| 129 |
return mVoiceInteractionManagerService != null |
| 130 |
&& mVoiceInteractionManagerService.activeServiceSupportsLaunchFromKeyguard(); |
| 131 |
} catch (RemoteException e) { |
| 132 |
Log.w(TAG, "Failed to call activeServiceSupportsLaunchFromKeyguard", e); |
| 133 |
return false; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
public ComponentName getActiveServiceComponentName() { |
| 138 |
try { |
| 139 |
if (mVoiceInteractionManagerService != null) { |
| 140 |
return mVoiceInteractionManagerService.getActiveServiceComponentName(); |
| 141 |
} else { |
| 142 |
return null; |
| 143 |
} |
| 144 |
} catch (RemoteException e) { |
| 145 |
Log.w(TAG, "Failed to call getActiveServiceComponentName", e); |
| 146 |
return null; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
public boolean isSessionRunning() { |
| 151 |
try { |
| 152 |
return mVoiceInteractionManagerService != null |
| 153 |
&& mVoiceInteractionManagerService.isSessionRunning(); |
| 154 |
} catch (RemoteException e) { |
| 155 |
Log.w(TAG, "Failed to call isSessionRunning", e); |
| 156 |
return false; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
public void hideCurrentSession() { |
| 161 |
try { |
| 162 |
if (mVoiceInteractionManagerService != null) { |
| 163 |
mVoiceInteractionManagerService.hideCurrentSession(); |
| 164 |
} |
| 165 |
} catch (RemoteException e) { |
| 166 |
Log.w(TAG, "Failed to call hideCurrentSession", e); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
public void onLockscreenShown() { |
| 171 |
try { |
| 172 |
if (mVoiceInteractionManagerService != null) { |
| 173 |
mVoiceInteractionManagerService.onLockscreenShown(); |
| 174 |
} |
| 175 |
} catch (RemoteException e) { |
| 176 |
Log.w(TAG, "Failed to call onLockscreenShown", e); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
public void registerVoiceInteractionSessionListener(IVoiceInteractionSessionListener listener) { |
| 181 |
try { |
| 182 |
if (mVoiceInteractionManagerService != null) { |
| 183 |
mVoiceInteractionManagerService.registerVoiceInteractionSessionListener(listener); |
| 184 |
} |
| 185 |
} catch (RemoteException e) { |
| 186 |
Log.w(TAG, "Failed to register voice interaction listener", e); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
@UnsupportedAppUsage |
| 191 |
public ComponentName getAssistComponentForUser(int userId) { |
| 192 |
final String setting = Settings.Secure.getStringForUser(mContext.getContentResolver(), |
| 193 |
Settings.Secure.ASSISTANT, userId); |
| 194 |
if (setting != null) { |
| 195 |
return ComponentName.unflattenFromString(setting); |
| 196 |
} else { |
| 197 |
return null; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
public static boolean isPreinstalledAssistant(Context context, ComponentName assistant) { |
| 202 |
if (assistant == null) { |
| 203 |
return false; |
| 204 |
} |
| 205 |
ApplicationInfo applicationInfo; |
| 206 |
try { |
| 207 |
applicationInfo = context.getPackageManager().getApplicationInfo( |
| 208 |
assistant.getPackageName(), 0); |
| 209 |
} catch (PackageManager.NameNotFoundException e) { |
| 210 |
return false; |
| 211 |
} |
| 212 |
return applicationInfo.isSystemApp() || applicationInfo.isUpdatedSystemApp(); |
| 213 |
} |
| 214 |
|
| 215 |
public static boolean isDisclosureEnabled(Context context) { |
| 216 |
return Settings.Secure.getInt(context.getContentResolver(), |
| 217 |
Settings.Secure.ASSIST_DISCLOSURE_ENABLED, 0) != 0; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
public static boolean shouldDisclose(Context context, ComponentName assistant) { |
| 227 |
if (!allowDisablingAssistDisclosure(context)) { |
| 228 |
return true; |
| 229 |
} |
| 230 |
|
| 231 |
return isDisclosureEnabled(context) || !isPreinstalledAssistant(context, assistant); |
| 232 |
} |
| 233 |
|
| 234 |
public static boolean allowDisablingAssistDisclosure(Context context) { |
| 235 |
return context.getResources().getBoolean( |
| 236 |
com.android.internal.R.bool.config_allowDisablingAssistDisclosure); |
| 237 |
} |
| 238 |
} |