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.app.Activity;
20 import android.content.Context;
21 import android.graphics.drawable.AnimatedVectorDrawable;
22 import android.graphics.drawable.Drawable;
23 import android.os.Bundle;
24 import android.os.UserHandle;
25 
26 import androidx.annotation.Nullable;
27 
28 import com.android.intentresolver.R;
29 
30 import java.util.function.Supplier;
31 
32 /**
33  * Distinguish between targets that selectable by the user, vs those that are
34  * placeholders for the system while information is loading in an async manner.
35  */
36 public final class NotSelectableTargetInfo {
37     /** Create a non-selectable {@link TargetInfo} with no content. */
newEmptyTargetInfo()38     public static TargetInfo newEmptyTargetInfo() {
39         return ImmutableTargetInfo.newBuilder()
40                 .setLegacyType(ImmutableTargetInfo.LegacyTargetType.EMPTY_TARGET_INFO)
41                 .setDisplayIconHolder(makeReadOnlyIconHolder(() -> null))
42                 .setActivityStarter(makeNoOpActivityStarter())
43                 .build();
44     }
45 
46     /**
47      * Create a non-selectable {@link TargetInfo} with placeholder content to be displayed
48      * unless/until it can be replaced by the result of a pending asynchronous load.
49      */
newPlaceHolderTargetInfo(Context context)50     public static TargetInfo newPlaceHolderTargetInfo(Context context) {
51         return ImmutableTargetInfo.newBuilder()
52                 .setLegacyType(ImmutableTargetInfo.LegacyTargetType.PLACEHOLDER_TARGET_INFO)
53                 .setDisplayIconHolder(
54                         makeReadOnlyIconHolder(() -> makeStartedPlaceholderDrawable(context)))
55                 .setActivityStarter(makeNoOpActivityStarter())
56                 .build();
57     }
58 
makeStartedPlaceholderDrawable(Context context)59     private static Drawable makeStartedPlaceholderDrawable(Context context) {
60         AnimatedVectorDrawable avd = (AnimatedVectorDrawable) context.getDrawable(
61                 R.drawable.chooser_direct_share_icon_placeholder);
62         avd.start();  // Start animation after generation.
63         return avd;
64     }
65 
makeReadOnlyIconHolder( Supplier< Drawable> iconProvider)66     private static ImmutableTargetInfo.IconHolder makeReadOnlyIconHolder(
67             Supplier</* @Nullable */ Drawable> iconProvider) {
68         return new ImmutableTargetInfo.IconHolder() {
69             @Override
70             @Nullable
71             public Drawable getDisplayIcon() {
72                 return iconProvider.get();
73             }
74 
75             @Override
76             public void setDisplayIcon(Drawable icon) {}
77         };
78     }
79 
80     private static ImmutableTargetInfo.TargetActivityStarter makeNoOpActivityStarter() {
81         return new ImmutableTargetInfo.TargetActivityStarter() {
82             @Override
83             public boolean startAsCaller(
84                     TargetInfo target, Activity activity, Bundle options, int userId) {
85                 return false;
86             }
87 
88             @Override
89             public boolean startAsUser(
90                     TargetInfo target, Activity activity, Bundle options, UserHandle user) {
91                 return false;
92             }
93         };
94     }
95 
96     // TODO: merge all the APIs up to a single `TargetInfo` class.
97     private NotSelectableTargetInfo() {}
98 }
99