1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.intentresolver.chooser; 18 19 import android.annotation.Nullable; 20 import android.app.Activity; 21 import android.content.Context; 22 import android.graphics.drawable.AnimatedVectorDrawable; 23 import android.graphics.drawable.Drawable; 24 import android.os.Bundle; 25 import android.os.UserHandle; 26 27 import com.android.intentresolver.R; 28 29 import java.util.function.Supplier; 30 31 /** 32 * Distinguish between targets that selectable by the user, vs those that are 33 * placeholders for the system while information is loading in an async manner. 34 */ 35 public final class NotSelectableTargetInfo { 36 /** Create a non-selectable {@link TargetInfo} with no content. */ newEmptyTargetInfo()37 public static TargetInfo newEmptyTargetInfo() { 38 return ImmutableTargetInfo.newBuilder() 39 .setLegacyType(ImmutableTargetInfo.LegacyTargetType.EMPTY_TARGET_INFO) 40 .setDisplayIconHolder(makeReadOnlyIconHolder(() -> null)) 41 .setActivityStarter(makeNoOpActivityStarter()) 42 .build(); 43 } 44 45 /** 46 * Create a non-selectable {@link TargetInfo} with placeholder content to be displayed 47 * unless/until it can be replaced by the result of a pending asynchronous load. 48 */ newPlaceHolderTargetInfo(Context context)49 public static TargetInfo newPlaceHolderTargetInfo(Context context) { 50 return ImmutableTargetInfo.newBuilder() 51 .setLegacyType(ImmutableTargetInfo.LegacyTargetType.PLACEHOLDER_TARGET_INFO) 52 .setDisplayIconHolder( 53 makeReadOnlyIconHolder(() -> makeStartedPlaceholderDrawable(context))) 54 .setActivityStarter(makeNoOpActivityStarter()) 55 .build(); 56 } 57 makeStartedPlaceholderDrawable(Context context)58 private static Drawable makeStartedPlaceholderDrawable(Context context) { 59 AnimatedVectorDrawable avd = (AnimatedVectorDrawable) context.getDrawable( 60 R.drawable.chooser_direct_share_icon_placeholder); 61 avd.start(); // Start animation after generation. 62 return avd; 63 } 64 makeReadOnlyIconHolder( Supplier< Drawable> iconProvider)65 private static ImmutableTargetInfo.IconHolder makeReadOnlyIconHolder( 66 Supplier</* @Nullable */ Drawable> iconProvider) { 67 return new ImmutableTargetInfo.IconHolder() { 68 @Override 69 @Nullable 70 public Drawable getDisplayIcon() { 71 return iconProvider.get(); 72 } 73 74 @Override 75 public void setDisplayIcon(Drawable icon) {} 76 }; 77 } 78 79 private static ImmutableTargetInfo.TargetActivityStarter makeNoOpActivityStarter() { 80 return new ImmutableTargetInfo.TargetActivityStarter() { 81 @Override 82 public boolean startAsCaller( 83 TargetInfo target, Activity activity, Bundle options, int userId) { 84 return false; 85 } 86 87 @Override 88 public boolean startAsUser( 89 TargetInfo target, Activity activity, Bundle options, UserHandle user) { 90 return false; 91 } 92 }; 93 } 94 95 // TODO: merge all the APIs up to a single `TargetInfo` class. 96 private NotSelectableTargetInfo() {} 97 } 98