• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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 android.content.pm;
18 
19 import android.compat.annotation.UnsupportedAppUsage;
20 import android.content.ComponentName;
21 import android.graphics.drawable.Drawable;
22 import android.os.Parcel;
23 import android.util.Printer;
24 
25 /**
26  * Base class containing information common to all application components
27  * ({@link ActivityInfo}, {@link ServiceInfo}).  This class is not intended
28  * to be used by itself; it is simply here to share common definitions
29  * between all application components.  As such, it does not itself
30  * implement Parcelable, but does provide convenience methods to assist
31  * in the implementation of Parcelable in subclasses.
32  */
33 public class ComponentInfo extends PackageItemInfo {
34     /**
35      * Global information about the application/package this component is a
36      * part of.
37      */
38     public ApplicationInfo applicationInfo;
39 
40     /**
41      * The name of the process this component should run in.
42      * From the "android:process" attribute or, if not set, the same
43      * as <var>applicationInfo.processName</var>.
44      */
45     public String processName;
46 
47     /**
48      * The name of the split in which this component is declared.
49      * Null if the component was declared in the base APK.
50      */
51     public String splitName;
52 
53     /**
54      * A string resource identifier (in the package's resources) containing
55      * a user-readable description of the component.  From the "description"
56      * attribute or, if not set, 0.
57      */
58     public int descriptionRes;
59 
60     /**
61      * Indicates whether or not this component may be instantiated.  Note that this value can be
62      * overridden by the one in its parent {@link ApplicationInfo}.
63      */
64     public boolean enabled = true;
65 
66     /**
67      * Set to true if this component is available for use by other applications.
68      * Comes from {@link android.R.attr#exported android:exported} of the
69      * &lt;activity&gt;, &lt;receiver&gt;, &lt;service&gt;, or
70      * &lt;provider&gt; tag.
71      */
72     public boolean exported = false;
73 
74     /**
75      * Indicates if this component is aware of direct boot lifecycle, and can be
76      * safely run before the user has entered their credentials (such as a lock
77      * pattern or PIN).
78      */
79     public boolean directBootAware = false;
80 
ComponentInfo()81     public ComponentInfo() {
82     }
83 
ComponentInfo(ComponentInfo orig)84     public ComponentInfo(ComponentInfo orig) {
85         super(orig);
86         applicationInfo = orig.applicationInfo;
87         processName = orig.processName;
88         splitName = orig.splitName;
89         descriptionRes = orig.descriptionRes;
90         enabled = orig.enabled;
91         exported = orig.exported;
92         directBootAware = orig.directBootAware;
93     }
94 
95     /** @hide */
loadUnsafeLabel(PackageManager pm)96     @Override public CharSequence loadUnsafeLabel(PackageManager pm) {
97         if (nonLocalizedLabel != null) {
98             return nonLocalizedLabel;
99         }
100         ApplicationInfo ai = applicationInfo;
101         CharSequence label;
102         if (labelRes != 0) {
103             label = pm.getText(packageName, labelRes, ai);
104             if (label != null) {
105                 return label;
106             }
107         }
108         if (ai.nonLocalizedLabel != null) {
109             return ai.nonLocalizedLabel;
110         }
111         if (ai.labelRes != 0) {
112             label = pm.getText(packageName, ai.labelRes, ai);
113             if (label != null) {
114                 return label;
115             }
116         }
117         return name;
118     }
119 
120     /**
121      * Return whether this component and its enclosing application are enabled.
122      */
isEnabled()123     public boolean isEnabled() {
124         return enabled && applicationInfo.enabled;
125     }
126 
127     /**
128      * Return the icon resource identifier to use for this component.  If
129      * the component defines an icon, that is used; else, the application
130      * icon is used.
131      *
132      * @return The icon associated with this component.
133      */
getIconResource()134     public final int getIconResource() {
135         return icon != 0 ? icon : applicationInfo.icon;
136     }
137 
138     /**
139      * Return the logo resource identifier to use for this component.  If
140      * the component defines a logo, that is used; else, the application
141      * logo is used.
142      *
143      * @return The logo associated with this component.
144      */
getLogoResource()145     public final int getLogoResource() {
146         return logo != 0 ? logo : applicationInfo.logo;
147     }
148 
149     /**
150      * Return the banner resource identifier to use for this component. If the
151      * component defines a banner, that is used; else, the application banner is
152      * used.
153      *
154      * @return The banner associated with this component.
155      */
getBannerResource()156     public final int getBannerResource() {
157         return banner != 0 ? banner : applicationInfo.banner;
158     }
159 
160     /** {@hide} */
161     @UnsupportedAppUsage
getComponentName()162     public ComponentName getComponentName() {
163         return new ComponentName(packageName, name);
164     }
165 
dumpFront(Printer pw, String prefix)166     protected void dumpFront(Printer pw, String prefix) {
167         super.dumpFront(pw, prefix);
168         if (processName != null && !packageName.equals(processName)) {
169             pw.println(prefix + "processName=" + processName);
170         }
171         if (splitName != null) {
172             pw.println(prefix + "splitName=" + splitName);
173         }
174         pw.println(prefix + "enabled=" + enabled + " exported=" + exported
175                 + " directBootAware=" + directBootAware);
176         if (descriptionRes != 0) {
177             pw.println(prefix + "description=" + descriptionRes);
178         }
179     }
180 
dumpBack(Printer pw, String prefix)181     protected void dumpBack(Printer pw, String prefix) {
182         dumpBack(pw, prefix, DUMP_FLAG_ALL);
183     }
184 
dumpBack(Printer pw, String prefix, int dumpFlags)185     void dumpBack(Printer pw, String prefix, int dumpFlags) {
186         if ((dumpFlags & DUMP_FLAG_APPLICATION) != 0) {
187             if (applicationInfo != null) {
188                 pw.println(prefix + "ApplicationInfo:");
189                 applicationInfo.dump(pw, prefix + "  ", dumpFlags);
190             } else {
191                 pw.println(prefix + "ApplicationInfo: null");
192             }
193         }
194         super.dumpBack(pw, prefix);
195     }
196 
writeToParcel(Parcel dest, int parcelableFlags)197     public void writeToParcel(Parcel dest, int parcelableFlags) {
198         super.writeToParcel(dest, parcelableFlags);
199         applicationInfo.writeToParcel(dest, parcelableFlags);
200         dest.writeString8(processName);
201         dest.writeString8(splitName);
202         dest.writeInt(descriptionRes);
203         dest.writeInt(enabled ? 1 : 0);
204         dest.writeInt(exported ? 1 : 0);
205         dest.writeInt(directBootAware ? 1 : 0);
206     }
207 
ComponentInfo(Parcel source)208     protected ComponentInfo(Parcel source) {
209         super(source);
210         applicationInfo = ApplicationInfo.CREATOR.createFromParcel(source);
211         processName = source.readString8();
212         splitName = source.readString8();
213         descriptionRes = source.readInt();
214         enabled = (source.readInt() != 0);
215         exported = (source.readInt() != 0);
216         directBootAware = (source.readInt() != 0);
217     }
218 
219     /**
220      * @hide
221      */
222     @Override
loadDefaultIcon(PackageManager pm)223     public Drawable loadDefaultIcon(PackageManager pm) {
224         return applicationInfo.loadIcon(pm);
225     }
226 
227     /**
228      * @hide
229      */
loadDefaultBanner(PackageManager pm)230     @Override protected Drawable loadDefaultBanner(PackageManager pm) {
231         return applicationInfo.loadBanner(pm);
232     }
233 
234     /**
235      * @hide
236      */
237     @Override
loadDefaultLogo(PackageManager pm)238     protected Drawable loadDefaultLogo(PackageManager pm) {
239         return applicationInfo.loadLogo(pm);
240     }
241 
242     /**
243      * @hide
244      */
getApplicationInfo()245     @Override protected ApplicationInfo getApplicationInfo() {
246         return applicationInfo;
247     }
248 }
249