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

Commit c03a2bfe authored by Kholoud Mohamed's avatar Kholoud Mohamed Committed by Android (Google) Code Review
Browse files

Merge "Add metrics in AccountManager" into sc-dev

parents bd351242 a74b54f7
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -1027,7 +1027,8 @@ public class AccountManager {
    public boolean addAccountExplicitly(Account account, String password, Bundle userdata) {
        if (account == null) throw new IllegalArgumentException("account is null");
        try {
            return mService.addAccountExplicitly(account, password, userdata);
            return mService.addAccountExplicitly(
                    account, password, userdata, mContext.getOpPackageName());
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
@@ -1064,7 +1065,7 @@ public class AccountManager {
            throw new IllegalArgumentException("account is null");
        try {
            return mService.addAccountExplicitlyWithVisibility(account, password, extras,
                    visibility);
                    visibility, mContext.getOpPackageName());
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
+2 −2
Original line number Diff line number Diff line
@@ -43,7 +43,7 @@ interface IAccountManager {
        in String[] features, String opPackageName);
    void getAccountsByFeatures(in IAccountManagerResponse response, String accountType,
        in String[] features, String opPackageName);
    boolean addAccountExplicitly(in Account account, String password, in Bundle extras);
    boolean addAccountExplicitly(in Account account, String password, in Bundle extras, in String opPackageName);
    void removeAccountAsUser(in IAccountManagerResponse response, in Account account,
        boolean expectActivityLaunch, int userId);
    boolean removeAccountExplicitly(in Account account);
@@ -107,7 +107,7 @@ interface IAccountManager {
    /* Returns Map<String, Integer> from package name to visibility with all values stored for given account */
    Map getPackagesAndVisibilityForAccount(in Account account);
    boolean addAccountExplicitlyWithVisibility(in Account account, String password, in Bundle extras,
            in Map visibility);
            in Map visibility, in String opPackageName);
    boolean setAccountVisibility(in Account a, in String packageName, int newVisibility);
    int getAccountVisibility(in Account a, in String packageName);
    /* Type may be null returns Map <Account, Integer>*/
+107 −42
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ import android.app.INotificationManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.admin.DevicePolicyEventLogger;
import android.app.admin.DevicePolicyManager;
import android.app.admin.DevicePolicyManagerInternal;
import android.content.BroadcastReceiver;
@@ -85,6 +86,7 @@ import android.os.StrictMode;
import android.os.SystemClock;
import android.os.UserHandle;
import android.os.UserManager;
import android.stats.devicepolicy.DevicePolicyEnums;
import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;
@@ -105,7 +107,6 @@ import com.android.internal.util.Preconditions;
import com.android.server.LocalServices;
import com.android.server.ServiceThread;
import com.android.server.SystemService;
import com.android.server.SystemService.TargetUser;

import com.google.android.collect.Lists;
import com.google.android.collect.Sets;
@@ -456,7 +457,7 @@ public class AccountManagerService

    @Override
    public boolean addAccountExplicitlyWithVisibility(Account account, String password,
            Bundle extras, Map packageToVisibility) {
            Bundle extras, Map packageToVisibility, String opPackageName) {
        Bundle.setDefusable(extras, true);
        int callingUid = Binder.getCallingUid();
        int userId = UserHandle.getCallingUserId();
@@ -481,7 +482,7 @@ public class AccountManagerService
        try {
            UserAccounts accounts = getUserAccounts(userId);
            return addAccountInternal(accounts, account, password, extras, callingUid,
                    (Map<String, Integer>) packageToVisibility);
                    (Map<String, Integer>) packageToVisibility, opPackageName);
        } finally {
            restoreCallingIdentity(identityToken);
        }
@@ -1650,8 +1651,10 @@ public class AccountManagerService
    }

    @Override
    public boolean addAccountExplicitly(Account account, String password, Bundle extras) {
        return addAccountExplicitlyWithVisibility(account, password, extras, null);
    public boolean addAccountExplicitly(
            Account account, String password, Bundle extras, String opPackageName) {
        return addAccountExplicitlyWithVisibility(
                account, password, extras, /* packageToVisibility= */ null, opPackageName);
    }

    @Override
@@ -1807,7 +1810,8 @@ public class AccountManagerService
    }

    private boolean addAccountInternal(UserAccounts accounts, Account account, String password,
            Bundle extras, int callingUid, Map<String, Integer> packageToVisibility) {
            Bundle extras, int callingUid, Map<String, Integer> packageToVisibility,
            String opPackageName) {
        Bundle.setDefusable(extras, true);
        if (account == null) {
            return false;
@@ -1879,9 +1883,59 @@ public class AccountManagerService
        // Only send LOGIN_ACCOUNTS_CHANGED when the database changed.
        sendAccountsChangedBroadcast(accounts.userId);

        logAddAccountExplicitlyMetrics(opPackageName, account.type, packageToVisibility);
        return true;
    }

    private void logAddAccountExplicitlyMetrics(
            String callerPackage, String accountType,
            @Nullable Map<String, Integer> accountVisibility) {
        // Although this is not a 'device policy' API, enterprise is the current use case.
        DevicePolicyEventLogger
                .createEvent(DevicePolicyEnums.ADD_ACCOUNT_EXPLICITLY)
                .setStrings(
                        TextUtils.emptyIfNull(accountType),
                        TextUtils.emptyIfNull(callerPackage),
                        findPackagesPerVisibility(accountVisibility))
                .write();
    }

    private String[] findPackagesPerVisibility(@Nullable Map<String, Integer> accountVisibility) {
        Map<Integer, Set<String>> packagesPerVisibility = new HashMap<>();
        if (accountVisibility != null) {
            for (Entry<String, Integer> entry : accountVisibility.entrySet()) {
                if (!packagesPerVisibility.containsKey(entry.getValue())) {
                    packagesPerVisibility.put(entry.getValue(), new HashSet<>());
                }
                packagesPerVisibility.get(entry.getValue()).add(entry.getKey());
            }
        }

        String[] packagesPerVisibilityStr = new String[5];
        packagesPerVisibilityStr[AccountManager.VISIBILITY_UNDEFINED] = getPackagesForVisibilityStr(
                AccountManager.VISIBILITY_UNDEFINED, packagesPerVisibility);
        packagesPerVisibilityStr[AccountManager.VISIBILITY_VISIBLE] = getPackagesForVisibilityStr(
                AccountManager.VISIBILITY_VISIBLE, packagesPerVisibility);
        packagesPerVisibilityStr[AccountManager.VISIBILITY_USER_MANAGED_VISIBLE] =
                getPackagesForVisibilityStr(
                        AccountManager.VISIBILITY_USER_MANAGED_VISIBLE, packagesPerVisibility);
        packagesPerVisibilityStr[AccountManager.VISIBILITY_NOT_VISIBLE] =
                getPackagesForVisibilityStr(
                        AccountManager.VISIBILITY_NOT_VISIBLE, packagesPerVisibility);
        packagesPerVisibilityStr[AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE] =
                getPackagesForVisibilityStr(
                        AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE, packagesPerVisibility);
        return packagesPerVisibilityStr;
    }

    private String getPackagesForVisibilityStr(
            int visibility, Map<Integer, Set<String>> packagesPerVisibility) {
        return visibility + ":"
                + (packagesPerVisibility.containsKey(visibility)
                    ? TextUtils.join(",", packagesPerVisibility.get(visibility))
                    : "");
    }

    private boolean isLocalUnlockedUser(int userId) {
        synchronized (mUsers) {
            return mLocalUnlockedUsers.get(userId);
@@ -2898,6 +2952,7 @@ public class AccountManagerService
            if (!customTokens && permissionGranted) {
                String authToken = readAuthTokenInternal(accounts, account, authTokenType);
                if (authToken != null) {
                    logGetAuthTokenMetrics(callerPkg, account.type);
                    Bundle result = new Bundle();
                    result.putString(AccountManager.KEY_AUTHTOKEN, authToken);
                    result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
@@ -2920,6 +2975,7 @@ public class AccountManagerService
                        callerPkg,
                        callerPkgSigDigest);
                if (token != null) {
                    logGetAuthTokenMetrics(callerPkg, account.type);
                    if (Log.isLoggable(TAG, Log.VERBOSE)) {
                        Log.v(TAG, "getAuthToken: cache hit ofr custom token authenticator.");
                    }
@@ -2958,6 +3014,7 @@ public class AccountManagerService
                        mAuthenticator.getAuthTokenLabel(this, authTokenType);
                    } else {
                        mAuthenticator.getAuthToken(this, account, authTokenType, loginOptions);
                        logGetAuthTokenMetrics(callerPkg, account.type);
                    }
                }

@@ -3040,6 +3097,16 @@ public class AccountManagerService
        }
    }

    private void logGetAuthTokenMetrics(final String callerPackage, String accountType) {
        // Although this is not a 'device policy' API, enterprise is the current use case.
        DevicePolicyEventLogger
                .createEvent(DevicePolicyEnums.GET_ACCOUNT_AUTH_TOKEN)
                .setStrings(
                        TextUtils.emptyIfNull(callerPackage),
                        TextUtils.emptyIfNull(accountType))
                .write();
    }

    private byte[] calculatePackageSignatureDigest(String callerPkg) {
        MessageDigest digester;
        try {
@@ -3189,38 +3256,8 @@ public class AccountManagerService
                    userId);
            return;
        }

        final int pid = Binder.getCallingPid();
        final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn;
        options.putInt(AccountManager.KEY_CALLER_UID, uid);
        options.putInt(AccountManager.KEY_CALLER_PID, pid);

        int usrId = UserHandle.getCallingUserId();
        final long identityToken = clearCallingIdentity();
        try {
            UserAccounts accounts = getUserAccounts(usrId);
            logRecordWithUid(
                    accounts, AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS,
                    uid);
            new Session(accounts, response, accountType, expectActivityLaunch,
                    true /* stripAuthTokenFromResult */, null /* accountName */,
                    false /* authDetailsRequired */, true /* updateLastAuthenticationTime */) {
                @Override
                public void run() throws RemoteException {
                    mAuthenticator.addAccount(this, mAccountType, authTokenType, requiredFeatures,
                            options);
                }

                @Override
                protected String toDebugString(long now) {
                    return super.toDebugString(now) + ", addAccount"
                            + ", accountType " + accountType
                            + ", requiredFeatures " + Arrays.toString(requiredFeatures);
                }
            }.bind();
        } finally {
            restoreCallingIdentity(identityToken);
        }
        addAccountAndLogMetrics(response, accountType, authTokenType, requiredFeatures,
                expectActivityLaunch, optionsIn, userId);
    }

    @Override
@@ -3270,7 +3307,14 @@ public class AccountManagerService
                    userId);
            return;
        }
        addAccountAndLogMetrics(response, accountType, authTokenType, requiredFeatures,
                expectActivityLaunch, optionsIn, userId);
    }

    private void addAccountAndLogMetrics(
            IAccountManagerResponse response, String accountType,
            String authTokenType, String[] requiredFeatures,
            boolean expectActivityLaunch, Bundle optionsIn, int userId) {
        final int pid = Binder.getCallingPid();
        final int uid = Binder.getCallingUid();
        final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn;
@@ -3288,8 +3332,12 @@ public class AccountManagerService
                    false /* authDetailsRequired */, true /* updateLastAuthenticationTime */) {
                @Override
                public void run() throws RemoteException {
                    mAuthenticator.addAccount(this, mAccountType, authTokenType, requiredFeatures,
                            options);
                    mAuthenticator.addAccount(
                            this, mAccountType, authTokenType, requiredFeatures, options);
                    String callerPackage = options.getString(
                            AccountManager.KEY_ANDROID_PACKAGE_NAME);
                    logAddAccountMetrics(
                            callerPackage, accountType, requiredFeatures, authTokenType);
                }

                @Override
@@ -3307,6 +3355,22 @@ public class AccountManagerService
        }
    }

    private void logAddAccountMetrics(
            String callerPackage, String accountType, String[] requiredFeatures,
            String authTokenType) {
        // Although this is not a 'device policy' API, enterprise is the current use case.
        DevicePolicyEventLogger
                .createEvent(DevicePolicyEnums.ADD_ACCOUNT)
                .setStrings(
                        TextUtils.emptyIfNull(accountType),
                        TextUtils.emptyIfNull(callerPackage),
                        TextUtils.emptyIfNull(authTokenType),
                        requiredFeatures == null
                                ? ""
                                : TextUtils.join(";", requiredFeatures))
                .write();
    }

    @Override
    public void startAddAccountSession(
            final IAccountManagerResponse response,
@@ -3378,6 +3442,7 @@ public class AccountManagerService
                public void run() throws RemoteException {
                    mAuthenticator.startAddAccountSession(this, mAccountType, authTokenType,
                            requiredFeatures, options);
                    logAddAccountMetrics(callerPkg, accountType, requiredFeatures, authTokenType);
                }

                @Override
+177 −46

File changed.

Preview size limit exceeded, changes collapsed.