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

Commit da34cd4f authored by Michal Karpinski's avatar Michal Karpinski
Browse files

Allow companion apps to start background activities from

PendingIntents (even if they aren't foreground)

And push companion packages to ATMS after the user is
unlocked.

Bug: 129757565
Test: atest WmTests:ActivityStarterTests
Test: manual
Change-Id: Ic76d5c8a3fb096a8caf76dafb6c38212a8506f3a
parent aa22f417
Loading
Loading
Loading
Loading
+14 −0
Original line number Original line Diff line number Diff line
@@ -163,6 +163,20 @@ public class CompanionDeviceManagerService extends SystemService implements Bind
        publishBinderService(Context.COMPANION_DEVICE_SERVICE, mImpl);
        publishBinderService(Context.COMPANION_DEVICE_SERVICE, mImpl);
    }
    }


    @Override
    public void onUnlockUser(int userHandle) {
        Set<Association> associations = readAllAssociations(userHandle);
        Set<String> companionAppPackages = new HashSet<>();
        for (Association association : associations) {
            companionAppPackages.add(association.companionAppPackage);
        }
        ActivityTaskManagerInternal atmInternal = LocalServices.getService(
                ActivityTaskManagerInternal.class);
        if (atmInternal != null) {
            atmInternal.setCompanionAppPackages(userHandle, companionAppPackages);
        }
    }

    @Override
    @Override
    public void binderDied() {
    public void binderDied() {
        Handler.getMain().post(this::cleanup);
        Handler.getMain().post(this::cleanup);
+6 −1
Original line number Original line Diff line number Diff line
@@ -981,6 +981,11 @@ class ActivityStarter {
            if (isRealCallingUidPersistentSystemProcess && allowBackgroundActivityStart) {
            if (isRealCallingUidPersistentSystemProcess && allowBackgroundActivityStart) {
                return false;
                return false;
            }
            }
            // don't abort if the realCallingUid is an associated companion app
            if (mService.isAssociatedCompanionApp(UserHandle.getUserId(realCallingUid),
                    realCallingUid)) {
                return false;
            }
        }
        }
        // If we don't have callerApp at this point, no caller was provided to startActivity().
        // If we don't have callerApp at this point, no caller was provided to startActivity().
        // That's the case for PendingIntent-based starts, since the creator's process might not be
        // That's the case for PendingIntent-based starts, since the creator's process might not be
@@ -1026,7 +1031,7 @@ class ActivityStarter {
        }
        }
        // don't abort if the callingPackage has companion device
        // don't abort if the callingPackage has companion device
        final int callingUserId = UserHandle.getUserId(callingUid);
        final int callingUserId = UserHandle.getUserId(callingUid);
        if (mService.isAssociatedCompanionApp(callingUserId, callingPackage)) {
        if (mService.isAssociatedCompanionApp(callingUserId, callingUid)) {
            return false;
            return false;
        }
        }
        // don't abort if the callingPackage is temporarily whitelisted
        // don't abort if the callingPackage is temporarily whitelisted
+13 −10
Original line number Original line Diff line number Diff line
@@ -443,8 +443,8 @@ public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
    // VoiceInteractionManagerService
    // VoiceInteractionManagerService
    ComponentName mActiveVoiceInteractionServiceComponent;
    ComponentName mActiveVoiceInteractionServiceComponent;


    // A map userId and all its companion app packages
    // A map userId and all its companion app uids
    private final Map<Integer, Set<String>> mCompanionAppPackageMap = new ArrayMap<>();
    private final Map<Integer, Set<Integer>> mCompanionAppUidsMap = new ArrayMap<>();


    VrController mVrController;
    VrController mVrController;
    KeyguardController mKeyguardController;
    KeyguardController mKeyguardController;
@@ -5907,12 +5907,12 @@ public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
        }
        }
    }
    }


    boolean isAssociatedCompanionApp(int userId, String packageName) {
    boolean isAssociatedCompanionApp(int userId, int uid) {
        final Set<String> allPackages = mCompanionAppPackageMap.get(userId);
        final Set<Integer> allUids = mCompanionAppUidsMap.get(userId);
        if (allPackages == null) {
        if (allUids == null) {
            return false;
            return false;
        }
        }
        return allPackages.contains(packageName);
        return allUids.contains(uid);
    }
    }


    final class H extends Handler {
    final class H extends Handler {
@@ -7291,13 +7291,16 @@ public class ActivityTaskManagerService extends IActivityTaskManager.Stub {


        @Override
        @Override
        public void setCompanionAppPackages(int userId, Set<String> companionAppPackages) {
        public void setCompanionAppPackages(int userId, Set<String> companionAppPackages) {
            // Deep copy all content to make sure we do not rely on the source
            // Translate package names into UIDs
            final Set<String> result = new HashSet<>();
            final Set<Integer> result = new HashSet<>();
            for (String pkg : companionAppPackages) {
            for (String pkg : companionAppPackages) {
                result.add(pkg);
                final int uid = getPackageManagerInternalLocked().getPackageUid(pkg, 0, userId);
                if (uid >= 0) {
                    result.add(uid);
                }
            }
            }
            synchronized (mGlobalLock) {
            synchronized (mGlobalLock) {
                mCompanionAppPackageMap.put(userId, result);
                mCompanionAppUidsMap.put(userId, result);
            }
            }
        }
        }
    }
    }