1 /*
2  * Copyright (C) 2023 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;
18 
19 import android.content.ComponentName;
20 import android.content.Intent;
21 import android.content.pm.ResolveInfo;
22 
23 import com.android.intentresolver.chooser.TargetInfo;
24 
25 import java.util.ArrayList;
26 import java.util.List;
27 
28 /**
29  * Record type to store all resolutions that are deduped to a single target component, along with
30  * other metadata about the component (which applies to all of the resolutions in the record).
31  * This record is assembled when we're first processing resolutions, and then later it's used to
32  * derive the {@link TargetInfo} record(s) that specify how the resolutions will be presented as
33  * targets in the UI.
34  */
35 public final class ResolvedComponentInfo {
36     public final ComponentName name;
37     private final List<Intent> mIntents = new ArrayList<>();
38     private final List<ResolveInfo> mResolveInfos = new ArrayList<>();
39     private boolean mPinned;
40 
41     /**
42      * @param name the name of the component that owns all the resolutions added to this record.
43      * @param intent an initial {@link Intent} to add to this record
44      * @param info the {@link ResolveInfo} associated with the given {@code intent}.
45      */
ResolvedComponentInfo(ComponentName name, Intent intent, ResolveInfo info)46     public ResolvedComponentInfo(ComponentName name, Intent intent, ResolveInfo info) {
47         this.name = name;
48         add(intent, info);
49     }
50 
51     /**
52      * Add an {@link Intent} and associated {@link ResolveInfo} as resolutions for this component.
53      */
add(Intent intent, ResolveInfo info)54     public void add(Intent intent, ResolveInfo info) {
55         mIntents.add(intent);
56         mResolveInfos.add(info);
57     }
58 
59     /** @return the number of {@link Intent}/{@link ResolveInfo} pairs added to this record. */
getCount()60     public int getCount() {
61         return mIntents.size();
62     }
63 
64     /** @return the {@link Intent} at the specified {@code index}, if any, or else null. */
getIntentAt(int index)65     public Intent getIntentAt(int index) {
66         return (index >= 0) ? mIntents.get(index) : null;
67     }
68 
69     /** @return the {@link ResolveInfo} at the specified {@code index}, if any, or else null. */
getResolveInfoAt(int index)70     public ResolveInfo getResolveInfoAt(int index) {
71         return (index >= 0) ? mResolveInfos.get(index) : null;
72     }
73 
74     /**
75      * @return the index of the provided {@link Intent} among those that have been added to this
76      * {@link ResolvedComponentInfo}, or -1 if it has't been added.
77      */
findIntent(Intent intent)78     public int findIntent(Intent intent) {
79         return mIntents.indexOf(intent);
80     }
81 
82     /**
83      * @return the index of the provided {@link ResolveInfo} among those that have been added to
84      * this {@link ResolvedComponentInfo}, or -1 if it has't been added.
85      */
findResolveInfo(ResolveInfo info)86     public int findResolveInfo(ResolveInfo info) {
87         return mResolveInfos.indexOf(info);
88     }
89 
90     /**
91      * @return whether this component was pinned by a call to {@link #setPinned}.
92      * TODO: consolidate sources of pinning data and/or document how this differs from other places
93      * we make a "pinning" determination.
94      */
isPinned()95     public boolean isPinned() {
96         return mPinned;
97     }
98 
99     /**
100      * Set whether this component will be considered pinned in future calls to {@link #isPinned()}.
101      * TODO: consolidate sources of pinning data and/or document how this differs from other places
102      * we make a "pinning" determination.
103      */
setPinned(boolean pinned)104     public void setPinned(boolean pinned) {
105         mPinned = pinned;
106     }
107 }
108