From 71bd41ce9212f4204a7211d0acb6adda1d8fd43d Mon Sep 17 00:00:00 2001 From: Scottie Biddle Date: Thu, 17 Feb 2022 11:17:17 -0800 Subject: [PATCH] Add getter for useTargetActivityForQuickAccess. Since this is hardcoded XML metadata, the implementation of this method reads the XML metadata to derive a value. If there are any errors in reading the metadata, the method returns false. The method is final, because subclasses should not be able to change this. This is in response to API council feedback. CTS tests are included in the same topic. Bug: 218860062 Test: atest QuickAccessWalletServiceTest Change-Id: I7bb53f73b836b0b17a723394a6abf2c3df1c8b38 --- core/api/current.txt | 1 + .../QuickAccessWalletService.java | 49 +++++++++++++++++++ .../QuickAccessWalletServiceInfo.java | 6 +-- 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/core/api/current.txt b/core/api/current.txt index cda5651549a41..eab6f8006dfa7 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -39163,6 +39163,7 @@ package android.service.quickaccesswallet { public abstract class QuickAccessWalletService extends android.app.Service { ctor public QuickAccessWalletService(); method @Nullable public android.app.PendingIntent getTargetActivityPendingIntent(); + method public final boolean getUseTargetActivityForQuickAccess(); method @Nullable public android.os.IBinder onBind(@NonNull android.content.Intent); method public abstract void onWalletCardSelected(@NonNull android.service.quickaccesswallet.SelectWalletCardRequest); method public abstract void onWalletCardsRequested(@NonNull android.service.quickaccesswallet.GetWalletCardsRequest, @NonNull android.service.quickaccesswallet.GetWalletCardsCallback); diff --git a/core/java/android/service/quickaccesswallet/QuickAccessWalletService.java b/core/java/android/service/quickaccesswallet/QuickAccessWalletService.java index 70ccd6fbd590f..1d74a6470a841 100644 --- a/core/java/android/service/quickaccesswallet/QuickAccessWalletService.java +++ b/core/java/android/service/quickaccesswallet/QuickAccessWalletService.java @@ -16,12 +16,20 @@ package android.service.quickaccesswallet; +import static android.content.pm.PackageManager.GET_META_DATA; +import static android.content.pm.PackageManager.MATCH_DEFAULT_ONLY; +import static android.content.pm.PackageManager.MATCH_DIRECT_BOOT_AWARE; +import static android.content.pm.PackageManager.MATCH_DIRECT_BOOT_UNAWARE; + import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.SdkConstant; import android.app.PendingIntent; import android.app.Service; import android.content.Intent; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; +import android.content.pm.ServiceInfo; import android.os.Build; import android.os.Handler; import android.os.IBinder; @@ -30,6 +38,8 @@ import android.os.RemoteException; import android.provider.Settings; import android.util.Log; +import java.util.List; + /** * A {@code QuickAccessWalletService} provides a list of {@code WalletCard}s shown in the Quick * Access Wallet. The Quick Access Wallet allows the user to change their selected payment method @@ -341,6 +351,45 @@ public abstract class QuickAccessWalletService extends Service { return null; } + /** + * Returns the value specified by the attribute + * {@link android.R.styleable#QuickAccessWalletService_useTargetActivityForQuickAccess} + * in the QuickAccessWalletService's metadata configuration. If the attribute or metadata is + * not supplied, returns false. + */ + public final boolean getUseTargetActivityForQuickAccess() { + Intent intent = new Intent(SERVICE_INTERFACE).setPackage(getPackageName()); + List matchedServices = getPackageManager() + .queryIntentServices(intent, + PackageManager.ResolveInfoFlags.of( + GET_META_DATA | MATCH_DIRECT_BOOT_UNAWARE | MATCH_DIRECT_BOOT_AWARE + | MATCH_DEFAULT_ONLY)); + + ResolveInfo resolveInfo = null; + + for (ResolveInfo info : matchedServices) { + ServiceInfo serviceInfo = info.serviceInfo; + if (serviceInfo == null) { + continue; + } + String serviceName = serviceInfo.name; + if (serviceName == null) { + continue; + } + if (serviceName.equals(getClass().getCanonicalName())) { + resolveInfo = info; + } + } + + if (resolveInfo == null || resolveInfo.serviceInfo == null) { + Log.i(TAG, "Matching QuickAccessWalletService not found."); + return false; + } + QuickAccessWalletServiceInfo.ServiceMetadata serviceMetadata = + QuickAccessWalletServiceInfo.parseServiceMetadata(this, resolveInfo.serviceInfo); + return serviceMetadata != null && serviceMetadata.mUseTargetActivityForQuickAccess; + } + private void sendWalletServiceEventInternal(WalletServiceEvent serviceEvent) { if (mEventListener == null) { Log.i(TAG, "No dismiss listener registered"); diff --git a/core/java/android/service/quickaccesswallet/QuickAccessWalletServiceInfo.java b/core/java/android/service/quickaccesswallet/QuickAccessWalletServiceInfo.java index cf4be739e05af..cc393a4ce6053 100644 --- a/core/java/android/service/quickaccesswallet/QuickAccessWalletServiceInfo.java +++ b/core/java/android/service/quickaccesswallet/QuickAccessWalletServiceInfo.java @@ -135,7 +135,7 @@ class QuickAccessWalletServiceInfo { return null; } - private static class ServiceMetadata { + static class ServiceMetadata { @Nullable private final String mSettingsActivity; @Nullable @@ -144,7 +144,7 @@ class QuickAccessWalletServiceInfo { private final CharSequence mShortcutShortLabel; @Nullable private final CharSequence mShortcutLongLabel; - private final boolean mUseTargetActivityForQuickAccess; + final boolean mUseTargetActivityForQuickAccess; private static ServiceMetadata empty() { return new ServiceMetadata(null, null, null, null, false); @@ -164,7 +164,7 @@ class QuickAccessWalletServiceInfo { } } - private static ServiceMetadata parseServiceMetadata(Context context, ServiceInfo serviceInfo) { + static ServiceMetadata parseServiceMetadata(Context context, ServiceInfo serviceInfo) { PackageManager pm = context.getPackageManager(); final XmlResourceParser parser = serviceInfo.loadXmlMetaData(pm, QuickAccessWalletService.SERVICE_META_DATA);