1 /*
2  * Copyright (C) 2022 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.internal.pm.pkg.component;
18 
19 import static com.android.internal.pm.parsing.pkg.PackageImpl.sForInternedString;
20 
21 import static java.util.Collections.emptyMap;
22 
23 import android.annotation.CallSuper;
24 import android.annotation.NonNull;
25 import android.annotation.Nullable;
26 import android.content.ComponentName;
27 import android.content.pm.PackageManager.Property;
28 import android.os.Bundle;
29 import android.os.Parcel;
30 import android.os.Parcelable;
31 import android.text.TextUtils;
32 import android.util.ArrayMap;
33 
34 import com.android.internal.annotations.VisibleForTesting;
35 import com.android.internal.pm.pkg.parsing.ParsingUtils;
36 import com.android.internal.util.CollectionUtils;
37 import com.android.internal.util.DataClass;
38 import com.android.internal.util.Parcelling.BuiltIn.ForInternedString;
39 
40 import java.util.ArrayList;
41 import java.util.Collections;
42 import java.util.List;
43 import java.util.Map;
44 
45 /**
46  * @hide
47  */
48 @DataClass(genGetters = true, genSetters = true, genConstructor = false, genBuilder = false,
49         genParcelable = false)
50 @DataClass.Suppress({"setComponentName", "setProperties", "setIntents"})
51 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
52 public abstract class ParsedComponentImpl implements ParsedComponent, Parcelable {
53 
54     @NonNull
55     @DataClass.ParcelWith(ForInternedString.class)
56     private String name;
57     private int icon;
58     private int labelRes;
59     @Nullable
60     private CharSequence nonLocalizedLabel;
61     private int logo;
62     private int banner;
63     private int descriptionRes;
64 
65     // TODO(b/135203078): Replace flags with individual booleans, scoped by subclass
66     private int flags;
67 
68     @NonNull
69     @DataClass.ParcelWith(ForInternedString.class)
70     private String packageName;
71 
72     @NonNull
73     @DataClass.PluralOf("intent")
74     private List<ParsedIntentInfoImpl> intents = Collections.emptyList();
75 
76     @Nullable
77     private ComponentName componentName;
78 
79     @Nullable
80     private Bundle metaData;
81 
82     @NonNull
83     private Map<String, Property> mProperties = emptyMap();
84 
ParsedComponentImpl()85     public ParsedComponentImpl() {
86 
87     }
88 
ParsedComponentImpl(ParsedComponent other)89     protected ParsedComponentImpl(ParsedComponent other) {
90         this.metaData = other.getMetaData();
91         this.name = other.getName();
92         this.icon = other.getIcon();
93         this.labelRes = other.getLabelRes();
94         this.nonLocalizedLabel = other.getNonLocalizedLabel();
95         this.logo = other.getLogo();
96         this.banner = other.getBanner();
97         this.descriptionRes = other.getDescriptionRes();
98         this.flags = other.getFlags();
99         this.packageName = other.getPackageName();
100         this.componentName = other.getComponentName();
101         this.intents = new ArrayList<>(((ParsedComponentImpl) other).intents);
102         this.mProperties = new ArrayMap<>();
103         this.mProperties.putAll(other.getProperties());
104     }
105 
addIntent(ParsedIntentInfoImpl intent)106     public void addIntent(ParsedIntentInfoImpl intent) {
107         this.intents = CollectionUtils.add(this.intents, intent);
108     }
109 
110     /**
111      * Add a property to the component
112      */
addProperty(@onNull Property property)113     public void addProperty(@NonNull Property property) {
114         this.mProperties = CollectionUtils.add(this.mProperties, property.getName(), property);
115     }
116 
setName(String name)117     public ParsedComponentImpl setName(String name) {
118         this.name = TextUtils.safeIntern(name);
119         return this;
120     }
121 
122     @CallSuper
setPackageName(@onNull String packageName)123     public void setPackageName(@NonNull String packageName) {
124         this.packageName = TextUtils.safeIntern(packageName);
125         //noinspection ConstantConditions
126         this.componentName = null;
127 
128         // Note: this method does not edit name (which can point to a class), because this package
129         // name change is not changing the package in code, but the identifier used by the system.
130     }
131 
132     @Override
133     @NonNull
getComponentName()134     public ComponentName getComponentName() {
135         if (componentName == null) {
136             componentName = new ComponentName(getPackageName(), getName());
137         }
138         return componentName;
139     }
140 
141     @NonNull
142     @Override
getMetaData()143     public Bundle getMetaData() {
144         return metaData == null ? Bundle.EMPTY : metaData;
145     }
146 
147     @NonNull
148     @Override
getIntents()149     public List<ParsedIntentInfo> getIntents() {
150         return new ArrayList<>(intents);
151     }
152 
153     @Override
describeContents()154     public int describeContents() {
155         return 0;
156     }
157 
158     @Override
writeToParcel(Parcel dest, int flags)159     public void writeToParcel(Parcel dest, int flags) {
160         dest.writeString(this.name);
161         dest.writeInt(this.getIcon());
162         dest.writeInt(this.getLabelRes());
163         dest.writeCharSequence(this.getNonLocalizedLabel());
164         dest.writeInt(this.getLogo());
165         dest.writeInt(this.getBanner());
166         dest.writeInt(this.getDescriptionRes());
167         dest.writeInt(this.getFlags());
168         sForInternedString.parcel(this.packageName, dest, flags);
169         dest.writeTypedList(this.intents);
170         dest.writeBundle(this.metaData);
171         dest.writeMap(this.mProperties);
172     }
173 
ParsedComponentImpl(Parcel in)174     protected ParsedComponentImpl(Parcel in) {
175         // We use the boot classloader for all classes that we load.
176         final ClassLoader boot = Object.class.getClassLoader();
177         //noinspection ConstantConditions
178         this.name = in.readString();
179         this.icon = in.readInt();
180         this.labelRes = in.readInt();
181         this.nonLocalizedLabel = in.readCharSequence();
182         this.logo = in.readInt();
183         this.banner = in.readInt();
184         this.descriptionRes = in.readInt();
185         this.flags = in.readInt();
186         //noinspection ConstantConditions
187         this.packageName = sForInternedString.unparcel(in);
188         this.intents = ParsingUtils.createTypedInterfaceList(in, ParsedIntentInfoImpl.CREATOR);
189         this.metaData = in.readBundle(boot);
190         this.mProperties = in.readHashMap(boot);
191     }
192 
193 
194 
195     // Code below generated by codegen v1.0.23.
196     //
197     // DO NOT MODIFY!
198     // CHECKSTYLE:OFF Generated code
199     //
200     // To regenerate run:
201     // $ codegen $ANDROID_BUILD_TOP/frameworks/base/core/java/com/android/internal/pm/pkg/component/ParsedComponentImpl.java
202     //
203     // To exclude the generated code from IntelliJ auto-formatting enable (one-time):
204     //   Settings > Editor > Code Style > Formatter Control
205     //@formatter:off
206 
207 
208     @DataClass.Generated.Member
getName()209     public @NonNull String getName() {
210         return name;
211     }
212 
213     @DataClass.Generated.Member
getIcon()214     public int getIcon() {
215         return icon;
216     }
217 
218     @DataClass.Generated.Member
getLabelRes()219     public int getLabelRes() {
220         return labelRes;
221     }
222 
223     @DataClass.Generated.Member
getNonLocalizedLabel()224     public @Nullable CharSequence getNonLocalizedLabel() {
225         return nonLocalizedLabel;
226     }
227 
228     @DataClass.Generated.Member
getLogo()229     public int getLogo() {
230         return logo;
231     }
232 
233     @DataClass.Generated.Member
getBanner()234     public int getBanner() {
235         return banner;
236     }
237 
238     @DataClass.Generated.Member
getDescriptionRes()239     public int getDescriptionRes() {
240         return descriptionRes;
241     }
242 
243     @DataClass.Generated.Member
getFlags()244     public int getFlags() {
245         return flags;
246     }
247 
248     @DataClass.Generated.Member
getPackageName()249     public @NonNull String getPackageName() {
250         return packageName;
251     }
252 
253     @DataClass.Generated.Member
getProperties()254     public @NonNull Map<String,Property> getProperties() {
255         return mProperties;
256     }
257 
258     @DataClass.Generated.Member
setIcon( int value)259     public @NonNull ParsedComponentImpl setIcon( int value) {
260         icon = value;
261         return this;
262     }
263 
264     @DataClass.Generated.Member
setLabelRes( int value)265     public @NonNull ParsedComponentImpl setLabelRes( int value) {
266         labelRes = value;
267         return this;
268     }
269 
270     @DataClass.Generated.Member
setNonLocalizedLabel(@onNull CharSequence value)271     public @NonNull ParsedComponentImpl setNonLocalizedLabel(@NonNull CharSequence value) {
272         nonLocalizedLabel = value;
273         return this;
274     }
275 
276     @DataClass.Generated.Member
setLogo( int value)277     public @NonNull ParsedComponentImpl setLogo( int value) {
278         logo = value;
279         return this;
280     }
281 
282     @DataClass.Generated.Member
setBanner( int value)283     public @NonNull ParsedComponentImpl setBanner( int value) {
284         banner = value;
285         return this;
286     }
287 
288     @DataClass.Generated.Member
setDescriptionRes( int value)289     public @NonNull ParsedComponentImpl setDescriptionRes( int value) {
290         descriptionRes = value;
291         return this;
292     }
293 
294     @DataClass.Generated.Member
setFlags( int value)295     public @NonNull ParsedComponentImpl setFlags( int value) {
296         flags = value;
297         return this;
298     }
299 
300     @DataClass.Generated.Member
setMetaData(@onNull Bundle value)301     public @NonNull ParsedComponentImpl setMetaData(@NonNull Bundle value) {
302         metaData = value;
303         return this;
304     }
305 
306     @DataClass.Generated(
307             time = 1701445673589L,
308             codegenVersion = "1.0.23",
309             sourceFile = "frameworks/base/core/java/com/android/internal/pm/pkg/component/ParsedComponentImpl.java",
310             inputSignatures = "private @android.annotation.NonNull @com.android.internal.util.DataClass.ParcelWith(com.android.internal.util.Parcelling.BuiltIn.ForInternedString.class) java.lang.String name\nprivate  int icon\nprivate  int labelRes\nprivate @android.annotation.Nullable java.lang.CharSequence nonLocalizedLabel\nprivate  int logo\nprivate  int banner\nprivate  int descriptionRes\nprivate  int flags\nprivate @android.annotation.NonNull @com.android.internal.util.DataClass.ParcelWith(com.android.internal.util.Parcelling.BuiltIn.ForInternedString.class) java.lang.String packageName\nprivate @android.annotation.NonNull @com.android.internal.util.DataClass.PluralOf(\"intent\") java.util.List<com.android.internal.pm.pkg.component.ParsedIntentInfoImpl> intents\nprivate @android.annotation.Nullable android.content.ComponentName componentName\nprivate @android.annotation.Nullable android.os.Bundle metaData\nprivate @android.annotation.NonNull java.util.Map<java.lang.String,android.content.pm.PackageManager.Property> mProperties\npublic  void addIntent(com.android.internal.pm.pkg.component.ParsedIntentInfoImpl)\npublic  void addProperty(android.content.pm.PackageManager.Property)\npublic  com.android.internal.pm.pkg.component.ParsedComponentImpl setName(java.lang.String)\npublic @android.annotation.CallSuper void setPackageName(java.lang.String)\npublic @java.lang.Override @android.annotation.NonNull android.content.ComponentName getComponentName()\npublic @android.annotation.NonNull @java.lang.Override android.os.Bundle getMetaData()\npublic @android.annotation.NonNull @java.lang.Override java.util.List<com.android.internal.pm.pkg.component.ParsedIntentInfo> getIntents()\npublic @java.lang.Override int describeContents()\npublic @java.lang.Override void writeToParcel(android.os.Parcel,int)\nclass ParsedComponentImpl extends java.lang.Object implements [com.android.internal.pm.pkg.component.ParsedComponent, android.os.Parcelable]\n@com.android.internal.util.DataClass(genGetters=true, genSetters=true, genConstructor=false, genBuilder=false, genParcelable=false)")
311     @Deprecated
__metadata()312     private void __metadata() {}
313 
314 
315     //@formatter:on
316     // End of generated code
317 
318 }
319