• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Intent;
21 import android.os.Bundle;
22 import android.os.UserHandle;
23 
24 import androidx.annotation.Nullable;
25 
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.List;
29 
30 /**
31  * Represents a "stack" of chooser targets for various activities within the same component.
32  */
33 public class MultiDisplayResolveInfo extends DisplayResolveInfo {
34 
35     final ArrayList<DisplayResolveInfo> mTargetInfos;
36 
37     // Index of selected target
38     private int mSelected = -1;
39 
40     /**
41      * @param targetInfos A list of targets in this stack. The first item is treated as the
42      * "representative" that provides the main icon, title, etc.
43      */
newMultiDisplayResolveInfo( List<DisplayResolveInfo> targetInfos)44     public static MultiDisplayResolveInfo newMultiDisplayResolveInfo(
45             List<DisplayResolveInfo> targetInfos) {
46         return new MultiDisplayResolveInfo(targetInfos);
47     }
48 
49     /**
50      * @param targetInfos A list of targets in this stack. The first item is treated as the
51      * "representative" that provides the main icon, title, etc.
52      */
MultiDisplayResolveInfo(List<DisplayResolveInfo> targetInfos)53     private MultiDisplayResolveInfo(List<DisplayResolveInfo> targetInfos) {
54         super(targetInfos.get(0));
55         mTargetInfos = new ArrayList<>(targetInfos);
56     }
57 
58     @Override
isMultiDisplayResolveInfo()59     public final boolean isMultiDisplayResolveInfo() {
60         return true;
61     }
62 
63     @Override
getExtendedInfo()64     public CharSequence getExtendedInfo() {
65         // Never show subtitle for stacked apps
66         return null;
67     }
68 
69     /**
70      * List of all {@link DisplayResolveInfo}s included in this target.
71      * TODO: provide as a generic {@code List<DisplayResolveInfo>} once
72      *  {@link com.android.intentresolver.ChooserActivity} stops requiring the signature to match
73      *  that of the other "lists" it builds up.
74      */
75     @Override
getAllDisplayTargets()76     public ArrayList<DisplayResolveInfo> getAllDisplayTargets() {
77         return mTargetInfos;
78     }
79 
setSelected(int selected)80     public void setSelected(int selected) {
81         mSelected = selected;
82     }
83 
84     /**
85      * Return selected target.
86      */
getSelectedTarget()87     public DisplayResolveInfo getSelectedTarget() {
88         return hasSelected() ? mTargetInfos.get(mSelected) : null;
89     }
90 
91     /**
92      * Whether or not the user has selected a specific target for this MultiInfo.
93      */
hasSelected()94     public boolean hasSelected() {
95         return mSelected >= 0;
96     }
97 
98     @Override
99     @Nullable
tryToCloneWithAppliedRefinement(Intent proposedRefinement)100     public MultiDisplayResolveInfo tryToCloneWithAppliedRefinement(Intent proposedRefinement) {
101         final int size = mTargetInfos.size();
102         ArrayList<DisplayResolveInfo> targetInfos = new ArrayList<>(size);
103         for (int i = 0; i < size; i++) {
104             DisplayResolveInfo target = mTargetInfos.get(i);
105             DisplayResolveInfo targetClone = (i == mSelected)
106                     ? target.tryToCloneWithAppliedRefinement(proposedRefinement)
107                     : new DisplayResolveInfo(target);
108             if (targetClone == null) {
109                 return null;
110             }
111             targetInfos.add(targetClone);
112         }
113         MultiDisplayResolveInfo clone = new MultiDisplayResolveInfo(targetInfos);
114         clone.mSelected = mSelected;
115         return clone;
116     }
117 
118     @Override
startAsCaller(Activity activity, Bundle options, int userId)119     public boolean startAsCaller(Activity activity, Bundle options, int userId) {
120         return mTargetInfos.get(mSelected).startAsCaller(activity, options, userId);
121     }
122 
123     @Override
startAsUser(Activity activity, Bundle options, UserHandle user)124     public boolean startAsUser(Activity activity, Bundle options, UserHandle user) {
125         return mTargetInfos.get(mSelected).startAsUser(activity, options, user);
126     }
127 
128     @Override
getTargetIntent()129     public Intent getTargetIntent() {
130         return mTargetInfos.get(mSelected).getTargetIntent();
131     }
132 
133     @Override
getAllSourceIntents()134     public List<Intent> getAllSourceIntents() {
135         return hasSelected()
136                 ? mTargetInfos.get(mSelected).getAllSourceIntents()
137                 : Collections.emptyList();
138     }
139 }
140