Merge changes I9a0d859f,I8ab1824a,I2093b163,Ie4370c2e,I62a9480f

* changes:
  VIMS: Simplify findAvailInteractor() by getting metadata in initial pm query.
  VIMS cleanup: set package on pm query instead of filtering results.
  VoiceInteraction: Remove MATCH_DEBUG_TRIAGED_MISSING from PM queries.
  VIMS: Remove useless field mCurUserUnlocked.
  VoiceInteractionServiceInfo cleanup: Nullability, javadoc, try-with, unused method.
This commit is contained in:
Ahaan Ugale
2021-01-25 21:43:02 +00:00
committed by Android (Google) Code Review
2 changed files with 58 additions and 74 deletions

View File

@@ -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;
}

View File

@@ -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<ResolveInfo> 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<numAvailable; i++) {
ServiceInfo cur = available.get(i).serviceInfo;
if ((cur.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) != 0) {
ComponentName comp = new ComponentName(cur.packageName, cur.name);
try {
VoiceInteractionServiceInfo info = new VoiceInteractionServiceInfo(
mContext.getPackageManager(), comp, userHandle);
if (info.getParseError() == null) {
if (packageName == null || info.getServiceInfo().packageName.equals(
packageName)) {
if (foundInfo == null) {
foundInfo = info;
} else {
Slog.w(TAG, "More than one voice interaction service, "
+ "picking first "
+ new ComponentName(
foundInfo.getServiceInfo().packageName,
foundInfo.getServiceInfo().name)
+ " over "
+ new ComponentName(cur.packageName, cur.name));
}
}
} else {
Slog.w(TAG, "Bad interaction service " + comp + ": "
+ info.getParseError());
}
} catch (PackageManager.NameNotFoundException e) {
Slog.w(TAG, "Failure looking up interaction service " + comp);
}
}
}
return foundInfo;
}
// 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 < numAvailable; i++) {
ServiceInfo cur = available.get(i).serviceInfo;
if ((cur.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
continue;
}
VoiceInteractionServiceInfo info =
new VoiceInteractionServiceInfo(mContext.getPackageManager(), cur);
if (info.getParseError() != null) {
Slog.w(TAG,
"Bad interaction service " + cur.packageName + "/"
+ cur.name + ": " + info.getParseError());
} else if (foundInfo == null) {
foundInfo = info;
} else {
Slog.w(TAG, "More than one voice interaction service, "
+ "picking first "
+ new ComponentName(
foundInfo.getServiceInfo().packageName,
foundInfo.getServiceInfo().name)
+ " over "
+ new ComponentName(cur.packageName, cur.name));
}
}
return foundInfo;
}
ComponentName getCurInteractor(int userHandle) {
@@ -661,7 +649,6 @@ public class VoiceInteractionManagerService extends SystemService {
PackageManager.MATCH_DIRECT_BOOT_AWARE
| PackageManager.MATCH_DIRECT_BOOT_UNAWARE, userHandle);
int numAvailable = available.size();
if (numAvailable == 0) {
Slog.w(TAG, "no available voice recognition services found for user " + userHandle);
return null;
@@ -1458,7 +1445,6 @@ public class VoiceInteractionManagerService extends SystemService {
pw.println(" mEnableService: " + mEnableService);
pw.println(" mTemporarilyDisabled: " + mTemporarilyDisabled);
pw.println(" mCurUser: " + mCurUser);
pw.println(" mCurUserUnlocked: " + mCurUserUnlocked);
pw.println(" mCurUserSupported: " + mCurUserSupported);
dumpSupportedUsers(pw, " ");
mDbHelper.dump(pw);