Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a2e6c888 authored by Omer Ozer's avatar Omer Ozer
Browse files

Honor the default wallet role in QuickAccessWallet.

Flag: ACONFIG android.permission.flags DEVELOPMENT
Bug: 291794775
Test: local
Change-Id: Ifff51770350d9d97a6eb6768191eae1900e31fcd
parent 16677ccc
Loading
Loading
Loading
Loading
+25 −4
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package android.service.quickaccesswallet;
import android.Manifest;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.role.RoleManager;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
@@ -30,6 +31,7 @@ import android.content.res.Resources;
import android.content.res.TypedArray;
import android.content.res.XmlResourceParser;
import android.graphics.drawable.Drawable;
import android.os.Binder;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.AttributeSet;
@@ -69,12 +71,17 @@ class QuickAccessWalletServiceInfo {

    @Nullable
    static QuickAccessWalletServiceInfo tryCreate(@NonNull Context context) {
        String defaultAppPackageName = getDefaultWalletApp(context);

        if (defaultAppPackageName == null) {
            ComponentName defaultPaymentApp = getDefaultPaymentApp(context);
            if (defaultPaymentApp == null) {
                return null;
            }
            defaultAppPackageName = defaultPaymentApp.getPackageName();
        }

        ServiceInfo serviceInfo = getWalletServiceInfo(context, defaultPaymentApp.getPackageName());
        ServiceInfo serviceInfo = getWalletServiceInfo(context, defaultAppPackageName);
        if (serviceInfo == null) {
            return null;
        }
@@ -92,6 +99,20 @@ class QuickAccessWalletServiceInfo {
        return new QuickAccessWalletServiceInfo(serviceInfo, metadata, tileServiceMetadata);
    }

    private static String getDefaultWalletApp(Context context) {
        final long token = Binder.clearCallingIdentity();
        try {
            RoleManager roleManager = context.getSystemService(RoleManager.class);
            if (roleManager.isRoleAvailable(RoleManager.ROLE_WALLET)) {
                List<String> roleHolders = roleManager.getRoleHolders(RoleManager.ROLE_WALLET);
                return roleHolders.isEmpty() ? null : roleHolders.get(0);
            }
        } finally {
            Binder.restoreCallingIdentity(token);
        }
        return null;
    }

    private static ComponentName getDefaultPaymentApp(Context context) {
        ContentResolver cr = context.getContentResolver();
        String comp = Settings.Secure.getString(cr, Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT);
+1 −0
Original line number Diff line number Diff line
@@ -5695,6 +5695,7 @@
         @hide -->
    <permission android:name="android.permission.MANAGE_ROLE_HOLDERS"
                android:protectionLevel="signature|installer|module" />
    <uses-permission android:name="android.permission.MANAGE_ROLE_HOLDERS" />

    <!-- @SystemApi Allows an application to manage the holders of roles associated with default
         applications.
+6 −3
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import static android.graphics.drawable.Icon.TYPE_URI;
import static android.provider.Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT;

import static com.android.systemui.wallet.controller.QuickAccessWalletController.WalletChangeEvent.DEFAULT_PAYMENT_APP_CHANGE;
import static com.android.systemui.wallet.controller.QuickAccessWalletController.WalletChangeEvent.DEFAULT_WALLET_APP_CHANGE;
import static com.android.systemui.wallet.util.WalletCardUtilsKt.getPaymentCards;

import android.content.Intent;
@@ -42,7 +43,6 @@ import androidx.annotation.Nullable;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.jank.InteractionJankMonitor;
import com.android.internal.logging.MetricsLogger;
import com.android.systemui.res.R;
import com.android.systemui.animation.ActivityLaunchAnimator;
import com.android.systemui.dagger.qualifiers.Background;
import com.android.systemui.dagger.qualifiers.Main;
@@ -54,6 +54,7 @@ import com.android.systemui.qs.QSHost;
import com.android.systemui.qs.QsEventLogger;
import com.android.systemui.qs.logging.QSLogger;
import com.android.systemui.qs.tileimpl.QSTileImpl;
import com.android.systemui.res.R;
import com.android.systemui.statusbar.policy.KeyguardStateController;
import com.android.systemui.util.settings.SecureSettings;
import com.android.systemui.wallet.controller.QuickAccessWalletController;
@@ -118,7 +119,8 @@ public class QuickAccessWalletTile extends QSTileImpl<QSTile.State> {
    protected void handleSetListening(boolean listening) {
        super.handleSetListening(listening);
        if (listening) {
            mController.setupWalletChangeObservers(mCardRetriever, DEFAULT_PAYMENT_APP_CHANGE);
            mController.setupWalletChangeObservers(mCardRetriever, DEFAULT_PAYMENT_APP_CHANGE,
                    DEFAULT_WALLET_APP_CHANGE);
            if (!mController.getWalletClient().isWalletServiceAvailable()
                    || !mController.getWalletClient().isWalletFeatureAvailable()) {
                Log.i(TAG, "QAW service is unavailable, recreating the wallet client.");
@@ -201,7 +203,8 @@ public class QuickAccessWalletTile extends QSTileImpl<QSTile.State> {
    @Override
    protected void handleDestroy() {
        super.handleDestroy();
        mController.unregisterWalletChangeObservers(DEFAULT_PAYMENT_APP_CHANGE);
        mController.unregisterWalletChangeObservers(DEFAULT_PAYMENT_APP_CHANGE,
                DEFAULT_WALLET_APP_CHANGE);
    }

    private class WalletCardRetriever implements
+36 −1
Original line number Diff line number Diff line
@@ -17,10 +17,13 @@
package com.android.systemui.wallet.controller;

import static com.android.systemui.wallet.controller.QuickAccessWalletController.WalletChangeEvent.DEFAULT_PAYMENT_APP_CHANGE;
import static com.android.systemui.wallet.controller.QuickAccessWalletController.WalletChangeEvent.DEFAULT_WALLET_APP_CHANGE;
import static com.android.systemui.wallet.controller.QuickAccessWalletController.WalletChangeEvent.WALLET_PREFERENCE_CHANGE;

import android.annotation.WorkerThread;
import android.app.PendingIntent;
import android.app.role.OnRoleHoldersChangedListener;
import android.app.role.RoleManager;
import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
@@ -31,12 +34,12 @@ import android.service.quickaccesswallet.QuickAccessWalletClient;
import android.service.quickaccesswallet.QuickAccessWalletClientImpl;
import android.util.Log;

import com.android.systemui.res.R;
import com.android.systemui.animation.ActivityLaunchAnimator;
import com.android.systemui.dagger.SysUISingleton;
import com.android.systemui.dagger.qualifiers.Background;
import com.android.systemui.dagger.qualifiers.Main;
import com.android.systemui.plugins.ActivityStarter;
import com.android.systemui.res.R;
import com.android.systemui.util.settings.SecureSettings;
import com.android.systemui.util.time.SystemClock;
import com.android.systemui.wallet.ui.WalletActivity;
@@ -58,6 +61,7 @@ public class QuickAccessWalletController {
     */
    public enum WalletChangeEvent {
        DEFAULT_PAYMENT_APP_CHANGE,
        DEFAULT_WALLET_APP_CHANGE,
        WALLET_PREFERENCE_CHANGE,
    }

@@ -71,9 +75,12 @@ public class QuickAccessWalletController {

    private QuickAccessWalletClient mQuickAccessWalletClient;
    private ContentObserver mWalletPreferenceObserver;
    private RoleManager mRoleManager;
    private OnRoleHoldersChangedListener mDefaultWalletAppObserver;
    private ContentObserver mDefaultPaymentAppObserver;
    private int mWalletPreferenceChangeEvents = 0;
    private int mDefaultPaymentAppChangeEvents = 0;
    private int mDefaultWalletAppChangeEvents = 0;
    private boolean mWalletEnabled = false;
    private long mQawClientCreatedTimeMillis;

@@ -89,6 +96,7 @@ public class QuickAccessWalletController {
        mExecutor = executor;
        mBgExecutor = bgExecutor;
        mSecureSettings = secureSettings;
        mRoleManager = mContext.getSystemService(RoleManager.class);
        mQuickAccessWalletClient = quickAccessWalletClient;
        mClock = clock;
        mQawClientCreatedTimeMillis = mClock.elapsedRealtime();
@@ -122,6 +130,8 @@ public class QuickAccessWalletController {
                setupWalletPreferenceObserver();
            } else if (event == DEFAULT_PAYMENT_APP_CHANGE) {
                setupDefaultPaymentAppObserver(cardsRetriever);
            } else if (event == DEFAULT_WALLET_APP_CHANGE) {
                setupDefaultWalletAppObserver(cardsRetriever);
            }
        }
    }
@@ -141,6 +151,12 @@ public class QuickAccessWalletController {
                if (mDefaultPaymentAppChangeEvents == 0) {
                    mSecureSettings.unregisterContentObserver(mDefaultPaymentAppObserver);
                }
            } else if (event == DEFAULT_WALLET_APP_CHANGE && mDefaultWalletAppObserver != null) {
                mDefaultWalletAppChangeEvents--;
                if (mDefaultWalletAppChangeEvents == 0) {
                    mRoleManager.removeOnRoleHoldersChangedListenerAsUser(mDefaultWalletAppObserver,
                            UserHandle.ALL);
                }
            }
        }
    }
@@ -300,6 +316,25 @@ public class QuickAccessWalletController {
        mDefaultPaymentAppChangeEvents++;
    }

    private void setupDefaultWalletAppObserver(
            QuickAccessWalletClient.OnWalletCardsRetrievedCallback cardsRetriever) {
        if (mDefaultWalletAppObserver == null) {
            mDefaultWalletAppObserver = (roleName, user) -> {
                if (!roleName.equals(RoleManager.ROLE_WALLET)) {
                    return;
                }
                mExecutor.execute(() -> {
                    reCreateWalletClient();
                    updateWalletPreference();
                    queryWalletCards(cardsRetriever);
                });
            };
            mRoleManager.addOnRoleHoldersChangedListenerAsUser(mExecutor,
                    mDefaultWalletAppObserver, UserHandle.ALL);
        }
        mDefaultWalletAppChangeEvents++;
    }

    private void setupWalletPreferenceObserver() {
        if (mWalletPreferenceObserver == null) {
            mWalletPreferenceObserver = new ContentObserver(null /* handler */) {
+6 −2
Original line number Diff line number Diff line
@@ -84,7 +84,9 @@ constructor(
                        walletController.setupWalletChangeObservers(
                            callback,
                            QuickAccessWalletController.WalletChangeEvent.WALLET_PREFERENCE_CHANGE,
                            QuickAccessWalletController.WalletChangeEvent.DEFAULT_PAYMENT_APP_CHANGE
                            QuickAccessWalletController.WalletChangeEvent
                                .DEFAULT_PAYMENT_APP_CHANGE,
                            QuickAccessWalletController.WalletChangeEvent.DEFAULT_WALLET_APP_CHANGE
                        )
                        walletController.updateWalletPreference()
                        walletController.queryWalletCards(callback, MAX_CARDS)
@@ -94,7 +96,9 @@ constructor(
                                QuickAccessWalletController.WalletChangeEvent
                                    .WALLET_PREFERENCE_CHANGE,
                                QuickAccessWalletController.WalletChangeEvent
                                    .DEFAULT_PAYMENT_APP_CHANGE
                                    .DEFAULT_PAYMENT_APP_CHANGE,
                                QuickAccessWalletController.WalletChangeEvent
                                    .DEFAULT_WALLET_APP_CHANGE
                            )
                        }
                    }