diff options
| author | Ahaan Ugale <augale@google.com> | 2021-02-28 22:02:53 -0800 |
|---|---|---|
| committer | Ahaan Ugale <augale@google.com> | 2021-02-28 22:17:41 -0800 |
| commit | dcced700fb0dd424ea3fc81e15146f87840febfa (patch) | |
| tree | 2d77eb593777d7f13a6c3500a6018903ad17d278 | |
| parent | b33721641ed491ab9deff871fa31739ce8b93be6 (diff) | |
AF: Add a helper method for querying available services.
This is used to list all relevant autofill services in system settings.
Bug: 169455298
Test: manual
Change-Id: Ieea22cc85604c80695819278310e04527aa1d067
| -rw-r--r-- | core/java/android/service/autofill/AutofillServiceInfo.java | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/core/java/android/service/autofill/AutofillServiceInfo.java b/core/java/android/service/autofill/AutofillServiceInfo.java index fbc25a6aaf74..e30529dbe6f8 100644 --- a/core/java/android/service/autofill/AutofillServiceInfo.java +++ b/core/java/android/service/autofill/AutofillServiceInfo.java @@ -18,10 +18,13 @@ package android.service.autofill; import android.Manifest; import android.annotation.NonNull; import android.annotation.Nullable; +import android.annotation.UserIdInt; import android.app.AppGlobals; import android.content.ComponentName; import android.content.Context; +import android.content.Intent; import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; import android.content.pm.ServiceInfo; import android.content.res.Resources; import android.content.res.TypedArray; @@ -44,6 +47,8 @@ import org.xmlpull.v1.XmlPullParserException; import java.io.IOException; import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.List; /** * {@link ServiceInfo} and meta-data about an {@link AutofillService}. @@ -238,6 +243,30 @@ public final class AutofillServiceInfo { return mInlineSuggestionsEnabled; } + /** + * Queries the valid autofill services available for the user. + */ + public static List<AutofillServiceInfo> getAvailableServices( + Context context, @UserIdInt int user) { + final List<AutofillServiceInfo> services = new ArrayList<>(); + + final List<ResolveInfo> resolveInfos = + context.getPackageManager().queryIntentServicesAsUser( + new Intent(AutofillService.SERVICE_INTERFACE), + PackageManager.GET_META_DATA, + user); + for (ResolveInfo resolveInfo : resolveInfos) { + final ServiceInfo serviceInfo = resolveInfo.serviceInfo; + try { + services.add(new AutofillServiceInfo(context, serviceInfo)); + } catch (SecurityException e) { + // Service does not declare the proper permission, ignore it. + Log.w(TAG, "Error getting info for " + serviceInfo + ": " + e); + } + } + return services; + } + @Override public String toString() { final StringBuilder builder = new StringBuilder(); |
