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