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

Commit ebc8ee26 authored by Philip P. Moltmann's avatar Philip P. Moltmann
Browse files

Not send switch intents if there is no real intent

When resolving activtities for the USB device/accessory connection UI a
special intent that allows to switch between profiles get added. This
also gets added if there is no activity in the secondary profile that
can be started.

Fixes: 36544815
Test: Added work profile. Add USB handling app only to personal profile
      and plugged in USB device -> no crash anymore

Change-Id: I311ddd53b3ff0c8406e62bac57972d4b790ebddc
parent fb54aee3
Loading
Loading
Loading
Loading
+36 −5
Original line number Diff line number Diff line
@@ -1039,18 +1039,34 @@ class UsbProfileGroupSettingsManager {
     * Start the appropriate package when an device/accessory got attached.
     *
     * @param intent The intent to start the package
     * @param matches The available resolutions of the intent
     * @param rawMatches The available resolutions of the intent
     * @param defaultActivity The default activity for the device (if set)
     * @param device The device if a device was attached
     * @param accessory The accessory if a device was attached
     */
    private void resolveActivity(@NonNull Intent intent, @NonNull ArrayList<ResolveInfo> matches,
    private void resolveActivity(@NonNull Intent intent, @NonNull ArrayList<ResolveInfo> rawMatches,
            @Nullable ActivityInfo defaultActivity, @Nullable UsbDevice device,
            @Nullable UsbAccessory accessory) {
        int count = matches.size();
        final int numRawMatches = rawMatches.size();

        // The raw matches contain the activities that can be started but also the intents to switch
        // between the profiles
        int numParentActivityMatches = 0;
        int numNonParentActivityMatches = 0;
        for (int i = 0; i < numRawMatches; i++) {
            final ResolveInfo rawMatch = rawMatches.get(i);
            if (!rawMatch.getComponentInfo().name.equals(FORWARD_INTENT_TO_MANAGED_PROFILE)) {
                if (UserHandle.getUserHandleForUid(
                        rawMatch.activityInfo.applicationInfo.uid).equals(mParentUser)) {
                    numParentActivityMatches++;
                } else {
                    numNonParentActivityMatches++;
                }
            }
        }

        // don't show the resolver activity if there are no choices available
        if (count == 0) {
        if (numParentActivityMatches + numNonParentActivityMatches == 0) {
            if (accessory != null) {
                String uri = accessory.getUri();
                if (uri != null && uri.length() > 0) {
@@ -1073,6 +1089,21 @@ class UsbProfileGroupSettingsManager {
            return;
        }

        // If only one profile has activity matches, we need to remove all switch intents
        ArrayList<ResolveInfo> matches;
        if (numParentActivityMatches == 0 || numNonParentActivityMatches == 0) {
            matches = new ArrayList<>(numParentActivityMatches + numNonParentActivityMatches);

            for (int i = 0; i < numRawMatches; i++) {
                ResolveInfo rawMatch = rawMatches.get(i);
                if (!rawMatch.getComponentInfo().name.equals(FORWARD_INTENT_TO_MANAGED_PROFILE)) {
                    matches.add(rawMatch);
                }
            }
        } else {
            matches = rawMatches;
        }

        if (defaultActivity != null) {
            UsbUserSettingsManager defaultRIUserSettings = mSettingsManager.getSettingsForUser(
                    UserHandle.getUserId(defaultActivity.applicationInfo.uid));
@@ -1101,7 +1132,7 @@ class UsbProfileGroupSettingsManager {
            resolverIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            UserHandle user;

            if (count == 1) {
            if (matches.size() == 1) {
                ResolveInfo rInfo = matches.get(0);

                // start UsbConfirmActivity if there is only one choice