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

Commit 7b8b881f authored by Yanting Yang's avatar Yanting Yang Committed by Android (Google) Code Review
Browse files

Merge "Resolve tiles intent before prompting a dialog for profile selection"

parents 0f61d2aa 552f340d
Loading
Loading
Loading
Loading
+21 −6
Original line number Diff line number Diff line
@@ -398,26 +398,41 @@ public class DashboardFeatureProviderImpl implements DashboardFeatureProvider {
            return;
        }
        ProfileSelectDialog.updateUserHandlesIfNeeded(mContext, tile);
        mMetricsFeatureProvider.logStartedIntent(intent, sourceMetricCategory);

        if (tile.userHandle == null || tile.isPrimaryProfileOnly()) {
            mMetricsFeatureProvider.logStartedIntent(intent, sourceMetricCategory);
            activity.startActivityForResult(intent, 0);
        } else if (tile.userHandle.size() == 1) {
            mMetricsFeatureProvider.logStartedIntent(intent, sourceMetricCategory);
            activity.startActivityForResultAsUser(intent, 0, tile.userHandle.get(0));
        } else {
            mMetricsFeatureProvider.logStartedIntent(intent, sourceMetricCategory);
            final UserHandle userHandle = intent.getParcelableExtra(EXTRA_USER);
            if (userHandle != null && tile.userHandle.contains(userHandle)) {
                activity.startActivityForResultAsUser(intent, 0, userHandle);
            } else {
                return;
            }

            final List<UserHandle> resolvableUsers = getResolvableUsers(intent, tile);
            if (resolvableUsers.size() == 1) {
                activity.startActivityForResultAsUser(intent, 0, resolvableUsers.get(0));
                return;
            }

            ProfileSelectDialog.show(activity.getSupportFragmentManager(), tile,
                    sourceMetricCategory);
        }
    }
    }

    private boolean isIntentResolvable(Intent intent) {
        return mPackageManager.resolveActivity(intent, 0) != null;
    }

    private List<UserHandle> getResolvableUsers(Intent intent, Tile tile) {
        final ArrayList<UserHandle> eligibleUsers = new ArrayList<>();
        for (UserHandle user : tile.userHandle) {
            if (mPackageManager.resolveActivityAsUser(intent, 0, user.getIdentifier()) != null) {
                eligibleUsers.add(user);
            }
        }
        return eligibleUsers;
    }
}
+24 −0
Original line number Diff line number Diff line
@@ -583,4 +583,28 @@ public class DashboardFeatureProviderImplTest {
                .startActivityForResultAsUser(any(Intent.class), anyInt(), any(UserHandle.class));
        verify(mActivity).getSupportFragmentManager();
    }

    @Test
    public void openTileIntent_profileSelectionDialog_unresolvableWorkProfileIntentShouldNotShow() {
        final int userId = 10;
        ShadowUserManager.getShadow().addUser(userId, "Someone", 0);
        final UserHandle userHandle = new UserHandle(userId);
        final Tile tile = new ActivityTile(mActivityInfo, CategoryKey.CATEGORY_HOMEPAGE);
        final ArrayList<UserHandle> handles = new ArrayList<>();
        handles.add(new UserHandle(0));
        handles.add(userHandle);
        tile.userHandle = handles;
        when(mPackageManager.resolveActivityAsUser(any(Intent.class), anyInt(), eq(0)))
                .thenReturn(new ResolveInfo());
        when(mPackageManager.resolveActivityAsUser(any(Intent.class), anyInt(), eq(userId)))
                .thenReturn(null);

        mImpl.openTileIntent(mActivity, tile);

        final ArgumentCaptor<UserHandle> argument = ArgumentCaptor.forClass(UserHandle.class);
        verify(mActivity)
                .startActivityForResultAsUser(any(Intent.class), anyInt(), argument.capture());
        assertThat(argument.getValue().getIdentifier()).isEqualTo(0);
        verify(mActivity, never()).getSupportFragmentManager();
    }
}