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

Commit 713fe0a7 authored by Grant Menke's avatar Grant Menke
Browse files

Ensure current user is used when setting the PhoneAccountSuggestionService.

This CL updates the relevant TelecomShellCommand code path to ensure the current primary user is being used when setting the PhoneAccountSuggestionService for Telecom CTS test purposes in PhoneAccountSuggestionHelper. In HSUM mode, this should be user 10 and in non HSUM mode this should be user 0. Without this change, PhoneAccountSuggestionServiceTest tests were failing in HSUM mode.

Test: atest PhoneAccountSuggestionServiceTest
Bug: 358587742
Flag: EXEMPT bugfix
Change-Id: I70ac06b08f0b94126c2f88adb11479785499e5d1
parent 79387f09
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -2059,7 +2059,8 @@ public class CallsManager extends Call.ListenerBase
                        return CompletableFuture.completedFuture(
                                Collections.singletonList(suggestion));
                    }
                    return PhoneAccountSuggestionHelper.bindAndGetSuggestions(mContext,
                    Context userContext = mContext.createContextAsUser(getCurrentUserHandle(), 0);
                    return PhoneAccountSuggestionHelper.bindAndGetSuggestions(userContext,
                            finalCall.getHandle(), potentialPhoneAccounts);
                }, new LoggedHandlerExecutor(outgoingCallHandler, "CM.cOCSS", mLock));

+39 −4
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.UserHandle;
import android.telecom.Log;
import android.telecom.Logging.Session;
import android.telecom.PhoneAccountHandle;
@@ -46,6 +47,7 @@ import java.util.stream.Stream;
public class PhoneAccountSuggestionHelper {
    private static final String TAG = PhoneAccountSuggestionHelper.class.getSimpleName();
    private static ComponentName sOverrideComponent;
    private static UserHandle sOverrideUserHandle;

    /**
     * @return A future (possible already complete) that contains a list of suggestions.
@@ -53,6 +55,15 @@ public class PhoneAccountSuggestionHelper {
    public static CompletableFuture<List<PhoneAccountSuggestion>>
    bindAndGetSuggestions(Context context, Uri handle,
            List<PhoneAccountHandle> availablePhoneAccounts) {
        Context userContext;
        if (sOverrideUserHandle != null) {
            userContext = context.createContextAsUser(sOverrideUserHandle, 0);
            Log.i(TAG, "bindAndGetSuggestions created context as user;  userContext=%s",
                    userContext);
        } else {
            userContext = context;
        }

        // Use the default list if there's no handle
        if (handle == null) {
            return CompletableFuture.completedFuture(getDefaultSuggestions(availablePhoneAccounts));
@@ -60,7 +71,7 @@ public class PhoneAccountSuggestionHelper {
        String number = PhoneNumberUtils.extractNetworkPortion(handle.getSchemeSpecificPart());

        // Use the default list if there's no service on the device.
        ServiceInfo suggestionServiceInfo = getSuggestionServiceInfo(context);
        ServiceInfo suggestionServiceInfo = getSuggestionServiceInfo(userContext);
        if (suggestionServiceInfo == null) {
            return CompletableFuture.completedFuture(getDefaultSuggestions(availablePhoneAccounts));
        }
@@ -124,7 +135,7 @@ public class PhoneAccountSuggestionHelper {
            }
        };

        if (!context.bindService(bindIntent, serviceConnection, Context.BIND_AUTO_CREATE)) {
        if (!userContext.bindService(bindIntent, serviceConnection, Context.BIND_AUTO_CREATE)) {
            Log.i(TAG, "Cancelling suggestion process due to bind failure.");
            future.complete(getDefaultSuggestions(availablePhoneAccounts));
        }
@@ -143,7 +154,7 @@ public class PhoneAccountSuggestionHelper {
                        Log.endSession();
                    }
                },
                Timeouts.getPhoneAccountSuggestionServiceTimeout(context.getContentResolver()));
                Timeouts.getPhoneAccountSuggestionServiceTimeout(userContext.getContentResolver()));
        return future;
    }

@@ -162,10 +173,25 @@ public class PhoneAccountSuggestionHelper {
    }

    private static ServiceInfo getSuggestionServiceInfo(Context context) {
        PackageManager packageManager = context.getPackageManager();
        Context userContext;
        if (sOverrideUserHandle != null) {
            userContext = context.createContextAsUser(sOverrideUserHandle, 0);
            Log.i(TAG, "getSuggestionServiceInfo: Created context as user; userContext= %s",
                    userContext);
        } else {
            userContext = context;
        }

        PackageManager packageManager = userContext.getPackageManager();

        Intent queryIntent = new Intent();
        queryIntent.setAction(PhoneAccountSuggestionService.SERVICE_INTERFACE);

        if (packageManager == null) {
            Log.i(TAG, "getSuggestionServiceInfo: PackageManager is null. Using defaults.");
            return null;
        }

        List<ResolveInfo> services;
        if (sOverrideComponent == null) {
            services = packageManager.queryIntentServices(queryIntent,
@@ -199,6 +225,15 @@ public class PhoneAccountSuggestionHelper {
        }
    }

    static void setOverrideUserHandle(UserHandle userHandle) {
        try {
            sOverrideUserHandle = userHandle;
        } catch (Exception e) {
            sOverrideUserHandle = null;
            throw e;
        }
    }

    private static List<PhoneAccountSuggestion> getDefaultSuggestions(
            List<PhoneAccountHandle> phoneAccountHandles) {
        return phoneAccountHandles.stream().map(phoneAccountHandle ->
+3 −1
Original line number Diff line number Diff line
@@ -2555,7 +2555,8 @@ public class TelecomServiceImpl {
        }

        @Override
        public void setTestPhoneAcctSuggestionComponent(String flattenedComponentName) {
        public void setTestPhoneAcctSuggestionComponent(String flattenedComponentName,
                UserHandle userHandle) {
            try {
                Log.startSession("TSI.sPASA");
                enforceModifyPermission();
@@ -2565,6 +2566,7 @@ public class TelecomServiceImpl {
                }
                synchronized (mLock) {
                    PhoneAccountSuggestionHelper.setOverrideServiceName(flattenedComponentName);
                    PhoneAccountSuggestionHelper.setOverrideUserHandle(userHandle);
                }
            } finally {
                Log.endSession();
+18 −1
Original line number Diff line number Diff line
@@ -341,7 +341,8 @@ public class TelecomShellCommand extends BasicShellCommandHandler {

    private void runSetTestPhoneAcctSuggestionComponent() throws RemoteException {
        final String componentName = getNextArg();
        mTelecomService.setTestPhoneAcctSuggestionComponent(componentName);
        final UserHandle userHandle = getUserHandleFromArgs();
        mTelecomService.setTestPhoneAcctSuggestionComponent(componentName, userHandle);
    }

    private void runSetUserSelectedOutgoingPhoneAccount() throws RemoteException {
@@ -457,6 +458,22 @@ public class TelecomShellCommand extends BasicShellCommandHandler {
        mTelecomService.requestLogMark(message);
    }

    private UserHandle getUserHandleFromArgs() throws RemoteException {
        if (TextUtils.isEmpty(peekNextArg())) {
            return null;
        }
        final String userSnInStr = getNextArgRequired();
        UserHandle userHandle;
        try {
            final int userSn = Integer.parseInt(userSnInStr);
            userHandle = UserHandle.of(getUserManager().getUserHandle(userSn));
        } catch (NumberFormatException ex) {
            Log.w(this, "getPhoneAccountHandleFromArgs - invalid user %s", userSnInStr);
            throw new IllegalArgumentException ("Invalid user serial number " + userSnInStr);
        }
        return userHandle;
    }

    private PhoneAccountHandle getPhoneAccountHandleFromArgs() throws RemoteException {
        if (TextUtils.isEmpty(peekNextArg())) {
            return null;