diff options
Diffstat (limited to 'java/src')
66 files changed, 4538 insertions, 2515 deletions
diff --git a/java/src/com/android/intentresolver/AbstractMultiProfilePagerAdapter.java b/java/src/com/android/intentresolver/AbstractMultiProfilePagerAdapter.java index e3f1b233..4b06db3b 100644 --- a/java/src/com/android/intentresolver/AbstractMultiProfilePagerAdapter.java +++ b/java/src/com/android/intentresolver/AbstractMultiProfilePagerAdapter.java @@ -62,6 +62,7 @@ public abstract class AbstractMultiProfilePagerAdapter extends PagerAdapter { private Set<Integer> mLoadedPages; private final EmptyStateProvider mEmptyStateProvider; private final UserHandle mWorkProfileUserHandle; + private final UserHandle mCloneProfileUserHandle; private final Supplier<Boolean> mWorkProfileQuietModeChecker; // True when work is quiet. AbstractMultiProfilePagerAdapter( @@ -69,11 +70,13 @@ public abstract class AbstractMultiProfilePagerAdapter extends PagerAdapter { int currentPage, EmptyStateProvider emptyStateProvider, Supplier<Boolean> workProfileQuietModeChecker, - UserHandle workProfileUserHandle) { + UserHandle workProfileUserHandle, + UserHandle cloneProfileUserHandle) { mContext = Objects.requireNonNull(context); mCurrentPage = currentPage; mLoadedPages = new HashSet<>(); mWorkProfileUserHandle = workProfileUserHandle; + mCloneProfileUserHandle = cloneProfileUserHandle; mEmptyStateProvider = emptyStateProvider; mWorkProfileQuietModeChecker = workProfileQuietModeChecker; } @@ -160,6 +163,10 @@ public abstract class AbstractMultiProfilePagerAdapter extends PagerAdapter { return null; } + public UserHandle getCloneUserHandle() { + return mCloneProfileUserHandle; + } + /** * Returns the {@link ProfileDescriptor} relevant to the given <code>pageIndex</code>. * <ul> diff --git a/java/src/com/android/intentresolver/AnnotatedUserHandles.java b/java/src/com/android/intentresolver/AnnotatedUserHandles.java index b4365b84..168f36d6 100644 --- a/java/src/com/android/intentresolver/AnnotatedUserHandles.java +++ b/java/src/com/android/intentresolver/AnnotatedUserHandles.java @@ -22,6 +22,8 @@ import android.app.ActivityManager; import android.os.UserHandle; import android.os.UserManager; +import androidx.annotation.VisibleForTesting; + /** * Helper class to precompute the (immutable) designations of various user handles in the system * that may contribute to the current Sharesheet session. @@ -78,36 +80,138 @@ public final class AnnotatedUserHandles { */ public final UserHandle tabOwnerUserHandleForLaunch; - public AnnotatedUserHandles(Activity forShareActivity) { - userIdOfCallingApp = forShareActivity.getLaunchedFromUid(); - if ((userIdOfCallingApp < 0) || UserHandle.isIsolated(userIdOfCallingApp)) { - throw new SecurityException("Can't start a resolver from uid " + userIdOfCallingApp); - } + /** Compute all handle designations for a new Sharesheet session in the specified activity. */ + public static AnnotatedUserHandles forShareActivity(Activity shareActivity) { + // TODO: consider integrating logic for `ResolverActivity.EXTRA_CALLING_USER`? + UserHandle userHandleSharesheetLaunchedAs = UserHandle.of(UserHandle.myUserId()); + + // ActivityManager.getCurrentUser() refers to the current Foreground user. When clone/work + // profile is active, we always make the personal tab from the foreground user. + // Outside profiles, current foreground user is potentially the same as the sharesheet + // process's user (UserHandle.myUserId()), so we continue to create personal tab with the + // current foreground user. + UserHandle personalProfileUserHandle = UserHandle.of(ActivityManager.getCurrentUser()); + + UserManager userManager = shareActivity.getSystemService(UserManager.class); + + return newBuilder() + .setUserIdOfCallingApp(shareActivity.getLaunchedFromUid()) + .setUserHandleSharesheetLaunchedAs(userHandleSharesheetLaunchedAs) + .setPersonalProfileUserHandle(personalProfileUserHandle) + .setWorkProfileUserHandle( + getWorkProfileForUser(userManager, personalProfileUserHandle)) + .setCloneProfileUserHandle( + getCloneProfileForUser(userManager, personalProfileUserHandle)) + .build(); + } - // TODO: integrate logic for `ResolverActivity.EXTRA_CALLING_USER`. - userHandleSharesheetLaunchedAs = UserHandle.of(UserHandle.myUserId()); + @VisibleForTesting static Builder newBuilder() { + return new Builder(); + } + + /** + * Returns the {@link UserHandle} to use when querying resolutions for intents in a + * {@link ResolverListController} configured for the provided {@code userHandle}. + */ + public UserHandle getQueryIntentsUser(UserHandle userHandle) { + // In case launching app is in clonedProfile, and we are building the personal tab, intent + // resolution will be attempted as clonedUser instead of user 0. This is because intent + // resolution from user 0 and clonedUser is not guaranteed to return same results. + // We do not care about the case when personal adapter is started with non-root user + // (secondary user case), as clone profile is guaranteed to be non-active in that case. + UserHandle queryIntentsUser = userHandle; + if (isLaunchedAsCloneProfile() && userHandle.equals(personalProfileUserHandle)) { + queryIntentsUser = cloneProfileUserHandle; + } + return queryIntentsUser; + } - personalProfileUserHandle = UserHandle.of(ActivityManager.getCurrentUser()); + private Boolean isLaunchedAsCloneProfile() { + return userHandleSharesheetLaunchedAs.equals(cloneProfileUserHandle); + } - UserManager userManager = forShareActivity.getSystemService(UserManager.class); - workProfileUserHandle = getWorkProfileForUser(userManager, personalProfileUserHandle); - cloneProfileUserHandle = getCloneProfileForUser(userManager, personalProfileUserHandle); + private AnnotatedUserHandles( + int userIdOfCallingApp, + UserHandle userHandleSharesheetLaunchedAs, + UserHandle personalProfileUserHandle, + @Nullable UserHandle workProfileUserHandle, + @Nullable UserHandle cloneProfileUserHandle) { + if ((userIdOfCallingApp < 0) || UserHandle.isIsolated(userIdOfCallingApp)) { + throw new SecurityException("Can't start a resolver from uid " + userIdOfCallingApp); + } - tabOwnerUserHandleForLaunch = (userHandleSharesheetLaunchedAs == workProfileUserHandle) - ? workProfileUserHandle : personalProfileUserHandle; + this.userIdOfCallingApp = userIdOfCallingApp; + this.userHandleSharesheetLaunchedAs = userHandleSharesheetLaunchedAs; + this.personalProfileUserHandle = personalProfileUserHandle; + this.workProfileUserHandle = workProfileUserHandle; + this.cloneProfileUserHandle = cloneProfileUserHandle; + this.tabOwnerUserHandleForLaunch = + (userHandleSharesheetLaunchedAs == workProfileUserHandle) + ? workProfileUserHandle : personalProfileUserHandle; } @Nullable private static UserHandle getWorkProfileForUser( UserManager userManager, UserHandle profileOwnerUserHandle) { - return userManager.getProfiles(profileOwnerUserHandle.getIdentifier()).stream() - .filter(info -> info.isManagedProfile()).findFirst() - .map(info -> info.getUserHandle()).orElse(null); + return userManager.getProfiles(profileOwnerUserHandle.getIdentifier()) + .stream() + .filter(info -> info.isManagedProfile()) + .findFirst() + .map(info -> info.getUserHandle()) + .orElse(null); } @Nullable private static UserHandle getCloneProfileForUser( UserManager userManager, UserHandle profileOwnerUserHandle) { - return null; // Not yet supported in framework. + return userManager.getProfiles(profileOwnerUserHandle.getIdentifier()) + .stream() + .filter(info -> info.isCloneProfile()) + .findFirst() + .map(info -> info.getUserHandle()) + .orElse(null); + } + + @VisibleForTesting + static class Builder { + private int mUserIdOfCallingApp; + private UserHandle mUserHandleSharesheetLaunchedAs; + private UserHandle mPersonalProfileUserHandle; + private UserHandle mWorkProfileUserHandle; + private UserHandle mCloneProfileUserHandle; + + public Builder setUserIdOfCallingApp(int id) { + mUserIdOfCallingApp = id; + return this; + } + + public Builder setUserHandleSharesheetLaunchedAs(UserHandle user) { + mUserHandleSharesheetLaunchedAs = user; + return this; + } + + public Builder setPersonalProfileUserHandle(UserHandle user) { + mPersonalProfileUserHandle = user; + return this; + } + + public Builder setWorkProfileUserHandle(UserHandle user) { + mWorkProfileUserHandle = user; + return this; + } + + public Builder setCloneProfileUserHandle(UserHandle user) { + mCloneProfileUserHandle = user; + return this; + } + + public AnnotatedUserHandles build() { + return new AnnotatedUserHandles( + mUserIdOfCallingApp, + mUserHandleSharesheetLaunchedAs, + mPersonalProfileUserHandle, + mWorkProfileUserHandle, + mCloneProfileUserHandle); + } } } diff --git a/java/src/com/android/intentresolver/ChooserActionFactory.java b/java/src/com/android/intentresolver/ChooserActionFactory.java index 947155f3..6ec62753 100644 --- a/java/src/com/android/intentresolver/ChooserActionFactory.java +++ b/java/src/com/android/intentresolver/ChooserActionFactory.java @@ -26,12 +26,9 @@ import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; -import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.ResolveInfo; -import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.net.Uri; -import android.os.Bundle; import android.service.chooser.ChooserAction; import android.text.TextUtils; import android.util.Log; @@ -40,8 +37,6 @@ import android.view.View; import com.android.intentresolver.chooser.DisplayResolveInfo; import com.android.intentresolver.chooser.TargetInfo; import com.android.intentresolver.contentpreview.ChooserContentPreviewUi; -import com.android.intentresolver.flags.FeatureFlagRepository; -import com.android.intentresolver.flags.Flags; import com.android.intentresolver.widget.ActionRow; import com.android.internal.annotations.VisibleForTesting; @@ -89,15 +84,10 @@ public final class ChooserActionFactory implements ChooserContentPreviewUi.Actio private static final String IMAGE_EDITOR_SHARED_ELEMENT = "screenshot_preview_image"; private final Context mContext; - private final String mCopyButtonLabel; - private final Drawable mCopyButtonDrawable; - private final Runnable mOnCopyButtonClicked; - private final TargetInfo mEditSharingTarget; - private final Runnable mOnEditButtonClicked; - private final TargetInfo mNearbySharingTarget; - private final Runnable mOnNearbyButtonClicked; + private final Runnable mCopyButtonRunnable; + private final Runnable mEditButtonRunnable; private final ImmutableList<ChooserAction> mCustomActions; - private final Runnable mOnModifyShareClicked; + private final @Nullable ChooserAction mModifyShareAction; private final Consumer<Boolean> mExcludeSharedTextAction; private final Consumer</* @Nullable */ Integer> mFinishCallback; private final ChooserActivityLogger mLogger; @@ -105,7 +95,6 @@ public final class ChooserActionFactory implements ChooserContentPreviewUi.Actio /** * @param context * @param chooserRequest data about the invocation of the current Sharesheet session. - * @param featureFlagRepository feature flags that may control the eligibility of some actions. * @param integratedDeviceComponents info about other components that are available on this * device to implement the supported action types. * @param onUpdateSharedTextIsExcluded a delegate to be invoked when the "exclude shared text" @@ -119,7 +108,6 @@ public final class ChooserActionFactory implements ChooserContentPreviewUi.Actio public ChooserActionFactory( Context context, ChooserRequestParameters chooserRequest, - FeatureFlagRepository featureFlagRepository, ChooserIntegratedDeviceComponents integratedDeviceComponents, ChooserActivityLogger logger, Consumer<Boolean> onUpdateSharedTextIsExcluded, @@ -128,19 +116,13 @@ public final class ChooserActionFactory implements ChooserContentPreviewUi.Actio Consumer</* @Nullable */ Integer> finishCallback) { this( context, - context.getString(com.android.internal.R.string.copy), - context.getDrawable(com.android.internal.R.drawable.ic_menu_copy_material), - makeOnCopyRunnable( + makeCopyButtonRunnable( context, chooserRequest.getTargetIntent(), chooserRequest.getReferrerPackageName(), finishCallback, logger), - getEditSharingTarget( - context, - chooserRequest.getTargetIntent(), - integratedDeviceComponents), - makeOnEditRunnable( + makeEditButtonRunnable( getEditSharingTarget( context, chooserRequest.getTargetIntent(), @@ -148,25 +130,8 @@ public final class ChooserActionFactory implements ChooserContentPreviewUi.Actio firstVisibleImageQuery, activityStarter, logger), - getNearbySharingTarget( - context, - chooserRequest.getTargetIntent(), - integratedDeviceComponents), - makeOnNearbyShareRunnable( - getNearbySharingTarget( - context, - chooserRequest.getTargetIntent(), - integratedDeviceComponents), - activityStarter, - finishCallback, - logger), chooserRequest.getChooserActions(), - (featureFlagRepository.isEnabled(Flags.SHARESHEET_RESELECTION_ACTION) - ? createModifyShareRunnable( - chooserRequest.getModifyShareAction(), - finishCallback, - logger) - : null), + chooserRequest.getModifyShareAction(), onUpdateSharedTextIsExcluded, logger, finishCallback); @@ -175,71 +140,33 @@ public final class ChooserActionFactory implements ChooserContentPreviewUi.Actio @VisibleForTesting ChooserActionFactory( Context context, - String copyButtonLabel, - Drawable copyButtonDrawable, - Runnable onCopyButtonClicked, - TargetInfo editSharingTarget, - Runnable onEditButtonClicked, - TargetInfo nearbySharingTarget, - Runnable onNearbyButtonClicked, + Runnable copyButtonRunnable, + Runnable editButtonRunnable, List<ChooserAction> customActions, - @Nullable Runnable onModifyShareClicked, + @Nullable ChooserAction modifyShareAction, Consumer<Boolean> onUpdateSharedTextIsExcluded, ChooserActivityLogger logger, Consumer</* @Nullable */ Integer> finishCallback) { mContext = context; - mCopyButtonLabel = copyButtonLabel; - mCopyButtonDrawable = copyButtonDrawable; - mOnCopyButtonClicked = onCopyButtonClicked; - mEditSharingTarget = editSharingTarget; - mOnEditButtonClicked = onEditButtonClicked; - mNearbySharingTarget = nearbySharingTarget; - mOnNearbyButtonClicked = onNearbyButtonClicked; + mCopyButtonRunnable = copyButtonRunnable; + mEditButtonRunnable = editButtonRunnable; mCustomActions = ImmutableList.copyOf(customActions); - mOnModifyShareClicked = onModifyShareClicked; + mModifyShareAction = modifyShareAction; mExcludeSharedTextAction = onUpdateSharedTextIsExcluded; mLogger = logger; mFinishCallback = finishCallback; } - /** Create an action that copies the share content to the clipboard. */ - @Override - public ActionRow.Action createCopyButton() { - return new ActionRow.Action( - com.android.internal.R.id.chooser_copy_button, - mCopyButtonLabel, - mCopyButtonDrawable, - mOnCopyButtonClicked); - } - - /** Create an action that opens the share content in a system-default editor. */ @Override @Nullable - public ActionRow.Action createEditButton() { - if (mEditSharingTarget == null) { - return null; - } - - return new ActionRow.Action( - com.android.internal.R.id.chooser_edit_button, - mEditSharingTarget.getDisplayLabel(), - mEditSharingTarget.getDisplayIconHolder().getDisplayIcon(), - mOnEditButtonClicked); + public Runnable getEditButtonRunnable() { + return mEditButtonRunnable; } - /** Create a "Share to Nearby" action. */ @Override @Nullable - public ActionRow.Action createNearbyButton() { - if (mNearbySharingTarget == null) { - return null; - } - - return new ActionRow.Action( - com.android.internal.R.id.chooser_nearby_button, - mNearbySharingTarget.getDisplayLabel(), - mNearbySharingTarget.getDisplayIconHolder().getDisplayIcon(), - mOnNearbyButtonClicked); + public Runnable getCopyButtonRunnable() { + return mCopyButtonRunnable; } /** Create custom actions */ @@ -247,8 +174,15 @@ public final class ChooserActionFactory implements ChooserContentPreviewUi.Actio public List<ActionRow.Action> createCustomActions() { List<ActionRow.Action> actions = new ArrayList<>(); for (int i = 0; i < mCustomActions.size(); i++) { + final int position = i; ActionRow.Action actionRow = createCustomAction( - mContext, mCustomActions.get(i), mFinishCallback, i, mLogger); + mContext, + mCustomActions.get(i), + mFinishCallback, + () -> { + mLogger.logCustomActionSelected(position); + } + ); if (actionRow != null) { actions.add(actionRow); } @@ -261,27 +195,14 @@ public final class ChooserActionFactory implements ChooserContentPreviewUi.Actio */ @Override @Nullable - public Runnable getModifyShareAction() { - return mOnModifyShareClicked; - } - - private static Runnable createModifyShareRunnable( - PendingIntent pendingIntent, - Consumer<Integer> finishCallback, - ChooserActivityLogger logger) { - if (pendingIntent == null) { - return null; - } - - return () -> { - try { - pendingIntent.send(); - } catch (PendingIntent.CanceledException e) { - Log.d(TAG, "Payload reselection action has been cancelled"); - } - logger.logActionSelected(ChooserActivityLogger.SELECTION_TYPE_MODIFY_SHARE); - finishCallback.accept(Activity.RESULT_OK); - }; + public ActionRow.Action getModifyShareAction() { + return createCustomAction( + mContext, + mModifyShareAction, + mFinishCallback, + () -> { + mLogger.logActionSelected(ChooserActivityLogger.SELECTION_TYPE_MODIFY_SHARE); + }); } /** @@ -298,7 +219,7 @@ public final class ChooserActionFactory implements ChooserContentPreviewUi.Actio return mExcludeSharedTextAction; } - private static Runnable makeOnCopyRunnable( + private static Runnable makeCopyButtonRunnable( Context context, Intent targetIntent, String referrerPackageName, @@ -386,7 +307,7 @@ public final class ChooserActionFactory implements ChooserContentPreviewUi.Actio final DisplayResolveInfo dri = DisplayResolveInfo.newDisplayResolveInfo( originalIntent, ri, - context.getString(com.android.internal.R.string.screenshot_edit), + context.getString(R.string.screenshot_edit), "", resolveIntent, null); @@ -395,7 +316,7 @@ public final class ChooserActionFactory implements ChooserContentPreviewUi.Actio return dri; } - private static Runnable makeOnEditRunnable( + private static Runnable makeEditButtonRunnable( TargetInfo editSharingTarget, Callable</* @Nullable */ View> firstVisibleImageQuery, ActionActivityStarter activityStarter, @@ -418,71 +339,15 @@ public final class ChooserActionFactory implements ChooserContentPreviewUi.Actio }; } - private static TargetInfo getNearbySharingTarget( - Context context, - Intent originalIntent, - ChooserIntegratedDeviceComponents integratedComponents) { - final ComponentName cn = integratedComponents.getNearbySharingComponent(); - if (cn == null) { - return null; - } - - final Intent resolveIntent = new Intent(originalIntent); - resolveIntent.setComponent(cn); - final ResolveInfo ri = context.getPackageManager().resolveActivity( - resolveIntent, PackageManager.GET_META_DATA); - if (ri == null || ri.activityInfo == null) { - Log.e(TAG, "Device-specified nearby sharing component (" + cn - + ") not available"); - return null; - } - - // Allow the nearby sharing component to provide a more appropriate icon and label - // for the chip. - CharSequence name = null; - Drawable icon = null; - final Bundle metaData = ri.activityInfo.metaData; - if (metaData != null) { - try { - final Resources pkgRes = context.getPackageManager().getResourcesForActivity(cn); - final int nameResId = metaData.getInt(CHIP_LABEL_METADATA_KEY); - name = pkgRes.getString(nameResId); - final int resId = metaData.getInt(CHIP_ICON_METADATA_KEY); - icon = pkgRes.getDrawable(resId); - } catch (NameNotFoundException | Resources.NotFoundException ex) { /* ignore */ } - } - if (TextUtils.isEmpty(name)) { - name = ri.loadLabel(context.getPackageManager()); - } - if (icon == null) { - icon = ri.loadIcon(context.getPackageManager()); - } - - final DisplayResolveInfo dri = DisplayResolveInfo.newDisplayResolveInfo( - originalIntent, ri, name, "", resolveIntent, null); - dri.getDisplayIconHolder().setDisplayIcon(icon); - return dri; - } - - private static Runnable makeOnNearbyShareRunnable( - TargetInfo nearbyShareTarget, - ActionActivityStarter activityStarter, - Consumer<Integer> finishCallback, - ChooserActivityLogger logger) { - return () -> { - logger.logActionSelected(ChooserActivityLogger.SELECTION_TYPE_NEARBY); - // Action bar is user-independent; always start as primary. - activityStarter.safelyStartActivityAsPersonalProfileUser(nearbyShareTarget); - }; - } - @Nullable private static ActionRow.Action createCustomAction( Context context, ChooserAction action, Consumer<Integer> finishCallback, - int position, - ChooserActivityLogger logger) { + Runnable loggingRunnable) { + if (action == null || action.getAction() == null) { + return null; + } Drawable icon = action.getIcon().loadDrawable(context); if (icon == null && TextUtils.isEmpty(action.getLabel())) { return null; @@ -507,7 +372,9 @@ public final class ChooserActionFactory implements ChooserContentPreviewUi.Actio } catch (PendingIntent.CanceledException e) { Log.d(TAG, "Custom action, " + action.getLabel() + ", has been cancelled"); } - logger.logCustomActionSelected(position); + if (loggingRunnable != null) { + loggingRunnable.run(); + } finishCallback.accept(Activity.RESULT_OK); } ); diff --git a/java/src/com/android/intentresolver/ChooserActivity.java b/java/src/com/android/intentresolver/ChooserActivity.java index ae5be26d..63ac6435 100644 --- a/java/src/com/android/intentresolver/ChooserActivity.java +++ b/java/src/com/android/intentresolver/ChooserActivity.java @@ -57,7 +57,6 @@ import android.os.SystemClock; import android.os.UserHandle; import android.os.UserManager; import android.os.storage.StorageManager; -import android.provider.DeviceConfig; import android.service.chooser.ChooserTarget; import android.util.Log; import android.util.Slog; @@ -73,6 +72,7 @@ import android.view.animation.LinearInterpolator; import android.widget.TextView; import androidx.annotation.MainThread; +import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.RecyclerView; import androidx.viewpager.widget.ViewPager; @@ -83,20 +83,23 @@ import com.android.intentresolver.NoCrossProfileEmptyStateProvider.DevicePolicyB import com.android.intentresolver.chooser.DisplayResolveInfo; import com.android.intentresolver.chooser.MultiDisplayResolveInfo; import com.android.intentresolver.chooser.TargetInfo; +import com.android.intentresolver.contentpreview.BasePreviewViewModel; import com.android.intentresolver.contentpreview.ChooserContentPreviewUi; +import com.android.intentresolver.contentpreview.HeadlineGeneratorImpl; +import com.android.intentresolver.contentpreview.PreviewViewModel; import com.android.intentresolver.flags.FeatureFlagRepository; import com.android.intentresolver.flags.FeatureFlagRepositoryFactory; -import com.android.intentresolver.flags.Flags; import com.android.intentresolver.grid.ChooserGridAdapter; -import com.android.intentresolver.grid.DirectShareViewHolder; +import com.android.intentresolver.icons.DefaultTargetDataLoader; +import com.android.intentresolver.icons.TargetDataLoader; +import com.android.intentresolver.measurements.Tracer; import com.android.intentresolver.model.AbstractResolverComparator; import com.android.intentresolver.model.AppPredictionServiceResolverComparator; import com.android.intentresolver.model.ResolverRankerServiceResolverComparator; import com.android.intentresolver.shortcuts.AppPredictorFactory; import com.android.intentresolver.shortcuts.ShortcutLoader; -import com.android.intentresolver.widget.ResolverDrawerLayout; +import com.android.intentresolver.widget.ImagePreviewView; import com.android.internal.annotations.VisibleForTesting; -import com.android.internal.config.sysui.SystemUiDeviceConfigFlags; import com.android.internal.content.PackageMonitor; import com.android.internal.logging.nano.MetricsProto.MetricsEvent; @@ -139,21 +142,11 @@ public class ChooserActivity extends ResolverActivity implements */ public static final String FIRST_IMAGE_PREVIEW_TRANSITION_NAME = "screenshot_preview_image"; - private static final String PREF_NUM_SHEET_EXPANSIONS = "pref_num_sheet_expansions"; - - private static final String CHIP_LABEL_METADATA_KEY = "android.service.chooser.chip_label"; - private static final String CHIP_ICON_METADATA_KEY = "android.service.chooser.chip_icon"; - private static final boolean DEBUG = true; public static final String LAUNCH_LOCATION_DIRECT_SHARE = "direct_share"; private static final String SHORTCUT_TARGET = "shortcut_target"; - private static final String PLURALS_COUNT = "count"; - private static final String PLURALS_FILE_NAME = "file_name"; - - private static final String IMAGE_EDITOR_SHARED_ELEMENT = "screenshot_preview_image"; - // TODO: these data structures are for one-time use in shuttling data from where they're // populated in `ShortcutToChooserTargetConverter` to where they're consumed in // `ShortcutSelectionLogic` which packs the appropriate elements into the final `TargetInfo`. @@ -180,18 +173,6 @@ public class ChooserActivity extends ResolverActivity implements @Retention(RetentionPolicy.SOURCE) public @interface ShareTargetType {} - public static final float DIRECT_SHARE_EXPANSION_RATE = 0.78f; - - private static final int DEFAULT_SALT_EXPIRATION_DAYS = 7; - private final int mMaxHashSaltDays = DeviceConfig.getInt(DeviceConfig.NAMESPACE_SYSTEMUI, - SystemUiDeviceConfigFlags.HASH_SALT_MAX_DAYS, - DEFAULT_SALT_EXPIRATION_DAYS); - - private static final int URI_PERMISSION_INTENT_FLAGS = Intent.FLAG_GRANT_READ_URI_PERMISSION - | Intent.FLAG_GRANT_WRITE_URI_PERMISSION - | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION - | Intent.FLAG_GRANT_PREFIX_URI_PERMISSION; - private ChooserIntegratedDeviceComponents mIntegratedDeviceComponents; /* TODO: this is `nullable` because we have to defer the assignment til onCreate(). We make the @@ -248,6 +229,7 @@ public class ChooserActivity extends ResolverActivity implements @Override protected void onCreate(Bundle savedInstanceState) { + Tracer.INSTANCE.markLaunched(); final long intentReceivedTime = System.currentTimeMillis(); mLatencyTracker.onActionStart(ACTION_LOAD_SHARE_SHEET); @@ -261,7 +243,6 @@ public class ChooserActivity extends ResolverActivity implements getIntent(), getReferrerPackageName(), getReferrer(), - mIntegratedDeviceComponents, mFeatureFlagRepository); } catch (IllegalArgumentException e) { Log.e(TAG, "Caller provided invalid Chooser request parameters", e); @@ -270,28 +251,37 @@ public class ChooserActivity extends ResolverActivity implements return; } - mRefinementManager = new ChooserRefinementManager( - this, - mChooserRequest.getRefinementIntentSender(), - (validatedRefinedTarget) -> { - maybeRemoveSharedText(validatedRefinedTarget); - if (super.onTargetSelected(validatedRefinedTarget, false)) { - finish(); - } - }, - () -> { - mRefinementManager.destroy(); - finish(); - }); + mRefinementManager = new ViewModelProvider(this).get(ChooserRefinementManager.class); + + mRefinementManager.getRefinementCompletion().observe(this, completion -> { + if (completion.consume()) { + TargetInfo targetInfo = completion.getTargetInfo(); + // targetInfo is non-null if the refinement process was successful. + if (targetInfo != null) { + maybeRemoveSharedText(targetInfo); + + // We already block suspended targets from going to refinement, and we probably + // can't recover a Chooser session if that's the reason the refined target fails + // to launch now. Fire-and-forget the refined launch; ignore the return value + // and just make sure the Sharesheet session gets cleaned up regardless. + ChooserActivity.super.onTargetSelected(targetInfo, false); + } + finish(); + } + }); + + BasePreviewViewModel previewViewModel = + new ViewModelProvider(this, createPreviewViewModelFactory()) + .get(BasePreviewViewModel.class); mChooserContentPreviewUi = new ChooserContentPreviewUi( + getLifecycle(), + previewViewModel.createOrReuseProvider(mChooserRequest), mChooserRequest.getTargetIntent(), - getContentResolver(), - this::isImageType, - createPreviewImageLoader(), + previewViewModel.createOrReuseImageLoader(), createChooserActionFactory(), mEnterTransitionAnimationDelegate, - mFeatureFlagRepository); + new HeadlineGeneratorImpl(this)); setAdditionalTargets(mChooserRequest.getAdditionalTargets()); @@ -318,7 +308,8 @@ public class ChooserActivity extends ResolverActivity implements mChooserRequest.getDefaultTitleResource(), mChooserRequest.getInitialIntents(), /* rList: List<ResolveInfo> = */ null, - /* supportsAlwaysUseOption = */ false); + /* supportsAlwaysUseOption = */ false, + new DefaultTargetDataLoader(this, getLifecycle(), false)); mChooserShownTime = System.currentTimeMillis(); final long systemCost = mChooserShownTime - intentReceivedTime; @@ -328,26 +319,10 @@ public class ChooserActivity extends ResolverActivity implements if (mResolverDrawerLayout != null) { mResolverDrawerLayout.addOnLayoutChangeListener(this::handleLayoutChange); - // expand/shrink direct share 4 -> 8 viewgroup - if (mChooserRequest.isSendActionTarget()) { - mResolverDrawerLayout.setOnScrollChangeListener(this::handleScroll); - } - mResolverDrawerLayout.setOnCollapsedChangedListener( - new ResolverDrawerLayout.OnCollapsedChangedListener() { - - // Only consider one expansion per activity creation - private boolean mWrittenOnce = false; - - @Override - public void onCollapsedChanged(boolean isCollapsed) { - if (!isCollapsed && !mWrittenOnce) { - incrementNumSheetExpansions(); - mWrittenOnce = true; - } - getChooserActivityLogger() - .logSharesheetExpansionChanged(isCollapsed); - } + isCollapsed -> { + mChooserMultiProfilePagerAdapter.setIsCollapsed(isCollapsed); + getChooserActivityLogger().logSharesheetExpansionChanged(isCollapsed); }); } @@ -388,7 +363,10 @@ public class ChooserActivity extends ResolverActivity implements private void createProfileRecords( AppPredictorFactory factory, IntentFilter targetIntentFilter) { UserHandle mainUserHandle = getPersonalProfileUserHandle(); - createProfileRecord(mainUserHandle, targetIntentFilter, factory); + ProfileRecord record = createProfileRecord(mainUserHandle, targetIntentFilter, factory); + if (record.shortcutLoader == null) { + Tracer.INSTANCE.endLaunchToShortcutTrace(); + } UserHandle workUserHandle = getWorkProfileUserHandle(); if (workUserHandle != null) { @@ -396,7 +374,7 @@ public class ChooserActivity extends ResolverActivity implements } } - private void createProfileRecord( + private ProfileRecord createProfileRecord( UserHandle userHandle, IntentFilter targetIntentFilter, AppPredictorFactory factory) { AppPredictor appPredictor = factory.create(userHandle); ShortcutLoader shortcutLoader = ActivityManager.isLowRamDeviceStatic() @@ -407,9 +385,9 @@ public class ChooserActivity extends ResolverActivity implements userHandle, targetIntentFilter, shortcutsResult -> onShortcutsLoaded(userHandle, shortcutsResult)); - mProfileRecords.put( - userHandle.getIdentifier(), - new ProfileRecord(appPredictor, shortcutLoader)); + ProfileRecord record = new ProfileRecord(appPredictor, shortcutLoader); + mProfileRecords.put(userHandle.getIdentifier(), record); + return record; } @Nullable @@ -426,6 +404,7 @@ public class ChooserActivity extends ResolverActivity implements Consumer<ShortcutLoader.Result> callback) { return new ShortcutLoader( context, + getLifecycle(), appPredictor, userHandle, targetIntentFilter, @@ -452,13 +431,14 @@ public class ChooserActivity extends ResolverActivity implements protected AbstractMultiProfilePagerAdapter createMultiProfilePagerAdapter( Intent[] initialIntents, List<ResolveInfo> rList, - boolean filterLastUsed) { + boolean filterLastUsed, + TargetDataLoader targetDataLoader) { if (shouldShowTabs()) { mChooserMultiProfilePagerAdapter = createChooserMultiProfilePagerAdapterForTwoProfiles( - initialIntents, rList, filterLastUsed); + initialIntents, rList, filterLastUsed, targetDataLoader); } else { mChooserMultiProfilePagerAdapter = createChooserMultiProfilePagerAdapterForOneProfile( - initialIntents, rList, filterLastUsed); + initialIntents, rList, filterLastUsed, targetDataLoader); } return mChooserMultiProfilePagerAdapter; } @@ -495,33 +475,37 @@ public class ChooserActivity extends ResolverActivity implements return new NoCrossProfileEmptyStateProvider(getPersonalProfileUserHandle(), noWorkToPersonalEmptyState, noPersonalToWorkEmptyState, - createCrossProfileIntentsChecker(), createMyUserIdProvider()); + createCrossProfileIntentsChecker(), getTabOwnerUserHandleForLaunch()); } private ChooserMultiProfilePagerAdapter createChooserMultiProfilePagerAdapterForOneProfile( Intent[] initialIntents, List<ResolveInfo> rList, - boolean filterLastUsed) { + boolean filterLastUsed, + TargetDataLoader targetDataLoader) { ChooserGridAdapter adapter = createChooserGridAdapter( /* context */ this, /* payloadIntents */ mIntents, initialIntents, rList, filterLastUsed, - /* userHandle */ UserHandle.of(UserHandle.myUserId())); + /* userHandle */ getPersonalProfileUserHandle(), + targetDataLoader); return new ChooserMultiProfilePagerAdapter( /* context */ this, adapter, createEmptyStateProvider(/* workProfileUserHandle= */ null), /* workProfileQuietModeChecker= */ () -> false, /* workProfileUserHandle= */ null, + getCloneProfileUserHandle(), mMaxTargetsPerRow); } private ChooserMultiProfilePagerAdapter createChooserMultiProfilePagerAdapterForTwoProfiles( Intent[] initialIntents, List<ResolveInfo> rList, - boolean filterLastUsed) { + boolean filterLastUsed, + TargetDataLoader targetDataLoader) { int selectedProfile = findSelectedProfile(); ChooserGridAdapter personalAdapter = createChooserGridAdapter( /* context */ this, @@ -529,14 +513,16 @@ public class ChooserActivity extends ResolverActivity implements selectedProfile == PROFILE_PERSONAL ? initialIntents : null, rList, filterLastUsed, - /* userHandle */ getPersonalProfileUserHandle()); + /* userHandle */ getPersonalProfileUserHandle(), + targetDataLoader); ChooserGridAdapter workAdapter = createChooserGridAdapter( /* context */ this, /* payloadIntents */ mIntents, selectedProfile == PROFILE_WORK ? initialIntents : null, rList, filterLastUsed, - /* userHandle */ getWorkProfileUserHandle()); + /* userHandle */ getWorkProfileUserHandle(), + targetDataLoader); return new ChooserMultiProfilePagerAdapter( /* context */ this, personalAdapter, @@ -545,13 +531,14 @@ public class ChooserActivity extends ResolverActivity implements () -> mWorkProfileAvailability.isQuietModeEnabled(), selectedProfile, getWorkProfileUserHandle(), + getCloneProfileUserHandle(), mMaxTargetsPerRow); } private int findSelectedProfile() { int selectedProfile = getSelectedProfileExtra(); if (selectedProfile == -1) { - selectedProfile = getProfileForUser(getUser()); + selectedProfile = getProfileForUser(getTabOwnerUserHandleForLaunch()); } return selectedProfile; } @@ -604,21 +591,32 @@ public class ChooserActivity extends ResolverActivity implements // Refresh pinned items mPinnedSharedPrefs = getPinnedSharedPrefs(this); if (listAdapter == null) { - mChooserMultiProfilePagerAdapter.getActiveListAdapter().handlePackagesChanged(); + handlePackageChangePerProfile(mChooserMultiProfilePagerAdapter.getActiveListAdapter()); if (mChooserMultiProfilePagerAdapter.getCount() > 1) { - mChooserMultiProfilePagerAdapter.getInactiveListAdapter().handlePackagesChanged(); + handlePackageChangePerProfile( + mChooserMultiProfilePagerAdapter.getInactiveListAdapter()); } } else { - listAdapter.handlePackagesChanged(); + handlePackageChangePerProfile(listAdapter); } updateProfileViewButton(); } + private void handlePackageChangePerProfile(ResolverListAdapter adapter) { + ProfileRecord record = getProfileRecord(adapter.getUserHandle()); + if (record != null && record.shortcutLoader != null) { + record.shortcutLoader.reset(); + } + adapter.handlePackagesChanged(); + } + @Override protected void onResume() { super.onResume(); Log.d(TAG, "onResume: " + getComponentName().flattenToShortString()); maybeCancelFinishAnimation(); + + mRefinementManager.onActivityResume(); } @Override @@ -652,8 +650,6 @@ public class ChooserActivity extends ResolverActivity implements parent = parent == null ? getWindow().getDecorView() : parent; - updateLayoutWidth(com.android.internal.R.id.content_preview_text_layout, width, parent); - updateLayoutWidth(com.android.internal.R.id.content_preview_title_layout, width, parent); updateLayoutWidth(com.android.internal.R.id.content_preview_file_layout, width, parent); } @@ -700,8 +696,10 @@ public class ChooserActivity extends ResolverActivity implements @Nullable private View getFirstVisibleImgPreviewView() { - View firstImage = findViewById(com.android.internal.R.id.content_preview_image_1_large); - return firstImage != null && firstImage.isVisibleToUser() ? firstImage : null; + View imagePreview = findViewById(R.id.scrollable_image_preview); + return imagePreview instanceof ImagePreviewView + ? ((ImagePreviewView) imagePreview).getTransitionView() + : null; } /** @@ -713,23 +711,11 @@ public class ChooserActivity extends ResolverActivity implements return resolver.query(uri, null, null, null, null); } - @VisibleForTesting - protected boolean isImageType(String mimeType) { - return mimeType != null && mimeType.startsWith("image/"); - } - - private int getNumSheetExpansions() { - return getPreferences(Context.MODE_PRIVATE).getInt(PREF_NUM_SHEET_EXPANSIONS, 0); - } - - private void incrementNumSheetExpansions() { - getPreferences(Context.MODE_PRIVATE).edit().putInt(PREF_NUM_SHEET_EXPANSIONS, - getNumSheetExpansions() + 1).apply(); - } - @Override protected void onStop() { super.onStop(); + mRefinementManager.onActivityStop(isChangingConfigurations()); + if (maybeCancelFinishAnimation()) { finish(); } @@ -743,11 +729,6 @@ public class ChooserActivity extends ResolverActivity implements mLatencyTracker.onActionCancel(ACTION_LOAD_SHARE_SHEET); } - if (mRefinementManager != null) { // TODO: null-checked in case of early-destroy, or skip? - mRefinementManager.destroy(); - mRefinementManager = null; - } - mBackgroundThreadPoolExecutor.shutdownNow(); destroyProfileRecords(); @@ -805,15 +786,20 @@ public class ChooserActivity extends ResolverActivity implements } } - @Override - public void addUseDifferentAppLabelIfNecessary(ResolverListAdapter adapter) { - if (mChooserRequest.getCallerChooserTargets().size() > 0) { - mChooserMultiProfilePagerAdapter.getActiveListAdapter().addServiceResults( - /* origTarget */ null, - new ArrayList<>(mChooserRequest.getCallerChooserTargets()), - TARGET_TYPE_DEFAULT, - /* directShareShortcutInfoCache */ Collections.emptyMap(), - /* directShareAppTargetCache */ Collections.emptyMap()); + private void addCallerChooserTargets() { + if (!mChooserRequest.getCallerChooserTargets().isEmpty()) { + // Send the caller's chooser targets only to the default profile. + UserHandle defaultUser = (findSelectedProfile() == PROFILE_WORK) + ? getAnnotatedUserHandles().workProfileUserHandle + : getAnnotatedUserHandles().personalProfileUserHandle; + if (mChooserMultiProfilePagerAdapter.getCurrentUserHandle() == defaultUser) { + mChooserMultiProfilePagerAdapter.getActiveListAdapter().addServiceResults( + /* origTarget */ null, + new ArrayList<>(mChooserRequest.getCallerChooserTargets()), + TARGET_TYPE_DEFAULT, + /* directShareShortcutInfoCache */ Collections.emptyMap(), + /* directShareAppTargetCache */ Collections.emptyMap()); + } } } @@ -860,7 +846,11 @@ public class ChooserActivity extends ResolverActivity implements ChooserTargetActionsDialogFragment.show( getSupportFragmentManager(), targetList, - mChooserMultiProfilePagerAdapter.getCurrentUserHandle(), + // Adding userHandle from ResolveInfo allows the app icon in Dialog Box to be + // resolved correctly within the same tab. + getResolveInfoUserHandle( + targetInfo.getResolveInfo(), + mChooserMultiProfilePagerAdapter.getCurrentUserHandle()), shortcutIdKey, shortcutTitle, isShortcutPinned, @@ -869,7 +859,11 @@ public class ChooserActivity extends ResolverActivity implements @Override protected boolean onTargetSelected(TargetInfo target, boolean alwaysCheck) { - if (mRefinementManager.maybeHandleSelection(target)) { + if (mRefinementManager.maybeHandleSelection( + target, + mChooserRequest.getRefinementIntentSender(), + getApplication(), + getMainThreadHandler())) { return false; } updateModelAndChooserCounts(target); @@ -892,11 +886,14 @@ public class ChooserActivity extends ResolverActivity implements if (targetInfo.isMultiDisplayResolveInfo()) { MultiDisplayResolveInfo mti = (MultiDisplayResolveInfo) targetInfo; if (!mti.hasSelected()) { + // Add userHandle based badge to the stackedAppDialogBox. ChooserStackedAppDialogFragment.show( getSupportFragmentManager(), mti, which, - mChooserMultiProfilePagerAdapter.getCurrentUserHandle()); + getResolveInfoUserHandle( + targetInfo.getResolveInfo(), + mChooserMultiProfilePagerAdapter.getCurrentUserHandle())); return; } } @@ -1008,9 +1005,11 @@ public class ChooserActivity extends ResolverActivity implements mChooserMultiProfilePagerAdapter.getActiveListAdapter(); if (currentListAdapter != null) { sendImpressionToAppPredictor(info, currentListAdapter); - currentListAdapter.updateModel(info.getResolvedComponentName()); - currentListAdapter.updateChooserCounts(ri.activityInfo.packageName, - targetIntent.getAction()); + currentListAdapter.updateModel(info); + currentListAdapter.updateChooserCounts( + ri.activityInfo.packageName, + targetIntent.getAction(), + ri.userHandle); } if (DEBUG) { Log.d(TAG, "ResolveInfo Package is " + ri.activityInfo.packageName); @@ -1096,22 +1095,33 @@ public class ChooserActivity extends ResolverActivity implements @Nullable private AppPredictor getAppPredictor(UserHandle userHandle) { ProfileRecord record = getProfileRecord(userHandle); - return (record == null) ? null : record.appPredictor; + // We cannot use APS service when clone profile is present as APS service cannot sort + // cross profile targets as of now. + return (record == null || getCloneProfileUserHandle() != null) ? null : record.appPredictor; } /** * Sort intents alphabetically based on display label. */ static class AzInfoComparator implements Comparator<DisplayResolveInfo> { - Collator mCollator; + Comparator<DisplayResolveInfo> mComparator; AzInfoComparator(Context context) { - mCollator = Collator.getInstance(context.getResources().getConfiguration().locale); + Collator collator = Collator + .getInstance(context.getResources().getConfiguration().locale); + // Adding two stage comparator, first stage compares using displayLabel, next stage + // compares using resolveInfo.userHandle + mComparator = Comparator.comparing(DisplayResolveInfo::getDisplayLabel, collator) + .thenComparingInt(displayResolveInfo -> + getResolveInfoUserHandle( + displayResolveInfo.getResolveInfo(), + // TODO: User resolveInfo.userHandle, once its available. + UserHandle.SYSTEM).getIdentifier()); } @Override public int compare( DisplayResolveInfo lhsp, DisplayResolveInfo rhsp) { - return mCollator.compare(lhsp.getDisplayLabel(), rhsp.getDisplayLabel()); + return mComparator.compare(lhsp, rhsp); } } @@ -1129,14 +1139,16 @@ public class ChooserActivity extends ResolverActivity implements Intent targetIntent, String referrerPackageName, int launchedFromUid, - AbstractResolverComparator resolverComparator) { + AbstractResolverComparator resolverComparator, + UserHandle queryIntentsAsUser) { super( context, pm, targetIntent, referrerPackageName, launchedFromUid, - resolverComparator); + resolverComparator, + queryIntentsAsUser); } @Override @@ -1157,7 +1169,8 @@ public class ChooserActivity extends ResolverActivity implements Intent[] initialIntents, List<ResolveInfo> rList, boolean filterLastUsed, - UserHandle userHandle) { + UserHandle userHandle, + TargetDataLoader targetDataLoader) { ChooserListAdapter chooserListAdapter = createChooserListAdapter( context, payloadIntents, @@ -1168,7 +1181,8 @@ public class ChooserActivity extends ResolverActivity implements userHandle, getTargetIntent(), mChooserRequest, - mMaxTargetsPerRow); + mMaxTargetsPerRow, + targetDataLoader); return new ChooserGridAdapter( context, @@ -1208,39 +1222,10 @@ public class ChooserActivity extends ResolverActivity implements mProfileView.setOnClickListener(ChooserActivity.this::onProfileClick); ChooserActivity.this.updateProfileViewButton(); } - - @Override - public int getValidTargetCount() { - return mChooserMultiProfilePagerAdapter - .getActiveListAdapter() - .getSelectableServiceTargetCount(); - } - - @Override - public void updateDirectShareExpansion(DirectShareViewHolder directShareGroup) { - RecyclerView activeAdapterView = - mChooserMultiProfilePagerAdapter.getActiveAdapterView(); - if (mResolverDrawerLayout.isCollapsed()) { - directShareGroup.collapse(activeAdapterView); - } else { - directShareGroup.expand(activeAdapterView); - } - } - - @Override - public void handleScrollToExpandDirectShare( - DirectShareViewHolder directShareGroup, int y, int oldy) { - directShareGroup.handleScroll( - mChooserMultiProfilePagerAdapter.getActiveAdapterView(), - y, - oldy, - mMaxTargetsPerRow); - } }, chooserListAdapter, shouldShowContentPreview(), - mMaxTargetsPerRow, - getNumSheetExpansions()); + mMaxTargetsPerRow); } @VisibleForTesting @@ -1254,21 +1239,37 @@ public class ChooserActivity extends ResolverActivity implements UserHandle userHandle, Intent targetIntent, ChooserRequestParameters chooserRequest, - int maxTargetsPerRow) { + int maxTargetsPerRow, + TargetDataLoader targetDataLoader) { + UserHandle initialIntentsUserSpace = isLaunchedAsCloneProfile() + && userHandle.equals(getPersonalProfileUserHandle()) + ? getCloneProfileUserHandle() : userHandle; return new ChooserListAdapter( context, payloadIntents, initialIntents, rList, filterLastUsed, - resolverListController, + createListController(userHandle), userHandle, targetIntent, this, context.getPackageManager(), getChooserActivityLogger(), chooserRequest, - maxTargetsPerRow); + maxTargetsPerRow, + initialIntentsUserSpace, + targetDataLoader); + } + + @Override + protected void onWorkProfileStatusUpdated() { + UserHandle workUser = getWorkProfileUserHandle(); + ProfileRecord record = workUser == null ? null : getProfileRecord(workUser); + if (record != null && record.shortcutLoader != null) { + record.shortcutLoader.reset(); + } + super.onWorkProfileStatusUpdated(); } @Override @@ -1278,11 +1279,18 @@ public class ChooserActivity extends ResolverActivity implements AbstractResolverComparator resolverComparator; if (appPredictor != null) { resolverComparator = new AppPredictionServiceResolverComparator(this, getTargetIntent(), - getReferrerPackageName(), appPredictor, userHandle, getChooserActivityLogger()); + getReferrerPackageName(), appPredictor, userHandle, getChooserActivityLogger(), + getIntegratedDeviceComponents().getNearbySharingComponent()); } else { resolverComparator = - new ResolverRankerServiceResolverComparator(this, getTargetIntent(), - getReferrerPackageName(), null, getChooserActivityLogger()); + new ResolverRankerServiceResolverComparator( + this, + getTargetIntent(), + getReferrerPackageName(), + null, + getChooserActivityLogger(), + getResolverRankerServiceUserHandleList(userHandle), + getIntegratedDeviceComponents().getNearbySharingComponent()); } return new ChooserListController( @@ -1291,27 +1299,19 @@ public class ChooserActivity extends ResolverActivity implements getTargetIntent(), getReferrerPackageName(), getAnnotatedUserHandles().userIdOfCallingApp, - resolverComparator); + resolverComparator, + getQueryIntentsUser(userHandle)); } @VisibleForTesting - protected ImageLoader createPreviewImageLoader() { - final int cacheSize; - if (mFeatureFlagRepository.isEnabled(Flags.SHARESHEET_SCROLLABLE_IMAGE_PREVIEW)) { - float chooserWidth = getResources().getDimension(R.dimen.chooser_width); - float imageWidth = getResources().getDimension(R.dimen.chooser_preview_image_width); - cacheSize = (int) (Math.ceil(chooserWidth / imageWidth) + 2); - } else { - cacheSize = 3; - } - return new ImagePreviewImageLoader(this, getLifecycle(), cacheSize); + protected ViewModelProvider.Factory createPreviewViewModelFactory() { + return PreviewViewModel.Companion.getFactory(); } private ChooserActionFactory createChooserActionFactory() { return new ChooserActionFactory( this, mChooserRequest, - mFeatureFlagRepository, mIntegratedDeviceComponents, getChooserActivityLogger(), (isExcluded) -> mExcludeSharedText = isExcluded, @@ -1341,12 +1341,6 @@ public class ChooserActivity extends ResolverActivity implements }); } - private void handleScroll(View view, int x, int y, int oldx, int oldy) { - if (mChooserMultiProfilePagerAdapter.getCurrentRootAdapter() != null) { - mChooserMultiProfilePagerAdapter.getCurrentRootAdapter().handleScroll(view, y, oldy); - } - } - /* * Need to dynamically adjust how many icons can fit per row before we add them, * which also means setting the correct offset to initially show the content @@ -1415,9 +1409,7 @@ public class ChooserActivity extends ResolverActivity implements private int calculateDrawerOffset( int top, int bottom, RecyclerView recyclerView, ChooserGridAdapter gridAdapter) { - final int bottomInset = mSystemWindowInsets != null - ? mSystemWindowInsets.bottom : 0; - int offset = bottomInset; + int offset = mSystemWindowInsets != null ? mSystemWindowInsets.bottom : 0; int rowsToShow = gridAdapter.getSystemRowCount() + gridAdapter.getProfileRowCount() + gridAdapter.getServiceTargetRowCount() @@ -1447,7 +1439,6 @@ public class ChooserActivity extends ResolverActivity implements } if (recyclerView.getVisibility() == View.VISIBLE) { - int directShareHeight = 0; rowsToShow = Math.min(4, rowsToShow); boolean shouldShowExtraRow = shouldShowExtraRow(rowsToShow); mLastNumberOfChildren = recyclerView.getChildCount(); @@ -1463,28 +1454,8 @@ public class ChooserActivity extends ResolverActivity implements if (shouldShowExtraRow) { offset += height; } - - if (gridAdapter.getTargetType( - recyclerView.getChildAdapterPosition(child)) - == ChooserListAdapter.TARGET_SERVICE) { - directShareHeight = height; - } rowsToShow--; } - - boolean isExpandable = getResources().getConfiguration().orientation - == Configuration.ORIENTATION_PORTRAIT && !isInMultiWindowMode(); - if (directShareHeight != 0 && shouldShowContentPreview() - && isExpandable) { - // make sure to leave room for direct share 4->8 expansion - int requiredExpansionHeight = - (int) (directShareHeight / DIRECT_SHARE_EXPANSION_RATE); - int topInset = mSystemWindowInsets != null ? mSystemWindowInsets.top : 0; - int minHeight = bottom - top - mResolverDrawerLayout.getAlwaysShowHeight() - - requiredExpansionHeight - topInset - bottomInset; - - offset = Math.min(offset, minHeight); - } } else { ViewGroup currentEmptyStateView = getActiveEmptyStateView(); if (currentEmptyStateView.getVisibility() == View.VISIBLE) { @@ -1508,17 +1479,16 @@ public class ChooserActivity extends ResolverActivity implements } /** - * Returns {@link #PROFILE_PERSONAL}, {@link #PROFILE_WORK}, or -1 if the given user handle - * does not match either the personal or work user handle. + * Returns {@link #PROFILE_WORK}, if the given user handle matches work user handle. + * Returns {@link #PROFILE_PERSONAL}, otherwise. **/ private int getProfileForUser(UserHandle currentUserHandle) { - if (currentUserHandle.equals(getPersonalProfileUserHandle())) { - return PROFILE_PERSONAL; - } else if (currentUserHandle.equals(getWorkProfileUserHandle())) { + if (currentUserHandle.equals(getWorkProfileUserHandle())) { return PROFILE_WORK; } - Log.e(TAG, "User " + currentUserHandle + " does not belong to a personal or work profile."); - return -1; + // We return personal profile, as it is the default when there is no work profile, personal + // profile represents rootUser, clonedUser & secondaryUser, covering all use cases. + return PROFILE_PERSONAL; } private ViewGroup getActiveEmptyStateView() { @@ -1553,6 +1523,11 @@ public class ChooserActivity extends ResolverActivity implements } if (rebuildComplete) { + long duration = Tracer.INSTANCE.endAppTargetLoadingSection(listAdapter.getUserHandle()); + if (duration >= 0) { + Log.d(TAG, "app target loading time " + duration + " ms"); + } + addCallerChooserTargets(); getChooserActivityLogger().logSharesheetAppLoadComplete(); maybeQueryAdditionalPostProcessingTargets(chooserListAdapter); mLatencyTracker.onActionEnd(ACTION_LOAD_SHARE_SHEET); @@ -1562,14 +1537,11 @@ public class ChooserActivity extends ResolverActivity implements private void maybeQueryAdditionalPostProcessingTargets(ChooserListAdapter chooserListAdapter) { UserHandle userHandle = chooserListAdapter.getUserHandle(); ProfileRecord record = getProfileRecord(userHandle); - if (record == null) { - return; - } - if (record.shortcutLoader == null) { + if (record == null || record.shortcutLoader == null) { return; } record.loadingStartTime = SystemClock.elapsedRealtime(); - record.shortcutLoader.queryShortcuts(chooserListAdapter.getDisplayResolveInfos()); + record.shortcutLoader.updateAppTargets(chooserListAdapter.getDisplayResolveInfos()); } @MainThread @@ -1595,6 +1567,12 @@ public class ChooserActivity extends ResolverActivity implements adapter.completeServiceTargetLoading(); } + if (mMultiProfilePagerAdapter.getActiveListAdapter() == adapter) { + long duration = Tracer.INSTANCE.endLaunchToShortcutTrace(); + if (duration >= 0) { + Log.d(TAG, "stat to first shortcut time: " + duration + " ms"); + } + } logDirectShareTargetReceived(userHandle); sendVoiceChoicesIfNeeded(); getChooserActivityLogger().logSharesheetDirectLoadComplete(); @@ -1667,8 +1645,7 @@ public class ChooserActivity extends ResolverActivity implements * we instead show the content preview as a regular list item. */ private boolean shouldShowStickyContentPreview() { - return shouldShowStickyContentPreviewNoOrientationCheck() - && !getResources().getBoolean(R.bool.resolver_landscape_phone); + return shouldShowStickyContentPreviewNoOrientationCheck(); } private boolean shouldShowStickyContentPreviewNoOrientationCheck() { @@ -1785,9 +1762,6 @@ public class ChooserActivity extends ResolverActivity implements @Override protected void onProfileTabSelected() { - ChooserGridAdapter currentRootAdapter = - mChooserMultiProfilePagerAdapter.getCurrentRootAdapter(); - currentRootAdapter.updateDirectShareExpansion(); // This fixes an edge case where after performing a variety of gestures, vertical scrolling // ends up disabled. That's because at some point the old tab's vertical scrolling is // disabled and the new tab's is enabled. For context, see b/159997845 @@ -1929,9 +1903,6 @@ public class ChooserActivity extends ResolverActivity implements } public void destroy() { - if (shortcutLoader != null) { - shortcutLoader.destroy(); - } if (appPredictor != null) { appPredictor.destroy(); } diff --git a/java/src/com/android/intentresolver/ChooserActivityReEnabler.kt b/java/src/com/android/intentresolver/ChooserActivityReEnabler.kt new file mode 100644 index 00000000..3236c1be --- /dev/null +++ b/java/src/com/android/intentresolver/ChooserActivityReEnabler.kt @@ -0,0 +1,39 @@ +package com.android.intentresolver + +import android.content.BroadcastReceiver +import android.content.ComponentName +import android.content.Context +import android.content.Intent +import android.content.pm.PackageManager + +/** + * Ensures that the unbundled version of [ChooserActivity] does not get stuck in a disabled state. + */ +class ChooserActivityReEnabler : BroadcastReceiver() { + + override fun onReceive(context: Context, intent: Intent) { + if (intent.action == Intent.ACTION_BOOT_COMPLETED) { + context.packageManager.setComponentEnabledSetting( + CHOOSER_COMPONENT, + PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, + /* flags = */ 0, + ) + + // This only needs to be run once, so we disable ourself to avoid additional startup + // process on future boots + context.packageManager.setComponentEnabledSetting( + SELF_COMPONENT, + PackageManager.COMPONENT_ENABLED_STATE_DISABLED, + /* flags = */ 0, + ) + } + } + + companion object { + private const val CHOOSER_PACKAGE = "com.android.intentresolver" + private val CHOOSER_COMPONENT = + ComponentName(CHOOSER_PACKAGE, "$CHOOSER_PACKAGE.ChooserActivity") + private val SELF_COMPONENT = + ComponentName(CHOOSER_PACKAGE, "$CHOOSER_PACKAGE.ChooserActivityReEnabler") + } +} diff --git a/java/src/com/android/intentresolver/ChooserListAdapter.java b/java/src/com/android/intentresolver/ChooserListAdapter.java index f0651360..b1fa16b0 100644 --- a/java/src/com/android/intentresolver/ChooserListAdapter.java +++ b/java/src/com/android/intentresolver/ChooserListAdapter.java @@ -27,14 +27,10 @@ import android.content.Context; import android.content.Intent; import android.content.pm.ActivityInfo; import android.content.pm.LabeledIntent; -import android.content.pm.LauncherApps; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.content.pm.ShortcutInfo; -import android.graphics.Bitmap; -import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; -import android.graphics.drawable.Icon; import android.os.AsyncTask; import android.os.Trace; import android.os.UserHandle; @@ -47,20 +43,20 @@ import android.view.View; import android.view.ViewGroup; import android.widget.TextView; -import androidx.annotation.WorkerThread; - import com.android.intentresolver.chooser.DisplayResolveInfo; import com.android.intentresolver.chooser.MultiDisplayResolveInfo; import com.android.intentresolver.chooser.NotSelectableTargetInfo; import com.android.intentresolver.chooser.SelectableTargetInfo; import com.android.intentresolver.chooser.TargetInfo; +import com.android.intentresolver.icons.TargetDataLoader; import com.android.internal.annotations.VisibleForTesting; import com.android.internal.config.sysui.SystemUiDeviceConfigFlags; import java.util.ArrayList; -import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.stream.Collectors; public class ChooserListAdapter extends ResolverListAdapter { @@ -86,10 +82,11 @@ public class ChooserListAdapter extends ResolverListAdapter { private final ChooserActivityLogger mChooserActivityLogger; - private final Map<TargetInfo, AsyncTask> mIconLoaders = new HashMap<>(); + private final Set<TargetInfo> mRequestedIcons = new HashSet<>(); // Reserve spots for incoming direct share targets by adding placeholders private final TargetInfo mPlaceHolderTargetInfo; + private final TargetDataLoader mTargetDataLoader; private final List<TargetInfo> mServiceTargets = new ArrayList<>(); private final List<DisplayResolveInfo> mCallerTargets = new ArrayList<>(); @@ -98,6 +95,8 @@ public class ChooserListAdapter extends ResolverListAdapter { // Sorted list of DisplayResolveInfos for the alphabetical app section. private List<DisplayResolveInfo> mSortedList = new ArrayList<>(); + private final ItemRevealAnimationTracker mAnimationTracker = new ItemRevealAnimationTracker(); + // For pinned direct share labels, if the text spans multiple lines, the TextView will consume // the full width, even if the characters actually take up less than that. Measure the actual // line widths and constrain the View's width based upon that so that the pin doesn't end up @@ -142,7 +141,9 @@ public class ChooserListAdapter extends ResolverListAdapter { PackageManager packageManager, ChooserActivityLogger chooserActivityLogger, ChooserRequestParameters chooserRequest, - int maxRankedTargets) { + int maxRankedTargets, + UserHandle initialIntentsUserSpace, + TargetDataLoader targetDataLoader) { // Don't send the initial intents through the shared ResolverActivity path, // we want to separate them into a different section. super( @@ -155,12 +156,14 @@ public class ChooserListAdapter extends ResolverListAdapter { userHandle, targetIntent, resolverListCommunicator, - false); + initialIntentsUserSpace, + targetDataLoader); mChooserRequest = chooserRequest; mMaxRankedTargets = maxRankedTargets; mPlaceHolderTargetInfo = NotSelectableTargetInfo.newPlaceHolderTargetInfo(context); + mTargetDataLoader = targetDataLoader; createPlaceHolders(); mChooserActivityLogger = chooserActivityLogger; mShortcutSelectionLogic = new ShortcutSelectionLogic( @@ -222,8 +225,10 @@ public class ChooserListAdapter extends ResolverListAdapter { ri.noResourceId = true; ri.icon = 0; } + ri.userHandle = initialIntentsUserSpace; + // TODO: remove DisplayResolveInfo dependency on presentation getter DisplayResolveInfo displayResolveInfo = DisplayResolveInfo.newDisplayResolveInfo( - ii, ri, ii, mPresentationFactory.makePresentationGetter(ri)); + ii, ri, ii, mTargetDataLoader.createPresentationGetter(ri)); mCallerTargets.add(displayResolveInfo); if (mCallerTargets.size() == MAX_SUGGESTED_APP_TARGETS) break; } @@ -240,6 +245,15 @@ public class ChooserListAdapter extends ResolverListAdapter { } + @Override + protected boolean rebuildList(boolean doPostProcessing) { + mAnimationTracker.reset(); + mSortedList.clear(); + boolean result = super.rebuildList(doPostProcessing); + notifyDataSetChanged(); + return result; + } + private void createPlaceHolders() { mServiceTargets.clear(); for (int i = 0; i < mMaxRankedTargets; ++i) { @@ -262,8 +276,18 @@ public class ChooserListAdapter extends ResolverListAdapter { return; } - holder.bindLabel(info.getDisplayLabel(), info.getExtendedInfo(), alwaysShowSubLabel()); - holder.bindIcon(info, /*animate =*/ true); + holder.bindLabel(info.getDisplayLabel(), info.getExtendedInfo()); + mAnimationTracker.animateLabel(holder.text, info); + if (holder.text2.getVisibility() == View.VISIBLE) { + mAnimationTracker.animateLabel(holder.text2, info); + } + holder.bindIcon(info); + if (info.getDisplayIconHolder().getDisplayIcon() != null) { + mAnimationTracker.animateIcon(holder.icon, info); + } else { + holder.icon.clearAnimation(); + } + if (info.isSelectableTargetInfo()) { // direct share targets should append the application name for a better readout DisplayResolveInfo rInfo = info.getDisplayResolveInfo(); @@ -320,19 +344,19 @@ public class ChooserListAdapter extends ResolverListAdapter { } private void loadDirectShareIcon(SelectableTargetInfo info) { - LoadDirectShareIconTask task = (LoadDirectShareIconTask) mIconLoaders.get(info); - if (task == null) { - task = createLoadDirectShareIconTask(info); - mIconLoaders.put(info, task); - task.loadIcon(); + if (mRequestedIcons.add(info)) { + mTargetDataLoader.loadDirectShareIcon( + info, + getUserHandle(), + (drawable) -> onDirectShareIconLoaded(info, drawable)); } } - @VisibleForTesting - protected LoadDirectShareIconTask createLoadDirectShareIconTask(SelectableTargetInfo info) { - return new LoadDirectShareIconTask( - mContext.createContextAsUser(getUserHandle(), 0), - info); + private void onDirectShareIconLoaded(SelectableTargetInfo mTargetInfo, Drawable icon) { + if (icon != null && !mTargetInfo.hasDisplayIcon()) { + mTargetInfo.getDisplayIconHolder().setDisplayIcon(icon); + notifyDataSetChanged(); + } } void updateAlphabeticalList() { @@ -341,6 +365,15 @@ public class ChooserListAdapter extends ResolverListAdapter { new AsyncTask<Void, Void, List<DisplayResolveInfo>>() { @Override protected List<DisplayResolveInfo> doInBackground(Void... voids) { + try { + Trace.beginSection("update-alphabetical-list"); + return updateList(); + } finally { + Trace.endSection(); + } + } + + private List<DisplayResolveInfo> updateList() { List<DisplayResolveInfo> allTargets = new ArrayList<>(); allTargets.addAll(getTargetsInCurrentDisplayList()); allTargets.addAll(mCallerTargets); @@ -351,6 +384,8 @@ public class ChooserListAdapter extends ResolverListAdapter { .collect(Collectors.groupingBy(target -> target.getResolvedComponentName().getPackageName() + "#" + target.getDisplayLabel() + + '#' + ResolverActivity.getResolveInfoUserHandle( + target.getResolveInfo(), getUserHandle()).getIdentifier() )) .values() .stream() @@ -604,12 +639,6 @@ public class ChooserListAdapter extends ResolverListAdapter { notifyDataSetChanged(); } - protected boolean alwaysShowSubLabel() { - // Always show a subLabel for visual consistency across list items. Show an empty - // subLabel if the subLabel is the same as the label - return true; - } - /** * Rather than fully sorting the input list, this sorting task will put the top k elements * in the head of input list and fill the tail with other elements in undetermined order. @@ -640,95 +669,4 @@ public class ChooserListAdapter extends ResolverListAdapter { }; } - /** - * Loads direct share targets icons. - */ - @VisibleForTesting - public class LoadDirectShareIconTask extends AsyncTask<Void, Void, Drawable> { - private final Context mContext; - private final SelectableTargetInfo mTargetInfo; - - private LoadDirectShareIconTask(Context context, SelectableTargetInfo targetInfo) { - mContext = context; - mTargetInfo = targetInfo; - } - - @Override - protected Drawable doInBackground(Void... voids) { - Drawable drawable; - try { - drawable = getChooserTargetIconDrawable( - mContext, - mTargetInfo.getChooserTargetIcon(), - mTargetInfo.getChooserTargetComponentName(), - mTargetInfo.getDirectShareShortcutInfo()); - } catch (Exception e) { - Log.e(TAG, - "Failed to load shortcut icon for " - + mTargetInfo.getChooserTargetComponentName(), - e); - drawable = loadIconPlaceholder(); - } - return drawable; - } - - @Override - protected void onPostExecute(@Nullable Drawable icon) { - if (icon != null && !mTargetInfo.hasDisplayIcon()) { - mTargetInfo.getDisplayIconHolder().setDisplayIcon(icon); - notifyDataSetChanged(); - } - } - - @WorkerThread - private Drawable getChooserTargetIconDrawable( - Context context, - @Nullable Icon icon, - ComponentName targetComponentName, - @Nullable ShortcutInfo shortcutInfo) { - Drawable directShareIcon = null; - - // First get the target drawable and associated activity info - if (icon != null) { - directShareIcon = icon.loadDrawable(context); - } else if (shortcutInfo != null) { - LauncherApps launcherApps = context.getSystemService(LauncherApps.class); - if (launcherApps != null) { - directShareIcon = launcherApps.getShortcutIconDrawable(shortcutInfo, 0); - } - } - - if (directShareIcon == null) { - return null; - } - - ActivityInfo info = null; - try { - info = context.getPackageManager().getActivityInfo(targetComponentName, 0); - } catch (PackageManager.NameNotFoundException error) { - Log.e(TAG, "Could not find activity associated with ChooserTarget"); - } - - if (info == null) { - return null; - } - - // Now fetch app icon and raster with no badging even in work profile - Bitmap appIcon = mPresentationFactory.makePresentationGetter(info).getIconBitmap(null); - - // Raster target drawable with appIcon as a badge - SimpleIconFactory sif = SimpleIconFactory.obtain(context); - Bitmap directShareBadgedIcon = sif.createAppBadgedIconBitmap(directShareIcon, appIcon); - sif.recycle(); - - return new BitmapDrawable(context.getResources(), directShareBadgedIcon); - } - - /** - * An alias for execute to use with unit tests. - */ - public void loadIcon() { - execute(); - } - } } diff --git a/java/src/com/android/intentresolver/ChooserMultiProfilePagerAdapter.java b/java/src/com/android/intentresolver/ChooserMultiProfilePagerAdapter.java index 3e2ea473..c159243e 100644 --- a/java/src/com/android/intentresolver/ChooserMultiProfilePagerAdapter.java +++ b/java/src/com/android/intentresolver/ChooserMultiProfilePagerAdapter.java @@ -26,6 +26,7 @@ import androidx.recyclerview.widget.RecyclerView; import androidx.viewpager.widget.PagerAdapter; import com.android.intentresolver.grid.ChooserGridAdapter; +import com.android.intentresolver.measurements.Tracer; import com.android.internal.annotations.VisibleForTesting; import com.google.common.collect.ImmutableList; @@ -50,6 +51,7 @@ public class ChooserMultiProfilePagerAdapter extends GenericMultiProfilePagerAda EmptyStateProvider emptyStateProvider, Supplier<Boolean> workProfileQuietModeChecker, UserHandle workProfileUserHandle, + UserHandle cloneProfileUserHandle, int maxTargetsPerRow) { this( context, @@ -59,6 +61,7 @@ public class ChooserMultiProfilePagerAdapter extends GenericMultiProfilePagerAda workProfileQuietModeChecker, /* defaultProfile= */ 0, workProfileUserHandle, + cloneProfileUserHandle, new BottomPaddingOverrideSupplier(context)); } @@ -70,6 +73,7 @@ public class ChooserMultiProfilePagerAdapter extends GenericMultiProfilePagerAda Supplier<Boolean> workProfileQuietModeChecker, @Profile int defaultProfile, UserHandle workProfileUserHandle, + UserHandle cloneProfileUserHandle, int maxTargetsPerRow) { this( context, @@ -79,6 +83,7 @@ public class ChooserMultiProfilePagerAdapter extends GenericMultiProfilePagerAda workProfileQuietModeChecker, defaultProfile, workProfileUserHandle, + cloneProfileUserHandle, new BottomPaddingOverrideSupplier(context)); } @@ -90,6 +95,7 @@ public class ChooserMultiProfilePagerAdapter extends GenericMultiProfilePagerAda Supplier<Boolean> workProfileQuietModeChecker, @Profile int defaultProfile, UserHandle workProfileUserHandle, + UserHandle cloneProfileUserHandle, BottomPaddingOverrideSupplier bottomPaddingOverrideSupplier) { super( context, @@ -100,6 +106,7 @@ public class ChooserMultiProfilePagerAdapter extends GenericMultiProfilePagerAda workProfileQuietModeChecker, defaultProfile, workProfileUserHandle, + cloneProfileUserHandle, () -> makeProfileView(context), bottomPaddingOverrideSupplier); mAdapterBinder = adapterBinder; @@ -114,6 +121,16 @@ public class ChooserMultiProfilePagerAdapter extends GenericMultiProfilePagerAda mBottomPaddingOverrideSupplier.setEmptyStateBottomOffset(bottomOffset); } + /** + * Notify adapter about the drawer's collapse state. This will affect the app divider's + * visibility. + */ + public void setIsCollapsed(boolean isCollapsed) { + for (int i = 0, size = getItemCount(); i < size; i++) { + getAdapterForIndex(i).setAzLabelVisibility(!isCollapsed); + } + } + private static ViewGroup makeProfileView(Context context) { LayoutInflater inflater = LayoutInflater.from(context); ViewGroup rootView = (ViewGroup) inflater.inflate( @@ -124,6 +141,22 @@ public class ChooserMultiProfilePagerAdapter extends GenericMultiProfilePagerAda return rootView; } + @Override + boolean rebuildActiveTab(boolean doPostProcessing) { + if (doPostProcessing) { + Tracer.INSTANCE.beginAppTargetLoadingSection(getActiveListAdapter().getUserHandle()); + } + return super.rebuildActiveTab(doPostProcessing); + } + + @Override + boolean rebuildInactiveTab(boolean doPostProcessing) { + if (getItemCount() != 1 && doPostProcessing) { + Tracer.INSTANCE.beginAppTargetLoadingSection(getInactiveListAdapter().getUserHandle()); + } + return super.rebuildInactiveTab(doPostProcessing); + } + private static class BottomPaddingOverrideSupplier implements Supplier<Optional<Integer>> { private final Context mContext; private int mBottomOffset; diff --git a/java/src/com/android/intentresolver/ChooserRefinementManager.java b/java/src/com/android/intentresolver/ChooserRefinementManager.java index 3ddc1c7c..2ebe48a6 100644 --- a/java/src/com/android/intentresolver/ChooserRefinementManager.java +++ b/java/src/com/android/intentresolver/ChooserRefinementManager.java @@ -17,18 +17,21 @@ package com.android.intentresolver; import android.annotation.Nullable; +import android.annotation.UiThread; import android.app.Activity; -import android.content.Context; +import android.app.Application; import android.content.Intent; import android.content.IntentSender; -import android.content.IntentSender.SendIntentException; import android.os.Bundle; import android.os.Handler; import android.os.Parcel; -import android.os.Parcelable; import android.os.ResultReceiver; import android.util.Log; +import androidx.lifecycle.LiveData; +import androidx.lifecycle.MutableLiveData; +import androidx.lifecycle.ViewModel; + import com.android.intentresolver.chooser.TargetInfo; import java.util.List; @@ -41,28 +44,52 @@ import java.util.function.Consumer; * convert the format of the payload, or lazy-download some data that was deferred in the original * call). */ -public final class ChooserRefinementManager { +@UiThread +public final class ChooserRefinementManager extends ViewModel { private static final String TAG = "ChooserRefinement"; - @Nullable - private final IntentSender mRefinementIntentSender; + @Nullable // Non-null only during an active refinement session. + private RefinementResultReceiver mRefinementResultReceiver; - private final Context mContext; - private final Consumer<TargetInfo> mOnSelectionRefined; - private final Runnable mOnRefinementCancelled; + private boolean mConfigurationChangeInProgress = false; - @Nullable - private RefinementResultReceiver mRefinementResultReceiver; + /** + * A token for the completion of a refinement process that can be consumed exactly once. + */ + public static class RefinementCompletion { + private TargetInfo mTargetInfo; + private boolean mConsumed; + + RefinementCompletion(TargetInfo targetInfo) { + mTargetInfo = targetInfo; + } + + /** + * @return The output of the completed refinement process. Null if the process was aborted + * or failed. + */ + public TargetInfo getTargetInfo() { + return mTargetInfo; + } - public ChooserRefinementManager( - Context context, - @Nullable IntentSender refinementIntentSender, - Consumer<TargetInfo> onSelectionRefined, - Runnable onRefinementCancelled) { - mContext = context; - mRefinementIntentSender = refinementIntentSender; - mOnSelectionRefined = onSelectionRefined; - mOnRefinementCancelled = onRefinementCancelled; + /** + * Mark this event as consumed if it wasn't already. + * + * @return true if this had not already been consumed. + */ + public boolean consume() { + if (!mConsumed) { + mConsumed = true; + return true; + } + return false; + } + } + + private MutableLiveData<RefinementCompletion> mRefinementCompletion = new MutableLiveData<>(); + + public LiveData<RefinementCompletion> getRefinementCompletion() { + return mRefinementCompletion; } /** @@ -70,44 +97,83 @@ public final class ChooserRefinementManager { * @return true if the selection should wait for a now-started refinement flow, or false if it * can proceed by the default (non-refinement) logic. */ - public boolean maybeHandleSelection(TargetInfo selectedTarget) { - if (mRefinementIntentSender == null) { + public boolean maybeHandleSelection(TargetInfo selectedTarget, + IntentSender refinementIntentSender, Application application, Handler mainHandler) { + if (refinementIntentSender == null) { return false; } if (selectedTarget.getAllSourceIntents().isEmpty()) { return false; } + if (selectedTarget.isSuspended()) { + // We expect all launches to fail for this target, so don't make the user go through the + // refinement flow first. Besides, the default (non-refinement) handling displays a + // warning in this case and recovers the session; we won't be equipped to recover if + // problems only come up after refinement. + return false; + } destroy(); // Terminate any prior sessions. mRefinementResultReceiver = new RefinementResultReceiver( refinedIntent -> { destroy(); + TargetInfo refinedTarget = selectedTarget.tryToCloneWithAppliedRefinement(refinedIntent); if (refinedTarget != null) { - mOnSelectionRefined.accept(refinedTarget); + mRefinementCompletion.setValue(new RefinementCompletion(refinedTarget)); } else { Log.e(TAG, "Failed to apply refinement to any matching source intent"); - mOnRefinementCancelled.run(); + mRefinementCompletion.setValue(new RefinementCompletion(null)); } }, - mOnRefinementCancelled, - mContext.getMainThreadHandler()); + () -> { + destroy(); + mRefinementCompletion.setValue(new RefinementCompletion(null)); + }, + mainHandler); Intent refinementRequest = makeRefinementRequest(mRefinementResultReceiver, selectedTarget); try { - mRefinementIntentSender.sendIntent(mContext, 0, refinementRequest, null, null); + refinementIntentSender.sendIntent(application, 0, refinementRequest, null, null); return true; - } catch (SendIntentException e) { + } catch (IntentSender.SendIntentException e) { Log.e(TAG, "Refinement IntentSender failed to send", e); } - return false; + return true; + } + + /** ChooserActivity has stopped */ + public void onActivityStop(boolean configurationChanging) { + mConfigurationChangeInProgress = configurationChanging; + } + + /** ChooserActivity has resumed */ + public void onActivityResume() { + if (mConfigurationChangeInProgress) { + mConfigurationChangeInProgress = false; + } else { + if (mRefinementResultReceiver != null) { + // This can happen if the refinement activity terminates without ever sending a + // response to our `ResultReceiver`. We're probably not prepared to return the user + // into a valid Chooser session, so we'll treat it as a cancellation instead. + Log.w(TAG, "Chooser resumed while awaiting refinement result; aborting"); + destroy(); + mRefinementCompletion.setValue(new RefinementCompletion(null)); + } + } + } + + @Override + protected void onCleared() { + // App lifecycle over, time to clean up. + destroy(); } /** Clean up any ongoing refinement session. */ - public void destroy() { + private void destroy() { if (mRefinementResultReceiver != null) { - mRefinementResultReceiver.destroy(); + mRefinementResultReceiver.destroyReceiver(); mRefinementResultReceiver = null; } } @@ -144,7 +210,7 @@ public final class ChooserRefinementManager { mOnRefinementCancelled = onRefinementCancelled; } - public void destroy() { + public void destroyReceiver() { mDestroyed = true; } @@ -154,27 +220,14 @@ public final class ChooserRefinementManager { Log.e(TAG, "Destroyed RefinementResultReceiver received a result"); return; } - if (resultData == null) { - Log.e(TAG, "RefinementResultReceiver received null resultData"); - // TODO: treat as cancellation? - return; - } - switch (resultCode) { - case Activity.RESULT_CANCELED: - mOnRefinementCancelled.run(); - break; - case Activity.RESULT_OK: - Parcelable intentParcelable = resultData.getParcelable(Intent.EXTRA_INTENT); - if (intentParcelable instanceof Intent) { - mOnSelectionRefined.accept((Intent) intentParcelable); - } else { - Log.e(TAG, "No valid Intent.EXTRA_INTENT in 'OK' refinement result data"); - } - break; - default: - Log.w(TAG, "Received unknown refinement result " + resultCode); - break; + destroyReceiver(); // This is the single callback we'll accept from this session. + + Intent refinedResult = tryToExtractRefinedResult(resultCode, resultData); + if (refinedResult == null) { + mOnRefinementCancelled.run(); + } else { + mOnSelectionRefined.accept(refinedResult); } } @@ -190,5 +243,24 @@ public final class ChooserRefinementManager { parcel.recycle(); return receiverForSending; } + + /** + * Get the refinement from the result data, if possible, or log diagnostics and return null. + */ + @Nullable + private static Intent tryToExtractRefinedResult(int resultCode, Bundle resultData) { + if (Activity.RESULT_CANCELED == resultCode) { + Log.i(TAG, "Refinement canceled by caller"); + } else if (Activity.RESULT_OK != resultCode) { + Log.w(TAG, "Canceling refinement on unrecognized result code " + resultCode); + } else if (resultData == null) { + Log.e(TAG, "RefinementResultReceiver received null resultData; canceling"); + } else if (!(resultData.getParcelable(Intent.EXTRA_INTENT) instanceof Intent)) { + Log.e(TAG, "No valid Intent.EXTRA_INTENT in 'OK' refinement result data"); + } else { + return resultData.getParcelable(Intent.EXTRA_INTENT, Intent.class); + } + return null; + } } } diff --git a/java/src/com/android/intentresolver/ChooserRequestParameters.java b/java/src/com/android/intentresolver/ChooserRequestParameters.java index 3d99e475..5157986b 100644 --- a/java/src/com/android/intentresolver/ChooserRequestParameters.java +++ b/java/src/com/android/intentresolver/ChooserRequestParameters.java @@ -18,7 +18,6 @@ package com.android.intentresolver; import android.annotation.NonNull; import android.annotation.Nullable; -import android.app.PendingIntent; import android.content.ComponentName; import android.content.Intent; import android.content.IntentFilter; @@ -34,7 +33,7 @@ import android.util.Log; import android.util.Pair; import com.android.intentresolver.flags.FeatureFlagRepository; -import com.android.intentresolver.flags.Flags; +import com.android.intentresolver.util.UriFilters; import com.google.common.collect.ImmutableList; @@ -69,16 +68,16 @@ public class ChooserRequestParameters { private static final int LAUNCH_FLAGS_FOR_SEND_ACTION = Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK; + private static final int MAX_CHOOSER_ACTIONS = 5; private final Intent mTarget; - private final ChooserIntegratedDeviceComponents mIntegratedDeviceComponents; private final String mReferrerPackageName; private final Pair<CharSequence, Integer> mTitleSpec; private final Intent mReferrerFillInIntent; private final ImmutableList<ComponentName> mFilteredComponentNames; private final ImmutableList<ChooserTarget> mCallerChooserTargets; private final @NonNull ImmutableList<ChooserAction> mChooserActions; - private final PendingIntent mModifyShareAction; + private final ChooserAction mModifyShareAction; private final boolean mRetainInOnStop; @Nullable @@ -106,14 +105,11 @@ public class ChooserRequestParameters { final Intent clientIntent, String referrerPackageName, final Uri referrer, - ChooserIntegratedDeviceComponents integratedDeviceComponents, FeatureFlagRepository featureFlags) { final Intent requestedTarget = parseTargetIntentExtra( clientIntent.getParcelableExtra(Intent.EXTRA_INTENT)); mTarget = intentWithModifiedLaunchFlags(requestedTarget); - mIntegratedDeviceComponents = integratedDeviceComponents; - mReferrerPackageName = referrerPackageName; mAdditionalTargets = intentsWithModifiedLaunchFlagsFromExtraIfPresent( @@ -135,8 +131,11 @@ public class ChooserRequestParameters { mRefinementIntentSender = clientIntent.getParcelableExtra( Intent.EXTRA_CHOOSER_REFINEMENT_INTENT_SENDER); - mFilteredComponentNames = getFilteredComponentNames( - clientIntent, mIntegratedDeviceComponents.getNearbySharingComponent()); + ComponentName[] filteredComponents = clientIntent.getParcelableArrayExtra( + Intent.EXTRA_EXCLUDE_COMPONENTS, ComponentName.class); + mFilteredComponentNames = filteredComponents != null + ? ImmutableList.copyOf(filteredComponents) + : ImmutableList.of(); mCallerChooserTargets = parseCallerTargetsFromClientIntent(clientIntent); @@ -147,12 +146,8 @@ public class ChooserRequestParameters { mTargetIntentFilter = getTargetIntentFilter(mTarget); - mChooserActions = featureFlags.isEnabled(Flags.SHARESHEET_CUSTOM_ACTIONS) - ? getChooserActions(clientIntent) - : ImmutableList.of(); - mModifyShareAction = featureFlags.isEnabled(Flags.SHARESHEET_RESELECTION_ACTION) - ? getModifyShareAction(clientIntent) - : null; + mChooserActions = getChooserActions(clientIntent); + mModifyShareAction = getModifyShareAction(clientIntent); } public Intent getTargetIntent() { @@ -204,7 +199,7 @@ public class ChooserRequestParameters { } @Nullable - public PendingIntent getModifyShareAction() { + public ChooserAction getModifyShareAction() { return mModifyShareAction; } @@ -258,10 +253,6 @@ public class ChooserRequestParameters { return mTargetIntentFilter; } - public ChooserIntegratedDeviceComponents getIntegratedDeviceComponents() { - return mIntegratedDeviceComponents; - } - private static boolean isSendAction(@Nullable String action) { return (Intent.ACTION_SEND.equals(action) || Intent.ACTION_SEND_MULTIPLE.equals(action)); } @@ -310,29 +301,11 @@ public class ChooserRequestParameters { requestedTitle = null; } - int defaultTitleRes = - (requestedTitle == null) ? com.android.internal.R.string.chooseActivity : 0; + int defaultTitleRes = (requestedTitle == null) ? R.string.chooseActivity : 0; return Pair.create(requestedTitle, defaultTitleRes); } - private static ImmutableList<ComponentName> getFilteredComponentNames( - Intent clientIntent, @Nullable ComponentName nearbySharingComponent) { - Stream<ComponentName> filteredComponents = streamParcelableArrayExtra( - clientIntent, Intent.EXTRA_EXCLUDE_COMPONENTS, ComponentName.class, true, true); - - if (nearbySharingComponent != null) { - // Exclude Nearby from main list if chip is present, to avoid duplication. - // TODO: we don't have an explicit guarantee that the chip will be displayed just - // because we have a non-null component; that's ultimately determined by the preview - // layout. Maybe we can make that decision further upstream? - filteredComponents = Stream.concat( - filteredComponents, Stream.of(nearbySharingComponent)); - } - - return filteredComponents.collect(toImmutableList()); - } - private static ImmutableList<ChooserTarget> parseCallerTargetsFromClientIntent( Intent clientIntent) { return @@ -349,15 +322,17 @@ public class ChooserRequestParameters { ChooserAction.class, true, true) - .collect(toImmutableList()); + .filter(UriFilters::hasValidIcon) + .limit(MAX_CHOOSER_ACTIONS) + .collect(toImmutableList()); } @Nullable - private static PendingIntent getModifyShareAction(Intent intent) { + private static ChooserAction getModifyShareAction(Intent intent) { try { return intent.getParcelableExtra( Intent.EXTRA_CHOOSER_MODIFY_SHARE_ACTION, - PendingIntent.class); + ChooserAction.class); } catch (Throwable t) { Log.w( TAG, diff --git a/java/src/com/android/intentresolver/ChooserTargetActionsDialogFragment.java b/java/src/com/android/intentresolver/ChooserTargetActionsDialogFragment.java index 0aa32505..4bfb21aa 100644 --- a/java/src/com/android/intentresolver/ChooserTargetActionsDialogFragment.java +++ b/java/src/com/android/intentresolver/ChooserTargetActionsDialogFragment.java @@ -142,6 +142,12 @@ public class ChooserTargetActionsDialogFragment extends DialogFragment return v; } + @Override + public void onStop() { + super.onStop(); + dismissAllowingStateLoss(); + } + class VHAdapter extends RecyclerView.Adapter<VH> { List<Pair<Drawable, CharSequence>> mItems; diff --git a/java/src/com/android/intentresolver/GenericMultiProfilePagerAdapter.java b/java/src/com/android/intentresolver/GenericMultiProfilePagerAdapter.java index 7613f35f..a1c53402 100644 --- a/java/src/com/android/intentresolver/GenericMultiProfilePagerAdapter.java +++ b/java/src/com/android/intentresolver/GenericMultiProfilePagerAdapter.java @@ -19,6 +19,7 @@ package com.android.intentresolver; import android.annotation.Nullable; import android.content.Context; import android.os.UserHandle; +import android.util.Log; import android.view.View; import android.view.ViewGroup; @@ -84,6 +85,7 @@ class GenericMultiProfilePagerAdapter< Supplier<Boolean> workProfileQuietModeChecker, @Profile int defaultProfile, UserHandle workProfileUserHandle, + UserHandle cloneProfileUserHandle, Supplier<ViewGroup> pageViewInflater, Supplier<Optional<Integer>> containerBottomPaddingOverrideSupplier) { super( @@ -91,7 +93,8 @@ class GenericMultiProfilePagerAdapter< /* currentPage= */ defaultProfile, emptyStateProvider, workProfileQuietModeChecker, - workProfileUserHandle); + workProfileUserHandle, + cloneProfileUserHandle); mListAdapterExtractor = listAdapterExtractor; mAdapterBinder = adapterBinder; @@ -145,12 +148,12 @@ class GenericMultiProfilePagerAdapter< @Override @Nullable protected ListAdapterT getListAdapterForUserHandle(UserHandle userHandle) { - if (getActiveListAdapter().getUserHandle().equals(userHandle)) { - return getActiveListAdapter(); - } - if ((getInactiveListAdapter() != null) && getInactiveListAdapter().getUserHandle().equals( - userHandle)) { - return getInactiveListAdapter(); + if (getPersonalListAdapter().getUserHandle().equals(userHandle) + || userHandle.equals(getCloneUserHandle())) { + return getPersonalListAdapter(); + } else if (getWorkListAdapter() != null + && getWorkListAdapter().getUserHandle().equals(userHandle)) { + return getWorkListAdapter(); } return null; } @@ -177,6 +180,9 @@ class GenericMultiProfilePagerAdapter< @Override public ListAdapterT getWorkListAdapter() { + if (!hasAdapterForIndex(PROFILE_WORK)) { + return null; + } return mListAdapterExtractor.apply(getAdapterForIndex(PROFILE_WORK)); } @@ -209,6 +215,10 @@ class GenericMultiProfilePagerAdapter< paddingBottom)); } + private boolean hasAdapterForIndex(int pageIndex) { + return (pageIndex < getCount()); + } + // TODO: `ChooserActivity` also has a per-profile record type. Maybe the "multi-profile pager" // should be the owner of all per-profile data (especially now that the API is generic)? private static class GenericProfileDescriptor<PageViewT, SinglePageAdapterT> extends diff --git a/java/src/com/android/intentresolver/ImagePreviewImageLoader.kt b/java/src/com/android/intentresolver/ImagePreviewImageLoader.kt deleted file mode 100644 index 7b6651a2..00000000 --- a/java/src/com/android/intentresolver/ImagePreviewImageLoader.kt +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright (C) 2022 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.intentresolver - -import android.content.Context -import android.graphics.Bitmap -import android.net.Uri -import android.util.Size -import androidx.annotation.GuardedBy -import androidx.annotation.VisibleForTesting -import androidx.collection.LruCache -import androidx.lifecycle.Lifecycle -import androidx.lifecycle.coroutineScope -import kotlinx.coroutines.CompletableDeferred -import kotlinx.coroutines.CoroutineDispatcher -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.isActive -import kotlinx.coroutines.launch -import java.util.function.Consumer - -@VisibleForTesting -class ImagePreviewImageLoader @JvmOverloads constructor( - private val context: Context, - private val lifecycle: Lifecycle, - cacheSize: Int, - private val dispatcher: CoroutineDispatcher = Dispatchers.IO -) : ImageLoader { - - private val thumbnailSize: Size = - context.resources.getDimensionPixelSize(R.dimen.chooser_preview_image_max_dimen).let { - Size(it, it) - } - - @GuardedBy("self") - private val cache = LruCache<Uri, CompletableDeferred<Bitmap?>>(cacheSize) - - override suspend fun invoke(uri: Uri): Bitmap? = loadImageAsync(uri) - - override fun loadImage(uri: Uri, callback: Consumer<Bitmap?>) { - lifecycle.coroutineScope.launch { - val image = loadImageAsync(uri) - if (isActive) { - callback.accept(image) - } - } - } - - override fun prePopulate(uris: List<Uri>) { - uris.asSequence().take(cache.maxSize()).forEach { uri -> - lifecycle.coroutineScope.launch { - loadImageAsync(uri) - } - } - } - - private suspend fun loadImageAsync(uri: Uri): Bitmap? { - return synchronized(cache) { - cache.get(uri) ?: CompletableDeferred<Bitmap?>().also { result -> - cache.put(uri, result) - lifecycle.coroutineScope.launch(dispatcher) { - result.loadBitmap(uri) - } - } - }.await() - } - - private fun CompletableDeferred<Bitmap?>.loadBitmap(uri: Uri) { - val bitmap = runCatching { - context.contentResolver.loadThumbnail(uri, thumbnailSize, null) - }.getOrNull() - complete(bitmap) - } -} diff --git a/java/src/com/android/intentresolver/IntentForwarderActivity.java b/java/src/com/android/intentresolver/IntentForwarderActivity.java index 78240250..5e8945f1 100644 --- a/java/src/com/android/intentresolver/IntentForwarderActivity.java +++ b/java/src/com/android/intentresolver/IntentForwarderActivity.java @@ -162,13 +162,13 @@ public class IntentForwarderActivity extends Activity { private String getForwardToPersonalMessage() { return getSystemService(DevicePolicyManager.class).getResources().getString( FORWARD_INTENT_TO_PERSONAL, - () -> getString(com.android.internal.R.string.forward_intent_to_owner)); + () -> getString(R.string.forward_intent_to_owner)); } private String getForwardToWorkMessage() { return getSystemService(DevicePolicyManager.class).getResources().getString( FORWARD_INTENT_TO_WORK, - () -> getString(com.android.internal.R.string.forward_intent_to_work)); + () -> getString(R.string.forward_intent_to_work)); } private boolean isIntentForwarderResolveInfo(ResolveInfo resolveInfo) { diff --git a/java/src/com/android/intentresolver/ItemRevealAnimationTracker.kt b/java/src/com/android/intentresolver/ItemRevealAnimationTracker.kt new file mode 100644 index 00000000..d3e07c6b --- /dev/null +++ b/java/src/com/android/intentresolver/ItemRevealAnimationTracker.kt @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.intentresolver + +import android.view.View +import android.view.animation.AlphaAnimation +import android.view.animation.LinearInterpolator +import android.view.animation.Transformation +import com.android.intentresolver.chooser.TargetInfo + +private const val IMAGE_FADE_IN_MILLIS = 150L + +internal class ItemRevealAnimationTracker { + private val iconProgress = HashMap<TargetInfo, Record>() + private val labelProgress = HashMap<TargetInfo, Record>() + + fun reset() { + iconProgress.clear() + labelProgress.clear() + } + + fun animateIcon(view: View, info: TargetInfo) = animateView(view, info, iconProgress) + fun animateLabel(view: View, info: TargetInfo) = animateView(view, info, labelProgress) + + private fun animateView(view: View, info: TargetInfo, map: MutableMap<TargetInfo, Record>) { + val record = map.getOrPut(info) { + Record() + } + if ((view.animation as? RevealAnimation)?.record === record) return + + view.clearAnimation() + if (record.alpha >= 1f) { + view.alpha = 1f + return + } + + view.startAnimation(RevealAnimation(record)) + } + + private class Record(var alpha: Float = 0f) + + private class RevealAnimation(val record: Record) : AlphaAnimation(record.alpha, 1f) { + init { + duration = (IMAGE_FADE_IN_MILLIS * (1f - record.alpha)).toLong() + interpolator = LinearInterpolator() + } + + override fun applyTransformation(interpolatedTime: Float, t: Transformation) { + super.applyTransformation(interpolatedTime, t) + // One TargetInfo can be simultaneously bou into multiple UI grid items; make sure + // that the alpha value only increases. This should not affect running animations, only + // a starting point for a new animation when a different view is bound to this target. + record.alpha = minOf(1f, maxOf(record.alpha, t.alpha)) + } + } +} diff --git a/java/src/com/android/intentresolver/NoAppsAvailableEmptyStateProvider.java b/java/src/com/android/intentresolver/NoAppsAvailableEmptyStateProvider.java index c1373f4b..a7b50f38 100644 --- a/java/src/com/android/intentresolver/NoAppsAvailableEmptyStateProvider.java +++ b/java/src/com/android/intentresolver/NoAppsAvailableEmptyStateProvider.java @@ -30,7 +30,6 @@ import android.stats.devicepolicy.nano.DevicePolicyEnums; import com.android.intentresolver.AbstractMultiProfilePagerAdapter.EmptyState; import com.android.intentresolver.AbstractMultiProfilePagerAdapter.EmptyStateProvider; -import com.android.intentresolver.AbstractMultiProfilePagerAdapter.MyUserIdProvider; import com.android.internal.R; import java.util.List; @@ -50,16 +49,16 @@ public class NoAppsAvailableEmptyStateProvider implements EmptyStateProvider { @NonNull private final String mMetricsCategory; @NonNull - private final MyUserIdProvider mMyUserIdProvider; + private final UserHandle mTabOwnerUserHandleForLaunch; public NoAppsAvailableEmptyStateProvider(Context context, UserHandle workProfileUserHandle, UserHandle personalProfileUserHandle, String metricsCategory, - MyUserIdProvider myUserIdProvider) { + UserHandle tabOwnerUserHandleForLaunch) { mContext = context; mWorkProfileUserHandle = workProfileUserHandle; mPersonalProfileUserHandle = personalProfileUserHandle; mMetricsCategory = metricsCategory; - mMyUserIdProvider = myUserIdProvider; + mTabOwnerUserHandleForLaunch = tabOwnerUserHandleForLaunch; } @Nullable @@ -69,7 +68,7 @@ public class NoAppsAvailableEmptyStateProvider implements EmptyStateProvider { UserHandle listUserHandle = resolverListAdapter.getUserHandle(); if (mWorkProfileUserHandle != null - && (mMyUserIdProvider.getMyUserId() == listUserHandle.getIdentifier() + && (mTabOwnerUserHandleForLaunch.equals(listUserHandle) || !hasAppsInOtherProfile(resolverListAdapter))) { String title; @@ -102,7 +101,7 @@ public class NoAppsAvailableEmptyStateProvider implements EmptyStateProvider { return false; } List<ResolvedComponentInfo> resolversForIntent = - adapter.getResolversForUser(UserHandle.of(mMyUserIdProvider.getMyUserId())); + adapter.getResolversForUser(mTabOwnerUserHandleForLaunch); for (ResolvedComponentInfo info : resolversForIntent) { ResolveInfo resolveInfo = info.getResolveInfoAt(0); if (resolveInfo.targetUserId != UserHandle.USER_CURRENT) { diff --git a/java/src/com/android/intentresolver/NoCrossProfileEmptyStateProvider.java b/java/src/com/android/intentresolver/NoCrossProfileEmptyStateProvider.java index 420d26c5..6f72bb00 100644 --- a/java/src/com/android/intentresolver/NoCrossProfileEmptyStateProvider.java +++ b/java/src/com/android/intentresolver/NoCrossProfileEmptyStateProvider.java @@ -27,7 +27,6 @@ import android.os.UserHandle; import com.android.intentresolver.AbstractMultiProfilePagerAdapter.CrossProfileIntentsChecker; import com.android.intentresolver.AbstractMultiProfilePagerAdapter.EmptyState; import com.android.intentresolver.AbstractMultiProfilePagerAdapter.EmptyStateProvider; -import com.android.intentresolver.AbstractMultiProfilePagerAdapter.MyUserIdProvider; /** * Empty state provider that does not allow cross profile sharing, it will return a blocker @@ -39,28 +38,28 @@ public class NoCrossProfileEmptyStateProvider implements EmptyStateProvider { private final EmptyState mNoWorkToPersonalEmptyState; private final EmptyState mNoPersonalToWorkEmptyState; private final CrossProfileIntentsChecker mCrossProfileIntentsChecker; - private final MyUserIdProvider mUserIdProvider; + private final UserHandle mTabOwnerUserHandleForLaunch; public NoCrossProfileEmptyStateProvider(UserHandle personalUserHandle, EmptyState noWorkToPersonalEmptyState, EmptyState noPersonalToWorkEmptyState, CrossProfileIntentsChecker crossProfileIntentsChecker, - MyUserIdProvider myUserIdProvider) { + UserHandle tabOwnerUserHandleForLaunch) { mPersonalProfileUserHandle = personalUserHandle; mNoWorkToPersonalEmptyState = noWorkToPersonalEmptyState; mNoPersonalToWorkEmptyState = noPersonalToWorkEmptyState; mCrossProfileIntentsChecker = crossProfileIntentsChecker; - mUserIdProvider = myUserIdProvider; + mTabOwnerUserHandleForLaunch = tabOwnerUserHandleForLaunch; } @Nullable @Override public EmptyState getEmptyState(ResolverListAdapter resolverListAdapter) { boolean shouldShowBlocker = - mUserIdProvider.getMyUserId() != resolverListAdapter.getUserHandle().getIdentifier() + !mTabOwnerUserHandleForLaunch.equals(resolverListAdapter.getUserHandle()) && !mCrossProfileIntentsChecker .hasCrossProfileIntents(resolverListAdapter.getIntents(), - mUserIdProvider.getMyUserId(), + mTabOwnerUserHandleForLaunch.getIdentifier(), resolverListAdapter.getUserHandle().getIdentifier()); if (!shouldShowBlocker) { diff --git a/java/src/com/android/intentresolver/ResolverActivity.java b/java/src/com/android/intentresolver/ResolverActivity.java index d224299e..57871532 100644 --- a/java/src/com/android/intentresolver/ResolverActivity.java +++ b/java/src/com/android/intentresolver/ResolverActivity.java @@ -33,6 +33,8 @@ import static android.stats.devicepolicy.nano.DevicePolicyEnums.RESOLVER_EMPTY_S import static android.stats.devicepolicy.nano.DevicePolicyEnums.RESOLVER_EMPTY_STATE_NO_SHARING_TO_WORK; import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS; +import static com.android.internal.annotations.VisibleForTesting.Visibility.PROTECTED; + import android.annotation.Nullable; import android.annotation.StringRes; import android.annotation.UiThread; @@ -58,7 +60,6 @@ import android.content.pm.UserInfo; import android.content.res.Configuration; import android.content.res.TypedArray; import android.graphics.Insets; -import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Build; import android.os.Bundle; @@ -106,6 +107,9 @@ import com.android.intentresolver.AbstractMultiProfilePagerAdapter.Profile; import com.android.intentresolver.NoCrossProfileEmptyStateProvider.DevicePolicyBlockerEmptyState; import com.android.intentresolver.chooser.DisplayResolveInfo; import com.android.intentresolver.chooser.TargetInfo; +import com.android.intentresolver.icons.DefaultTargetDataLoader; +import com.android.intentresolver.icons.TargetDataLoader; +import com.android.intentresolver.model.ResolverRankerServiceResolverComparator; import com.android.intentresolver.widget.ResolverDrawerLayout; import com.android.internal.annotations.VisibleForTesting; import com.android.internal.content.PackageMonitor; @@ -122,9 +126,10 @@ import java.util.Set; import java.util.function.Supplier; /** - * This activity is displayed when the system attempts to start an Intent for - * which there is more than one matching activity, allowing the user to decide - * which to go to. It is not normally used directly by application developers. + * This is a copy of ResolverActivity to support IntentResolver's ChooserActivity. This code is + * *not* the resolver that is actually triggered by the system right now (you want + * frameworks/base/core/java/com/android/internal/app/ResolverActivity.java for that), the full + * migration is not complete. */ @UiThread public class ResolverActivity extends FragmentActivity implements @@ -225,7 +230,7 @@ public class ResolverActivity extends FragmentActivity implements // new component whose lifecycle is limited to the "created" Activity (so that we can just hold // the annotations as a `final` ivar, which is a better way to show immutability). private Supplier<AnnotatedUserHandles> mLazyAnnotatedUserHandles = () -> { - final AnnotatedUserHandles result = new AnnotatedUserHandles(this); + final AnnotatedUserHandles result = AnnotatedUserHandles.forShareActivity(this); mLazyAnnotatedUserHandles = () -> result; return result; }; @@ -237,47 +242,43 @@ public class ResolverActivity extends FragmentActivity implements private enum ActionTitle { VIEW(Intent.ACTION_VIEW, - com.android.internal.R.string.whichViewApplication, - com.android.internal.R.string.whichViewApplicationNamed, - com.android.internal.R.string.whichViewApplicationLabel), + R.string.whichViewApplication, + R.string.whichViewApplicationNamed, + R.string.whichViewApplicationLabel), EDIT(Intent.ACTION_EDIT, - com.android.internal.R.string.whichEditApplication, - com.android.internal.R.string.whichEditApplicationNamed, - com.android.internal.R.string.whichEditApplicationLabel), + R.string.whichEditApplication, + R.string.whichEditApplicationNamed, + R.string.whichEditApplicationLabel), SEND(Intent.ACTION_SEND, - com.android.internal.R.string.whichSendApplication, - com.android.internal.R.string.whichSendApplicationNamed, - com.android.internal.R.string.whichSendApplicationLabel), + R.string.whichSendApplication, + R.string.whichSendApplicationNamed, + R.string.whichSendApplicationLabel), SENDTO(Intent.ACTION_SENDTO, - com.android.internal.R.string.whichSendToApplication, - com.android.internal.R.string.whichSendToApplicationNamed, - com.android.internal.R.string.whichSendToApplicationLabel), + R.string.whichSendToApplication, + R.string.whichSendToApplicationNamed, + R.string.whichSendToApplicationLabel), SEND_MULTIPLE(Intent.ACTION_SEND_MULTIPLE, - com.android.internal.R.string.whichSendApplication, - com.android.internal.R.string.whichSendApplicationNamed, - com.android.internal.R.string.whichSendApplicationLabel), + R.string.whichSendApplication, + R.string.whichSendApplicationNamed, + R.string.whichSendApplicationLabel), CAPTURE_IMAGE(MediaStore.ACTION_IMAGE_CAPTURE, - com.android.internal.R.string.whichImageCaptureApplication, - com.android.internal.R.string.whichImageCaptureApplicationNamed, - com.android.internal.R.string.whichImageCaptureApplicationLabel), + R.string.whichImageCaptureApplication, + R.string.whichImageCaptureApplicationNamed, + R.string.whichImageCaptureApplicationLabel), DEFAULT(null, - com.android.internal.R.string.whichApplication, - com.android.internal.R.string.whichApplicationNamed, - com.android.internal.R.string.whichApplicationLabel), + R.string.whichApplication, + R.string.whichApplicationNamed, + R.string.whichApplicationLabel), HOME(Intent.ACTION_MAIN, - com.android.internal.R.string.whichHomeApplication, - com.android.internal.R.string.whichHomeApplicationNamed, - com.android.internal.R.string.whichHomeApplicationLabel); + R.string.whichHomeApplication, + R.string.whichHomeApplicationNamed, + R.string.whichHomeApplicationLabel); // titles for layout that deals with http(s) intents - public static final int BROWSABLE_TITLE_RES = - com.android.internal.R.string.whichOpenLinksWith; - public static final int BROWSABLE_HOST_TITLE_RES = - com.android.internal.R.string.whichOpenHostLinksWith; - public static final int BROWSABLE_HOST_APP_TITLE_RES = - com.android.internal.R.string.whichOpenHostLinksWithApp; - public static final int BROWSABLE_APP_TITLE_RES = - com.android.internal.R.string.whichOpenLinksWithApp; + public static final int BROWSABLE_TITLE_RES = R.string.whichOpenLinksWith; + public static final int BROWSABLE_HOST_TITLE_RES = R.string.whichOpenHostLinksWith; + public static final int BROWSABLE_HOST_APP_TITLE_RES = R.string.whichOpenHostLinksWithApp; + public static final int BROWSABLE_APP_TITLE_RES = R.string.whichOpenLinksWithApp; public final String action; public final int titleRes; @@ -333,7 +334,7 @@ public class ResolverActivity extends FragmentActivity implements setSafeForwardingMode(true); - onCreate(savedInstanceState, intent, null, 0, null, null, true); + onCreate(savedInstanceState, intent, null, 0, null, null, true, createIconLoader()); } /** @@ -343,13 +344,26 @@ public class ResolverActivity extends FragmentActivity implements protected void onCreate(Bundle savedInstanceState, Intent intent, CharSequence title, Intent[] initialIntents, List<ResolveInfo> rList, boolean supportsAlwaysUseOption) { - onCreate(savedInstanceState, intent, title, 0, initialIntents, rList, - supportsAlwaysUseOption); + onCreate( + savedInstanceState, + intent, + title, + 0, + initialIntents, + rList, + supportsAlwaysUseOption, + createIconLoader()); } - protected void onCreate(Bundle savedInstanceState, Intent intent, - CharSequence title, int defaultTitleRes, Intent[] initialIntents, - List<ResolveInfo> rList, boolean supportsAlwaysUseOption) { + protected void onCreate( + Bundle savedInstanceState, + Intent intent, + CharSequence title, + int defaultTitleRes, + Intent[] initialIntents, + List<ResolveInfo> rList, + boolean supportsAlwaysUseOption, + TargetDataLoader targetDataLoader) { setTheme(appliedThemeResId()); super.onCreate(savedInstanceState); @@ -379,10 +393,14 @@ public class ResolverActivity extends FragmentActivity implements // turn this off when running under voice interaction, since it results in // a more complicated UI that the current voice interaction flow is not able // to handle. We also turn it off when the work tab is shown to simplify the UX. + // We also turn it off when clonedProfile is present on the device, because we might have + // different "last chosen" activities in the different profiles, and PackageManager doesn't + // provide any more information to help us select between them. boolean filterLastUsed = mSupportsAlwaysUseOption && !isVoiceInteraction() - && !shouldShowTabs(); - mMultiProfilePagerAdapter = createMultiProfilePagerAdapter(initialIntents, rList, filterLastUsed); - if (configureContentView()) { + && !shouldShowTabs() && !hasCloneProfile(); + mMultiProfilePagerAdapter = createMultiProfilePagerAdapter( + initialIntents, rList, filterLastUsed, targetDataLoader); + if (configureContentView(targetDataLoader)) { return; } @@ -438,15 +456,16 @@ public class ResolverActivity extends FragmentActivity implements protected AbstractMultiProfilePagerAdapter createMultiProfilePagerAdapter( Intent[] initialIntents, List<ResolveInfo> rList, - boolean filterLastUsed) { + boolean filterLastUsed, + TargetDataLoader targetDataLoader) { AbstractMultiProfilePagerAdapter resolverMultiProfilePagerAdapter = null; if (shouldShowTabs()) { resolverMultiProfilePagerAdapter = createResolverMultiProfilePagerAdapterForTwoProfiles( - initialIntents, rList, filterLastUsed); + initialIntents, rList, filterLastUsed, targetDataLoader); } else { resolverMultiProfilePagerAdapter = createResolverMultiProfilePagerAdapterForOneProfile( - initialIntents, rList, filterLastUsed); + initialIntents, rList, filterLastUsed, targetDataLoader); } return resolverMultiProfilePagerAdapter; } @@ -484,7 +503,7 @@ public class ResolverActivity extends FragmentActivity implements return new NoCrossProfileEmptyStateProvider(getPersonalProfileUserHandle(), noWorkToPersonalEmptyState, noPersonalToWorkEmptyState, - createCrossProfileIntentsChecker(), createMyUserIdProvider()); + createCrossProfileIntentsChecker(), getTabOwnerUserHandleForLaunch()); } protected int appliedThemeResId() { @@ -861,13 +880,24 @@ public class ResolverActivity extends FragmentActivity implements // the future if resolver *were* to make any (non-overridden) calls to a version that used a // different signature (and thus didn't return the subclass type). @VisibleForTesting - protected ResolverListController createListController(UserHandle unused) { + protected ResolverListController createListController(UserHandle userHandle) { + ResolverRankerServiceResolverComparator resolverComparator = + new ResolverRankerServiceResolverComparator( + this, + getTargetIntent(), + getReferrerPackageName(), + null, + null, + getResolverRankerServiceUserHandleList(userHandle), + null); return new ResolverListController( this, mPm, getTargetIntent(), getReferrerPackageName(), - getAnnotatedUserHandles().userIdOfCallingApp); + getAnnotatedUserHandles().userIdOfCallingApp, + resolverComparator, + getQueryIntentsUser(userHandle)); } /** @@ -990,52 +1020,36 @@ public class ResolverActivity extends FragmentActivity implements return new CrossProfileIntentsChecker(getContentResolver()); } - // @NonFinalForTesting - @VisibleForTesting protected WorkProfileAvailabilityManager createWorkProfileAvailabilityManager() { final UserHandle workUser = getWorkProfileUserHandle(); return new WorkProfileAvailabilityManager( getSystemService(UserManager.class), workUser, - () -> { - if (mMultiProfilePagerAdapter.getCurrentUserHandle().equals(workUser)) { - mMultiProfilePagerAdapter.rebuildActiveTab(true); - } else { - mMultiProfilePagerAdapter.clearInactiveProfileCache(); - } - }); - } - - // TODO: have tests override `getAnnotatedUserHandles()`, and make this method `final`. - // @NonFinalForTesting - @Nullable - protected UserHandle getWorkProfileUserHandle() { - return getAnnotatedUserHandles().workProfileUserHandle; + this::onWorkProfileStatusUpdated); } - // @NonFinalForTesting - @VisibleForTesting - public void safelyStartActivity(TargetInfo cti) { - // We're dispatching intents that might be coming from legacy apps, so - // don't kill ourselves. - StrictMode.disableDeathOnFileUriExposure(); - try { - UserHandle currentUserHandle = mMultiProfilePagerAdapter.getCurrentUserHandle(); - safelyStartActivityInternal(cti, currentUserHandle, null); - } finally { - StrictMode.enableDeathOnFileUriExposure(); + protected void onWorkProfileStatusUpdated() { + if (mMultiProfilePagerAdapter.getCurrentUserHandle().equals(getWorkProfileUserHandle())) { + mMultiProfilePagerAdapter.rebuildActiveTab(true); + } else { + mMultiProfilePagerAdapter.clearInactiveProfileCache(); } } // @NonFinalForTesting @VisibleForTesting - protected ResolverListAdapter createResolverListAdapter(Context context, - List<Intent> payloadIntents, Intent[] initialIntents, List<ResolveInfo> rList, - boolean filterLastUsed, UserHandle userHandle) { - Intent startIntent = getIntent(); - boolean isAudioCaptureDevice = - startIntent.getBooleanExtra(EXTRA_IS_AUDIO_CAPTURE_DEVICE, false); + protected ResolverListAdapter createResolverListAdapter( + Context context, + List<Intent> payloadIntents, + Intent[] initialIntents, + List<ResolveInfo> rList, + boolean filterLastUsed, + UserHandle userHandle, + TargetDataLoader targetDataLoader) { + UserHandle initialIntentsUserSpace = isLaunchedAsCloneProfile() + && userHandle.equals(getPersonalProfileUserHandle()) + ? getCloneProfileUserHandle() : userHandle; return new ResolverListAdapter( context, payloadIntents, @@ -1046,7 +1060,15 @@ public class ResolverActivity extends FragmentActivity implements userHandle, getTargetIntent(), this, - isAudioCaptureDevice); + initialIntentsUserSpace, + targetDataLoader); + } + + private TargetDataLoader createIconLoader() { + Intent startIntent = getIntent(); + boolean isAudioCaptureDevice = + startIntent.getBooleanExtra(EXTRA_IS_AUDIO_CAPTURE_DEVICE, false); + return new DefaultTargetDataLoader(this, getLifecycle(), isAudioCaptureDevice); } private LatencyTracker getLatencyTracker() { @@ -1085,7 +1107,7 @@ public class ResolverActivity extends FragmentActivity implements workProfileUserHandle, getPersonalProfileUserHandle(), getMetricsCategory(), - createMyUserIdProvider() + getTabOwnerUserHandleForLaunch() ); // Return composite provider, the order matters (the higher, the more priority) @@ -1121,38 +1143,42 @@ public class ResolverActivity extends FragmentActivity implements createResolverMultiProfilePagerAdapterForOneProfile( Intent[] initialIntents, List<ResolveInfo> rList, - boolean filterLastUsed) { + boolean filterLastUsed, + TargetDataLoader targetDataLoader) { ResolverListAdapter adapter = createResolverListAdapter( /* context */ this, /* payloadIntents */ mIntents, initialIntents, rList, filterLastUsed, - /* userHandle */ UserHandle.of(UserHandle.myUserId())); + /* userHandle */ getPersonalProfileUserHandle(), + targetDataLoader); return new ResolverMultiProfilePagerAdapter( /* context */ this, adapter, createEmptyStateProvider(/* workProfileUserHandle= */ null), /* workProfileQuietModeChecker= */ () -> false, - /* workProfileUserHandle= */ null); + /* workProfileUserHandle= */ null, + getCloneProfileUserHandle()); } private UserHandle getIntentUser() { return getIntent().hasExtra(EXTRA_CALLING_USER) ? getIntent().getParcelableExtra(EXTRA_CALLING_USER) - : getUser(); + : getTabOwnerUserHandleForLaunch(); } private ResolverMultiProfilePagerAdapter createResolverMultiProfilePagerAdapterForTwoProfiles( Intent[] initialIntents, List<ResolveInfo> rList, - boolean filterLastUsed) { + boolean filterLastUsed, + TargetDataLoader targetDataLoader) { // In the edge case when we have 0 apps in the current profile and >1 apps in the other, // the intent resolver is started in the other profile. Since this is the only case when // this happens, we check for it here and set the current profile's tab. int selectedProfile = getCurrentProfile(); UserHandle intentUser = getIntentUser(); - if (!getUser().equals(intentUser)) { + if (!getTabOwnerUserHandleForLaunch().equals(intentUser)) { if (getPersonalProfileUserHandle().equals(intentUser)) { selectedProfile = PROFILE_PERSONAL; } else if (getWorkProfileUserHandle().equals(intentUser)) { @@ -1174,7 +1200,8 @@ public class ResolverActivity extends FragmentActivity implements rList, (filterLastUsed && UserHandle.myUserId() == getPersonalProfileUserHandle().getIdentifier()), - /* userHandle */ getPersonalProfileUserHandle()); + /* userHandle */ getPersonalProfileUserHandle(), + targetDataLoader); UserHandle workProfileUserHandle = getWorkProfileUserHandle(); ResolverListAdapter workAdapter = createResolverListAdapter( /* context */ this, @@ -1183,7 +1210,8 @@ public class ResolverActivity extends FragmentActivity implements rList, (filterLastUsed && UserHandle.myUserId() == workProfileUserHandle.getIdentifier()), - /* userHandle */ workProfileUserHandle); + /* userHandle */ workProfileUserHandle, + targetDataLoader); return new ResolverMultiProfilePagerAdapter( /* context */ this, personalAdapter, @@ -1191,7 +1219,8 @@ public class ResolverActivity extends FragmentActivity implements createEmptyStateProvider(getWorkProfileUserHandle()), () -> mWorkProfileAvailability.isQuietModeEnabled(), selectedProfile, - getWorkProfileUserHandle()); + getWorkProfileUserHandle(), + getCloneProfileUserHandle()); } /** @@ -1214,7 +1243,8 @@ public class ResolverActivity extends FragmentActivity implements } protected final @Profile int getCurrentProfile() { - return (UserHandle.myUserId() == UserHandle.USER_SYSTEM ? PROFILE_PERSONAL : PROFILE_WORK); + return (getTabOwnerUserHandleForLaunch().equals(getPersonalProfileUserHandle()) + ? PROFILE_PERSONAL : PROFILE_WORK); } protected final AnnotatedUserHandles getAnnotatedUserHandles() { @@ -1225,10 +1255,43 @@ public class ResolverActivity extends FragmentActivity implements return getAnnotatedUserHandles().personalProfileUserHandle; } + // TODO: have tests override `getAnnotatedUserHandles()`, and make this method `final`. + // @NonFinalForTesting + @Nullable + protected UserHandle getWorkProfileUserHandle() { + return getAnnotatedUserHandles().workProfileUserHandle; + } + + // TODO: have tests override `getAnnotatedUserHandles()`, and make this method `final`. + @Nullable + protected UserHandle getCloneProfileUserHandle() { + return getAnnotatedUserHandles().cloneProfileUserHandle; + } + + // TODO: have tests override `getAnnotatedUserHandles()`, and make this method `final`. + protected UserHandle getTabOwnerUserHandleForLaunch() { + return getAnnotatedUserHandles().tabOwnerUserHandleForLaunch; + } + + protected UserHandle getUserHandleSharesheetLaunchedAs() { + return getAnnotatedUserHandles().userHandleSharesheetLaunchedAs; + } + + private boolean hasWorkProfile() { return getWorkProfileUserHandle() != null; } + private boolean hasCloneProfile() { + return getCloneProfileUserHandle() != null; + } + + protected final boolean isLaunchedAsCloneProfile() { + return hasCloneProfile() + && getUserHandleSharesheetLaunchedAs().equals(getCloneProfileUserHandle()); + } + + protected final boolean shouldShowTabs() { return hasWorkProfile(); } @@ -1361,13 +1424,13 @@ public class ResolverActivity extends FragmentActivity implements private String getForwardToPersonalMsg() { return getSystemService(DevicePolicyManager.class).getResources().getString( FORWARD_INTENT_TO_PERSONAL, - () -> getString(com.android.internal.R.string.forward_intent_to_owner)); + () -> getString(R.string.forward_intent_to_owner)); } private String getForwardToWorkMsg() { return getSystemService(DevicePolicyManager.class).getResources().getString( FORWARD_INTENT_TO_WORK, - () -> getString(com.android.internal.R.string.forward_intent_to_work)); + () -> getString(R.string.forward_intent_to_work)); } /** @@ -1502,6 +1565,13 @@ public class ResolverActivity extends FragmentActivity implements mAlwaysButton.setEnabled(false); return; } + // In case of clonedProfile being active, we do not allow the 'Always' option in the + // disambiguation dialog of Personal Profile as the package manager cannot distinguish + // between cross-profile preferred activities. + if (hasCloneProfile() && (mMultiProfilePagerAdapter.getCurrentPage() == PROFILE_PERSONAL)) { + mAlwaysButton.setEnabled(false); + return; + } boolean enabled = false; ResolveInfo ri = null; if (hasValidSelection) { @@ -1544,7 +1614,7 @@ public class ResolverActivity extends FragmentActivity implements return getSystemService(DevicePolicyManager.class).getResources().getString( RESOLVER_WORK_PROFILE_NOT_SUPPORTED, () -> getString( - com.android.internal.R.string.activity_resolver_work_profiles_support, + R.string.activity_resolver_work_profiles_support, launcherName), launcherName); } @@ -1576,6 +1646,16 @@ public class ResolverActivity extends FragmentActivity implements } } + /** Start the activity specified by the {@link TargetInfo}.*/ + public final void safelyStartActivity(TargetInfo cti) { + // In case cloned apps are present, we would want to start those apps in cloned user + // space, which will not be same as adaptor's userHandle. resolveInfo.userHandle + // identifies the correct user space in such cases. + UserHandle activityUserHandle = getResolveInfoUserHandle( + cti.getResolveInfo(), mMultiProfilePagerAdapter.getCurrentUserHandle()); + safelyStartActivityAsUser(cti, activityUserHandle, null); + } + /** * Start activity as a fixed user handle. * @param cti TargetInfo to be launched. @@ -1598,7 +1678,8 @@ public class ResolverActivity extends FragmentActivity implements } } - private void safelyStartActivityInternal( + @VisibleForTesting + protected void safelyStartActivityInternal( TargetInfo cti, UserHandle user, @Nullable Bundle options) { // If the target is suspended, the activity will not be successfully launched. // Do not unregister from package manager updates in this case @@ -1647,7 +1728,7 @@ public class ResolverActivity extends FragmentActivity implements * Sets up the content view. * @return <code>true</code> if the activity is finishing and creation should halt. */ - private boolean configureContentView() { + private boolean configureContentView(TargetDataLoader targetDataLoader) { if (mMultiProfilePagerAdapter.getActiveListAdapter() == null) { throw new IllegalStateException("mMultiProfilePagerAdapter.getCurrentListAdapter() " + "cannot be null."); @@ -1664,7 +1745,7 @@ public class ResolverActivity extends FragmentActivity implements } if (shouldUseMiniResolver()) { - configureMiniResolverContent(); + configureMiniResolverContent(targetDataLoader); Trace.endSection(); return false; } @@ -1687,7 +1768,7 @@ public class ResolverActivity extends FragmentActivity implements * and asks the user if they'd like to open that cross-profile app or use the in-profile * browser. */ - private void configureMiniResolverContent() { + private void configureMiniResolverContent(TargetDataLoader targetDataLoader) { mLayoutId = R.layout.miniresolver; setContentView(mLayoutId); @@ -1702,15 +1783,15 @@ public class ResolverActivity extends FragmentActivity implements // Load the icon asynchronously ImageView icon = findViewById(com.android.internal.R.id.icon); - inactiveAdapter.new LoadIconTask(otherProfileResolveInfo) { - @Override - protected void onPostExecute(Drawable drawable) { - if (!isDestroyed()) { - otherProfileResolveInfo.getDisplayIconHolder().setDisplayIcon(drawable); - new ResolverListAdapter.ViewHolder(icon).bindIcon(otherProfileResolveInfo); - } - } - }.execute(); + targetDataLoader.loadAppTargetIcon( + otherProfileResolveInfo, + inactiveAdapter.getUserHandle(), + (drawable) -> { + if (!isDestroyed()) { + otherProfileResolveInfo.getDisplayIconHolder().setDisplayIcon(drawable); + new ResolverListAdapter.ViewHolder(icon).bindIcon(otherProfileResolveInfo); + } + }); ((TextView) findViewById(com.android.internal.R.id.open_cross_profile)).setText( getResources().getString( @@ -1814,8 +1895,10 @@ public class ResolverActivity extends FragmentActivity implements } else if (numberOfProfiles == 2 && mMultiProfilePagerAdapter.getActiveListAdapter().isTabLoaded() && mMultiProfilePagerAdapter.getInactiveListAdapter().isTabLoaded() - && (maybeAutolaunchIfNoAppsOnInactiveTab() - || maybeAutolaunchIfCrossProfileSupported())) { + && maybeAutolaunchIfCrossProfileSupported()) { + // TODO(b/280988288): If the ChooserActivity is shown we should consider showing the + // correct intent-picker UIs (e.g., mini-resolver) if it was launched without + // ACTION_SEND. return true; } return false; @@ -1842,23 +1925,6 @@ public class ResolverActivity extends FragmentActivity implements return false; } - private boolean maybeAutolaunchIfNoAppsOnInactiveTab() { - int count = mMultiProfilePagerAdapter.getActiveListAdapter().getUnfilteredCount(); - if (count != 1) { - return false; - } - ResolverListAdapter inactiveListAdapter = - mMultiProfilePagerAdapter.getInactiveListAdapter(); - if (inactiveListAdapter.getUnfilteredCount() != 0) { - return false; - } - TargetInfo target = mMultiProfilePagerAdapter.getActiveListAdapter() - .targetInfoForPosition(0, false); - safelyStartActivity(target); - finish(); - return true; - } - /** * When we have a personal and a work profile, we auto launch in the following scenario: * - There is 1 resolved target on each profile @@ -2176,16 +2242,10 @@ public class ResolverActivity extends FragmentActivity implements public final boolean useLayoutWithDefault() { // We only use the default app layout when the profile of the active user has a // filtered item. We always show the same default app even in the inactive user profile. - boolean currentUserAdapterHasFilteredItem; - if (mMultiProfilePagerAdapter.getCurrentUserHandle().getIdentifier() - == UserHandle.myUserId()) { - currentUserAdapterHasFilteredItem = - mMultiProfilePagerAdapter.getActiveListAdapter().hasFilteredItem(); - } else { - currentUserAdapterHasFilteredItem = - mMultiProfilePagerAdapter.getInactiveListAdapter().hasFilteredItem(); - } - return mSupportsAlwaysUseOption && currentUserAdapterHasFilteredItem; + boolean adapterForCurrentUserHasFilteredItem = + mMultiProfilePagerAdapter.getListAdapterForUserHandle( + getTabOwnerUserHandleForLaunch()).hasFilteredItem(); + return mSupportsAlwaysUseOption && adapterForCurrentUserHasFilteredItem; } /** @@ -2204,7 +2264,14 @@ public class ResolverActivity extends FragmentActivity implements return lhs == null ? rhs == null : lhs.activityInfo == null ? rhs.activityInfo == null : Objects.equals(lhs.activityInfo.name, rhs.activityInfo.name) - && Objects.equals(lhs.activityInfo.packageName, rhs.activityInfo.packageName); + && Objects.equals(lhs.activityInfo.packageName, rhs.activityInfo.packageName) + // Comparing against resolveInfo.userHandle in case cloned apps are present, + // as they will have the same activityInfo. + && Objects.equals( + getResolveInfoUserHandle(lhs, + mMultiProfilePagerAdapter.getActiveListAdapter().getUserHandle()), + getResolveInfoUserHandle(rhs, + mMultiProfilePagerAdapter.getActiveListAdapter().getUserHandle())); } private boolean inactiveListAdapterHasItems() { @@ -2311,4 +2378,44 @@ public class ResolverActivity extends FragmentActivity implements } } } + /** + * Returns the {@link UserHandle} to use when querying resolutions for intents in a + * {@link ResolverListController} configured for the provided {@code userHandle}. + */ + protected final UserHandle getQueryIntentsUser(UserHandle userHandle) { + return mLazyAnnotatedUserHandles.get().getQueryIntentsUser(userHandle); + } + + /** + * Returns the {@link List} of {@link UserHandle} to pass on to the + * {@link ResolverRankerServiceResolverComparator} as per the provided {@code userHandle}. + */ + @VisibleForTesting(visibility = PROTECTED) + public final List<UserHandle> getResolverRankerServiceUserHandleList(UserHandle userHandle) { + return getResolverRankerServiceUserHandleListInternal(userHandle); + } + + @VisibleForTesting + protected List<UserHandle> getResolverRankerServiceUserHandleListInternal( + UserHandle userHandle) { + List<UserHandle> userList = new ArrayList<>(); + userList.add(userHandle); + // Add clonedProfileUserHandle to the list only if we are: + // a. Building the Personal Tab. + // b. CloneProfile exists on the device. + if (userHandle.equals(getPersonalProfileUserHandle()) + && getCloneProfileUserHandle() != null) { + userList.add(getCloneProfileUserHandle()); + } + return userList; + } + + /** + * This function is temporary in nature, and its usages will be replaced with just + * resolveInfo.userHandle, once it is available, once sharesheet is stable. + */ + public static UserHandle getResolveInfoUserHandle(ResolveInfo resolveInfo, + UserHandle predictedHandle) { + return resolveInfo.userHandle; + } } diff --git a/java/src/com/android/intentresolver/ResolverListAdapter.java b/java/src/com/android/intentresolver/ResolverListAdapter.java index eac275cc..282a672f 100644 --- a/java/src/com/android/intentresolver/ResolverListAdapter.java +++ b/java/src/com/android/intentresolver/ResolverListAdapter.java @@ -16,16 +16,10 @@ package com.android.intentresolver; -import static android.content.Context.ACTIVITY_SERVICE; - -import android.animation.ObjectAnimator; import android.annotation.NonNull; import android.annotation.Nullable; -import android.app.ActivityManager; -import android.content.ComponentName; import android.content.Context; import android.content.Intent; -import android.content.PermissionChecker; import android.content.pm.ActivityInfo; import android.content.pm.LabeledIntent; import android.content.pm.PackageManager; @@ -43,7 +37,6 @@ import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.view.animation.DecelerateInterpolator; import android.widget.AbsListView; import android.widget.BaseAdapter; import android.widget.ImageView; @@ -51,15 +44,15 @@ import android.widget.TextView; import com.android.intentresolver.chooser.DisplayResolveInfo; import com.android.intentresolver.chooser.TargetInfo; +import com.android.intentresolver.icons.TargetDataLoader; import com.android.internal.annotations.VisibleForTesting; import com.google.common.collect.ImmutableList; import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; +import java.util.HashSet; import java.util.List; -import java.util.Map; +import java.util.Set; public class ResolverListAdapter extends BaseAdapter { private static final String TAG = "ResolverListAdapter"; @@ -71,32 +64,32 @@ public class ResolverListAdapter extends BaseAdapter { protected final LayoutInflater mInflater; protected final ResolverListCommunicator mResolverListCommunicator; protected final ResolverListController mResolverListController; - protected final TargetPresentationGetter.Factory mPresentationFactory; private final List<Intent> mIntents; private final Intent[] mInitialIntents; private final List<ResolveInfo> mBaseResolveList; private final PackageManager mPm; - private final int mIconDpi; - private final boolean mIsAudioCaptureDevice; + private final TargetDataLoader mTargetDataLoader; private final UserHandle mUserHandle; private final Intent mTargetIntent; - private final Map<DisplayResolveInfo, LoadIconTask> mIconLoaders = new HashMap<>(); - private final Map<DisplayResolveInfo, LoadLabelTask> mLabelLoaders = new HashMap<>(); + private final Set<DisplayResolveInfo> mRequestedIcons = new HashSet<>(); + private final Set<DisplayResolveInfo> mRequestedLabels = new HashSet<>(); private ResolveInfo mLastChosen; private DisplayResolveInfo mOtherProfile; private int mPlaceholderCount; // This one is the list that the Adapter will actually present. - private List<DisplayResolveInfo> mDisplayList; + private final List<DisplayResolveInfo> mDisplayList; private List<ResolvedComponentInfo> mUnfilteredResolveList; private int mLastChosenPosition = -1; - private boolean mFilterLastUsed; + private final boolean mFilterLastUsed; private Runnable mPostListReadyRunnable; private boolean mIsTabLoaded; + // Represents the UserSpace in which the Initial Intents should be resolved. + private final UserHandle mInitialIntentsUserSpace; public ResolverListAdapter( Context context, @@ -108,23 +101,22 @@ public class ResolverListAdapter extends BaseAdapter { UserHandle userHandle, Intent targetIntent, ResolverListCommunicator resolverListCommunicator, - boolean isAudioCaptureDevice) { + UserHandle initialIntentsUserSpace, + TargetDataLoader targetDataLoader) { mContext = context; mIntents = payloadIntents; mInitialIntents = initialIntents; mBaseResolveList = rList; mInflater = LayoutInflater.from(context); mPm = context.getPackageManager(); + mTargetDataLoader = targetDataLoader; mDisplayList = new ArrayList<>(); mFilterLastUsed = filterLastUsed; mResolverListController = resolverListController; mUserHandle = userHandle; mTargetIntent = targetIntent; mResolverListCommunicator = resolverListCommunicator; - mIsAudioCaptureDevice = isAudioCaptureDevice; - final ActivityManager am = (ActivityManager) mContext.getSystemService(ACTIVITY_SERVICE); - mIconDpi = am.getLauncherLargeIconDensity(); - mPresentationFactory = new TargetPresentationGetter.Factory(mContext, mIconDpi); + mInitialIntentsUserSpace = initialIntentsUserSpace; } public final DisplayResolveInfo getFirstDisplayResolveInfo() { @@ -176,19 +168,25 @@ public class ResolverListAdapter extends BaseAdapter { } /** - * Returns the app share score of the given {@code componentName}. + * Returns the app share score of the given {@code targetInfo}. */ - public float getScore(ComponentName componentName) { - return mResolverListController.getScore(componentName); + public float getScore(TargetInfo targetInfo) { + return mResolverListController.getScore(targetInfo); } - public void updateModel(ComponentName componentName) { - mResolverListController.updateModel(componentName); + /** + * Updates the model about the chosen {@code targetInfo}. + */ + public void updateModel(TargetInfo targetInfo) { + mResolverListController.updateModel(targetInfo); } - public void updateChooserCounts(String packageName, String action) { + /** + * Updates the model about Chooser Activity selection. + */ + public void updateChooserCounts(String packageName, String action, UserHandle userHandle) { mResolverListController.updateChooserCounts( - packageName, getUserHandle().getIdentifier(), action); + packageName, userHandle, action); } List<ResolvedComponentInfo> getUnfilteredResolveList() { @@ -356,12 +354,11 @@ public class ResolverListAdapter extends BaseAdapter { if (otherProfileInfo != null) { mOtherProfile = makeOtherProfileDisplayResolveInfo( - mContext, otherProfileInfo, mPm, mTargetIntent, mResolverListCommunicator, - mIconDpi); + mTargetDataLoader); } else { mOtherProfile = null; try { @@ -468,13 +465,14 @@ public class ResolverListAdapter extends BaseAdapter { ri.icon = 0; } + ri.userHandle = mInitialIntentsUserSpace; addResolveInfo(DisplayResolveInfo.newDisplayResolveInfo( ii, ri, ri.loadLabel(mPm), null, ii, - mPresentationFactory.makePresentationGetter(ri))); + mTargetDataLoader.createPresentationGetter(ri))); } } @@ -527,7 +525,7 @@ public class ResolverListAdapter extends BaseAdapter { intent, add, (replaceIntent != null) ? replaceIntent : defaultIntent, - mPresentationFactory.makePresentationGetter(add)); + mTargetDataLoader.createPresentationGetter(add)); dri.setPinned(rci.isPinned()); if (rci.isPinned()) { Log.i(TAG, "Pinned item: " + rci.name); @@ -673,7 +671,7 @@ public class ResolverListAdapter extends BaseAdapter { final ViewHolder holder = (ViewHolder) view.getTag(); if (info == null) { holder.icon.setImageDrawable(loadIconPlaceholder()); - holder.bindLabel("", "", false); + holder.bindLabel("", ""); return; } @@ -682,10 +680,9 @@ public class ResolverListAdapter extends BaseAdapter { if (dri.hasDisplayLabel()) { holder.bindLabel( dri.getDisplayLabel(), - dri.getExtendedInfo(), - alwaysShowSubLabel()); + dri.getExtendedInfo()); } else { - holder.bindLabel("", "", false); + holder.bindLabel("", ""); loadLabel(dri); } holder.bindIcon(info); @@ -696,25 +693,37 @@ public class ResolverListAdapter extends BaseAdapter { } protected final void loadIcon(DisplayResolveInfo info) { - LoadIconTask task = mIconLoaders.get(info); - if (task == null) { - task = new LoadIconTask(info); - mIconLoaders.put(info, task); - task.execute(); + if (mRequestedIcons.add(info)) { + mTargetDataLoader.loadAppTargetIcon( + info, + getUserHandle(), + (drawable) -> onIconLoaded(info, drawable)); + } + } + + private void onIconLoaded(DisplayResolveInfo displayResolveInfo, Drawable drawable) { + if (getOtherProfile() == displayResolveInfo) { + mResolverListCommunicator.updateProfileViewButton(); + } else if (!displayResolveInfo.hasDisplayIcon()) { + displayResolveInfo.getDisplayIconHolder().setDisplayIcon(drawable); + notifyDataSetChanged(); } } private void loadLabel(DisplayResolveInfo info) { - LoadLabelTask task = mLabelLoaders.get(info); - if (task == null) { - task = createLoadLabelTask(info); - mLabelLoaders.put(info, task); - task.execute(); + if (mRequestedLabels.add(info)) { + mTargetDataLoader.loadLabel(info, (result) -> onLabelLoaded(info, result)); } } - protected LoadLabelTask createLoadLabelTask(DisplayResolveInfo info) { - return new LoadLabelTask(info); + protected final void onLabelLoaded( + DisplayResolveInfo displayResolveInfo, CharSequence[] result) { + if (displayResolveInfo.hasDisplayLabel()) { + return; + } + displayResolveInfo.setDisplayLabel(result[0]); + displayResolveInfo.setExtendedInfo(result[1]); + notifyDataSetChanged(); } public void onDestroy() { @@ -725,16 +734,8 @@ public class ResolverListAdapter extends BaseAdapter { if (mResolverListController != null) { mResolverListController.destroy(); } - cancelTasks(mIconLoaders.values()); - cancelTasks(mLabelLoaders.values()); - mIconLoaders.clear(); - mLabelLoaders.clear(); - } - - private <T extends AsyncTask> void cancelTasks(Collection<T> tasks) { - for (T task: tasks) { - task.cancel(false); - } + mRequestedIcons.clear(); + mRequestedLabels.clear(); } private static ColorMatrixColorFilter getSuspendedColorMatrix() { @@ -760,37 +761,15 @@ public class ResolverListAdapter extends BaseAdapter { return sSuspendedMatrixColorFilter; } - Drawable loadIconForResolveInfo(ResolveInfo ri) { - // Load icons based on the current process. If in work profile icons should be badged. - return mPresentationFactory.makePresentationGetter(ri).getIcon(getUserHandle()); - } - protected final Drawable loadIconPlaceholder() { return mContext.getDrawable(R.drawable.resolver_icon_placeholder); } void loadFilteredItemIconTaskAsync(@NonNull ImageView iconView) { final DisplayResolveInfo iconInfo = getFilteredItem(); - if (iconView != null && iconInfo != null) { - new AsyncTask<Void, Void, Drawable>() { - @Override - protected Drawable doInBackground(Void... params) { - Drawable drawable; - try { - drawable = loadIconForResolveInfo(iconInfo.getResolveInfo()); - } catch (Exception e) { - ComponentName componentName = iconInfo.getResolvedComponentName(); - Log.e(TAG, "Failed to load app icon for " + componentName, e); - drawable = loadIconPlaceholder(); - } - return drawable; - } - - @Override - protected void onPostExecute(Drawable d) { - iconView.setImageDrawable(d); - } - }.execute(); + if (iconInfo != null) { + mTargetDataLoader.loadAppTargetIcon( + iconInfo, getUserHandle(), iconView::setImageDrawable); } } @@ -819,10 +798,6 @@ public class ResolverListAdapter extends BaseAdapter { mIsTabLoaded = true; } - protected boolean alwaysShowSubLabel() { - return false; - } - /** * Find the first element in a list of {@code ResolvedComponentInfo} objects whose * {@code ResolveInfo} specifies a {@code targetUserId} other than the current user. @@ -850,12 +825,11 @@ public class ResolverListAdapter extends BaseAdapter { * of an element in the resolve list). */ private static DisplayResolveInfo makeOtherProfileDisplayResolveInfo( - Context context, ResolvedComponentInfo resolvedComponentInfo, PackageManager pm, Intent targetIntent, ResolverListCommunicator resolverListCommunicator, - int iconDpi) { + TargetDataLoader targetDataLoader) { ResolveInfo resolveInfo = resolvedComponentInfo.getResolveInfoAt(0); Intent pOrigIntent = resolverListCommunicator.getReplacementIntent( @@ -865,8 +839,7 @@ public class ResolverListAdapter extends BaseAdapter { resolveInfo.activityInfo, targetIntent); TargetPresentationGetter presentationGetter = - new TargetPresentationGetter.Factory(context, iconDpi) - .makePresentationGetter(resolveInfo); + targetDataLoader.createPresentationGetter(resolveInfo); return DisplayResolveInfo.newDisplayResolveInfo( resolvedComponentInfo.getIntentAt(0), @@ -913,7 +886,6 @@ public class ResolverListAdapter extends BaseAdapter { */ @VisibleForTesting public static class ViewHolder { - private static final long IMAGE_FADE_IN_MILLIS = 150; public View itemView; public Drawable defaultItemViewBackground; @@ -930,17 +902,19 @@ public class ResolverListAdapter extends BaseAdapter { icon = (ImageView) view.findViewById(com.android.internal.R.id.icon); } - public void bindLabel(CharSequence label, CharSequence subLabel, boolean showSubLabel) { + public void bindLabel(CharSequence label, CharSequence subLabel) { text.setText(label); if (TextUtils.equals(label, subLabel)) { subLabel = null; } - text2.setText(subLabel); - if (showSubLabel || subLabel != null) { + if (!TextUtils.isEmpty(subLabel)) { + text.setMaxLines(1); + text2.setText(subLabel); text2.setVisibility(View.VISIBLE); } else { + text.setMaxLines(2); text2.setVisibility(View.GONE); } @@ -951,23 +925,12 @@ public class ResolverListAdapter extends BaseAdapter { itemView.setContentDescription(description); } - public void bindIcon(TargetInfo info) { - bindIcon(info, false); - } - /** - * Bind view holder to a TargetInfo, run icon reveal animation, if required. + * Bind view holder to a TargetInfo. */ - public void bindIcon(TargetInfo info, boolean animate) { + public void bindIcon(TargetInfo info) { Drawable displayIcon = info.getDisplayIconHolder().getDisplayIcon(); - boolean runAnimation = animate && (icon.getDrawable() == null) && (displayIcon != null); icon.setImageDrawable(displayIcon); - if (runAnimation) { - ObjectAnimator animator = ObjectAnimator.ofFloat(icon, "alpha", 0.0f, 1.0f); - animator.setInterpolator(new DecelerateInterpolator(1.0f)); - animator.setDuration(IMAGE_FADE_IN_MILLIS); - animator.start(); - } if (info.isSuspended()) { icon.setColorFilter(getSuspendedColorMatrix()); } else { @@ -975,86 +938,4 @@ public class ResolverListAdapter extends BaseAdapter { } } } - - protected class LoadLabelTask extends AsyncTask<Void, Void, CharSequence[]> { - private final DisplayResolveInfo mDisplayResolveInfo; - - protected LoadLabelTask(DisplayResolveInfo dri) { - mDisplayResolveInfo = dri; - } - - @Override - protected CharSequence[] doInBackground(Void... voids) { - TargetPresentationGetter pg = mPresentationFactory.makePresentationGetter( - mDisplayResolveInfo.getResolveInfo()); - - if (mIsAudioCaptureDevice) { - // This is an audio capture device, so check record permissions - ActivityInfo activityInfo = mDisplayResolveInfo.getResolveInfo().activityInfo; - String packageName = activityInfo.packageName; - - int uid = activityInfo.applicationInfo.uid; - boolean hasRecordPermission = - PermissionChecker.checkPermissionForPreflight( - mContext, - android.Manifest.permission.RECORD_AUDIO, -1, uid, - packageName) - == android.content.pm.PackageManager.PERMISSION_GRANTED; - - if (!hasRecordPermission) { - // Doesn't have record permission, so warn the user - return new CharSequence[] { - pg.getLabel(), - mContext.getString(R.string.usb_device_resolve_prompt_warn) - }; - } - } - - return new CharSequence[] { - pg.getLabel(), - pg.getSubLabel() - }; - } - - @Override - protected void onPostExecute(CharSequence[] result) { - if (mDisplayResolveInfo.hasDisplayLabel()) { - return; - } - mDisplayResolveInfo.setDisplayLabel(result[0]); - mDisplayResolveInfo.setExtendedInfo(result[1]); - notifyDataSetChanged(); - } - } - - class LoadIconTask extends AsyncTask<Void, Void, Drawable> { - protected final DisplayResolveInfo mDisplayResolveInfo; - private final ResolveInfo mResolveInfo; - - LoadIconTask(DisplayResolveInfo dri) { - mDisplayResolveInfo = dri; - mResolveInfo = dri.getResolveInfo(); - } - - @Override - protected Drawable doInBackground(Void... params) { - try { - return loadIconForResolveInfo(mResolveInfo); - } catch (Exception e) { - ComponentName componentName = mDisplayResolveInfo.getResolvedComponentName(); - Log.e(TAG, "Failed to load app icon for " + componentName, e); - return loadIconPlaceholder(); - } - } - - @Override - protected void onPostExecute(Drawable d) { - if (getOtherProfile() == mDisplayResolveInfo) { - mResolverListCommunicator.updateProfileViewButton(); - } else if (!mDisplayResolveInfo.hasDisplayIcon()) { - mDisplayResolveInfo.getDisplayIconHolder().setDisplayIcon(d); - notifyDataSetChanged(); - } - } - } } diff --git a/java/src/com/android/intentresolver/ResolverListController.java b/java/src/com/android/intentresolver/ResolverListController.java index b4544c43..d5a5fedf 100644 --- a/java/src/com/android/intentresolver/ResolverListController.java +++ b/java/src/com/android/intentresolver/ResolverListController.java @@ -32,8 +32,8 @@ import android.os.UserHandle; import android.util.Log; import com.android.intentresolver.chooser.DisplayResolveInfo; +import com.android.intentresolver.chooser.TargetInfo; import com.android.intentresolver.model.AbstractResolverComparator; -import com.android.intentresolver.model.ResolverRankerServiceResolverComparator; import com.android.internal.annotations.VisibleForTesting; import java.util.ArrayList; @@ -58,6 +58,7 @@ public class ResolverListController { private static final String TAG = "ResolverListController"; private static final boolean DEBUG = false; + private final UserHandle mQueryIntentsAsUser; private AbstractResolverComparator mResolverComparator; private boolean isComputed = false; @@ -67,25 +68,16 @@ public class ResolverListController { PackageManager pm, Intent targetIntent, String referrerPackage, - int launchedFromUid) { - this(context, pm, targetIntent, referrerPackage, launchedFromUid, - new ResolverRankerServiceResolverComparator( - context, targetIntent, referrerPackage, null, null)); - } - - public ResolverListController( - Context context, - PackageManager pm, - Intent targetIntent, - String referrerPackage, int launchedFromUid, - AbstractResolverComparator resolverComparator) { + AbstractResolverComparator resolverComparator, + UserHandle queryIntentsAsUser) { mContext = context; mpm = pm; mLaunchedFromUid = launchedFromUid; mTargetIntent = targetIntent; mReferrerPackage = referrerPackage; mResolverComparator = resolverComparator; + mQueryIntentsAsUser = queryIntentsAsUser; } @VisibleForTesting @@ -118,7 +110,8 @@ public class ResolverListController { | PackageManager.MATCH_DIRECT_BOOT_AWARE | PackageManager.MATCH_DIRECT_BOOT_UNAWARE | (shouldGetResolvedFilter ? PackageManager.GET_RESOLVED_FILTER : 0) - | (shouldGetActivityMetadata ? PackageManager.GET_META_DATA : 0); + | (shouldGetActivityMetadata ? PackageManager.GET_META_DATA : 0) + | PackageManager.MATCH_CLONE_PROFILE; return getResolversForIntentAsUserInternal(intents, userHandle, baseFlags); } @@ -154,6 +147,10 @@ public class ResolverListController { final int intoCount = into.size(); for (int i = 0; i < fromCount; i++) { final ResolveInfo newInfo = from.get(i); + if (newInfo.userHandle == null) { + Log.w(TAG, "Skipping ResolveInfo with no userHandle: " + newInfo); + continue; + } boolean found = false; // Only loop to the end of into as it was before we started; no dupes in from. for (int j = 0; j < intoCount; j++) { @@ -344,22 +341,28 @@ public class ResolverListController { @VisibleForTesting public float getScore(DisplayResolveInfo target) { - return mResolverComparator.getScore(target.getResolvedComponentName()); + return mResolverComparator.getScore(target); } /** * Returns the app share score of the given {@code componentName}. */ - public float getScore(ComponentName componentName) { - return mResolverComparator.getScore(componentName); + public float getScore(TargetInfo targetInfo) { + return mResolverComparator.getScore(targetInfo); } - public void updateModel(ComponentName componentName) { - mResolverComparator.updateModel(componentName); + /** + * Updates the model about the chosen {@code targetInfo}. + */ + public void updateModel(TargetInfo targetInfo) { + mResolverComparator.updateModel(targetInfo); } - public void updateChooserCounts(String packageName, int userId, String action) { - mResolverComparator.updateChooserCounts(packageName, userId, action); + /** + * Updates the model about Chooser Activity selection. + */ + public void updateChooserCounts(String packageName, UserHandle user, String action) { + mResolverComparator.updateChooserCounts(packageName, user, action); } public void destroy() { diff --git a/java/src/com/android/intentresolver/ResolverMultiProfilePagerAdapter.java b/java/src/com/android/intentresolver/ResolverMultiProfilePagerAdapter.java index 48e3b62d..85d97ad5 100644 --- a/java/src/com/android/intentresolver/ResolverMultiProfilePagerAdapter.java +++ b/java/src/com/android/intentresolver/ResolverMultiProfilePagerAdapter.java @@ -44,7 +44,8 @@ public class ResolverMultiProfilePagerAdapter extends ResolverListAdapter adapter, EmptyStateProvider emptyStateProvider, Supplier<Boolean> workProfileQuietModeChecker, - UserHandle workProfileUserHandle) { + UserHandle workProfileUserHandle, + UserHandle cloneProfileUserHandle) { this( context, ImmutableList.of(adapter), @@ -52,6 +53,7 @@ public class ResolverMultiProfilePagerAdapter extends workProfileQuietModeChecker, /* defaultProfile= */ 0, workProfileUserHandle, + cloneProfileUserHandle, new BottomPaddingOverrideSupplier()); } @@ -61,7 +63,8 @@ public class ResolverMultiProfilePagerAdapter extends EmptyStateProvider emptyStateProvider, Supplier<Boolean> workProfileQuietModeChecker, @Profile int defaultProfile, - UserHandle workProfileUserHandle) { + UserHandle workProfileUserHandle, + UserHandle cloneProfileUserHandle) { this( context, ImmutableList.of(personalAdapter, workAdapter), @@ -69,6 +72,7 @@ public class ResolverMultiProfilePagerAdapter extends workProfileQuietModeChecker, defaultProfile, workProfileUserHandle, + cloneProfileUserHandle, new BottomPaddingOverrideSupplier()); } @@ -79,6 +83,7 @@ public class ResolverMultiProfilePagerAdapter extends Supplier<Boolean> workProfileQuietModeChecker, @Profile int defaultProfile, UserHandle workProfileUserHandle, + UserHandle cloneProfileUserHandle, BottomPaddingOverrideSupplier bottomPaddingOverrideSupplier) { super( context, @@ -89,6 +94,7 @@ public class ResolverMultiProfilePagerAdapter extends workProfileQuietModeChecker, defaultProfile, workProfileUserHandle, + cloneProfileUserHandle, () -> (ViewGroup) LayoutInflater.from(context).inflate( R.layout.resolver_list_per_profile, null, false), bottomPaddingOverrideSupplier); diff --git a/java/src/com/android/intentresolver/WorkProfilePausedEmptyStateProvider.java b/java/src/com/android/intentresolver/WorkProfilePausedEmptyStateProvider.java index 0333039b..2f3dfbd5 100644 --- a/java/src/com/android/intentresolver/WorkProfilePausedEmptyStateProvider.java +++ b/java/src/com/android/intentresolver/WorkProfilePausedEmptyStateProvider.java @@ -29,7 +29,6 @@ import android.stats.devicepolicy.nano.DevicePolicyEnums; import com.android.intentresolver.AbstractMultiProfilePagerAdapter.EmptyState; import com.android.intentresolver.AbstractMultiProfilePagerAdapter.EmptyStateProvider; import com.android.intentresolver.AbstractMultiProfilePagerAdapter.OnSwitchOnWorkSelectedListener; -import com.android.internal.R; /** * Chooser/ResolverActivity empty state provider that returns empty state which is shown when diff --git a/java/src/com/android/intentresolver/chooser/DisplayResolveInfo.java b/java/src/com/android/intentresolver/chooser/DisplayResolveInfo.java index 29be6dc6..09cf319f 100644 --- a/java/src/com/android/intentresolver/chooser/DisplayResolveInfo.java +++ b/java/src/com/android/intentresolver/chooser/DisplayResolveInfo.java @@ -184,9 +184,10 @@ public class DisplayResolveInfo implements TargetInfo { return null; } - Intent merged = new Intent(matchingBase); - merged.fillIn(proposedRefinement, 0); - return new DisplayResolveInfo(this, merged, mPresentationGetter); + return new DisplayResolveInfo( + this, + TargetInfo.mergeRefinementIntoMatchingBaseIntent(matchingBase, proposedRefinement), + mPresentationGetter); } @Override diff --git a/java/src/com/android/intentresolver/chooser/ImmutableTargetInfo.java b/java/src/com/android/intentresolver/chooser/ImmutableTargetInfo.java index 2d9683e1..10d4415a 100644 --- a/java/src/com/android/intentresolver/chooser/ImmutableTargetInfo.java +++ b/java/src/com/android/intentresolver/chooser/ImmutableTargetInfo.java @@ -427,8 +427,8 @@ public final class ImmutableTargetInfo implements TargetInfo { return null; } - Intent merged = new Intent(matchingBase); - merged.fillIn(proposedRefinement, 0); + Intent merged = TargetInfo.mergeRefinementIntoMatchingBaseIntent( + matchingBase, proposedRefinement); return toBuilder().setBaseIntentToSend(merged).build(); } diff --git a/java/src/com/android/intentresolver/chooser/SelectableTargetInfo.java b/java/src/com/android/intentresolver/chooser/SelectableTargetInfo.java index 1fbe2da7..5766db0e 100644 --- a/java/src/com/android/intentresolver/chooser/SelectableTargetInfo.java +++ b/java/src/com/android/intentresolver/chooser/SelectableTargetInfo.java @@ -195,13 +195,13 @@ public final class SelectableTargetInfo extends ChooserTargetInfo { mResolvedComponentName = getResolvedComponentName(mSourceInfo, mBackupResolveInfo); - mAllSourceIntents = getAllSourceIntents(sourceInfo); - mBaseIntentToSend = getBaseIntentToSend( baseIntentToSend, mResolvedIntent, mReferrerFillInIntent); + mAllSourceIntents = getAllSourceIntents(sourceInfo, mBaseIntentToSend); + mHashProvider = context -> { final String plaintext = getChooserTargetComponentName().getPackageName() @@ -279,9 +279,9 @@ public final class SelectableTargetInfo extends ChooserTargetInfo { return null; } - Intent merged = new Intent(matchingBase); - merged.fillIn(proposedRefinement, 0); - return new SelectableTargetInfo(this, merged); + return new SelectableTargetInfo( + this, + TargetInfo.mergeRefinementIntoMatchingBaseIntent(matchingBase, proposedRefinement)); } @Override @@ -395,11 +395,22 @@ public final class SelectableTargetInfo extends ChooserTargetInfo { return sb.toString(); } - private static List<Intent> getAllSourceIntents(@Nullable DisplayResolveInfo sourceInfo) { + private static List<Intent> getAllSourceIntents( + @Nullable DisplayResolveInfo sourceInfo, Intent fallbackSourceIntent) { final List<Intent> results = new ArrayList<>(); if (sourceInfo != null) { - // We only queried the service for the first one in our sourceinfo. - results.add(sourceInfo.getAllSourceIntents().get(0)); + results.addAll(sourceInfo.getAllSourceIntents()); + } else { + // This target wasn't joined to a `DisplayResolveInfo` result from our intent-resolution + // step, so it was provided directly by the caller. We don't support alternate intents + // in this case, but we still permit refinement of the intent we'll dispatch; e.g., + // clients may use this hook to defer the computation of "lazy" extras in their share + // payload. Note this accommodation isn't strictly "necessary" because clients could + // always implement equivalent behavior by pointing custom targets back at their own app + // for any amount of further refinement/modification outside of the Sharesheet flow; + // nevertheless, it's offered as a convenience for clients who may expect their normal + // refinement logic to apply equally in the case of these "special targets." + results.add(fallbackSourceIntent); } return results; } diff --git a/java/src/com/android/intentresolver/chooser/TargetInfo.java b/java/src/com/android/intentresolver/chooser/TargetInfo.java index 2f48704c..9d793994 100644 --- a/java/src/com/android/intentresolver/chooser/TargetInfo.java +++ b/java/src/com/android/intentresolver/chooser/TargetInfo.java @@ -454,4 +454,49 @@ public interface TargetInfo { intent.fixUris(currentUserId); } } + + /** + * Derive a "complete" intent from a proposed `refinement` intent by merging it into a matching + * `base` intent, without modifying the filter-equality properties of the `base` intent, while + * still allowing the `refinement` to replace Share "payload" fields. + * Note! Callers are responsible for ensuring that the `base` is a suitable match for the given + * `refinement`, such that the two can be merged without modifying filter-equality properties. + */ + static Intent mergeRefinementIntoMatchingBaseIntent(Intent base, Intent refinement) { + Intent mergedIntent = new Intent(base); + + /* Copy over any fields from the `refinement` that weren't already specified by the `base`, + * along with the refined ClipData (if present, even if that overwrites data given in the + * `base` intent). + * + * Refinement may have modified the payload content stored in the ClipData; such changes + * are permitted in refinement since ClipData isn't a factor in the determination of + * `Intent.filterEquals()` (which must be preserved as an invariant of refinement). */ + mergedIntent.fillIn(refinement, Intent.FILL_IN_CLIP_DATA); + + /* Refinement may also modify payload content held in the 'extras' representation, as again + * those attributes aren't a factor in determining filter-equality. There is no `FILL_IN_*` + * flag that would allow the refinement to overwrite existing keys in the `base` extras, so + * here we have to implement the logic ourselves. + * + * Note this still doesn't imply that the refined intent is the final authority on extras; + * in particular, `SelectableTargetInfo.mActivityStarter` uses `Intent.putExtras(Bundle)` to + * merge in the `mChooserTargetIntentExtras` (i.e., the `EXTRA_SHORTCUT_ID`), which will + * overwrite any existing value. + * + * TODO: formalize the precedence and make sure extras are set in the appropriate stages, + * instead of relying on maintainers to know that (e.g.) authoritative changes belong in the + * `TargetActivityStarter`. Otherwise, any extras-based data that Sharesheet adds internally + * might be susceptible to "spoofing" from the refinement activity. */ + mergedIntent.putExtras(refinement); // Re-merge extras to favor refinement. + + // TODO(b/279067078): consider how to populate the "merged" ClipData. The `base` + // already has non-null ClipData due to the implicit migration in Intent, so if the + // refinement modified any of the payload extras, they *must* also provide a modified + // ClipData, or else the updated "extras" payload will be inconsistent with the + // pre-refinement ClipData when they're merged together. We may be able to do better, + // but there are complicated tradeoffs. + + return mergedIntent; + } } diff --git a/java/src/com/android/intentresolver/contentpreview/BasePreviewViewModel.kt b/java/src/com/android/intentresolver/contentpreview/BasePreviewViewModel.kt new file mode 100644 index 00000000..103e8bf4 --- /dev/null +++ b/java/src/com/android/intentresolver/contentpreview/BasePreviewViewModel.kt @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.intentresolver.contentpreview + +import androidx.annotation.MainThread +import androidx.lifecycle.ViewModel +import com.android.intentresolver.ChooserRequestParameters + +/** A contract for the preview view model. Added for testing. */ +abstract class BasePreviewViewModel : ViewModel() { + @MainThread + abstract fun createOrReuseProvider( + chooserRequest: ChooserRequestParameters + ): PreviewDataProvider + + @MainThread abstract fun createOrReuseImageLoader(): ImageLoader +} diff --git a/java/src/com/android/intentresolver/contentpreview/ChooserContentPreviewUi.java b/java/src/com/android/intentresolver/contentpreview/ChooserContentPreviewUi.java index 205be444..e8367c4e 100644 --- a/java/src/com/android/intentresolver/contentpreview/ChooserContentPreviewUi.java +++ b/java/src/com/android/intentresolver/contentpreview/ChooserContentPreviewUi.java @@ -21,51 +21,49 @@ import static com.android.intentresolver.contentpreview.ContentPreviewType.CONTE import static com.android.intentresolver.contentpreview.ContentPreviewType.CONTENT_PREVIEW_TEXT; import android.content.ClipData; -import android.content.ClipDescription; -import android.content.ContentInterface; import android.content.Intent; import android.content.res.Resources; import android.net.Uri; -import android.os.RemoteException; +import android.text.TextUtils; import android.view.LayoutInflater; import android.view.ViewGroup; import androidx.annotation.Nullable; +import androidx.annotation.VisibleForTesting; +import androidx.lifecycle.Lifecycle; -import com.android.intentresolver.ImageLoader; -import com.android.intentresolver.flags.FeatureFlagRepository; import com.android.intentresolver.widget.ActionRow; -import com.android.intentresolver.widget.ImagePreviewView; import com.android.intentresolver.widget.ImagePreviewView.TransitionElementStatusCallback; -import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; -import java.util.stream.Collectors; /** * Collection of helpers for building the content preview UI displayed in * {@link com.android.intentresolver.ChooserActivity}. - * * A content preview façade. */ public final class ChooserContentPreviewUi { + + private final Lifecycle mLifecycle; + /** * Delegate to build the default system action buttons to display in the preview layout, if/when * they're determined to be appropriate for the particular preview we display. * TODO: clarify why action buttons are part of preview logic. */ public interface ActionFactory { - /** Create an action that copies the share content to the clipboard. */ - ActionRow.Action createCopyButton(); - - /** Create an action that opens the share content in a system-default editor. */ + /** + * @return Runnable to be run when an edit button is clicked (if available). + */ @Nullable - ActionRow.Action createEditButton(); + Runnable getEditButtonRunnable(); - /** Create an "Share to Nearby" action. */ + /** + * @return Runnable to be run when a copy button is clicked (if available). + */ @Nullable - ActionRow.Action createNearbyButton(); + Runnable getCopyButtonRunnable(); /** Create custom actions */ List<ActionRow.Action> createCustomActions(); @@ -74,7 +72,7 @@ public final class ChooserContentPreviewUi { * Provides a share modification action, if any. */ @Nullable - Runnable getModifyShareAction(); + ActionRow.Action getModifyShareAction(); /** * <p> @@ -88,76 +86,90 @@ public final class ChooserContentPreviewUi { Consumer<Boolean> getExcludeSharedTextAction(); } - /** - * Testing shim to specify whether a given mime type is considered to be an "image." - * - * TODO: move away from {@link ChooserActivityOverrideData} as a model to configure our tests, - * then migrate {@link com.android.intentresolver.ChooserActivity#isImageType(String)} into this - * class. - */ - public interface ImageMimeTypeClassifier { - /** @return whether the specified {@code mimeType} is classified as an "image" type. */ - boolean isImageType(String mimeType); - } - - private final ContentPreviewUi mContentPreviewUi; + @VisibleForTesting + final ContentPreviewUi mContentPreviewUi; public ChooserContentPreviewUi( + Lifecycle lifecycle, + PreviewDataProvider previewData, Intent targetIntent, - ContentInterface contentResolver, - ImageMimeTypeClassifier imageClassifier, ImageLoader imageLoader, ActionFactory actionFactory, TransitionElementStatusCallback transitionElementStatusCallback, - FeatureFlagRepository featureFlagRepository) { - + HeadlineGenerator headlineGenerator) { + mLifecycle = lifecycle; mContentPreviewUi = createContentPreview( + previewData, targetIntent, - contentResolver, - imageClassifier, + DefaultMimeTypeClassifier.INSTANCE, imageLoader, actionFactory, transitionElementStatusCallback, - featureFlagRepository); + headlineGenerator); if (mContentPreviewUi.getType() != CONTENT_PREVIEW_IMAGE) { transitionElementStatusCallback.onAllTransitionElementsReady(); } } private ContentPreviewUi createContentPreview( + PreviewDataProvider previewData, Intent targetIntent, - ContentInterface contentResolver, - ImageMimeTypeClassifier imageClassifier, + MimeTypeClassifier typeClassifier, ImageLoader imageLoader, ActionFactory actionFactory, TransitionElementStatusCallback transitionElementStatusCallback, - FeatureFlagRepository featureFlagRepository) { - int type = findPreferredContentPreview(targetIntent, contentResolver, imageClassifier); - switch (type) { - case CONTENT_PREVIEW_TEXT: - return createTextPreview( - targetIntent, actionFactory, imageLoader, featureFlagRepository); - - case CONTENT_PREVIEW_FILE: - return new FileContentPreviewUi( - extractContentUris(targetIntent), - actionFactory, - imageLoader, - contentResolver, - featureFlagRepository); - - case CONTENT_PREVIEW_IMAGE: - return createImagePreview( - targetIntent, - actionFactory, - contentResolver, - imageClassifier, - imageLoader, - transitionElementStatusCallback, - featureFlagRepository); + HeadlineGenerator headlineGenerator) { + + int previewType = previewData.getPreviewType(); + if (previewType == CONTENT_PREVIEW_TEXT) { + return createTextPreview( + mLifecycle, + targetIntent, + actionFactory, + imageLoader, + headlineGenerator); + } + if (previewType == CONTENT_PREVIEW_FILE) { + FileContentPreviewUi fileContentPreviewUi = new FileContentPreviewUi( + previewData.getUriCount(), + actionFactory, + headlineGenerator); + if (previewData.getUriCount() > 0) { + previewData.getFirstFileName( + mLifecycle, fileContentPreviewUi::setFirstFileName); + } + return fileContentPreviewUi; + } + boolean isSingleImageShare = previewData.getUriCount() == 1 + && typeClassifier.isImageType(previewData.getFirstFileInfo().getMimeType()); + CharSequence text = targetIntent.getCharSequenceExtra(Intent.EXTRA_TEXT); + if (!TextUtils.isEmpty(text)) { + FilesPlusTextContentPreviewUi previewUi = + new FilesPlusTextContentPreviewUi( + mLifecycle, + isSingleImageShare, + previewData.getUriCount(), + targetIntent.getCharSequenceExtra(Intent.EXTRA_TEXT), + actionFactory, + imageLoader, + typeClassifier, + headlineGenerator); + if (previewData.getUriCount() > 0) { + previewData.getFileMetadataForImagePreview( + mLifecycle, previewUi::updatePreviewMetadata); + } + return previewUi; } - return new NoContextPreviewUi(type); + UnifiedContentPreviewUi unifiedContentPreviewUi = new UnifiedContentPreviewUi( + isSingleImageShare, + actionFactory, + imageLoader, + typeClassifier, + transitionElementStatusCallback, + headlineGenerator); + previewData.getFileMetadataForImagePreview(mLifecycle, unifiedContentPreviewUi::setFiles); + return unifiedContentPreviewUi; } public int getPreferredContentPreview() { @@ -174,68 +186,12 @@ public final class ChooserContentPreviewUi { return mContentPreviewUi.display(resources, layoutInflater, parent); } - /** Determine the most appropriate type of preview to show for the provided {@link Intent}. */ - @ContentPreviewType - private static int findPreferredContentPreview( - Intent targetIntent, - ContentInterface resolver, - ImageMimeTypeClassifier imageClassifier) { - /* In {@link android.content.Intent#getType}, the app may specify a very general mime type - * that broadly covers all data being shared, such as {@literal *}/* when sending an image - * and text. We therefore should inspect each item for the preferred type, in order: IMAGE, - * FILE, TEXT. */ - final String action = targetIntent.getAction(); - final String type = targetIntent.getType(); - final boolean isSend = Intent.ACTION_SEND.equals(action); - final boolean isSendMultiple = Intent.ACTION_SEND_MULTIPLE.equals(action); - - if (!(isSend || isSendMultiple) - || (type != null && ClipDescription.compareMimeTypes(type, "text/*"))) { - return CONTENT_PREVIEW_TEXT; - } - - if (isSend) { - Uri uri = targetIntent.getParcelableExtra(Intent.EXTRA_STREAM); - return findPreferredContentPreview(uri, resolver, imageClassifier); - } - - List<Uri> uris = targetIntent.getParcelableArrayListExtra(Intent.EXTRA_STREAM); - if (uris == null || uris.isEmpty()) { - return CONTENT_PREVIEW_TEXT; - } - - for (Uri uri : uris) { - // Defaulting to file preview when there are mixed image/file types is - // preferable, as it shows the user the correct number of items being shared - int uriPreviewType = findPreferredContentPreview(uri, resolver, imageClassifier); - if (uriPreviewType == CONTENT_PREVIEW_FILE) { - return CONTENT_PREVIEW_FILE; - } - } - - return CONTENT_PREVIEW_IMAGE; - } - - @ContentPreviewType - private static int findPreferredContentPreview( - Uri uri, ContentInterface resolver, ImageMimeTypeClassifier imageClassifier) { - if (uri == null) { - return CONTENT_PREVIEW_TEXT; - } - - String mimeType = null; - try { - mimeType = resolver.getType(uri); - } catch (RemoteException ignored) { - } - return imageClassifier.isImageType(mimeType) ? CONTENT_PREVIEW_IMAGE : CONTENT_PREVIEW_FILE; - } - private static TextContentPreviewUi createTextPreview( + Lifecycle lifecycle, Intent targetIntent, ChooserContentPreviewUi.ActionFactory actionFactory, ImageLoader imageLoader, - FeatureFlagRepository featureFlagRepository) { + HeadlineGenerator headlineGenerator) { CharSequence sharingText = targetIntent.getCharSequenceExtra(Intent.EXTRA_TEXT); String previewTitle = targetIntent.getStringExtra(Intent.EXTRA_TITLE); ClipData previewData = targetIntent.getClipData(); @@ -247,64 +203,12 @@ public final class ChooserContentPreviewUi { } } return new TextContentPreviewUi( + lifecycle, sharingText, previewTitle, previewThumbnail, actionFactory, imageLoader, - featureFlagRepository); - } - - static ImageContentPreviewUi createImagePreview( - Intent targetIntent, - ChooserContentPreviewUi.ActionFactory actionFactory, - ContentInterface contentResolver, - ChooserContentPreviewUi.ImageMimeTypeClassifier imageClassifier, - ImageLoader imageLoader, - ImagePreviewView.TransitionElementStatusCallback transitionElementStatusCallback, - FeatureFlagRepository featureFlagRepository) { - CharSequence text = targetIntent.getCharSequenceExtra(Intent.EXTRA_TEXT); - String action = targetIntent.getAction(); - // TODO: why don't we use image classifier for single-element ACTION_SEND? - final List<Uri> imageUris = Intent.ACTION_SEND.equals(action) - ? extractContentUris(targetIntent) - : extractContentUris(targetIntent) - .stream() - .filter(uri -> { - String type = null; - try { - type = contentResolver.getType(uri); - } catch (RemoteException ignored) { - } - return imageClassifier.isImageType(type); - }) - .collect(Collectors.toList()); - return new ImageContentPreviewUi( - imageUris, - text, - actionFactory, - imageLoader, - transitionElementStatusCallback, - featureFlagRepository); - } - - private static List<Uri> extractContentUris(Intent targetIntent) { - List<Uri> uris = new ArrayList<>(); - if (Intent.ACTION_SEND.equals(targetIntent.getAction())) { - Uri uri = targetIntent.getParcelableExtra(Intent.EXTRA_STREAM); - if (ContentPreviewUi.validForContentPreview(uri)) { - uris.add(uri); - } - } else { - List<Uri> receivedUris = targetIntent.getParcelableArrayListExtra(Intent.EXTRA_STREAM); - if (receivedUris != null) { - for (Uri uri : receivedUris) { - if (ContentPreviewUi.validForContentPreview(uri)) { - uris.add(uri); - } - } - } - } - return uris; + headlineGenerator); } } diff --git a/java/src/com/android/intentresolver/contentpreview/ContentPreviewUi.java b/java/src/com/android/intentresolver/contentpreview/ContentPreviewUi.java index 39856e66..07071236 100644 --- a/java/src/com/android/intentresolver/contentpreview/ContentPreviewUi.java +++ b/java/src/com/android/intentresolver/contentpreview/ContentPreviewUi.java @@ -16,31 +16,21 @@ package com.android.intentresolver.contentpreview; -import static android.content.ContentProvider.getUserIdFromUri; - import android.animation.ObjectAnimator; import android.animation.ValueAnimator; import android.content.res.Resources; import android.graphics.Bitmap; -import android.net.Uri; -import android.os.UserHandle; -import android.util.Log; +import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.view.ViewStub; import android.view.animation.DecelerateInterpolator; - -import androidx.annotation.LayoutRes; +import android.widget.ImageView; +import android.widget.TextView; import com.android.intentresolver.R; -import com.android.intentresolver.flags.FeatureFlagRepository; -import com.android.intentresolver.flags.Flags; import com.android.intentresolver.widget.ActionRow; -import com.android.intentresolver.widget.RoundedRectImageView; - -import java.util.ArrayList; -import java.util.List; +import com.android.intentresolver.widget.ScrollableImagePreviewView; abstract class ContentPreviewUi { private static final int IMAGE_FADE_IN_MILLIS = 150; @@ -52,53 +42,7 @@ abstract class ContentPreviewUi { public abstract ViewGroup display( Resources resources, LayoutInflater layoutInflater, ViewGroup parent); - protected static int getActionRowLayout(FeatureFlagRepository featureFlagRepository) { - return featureFlagRepository.isEnabled(Flags.SHARESHEET_CUSTOM_ACTIONS) - ? R.layout.scrollable_chooser_action_row - : R.layout.chooser_action_row; - } - - protected static ActionRow inflateActionRow(ViewGroup parent, @LayoutRes int actionRowLayout) { - final ViewStub stub = parent.findViewById(com.android.intentresolver.R.id.action_row_stub); - if (stub != null) { - stub.setLayoutResource(actionRowLayout); - stub.inflate(); - } - return parent.findViewById(com.android.internal.R.id.chooser_action_row); - } - - protected static List<ActionRow.Action> createActions( - List<ActionRow.Action> systemActions, - List<ActionRow.Action> customActions, - FeatureFlagRepository featureFlagRepository) { - ArrayList<ActionRow.Action> actions = - new ArrayList<>(systemActions.size() + customActions.size()); - actions.addAll(systemActions); - if (featureFlagRepository.isEnabled(Flags.SHARESHEET_CUSTOM_ACTIONS)) { - actions.addAll(customActions); - } - return actions; - } - - /** - * Indicate if the incoming content URI should be allowed. - * - * @param uri the uri to test - * @return true if the URI is allowed for content preview - */ - protected static boolean validForContentPreview(Uri uri) throws SecurityException { - if (uri == null) { - return false; - } - int userId = getUserIdFromUri(uri, UserHandle.USER_CURRENT); - if (userId != UserHandle.USER_CURRENT && userId != UserHandle.myUserId()) { - Log.e(ContentPreviewUi.TAG, "dropped invalid content URI belonging to user " + userId); - return false; - } - return true; - } - - protected static void updateViewWithImage(RoundedRectImageView imageView, Bitmap image) { + protected static void updateViewWithImage(ImageView imageView, Bitmap image) { if (image == null) { imageView.setVisibility(View.GONE); return; @@ -113,18 +57,45 @@ abstract class ContentPreviewUi { fadeAnim.start(); } - protected static void displayPayloadReselectionAction( + protected static void displayHeadline(ViewGroup layout, String headline) { + if (layout != null) { + TextView titleView = layout.findViewById(R.id.headline); + if (titleView != null) { + if (!TextUtils.isEmpty(headline)) { + titleView.setText(headline); + titleView.setVisibility(View.VISIBLE); + } else { + titleView.setVisibility(View.GONE); + } + } + } + } + + protected static void displayModifyShareAction( ViewGroup layout, - ChooserContentPreviewUi.ActionFactory actionFactory, - FeatureFlagRepository featureFlagRepository) { - Runnable modifyShareAction = actionFactory.getModifyShareAction(); - if (modifyShareAction != null && layout != null - && featureFlagRepository.isEnabled(Flags.SHARESHEET_RESELECTION_ACTION)) { - View modifyShareView = layout.findViewById(R.id.reselection_action); + ChooserContentPreviewUi.ActionFactory actionFactory) { + ActionRow.Action modifyShareAction = actionFactory.getModifyShareAction(); + if (modifyShareAction != null && layout != null) { + TextView modifyShareView = layout.findViewById(R.id.reselection_action); if (modifyShareView != null) { + modifyShareView.setText(modifyShareAction.getLabel()); modifyShareView.setVisibility(View.VISIBLE); - modifyShareView.setOnClickListener(view -> modifyShareAction.run()); + modifyShareView.setOnClickListener(view -> modifyShareAction.getOnClicked().run()); } } } + + protected static ScrollableImagePreviewView.PreviewType getPreviewType( + MimeTypeClassifier typeClassifier, String mimeType) { + if (mimeType == null) { + return ScrollableImagePreviewView.PreviewType.File; + } + if (typeClassifier.isImageType(mimeType)) { + return ScrollableImagePreviewView.PreviewType.Image; + } + if (typeClassifier.isVideoType(mimeType)) { + return ScrollableImagePreviewView.PreviewType.Video; + } + return ScrollableImagePreviewView.PreviewType.File; + } } diff --git a/java/src/com/android/intentresolver/ImageLoader.kt b/java/src/com/android/intentresolver/contentpreview/DefaultMimeTypeClassifier.kt index 0ed8b122..b9215709 100644 --- a/java/src/com/android/intentresolver/ImageLoader.kt +++ b/java/src/com/android/intentresolver/contentpreview/DefaultMimeTypeClassifier.kt @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 The Android Open Source Project + * Copyright (C) 2023 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,13 +14,6 @@ * limitations under the License. */ -package com.android.intentresolver +package com.android.intentresolver.contentpreview -import android.graphics.Bitmap -import android.net.Uri -import java.util.function.Consumer - -interface ImageLoader : suspend (Uri) -> Bitmap? { - fun loadImage(uri: Uri, callback: Consumer<Bitmap?>) - fun prePopulate(uris: List<Uri>) -} +object DefaultMimeTypeClassifier : MimeTypeClassifier diff --git a/java/src/com/android/intentresolver/contentpreview/FileContentPreviewUi.java b/java/src/com/android/intentresolver/contentpreview/FileContentPreviewUi.java index 7cd71475..20758189 100644 --- a/java/src/com/android/intentresolver/contentpreview/FileContentPreviewUi.java +++ b/java/src/com/android/intentresolver/contentpreview/FileContentPreviewUi.java @@ -16,15 +16,7 @@ package com.android.intentresolver.contentpreview; -import android.content.ContentInterface; import android.content.res.Resources; -import android.database.Cursor; -import android.net.Uri; -import android.os.RemoteException; -import android.provider.DocumentsContract; -import android.provider.Downloads; -import android.provider.OpenableColumns; -import android.text.TextUtils; import android.util.Log; import android.util.PluralsMessageFormatter; import android.view.LayoutInflater; @@ -33,38 +25,33 @@ import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; -import androidx.annotation.LayoutRes; +import androidx.annotation.Nullable; -import com.android.intentresolver.ImageLoader; import com.android.intentresolver.R; -import com.android.intentresolver.flags.FeatureFlagRepository; import com.android.intentresolver.widget.ActionRow; -import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; class FileContentPreviewUi extends ContentPreviewUi { private static final String PLURALS_COUNT = "count"; - private static final String PLURALS_FILE_NAME = "file_name"; - private final List<Uri> mUris; + @Nullable + private String mFirstFileName = null; + private final int mFileCount; private final ChooserContentPreviewUi.ActionFactory mActionFactory; - private final ImageLoader mImageLoader; - private final ContentInterface mContentResolver; - private final FeatureFlagRepository mFeatureFlagRepository; + private final HeadlineGenerator mHeadlineGenerator; + @Nullable + private ViewGroup mContentPreview = null; - FileContentPreviewUi(List<Uri> uris, + FileContentPreviewUi( + int fileCount, ChooserContentPreviewUi.ActionFactory actionFactory, - ImageLoader imageLoader, - ContentInterface contentResolver, - FeatureFlagRepository featureFlagRepository) { - mUris = uris; + HeadlineGenerator headlineGenerator) { + mFileCount = fileCount; mActionFactory = actionFactory; - mImageLoader = imageLoader; - mContentResolver = contentResolver; - mFeatureFlagRepository = featureFlagRepository; + mHeadlineGenerator = headlineGenerator; } @Override @@ -72,165 +59,62 @@ class FileContentPreviewUi extends ContentPreviewUi { return ContentPreviewType.CONTENT_PREVIEW_FILE; } + public void setFirstFileName(String fileName) { + mFirstFileName = fileName; + if (mContentPreview != null) { + showFileName(mContentPreview, fileName); + } + } + @Override public ViewGroup display(Resources resources, LayoutInflater layoutInflater, ViewGroup parent) { ViewGroup layout = displayInternal(resources, layoutInflater, parent); - displayPayloadReselectionAction(layout, mActionFactory, mFeatureFlagRepository); + displayModifyShareAction(layout, mActionFactory); return layout; } private ViewGroup displayInternal( Resources resources, LayoutInflater layoutInflater, ViewGroup parent) { - @LayoutRes int actionRowLayout = getActionRowLayout(mFeatureFlagRepository); - ViewGroup contentPreviewLayout = (ViewGroup) layoutInflater.inflate( + mContentPreview = (ViewGroup) layoutInflater.inflate( R.layout.chooser_grid_preview_file, parent, false); - final int uriCount = mUris.size(); + displayHeadline(mContentPreview, mHeadlineGenerator.getFilesHeadline(mFileCount)); - if (uriCount == 0) { - contentPreviewLayout.setVisibility(View.GONE); + if (mFileCount == 0) { + mContentPreview.setVisibility(View.GONE); Log.i(TAG, "Appears to be no uris available in EXTRA_STREAM," + " removing preview area"); - return contentPreviewLayout; - } - - if (uriCount == 1) { - loadFileUriIntoView(mUris.get(0), contentPreviewLayout, mImageLoader, mContentResolver); - } else { - FileInfo fileInfo = extractFileInfo(mUris.get(0), mContentResolver); - int remUriCount = uriCount - 1; - Map<String, Object> arguments = new HashMap<>(); - arguments.put(PLURALS_COUNT, remUriCount); - arguments.put(PLURALS_FILE_NAME, fileInfo.name); - String fileName = - PluralsMessageFormatter.format(resources, arguments, R.string.file_count); - - TextView fileNameView = contentPreviewLayout.findViewById( - com.android.internal.R.id.content_preview_filename); - fileNameView.setText(fileName); - - View thumbnailView = contentPreviewLayout.findViewById( - com.android.internal.R.id.content_preview_file_thumbnail); - thumbnailView.setVisibility(View.GONE); - - ImageView fileIconView = contentPreviewLayout.findViewById( - com.android.internal.R.id.content_preview_file_icon); - fileIconView.setVisibility(View.VISIBLE); - fileIconView.setImageResource(R.drawable.ic_file_copy); - } - - final ActionRow actionRow = inflateActionRow(contentPreviewLayout, actionRowLayout); - if (actionRow != null) { - actionRow.setActions( - createActions( - createFilePreviewActions(), - mActionFactory.createCustomActions(), - mFeatureFlagRepository)); + return mContentPreview; } - return contentPreviewLayout; - } - - private List<ActionRow.Action> createFilePreviewActions() { - List<ActionRow.Action> actions = new ArrayList<>(1); - //TODO(b/120417119): - // add action buttonFactory.createCopyButton() - ActionRow.Action action = mActionFactory.createNearbyButton(); - if (action != null) { - actions.add(action); + if (mFirstFileName != null) { + showFileName(mContentPreview, mFirstFileName); } - return actions; - } - private static void loadFileUriIntoView( - final Uri uri, - final View parent, - final ImageLoader imageLoader, - final ContentInterface contentResolver) { - FileInfo fileInfo = extractFileInfo(uri, contentResolver); - - TextView fileNameView = parent.findViewById( - com.android.internal.R.id.content_preview_filename); - fileNameView.setText(fileInfo.name); - - if (fileInfo.hasThumbnail) { - imageLoader.loadImage( - uri, - (bitmap) -> updateViewWithImage( - parent.findViewById( - com.android.internal.R.id.content_preview_file_thumbnail), - bitmap)); + TextView secondLine = mContentPreview.findViewById( + R.id.content_preview_more_files); + if (mFileCount > 1) { + int remUriCount = mFileCount - 1; + Map<String, Object> arguments = new HashMap<>(); + arguments.put(PLURALS_COUNT, remUriCount); + secondLine.setText( + PluralsMessageFormatter.format(resources, arguments, R.string.more_files)); } else { - View thumbnailView = parent.findViewById( - com.android.internal.R.id.content_preview_file_thumbnail); - thumbnailView.setVisibility(View.GONE); - - ImageView fileIconView = parent.findViewById( - com.android.internal.R.id.content_preview_file_icon); - fileIconView.setVisibility(View.VISIBLE); - fileIconView.setImageResource(R.drawable.chooser_file_generic); - } - } - - private static FileInfo extractFileInfo(Uri uri, ContentInterface resolver) { - String fileName = null; - boolean hasThumbnail = false; - - try (Cursor cursor = queryResolver(resolver, uri)) { - if (cursor != null && cursor.getCount() > 0) { - int nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); - int titleIndex = cursor.getColumnIndex(Downloads.Impl.COLUMN_TITLE); - int flagsIndex = cursor.getColumnIndex(DocumentsContract.Document.COLUMN_FLAGS); - - cursor.moveToFirst(); - if (nameIndex != -1) { - fileName = cursor.getString(nameIndex); - } else if (titleIndex != -1) { - fileName = cursor.getString(titleIndex); - } - - if (flagsIndex != -1) { - hasThumbnail = (cursor.getInt(flagsIndex) - & DocumentsContract.Document.FLAG_SUPPORTS_THUMBNAIL) != 0; - } - } - } catch (SecurityException | NullPointerException e) { - // The ContentResolver already logs the exception. Log something more informative. - Log.w( - TAG, - "Could not load (" + uri.toString() + ") thumbnail/name for preview. If " - + "desired, consider using Intent#createChooser to launch the ChooserActivity, " - + "and set your Intent's clipData and flags in accordance with that method's " - + "documentation"); + ImageView icon = mContentPreview.findViewById(R.id.content_preview_file_icon); + icon.setImageResource(R.drawable.single_file); + secondLine.setVisibility(View.GONE); } - if (TextUtils.isEmpty(fileName)) { - fileName = uri.getPath(); - fileName = fileName == null ? "" : fileName; - int index = fileName.lastIndexOf('/'); - if (index != -1) { - fileName = fileName.substring(index + 1); - } - } - - return new FileInfo(fileName, hasThumbnail); - } + final ActionRow actionRow = + mContentPreview.findViewById(com.android.internal.R.id.chooser_action_row); + List<ActionRow.Action> actions = mActionFactory.createCustomActions(); + actionRow.setActions(actions); - private static Cursor queryResolver(ContentInterface resolver, Uri uri) { - try { - return resolver.query(uri, null, null, null); - } catch (RemoteException e) { - return null; - } + return mContentPreview; } - private static class FileInfo { - public final String name; - public final boolean hasThumbnail; - - FileInfo(String name, boolean hasThumbnail) { - this.name = name; - this.hasThumbnail = hasThumbnail; - } + private void showFileName(ViewGroup contentPreview, String name) { + TextView fileNameView = contentPreview.requireViewById(R.id.content_preview_filename); + fileNameView.setText(name); } } diff --git a/java/src/com/android/intentresolver/contentpreview/FileInfo.kt b/java/src/com/android/intentresolver/contentpreview/FileInfo.kt new file mode 100644 index 00000000..fe35365b --- /dev/null +++ b/java/src/com/android/intentresolver/contentpreview/FileInfo.kt @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.intentresolver.contentpreview + +import android.net.Uri +import androidx.annotation.VisibleForTesting + +class FileInfo private constructor(val uri: Uri, val previewUri: Uri?, val mimeType: String?) { + @VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE) + class Builder(val uri: Uri) { + var previewUri: Uri? = null + private set + var mimeType: String? = null + private set + + @Synchronized fun withPreviewUri(uri: Uri?): Builder = apply { previewUri = uri } + + @Synchronized + fun withMimeType(mimeType: String?): Builder = apply { this.mimeType = mimeType } + + @Synchronized fun build(): FileInfo = FileInfo(uri, previewUri, mimeType) + } +} diff --git a/java/src/com/android/intentresolver/contentpreview/FilesPlusTextContentPreviewUi.java b/java/src/com/android/intentresolver/contentpreview/FilesPlusTextContentPreviewUi.java new file mode 100644 index 00000000..35990990 --- /dev/null +++ b/java/src/com/android/intentresolver/contentpreview/FilesPlusTextContentPreviewUi.java @@ -0,0 +1,231 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.intentresolver.contentpreview; + +import static com.android.intentresolver.contentpreview.ContentPreviewType.CONTENT_PREVIEW_FILE; +import static com.android.intentresolver.contentpreview.ContentPreviewType.CONTENT_PREVIEW_IMAGE; + +import android.content.res.Resources; +import android.net.Uri; +import android.text.util.Linkify; +import android.util.PluralsMessageFormatter; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.CheckBox; +import android.widget.ImageView; +import android.widget.TextView; + +import androidx.annotation.Nullable; +import androidx.lifecycle.Lifecycle; + +import com.android.intentresolver.R; +import com.android.intentresolver.widget.ActionRow; +import com.android.intentresolver.widget.ScrollableImagePreviewView; + +import java.util.HashMap; +import java.util.List; +import java.util.function.Consumer; + +/** + * FilesPlusTextContentPreviewUi is shown when the user is sending 1 or more files along with + * non-empty EXTRA_TEXT. The text can be toggled with a checkbox. If a single image file is being + * shared, it is shown in a preview (otherwise the headline summary is the sole indication of the + * file content). + */ +class FilesPlusTextContentPreviewUi extends ContentPreviewUi { + private final Lifecycle mLifecycle; + private final CharSequence mText; + private final ChooserContentPreviewUi.ActionFactory mActionFactory; + private final ImageLoader mImageLoader; + private final MimeTypeClassifier mTypeClassifier; + private final HeadlineGenerator mHeadlineGenerator; + private final boolean mIsSingleImage; + private final int mFileCount; + private ViewGroup mContentPreviewView; + private boolean mIsMetadataUpdated = false; + @Nullable + private Uri mFirstFilePreviewUri; + private boolean mAllImages; + private boolean mAllVideos; + // TODO(b/285309527): make this a flag + private static final boolean SHOW_TOGGLE_CHECKMARK = false; + + FilesPlusTextContentPreviewUi( + Lifecycle lifecycle, + boolean isSingleImage, + int fileCount, + CharSequence text, + ChooserContentPreviewUi.ActionFactory actionFactory, + ImageLoader imageLoader, + MimeTypeClassifier typeClassifier, + HeadlineGenerator headlineGenerator) { + mLifecycle = lifecycle; + if (isSingleImage && fileCount != 1) { + throw new IllegalArgumentException( + "fileCount = " + fileCount + " and isSingleImage = true"); + } + mFileCount = fileCount; + mIsSingleImage = isSingleImage; + mText = text; + mActionFactory = actionFactory; + mImageLoader = imageLoader; + mTypeClassifier = typeClassifier; + mHeadlineGenerator = headlineGenerator; + } + + @Override + public int getType() { + return mIsSingleImage ? CONTENT_PREVIEW_IMAGE : CONTENT_PREVIEW_FILE; + } + + @Override + public ViewGroup display(Resources resources, LayoutInflater layoutInflater, ViewGroup parent) { + ViewGroup layout = displayInternal(layoutInflater, parent); + displayModifyShareAction(layout, mActionFactory); + return layout; + } + + public void updatePreviewMetadata(List<FileInfo> files) { + boolean allImages = true; + boolean allVideos = true; + for (FileInfo fileInfo : files) { + ScrollableImagePreviewView.PreviewType previewType = + getPreviewType(mTypeClassifier, fileInfo.getMimeType()); + allImages = allImages && previewType == ScrollableImagePreviewView.PreviewType.Image; + allVideos = allVideos && previewType == ScrollableImagePreviewView.PreviewType.Video; + } + mAllImages = allImages; + mAllVideos = allVideos; + mFirstFilePreviewUri = files.isEmpty() ? null : files.get(0).getPreviewUri(); + mIsMetadataUpdated = true; + if (mContentPreviewView != null) { + updateUiWithMetadata(mContentPreviewView); + } + } + + private ViewGroup displayInternal(LayoutInflater layoutInflater, ViewGroup parent) { + mContentPreviewView = (ViewGroup) layoutInflater.inflate( + R.layout.chooser_grid_preview_files_text, parent, false); + + final ActionRow actionRow = + mContentPreviewView.findViewById(com.android.internal.R.id.chooser_action_row); + List<ActionRow.Action> actions = mActionFactory.createCustomActions(); + actionRow.setActions(actions); + + if (mIsMetadataUpdated) { + updateUiWithMetadata(mContentPreviewView); + } else if (!mIsSingleImage) { + mContentPreviewView.requireViewById(R.id.image_view).setVisibility(View.GONE); + } + + return mContentPreviewView; + } + + private void updateUiWithMetadata(ViewGroup contentPreviewView) { + prepareTextPreview(contentPreviewView, mActionFactory); + updateHeadline(contentPreviewView); + + ImageView imagePreview = mContentPreviewView.requireViewById(R.id.image_view); + if (mIsSingleImage && mFirstFilePreviewUri != null) { + mImageLoader.loadImage( + mLifecycle, + mFirstFilePreviewUri, + bitmap -> { + if (bitmap == null) { + imagePreview.setVisibility(View.GONE); + } else { + imagePreview.setImageBitmap(bitmap); + } + }); + } else { + imagePreview.setVisibility(View.GONE); + } + } + + private void updateHeadline(ViewGroup contentPreview) { + CheckBox includeText = contentPreview.requireViewById(R.id.include_text_action); + String headline; + if (includeText.getVisibility() == View.VISIBLE && includeText.isChecked()) { + if (mAllImages) { + headline = mHeadlineGenerator.getImagesWithTextHeadline(mText, mFileCount); + } else if (mAllVideos) { + headline = mHeadlineGenerator.getVideosWithTextHeadline(mText, mFileCount); + } else { + headline = mHeadlineGenerator.getFilesWithTextHeadline(mText, mFileCount); + } + } else { + if (mAllImages) { + headline = mHeadlineGenerator.getImagesHeadline(mFileCount); + } else if (mAllVideos) { + headline = mHeadlineGenerator.getVideosHeadline(mFileCount); + } else { + headline = mHeadlineGenerator.getFilesHeadline(mFileCount); + } + } + + displayHeadline(contentPreview, headline); + } + + private void prepareTextPreview( + ViewGroup contentPreview, + ChooserContentPreviewUi.ActionFactory actionFactory) { + final TextView textView = contentPreview.requireViewById(R.id.content_preview_text); + CheckBox includeText = contentPreview.requireViewById(R.id.include_text_action); + boolean isLink = HttpUriMatcher.isHttpUri(mText.toString()); + textView.setAutoLinkMask(isLink ? Linkify.WEB_URLS : 0); + textView.setText(mText); + + final Consumer<Boolean> shareTextAction = actionFactory.getExcludeSharedTextAction(); + includeText.setChecked(true); + includeText.setText(isLink ? R.string.include_link : R.string.include_text); + shareTextAction.accept(false); + includeText.setOnCheckedChangeListener((view, isChecked) -> { + if (isChecked) { + textView.setText(mText); + } else { + textView.setText(getNoTextString(contentPreview.getResources())); + } + shareTextAction.accept(!isChecked); + updateHeadline(contentPreview); + }); + if (SHOW_TOGGLE_CHECKMARK) { + includeText.setVisibility(View.VISIBLE); + } + } + + private String getNoTextString(Resources resources) { + int stringResource; + + if (mAllImages) { + stringResource = R.string.sharing_images_only; + } else if (mAllVideos) { + stringResource = R.string.sharing_videos_only; + } else { + stringResource = R.string.sharing_files_only; + } + + HashMap<String, Object> params = new HashMap<>(); + params.put("count", mFileCount); + + return PluralsMessageFormatter.format( + resources, + params, + stringResource + ); + } +} diff --git a/java/src/com/android/intentresolver/contentpreview/HeadlineGenerator.kt b/java/src/com/android/intentresolver/contentpreview/HeadlineGenerator.kt new file mode 100644 index 00000000..5f87c924 --- /dev/null +++ b/java/src/com/android/intentresolver/contentpreview/HeadlineGenerator.kt @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.intentresolver.contentpreview + +/** + * HeadlineGenerator generates the text to show at the top of the sharesheet as a brief + * description of the content being shared. + */ +interface HeadlineGenerator { + fun getTextHeadline(text: CharSequence): String + + fun getImagesWithTextHeadline(text: CharSequence, count: Int): String + + fun getVideosWithTextHeadline(text: CharSequence, count: Int): String + + fun getFilesWithTextHeadline(text: CharSequence, count: Int): String + + fun getImagesHeadline(count: Int): String + + fun getVideosHeadline(count: Int): String + + fun getFilesHeadline(count: Int): String +} diff --git a/java/src/com/android/intentresolver/contentpreview/HeadlineGeneratorImpl.kt b/java/src/com/android/intentresolver/contentpreview/HeadlineGeneratorImpl.kt new file mode 100644 index 00000000..1aace8c3 --- /dev/null +++ b/java/src/com/android/intentresolver/contentpreview/HeadlineGeneratorImpl.kt @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.intentresolver.contentpreview + +import android.annotation.StringRes +import android.content.Context +import com.android.intentresolver.R +import android.util.PluralsMessageFormatter + +private const val PLURALS_COUNT = "count" + +/** + * HeadlineGenerator generates the text to show at the top of the sharesheet as a brief + * description of the content being shared. + */ +class HeadlineGeneratorImpl(private val context: Context) : HeadlineGenerator { + override fun getTextHeadline(text: CharSequence): String { + return context.getString( + getTemplateResource(text, R.string.sharing_link, R.string.sharing_text)) + } + + override fun getImagesWithTextHeadline(text: CharSequence, count: Int): String { + return getPluralString(getTemplateResource( + text, R.string.sharing_images_with_link, R.string.sharing_images_with_text), count) + } + + override fun getVideosWithTextHeadline(text: CharSequence, count: Int): String { + return getPluralString(getTemplateResource( + text, R.string.sharing_videos_with_link, R.string.sharing_videos_with_text), count) + } + + override fun getFilesWithTextHeadline(text: CharSequence, count: Int): String { + return getPluralString(getTemplateResource( + text, R.string.sharing_files_with_link, R.string.sharing_files_with_text), count) + } + + override fun getImagesHeadline(count: Int): String { + return getPluralString(R.string.sharing_images, count) + } + + override fun getVideosHeadline(count: Int): String { + return getPluralString(R.string.sharing_videos, count) + } + + override fun getFilesHeadline(count: Int): String { + return getPluralString(R.string.sharing_files, count) + } + + private fun getPluralString(@StringRes templateResource: Int, count: Int): String { + return PluralsMessageFormatter.format( + context.resources, + mapOf(PLURALS_COUNT to count), + templateResource + ) + } + + @StringRes + private fun getTemplateResource( + text: CharSequence, @StringRes linkResource: Int, @StringRes nonLinkResource: Int + ): Int { + return if (text.toString().isHttpUri()) linkResource else nonLinkResource + } +} diff --git a/java/src/com/android/intentresolver/contentpreview/ImageContentPreviewUi.java b/java/src/com/android/intentresolver/contentpreview/ImageContentPreviewUi.java deleted file mode 100644 index db26ab1b..00000000 --- a/java/src/com/android/intentresolver/contentpreview/ImageContentPreviewUi.java +++ /dev/null @@ -1,179 +0,0 @@ -/* - * Copyright (C) 2023 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.intentresolver.contentpreview; - -import static com.android.intentresolver.contentpreview.ContentPreviewType.CONTENT_PREVIEW_IMAGE; - -import android.content.res.Resources; -import android.net.Uri; -import android.text.TextUtils; -import android.text.util.Linkify; -import android.transition.TransitionManager; -import android.util.Log; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.view.ViewStub; -import android.widget.CheckBox; -import android.widget.TextView; - -import androidx.annotation.LayoutRes; -import androidx.annotation.Nullable; - -import com.android.intentresolver.ImageLoader; -import com.android.intentresolver.R; -import com.android.intentresolver.flags.FeatureFlagRepository; -import com.android.intentresolver.flags.Flags; -import com.android.intentresolver.widget.ActionRow; -import com.android.intentresolver.widget.ImagePreviewView; - -import java.util.ArrayList; -import java.util.List; -import java.util.function.Consumer; - -class ImageContentPreviewUi extends ContentPreviewUi { - private final List<Uri> mImageUris; - @Nullable - private final CharSequence mText; - private final ChooserContentPreviewUi.ActionFactory mActionFactory; - private final ImageLoader mImageLoader; - private final ImagePreviewView.TransitionElementStatusCallback mTransitionElementStatusCallback; - private final FeatureFlagRepository mFeatureFlagRepository; - - ImageContentPreviewUi( - List<Uri> imageUris, - @Nullable CharSequence text, - ChooserContentPreviewUi.ActionFactory actionFactory, - ImageLoader imageLoader, - ImagePreviewView.TransitionElementStatusCallback transitionElementStatusCallback, - FeatureFlagRepository featureFlagRepository) { - mImageUris = imageUris; - mText = text; - mActionFactory = actionFactory; - mImageLoader = imageLoader; - mTransitionElementStatusCallback = transitionElementStatusCallback; - mFeatureFlagRepository = featureFlagRepository; - - mImageLoader.prePopulate(mImageUris); - } - - @Override - public int getType() { - return CONTENT_PREVIEW_IMAGE; - } - - @Override - public ViewGroup display(Resources resources, LayoutInflater layoutInflater, ViewGroup parent) { - ViewGroup layout = displayInternal(layoutInflater, parent); - displayPayloadReselectionAction(layout, mActionFactory, mFeatureFlagRepository); - return layout; - } - - private ViewGroup displayInternal(LayoutInflater layoutInflater, ViewGroup parent) { - @LayoutRes int actionRowLayout = getActionRowLayout(mFeatureFlagRepository); - ViewGroup contentPreviewLayout = (ViewGroup) layoutInflater.inflate( - R.layout.chooser_grid_preview_image, parent, false); - ImagePreviewView imagePreview = inflateImagePreviewView(contentPreviewLayout); - - final ActionRow actionRow = inflateActionRow(contentPreviewLayout, actionRowLayout); - if (actionRow != null) { - actionRow.setActions( - createActions( - createImagePreviewActions(), - mActionFactory.createCustomActions(), - mFeatureFlagRepository)); - } - - if (mImageUris.size() == 0) { - Log.i( - TAG, - "Attempted to display image preview area with zero" - + " available images detected in EXTRA_STREAM list"); - ((View) imagePreview).setVisibility(View.GONE); - mTransitionElementStatusCallback.onAllTransitionElementsReady(); - return contentPreviewLayout; - } - - setTextInImagePreviewVisibility(contentPreviewLayout, mActionFactory); - imagePreview.setTransitionElementStatusCallback(mTransitionElementStatusCallback); - imagePreview.setImages(mImageUris, mImageLoader); - - return contentPreviewLayout; - } - - private List<ActionRow.Action> createImagePreviewActions() { - ArrayList<ActionRow.Action> actions = new ArrayList<>(2); - //TODO: add copy action; - ActionRow.Action action = mActionFactory.createNearbyButton(); - if (action != null) { - actions.add(action); - } - action = mActionFactory.createEditButton(); - if (action != null) { - actions.add(action); - } - return actions; - } - - private ImagePreviewView inflateImagePreviewView(ViewGroup previewLayout) { - ViewStub stub = previewLayout.findViewById(R.id.image_preview_stub); - if (stub != null) { - int layoutId = - mFeatureFlagRepository.isEnabled(Flags.SHARESHEET_SCROLLABLE_IMAGE_PREVIEW) - ? R.layout.scrollable_image_preview_view - : R.layout.chooser_image_preview_view; - stub.setLayoutResource(layoutId); - stub.inflate(); - } - return previewLayout.findViewById( - com.android.internal.R.id.content_preview_image_area); - } - - private void setTextInImagePreviewVisibility( - ViewGroup contentPreview, ChooserContentPreviewUi.ActionFactory actionFactory) { - int visibility = mFeatureFlagRepository.isEnabled(Flags.SHARESHEET_IMAGE_AND_TEXT_PREVIEW) - && !TextUtils.isEmpty(mText) - ? View.VISIBLE - : View.GONE; - - final TextView textView = contentPreview - .requireViewById(com.android.internal.R.id.content_preview_text); - CheckBox actionView = contentPreview - .requireViewById(R.id.include_text_action); - textView.setVisibility(visibility); - boolean isLink = visibility == View.VISIBLE && HttpUriMatcher.isHttpUri(mText.toString()); - textView.setAutoLinkMask(isLink ? Linkify.WEB_URLS : 0); - textView.setText(mText); - - if (visibility == View.VISIBLE) { - final int[] actionLabels = isLink - ? new int[] { R.string.include_link, R.string.exclude_link } - : new int[] { R.string.include_text, R.string.exclude_text }; - final Consumer<Boolean> shareTextAction = actionFactory.getExcludeSharedTextAction(); - actionView.setChecked(true); - actionView.setText(actionLabels[1]); - shareTextAction.accept(false); - actionView.setOnCheckedChangeListener((view, isChecked) -> { - view.setText(actionLabels[isChecked ? 1 : 0]); - TransitionManager.beginDelayedTransition((ViewGroup) textView.getParent()); - textView.setVisibility(isChecked ? View.VISIBLE : View.GONE); - shareTextAction.accept(!isChecked); - }); - } - actionView.setVisibility(visibility); - } -} diff --git a/java/src/com/android/intentresolver/contentpreview/ImageLoader.kt b/java/src/com/android/intentresolver/contentpreview/ImageLoader.kt new file mode 100644 index 00000000..8d0fb84b --- /dev/null +++ b/java/src/com/android/intentresolver/contentpreview/ImageLoader.kt @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.intentresolver.contentpreview + +import android.graphics.Bitmap +import android.net.Uri +import androidx.lifecycle.Lifecycle +import java.util.function.Consumer + +/** A content preview image loader. */ +interface ImageLoader : suspend (Uri) -> Bitmap?, suspend (Uri, Boolean) -> Bitmap? { + /** + * Load preview image asynchronously; caching is allowed. + * + * @param uri content URI + * @param callback a callback that will be invoked with the loaded image or null if loading has + * failed. + */ + fun loadImage(callerLifecycle: Lifecycle, uri: Uri, callback: Consumer<Bitmap?>) + + /** Prepopulate the image loader cache. */ + fun prePopulate(uris: List<Uri>) + + /** Load preview image; caching is allowed. */ + override suspend fun invoke(uri: Uri) = invoke(uri, true) + + /** + * Load preview image. + * + * @param uri content URI + * @param caching indicates if the loaded image could be cached. + */ + override suspend fun invoke(uri: Uri, caching: Boolean): Bitmap? +} diff --git a/java/src/com/android/intentresolver/contentpreview/ImagePreviewImageLoader.kt b/java/src/com/android/intentresolver/contentpreview/ImagePreviewImageLoader.kt new file mode 100644 index 00000000..22dd1125 --- /dev/null +++ b/java/src/com/android/intentresolver/contentpreview/ImagePreviewImageLoader.kt @@ -0,0 +1,156 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.intentresolver.contentpreview + +import android.content.ContentResolver +import android.graphics.Bitmap +import android.net.Uri +import android.util.Log +import android.util.Size +import androidx.annotation.GuardedBy +import androidx.annotation.VisibleForTesting +import androidx.collection.LruCache +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.coroutineScope +import java.util.function.Consumer +import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.CompletableDeferred +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Deferred +import kotlinx.coroutines.isActive +import kotlinx.coroutines.launch +import kotlinx.coroutines.sync.Semaphore + +private const val TAG = "ImagePreviewImageLoader" + +/** + * Implements preview image loading for the content preview UI. Provides requests deduplication, + * image caching, and a limit on the number of parallel loadings. + */ +@VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE) +class ImagePreviewImageLoader +@VisibleForTesting +constructor( + private val scope: CoroutineScope, + thumbnailSize: Int, + private val contentResolver: ContentResolver, + cacheSize: Int, + // TODO: consider providing a scope with the dispatcher configured with + // [CoroutineDispatcher#limitedParallelism] instead + private val contentResolverSemaphore: Semaphore, +) : ImageLoader { + + constructor( + scope: CoroutineScope, + thumbnailSize: Int, + contentResolver: ContentResolver, + cacheSize: Int, + maxSimultaneousRequests: Int = 4 + ) : this(scope, thumbnailSize, contentResolver, cacheSize, Semaphore(maxSimultaneousRequests)) + + private val thumbnailSize: Size = Size(thumbnailSize, thumbnailSize) + + private val lock = Any() + @GuardedBy("lock") private val cache = LruCache<Uri, RequestRecord>(cacheSize) + @GuardedBy("lock") private val runningRequests = HashMap<Uri, RequestRecord>() + + override suspend fun invoke(uri: Uri, caching: Boolean): Bitmap? = loadImageAsync(uri, caching) + + override fun loadImage(callerLifecycle: Lifecycle, uri: Uri, callback: Consumer<Bitmap?>) { + callerLifecycle.coroutineScope.launch { + val image = loadImageAsync(uri, caching = true) + if (isActive) { + callback.accept(image) + } + } + } + + override fun prePopulate(uris: List<Uri>) { + uris.asSequence().take(cache.maxSize()).forEach { uri -> + scope.launch { loadImageAsync(uri, caching = true) } + } + } + + private suspend fun loadImageAsync(uri: Uri, caching: Boolean): Bitmap? { + return getRequestDeferred(uri, caching).await() + } + + private fun getRequestDeferred(uri: Uri, caching: Boolean): Deferred<Bitmap?> { + var shouldLaunchImageLoading = false + val request = + synchronized(lock) { + cache[uri] + ?: runningRequests + .getOrPut(uri) { + shouldLaunchImageLoading = true + RequestRecord(uri, CompletableDeferred(), caching) + } + .apply { this.caching = this.caching || caching } + } + if (shouldLaunchImageLoading) { + request.loadBitmapAsync() + } + return request.deferred + } + + private fun RequestRecord.loadBitmapAsync() { + scope + .launch { loadBitmap() } + .invokeOnCompletion { cause -> + if (cause is CancellationException) { + cancel() + } + } + } + + private suspend fun RequestRecord.loadBitmap() { + contentResolverSemaphore.acquire() + val bitmap = + try { + contentResolver.loadThumbnail(uri, thumbnailSize, null) + } catch (t: Throwable) { + Log.d(TAG, "failed to load $uri preview", t) + null + } finally { + contentResolverSemaphore.release() + } + complete(bitmap) + } + + private fun RequestRecord.cancel() { + synchronized(lock) { + runningRequests.remove(uri) + deferred.cancel() + } + } + + private fun RequestRecord.complete(bitmap: Bitmap?) { + deferred.complete(bitmap) + synchronized(lock) { + runningRequests.remove(uri) + if (bitmap != null && caching) { + cache.put(uri, this) + } + } + } + + private class RequestRecord( + val uri: Uri, + val deferred: CompletableDeferred<Bitmap?>, + @GuardedBy("lock") var caching: Boolean + ) +} diff --git a/java/src/com/android/intentresolver/contentpreview/MimeTypeClassifier.java b/java/src/com/android/intentresolver/contentpreview/MimeTypeClassifier.java new file mode 100644 index 00000000..0c333b68 --- /dev/null +++ b/java/src/com/android/intentresolver/contentpreview/MimeTypeClassifier.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.intentresolver.contentpreview; + +import android.content.ClipDescription; + +import androidx.annotation.Nullable; + +/** + * Testing shim to specify whether a given mime type is considered to be an "image." + */ +public interface MimeTypeClassifier { + /** @return whether the specified {@code mimeType} is classified as an "image" type. */ + default boolean isImageType(@Nullable String mimeType) { + return (mimeType != null) && ClipDescription.compareMimeTypes(mimeType, "image/*"); + } + + /** @return whether the specified {@code mimeType} is classified as an "video" type */ + default boolean isVideoType(@Nullable String mimeType) { + return (mimeType != null) && ClipDescription.compareMimeTypes(mimeType, "video/*"); + } +} diff --git a/java/src/com/android/intentresolver/contentpreview/PreviewDataProvider.kt b/java/src/com/android/intentresolver/contentpreview/PreviewDataProvider.kt new file mode 100644 index 00000000..8ab3a272 --- /dev/null +++ b/java/src/com/android/intentresolver/contentpreview/PreviewDataProvider.kt @@ -0,0 +1,394 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.intentresolver.contentpreview + +import android.content.ContentInterface +import android.content.Intent +import android.database.Cursor +import android.media.MediaMetadata +import android.net.Uri +import android.provider.DocumentsContract +import android.provider.DocumentsContract.Document.FLAG_SUPPORTS_THUMBNAIL +import android.provider.Downloads +import android.provider.OpenableColumns +import android.text.TextUtils +import android.util.Log +import androidx.annotation.OpenForTesting +import androidx.annotation.VisibleForTesting +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.coroutineScope +import com.android.intentresolver.contentpreview.ContentPreviewType.CONTENT_PREVIEW_FILE +import com.android.intentresolver.contentpreview.ContentPreviewType.CONTENT_PREVIEW_IMAGE +import com.android.intentresolver.contentpreview.ContentPreviewType.CONTENT_PREVIEW_TEXT +import com.android.intentresolver.measurements.runTracing +import com.android.intentresolver.util.ownedByCurrentUser +import java.util.concurrent.atomic.AtomicInteger +import java.util.function.Consumer +import kotlinx.coroutines.CompletableDeferred +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.isActive +import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.withContext +import kotlinx.coroutines.withTimeoutOrNull + +/** + * A set of metadata columns we read for a content URI (see + * [PreviewDataProvider.UriRecord.readQueryResult] method). + */ +@VisibleForTesting +val METADATA_COLUMNS = + arrayOf( + DocumentsContract.Document.COLUMN_FLAGS, + MediaMetadata.METADATA_KEY_DISPLAY_ICON_URI, + OpenableColumns.DISPLAY_NAME, + Downloads.Impl.COLUMN_TITLE + ) +private const val TIMEOUT_MS = 1_000L + +/** + * Asynchronously loads and stores shared URI metadata (see [Intent.EXTRA_STREAM]) such as mime + * type, file name, and a preview thumbnail URI. + */ +@OpenForTesting +open class PreviewDataProvider +@VisibleForTesting +constructor( + private val targetIntent: Intent, + private val contentResolver: ContentInterface, + private val typeClassifier: MimeTypeClassifier, + private val dispatcher: CoroutineDispatcher, +) { + constructor( + targetIntent: Intent, + contentResolver: ContentInterface, + ) : this( + targetIntent, + contentResolver, + DefaultMimeTypeClassifier, + Dispatchers.IO, + ) + + private val records = targetIntent.contentUris.map { UriRecord(it) } + + /** returns number of shared URIs, see [Intent.EXTRA_STREAM] */ + @get:OpenForTesting + open val uriCount: Int + get() = records.size + + /** + * Preview type to use. The type is determined asynchronously with a timeout; the fall-back + * values is [ContentPreviewType.CONTENT_PREVIEW_FILE] + */ + @get:OpenForTesting + @get:ContentPreviewType + open val previewType: Int by lazy { + runTracing("preview-type") { + /* In [android.content.Intent#getType], the app may specify a very general mime type + * that broadly covers all data being shared, such as '*' when sending an image + * and text. We therefore should inspect each item for the preferred type, in order: + * IMAGE, FILE, TEXT. */ + if (!targetIntent.isSend || records.isEmpty()) { + CONTENT_PREVIEW_TEXT + } else { + runBlocking(dispatcher) { + withTimeoutOrNull(TIMEOUT_MS) { + loadPreviewType() + } ?: CONTENT_PREVIEW_FILE + } + } + } + } + + /** + * The first shared URI's metadata. This call wait's for the data to be loaded and falls back to + * a crude value if the data is not loaded within a time limit. + */ + open val firstFileInfo: FileInfo? by lazy { + runTracing("first-uri-metadata") { + records.firstOrNull()?.let { record -> + runBlocking(dispatcher) { + val builder = FileInfo.Builder(record.uri) + withTimeoutOrNull(TIMEOUT_MS) { + builder.readFromRecord(record) + } + builder.build() + } + } + } + } + + /** + * Returns a collection of [FileInfo], for each shared URI in order, with [FileInfo.mimeType] + * and [FileInfo.previewUri] set (a data projection tailored for the image preview UI). + */ + @OpenForTesting + open fun getFileMetadataForImagePreview( + callerLifecycle: Lifecycle, + callback: Consumer<List<FileInfo>>, + ) { + callerLifecycle.coroutineScope.launch { + val result = withContext(dispatcher) { + getFileMetadataForImagePreview() + } + callback.accept(result) + } + } + + private fun getFileMetadataForImagePreview(): List<FileInfo> = + runTracing("image-preview-metadata") { + ArrayList<FileInfo>(records.size).also { result -> + for (record in records) { + result.add( + FileInfo.Builder(record.uri) + .readFromRecord(record) + .build() + ) + } + } + } + + private fun FileInfo.Builder.readFromRecord(record: UriRecord): FileInfo.Builder { + withMimeType(record.mimeType) + val previewUri = + when { + record.isImageType || record.supportsImageType || record.supportsThumbnail -> + record.uri + else -> record.iconUri + } + withPreviewUri(previewUri) + return this + } + + /** + * Returns a title for the first shared URI which is read from URI metadata or, if the metadata + * is not provided, derived from the URI. + */ + @Throws(IndexOutOfBoundsException::class) + fun getFirstFileName(callerLifecycle: Lifecycle, callback: Consumer<String>) { + if (records.isEmpty()) { + throw IndexOutOfBoundsException("There are no shared URIs") + } + callerLifecycle.coroutineScope.launch { + val result = withContext(dispatcher) { + getFirstFileName() + } + callback.accept(result) + } + } + + @Throws(IndexOutOfBoundsException::class) + private fun getFirstFileName(): String { + if (records.isEmpty()) throw IndexOutOfBoundsException("There are no shared URIs") + + val record = records[0] + return if (TextUtils.isEmpty(record.title)) getFileName(record.uri) else record.title + } + + @ContentPreviewType + private suspend fun loadPreviewType(): Int { + // Execute [ContentResolver#getType()] calls sequentially as the method contains a timeout + // logic for the actual [ContentProvider#getType] call. Thus it is possible for one getType + // call's timeout work against other concurrent getType calls e.g. when a two concurrent + // calls on the caller side are scheduled on the same thread on the callee side. + records + .firstOrNull { it.isImageType } + ?.run { + return CONTENT_PREVIEW_IMAGE + } + + val resultDeferred = CompletableDeferred<Int>() + return coroutineScope { + val job = launch { + coroutineScope { + val nextIndex = AtomicInteger(0) + repeat(4) { + launch { + while (isActive) { + val i = nextIndex.getAndIncrement() + if (i >= records.size) break + val hasPreview = + with(records[i]) { + supportsImageType || supportsThumbnail || iconUri != null + } + if (hasPreview) { + resultDeferred.complete(CONTENT_PREVIEW_IMAGE) + break + } + } + } + } + } + resultDeferred.complete(CONTENT_PREVIEW_FILE) + } + resultDeferred.await() + .also { job.cancel() } + } + } + + /** + * Provides a lazy evaluation and caches results of [ContentInterface.getType], + * [ContentInterface.getStreamTypes], and [ContentInterface.query] methods for the given [uri]. + */ + private inner class UriRecord(val uri: Uri) { + val mimeType: String? by lazy { contentResolver.getTypeSafe(uri) } + val isImageType: Boolean + get() = typeClassifier.isImageType(mimeType) + val supportsImageType: Boolean by lazy { + contentResolver.getStreamTypesSafe(uri) + ?.firstOrNull(typeClassifier::isImageType) != null + } + val supportsThumbnail: Boolean + get() = query.supportsThumbnail + val title: String + get() = query.title + val iconUri: Uri? + get() = query.iconUri + + private val query by lazy { readQueryResult() } + + private fun readQueryResult(): QueryResult { + val cursor = contentResolver.querySafe(uri) + ?.takeIf { it.moveToFirst() } + ?: return QueryResult() + + var flagColIdx = -1 + var displayIconUriColIdx = -1 + var nameColIndex = -1 + var titleColIndex = -1 + // TODO: double-check why Cursor#getColumnInded didn't work + cursor.columnNames.forEachIndexed { i, columnName -> + when (columnName) { + DocumentsContract.Document.COLUMN_FLAGS -> flagColIdx = i + MediaMetadata.METADATA_KEY_DISPLAY_ICON_URI -> displayIconUriColIdx = i + OpenableColumns.DISPLAY_NAME -> nameColIndex = i + Downloads.Impl.COLUMN_TITLE -> titleColIndex = i + } + } + + val supportsThumbnail = + flagColIdx >= 0 && ((cursor.getInt(flagColIdx) and FLAG_SUPPORTS_THUMBNAIL) != 0) + + var title = "" + if (nameColIndex >= 0) { + title = cursor.getString(nameColIndex) ?: "" + } + if (TextUtils.isEmpty(title) && titleColIndex >= 0) { + title = cursor.getString(titleColIndex) ?: "" + } + + val iconUri = + if (displayIconUriColIdx >= 0) { + cursor.getString(displayIconUriColIdx)?.let(Uri::parse) + } else { + null + } + + return QueryResult(supportsThumbnail, title, iconUri) + } + } + + private class QueryResult( + val supportsThumbnail: Boolean = false, + val title: String = "", + val iconUri: Uri? = null + ) +} + +private val Intent.isSend: Boolean + get() = + action.let { action -> + Intent.ACTION_SEND == action || Intent.ACTION_SEND_MULTIPLE == action + } + +private val Intent.contentUris: ArrayList<Uri> + get() = + ArrayList<Uri>().also { uris -> + if (Intent.ACTION_SEND == action) { + getParcelableExtra<Uri>(Intent.EXTRA_STREAM) + ?.takeIf { it.ownedByCurrentUser } + ?.let { uris.add(it) } + } else { + getParcelableArrayListExtra<Uri>(Intent.EXTRA_STREAM)?.fold(uris) { accumulator, uri + -> + if (uri.ownedByCurrentUser) { + accumulator.add(uri) + } + accumulator + } + } + } + +private fun getFileName(uri: Uri): String { + val fileName = uri.path ?: return "" + val index = fileName.lastIndexOf('/') + return if (index < 0) { + fileName + } else { + fileName.substring(index + 1) + } +} + +private fun ContentInterface.getTypeSafe(uri: Uri): String? = + runTracing("getType") { + try { + getType(uri) + } catch (e: SecurityException) { + logProviderPermissionWarning(uri, "mime type") + null + } catch (t: Throwable) { + Log.e(ContentPreviewUi.TAG, "Failed to read metadata, uri: $uri", t) + null + } + } + +private fun ContentInterface.getStreamTypesSafe(uri: Uri): Array<String>? = + runTracing("getStreamTypes") { + try { + getStreamTypes(uri, "*/*") + } catch (e: SecurityException) { + logProviderPermissionWarning(uri, "stream types") + null + } catch (t: Throwable) { + Log.e(ContentPreviewUi.TAG, "Failed to read stream types, uri: $uri", t) + null + } + } + +private fun ContentInterface.querySafe(uri: Uri): Cursor? = + runTracing("query") { + try { + query(uri, METADATA_COLUMNS, null, null) + } catch (e: SecurityException) { + logProviderPermissionWarning(uri, "metadata") + null + } catch (t: Throwable) { + Log.e(ContentPreviewUi.TAG, "Failed to read metadata, uri: $uri", t) + null + } + } + +private fun logProviderPermissionWarning(uri: Uri, dataName: String) { + // The ContentResolver already logs the exception. Log something more informative. + Log.w( + ContentPreviewUi.TAG, + "Could not read $uri $dataName. If a preview is desired, call Intent#setClipData() to" + + " ensure that the sharesheet is given permission." + ) +} diff --git a/java/src/com/android/intentresolver/contentpreview/PreviewViewModel.kt b/java/src/com/android/intentresolver/contentpreview/PreviewViewModel.kt new file mode 100644 index 00000000..331b0cb6 --- /dev/null +++ b/java/src/com/android/intentresolver/contentpreview/PreviewViewModel.kt @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.intentresolver.contentpreview + +import android.app.Application +import androidx.annotation.MainThread +import androidx.lifecycle.ViewModel +import androidx.lifecycle.ViewModelProvider +import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory.Companion.APPLICATION_KEY +import androidx.lifecycle.viewModelScope +import androidx.lifecycle.viewmodel.CreationExtras +import com.android.intentresolver.ChooserRequestParameters +import com.android.intentresolver.R +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.plus + +/** A trivial view model to keep a [PreviewDataProvider] instance over a configuration change */ +class PreviewViewModel(private val application: Application) : BasePreviewViewModel() { + private var previewDataProvider: PreviewDataProvider? = null + private var imageLoader: ImagePreviewImageLoader? = null + + @MainThread + override fun createOrReuseProvider( + chooserRequest: ChooserRequestParameters + ): PreviewDataProvider = + previewDataProvider + ?: PreviewDataProvider(chooserRequest.targetIntent, application.contentResolver).also { + previewDataProvider = it + } + + @MainThread + override fun createOrReuseImageLoader(): ImageLoader = + imageLoader + ?: ImagePreviewImageLoader( + viewModelScope + Dispatchers.IO, + thumbnailSize = + application.resources.getDimensionPixelSize( + R.dimen.chooser_preview_image_max_dimen + ), + application.contentResolver, + cacheSize = 16 + ) + .also { imageLoader = it } + + companion object { + val Factory: ViewModelProvider.Factory = + object : ViewModelProvider.Factory { + @Suppress("UNCHECKED_CAST") + override fun <T : ViewModel> create( + modelClass: Class<T>, + extras: CreationExtras + ): T = PreviewViewModel(checkNotNull(extras[APPLICATION_KEY])) as T + } + } +} diff --git a/java/src/com/android/intentresolver/contentpreview/TextContentPreviewUi.java b/java/src/com/android/intentresolver/contentpreview/TextContentPreviewUi.java index 7901e4cb..c38ed03a 100644 --- a/java/src/com/android/intentresolver/contentpreview/TextContentPreviewUi.java +++ b/java/src/com/android/intentresolver/contentpreview/TextContentPreviewUi.java @@ -16,6 +16,8 @@ package com.android.intentresolver.contentpreview; +import static com.android.intentresolver.util.UriFilters.isOwnedByCurrentUser; + import android.content.res.Resources; import android.net.Uri; import android.text.TextUtils; @@ -25,18 +27,14 @@ import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; -import androidx.annotation.LayoutRes; import androidx.annotation.Nullable; +import androidx.lifecycle.Lifecycle; -import com.android.intentresolver.ImageLoader; import com.android.intentresolver.R; -import com.android.intentresolver.flags.FeatureFlagRepository; import com.android.intentresolver.widget.ActionRow; -import java.util.ArrayList; -import java.util.List; - class TextContentPreviewUi extends ContentPreviewUi { + private final Lifecycle mLifecycle; @Nullable private final CharSequence mSharingText; @Nullable @@ -45,21 +43,23 @@ class TextContentPreviewUi extends ContentPreviewUi { private final Uri mPreviewThumbnail; private final ImageLoader mImageLoader; private final ChooserContentPreviewUi.ActionFactory mActionFactory; - private final FeatureFlagRepository mFeatureFlagRepository; + private final HeadlineGenerator mHeadlineGenerator; TextContentPreviewUi( + Lifecycle lifecycle, @Nullable CharSequence sharingText, @Nullable CharSequence previewTitle, @Nullable Uri previewThumbnail, ChooserContentPreviewUi.ActionFactory actionFactory, ImageLoader imageLoader, - FeatureFlagRepository featureFlagRepository) { + HeadlineGenerator headlineGenerator) { + mLifecycle = lifecycle; mSharingText = sharingText; mPreviewTitle = previewTitle; mPreviewThumbnail = previewThumbnail; mImageLoader = imageLoader; mActionFactory = actionFactory; - mFeatureFlagRepository = featureFlagRepository; + mHeadlineGenerator = headlineGenerator; } @Override @@ -70,69 +70,69 @@ class TextContentPreviewUi extends ContentPreviewUi { @Override public ViewGroup display(Resources resources, LayoutInflater layoutInflater, ViewGroup parent) { ViewGroup layout = displayInternal(layoutInflater, parent); - displayPayloadReselectionAction(layout, mActionFactory, mFeatureFlagRepository); + displayModifyShareAction(layout, mActionFactory); return layout; } private ViewGroup displayInternal( LayoutInflater layoutInflater, ViewGroup parent) { - @LayoutRes int actionRowLayout = getActionRowLayout(mFeatureFlagRepository); ViewGroup contentPreviewLayout = (ViewGroup) layoutInflater.inflate( R.layout.chooser_grid_preview_text, parent, false); - final ActionRow actionRow = inflateActionRow(contentPreviewLayout, actionRowLayout); - if (actionRow != null) { - actionRow.setActions( - createActions( - createTextPreviewActions(), - mActionFactory.createCustomActions(), - mFeatureFlagRepository)); - } + final ActionRow actionRow = + contentPreviewLayout.findViewById(com.android.internal.R.id.chooser_action_row); + actionRow.setActions(mActionFactory.createCustomActions()); if (mSharingText == null) { contentPreviewLayout - .findViewById(com.android.internal.R.id.content_preview_text_layout) + .findViewById(R.id.text_preview_layout) .setVisibility(View.GONE); - } else { - TextView textView = contentPreviewLayout.findViewById( - com.android.internal.R.id.content_preview_text); - textView.setText(mSharingText); + return contentPreviewLayout; } + TextView textView = contentPreviewLayout.findViewById( + com.android.internal.R.id.content_preview_text); + String text = mSharingText.toString(); + + // If we're only previewing one line, then strip out newlines. + if (textView.getMaxLines() == 1) { + text = text.replace("\n", " "); + } + textView.setText(text); + + TextView previewTitleView = contentPreviewLayout.findViewById( + com.android.internal.R.id.content_preview_title); if (TextUtils.isEmpty(mPreviewTitle)) { - contentPreviewLayout - .findViewById(com.android.internal.R.id.content_preview_title_layout) - .setVisibility(View.GONE); + previewTitleView.setVisibility(View.GONE); } else { - TextView previewTitleView = contentPreviewLayout.findViewById( - com.android.internal.R.id.content_preview_title); previewTitleView.setText(mPreviewTitle); - - ImageView previewThumbnailView = contentPreviewLayout.findViewById( - com.android.internal.R.id.content_preview_thumbnail); - if (!validForContentPreview(mPreviewThumbnail)) { - previewThumbnailView.setVisibility(View.GONE); - } else { - mImageLoader.loadImage( - mPreviewThumbnail, - (bitmap) -> updateViewWithImage( - contentPreviewLayout.findViewById( - com.android.internal.R.id.content_preview_thumbnail), - bitmap)); - } } - return contentPreviewLayout; - } + ImageView previewThumbnailView = contentPreviewLayout.findViewById( + com.android.internal.R.id.content_preview_thumbnail); + if (!isOwnedByCurrentUser(mPreviewThumbnail)) { + previewThumbnailView.setVisibility(View.GONE); + } else { + mImageLoader.loadImage( + mLifecycle, + mPreviewThumbnail, + (bitmap) -> updateViewWithImage( + contentPreviewLayout.findViewById( + com.android.internal.R.id.content_preview_thumbnail), + bitmap)); + } - private List<ActionRow.Action> createTextPreviewActions() { - ArrayList<ActionRow.Action> actions = new ArrayList<>(2); - actions.add(mActionFactory.createCopyButton()); - ActionRow.Action nearbyAction = mActionFactory.createNearbyButton(); - if (nearbyAction != null) { - actions.add(nearbyAction); + Runnable onCopy = mActionFactory.getCopyButtonRunnable(); + View copyButton = contentPreviewLayout.findViewById(R.id.copy); + if (onCopy != null) { + copyButton.setOnClickListener((v) -> onCopy.run()); + } else { + copyButton.setVisibility(View.GONE); } - return actions; + + displayHeadline(contentPreviewLayout, mHeadlineGenerator.getTextHeadline(mSharingText)); + + return contentPreviewLayout; } } diff --git a/java/src/com/android/intentresolver/contentpreview/UnifiedContentPreviewUi.java b/java/src/com/android/intentresolver/contentpreview/UnifiedContentPreviewUi.java new file mode 100644 index 00000000..6385f2b6 --- /dev/null +++ b/java/src/com/android/intentresolver/contentpreview/UnifiedContentPreviewUi.java @@ -0,0 +1,151 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.intentresolver.contentpreview; + +import static com.android.intentresolver.contentpreview.ContentPreviewType.CONTENT_PREVIEW_IMAGE; + +import android.content.res.Resources; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import androidx.annotation.Nullable; + +import com.android.intentresolver.R; +import com.android.intentresolver.widget.ActionRow; +import com.android.intentresolver.widget.ImagePreviewView.TransitionElementStatusCallback; +import com.android.intentresolver.widget.ScrollableImagePreviewView; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +class UnifiedContentPreviewUi extends ContentPreviewUi { + private final boolean mShowEditAction; + private final ChooserContentPreviewUi.ActionFactory mActionFactory; + private final ImageLoader mImageLoader; + private final MimeTypeClassifier mTypeClassifier; + private final TransitionElementStatusCallback mTransitionElementStatusCallback; + private final HeadlineGenerator mHeadlineGenerator; + @Nullable + private List<FileInfo> mFiles; + @Nullable + private ViewGroup mContentPreviewView; + + UnifiedContentPreviewUi( + boolean isSingleImage, + ChooserContentPreviewUi.ActionFactory actionFactory, + ImageLoader imageLoader, + MimeTypeClassifier typeClassifier, + TransitionElementStatusCallback transitionElementStatusCallback, + HeadlineGenerator headlineGenerator) { + mShowEditAction = isSingleImage; + mActionFactory = actionFactory; + mImageLoader = imageLoader; + mTypeClassifier = typeClassifier; + mTransitionElementStatusCallback = transitionElementStatusCallback; + mHeadlineGenerator = headlineGenerator; + } + + @Override + public int getType() { + return CONTENT_PREVIEW_IMAGE; + } + + @Override + public ViewGroup display(Resources resources, LayoutInflater layoutInflater, ViewGroup parent) { + ViewGroup layout = displayInternal(layoutInflater, parent); + displayModifyShareAction(layout, mActionFactory); + return layout; + } + + public void setFiles(List<FileInfo> files) { + mImageLoader.prePopulate(files.stream() + .map(FileInfo::getPreviewUri) + .filter(Objects::nonNull) + .toList()); + mFiles = files; + if (mContentPreviewView != null) { + updatePreviewWithFiles(mContentPreviewView, files); + } + } + + private ViewGroup displayInternal(LayoutInflater layoutInflater, ViewGroup parent) { + mContentPreviewView = (ViewGroup) layoutInflater.inflate( + R.layout.chooser_grid_preview_image, parent, false); + + final ActionRow actionRow = + mContentPreviewView.findViewById(com.android.internal.R.id.chooser_action_row); + List<ActionRow.Action> actions = mActionFactory.createCustomActions(); + actionRow.setActions(actions); + + ScrollableImagePreviewView imagePreview = + mContentPreviewView.requireViewById(R.id.scrollable_image_preview); + imagePreview.setOnNoPreviewCallback(() -> imagePreview.setVisibility(View.GONE)); + imagePreview.setTransitionElementStatusCallback(mTransitionElementStatusCallback); + + if (mFiles != null) { + updatePreviewWithFiles(mContentPreviewView, mFiles); + } + + return mContentPreviewView; + } + + private void updatePreviewWithFiles(ViewGroup contentPreviewView, List<FileInfo> files) { + final int count = files.size(); + ScrollableImagePreviewView imagePreview = + contentPreviewView.requireViewById(R.id.scrollable_image_preview); + if (count == 0) { + Log.i( + TAG, + "Attempted to display image preview area with zero" + + " available images detected in EXTRA_STREAM list"); + imagePreview.setVisibility(View.GONE); + mTransitionElementStatusCallback.onAllTransitionElementsReady(); + return; + } + + List<ScrollableImagePreviewView.Preview> previews = new ArrayList<>(); + boolean allImages = true; + boolean allVideos = true; + for (FileInfo fileInfo : files) { + ScrollableImagePreviewView.PreviewType previewType = + getPreviewType(mTypeClassifier, fileInfo.getMimeType()); + allImages = allImages && previewType == ScrollableImagePreviewView.PreviewType.Image; + allVideos = allVideos && previewType == ScrollableImagePreviewView.PreviewType.Video; + + if (fileInfo.getPreviewUri() != null) { + Runnable editAction = + mShowEditAction ? mActionFactory.getEditButtonRunnable() : null; + previews.add( + new ScrollableImagePreviewView.Preview( + previewType, fileInfo.getPreviewUri(), editAction)); + } + } + + imagePreview.setPreviews(previews, count - previews.size(), mImageLoader); + + if (allImages) { + displayHeadline(contentPreviewView, mHeadlineGenerator.getImagesHeadline(count)); + } else if (allVideos) { + displayHeadline(contentPreviewView, mHeadlineGenerator.getVideosHeadline(count)); + } else { + displayHeadline(contentPreviewView, mHeadlineGenerator.getFilesHeadline(count)); + } + } +} diff --git a/java/src/com/android/intentresolver/flags/Flags.kt b/java/src/com/android/intentresolver/flags/Flags.kt index f4dbeddb..b303dd1a 100644 --- a/java/src/com/android/intentresolver/flags/Flags.kt +++ b/java/src/com/android/intentresolver/flags/Flags.kt @@ -16,39 +16,15 @@ package com.android.intentresolver.flags +import com.android.systemui.flags.ReleasedFlag import com.android.systemui.flags.UnreleasedFlag // Flag id, name and namespace should be kept in sync with [com.android.systemui.flags.Flags] to // make the flags available in the flag flipper app (see go/sysui-flags). +// All flags added should be included in UnbundledChooserActivityTest.ALL_FLAGS. object Flags { - const val SHARESHEET_CUSTOM_ACTIONS_NAME = "sharesheet_custom_actions" - const val SHARESHEET_RESELECTION_ACTION_NAME = "sharesheet_reselection_action" - const val SHARESHEET_IMAGE_AND_TEXT_PREVIEW_NAME = "sharesheet_image_text_preview" - const val SHARESHEET_SCROLLABLE_IMAGE_PREVIEW_NAME = "sharesheet_scrollable_image_preview" - - // TODO(b/266983432) Tracking Bug - @JvmField - val SHARESHEET_CUSTOM_ACTIONS = unreleasedFlag( - 1501, SHARESHEET_CUSTOM_ACTIONS_NAME, teamfood = true - ) - - // TODO(b/266982749) Tracking Bug - @JvmField - val SHARESHEET_RESELECTION_ACTION = unreleasedFlag( - 1502, SHARESHEET_RESELECTION_ACTION_NAME, teamfood = true - ) - - // TODO(b/266983474) Tracking Bug - @JvmField - val SHARESHEET_IMAGE_AND_TEXT_PREVIEW = unreleasedFlag( - id = 1503, name = SHARESHEET_IMAGE_AND_TEXT_PREVIEW_NAME, teamfood = true - ) - - // TODO(b/267355521) Tracking Bug - @JvmField - val SHARESHEET_SCROLLABLE_IMAGE_PREVIEW = unreleasedFlag( - 1504, SHARESHEET_SCROLLABLE_IMAGE_PREVIEW_NAME, teamfood = true - ) + private fun releasedFlag(id: Int, name: String) = + ReleasedFlag(id, name, "systemui") private fun unreleasedFlag(id: Int, name: String, teamfood: Boolean = false) = UnreleasedFlag(id, name, "systemui", teamfood) diff --git a/java/src/com/android/intentresolver/grid/ChooserGridAdapter.java b/java/src/com/android/intentresolver/grid/ChooserGridAdapter.java index 1cf59316..77ae20f5 100644 --- a/java/src/com/android/intentresolver/grid/ChooserGridAdapter.java +++ b/java/src/com/android/intentresolver/grid/ChooserGridAdapter.java @@ -58,7 +58,7 @@ public final class ChooserGridAdapter extends RecyclerView.Adapter<RecyclerView. /** * Injectable interface for any considerations that should be delegated to other components - * in the {@link ChooserActivity}. + * in the {@link com.android.intentresolver.ChooserActivity}. * TODO: determine whether any of these methods return parameters that can safely be * precomputed; whether any should be converted to `ChooserGridAdapter` setters to be * invoked by external callbacks; and whether any reflect requirements that should be moved @@ -89,26 +89,6 @@ public final class ChooserGridAdapter extends RecyclerView.Adapter<RecyclerView. * behaviors on this view. */ void updateProfileViewButton(View newButtonFromProfileRow); - - /** - * @return the number of "valid" targets in the active list adapter. - * TODO: define "valid." - */ - int getValidTargetCount(); - - /** - * Request that the client update our {@code directShareGroup} to match their desired - * state for the "expansion" UI. - */ - void updateDirectShareExpansion(DirectShareViewHolder directShareGroup); - - /** - * Request that the client handle a scroll event that should be taken as expanding the - * provided {@code directShareGroup}. Note that this currently never happens due to a - * hard-coded condition in {@link #canExpandDirectShare()}. - */ - void handleScrollToExpandDirectShare( - DirectShareViewHolder directShareGroup, int y, int oldy); } private static final int VIEW_TYPE_DIRECT_SHARE = 0; @@ -119,8 +99,6 @@ public final class ChooserGridAdapter extends RecyclerView.Adapter<RecyclerView. private static final int VIEW_TYPE_CALLER_AND_RANK = 5; private static final int VIEW_TYPE_FOOTER = 6; - private static final int NUM_EXPANSIONS_TO_HIDE_AZ_LABEL = 20; - private final ChooserActivityDelegate mChooserActivityDelegate; private final ChooserListAdapter mChooserListAdapter; private final LayoutInflater mLayoutInflater; @@ -129,20 +107,19 @@ public final class ChooserGridAdapter extends RecyclerView.Adapter<RecyclerView. private final boolean mShouldShowContentPreview; private final int mChooserWidthPixels; private final int mChooserRowTextOptionTranslatePixelSize; - private final boolean mShowAzLabelIfPoss; - private DirectShareViewHolder mDirectShareViewHolder; private int mChooserTargetWidth = 0; private int mFooterHeight = 0; + private boolean mAzLabelVisibility = false; + public ChooserGridAdapter( Context context, ChooserActivityDelegate chooserActivityDelegate, ChooserListAdapter wrappedAdapter, boolean shouldShowContentPreview, - int maxTargetsPerRow, - int numSheetExpansions) { + int maxTargetsPerRow) { super(); mChooserActivityDelegate = chooserActivityDelegate; @@ -157,8 +134,6 @@ public final class ChooserGridAdapter extends RecyclerView.Adapter<RecyclerView. mChooserRowTextOptionTranslatePixelSize = context.getResources().getDimensionPixelSize( R.dimen.chooser_row_text_option_translate); - mShowAzLabelIfPoss = numSheetExpansions < NUM_EXPANSIONS_TO_HIDE_AZ_LABEL; - wrappedAdapter.registerDataSetObserver(new DataSetObserver() { @Override public void onChanged() { @@ -190,8 +165,7 @@ public final class ChooserGridAdapter extends RecyclerView.Adapter<RecyclerView. } // Limit width to the maximum width of the chooser activity - int maxWidth = mChooserWidthPixels; - width = Math.min(maxWidth, width); + width = Math.min(mChooserWidthPixels, width); int newWidth = width / mMaxTargetsPerRow; if (newWidth != mChooserTargetWidth) { @@ -265,20 +239,30 @@ public final class ChooserGridAdapter extends RecyclerView.Adapter<RecyclerView. public int getAzLabelRowCount() { // Only show a label if the a-z list is showing - return (mShowAzLabelIfPoss && mChooserListAdapter.getAlphaTargetCount() > 0) ? 1 : 0; + return (mChooserListAdapter.getAlphaTargetCount() > 0) ? 1 : 0; + } + + private int getAzLabelRowPosition() { + int azRowCount = getAzLabelRowCount(); + if (azRowCount == 0) { + return -1; + } + + return getSystemRowCount() + + getProfileRowCount() + + getServiceTargetRowCount() + + getCallerAndRankedTargetRowCount(); } @Override public int getItemCount() { - return (int) ( - getSystemRowCount() - + getProfileRowCount() - + getServiceTargetRowCount() - + getCallerAndRankedTargetRowCount() - + getAzLabelRowCount() - + mChooserListAdapter.getAlphaTargetCount() - + getFooterRowCount() - ); + return getSystemRowCount() + + getProfileRowCount() + + getServiceTargetRowCount() + + getCallerAndRankedTargetRowCount() + + getAzLabelRowCount() + + mChooserListAdapter.getAlphaTargetCount() + + getFooterRowCount(); } @Override @@ -322,8 +306,26 @@ public final class ChooserGridAdapter extends RecyclerView.Adapter<RecyclerView. } } + /** + * Set the app divider's visibility, when it's present. + */ + public void setAzLabelVisibility(boolean isVisible) { + if (mAzLabelVisibility == isVisible) { + return; + } + mAzLabelVisibility = isVisible; + int azRowPos = getAzLabelRowPosition(); + if (azRowPos >= 0) { + notifyItemChanged(azRowPos); + } + } + @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { + if (holder.getItemViewType() == VIEW_TYPE_AZ_LABEL) { + holder.itemView.setVisibility( + mAzLabelVisibility ? View.VISIBLE : View.INVISIBLE); + } int viewType = ((ViewHolderBase) holder).getViewType(); switch (viewType) { case VIEW_TYPE_DIRECT_SHARE: @@ -453,12 +455,11 @@ public final class ChooserGridAdapter extends RecyclerView.Adapter<RecyclerView. parentGroup.addView(row1); parentGroup.addView(row2); - mDirectShareViewHolder = new DirectShareViewHolder(parentGroup, - Lists.newArrayList(row1, row2), mMaxTargetsPerRow, viewType, - mChooserActivityDelegate::getValidTargetCount); - loadViewsIntoGroup(mDirectShareViewHolder); + DirectShareViewHolder directShareViewHolder = new DirectShareViewHolder(parentGroup, + Lists.newArrayList(row1, row2), mMaxTargetsPerRow, viewType); + loadViewsIntoGroup(directShareViewHolder); - return mDirectShareViewHolder; + return directShareViewHolder; } else { ViewGroup row = (ViewGroup) mLayoutInflater.inflate( R.layout.chooser_row, parent, false); @@ -572,21 +573,6 @@ public final class ChooserGridAdapter extends RecyclerView.Adapter<RecyclerView. return callerAndRankedCount + serviceCount + position; } - public void handleScroll(View v, int y, int oldy) { - boolean canExpandDirectShare = canExpandDirectShare(); - if (mDirectShareViewHolder != null && canExpandDirectShare) { - mChooserActivityDelegate.handleScrollToExpandDirectShare( - mDirectShareViewHolder, y, oldy); - } - } - - /** Only expand direct share area if there is a minimum number of targets. */ - private boolean canExpandDirectShare() { - // Do not enable until we have confirmed more apps are using sharing shortcuts - // Check git history for enablement logic - return false; - } - public ChooserListAdapter getListAdapter() { return mChooserListAdapter; } @@ -594,11 +580,4 @@ public final class ChooserGridAdapter extends RecyclerView.Adapter<RecyclerView. public boolean shouldCellSpan(int position) { return getItemViewType(position) == VIEW_TYPE_NORMAL; } - - public void updateDirectShareExpansion() { - if (mDirectShareViewHolder == null || !canExpandDirectShare()) { - return; - } - mChooserActivityDelegate.updateDirectShareExpansion(mDirectShareViewHolder); - } } diff --git a/java/src/com/android/intentresolver/grid/DirectShareViewHolder.java b/java/src/com/android/intentresolver/grid/DirectShareViewHolder.java index 316c9f07..ad78c719 100644 --- a/java/src/com/android/intentresolver/grid/DirectShareViewHolder.java +++ b/java/src/com/android/intentresolver/grid/DirectShareViewHolder.java @@ -25,35 +25,25 @@ import android.view.View.MeasureSpec; import android.view.ViewGroup; import android.view.animation.AccelerateInterpolator; -import androidx.recyclerview.widget.RecyclerView; - -import com.android.intentresolver.ChooserActivity; - import java.util.Arrays; import java.util.List; -import java.util.function.Supplier; /** Holder for direct share targets in the {@link ChooserGridAdapter}. */ public class DirectShareViewHolder extends ItemGroupViewHolder { private final ViewGroup mParent; private final List<ViewGroup> mRows; - private int mCellCountPerRow; + private final int mCellCountPerRow; - private boolean mHideDirectShareExpansion = false; private int mDirectShareMinHeight = 0; private int mDirectShareCurrHeight = 0; - private int mDirectShareMaxHeight = 0; private final boolean[] mCellVisibility; - private final Supplier<Integer> mDeferredTargetCountSupplier; - public DirectShareViewHolder( ViewGroup parent, List<ViewGroup> rows, int cellCountPerRow, - int viewType, - Supplier<Integer> deferredTargetCountSupplier) { + int viewType) { super(rows.size() * cellCountPerRow, parent, viewType); this.mParent = parent; @@ -61,7 +51,6 @@ public class DirectShareViewHolder extends ItemGroupViewHolder { this.mCellCountPerRow = cellCountPerRow; this.mCellVisibility = new boolean[rows.size() * cellCountPerRow]; Arrays.fill(mCellVisibility, true); - this.mDeferredTargetCountSupplier = deferredTargetCountSupplier; } public ViewGroup addView(int index, View v) { @@ -92,7 +81,6 @@ public class DirectShareViewHolder extends ItemGroupViewHolder { mDirectShareMinHeight = getRow(0).getMeasuredHeight(); mDirectShareCurrHeight = (mDirectShareCurrHeight > 0) ? mDirectShareCurrHeight : mDirectShareMinHeight; - mDirectShareMaxHeight = 2 * mDirectShareMinHeight; } public int getMeasuredRowHeight() { @@ -123,75 +111,4 @@ public class DirectShareViewHolder extends ItemGroupViewHolder { fadeAnim.start(); } } - - public void handleScroll(RecyclerView view, int y, int oldy, int maxTargetsPerRow) { - // only exit early if fully collapsed, otherwise onListRebuilt() with shifting - // targets can lock us into an expanded mode - boolean notExpanded = mDirectShareCurrHeight == mDirectShareMinHeight; - if (notExpanded) { - if (mHideDirectShareExpansion) { - return; - } - - // only expand if we have more than maxTargetsPerRow, and delay that decision - // until they start to scroll - final int validTargets = this.mDeferredTargetCountSupplier.get(); - if (validTargets <= maxTargetsPerRow) { - mHideDirectShareExpansion = true; - return; - } - } - - int yDiff = (int) ((oldy - y) * ChooserActivity.DIRECT_SHARE_EXPANSION_RATE); - - int prevHeight = mDirectShareCurrHeight; - int newHeight = Math.min(prevHeight + yDiff, mDirectShareMaxHeight); - newHeight = Math.max(newHeight, mDirectShareMinHeight); - yDiff = newHeight - prevHeight; - - updateDirectShareRowHeight(view, yDiff, newHeight); - } - - public void expand(RecyclerView view) { - updateDirectShareRowHeight( - view, mDirectShareMaxHeight - mDirectShareCurrHeight, mDirectShareMaxHeight); - } - - public void collapse(RecyclerView view) { - updateDirectShareRowHeight( - view, mDirectShareMinHeight - mDirectShareCurrHeight, mDirectShareMinHeight); - } - - private void updateDirectShareRowHeight(RecyclerView view, int yDiff, int newHeight) { - if (view == null || view.getChildCount() == 0 || yDiff == 0) { - return; - } - - // locate the item to expand, and offset the rows below that one - boolean foundExpansion = false; - for (int i = 0; i < view.getChildCount(); i++) { - View child = view.getChildAt(i); - - if (foundExpansion) { - child.offsetTopAndBottom(yDiff); - } else { - if (child.getTag() != null && child.getTag() instanceof DirectShareViewHolder) { - int widthSpec = MeasureSpec.makeMeasureSpec(child.getWidth(), - MeasureSpec.EXACTLY); - int heightSpec = MeasureSpec.makeMeasureSpec(newHeight, - MeasureSpec.EXACTLY); - child.measure(widthSpec, heightSpec); - child.getLayoutParams().height = child.getMeasuredHeight(); - child.layout(child.getLeft(), child.getTop(), child.getRight(), - child.getTop() + child.getMeasuredHeight()); - - foundExpansion = true; - } - } - } - - if (foundExpansion) { - mDirectShareCurrHeight = newHeight; - } - } } diff --git a/java/src/com/android/intentresolver/icons/BaseLoadIconTask.java b/java/src/com/android/intentresolver/icons/BaseLoadIconTask.java new file mode 100644 index 00000000..2eceb89c --- /dev/null +++ b/java/src/com/android/intentresolver/icons/BaseLoadIconTask.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.intentresolver.icons; + +import android.content.Context; +import android.graphics.drawable.Drawable; +import android.os.AsyncTask; + +import com.android.intentresolver.R; +import com.android.intentresolver.TargetPresentationGetter; + +import java.util.function.Consumer; + +abstract class BaseLoadIconTask extends AsyncTask<Void, Void, Drawable> { + protected final Context mContext; + protected final TargetPresentationGetter.Factory mPresentationFactory; + private final Consumer<Drawable> mCallback; + + BaseLoadIconTask( + Context context, + TargetPresentationGetter.Factory presentationFactory, + Consumer<Drawable> callback) { + mContext = context; + mPresentationFactory = presentationFactory; + mCallback = callback; + } + + protected final Drawable loadIconPlaceholder() { + return mContext.getDrawable(R.drawable.resolver_icon_placeholder); + } + + @Override + protected final void onPostExecute(Drawable d) { + mCallback.accept(d); + } +} diff --git a/java/src/com/android/intentresolver/icons/DefaultTargetDataLoader.kt b/java/src/com/android/intentresolver/icons/DefaultTargetDataLoader.kt new file mode 100644 index 00000000..0e4d0209 --- /dev/null +++ b/java/src/com/android/intentresolver/icons/DefaultTargetDataLoader.kt @@ -0,0 +1,127 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.intentresolver.icons + +import android.app.ActivityManager +import android.content.Context +import android.content.pm.ResolveInfo +import android.graphics.drawable.Drawable +import android.os.AsyncTask +import android.os.UserHandle +import android.util.SparseArray +import androidx.annotation.GuardedBy +import androidx.lifecycle.DefaultLifecycleObserver +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.LifecycleOwner +import com.android.intentresolver.TargetPresentationGetter +import com.android.intentresolver.chooser.DisplayResolveInfo +import com.android.intentresolver.chooser.SelectableTargetInfo +import java.util.concurrent.atomic.AtomicInteger +import java.util.function.Consumer +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.asExecutor + +/** An actual [TargetDataLoader] implementation. */ +// TODO: replace async tasks with coroutines. +class DefaultTargetDataLoader( + private val context: Context, + private val lifecycle: Lifecycle, + private val isAudioCaptureDevice: Boolean, +) : TargetDataLoader() { + private val presentationFactory = + TargetPresentationGetter.Factory( + context, + context.getSystemService(ActivityManager::class.java)?.launcherLargeIconDensity + ?: error("Unable to access ActivityManager") + ) + private val nextTaskId = AtomicInteger(0) + @GuardedBy("self") private val activeTasks = SparseArray<AsyncTask<*, *, *>>() + private val executor = Dispatchers.IO.asExecutor() + + init { + lifecycle.addObserver( + object : DefaultLifecycleObserver { + override fun onDestroy(owner: LifecycleOwner) { + lifecycle.removeObserver(this) + destroy() + } + } + ) + } + + override fun loadAppTargetIcon( + info: DisplayResolveInfo, + userHandle: UserHandle, + callback: Consumer<Drawable>, + ) { + val taskId = nextTaskId.getAndIncrement() + LoadIconTask(context, info, userHandle, presentationFactory) { result -> + removeTask(taskId) + callback.accept(result) + } + .also { addTask(taskId, it) } + .executeOnExecutor(executor) + } + + override fun loadDirectShareIcon( + info: SelectableTargetInfo, + userHandle: UserHandle, + callback: Consumer<Drawable>, + ) { + val taskId = nextTaskId.getAndIncrement() + LoadDirectShareIconTask( + context.createContextAsUser(userHandle, 0), + info, + presentationFactory, + ) { result -> + removeTask(taskId) + callback.accept(result) + } + .also { addTask(taskId, it) } + .executeOnExecutor(executor) + } + + override fun loadLabel(info: DisplayResolveInfo, callback: Consumer<Array<CharSequence?>>) { + val taskId = nextTaskId.getAndIncrement() + LoadLabelTask(context, info, isAudioCaptureDevice, presentationFactory) { result -> + removeTask(taskId) + callback.accept(result) + } + .also { addTask(taskId, it) } + .executeOnExecutor(executor) + } + + override fun createPresentationGetter(info: ResolveInfo): TargetPresentationGetter = + presentationFactory.makePresentationGetter(info) + + private fun addTask(id: Int, task: AsyncTask<*, *, *>) { + synchronized(activeTasks) { activeTasks.put(id, task) } + } + + private fun removeTask(id: Int) { + synchronized(activeTasks) { activeTasks.remove(id) } + } + + private fun destroy() { + synchronized(activeTasks) { + for (i in 0 until activeTasks.size()) { + activeTasks.valueAt(i).cancel(false) + } + activeTasks.clear() + } + } +} diff --git a/java/src/com/android/intentresolver/icons/LoadDirectShareIconTask.java b/java/src/com/android/intentresolver/icons/LoadDirectShareIconTask.java new file mode 100644 index 00000000..6aee69b5 --- /dev/null +++ b/java/src/com/android/intentresolver/icons/LoadDirectShareIconTask.java @@ -0,0 +1,131 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.intentresolver.icons; + +import android.annotation.Nullable; +import android.content.ComponentName; +import android.content.Context; +import android.content.pm.ActivityInfo; +import android.content.pm.LauncherApps; +import android.content.pm.PackageManager; +import android.content.pm.ShortcutInfo; +import android.graphics.Bitmap; +import android.graphics.drawable.BitmapDrawable; +import android.graphics.drawable.Drawable; +import android.graphics.drawable.Icon; +import android.os.Trace; +import android.util.Log; + +import androidx.annotation.WorkerThread; + +import com.android.intentresolver.SimpleIconFactory; +import com.android.intentresolver.TargetPresentationGetter; +import com.android.intentresolver.chooser.SelectableTargetInfo; +import com.android.intentresolver.util.UriFilters; + +import java.util.function.Consumer; + +/** + * Loads direct share targets icons. + */ +class LoadDirectShareIconTask extends BaseLoadIconTask { + private static final String TAG = "DirectShareIconTask"; + private final SelectableTargetInfo mTargetInfo; + + LoadDirectShareIconTask( + Context context, + SelectableTargetInfo targetInfo, + TargetPresentationGetter.Factory presentationFactory, + Consumer<Drawable> callback) { + super(context, presentationFactory, callback); + mTargetInfo = targetInfo; + } + + @Override + protected Drawable doInBackground(Void... voids) { + Drawable drawable; + Trace.beginSection("shortcut-icon"); + try { + final Icon icon = mTargetInfo.getChooserTargetIcon(); + if (icon == null || UriFilters.hasValidIcon(icon)) { + drawable = getChooserTargetIconDrawable( + mContext, + icon, + mTargetInfo.getChooserTargetComponentName(), + mTargetInfo.getDirectShareShortcutInfo()); + } else { + Log.e(TAG, "Failed to load shortcut icon for " + + mTargetInfo.getChooserTargetComponentName() + "; no access"); + drawable = loadIconPlaceholder(); + } + } catch (Exception e) { + Log.e( + TAG, + "Failed to load shortcut icon for " + + mTargetInfo.getChooserTargetComponentName(), + e); + drawable = loadIconPlaceholder(); + } finally { + Trace.endSection(); + } + return drawable; + } + + @WorkerThread + private Drawable getChooserTargetIconDrawable( + Context context, + @Nullable Icon icon, + ComponentName targetComponentName, + @Nullable ShortcutInfo shortcutInfo) { + Drawable directShareIcon = null; + + // First get the target drawable and associated activity info + if (icon != null) { + directShareIcon = icon.loadDrawable(context); + } else if (shortcutInfo != null) { + LauncherApps launcherApps = context.getSystemService(LauncherApps.class); + if (launcherApps != null) { + directShareIcon = launcherApps.getShortcutIconDrawable(shortcutInfo, 0); + } + } + + if (directShareIcon == null) { + return null; + } + + ActivityInfo info = null; + try { + info = context.getPackageManager().getActivityInfo(targetComponentName, 0); + } catch (PackageManager.NameNotFoundException error) { + Log.e(TAG, "Could not find activity associated with ChooserTarget"); + } + + if (info == null) { + return null; + } + + // Now fetch app icon and raster with no badging even in work profile + Bitmap appIcon = mPresentationFactory.makePresentationGetter(info).getIconBitmap(null); + + // Raster target drawable with appIcon as a badge + SimpleIconFactory sif = SimpleIconFactory.obtain(context); + Bitmap directShareBadgedIcon = sif.createAppBadgedIconBitmap(directShareIcon, appIcon); + sif.recycle(); + + return new BitmapDrawable(context.getResources(), directShareBadgedIcon); + } +} diff --git a/java/src/com/android/intentresolver/icons/LoadIconTask.java b/java/src/com/android/intentresolver/icons/LoadIconTask.java new file mode 100644 index 00000000..37ce4093 --- /dev/null +++ b/java/src/com/android/intentresolver/icons/LoadIconTask.java @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.intentresolver.icons; + +import android.content.ComponentName; +import android.content.Context; +import android.content.pm.ResolveInfo; +import android.graphics.drawable.Drawable; +import android.os.Trace; +import android.os.UserHandle; +import android.util.Log; + +import com.android.intentresolver.ResolverActivity; +import com.android.intentresolver.TargetPresentationGetter; +import com.android.intentresolver.chooser.DisplayResolveInfo; + +import java.util.function.Consumer; + +class LoadIconTask extends BaseLoadIconTask { + private static final String TAG = "IconTask"; + protected final DisplayResolveInfo mDisplayResolveInfo; + private final UserHandle mUserHandle; + private final ResolveInfo mResolveInfo; + + LoadIconTask( + Context context, DisplayResolveInfo dri, + UserHandle userHandle, + TargetPresentationGetter.Factory presentationFactory, + Consumer<Drawable> callback) { + super(context, presentationFactory, callback); + mUserHandle = userHandle; + mDisplayResolveInfo = dri; + mResolveInfo = dri.getResolveInfo(); + } + + @Override + protected Drawable doInBackground(Void... params) { + Trace.beginSection("app-icon"); + try { + return loadIconForResolveInfo(mResolveInfo); + } catch (Exception e) { + ComponentName componentName = mDisplayResolveInfo.getResolvedComponentName(); + Log.e(TAG, "Failed to load app icon for " + componentName, e); + return loadIconPlaceholder(); + } finally { + Trace.endSection(); + } + } + + protected final Drawable loadIconForResolveInfo(ResolveInfo ri) { + // Load icons based on userHandle from ResolveInfo. If in work profile/clone profile, icons + // should be badged. + return mPresentationFactory.makePresentationGetter(ri) + .getIcon(ResolverActivity.getResolveInfoUserHandle(ri, mUserHandle)); + } + +} diff --git a/java/src/com/android/intentresolver/icons/LoadLabelTask.java b/java/src/com/android/intentresolver/icons/LoadLabelTask.java new file mode 100644 index 00000000..a0867b8e --- /dev/null +++ b/java/src/com/android/intentresolver/icons/LoadLabelTask.java @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.intentresolver.icons; + +import android.content.Context; +import android.content.PermissionChecker; +import android.content.pm.ActivityInfo; +import android.os.AsyncTask; +import android.os.Trace; + +import com.android.intentresolver.R; +import com.android.intentresolver.TargetPresentationGetter; +import com.android.intentresolver.chooser.DisplayResolveInfo; + +import java.util.function.Consumer; + +class LoadLabelTask extends AsyncTask<Void, Void, CharSequence[]> { + private final Context mContext; + private final DisplayResolveInfo mDisplayResolveInfo; + private final boolean mIsAudioCaptureDevice; + protected final TargetPresentationGetter.Factory mPresentationFactory; + private final Consumer<CharSequence[]> mCallback; + + LoadLabelTask(Context context, DisplayResolveInfo dri, + boolean isAudioCaptureDevice, TargetPresentationGetter.Factory presentationFactory, + Consumer<CharSequence[]> callback) { + mContext = context; + mDisplayResolveInfo = dri; + mIsAudioCaptureDevice = isAudioCaptureDevice; + mPresentationFactory = presentationFactory; + mCallback = callback; + } + + @Override + protected CharSequence[] doInBackground(Void... voids) { + try { + Trace.beginSection("app-label"); + return loadLabel(); + } finally { + Trace.endSection(); + } + } + + private CharSequence[] loadLabel() { + TargetPresentationGetter pg = mPresentationFactory.makePresentationGetter( + mDisplayResolveInfo.getResolveInfo()); + + if (mIsAudioCaptureDevice) { + // This is an audio capture device, so check record permissions + ActivityInfo activityInfo = mDisplayResolveInfo.getResolveInfo().activityInfo; + String packageName = activityInfo.packageName; + + int uid = activityInfo.applicationInfo.uid; + boolean hasRecordPermission = + PermissionChecker.checkPermissionForPreflight( + mContext, + android.Manifest.permission.RECORD_AUDIO, -1, uid, + packageName) + == android.content.pm.PackageManager.PERMISSION_GRANTED; + + if (!hasRecordPermission) { + // Doesn't have record permission, so warn the user + return new CharSequence[]{ + pg.getLabel(), + mContext.getString(R.string.usb_device_resolve_prompt_warn) + }; + } + } + + return new CharSequence[]{ + pg.getLabel(), + pg.getSubLabel() + }; + } + + @Override + protected void onPostExecute(CharSequence[] result) { + mCallback.accept(result); + } +} diff --git a/java/src/com/android/intentresolver/icons/TargetDataLoader.kt b/java/src/com/android/intentresolver/icons/TargetDataLoader.kt new file mode 100644 index 00000000..50f731f8 --- /dev/null +++ b/java/src/com/android/intentresolver/icons/TargetDataLoader.kt @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.intentresolver.icons + +import android.content.pm.ResolveInfo +import android.graphics.drawable.Drawable +import android.os.UserHandle +import com.android.intentresolver.TargetPresentationGetter +import com.android.intentresolver.chooser.DisplayResolveInfo +import com.android.intentresolver.chooser.SelectableTargetInfo +import java.util.function.Consumer + +/** A target data loader contract. Added to support testing. */ +abstract class TargetDataLoader { + /** Load an app target icon */ + abstract fun loadAppTargetIcon( + info: DisplayResolveInfo, + userHandle: UserHandle, + callback: Consumer<Drawable>, + ) + + /** Load a shortcut icon */ + abstract fun loadDirectShareIcon( + info: SelectableTargetInfo, + userHandle: UserHandle, + callback: Consumer<Drawable>, + ) + + /** Load target label */ + abstract fun loadLabel(info: DisplayResolveInfo, callback: Consumer<Array<CharSequence?>>) + + /** Create a presentation getter to be used with a [DisplayResolveInfo] */ + // TODO: get rid of DisplayResolveInfo's dependency on the presentation getter and remove this + // method. + abstract fun createPresentationGetter(info: ResolveInfo): TargetPresentationGetter +} diff --git a/java/src/com/android/intentresolver/measurements/Tracer.kt b/java/src/com/android/intentresolver/measurements/Tracer.kt new file mode 100644 index 00000000..5f69932a --- /dev/null +++ b/java/src/com/android/intentresolver/measurements/Tracer.kt @@ -0,0 +1,155 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.intentresolver.measurements + +import android.os.SystemClock +import android.os.Trace +import android.os.UserHandle +import android.util.SparseArray +import androidx.annotation.GuardedBy +import java.util.concurrent.atomic.AtomicInteger +import java.util.concurrent.atomic.AtomicLong + +private const val SECTION_LAUNCH_TO_SHORTCUT = "launch-to-shortcut" +private const val SECTION_APP_PREDICTOR_PREFIX = "app-predictor-" +private const val SECTION_APP_TARGET_PREFIX = "app-target-" + +object Tracer { + private val launchToFirstShortcut = AtomicLong(-1L) + private val nextId = AtomicInteger(0) + @GuardedBy("self") private val profileRecords = SparseArray<ProfileRecord>() + + fun markLaunched() { + if (launchToFirstShortcut.compareAndSet(-1, elapsedTimeNow())) { + Trace.beginAsyncSection(SECTION_LAUNCH_TO_SHORTCUT, 1) + } + } + + fun endLaunchToShortcutTrace(): Long { + val time = elapsedTimeNow() + val startTime = launchToFirstShortcut.get() + return if (startTime >= 0 && launchToFirstShortcut.compareAndSet(startTime, -1L)) { + Trace.endAsyncSection(SECTION_LAUNCH_TO_SHORTCUT, 1) + time - startTime + } else { + -1L + } + } + + /** + * Begin shortcuts request tracing. The logic is based on an assumption that each request for + * shortcuts update is followed by at least one response. Note, that it is not always measure + * the request duration correctly as in the case of a two overlapping requests when the second + * requests starts and ends while the first is running, the end of the second request will be + * attributed to the first. This is tolerable as this still represents the visible to the user + * app's behavior and expected to be quite rare. + */ + fun beginAppPredictorQueryTrace(userHandle: UserHandle) { + val queue = getUserShortcutRequestQueue(userHandle, createIfMissing = true) ?: return + val startTime = elapsedTimeNow() + val id = nextId.getAndIncrement() + val sectionName = userHandle.toAppPredictorSectionName() + synchronized(queue) { + Trace.beginAsyncSection(sectionName, id) + queue.addFirst(longArrayOf(startTime, id.toLong())) + } + } + + /** + * End shortcut request tracing, see [beginAppPredictorQueryTrace]. + * + * @return request duration is milliseconds. + */ + fun endAppPredictorQueryTrace(userHandle: UserHandle): Long { + val queue = getUserShortcutRequestQueue(userHandle, createIfMissing = false) ?: return -1L + val endTime = elapsedTimeNow() + val sectionName = userHandle.toAppPredictorSectionName() + return synchronized(queue) { queue.removeLastOrNull() } + ?.let { record -> + Trace.endAsyncSection(sectionName, record[1].toInt()) + endTime - record[0] + } + ?: -1L + } + + /** + * Trace app target loading section per profile. If there's already an active section, it will + * be ended an a new section started. + */ + fun beginAppTargetLoadingSection(userHandle: UserHandle) { + val profile = getProfileRecord(userHandle, createIfMissing = true) ?: return + val sectionName = userHandle.toAppTargetSectionName() + val time = elapsedTimeNow() + synchronized(profile) { + if (profile.appTargetLoading >= 0) { + Trace.endAsyncSection(sectionName, 0) + } + profile.appTargetLoading = time + Trace.beginAsyncSection(sectionName, 0) + } + } + + fun endAppTargetLoadingSection(userHandle: UserHandle): Long { + val profile = getProfileRecord(userHandle, createIfMissing = false) ?: return -1L + val time = elapsedTimeNow() + val sectionName = userHandle.toAppTargetSectionName() + return synchronized(profile) { + if (profile.appTargetLoading >= 0) { + Trace.endAsyncSection(sectionName, 0) + (time - profile.appTargetLoading).also { profile.appTargetLoading = -1L } + } else { + -1L + } + } + } + + private fun getUserShortcutRequestQueue( + userHandle: UserHandle, + createIfMissing: Boolean + ): ArrayDeque<LongArray>? = getProfileRecord(userHandle, createIfMissing)?.appPredictorRequests + + private fun getProfileRecord(userHandle: UserHandle, createIfMissing: Boolean): ProfileRecord? = + synchronized(profileRecords) { + val idx = profileRecords.indexOfKey(userHandle.identifier) + when { + idx >= 0 -> profileRecords.valueAt(idx) + createIfMissing -> + ProfileRecord().also { profileRecords.put(userHandle.identifier, it) } + else -> null + } + } + + private fun elapsedTimeNow() = SystemClock.elapsedRealtime() +} + +private class ProfileRecord { + val appPredictorRequests = ArrayDeque<LongArray>() + @GuardedBy("this") var appTargetLoading = -1L +} + +private fun UserHandle.toAppPredictorSectionName() = SECTION_APP_PREDICTOR_PREFIX + identifier + +private fun UserHandle.toAppTargetSectionName() = SECTION_APP_TARGET_PREFIX + identifier + +inline fun <R> runTracing(name: String, block: () -> R): R { + Trace.beginSection(name) + try { + return block() + } finally { + Trace.endSection() + } +} diff --git a/java/src/com/android/intentresolver/model/AbstractResolverComparator.java b/java/src/com/android/intentresolver/model/AbstractResolverComparator.java index ea767568..bc54e01e 100644 --- a/java/src/com/android/intentresolver/model/AbstractResolverComparator.java +++ b/java/src/com/android/intentresolver/model/AbstractResolverComparator.java @@ -16,6 +16,7 @@ package com.android.intentresolver.model; +import android.annotation.Nullable; import android.app.usage.UsageStatsManager; import android.content.ComponentName; import android.content.Context; @@ -32,11 +33,14 @@ import android.util.Log; import com.android.intentresolver.ChooserActivityLogger; import com.android.intentresolver.ResolvedComponentInfo; import com.android.intentresolver.ResolverActivity; +import com.android.intentresolver.chooser.TargetInfo; import java.text.Collator; import java.util.ArrayList; import java.util.Comparator; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * Used to sort resolved activities in {@link ResolverListController}. @@ -50,10 +54,11 @@ public abstract class AbstractResolverComparator implements Comparator<ResolvedC private static final String TAG = "AbstractResolverComp"; protected Runnable mAfterCompute; - protected final PackageManager mPm; - protected final UsageStatsManager mUsm; + protected final Map<UserHandle, PackageManager> mPmMap = new HashMap<>(); + protected final Map<UserHandle, UsageStatsManager> mUsmMap = new HashMap<>(); protected String[] mAnnotations; protected String mContentType; + protected final ComponentName mPromoteToFirst; // True if the current share is a link. private final boolean mHttp; @@ -100,14 +105,35 @@ public abstract class AbstractResolverComparator implements Comparator<ResolvedC } }; - public AbstractResolverComparator(Context context, Intent intent) { + /** + * Constructor to initialize the comparator. + * @param launchedFromContext the activity calling this comparator + * @param intent original intent + * @param resolvedActivityUserSpaceList refers to the userSpace(s) used by the comparator for + * fetching activity stats and recording activity + * selection. The latter could be different from the + * userSpace provided by context. + * @param promoteToFirst a component to be moved to the front of the app list if it's being + * ranked. Unlike pinned apps, this cannot be modified by the user. + */ + public AbstractResolverComparator( + Context launchedFromContext, + Intent intent, + List<UserHandle> resolvedActivityUserSpaceList, + @Nullable ComponentName promoteToFirst) { String scheme = intent.getScheme(); mHttp = "http".equals(scheme) || "https".equals(scheme); mContentType = intent.getType(); getContentAnnotations(intent); - mPm = context.getPackageManager(); - mUsm = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE); - mAzComparator = new AzInfoComparator(context); + for (UserHandle user : resolvedActivityUserSpaceList) { + Context userContext = launchedFromContext.createContextAsUser(user, 0); + mPmMap.put(user, userContext.getPackageManager()); + mUsmMap.put( + user, + (UsageStatsManager) userContext.getSystemService(Context.USAGE_STATS_SERVICE)); + } + mAzComparator = new AzInfoComparator(launchedFromContext); + mPromoteToFirst = promoteToFirst; } // get annotations of content from intent. @@ -163,6 +189,16 @@ public abstract class AbstractResolverComparator implements Comparator<ResolvedC return -1; } + if (mPromoteToFirst != null) { + // A single component can be cemented to the front of the list. If it is seen, let it + // always get priority. + if (mPromoteToFirst.equals(lhs.activityInfo.getComponentName())) { + return -1; + } else if (mPromoteToFirst.equals(rhs.activityInfo.getComponentName())) { + return 1; + } + } + if (mHttp) { final boolean lhsSpecific = ResolverActivity.isSpecificUriMatch(lhs.match); final boolean rhsSpecific = ResolverActivity.isSpecificUriMatch(rhs.match); @@ -197,8 +233,8 @@ public abstract class AbstractResolverComparator implements Comparator<ResolvedC /** * Computes features for each target. This will be called before calls to {@link - * #getScore(ComponentName)} or {@link #compare(Object, Object)}, in order to prepare the - * comparator for those calls. Note that {@link #getScore(ComponentName)} uses {@link + * #getScore(TargetInfo)} or {@link #compare(ResolveInfo, ResolveInfo)}, in order to prepare the + * comparator for those calls. Note that {@link #getScore(TargetInfo)} uses {@link * ComponentName}, so the implementation will have to be prepared to identify a {@link * ResolvedComponentInfo} by {@link ComponentName}. {@link #beforeCompute()} will be called * before doing any computing. @@ -215,7 +251,7 @@ public abstract class AbstractResolverComparator implements Comparator<ResolvedC * Returns the score that was calculated for the corresponding {@link ResolvedComponentInfo} * when {@link #compute(List)} was called before this. */ - public abstract float getScore(ComponentName name); + public abstract float getScore(TargetInfo targetInfo); /** Handles result message sent to mHandler. */ abstract void handleResultMessage(Message message); @@ -223,9 +259,14 @@ public abstract class AbstractResolverComparator implements Comparator<ResolvedC /** * Reports to UsageStats what was chosen. */ - public final void updateChooserCounts(String packageName, int userId, String action) { - if (mUsm != null) { - mUsm.reportChooserSelection(packageName, userId, mContentType, mAnnotations, action); + public final void updateChooserCounts(String packageName, UserHandle user, String action) { + if (mUsmMap.containsKey(user)) { + mUsmMap.get(user).reportChooserSelection( + packageName, + user.getIdentifier(), + mContentType, + mAnnotations, + action); } } @@ -235,9 +276,9 @@ public abstract class AbstractResolverComparator implements Comparator<ResolvedC * <p>Default implementation does nothing, as we could have simple model that does not train * online. * - * @param componentName the component that the user clicked + * * @param targetInfo the target that the user clicked. */ - public void updateModel(ComponentName componentName) { + public void updateModel(TargetInfo targetInfo) { } /** Called before {@link #doCompute(List)}. Sets up 500ms timeout. */ diff --git a/java/src/com/android/intentresolver/model/AppPredictionServiceResolverComparator.java b/java/src/com/android/intentresolver/model/AppPredictionServiceResolverComparator.java index c986ef15..ba054731 100644 --- a/java/src/com/android/intentresolver/model/AppPredictionServiceResolverComparator.java +++ b/java/src/com/android/intentresolver/model/AppPredictionServiceResolverComparator.java @@ -33,6 +33,9 @@ import android.util.Log; import com.android.intentresolver.ChooserActivityLogger; import com.android.intentresolver.ResolvedComponentInfo; +import com.android.intentresolver.chooser.TargetInfo; + +import com.google.android.collect.Lists; import java.util.ArrayList; import java.util.Comparator; @@ -69,8 +72,9 @@ public class AppPredictionServiceResolverComparator extends AbstractResolverComp String referrerPackage, AppPredictor appPredictor, UserHandle user, - ChooserActivityLogger chooserActivityLogger) { - super(context, intent); + ChooserActivityLogger chooserActivityLogger, + @Nullable ComponentName promoteToFirst) { + super(context, intent, Lists.newArrayList(user), promoteToFirst); mContext = context; mIntent = intent; mAppPredictor = appPredictor; @@ -108,9 +112,13 @@ public class AppPredictionServiceResolverComparator extends AbstractResolverComp // APS for chooser is disabled. Fallback to resolver. mResolverRankerService = new ResolverRankerServiceResolverComparator( - mContext, mIntent, mReferrerPackage, + mContext, + mIntent, + mReferrerPackage, () -> mHandler.sendEmptyMessage(RANKER_SERVICE_RESULT), - getChooserActivityLogger()); + getChooserActivityLogger(), + mUser, + mPromoteToFirst); mComparatorModel = buildUpdatedModel(); mResolverRankerService.compute(targets); } else { @@ -167,13 +175,13 @@ public class AppPredictionServiceResolverComparator extends AbstractResolverComp } @Override - public float getScore(ComponentName name) { - return mComparatorModel.getScore(name); + public float getScore(TargetInfo targetInfo) { + return mComparatorModel.getScore(targetInfo); } @Override - public void updateModel(ComponentName componentName) { - mComparatorModel.notifyOnTargetSelected(componentName); + public void updateModel(TargetInfo targetInfo) { + mComparatorModel.notifyOnTargetSelected(targetInfo); } @Override @@ -246,11 +254,11 @@ public class AppPredictionServiceResolverComparator extends AbstractResolverComp } @Override - public float getScore(ComponentName name) { + public float getScore(TargetInfo targetInfo) { if (mResolverRankerService != null) { - return mResolverRankerService.getScore(name); + return mResolverRankerService.getScore(targetInfo); } - Integer rank = mTargetRanks.get(name); + Integer rank = mTargetRanks.get(targetInfo.getResolvedComponentName()); if (rank == null) { Log.w(TAG, "Score requested for unknown component. Did you call compute yet?"); return 0f; @@ -260,18 +268,19 @@ public class AppPredictionServiceResolverComparator extends AbstractResolverComp } @Override - public void notifyOnTargetSelected(ComponentName componentName) { + public void notifyOnTargetSelected(TargetInfo targetInfo) { if (mResolverRankerService != null) { - mResolverRankerService.updateModel(componentName); + mResolverRankerService.updateModel(targetInfo); return; } + ComponentName targetComponent = targetInfo.getResolvedComponentName(); + AppTargetId targetId = new AppTargetId(targetComponent.toString()); + AppTarget appTarget = + new AppTarget.Builder(targetId, targetComponent.getPackageName(), mUser) + .setClassName(targetComponent.getClassName()) + .build(); mAppPredictor.notifyAppTargetEvent( - new AppTargetEvent.Builder( - new AppTarget.Builder( - new AppTargetId(componentName.toString()), - componentName.getPackageName(), mUser) - .setClassName(componentName.getClassName()).build(), - ACTION_LAUNCH).build()); + new AppTargetEvent.Builder(appTarget, ACTION_LAUNCH).build()); } } } diff --git a/java/src/com/android/intentresolver/model/ResolverComparatorModel.java b/java/src/com/android/intentresolver/model/ResolverComparatorModel.java index 3616a853..4835ea17 100644 --- a/java/src/com/android/intentresolver/model/ResolverComparatorModel.java +++ b/java/src/com/android/intentresolver/model/ResolverComparatorModel.java @@ -16,9 +16,10 @@ package com.android.intentresolver.model; -import android.content.ComponentName; import android.content.pm.ResolveInfo; +import com.android.intentresolver.chooser.TargetInfo; + import java.util.Comparator; /** @@ -44,7 +45,7 @@ interface ResolverComparatorModel { * likelihood that the user will select that component as the target. Implementations that don't * assign numerical scores are <em>recommended</em> to return a value of 0 for all components. */ - float getScore(ComponentName name); + float getScore(TargetInfo targetInfo); /** * Notify the model that the user selected a target. (Models may log this information, use it as @@ -52,5 +53,5 @@ interface ResolverComparatorModel { * {@code ResolverComparatorModel} instance is immutable, clients will need to get an up-to-date * instance in order to see any changes in the ranking that might result from this feedback. */ - void notifyOnTargetSelected(ComponentName componentName); + void notifyOnTargetSelected(TargetInfo targetInfo); } diff --git a/java/src/com/android/intentresolver/model/ResolverRankerServiceResolverComparator.java b/java/src/com/android/intentresolver/model/ResolverRankerServiceResolverComparator.java index 0431078c..ebaffc36 100644 --- a/java/src/com/android/intentresolver/model/ResolverRankerServiceResolverComparator.java +++ b/java/src/com/android/intentresolver/model/ResolverRankerServiceResolverComparator.java @@ -17,11 +17,13 @@ package com.android.intentresolver.model; +import android.annotation.Nullable; import android.app.usage.UsageStats; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; +import android.content.pm.ActivityInfo; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; @@ -39,12 +41,16 @@ import android.util.Log; import com.android.intentresolver.ChooserActivityLogger; import com.android.intentresolver.ResolvedComponentInfo; +import com.android.intentresolver.chooser.TargetInfo; import com.android.internal.logging.MetricsLogger; import com.android.internal.logging.nano.MetricsProto.MetricsEvent; +import com.google.android.collect.Lists; + import java.text.Collator; import java.util.ArrayList; import java.util.Comparator; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -70,10 +76,10 @@ public class ResolverRankerServiceResolverComparator extends AbstractResolverCom private static final int CONNECTION_COST_TIMEOUT_MILLIS = 200; private final Collator mCollator; - private final Map<String, UsageStats> mStats; + private final Map<UserHandle, Map<String, UsageStats>> mStatsPerUser; private final long mCurrentTime; private final long mSinceTime; - private final LinkedHashMap<ComponentName, ResolverTarget> mTargetsDict = new LinkedHashMap<>(); + private final Map<UserHandle, Map<ComponentName, ResolverTarget>> mTargetsDictPerUser; private final String mReferrerPackage; private final Object mLock = new Object(); private ArrayList<ResolverTarget> mTargets; @@ -86,17 +92,50 @@ public class ResolverRankerServiceResolverComparator extends AbstractResolverCom private CountDownLatch mConnectSignal; private ResolverRankerServiceComparatorModel mComparatorModel; - public ResolverRankerServiceResolverComparator(Context context, Intent intent, - String referrerPackage, Runnable afterCompute, - ChooserActivityLogger chooserActivityLogger) { - super(context, intent); - mCollator = Collator.getInstance(context.getResources().getConfiguration().locale); + /** + * Constructor to initialize the comparator. + * @param launchedFromContext the activity calling this comparator + * @param intent original intent + * @param targetUserSpace the userSpace(s) used by the comparator for fetching activity stats + * and recording activity selection. The latter could be different from + * the userSpace provided by context. + */ + public ResolverRankerServiceResolverComparator(Context launchedFromContext, Intent intent, + String referrerPackage, Runnable afterCompute, + ChooserActivityLogger chooserActivityLogger, UserHandle targetUserSpace, + ComponentName promoteToFirst) { + this(launchedFromContext, intent, referrerPackage, afterCompute, chooserActivityLogger, + Lists.newArrayList(targetUserSpace), promoteToFirst); + } + + /** + * Constructor to initialize the comparator. + * @param launchedFromContext the activity calling this comparator + * @param intent original intent + * @param targetUserSpaceList the userSpace(s) used by the comparator for fetching activity + * stats and recording activity selection. The latter could be + * different from the userSpace provided by context. + */ + public ResolverRankerServiceResolverComparator(Context launchedFromContext, Intent intent, + String referrerPackage, Runnable afterCompute, + ChooserActivityLogger chooserActivityLogger, List<UserHandle> targetUserSpaceList, + @Nullable ComponentName promoteToFirst) { + super(launchedFromContext, intent, targetUserSpaceList, promoteToFirst); + mCollator = Collator.getInstance( + launchedFromContext.getResources().getConfiguration().locale); mReferrerPackage = referrerPackage; - mContext = context; + mContext = launchedFromContext; mCurrentTime = System.currentTimeMillis(); mSinceTime = mCurrentTime - USAGE_STATS_PERIOD; - mStats = mUsm.queryAndAggregateUsageStats(mSinceTime, mCurrentTime); + mStatsPerUser = new HashMap<>(); + mTargetsDictPerUser = new HashMap<>(); + for (UserHandle user : targetUserSpaceList) { + mStatsPerUser.put( + user, + mUsmMap.get(user).queryAndAggregateUsageStats(mSinceTime, mCurrentTime)); + mTargetsDictPerUser.put(user, new LinkedHashMap<>()); + } mAction = intent.getAction(); mRankerServiceName = new ComponentName(mContext, this.getClass()); setCallBack(afterCompute); @@ -147,58 +186,68 @@ public class ResolverRankerServiceResolverComparator extends AbstractResolverCom float mostChooserScore = 1.0f; for (ResolvedComponentInfo target : targets) { + if (target.getResolveInfoAt(0) == null) { + continue; + } final ResolverTarget resolverTarget = new ResolverTarget(); - mTargetsDict.put(target.name, resolverTarget); - final UsageStats pkStats = mStats.get(target.name.getPackageName()); - if (pkStats != null) { - // Only count recency for apps that weren't the caller - // since the caller is always the most recent. - // Persistent processes muck this up, so omit them too. - if (!target.name.getPackageName().equals(mReferrerPackage) - && !isPersistentProcess(target)) { - final float recencyScore = - (float) Math.max(pkStats.getLastTimeUsed() - recentSinceTime, 0); - resolverTarget.setRecencyScore(recencyScore); - if (recencyScore > mostRecencyScore) { - mostRecencyScore = recencyScore; + final UserHandle resolvedComponentUserSpace = + target.getResolveInfoAt(0).userHandle; + final Map<ComponentName, ResolverTarget> targetsDict = + mTargetsDictPerUser.get(resolvedComponentUserSpace); + final Map<String, UsageStats> stats = mStatsPerUser.get(resolvedComponentUserSpace); + if (targetsDict != null && stats != null) { + targetsDict.put(target.name, resolverTarget); + final UsageStats pkStats = stats.get(target.name.getPackageName()); + if (pkStats != null) { + // Only count recency for apps that weren't the caller + // since the caller is always the most recent. + // Persistent processes muck this up, so omit them too. + if (!target.name.getPackageName().equals(mReferrerPackage) + && !isPersistentProcess(target)) { + final float recencyScore = + (float) Math.max(pkStats.getLastTimeUsed() - recentSinceTime, 0); + resolverTarget.setRecencyScore(recencyScore); + if (recencyScore > mostRecencyScore) { + mostRecencyScore = recencyScore; + } + } + final float timeSpentScore = (float) pkStats.getTotalTimeInForeground(); + resolverTarget.setTimeSpentScore(timeSpentScore); + if (timeSpentScore > mostTimeSpentScore) { + mostTimeSpentScore = timeSpentScore; + } + final float launchScore = (float) pkStats.mLaunchCount; + resolverTarget.setLaunchScore(launchScore); + if (launchScore > mostLaunchScore) { + mostLaunchScore = launchScore; } - } - final float timeSpentScore = (float) pkStats.getTotalTimeInForeground(); - resolverTarget.setTimeSpentScore(timeSpentScore); - if (timeSpentScore > mostTimeSpentScore) { - mostTimeSpentScore = timeSpentScore; - } - final float launchScore = (float) pkStats.mLaunchCount; - resolverTarget.setLaunchScore(launchScore); - if (launchScore > mostLaunchScore) { - mostLaunchScore = launchScore; - } - float chooserScore = 0.0f; - if (pkStats.mChooserCounts != null && mAction != null - && pkStats.mChooserCounts.get(mAction) != null) { - chooserScore = (float) pkStats.mChooserCounts.get(mAction) - .getOrDefault(mContentType, 0); - if (mAnnotations != null) { - final int size = mAnnotations.length; - for (int i = 0; i < size; i++) { - chooserScore += (float) pkStats.mChooserCounts.get(mAction) - .getOrDefault(mAnnotations[i], 0); + float chooserScore = 0.0f; + if (pkStats.mChooserCounts != null && mAction != null + && pkStats.mChooserCounts.get(mAction) != null) { + chooserScore = (float) pkStats.mChooserCounts.get(mAction) + .getOrDefault(mContentType, 0); + if (mAnnotations != null) { + final int size = mAnnotations.length; + for (int i = 0; i < size; i++) { + chooserScore += (float) pkStats.mChooserCounts.get(mAction) + .getOrDefault(mAnnotations[i], 0); + } } } - } - if (DEBUG) { - if (mAction == null) { - Log.d(TAG, "Action type is null"); - } else { - Log.d(TAG, "Chooser Count of " + mAction + ":" - + target.name.getPackageName() + " is " - + Float.toString(chooserScore)); + if (DEBUG) { + if (mAction == null) { + Log.d(TAG, "Action type is null"); + } else { + Log.d(TAG, "Chooser Count of " + mAction + ":" + + target.name.getPackageName() + " is " + + Float.toString(chooserScore)); + } + } + resolverTarget.setChooserScore(chooserScore); + if (chooserScore > mostChooserScore) { + mostChooserScore = chooserScore; } - } - resolverTarget.setChooserScore(chooserScore); - if (chooserScore > mostChooserScore) { - mostChooserScore = chooserScore; } } } @@ -210,7 +259,10 @@ public class ResolverRankerServiceResolverComparator extends AbstractResolverCom + " mostChooserScore: " + mostChooserScore); } - mTargets = new ArrayList<>(mTargetsDict.values()); + mTargets = new ArrayList<>(); + for (UserHandle u : mTargetsDictPerUser.keySet()) { + mTargets.addAll(mTargetsDictPerUser.get(u).values()); + } for (ResolverTarget target : mTargets) { final float recency = target.getRecencyScore() / mostRecencyScore; setFeatures(target, recency * recency * RECENCY_MULTIPLIER, @@ -233,15 +285,15 @@ public class ResolverRankerServiceResolverComparator extends AbstractResolverCom } @Override - public float getScore(ComponentName name) { - return mComparatorModel.getScore(name); + public float getScore(TargetInfo targetInfo) { + return mComparatorModel.getScore(targetInfo); } // update ranking model when the connection to it is valid. @Override - public void updateModel(ComponentName componentName) { + public void updateModel(TargetInfo targetInfo) { synchronized (mLock) { - mComparatorModel.notifyOnTargetSelected(componentName); + mComparatorModel.notifyOnTargetSelected(targetInfo); } } @@ -282,7 +334,8 @@ public class ResolverRankerServiceResolverComparator extends AbstractResolverCom // resolve the service for ranking. private Intent resolveRankerService() { Intent intent = new Intent(ResolverRankerService.SERVICE_INTERFACE); - final List<ResolveInfo> resolveInfos = mPm.queryIntentServices(intent, 0); + final List<ResolveInfo> resolveInfos = mContext.getPackageManager() + .queryIntentServices(intent, 0); for (ResolveInfo resolveInfo : resolveInfos) { if (resolveInfo == null || resolveInfo.serviceInfo == null || resolveInfo.serviceInfo.applicationInfo == null) { @@ -295,7 +348,8 @@ public class ResolverRankerServiceResolverComparator extends AbstractResolverCom resolveInfo.serviceInfo.applicationInfo.packageName, resolveInfo.serviceInfo.name); try { - final String perm = mPm.getServiceInfo(componentName, 0).permission; + final String perm = + mContext.getPackageManager().getServiceInfo(componentName, 0).permission; if (!ResolverRankerService.BIND_PERMISSION.equals(perm)) { Log.w(TAG, "ResolverRankerService " + componentName + " does not require" + " permission " + ResolverRankerService.BIND_PERMISSION @@ -306,9 +360,9 @@ public class ResolverRankerServiceResolverComparator extends AbstractResolverCom + " in the manifest."); continue; } - if (PackageManager.PERMISSION_GRANTED != mPm.checkPermission( - ResolverRankerService.HOLD_PERMISSION, - resolveInfo.serviceInfo.packageName)) { + if (PackageManager.PERMISSION_GRANTED != mContext.getPackageManager() + .checkPermission(ResolverRankerService.HOLD_PERMISSION, + resolveInfo.serviceInfo.packageName)) { Log.w(TAG, "ResolverRankerService " + componentName + " does not hold" + " permission " + ResolverRankerService.HOLD_PERMISSION + " - this service will not be queried for " @@ -386,7 +440,9 @@ public class ResolverRankerServiceResolverComparator extends AbstractResolverCom @Override void beforeCompute() { super.beforeCompute(); - mTargetsDict.clear(); + for (UserHandle userHandle : mTargetsDictPerUser.keySet()) { + mTargetsDictPerUser.get(userHandle).clear(); + } mTargets = null; mRankerServiceName = new ComponentName(mContext, this.getClass()); mComparatorModel = buildUpdatedModel(); @@ -468,14 +524,14 @@ public class ResolverRankerServiceResolverComparator extends AbstractResolverCom // so the ResolverComparatorModel may provide inconsistent results. We should make immutable // copies of the data (waiting for any necessary remaining data before creating the model). return new ResolverRankerServiceComparatorModel( - mStats, - mTargetsDict, + mStatsPerUser, + mTargetsDictPerUser, mTargets, mCollator, mRanker, mRankerServiceName, (mAnnotations != null), - mPm); + mPmMap); } /** @@ -484,35 +540,36 @@ public class ResolverRankerServiceResolverComparator extends AbstractResolverCom * removing the complex legacy API. */ static class ResolverRankerServiceComparatorModel implements ResolverComparatorModel { - private final Map<String, UsageStats> mStats; // Treat as immutable. - private final Map<ComponentName, ResolverTarget> mTargetsDict; // Treat as immutable. + private final Map<UserHandle, Map<String, UsageStats>> mStatsPerUser; // Treat as immutable. + // Treat as immutable. + private final Map<UserHandle, Map<ComponentName, ResolverTarget>> mTargetsDictPerUser; private final List<ResolverTarget> mTargets; // Treat as immutable. private final Collator mCollator; private final IResolverRankerService mRanker; private final ComponentName mRankerServiceName; private final boolean mAnnotationsUsed; - private final PackageManager mPm; + private final Map<UserHandle, PackageManager> mPmMap; // TODO: it doesn't look like we should have to pass both targets and targetsDict, but it's // not written in a way that makes it clear whether we can derive one from the other (at // least in this constructor). ResolverRankerServiceComparatorModel( - Map<String, UsageStats> stats, - Map<ComponentName, ResolverTarget> targetsDict, + Map<UserHandle, Map<String, UsageStats>> statsPerUser, + Map<UserHandle, Map<ComponentName, ResolverTarget>> targetsDictPerUser, List<ResolverTarget> targets, Collator collator, IResolverRankerService ranker, ComponentName rankerServiceName, boolean annotationsUsed, - PackageManager pm) { - mStats = stats; - mTargetsDict = targetsDict; + Map<UserHandle, PackageManager> pmMap) { + mStatsPerUser = statsPerUser; + mTargetsDictPerUser = targetsDictPerUser; mTargets = targets; mCollator = collator; mRanker = ranker; mRankerServiceName = rankerServiceName; mAnnotationsUsed = annotationsUsed; - mPm = pm; + mPmMap = pmMap; } @Override @@ -521,25 +578,29 @@ public class ResolverRankerServiceResolverComparator extends AbstractResolverCom // a bug there, or do we have a way of knowing it will be non-null under certain // conditions? return (lhs, rhs) -> { - if (mStats != null) { - final ResolverTarget lhsTarget = mTargetsDict.get(new ComponentName( - lhs.activityInfo.packageName, lhs.activityInfo.name)); - final ResolverTarget rhsTarget = mTargetsDict.get(new ComponentName( - rhs.activityInfo.packageName, rhs.activityInfo.name)); - - if (lhsTarget != null && rhsTarget != null) { - final int selectProbabilityDiff = Float.compare( - rhsTarget.getSelectProbability(), lhsTarget.getSelectProbability()); - - if (selectProbabilityDiff != 0) { - return selectProbabilityDiff > 0 ? 1 : -1; - } + final ResolverTarget lhsTarget = + getActivityResolverTargetForUser(lhs.activityInfo, lhs.userHandle); + final ResolverTarget rhsTarget = + getActivityResolverTargetForUser(rhs.activityInfo, rhs.userHandle); + + if (lhsTarget != null && rhsTarget != null) { + final int selectProbabilityDiff = Float.compare( + rhsTarget.getSelectProbability(), lhsTarget.getSelectProbability()); + + if (selectProbabilityDiff != 0) { + return selectProbabilityDiff > 0 ? 1 : -1; } } - CharSequence sa = lhs.loadLabel(mPm); + CharSequence sa = null; + if (mPmMap.containsKey(lhs.userHandle)) { + sa = lhs.loadLabel(mPmMap.get(lhs.userHandle)); + } if (sa == null) sa = lhs.activityInfo.name; - CharSequence sb = rhs.loadLabel(mPm); + CharSequence sb = null; + if (mPmMap.containsKey(rhs.userHandle)) { + sb = rhs.loadLabel(mPmMap.get(rhs.userHandle)); + } if (sb == null) sb = rhs.activityInfo.name; return mCollator.compare(sa.toString().trim(), sb.toString().trim()); @@ -547,8 +608,9 @@ public class ResolverRankerServiceResolverComparator extends AbstractResolverCom } @Override - public float getScore(ComponentName name) { - final ResolverTarget target = mTargetsDict.get(name); + public float getScore(TargetInfo targetInfo) { + ResolverTarget target = getResolverTargetForUserAndComponent( + targetInfo.getResolvedComponentName(), targetInfo.getResolveInfo().userHandle); if (target != null) { return target.getSelectProbability(); } @@ -556,13 +618,17 @@ public class ResolverRankerServiceResolverComparator extends AbstractResolverCom } @Override - public void notifyOnTargetSelected(ComponentName componentName) { + public void notifyOnTargetSelected(TargetInfo targetInfo) { if (mRanker != null) { try { - int selectedPos = new ArrayList<ComponentName>(mTargetsDict.keySet()) - .indexOf(componentName); + int selectedPos = -1; + if (mTargetsDictPerUser.containsKey(targetInfo.getResolveInfo().userHandle)) { + selectedPos = new ArrayList<>(mTargetsDictPerUser + .get(targetInfo.getResolveInfo().userHandle).keySet()) + .indexOf(targetInfo.getResolvedComponentName()); + } if (selectedPos >= 0 && mTargets != null) { - final float selectedProbability = getScore(componentName); + final float selectedProbability = getScore(targetInfo); int order = 0; for (ResolverTarget target : mTargets) { if (target.getSelectProbability() > selectedProbability) { @@ -573,7 +639,8 @@ public class ResolverRankerServiceResolverComparator extends AbstractResolverCom mRanker.train(mTargets, selectedPos); } else { if (DEBUG) { - Log.d(TAG, "Selected a unknown component: " + componentName); + Log.d(TAG, "Selected a unknown component: " + targetInfo + .getResolvedComponentName()); } } } catch (RemoteException e) { @@ -597,5 +664,21 @@ public class ResolverRankerServiceResolverComparator extends AbstractResolverCom metricsLogger.write(log); } } + + @Nullable + private ResolverTarget getActivityResolverTargetForUser( + ActivityInfo activity, UserHandle user) { + return getResolverTargetForUserAndComponent( + new ComponentName(activity.packageName, activity.name), user); + } + + @Nullable + private ResolverTarget getResolverTargetForUserAndComponent( + ComponentName targetComponentName, UserHandle user) { + if ((mStatsPerUser == null) || !mTargetsDictPerUser.containsKey(user)) { + return null; + } + return mTargetsDictPerUser.get(user).get(targetComponentName); + } } } diff --git a/java/src/com/android/intentresolver/shortcuts/ShortcutLoader.kt b/java/src/com/android/intentresolver/shortcuts/ShortcutLoader.kt index 6f7542f1..3ffbe039 100644 --- a/java/src/com/android/intentresolver/shortcuts/ShortcutLoader.kt +++ b/java/src/com/android/intentresolver/shortcuts/ShortcutLoader.kt @@ -26,7 +26,6 @@ import android.content.pm.PackageManager import android.content.pm.ShortcutInfo import android.content.pm.ShortcutManager import android.content.pm.ShortcutManager.ShareShortcutInfo -import android.os.AsyncTask import android.os.UserHandle import android.os.UserManager import android.service.chooser.ChooserTarget @@ -36,126 +35,181 @@ import androidx.annotation.MainThread import androidx.annotation.OpenForTesting import androidx.annotation.VisibleForTesting import androidx.annotation.WorkerThread +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.coroutineScope import com.android.intentresolver.chooser.DisplayResolveInfo -import java.lang.RuntimeException -import java.util.ArrayList -import java.util.HashMap +import com.android.intentresolver.measurements.Tracer +import com.android.intentresolver.measurements.runTracing import java.util.concurrent.Executor -import java.util.concurrent.atomic.AtomicReference import java.util.function.Consumer +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.asExecutor +import kotlinx.coroutines.channels.BufferOverflow +import kotlinx.coroutines.flow.MutableSharedFlow +import kotlinx.coroutines.flow.combine +import kotlinx.coroutines.flow.filter +import kotlinx.coroutines.flow.flowOn +import kotlinx.coroutines.launch /** * Encapsulates shortcuts loading logic from either AppPredictor or ShortcutManager. * - * * A ShortcutLoader instance can be viewed as a per-profile singleton hot stream of shortcut - * updates. The shortcut loading is triggered by the [queryShortcuts], - * the processing will happen on the [backgroundExecutor] and the result is delivered - * through the [callback] on the [callbackExecutor], the main thread. - * - * - * The current version does not improve on the legacy in a way that it does not guarantee that - * each invocation of the [queryShortcuts] will be matched by an - * invocation of the callback (there are early terminations of the flow). Also, the fetched - * shortcuts would be matched against the last known input, i.e. two invocations of - * [queryShortcuts] may result in two callbacks where shortcuts are - * processed against the latest input. - * + * updates. The shortcut loading is triggered in the constructor or by the [reset] method, the + * processing happens on the [dispatcher] and the result is delivered through the [callback] on the + * default [lifecycle]'s dispatcher, the main thread. */ @OpenForTesting -open class ShortcutLoader @VisibleForTesting constructor( +open class ShortcutLoader +@VisibleForTesting +constructor( private val context: Context, + private val lifecycle: Lifecycle, private val appPredictor: AppPredictorProxy?, private val userHandle: UserHandle, private val isPersonalProfile: Boolean, private val targetIntentFilter: IntentFilter?, - private val backgroundExecutor: Executor, - private val callbackExecutor: Executor, + private val dispatcher: CoroutineDispatcher, private val callback: Consumer<Result> ) { private val shortcutToChooserTargetConverter = ShortcutToChooserTargetConverter() private val userManager = context.getSystemService(Context.USER_SERVICE) as UserManager - private val activeRequest = AtomicReference(NO_REQUEST) private val appPredictorCallback = AppPredictor.Callback { onAppPredictorCallback(it) } - private var isDestroyed = false + private val appTargetSource = + MutableSharedFlow<Array<DisplayResolveInfo>?>( + replay = 1, + onBufferOverflow = BufferOverflow.DROP_OLDEST + ) + private val shortcutSource = + MutableSharedFlow<ShortcutData?>(replay = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST) + private val isDestroyed + get() = !lifecycle.currentState.isAtLeast(Lifecycle.State.CREATED) @MainThread constructor( context: Context, + lifecycle: Lifecycle, appPredictor: AppPredictor?, userHandle: UserHandle, targetIntentFilter: IntentFilter?, callback: Consumer<Result> ) : this( context, + lifecycle, appPredictor?.let { AppPredictorProxy(it) }, - userHandle, userHandle == UserHandle.of(ActivityManager.getCurrentUser()), + userHandle, + userHandle == UserHandle.of(ActivityManager.getCurrentUser()), targetIntentFilter, - AsyncTask.SERIAL_EXECUTOR, - context.mainExecutor, + Dispatchers.IO, callback ) init { - appPredictor?.registerPredictionUpdates(callbackExecutor, appPredictorCallback) + appPredictor?.registerPredictionUpdates(dispatcher.asExecutor(), appPredictorCallback) + lifecycle.coroutineScope + .launch { + appTargetSource + .combine(shortcutSource) { appTargets, shortcutData -> + if (appTargets == null || shortcutData == null) { + null + } else { + runTracing("filter-shortcuts-${userHandle.identifier}") { + filterShortcuts( + appTargets, + shortcutData.shortcuts, + shortcutData.isFromAppPredictor, + shortcutData.appPredictorTargets + ) + } + } + } + .filter { it != null } + .flowOn(dispatcher) + .collect { callback.accept(it ?: error("can not be null")) } + } + .invokeOnCompletion { + runCatching { appPredictor?.unregisterPredictionUpdates(appPredictorCallback) } + Log.d(TAG, "destroyed, user: $userHandle") + } + reset() } - /** - * Unsubscribe from app predictor if one was provided. - */ - @OpenForTesting - @MainThread - open fun destroy() { - isDestroyed = true - appPredictor?.unregisterPredictionUpdates(appPredictorCallback) + /** Clear application targets (see [updateAppTargets] and initiate shrtcuts loading. */ + fun reset() { + Log.d(TAG, "reset shortcut loader for user $userHandle") + appTargetSource.tryEmit(null) + shortcutSource.tryEmit(null) + lifecycle.coroutineScope.launch(dispatcher) { loadShortcuts() } } /** - * Set new resolved targets. This will trigger shortcut loading. - * @param appTargets a collection of application targets a loaded set of shortcuts will be - * grouped against + * Update resolved application targets; as soon as shortcuts are loaded, they will be filtered + * against the targets and the is delivered to the client through the [callback]. */ @OpenForTesting - @MainThread - open fun queryShortcuts(appTargets: Array<DisplayResolveInfo>) { - if (isDestroyed) return - activeRequest.set(Request(appTargets)) - backgroundExecutor.execute { loadShortcuts() } + open fun updateAppTargets(appTargets: Array<DisplayResolveInfo>) { + appTargetSource.tryEmit(appTargets) } @WorkerThread private fun loadShortcuts() { // no need to query direct share for work profile when its locked or disabled - if (!shouldQueryDirectShareTargets()) return - Log.d(TAG, "querying direct share targets") + if (!shouldQueryDirectShareTargets()) { + Log.d(TAG, "skip shortcuts loading for user $userHandle") + return + } + Log.d(TAG, "querying direct share targets for user $userHandle") queryDirectShareTargets(false) } @WorkerThread private fun queryDirectShareTargets(skipAppPredictionService: Boolean) { if (!skipAppPredictionService && appPredictor != null) { - appPredictor.requestPredictionUpdate() - return + try { + Log.d(TAG, "query AppPredictor for user $userHandle") + Tracer.beginAppPredictorQueryTrace(userHandle) + appPredictor.requestPredictionUpdate() + return + } catch (e: Throwable) { + endAppPredictorQueryTrace(userHandle) + // we might have been destroyed concurrently, nothing left to do + if (isDestroyed) { + return + } + Log.e(TAG, "Failed to query AppPredictor for user $userHandle", e) + } } // Default to just querying ShortcutManager if AppPredictor not present. - if (targetIntentFilter == null) return - val shortcuts = queryShortcutManager(targetIntentFilter) + if (targetIntentFilter == null) { + Log.d(TAG, "skip querying ShortcutManager for $userHandle") + return + } + Log.d(TAG, "query ShortcutManager for user $userHandle") + val shortcuts = + runTracing("shortcut-mngr-${userHandle.identifier}") { + queryShortcutManager(targetIntentFilter) + } + Log.d(TAG, "receive shortcuts from ShortcutManager for user $userHandle") sendShareShortcutInfoList(shortcuts, false, null) } @WorkerThread private fun queryShortcutManager(targetIntentFilter: IntentFilter): List<ShareShortcutInfo> { val selectedProfileContext = context.createContextAsUser(userHandle, 0 /* flags */) - val sm = selectedProfileContext - .getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager? + val sm = + selectedProfileContext.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager? val pm = context.createContextAsUser(userHandle, 0 /* flags */).packageManager - return sm?.getShareTargets(targetIntentFilter) - ?.filter { pm.isPackageEnabled(it.targetComponent.packageName) } + return sm?.getShareTargets(targetIntentFilter)?.filter { + pm.isPackageEnabled(it.targetComponent.packageName) + } ?: emptyList() } @WorkerThread private fun onAppPredictorCallback(appPredictorTargets: List<AppTarget>) { + endAppPredictorQueryTrace(userHandle) + Log.d(TAG, "receive app targets from AppPredictor") if (appPredictorTargets.isEmpty() && shouldQueryDirectShareTargets()) { // APS may be disabled, so try querying targets ourselves. queryDirectShareTargets(true) @@ -168,9 +222,7 @@ open class ShortcutLoader @VisibleForTesting constructor( @WorkerThread private fun List<AppTarget>.toShortcuts(pm: PackageManager): ShortcutsAppTargetsPair = - fold( - ShortcutsAppTargetsPair(ArrayList(size), ArrayList(size)) - ) { acc, appTarget -> + fold(ShortcutsAppTargetsPair(ArrayList(size), ArrayList(size))) { acc, appTarget -> val shortcutInfo = appTarget.shortcutInfo val packageName = appTarget.packageName val className = appTarget.className @@ -189,11 +241,22 @@ open class ShortcutLoader @VisibleForTesting constructor( isFromAppPredictor: Boolean, appPredictorTargets: List<AppTarget>? ) { + shortcutSource.tryEmit(ShortcutData(shortcuts, isFromAppPredictor, appPredictorTargets)) + } + + private fun filterShortcuts( + appTargets: Array<DisplayResolveInfo>, + shortcuts: List<ShareShortcutInfo>, + isFromAppPredictor: Boolean, + appPredictorTargets: List<AppTarget>? + ): Result { if (appPredictorTargets != null && appPredictorTargets.size != shortcuts.size) { throw RuntimeException( - "resultList and appTargets must have the same size." - + " resultList.size()=" + shortcuts.size - + " appTargets.size()=" + appPredictorTargets.size + "resultList and appTargets must have the same size." + + " resultList.size()=" + + shortcuts.size + + " appTargets.size()=" + + appPredictorTargets.size ) } val directShareAppTargetCache = HashMap<ChooserTarget, AppTarget>() @@ -201,77 +264,65 @@ open class ShortcutLoader @VisibleForTesting constructor( // Match ShareShortcutInfos with DisplayResolveInfos to be able to use the old code path // for direct share targets. After ShareSheet is refactored we should use the // ShareShortcutInfos directly. - val appTargets = activeRequest.get().appTargets val resultRecords: MutableList<ShortcutResultInfo> = ArrayList() for (displayResolveInfo in appTargets) { - val matchingShortcuts = shortcuts.filter { - it.targetComponent == displayResolveInfo.resolvedComponentName - } + val matchingShortcuts = + shortcuts.filter { it.targetComponent == displayResolveInfo.resolvedComponentName } if (matchingShortcuts.isEmpty()) continue - val chooserTargets = shortcutToChooserTargetConverter.convertToChooserTarget( - matchingShortcuts, - shortcuts, - appPredictorTargets, - directShareAppTargetCache, - directShareShortcutInfoCache - ) + val chooserTargets = + shortcutToChooserTargetConverter.convertToChooserTarget( + matchingShortcuts, + shortcuts, + appPredictorTargets, + directShareAppTargetCache, + directShareShortcutInfoCache + ) val resultRecord = ShortcutResultInfo(displayResolveInfo, chooserTargets) resultRecords.add(resultRecord) } - postReport( - Result( - isFromAppPredictor, - appTargets, - resultRecords.toTypedArray(), - directShareAppTargetCache, - directShareShortcutInfoCache - ) + return Result( + isFromAppPredictor, + appTargets, + resultRecords.toTypedArray(), + directShareAppTargetCache, + directShareShortcutInfoCache ) } - private fun postReport(result: Result) = callbackExecutor.execute { report(result) } - - @MainThread - private fun report(result: Result) { - if (isDestroyed) return - callback.accept(result) - } - /** - * Returns `false` if `userHandle` is the work profile and it's either - * in quiet mode or not running. + * Returns `false` if `userHandle` is the work profile and it's either in quiet mode or not + * running. */ private fun shouldQueryDirectShareTargets(): Boolean = isPersonalProfile || isProfileActive @get:VisibleForTesting protected val isProfileActive: Boolean - get() = userManager.isUserRunning(userHandle) - && userManager.isUserUnlocked(userHandle) - && !userManager.isQuietModeEnabled(userHandle) + get() = + userManager.isUserRunning(userHandle) && + userManager.isUserUnlocked(userHandle) && + !userManager.isQuietModeEnabled(userHandle) - private class Request(val appTargets: Array<DisplayResolveInfo>) + private class ShortcutData( + val shortcuts: List<ShareShortcutInfo>, + val isFromAppPredictor: Boolean, + val appPredictorTargets: List<AppTarget>? + ) - /** - * Resolved shortcuts with corresponding app targets. - */ + /** Resolved shortcuts with corresponding app targets. */ class Result( val isFromAppPredictor: Boolean, /** - * Input app targets (see [ShortcutLoader.queryShortcuts] the - * shortcuts were process against. + * Input app targets (see [ShortcutLoader.updateAppTargets] the shortcuts were process + * against. */ val appTargets: Array<DisplayResolveInfo>, - /** - * Shortcuts grouped by app target. - */ + /** Shortcuts grouped by app target. */ val shortcutsByApp: Array<ShortcutResultInfo>, val directShareAppTargetCache: Map<ChooserTarget, AppTarget>, val directShareShortcutInfoCache: Map<ChooserTarget, ShortcutInfo> ) - /** - * Shortcuts grouped by app. - */ + /** Shortcuts grouped by app. */ class ShortcutResultInfo( val appTarget: DisplayResolveInfo, val shortcuts: List<ChooserTarget?> @@ -282,45 +333,46 @@ open class ShortcutLoader @VisibleForTesting constructor( val appTargets: List<AppTarget>? ) - /** - * A wrapper around AppPredictor to facilitate unit-testing. - */ + /** A wrapper around AppPredictor to facilitate unit-testing. */ @VisibleForTesting open class AppPredictorProxy internal constructor(private val mAppPredictor: AppPredictor) { - /** - * [AppPredictor.registerPredictionUpdates] - */ + /** [AppPredictor.registerPredictionUpdates] */ open fun registerPredictionUpdates( - callbackExecutor: Executor, callback: AppPredictor.Callback + callbackExecutor: Executor, + callback: AppPredictor.Callback ) = mAppPredictor.registerPredictionUpdates(callbackExecutor, callback) - /** - * [AppPredictor.unregisterPredictionUpdates] - */ + /** [AppPredictor.unregisterPredictionUpdates] */ open fun unregisterPredictionUpdates(callback: AppPredictor.Callback) = mAppPredictor.unregisterPredictionUpdates(callback) - /** - * [AppPredictor.requestPredictionUpdate] - */ + /** [AppPredictor.requestPredictionUpdate] */ open fun requestPredictionUpdate() = mAppPredictor.requestPredictionUpdate() } companion object { private const val TAG = "ShortcutLoader" - private val NO_REQUEST = Request(arrayOf()) private fun PackageManager.isPackageEnabled(packageName: String): Boolean { if (TextUtils.isEmpty(packageName)) { return false } return runCatching { - val appInfo = getApplicationInfo( - packageName, - PackageManager.ApplicationInfoFlags.of(PackageManager.GET_META_DATA.toLong()) - ) - appInfo.enabled && (appInfo.flags and ApplicationInfo.FLAG_SUSPENDED) == 0 - }.getOrDefault(false) + val appInfo = + getApplicationInfo( + packageName, + PackageManager.ApplicationInfoFlags.of( + PackageManager.GET_META_DATA.toLong() + ) + ) + appInfo.enabled && (appInfo.flags and ApplicationInfo.FLAG_SUSPENDED) == 0 + } + .getOrDefault(false) + } + + private fun endAppPredictorQueryTrace(userHandle: UserHandle) { + val duration = Tracer.endAppPredictorQueryTrace(userHandle) + Log.d(TAG, "AppPredictor query duration for user $userHandle: $duration ms") } } } diff --git a/java/src/com/android/intentresolver/util/Flow.kt b/java/src/com/android/intentresolver/util/Flow.kt new file mode 100644 index 00000000..1155b9fe --- /dev/null +++ b/java/src/com/android/intentresolver/util/Flow.kt @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.intentresolver.util + +import android.os.SystemClock +import kotlinx.coroutines.CoroutineStart +import kotlinx.coroutines.Job +import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.delay +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.channelFlow +import kotlinx.coroutines.launch + +/** + * Returns a flow that mirrors the original flow, but delays values following emitted values for the + * given [periodMs]. If the original flow emits more than one value during this period, only the + * latest value is emitted. + * + * Example: + * + * ```kotlin + * flow { + * emit(1) // t=0ms + * delay(90) + * emit(2) // t=90ms + * delay(90) + * emit(3) // t=180ms + * delay(1010) + * emit(4) // t=1190ms + * delay(1010) + * emit(5) // t=2200ms + * }.throttle(1000) + * ``` + * + * produces the following emissions at the following times + * + * ```text + * 1 (t=0ms), 3 (t=1000ms), 4 (t=2000ms), 5 (t=3000ms) + * ``` + */ +// A SystemUI com.android.systemui.util.kotlin.throttle copy. +fun <T> Flow<T>.throttle(periodMs: Long): Flow<T> = channelFlow { + coroutineScope { + var previousEmitTimeMs = 0L + var delayJob: Job? = null + var sendJob: Job? = null + val outerScope = this + + collect { + delayJob?.cancel() + sendJob?.join() + val currentTimeMs = SystemClock.elapsedRealtime() + val timeSinceLastEmit = currentTimeMs - previousEmitTimeMs + val timeUntilNextEmit = maxOf(0L, periodMs - timeSinceLastEmit) + if (timeUntilNextEmit > 0L) { + // We create delayJob to allow cancellation during the delay period + delayJob = launch { + delay(timeUntilNextEmit) + sendJob = outerScope.launch(start = CoroutineStart.UNDISPATCHED) { + send(it) + previousEmitTimeMs = SystemClock.elapsedRealtime() + } + } + } else { + send(it) + previousEmitTimeMs = currentTimeMs + } + } + } +} diff --git a/java/src/com/android/intentresolver/util/UriFilters.kt b/java/src/com/android/intentresolver/util/UriFilters.kt new file mode 100644 index 00000000..a4c6e574 --- /dev/null +++ b/java/src/com/android/intentresolver/util/UriFilters.kt @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +@file:JvmName("UriFilters") + +package com.android.intentresolver.util + +import android.content.ContentProvider.getUserIdFromUri +import android.content.ContentResolver.SCHEME_CONTENT +import android.graphics.drawable.Icon +import android.graphics.drawable.Icon.TYPE_URI +import android.graphics.drawable.Icon.TYPE_URI_ADAPTIVE_BITMAP +import android.net.Uri +import android.os.UserHandle +import android.service.chooser.ChooserAction + +/** + * Checks if the [Uri] is a `content://` uri which references the current user (from process uid). + * + * MediaStore interprets the user field of a content:// URI as a UserId and applies it if the caller + * holds INTERACT_ACROSS_USERS permission. (Example: `content://10@media/images/1234`) + * + * No URI content should be loaded unless it passes this check since the caller would not have + * permission to read it. + * + * @return false if this is a content:// [Uri] which references another user + */ +val Uri?.ownedByCurrentUser: Boolean + @JvmName("isOwnedByCurrentUser") + get() = + this?.let { + when (getUserIdFromUri(this, UserHandle.USER_CURRENT)) { + UserHandle.USER_CURRENT, + UserHandle.myUserId() -> true + else -> false + } + } == true + +/** Does the [Uri] reference a content provider ('content://')? */ +internal val Uri.contentScheme: Boolean + get() = scheme == SCHEME_CONTENT + +/** + * Checks if the Icon of a [ChooserAction] backed by content:// [Uri] is safe for display. + * + * @param action the chooser action + * @see [Uri.ownedByCurrentUser] + */ +fun hasValidIcon(action: ChooserAction) = hasValidIcon(action.icon) + +/** + * Checks if the Icon backed by content:// [Uri] is safe for display. + * + * @see [Uri.ownedByCurrentUser] + */ +fun hasValidIcon(icon: Icon) = + with(icon) { + when (type) { + TYPE_URI, + TYPE_URI_ADAPTIVE_BITMAP -> !uri.contentScheme || uri.ownedByCurrentUser + else -> true + } + } diff --git a/java/src/com/android/intentresolver/widget/ChooserActionRow.kt b/java/src/com/android/intentresolver/widget/ChooserActionRow.kt deleted file mode 100644 index a4656bb5..00000000 --- a/java/src/com/android/intentresolver/widget/ChooserActionRow.kt +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (C) 2022 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.intentresolver.widget - -import android.annotation.LayoutRes -import android.content.Context -import android.os.Parcelable -import android.util.AttributeSet -import android.view.LayoutInflater -import android.widget.Button -import android.widget.LinearLayout -import com.android.intentresolver.R -import com.android.intentresolver.widget.ActionRow.Action - -class ChooserActionRow : LinearLayout, ActionRow { - constructor(context: Context) : this(context, null) - constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) - constructor( - context: Context, attrs: AttributeSet?, defStyleAttr: Int - ) : this(context, attrs, defStyleAttr, 0) - - constructor( - context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int - ) : super(context, attrs, defStyleAttr, defStyleRes) { - orientation = HORIZONTAL - } - - @LayoutRes - private val itemLayout = R.layout.chooser_action_button - private val itemMargin = - context.resources.getDimensionPixelSize(R.dimen.resolver_icon_margin) / 2 - private var actions: List<Action> = emptyList() - - override fun onRestoreInstanceState(state: Parcelable?) { - super.onRestoreInstanceState(state) - setActions(actions) - } - - override fun setActions(actions: List<Action>) { - removeAllViews() - this.actions = ArrayList(actions) - for (action in actions) { - addAction(action) - } - } - - private fun addAction(action: Action) { - val b = LayoutInflater.from(context).inflate(itemLayout, null) as Button - if (action.icon != null) { - val size = resources - .getDimensionPixelSize(R.dimen.chooser_action_button_icon_size) - action.icon.setBounds(0, 0, size, size) - b.setCompoundDrawablesRelative(action.icon, null, null, null) - } - b.text = action.label ?: "" - b.setOnClickListener { - action.onClicked.run() - } - b.id = action.id - addView(b) - } - - override fun generateDefaultLayoutParams(): LayoutParams = - super.generateDefaultLayoutParams().apply { - setMarginsRelative(itemMargin, 0, itemMargin, 0) - } -} diff --git a/java/src/com/android/intentresolver/widget/ChooserImagePreviewView.kt b/java/src/com/android/intentresolver/widget/ChooserImagePreviewView.kt deleted file mode 100644 index ca94a95d..00000000 --- a/java/src/com/android/intentresolver/widget/ChooserImagePreviewView.kt +++ /dev/null @@ -1,163 +0,0 @@ -/* - * Copyright (C) 2022 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.intentresolver.widget - -import android.animation.ObjectAnimator -import android.content.Context -import android.net.Uri -import android.util.AttributeSet -import android.view.LayoutInflater -import android.view.animation.DecelerateInterpolator -import android.widget.RelativeLayout -import androidx.core.view.isVisible -import com.android.intentresolver.R -import com.android.intentresolver.widget.ImagePreviewView.TransitionElementStatusCallback -import kotlinx.coroutines.Job -import kotlinx.coroutines.MainScope -import kotlinx.coroutines.coroutineScope -import kotlinx.coroutines.launch -import com.android.internal.R as IntR - -private const val IMAGE_FADE_IN_MILLIS = 150L - -class ChooserImagePreviewView : RelativeLayout, ImagePreviewView { - - constructor(context: Context) : this(context, null) - constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) - - constructor( - context: Context, attrs: AttributeSet?, defStyleAttr: Int - ) : this(context, attrs, defStyleAttr, 0) - - constructor( - context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int - ) : super(context, attrs, defStyleAttr, defStyleRes) - - private val coroutineScope = MainScope() - private lateinit var mainImage: RoundedRectImageView - private lateinit var secondLargeImage: RoundedRectImageView - private lateinit var secondSmallImage: RoundedRectImageView - private lateinit var thirdImage: RoundedRectImageView - - private var loadImageJob: Job? = null - private var transitionStatusElementCallback: TransitionElementStatusCallback? = null - - override fun onFinishInflate() { - LayoutInflater.from(context) - .inflate(R.layout.chooser_image_preview_view_internals, this, true) - mainImage = requireViewById(IntR.id.content_preview_image_1_large) - secondLargeImage = requireViewById(IntR.id.content_preview_image_2_large) - secondSmallImage = requireViewById(IntR.id.content_preview_image_2_small) - thirdImage = requireViewById(IntR.id.content_preview_image_3_small) - } - - /** - * Specifies a transition animation target readiness callback. The callback will be - * invoked once when views preparation is done. - * Should be called before [setImages]. - */ - override fun setTransitionElementStatusCallback(callback: TransitionElementStatusCallback?) { - transitionStatusElementCallback = callback - } - - override fun setImages(uris: List<Uri>, imageLoader: ImageLoader) { - loadImageJob?.cancel() - loadImageJob = coroutineScope.launch { - when (uris.size) { - 0 -> hideAllViews() - 1 -> showOneImage(uris, imageLoader) - 2 -> showTwoImages(uris, imageLoader) - else -> showThreeImages(uris, imageLoader) - } - } - } - - private fun hideAllViews() { - mainImage.isVisible = false - secondLargeImage.isVisible = false - secondSmallImage.isVisible = false - thirdImage.isVisible = false - invokeTransitionViewReadyCallback() - } - - private suspend fun showOneImage(uris: List<Uri>, imageLoader: ImageLoader) { - secondLargeImage.isVisible = false - secondSmallImage.isVisible = false - thirdImage.isVisible = false - showImages(uris, imageLoader, mainImage) - } - - private suspend fun showTwoImages(uris: List<Uri>, imageLoader: ImageLoader) { - secondSmallImage.isVisible = false - thirdImage.isVisible = false - showImages(uris, imageLoader, mainImage, secondLargeImage) - } - - private suspend fun showThreeImages(uris: List<Uri>, imageLoader: ImageLoader) { - secondLargeImage.isVisible = false - showImages(uris, imageLoader, mainImage, secondSmallImage, thirdImage) - thirdImage.setExtraImageCount(uris.size - 3) - } - - private suspend fun showImages( - uris: List<Uri>, imageLoader: ImageLoader, vararg views: RoundedRectImageView - ) = coroutineScope { - for (i in views.indices) { - launch { - loadImageIntoView(views[i], uris[i], imageLoader) - } - } - } - - private suspend fun loadImageIntoView( - view: RoundedRectImageView, uri: Uri, imageLoader: ImageLoader - ) { - val bitmap = runCatching { - imageLoader(uri) - }.getOrDefault(null) - if (bitmap == null) { - view.isVisible = false - if (view === mainImage) { - invokeTransitionViewReadyCallback() - } - } else { - view.isVisible = true - view.setImageBitmap(bitmap) - - view.alpha = 0f - ObjectAnimator.ofFloat(view, "alpha", 0.0f, 1.0f).apply { - interpolator = DecelerateInterpolator(1.0f) - duration = IMAGE_FADE_IN_MILLIS - start() - } - if (view === mainImage && transitionStatusElementCallback != null) { - view.waitForPreDraw() - invokeTransitionViewReadyCallback() - } - } - } - - private fun invokeTransitionViewReadyCallback() { - transitionStatusElementCallback?.apply { - if (mainImage.isVisible && mainImage.drawable != null) { - mainImage.transitionName?.let { onTransitionElementReady(it) } - } - onAllTransitionElementsReady() - } - transitionStatusElementCallback = null - } -} diff --git a/java/src/com/android/intentresolver/widget/ImagePreviewView.kt b/java/src/com/android/intentresolver/widget/ImagePreviewView.kt index a166ef27..3f0458ee 100644 --- a/java/src/com/android/intentresolver/widget/ImagePreviewView.kt +++ b/java/src/com/android/intentresolver/widget/ImagePreviewView.kt @@ -16,14 +16,11 @@ package com.android.intentresolver.widget -import android.graphics.Bitmap -import android.net.Uri - -internal typealias ImageLoader = suspend (Uri) -> Bitmap? +import android.view.View interface ImagePreviewView { fun setTransitionElementStatusCallback(callback: TransitionElementStatusCallback?) - fun setImages(uris: List<Uri>, imageLoader: ImageLoader) + fun getTransitionView(): View? /** * [ImagePreviewView] progressively prepares views for shared element transition and reports diff --git a/java/src/com/android/intentresolver/widget/ResolverDrawerLayout.java b/java/src/com/android/intentresolver/widget/ResolverDrawerLayout.java index f5e20510..de76a1d2 100644 --- a/java/src/com/android/intentresolver/widget/ResolverDrawerLayout.java +++ b/java/src/com/android/intentresolver/widget/ResolverDrawerLayout.java @@ -841,7 +841,14 @@ public class ResolverDrawerLayout extends ViewGroup { @Override public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) { - if (!consumed && Math.abs(velocityY) > mMinFlingVelocity) { + // TODO: find a more suitable way to fix it. + // RecyclerView started reporting `consumed` as true whenever a scrolling is enabled, + // previously the value was based whether the fling can be performed in given direction + // i.e. whether it is at the top or at the bottom. isRecyclerViewAtTheTop method is a + // workaround that restores the legacy functionality. + boolean shouldConsume = (Math.abs(velocityY) > mMinFlingVelocity) + && (!consumed || (velocityY < 0 && isRecyclerViewAtTheTop(target))); + if (shouldConsume) { if (getShowAtTop()) { if (isDismissable() && velocityY > 0) { abortAnimation(); @@ -863,6 +870,21 @@ public class ResolverDrawerLayout extends ViewGroup { return false; } + private static boolean isRecyclerViewAtTheTop(View target) { + // TODO: there's a very similar functionality in #isNestedRecyclerChildScrolled(), + // consolidate the two. + if (!(target instanceof RecyclerView)) { + return false; + } + RecyclerView recyclerView = (RecyclerView) target; + if (recyclerView.getChildCount() == 0) { + return true; + } + View firstChild = recyclerView.getChildAt(0); + return recyclerView.getChildAdapterPosition(firstChild) == 0 + && firstChild.getTop() >= recyclerView.getPaddingTop(); + } + private boolean performAccessibilityActionCommon(int action) { switch (action) { case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: diff --git a/java/src/com/android/intentresolver/widget/RoundedRectImageView.java b/java/src/com/android/intentresolver/widget/RoundedRectImageView.java index 8538041b..8ca6ed14 100644 --- a/java/src/com/android/intentresolver/widget/RoundedRectImageView.java +++ b/java/src/com/android/intentresolver/widget/RoundedRectImageView.java @@ -17,6 +17,7 @@ package com.android.intentresolver.widget; import android.content.Context; +import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; @@ -52,7 +53,17 @@ public class RoundedRectImageView extends ImageView { public RoundedRectImageView( Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); - mRadius = context.getResources().getDimensionPixelSize(R.dimen.chooser_corner_radius); + + final TypedArray a = context.obtainStyledAttributes( + attrs, + R.styleable.RoundedRectImageView, + defStyleAttr, + 0); + mRadius = a.getDimensionPixelSize(R.styleable.RoundedRectImageView_radius, -1); + if (mRadius < 0) { + mRadius = context.getResources().getDimensionPixelSize(R.dimen.chooser_corner_radius); + } + a.recycle(); mOverlayPaint.setColor(0x99000000); mOverlayPaint.setStyle(Paint.Style.FILL); diff --git a/java/src/com/android/intentresolver/widget/ScrollableActionRow.kt b/java/src/com/android/intentresolver/widget/ScrollableActionRow.kt index f2a8b9e8..2b64ca30 100644 --- a/java/src/com/android/intentresolver/widget/ScrollableActionRow.kt +++ b/java/src/com/android/intentresolver/widget/ScrollableActionRow.kt @@ -17,12 +17,14 @@ package com.android.intentresolver.widget import android.content.Context +import android.graphics.Rect import android.graphics.drawable.Drawable import android.util.AttributeSet import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView +import androidx.core.view.ViewCompat import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.android.intentresolver.R @@ -31,13 +33,23 @@ class ScrollableActionRow : RecyclerView, ActionRow { constructor(context: Context) : this(context, null) constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) constructor( - context: Context, attrs: AttributeSet?, defStyleAttr: Int + context: Context, + attrs: AttributeSet?, + defStyleAttr: Int ) : super(context, attrs, defStyleAttr) { layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) adapter = Adapter(context) + + addItemDecoration( + MarginDecoration( + context.resources.getDimensionPixelSize(R.dimen.chooser_action_horizontal_margin), + context.resources.getDimensionPixelSize(R.dimen.chooser_edge_margin_normal) + ) + ) } - private val actionsAdapter get() = adapter as Adapter + private val actionsAdapter + get() = adapter as Adapter override fun setActions(actions: List<ActionRow.Action>) { actionsAdapter.setActions(actions) @@ -50,7 +62,7 @@ class ScrollableActionRow : RecyclerView, ActionRow { ) } - private class Adapter(private val context: Context) : RecyclerView.Adapter<ViewHolder>() { + private inner class Adapter(private val context: Context) : RecyclerView.Adapter<ViewHolder>() { private val iconSize: Int = context.resources.getDimensionPixelSize(R.dimen.chooser_action_view_icon_size) private val itemLayout = R.layout.chooser_action_view @@ -59,7 +71,7 @@ class ScrollableActionRow : RecyclerView, ActionRow { override fun onCreateViewHolder(parent: ViewGroup, type: Int): ViewHolder = ViewHolder( LayoutInflater.from(context).inflate(itemLayout, null) as TextView, - iconSize + iconSize, ) override fun onBindViewHolder(holder: ViewHolder, position: Int) { @@ -83,8 +95,9 @@ class ScrollableActionRow : RecyclerView, ActionRow { } } - private class ViewHolder( - private val view: TextView, private val iconSize: Int + private inner class ViewHolder( + private val view: TextView, + private val iconSize: Int, ) : RecyclerView.ViewHolder(view) { fun bind(action: ActionRow.Action) { @@ -93,12 +106,10 @@ class ScrollableActionRow : RecyclerView, ActionRow { // some drawables (edit) does not gets tinted when set to the top of the text // with TextView#setCompoundDrawableRelative tintIcon(icon, view) - view.setCompoundDrawablesRelative(null, icon, null, null) + view.setCompoundDrawablesRelative(icon, null, null, null) } view.text = action.label ?: "" - view.setOnClickListener { - action.onClicked.run() - } + view.setOnClickListener { action.onClicked.run() } view.id = action.id } @@ -113,4 +124,21 @@ class ScrollableActionRow : RecyclerView, ActionRow { view.compoundDrawableTintBlendMode?.let { drawable.setTintBlendMode(it) } } } + + private class MarginDecoration(private val innerMargin: Int, private val outerMargin: Int) : + ItemDecoration() { + override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: State) { + val index = parent.getChildAdapterPosition(view) + val startMargin = if (index == 0) outerMargin else innerMargin + val endMargin = if (index == state.itemCount - 1) outerMargin else innerMargin + + if (ViewCompat.getLayoutDirection(parent) == ViewCompat.LAYOUT_DIRECTION_RTL) { + outRect.right = startMargin + outRect.left = endMargin + } else { + outRect.left = startMargin + outRect.right = endMargin + } + } + } } diff --git a/java/src/com/android/intentresolver/widget/ScrollableImagePreviewView.kt b/java/src/com/android/intentresolver/widget/ScrollableImagePreviewView.kt index 467c404a..583a2887 100644 --- a/java/src/com/android/intentresolver/widget/ScrollableImagePreviewView.kt +++ b/java/src/com/android/intentresolver/widget/ScrollableImagePreviewView.kt @@ -17,43 +17,126 @@ package com.android.intentresolver.widget import android.content.Context +import android.graphics.Bitmap import android.graphics.Rect import android.net.Uri import android.util.AttributeSet +import android.util.PluralsMessageFormatter import android.util.TypedValue import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ImageView +import android.widget.TextView +import androidx.annotation.VisibleForTesting +import androidx.constraintlayout.widget.ConstraintLayout +import androidx.core.view.ViewCompat import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.android.intentresolver.R +import com.android.intentresolver.util.throttle import com.android.intentresolver.widget.ImagePreviewView.TransitionElementStatusCallback import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job import kotlinx.coroutines.MainScope import kotlinx.coroutines.cancel -import kotlinx.coroutines.isActive +import kotlinx.coroutines.flow.MutableSharedFlow +import kotlinx.coroutines.flow.onCompletion +import kotlinx.coroutines.flow.takeWhile +import kotlinx.coroutines.joinAll import kotlinx.coroutines.launch import kotlinx.coroutines.plus private const val TRANSITION_NAME = "screenshot_preview_image" +private const val PLURALS_COUNT = "count" +private const val ADAPTER_UPDATE_INTERVAL_MS = 150L +private const val MIN_ASPECT_RATIO = 0.4f +private const val MIN_ASPECT_RATIO_STRING = "2:5" +private const val MAX_ASPECT_RATIO = 2.5f +private const val MAX_ASPECT_RATIO_STRING = "5:2" + +private typealias CachingImageLoader = suspend (Uri, Boolean) -> Bitmap? class ScrollableImagePreviewView : RecyclerView, ImagePreviewView { constructor(context: Context) : this(context, null) constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) constructor( - context: Context, attrs: AttributeSet?, defStyleAttr: Int + context: Context, + attrs: AttributeSet?, + defStyleAttr: Int ) : super(context, attrs, defStyleAttr) { layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) adapter = Adapter(context) - val spacing = TypedValue.applyDimension( - TypedValue.COMPLEX_UNIT_DIP, 5f, context.resources.displayMetrics - ).toInt() - addItemDecoration(SpacingDecoration(spacing)) + + context + .obtainStyledAttributes(attrs, R.styleable.ScrollableImagePreviewView, defStyleAttr, 0) + .use { a -> + var innerSpacing = + a.getDimensionPixelSize( + R.styleable.ScrollableImagePreviewView_itemInnerSpacing, + -1 + ) + if (innerSpacing < 0) { + innerSpacing = + TypedValue.applyDimension( + TypedValue.COMPLEX_UNIT_DIP, + 3f, + context.resources.displayMetrics + ) + .toInt() + } + outerSpacing = + a.getDimensionPixelSize( + R.styleable.ScrollableImagePreviewView_itemOuterSpacing, + -1 + ) + if (outerSpacing < 0) { + outerSpacing = + TypedValue.applyDimension( + TypedValue.COMPLEX_UNIT_DIP, + 16f, + context.resources.displayMetrics + ) + .toInt() + } + addItemDecoration(SpacingDecoration(innerSpacing, outerSpacing)) + + maxWidthHint = + a.getDimensionPixelSize(R.styleable.ScrollableImagePreviewView_maxWidthHint, -1) + } } - private val previewAdapter get() = adapter as Adapter + private var batchLoader: BatchPreviewLoader? = null + private val previewAdapter + get() = adapter as Adapter + + /** + * A hint about the maximum width this view can grow to, this helps to optimize preview loading. + */ + var maxWidthHint: Int = -1 + private var requestedHeight: Int = 0 + private var isMeasured = false + private var maxAspectRatio = MAX_ASPECT_RATIO + private var maxAspectRatioString = MAX_ASPECT_RATIO_STRING + private var outerSpacing: Int = 0 + + override fun onMeasure(widthSpec: Int, heightSpec: Int) { + super.onMeasure(widthSpec, heightSpec) + if (!isMeasured) { + isMeasured = true + updateMaxWidthHint(widthSpec) + updateMaxAspectRatio() + batchLoader?.loadAspectRatios(getMaxWidth(), this::updatePreviewSize) + } + } + + private fun updateMaxWidthHint(widthSpec: Int) { + if (maxWidthHint > 0) return + if (View.MeasureSpec.getMode(widthSpec) != View.MeasureSpec.UNSPECIFIED) { + maxWidthHint = View.MeasureSpec.getSize(widthSpec) + } + } override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) { super.onLayout(changed, l, t, r, b) @@ -66,41 +149,200 @@ class ScrollableImagePreviewView : RecyclerView, ImagePreviewView { previewAdapter.transitionStatusElementCallback = callback } - override fun setImages(uris: List<Uri>, imageLoader: ImageLoader) { - previewAdapter.setImages(uris, imageLoader) + override fun getTransitionView(): View? { + for (i in 0 until childCount) { + val child = getChildAt(i) + val vh = getChildViewHolder(child) + if (vh is PreviewViewHolder && vh.image.transitionName != null) return child + } + return null + } + + fun setPreviews(previews: List<Preview>, otherItemCount: Int, imageLoader: CachingImageLoader) { + previewAdapter.reset(0, imageLoader) + batchLoader?.cancel() + batchLoader = + BatchPreviewLoader( + imageLoader, + previews, + otherItemCount, + onReset = { totalItemCount -> + previewAdapter.reset(totalItemCount, imageLoader) + }, + onUpdate = previewAdapter::addPreviews, + onCompletion = { + if (!previewAdapter.hasPreviews) { + onNoPreviewCallback?.run() + } + } + ) + .apply { + if (isMeasured) { + loadAspectRatios( + getMaxWidth(), + this@ScrollableImagePreviewView::updatePreviewSize + ) + } + } + } + + var onNoPreviewCallback: Runnable? = null + + private fun getMaxWidth(): Int = + when { + maxWidthHint > 0 -> maxWidthHint + isLaidOut -> width + else -> measuredWidth + } + + private fun updateMaxAspectRatio() { + val padding = outerSpacing * 2 + val w = maxOf(padding, getMaxWidth() - padding) + val h = if (isLaidOut) height else measuredHeight + if (w > 0 && h > 0) { + maxAspectRatio = + (w.toFloat() / h.toFloat()).coerceIn(MIN_ASPECT_RATIO, MAX_ASPECT_RATIO) + maxAspectRatioString = + when { + maxAspectRatio <= MIN_ASPECT_RATIO -> MIN_ASPECT_RATIO_STRING + maxAspectRatio >= MAX_ASPECT_RATIO -> MAX_ASPECT_RATIO_STRING + else -> "$w:$h" + } + } + } + + /** + * Sets [preview]'s aspect ratio based on the preview image size. + * + * @return adjusted preview width + */ + private fun updatePreviewSize(preview: Preview, width: Int, height: Int): Int { + val effectiveHeight = if (isLaidOut) height else measuredHeight + return if (width <= 0 || height <= 0) { + preview.aspectRatioString = "1:1" + effectiveHeight + } else { + val aspectRatio = + (width.toFloat() / height.toFloat()).coerceIn(MIN_ASPECT_RATIO, maxAspectRatio) + preview.aspectRatioString = + when { + aspectRatio <= MIN_ASPECT_RATIO -> MIN_ASPECT_RATIO_STRING + aspectRatio >= maxAspectRatio -> maxAspectRatioString + else -> "$width:$height" + } + (effectiveHeight * aspectRatio).toInt() + } + } + + class Preview + internal constructor( + val type: PreviewType, + val uri: Uri, + val editAction: Runnable?, + internal var aspectRatioString: String + ) { + constructor( + type: PreviewType, + uri: Uri, + editAction: Runnable? + ) : this(type, uri, editAction, "1:1") + } + + enum class PreviewType { + Image, + Video, + File } private class Adapter(private val context: Context) : RecyclerView.Adapter<ViewHolder>() { - private val uris = ArrayList<Uri>() - private var imageLoader: ImageLoader? = null + private val previews = ArrayList<Preview>() + private val imagePreviewDescription = + context.resources.getString(R.string.image_preview_a11y_description) + private val videoPreviewDescription = + context.resources.getString(R.string.video_preview_a11y_description) + private val filePreviewDescription = + context.resources.getString(R.string.file_preview_a11y_description) + private var imageLoader: CachingImageLoader? = null + private var firstImagePos = -1 + private var totalItemCount: Int = 0 + + private val hasOtherItem + get() = previews.size < totalItemCount + val hasPreviews: Boolean + get() = previews.isNotEmpty() + var transitionStatusElementCallback: TransitionElementStatusCallback? = null - fun setImages(uris: List<Uri>, imageLoader: ImageLoader) { - this.uris.clear() - this.uris.addAll(uris) + fun reset(totalItemCount: Int, imageLoader: CachingImageLoader) { this.imageLoader = imageLoader + firstImagePos = -1 + previews.clear() + this.totalItemCount = maxOf(0, totalItemCount) notifyDataSetChanged() } + fun addPreviews(newPreviews: Collection<Preview>) { + if (newPreviews.isEmpty()) return + val insertPos = previews.size + val hadOtherItem = hasOtherItem + previews.addAll(newPreviews) + if (firstImagePos < 0) { + val pos = newPreviews.indexOfFirst { it.type == PreviewType.Image } + if (pos >= 0) firstImagePos = insertPos + pos + } + notifyItemRangeInserted(insertPos, newPreviews.size) + when { + hadOtherItem && previews.size >= totalItemCount -> { + notifyItemRemoved(previews.size) + } + !hadOtherItem && previews.size < totalItemCount -> { + notifyItemInserted(previews.size) + } + } + } + override fun onCreateViewHolder(parent: ViewGroup, itemType: Int): ViewHolder { - return ViewHolder( - LayoutInflater.from(context) - .inflate(R.layout.image_preview_image_item, parent, false) - ) + val view = LayoutInflater.from(context).inflate(itemType, parent, false) + return if (itemType == R.layout.image_preview_other_item) { + OtherItemViewHolder(view) + } else { + PreviewViewHolder( + view, + imagePreviewDescription, + videoPreviewDescription, + filePreviewDescription, + ) + } } - override fun getItemCount(): Int = uris.size + override fun getItemCount(): Int = previews.size + if (hasOtherItem) 1 else 0 + + override fun getItemViewType(position: Int): Int { + return if (position == previews.size) { + R.layout.image_preview_other_item + } else { + R.layout.image_preview_image_item + } + } override fun onBindViewHolder(vh: ViewHolder, position: Int) { - vh.bind( - uris[position], - imageLoader ?: error("ImageLoader is missing"), - if (position == 0 && transitionStatusElementCallback != null) { - this::onTransitionElementReady - } else { - null - } - ) + when (vh) { + is OtherItemViewHolder -> vh.bind(totalItemCount - previews.size) + is PreviewViewHolder -> + vh.bind( + previews[position], + imageLoader ?: error("ImageLoader is missing"), + isSharedTransitionElement = position == firstImagePos, + previewReadyCallback = + if ( + position == firstImagePos && transitionStatusElementCallback != null + ) { + this::onTransitionElementReady + } else { + null + } + ) + } } override fun onViewRecycled(vh: ViewHolder) { @@ -121,41 +363,80 @@ class ScrollableImagePreviewView : RecyclerView, ImagePreviewView { } } - private class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { - private val image = view.requireViewById<ImageView>(R.id.image) + private sealed class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { + abstract fun unbind() + } + + private class PreviewViewHolder( + view: View, + private val imagePreviewDescription: String, + private val videoPreviewDescription: String, + private val filePreviewDescription: String, + ) : ViewHolder(view) { + val image = view.requireViewById<ImageView>(R.id.image) + private val badgeFrame = view.requireViewById<View>(R.id.badge_frame) + private val badge = view.requireViewById<ImageView>(R.id.badge) + private val editActionContainer = view.findViewById<View?>(R.id.edit) private var scope: CoroutineScope? = null fun bind( - uri: Uri, - imageLoader: ImageLoader, + preview: Preview, + imageLoader: CachingImageLoader, + isSharedTransitionElement: Boolean, previewReadyCallback: ((String) -> Unit)? ) { image.setImageDrawable(null) - image.transitionName = if (previewReadyCallback != null) { - TRANSITION_NAME - } else { - null + (image.layoutParams as? ConstraintLayout.LayoutParams)?.let { params -> + params.dimensionRatio = preview.aspectRatioString + } + image.transitionName = + if (isSharedTransitionElement) { + TRANSITION_NAME + } else { + null + } + when (preview.type) { + PreviewType.Image -> { + itemView.contentDescription = imagePreviewDescription + badgeFrame.visibility = View.GONE + } + PreviewType.Video -> { + itemView.contentDescription = videoPreviewDescription + badge.setImageResource(R.drawable.ic_file_video) + badgeFrame.visibility = View.VISIBLE + } + else -> { + itemView.contentDescription = filePreviewDescription + badge.setImageResource(R.drawable.chooser_file_generic) + badgeFrame.visibility = View.VISIBLE + } + } + preview.editAction?.also { onClick -> + editActionContainer?.apply { + setOnClickListener { onClick.run() } + visibility = View.VISIBLE + } } resetScope().launch { - loadImage(uri, imageLoader, previewReadyCallback) + loadImage(preview, imageLoader) + if (preview.type == PreviewType.Image) { + previewReadyCallback?.let { callback -> + image.waitForPreDraw() + callback(TRANSITION_NAME) + } + } } } - private suspend fun loadImage( - uri: Uri, - imageLoader: ImageLoader, - previewReadyCallback: ((String) -> Unit)? - ) { - val bitmap = runCatching { - // it's expected for all loading/caching optimizations to be implemented by the - // loader - imageLoader(uri) - }.getOrNull() + private suspend fun loadImage(preview: Preview, imageLoader: CachingImageLoader) { + val bitmap = + runCatching { + // it's expected for all loading/caching optimizations to be implemented by + // the loader + imageLoader(preview.uri, true) + } + .getOrNull() image.setImageBitmap(bitmap) - previewReadyCallback?.let { callback -> - image.waitForPreDraw() - callback(TRANSITION_NAME) - } } private fun resetScope(): CoroutineScope = @@ -164,15 +445,153 @@ class ScrollableImagePreviewView : RecyclerView, ImagePreviewView { scope = it } - fun unbind() { + override fun unbind() { scope?.cancel() scope = null } } - private class SpacingDecoration(private val margin: Int) : RecyclerView.ItemDecoration() { + private class OtherItemViewHolder(view: View) : ViewHolder(view) { + private val label = view.requireViewById<TextView>(R.id.label) + + fun bind(count: Int) { + label.text = + PluralsMessageFormatter.format( + itemView.context.resources, + mapOf(PLURALS_COUNT to count), + R.string.other_files + ) + } + + override fun unbind() = Unit + } + + private class SpacingDecoration(private val innerSpacing: Int, private val outerSpacing: Int) : + ItemDecoration() { override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: State) { - outRect.set(margin, 0, margin, 0) + val itemCount = parent.adapter?.itemCount ?: return + val pos = parent.getChildAdapterPosition(view) + var startMargin = if (pos == 0) outerSpacing else innerSpacing + var endMargin = if (pos == itemCount - 1) outerSpacing else 0 + + if (ViewCompat.getLayoutDirection(parent) == ViewCompat.LAYOUT_DIRECTION_RTL) { + outRect.set(endMargin, 0, startMargin, 0) + } else { + outRect.set(startMargin, 0, endMargin, 0) + } + } + } + + @VisibleForTesting + class BatchPreviewLoader( + private val imageLoader: CachingImageLoader, + previews: List<Preview>, + otherItemCount: Int, + private val onReset: (Int) -> Unit, + private val onUpdate: (List<Preview>) -> Unit, + private val onCompletion: () -> Unit, + ) { + private val previews: List<Preview> = + if (previews is RandomAccess) previews else ArrayList(previews) + private val totalItemCount = previews.size + otherItemCount + private var scope: CoroutineScope? = MainScope() + Dispatchers.Main.immediate + + fun cancel() { + scope?.cancel() + scope = null + } + + fun loadAspectRatios(maxWidth: Int, previewSizeUpdater: (Preview, Int, Int) -> Int) { + val scope = this.scope ?: return + // -1 encodes that the preview has not been processed, + // 0 means failed, > 0 is a preview width + val previewWidths = IntArray(previews.size) { -1 } + var blockStart = 0 // inclusive + var blockEnd = 0 // exclusive + + // replay 2 items to guarantee that we'd get at least one update + val reportFlow = MutableSharedFlow<Any>(replay = 2) + val updateEvent = Any() + val completedEvent = Any() + + // throttle adapter updates using flow; the flow first emits when enough preview + // elements is loaded to fill the viewport and then each time a subsequent block of + // previews is loaded + scope.launch(Dispatchers.Main) { + reportFlow + .takeWhile { it !== completedEvent } + .throttle(ADAPTER_UPDATE_INTERVAL_MS) + .onCompletion { cause -> + if (cause == null) { + onCompletion() + } + } + .collect { + if (blockStart == 0) { + onReset(totalItemCount) + } + val updates = ArrayList<Preview>(blockEnd - blockStart) + while (blockStart < blockEnd) { + if (previewWidths[blockStart] > 0) { + updates.add(previews[blockStart]) + } + blockStart++ + } + if (updates.isNotEmpty()) { + onUpdate(updates) + } + } + } + + scope.launch { + var blockWidth = 0 + var isFirstBlock = true + var nextIdx = 0 + List<Job>(4) { + launch { + while (true) { + val i = nextIdx++ + if (i >= previews.size) break + val preview = previews[i] + + previewWidths[i] = + runCatching { + // TODO: decide on adding a timeout + imageLoader(preview.uri, isFirstBlock)?.let { bitmap -> + previewSizeUpdater( + preview, + bitmap.width, + bitmap.height + ) + } + ?: 0 + } + .getOrDefault(0) + + if (blockEnd != i) continue + while ( + blockEnd < previewWidths.size && previewWidths[blockEnd] >= 0 + ) { + blockWidth += previewWidths[blockEnd] + blockEnd++ + } + if (isFirstBlock) { + if (blockWidth >= maxWidth) { + isFirstBlock = false + // notify that the preview now can be displayed + reportFlow.emit(updateEvent) + } + } else { + reportFlow.emit(updateEvent) + } + } + } + } + .joinAll() + // in case all previews have failed to load + reportFlow.emit(updateEvent) + reportFlow.emit(completedEvent) + } } } } |