diff --git a/core/java/android/service/voice/VoiceInteractionServiceInfo.java b/core/java/android/service/voice/VoiceInteractionServiceInfo.java index e1a9a050212c0..f7710e6a82ce8 100644 --- a/core/java/android/service/voice/VoiceInteractionServiceInfo.java +++ b/core/java/android/service/voice/VoiceInteractionServiceInfo.java @@ -17,6 +17,7 @@ package android.service.voice; import android.Manifest; +import android.annotation.NonNull; import android.app.AppGlobals; import android.content.ComponentName; import android.content.pm.PackageManager; @@ -28,6 +29,7 @@ import android.os.RemoteException; import android.util.AttributeSet; import android.util.Log; import android.util.Xml; + import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; @@ -47,24 +49,27 @@ public class VoiceInteractionServiceInfo { private boolean mSupportsLaunchFromKeyguard; private boolean mSupportsLocalInteraction; - public VoiceInteractionServiceInfo(PackageManager pm, ComponentName comp) - throws PackageManager.NameNotFoundException { - this(pm, pm.getServiceInfo(comp, PackageManager.GET_META_DATA)); - } - - public VoiceInteractionServiceInfo(PackageManager pm, ComponentName comp, int userHandle) + /** + * Loads the service metadata published by the component. Success is indicated by + * {@link #getParseError()}. + * + * @param pm A PackageManager from which the XML can be loaded. + * @param comp The {@link VoiceInteractionService} component. + */ + public VoiceInteractionServiceInfo( + @NonNull PackageManager pm, @NonNull ComponentName comp, int userHandle) throws PackageManager.NameNotFoundException { this(pm, getServiceInfoOrThrow(comp, userHandle)); } - static ServiceInfo getServiceInfoOrThrow(ComponentName comp, int userHandle) + @NonNull + private static ServiceInfo getServiceInfoOrThrow(@NonNull ComponentName comp, int userHandle) throws PackageManager.NameNotFoundException { try { ServiceInfo si = AppGlobals.getPackageManager().getServiceInfo(comp, PackageManager.GET_META_DATA | PackageManager.MATCH_DIRECT_BOOT_AWARE - | PackageManager.MATCH_DIRECT_BOOT_UNAWARE - | PackageManager.MATCH_DEBUG_TRIAGED_MISSING, + | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, userHandle); if (si != null) { return si; @@ -74,20 +79,23 @@ public class VoiceInteractionServiceInfo { throw new PackageManager.NameNotFoundException(comp.toString()); } - public VoiceInteractionServiceInfo(PackageManager pm, ServiceInfo si) { - if (si == null) { - mParseError = "Service not available"; - return; - } + /** + * Loads the service metadata published by the component. Success is indicated by + * {@link #getParseError()}. + * + * @param pm A PackageManager from which the XML can be loaded; usually the PackageManager + * from which {@code si} was originally retrieved. + * @param si The {@link VoiceInteractionService} info. + */ + public VoiceInteractionServiceInfo(@NonNull PackageManager pm, @NonNull ServiceInfo si) { if (!Manifest.permission.BIND_VOICE_INTERACTION.equals(si.permission)) { mParseError = "Service does not require permission " + Manifest.permission.BIND_VOICE_INTERACTION; return; } - XmlResourceParser parser = null; - try { - parser = si.loadXmlMetaData(pm, VoiceInteractionService.SERVICE_META_DATA); + try (XmlResourceParser parser = si.loadXmlMetaData(pm, + VoiceInteractionService.SERVICE_META_DATA)) { if (parser == null) { mParseError = "No " + VoiceInteractionService.SERVICE_META_DATA + " meta-data for " + si.packageName; @@ -134,20 +142,10 @@ public class VoiceInteractionServiceInfo { mParseError = "No recognitionService specified"; return; } - } catch (XmlPullParserException e) { + } catch (XmlPullParserException | IOException | PackageManager.NameNotFoundException e) { mParseError = "Error parsing voice interation service meta-data: " + e; Log.w(TAG, "error parsing voice interaction service meta-data", e); return; - } catch (IOException e) { - mParseError = "Error parsing voice interation service meta-data: " + e; - Log.w(TAG, "error parsing voice interaction service meta-data", e); - return; - } catch (PackageManager.NameNotFoundException e) { - mParseError = "Error parsing voice interation service meta-data: " + e; - Log.w(TAG, "error parsing voice interaction service meta-data", e); - return; - } finally { - if (parser != null) parser.close(); } mServiceInfo = si; } diff --git a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java index 9f203e1090bd3..a5fea344f9810 100644 --- a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java +++ b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java @@ -238,7 +238,6 @@ public class VoiceInteractionManagerService extends SystemService { private boolean mSafeMode; private int mCurUser; - private boolean mCurUserUnlocked; private boolean mCurUserSupported; @GuardedBy("this") @@ -494,7 +493,6 @@ public class VoiceInteractionManagerService extends SystemService { FgThread.getHandler().post(() -> { synchronized (this) { setCurrentUserLocked(userHandle); - mCurUserUnlocked = false; switchImplementationIfNeededLocked(false); } }); @@ -585,53 +583,43 @@ public class VoiceInteractionManagerService extends SystemService { VoiceInteractionServiceInfo findAvailInteractor(int userHandle, String packageName) { List available = mContext.getPackageManager().queryIntentServicesAsUser( - new Intent(VoiceInteractionService.SERVICE_INTERFACE), - PackageManager.MATCH_DIRECT_BOOT_AWARE - | PackageManager.MATCH_DIRECT_BOOT_UNAWARE - | PackageManager.MATCH_DEBUG_TRIAGED_MISSING, userHandle); + new Intent(VoiceInteractionService.SERVICE_INTERFACE) + .setPackage(packageName), + PackageManager.GET_META_DATA + | PackageManager.MATCH_DIRECT_BOOT_AWARE + | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, userHandle); int numAvailable = available.size(); - if (numAvailable == 0) { Slog.w(TAG, "no available voice interaction services found for user " + userHandle); return null; - } else { - // Find first system package. We never want to allow third party services to - // be automatically selected, because those require approval of the user. - VoiceInteractionServiceInfo foundInfo = null; - for (int i=0; i