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.content.ComponentName; 20 import android.graphics.drawable.Drawable; 21 import android.os.Parcel; 22 import android.os.Parcelable; 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 * <activity>, <receiver>, <service>, or 70 * <provider> 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 81 /** @removed */ 82 @Deprecated 83 public boolean encryptionAware = false; 84 ComponentInfo()85 public ComponentInfo() { 86 } 87 ComponentInfo(ComponentInfo orig)88 public ComponentInfo(ComponentInfo orig) { 89 super(orig); 90 applicationInfo = orig.applicationInfo; 91 processName = orig.processName; 92 splitName = orig.splitName; 93 descriptionRes = orig.descriptionRes; 94 enabled = orig.enabled; 95 exported = orig.exported; 96 encryptionAware = directBootAware = orig.directBootAware; 97 } 98 loadLabel(PackageManager pm)99 @Override public CharSequence loadLabel(PackageManager pm) { 100 if (nonLocalizedLabel != null) { 101 return nonLocalizedLabel; 102 } 103 ApplicationInfo ai = applicationInfo; 104 CharSequence label; 105 if (labelRes != 0) { 106 label = pm.getText(packageName, labelRes, ai); 107 if (label != null) { 108 return label; 109 } 110 } 111 if (ai.nonLocalizedLabel != null) { 112 return ai.nonLocalizedLabel; 113 } 114 if (ai.labelRes != 0) { 115 label = pm.getText(packageName, ai.labelRes, ai); 116 if (label != null) { 117 return label; 118 } 119 } 120 return name; 121 } 122 123 /** 124 * Return whether this component and its enclosing application are enabled. 125 */ isEnabled()126 public boolean isEnabled() { 127 return enabled && applicationInfo.enabled; 128 } 129 130 /** 131 * Return the icon resource identifier to use for this component. If 132 * the component defines an icon, that is used; else, the application 133 * icon is used. 134 * 135 * @return The icon associated with this component. 136 */ getIconResource()137 public final int getIconResource() { 138 return icon != 0 ? icon : applicationInfo.icon; 139 } 140 141 /** 142 * Return the logo resource identifier to use for this component. If 143 * the component defines a logo, that is used; else, the application 144 * logo is used. 145 * 146 * @return The logo associated with this component. 147 */ getLogoResource()148 public final int getLogoResource() { 149 return logo != 0 ? logo : applicationInfo.logo; 150 } 151 152 /** 153 * Return the banner resource identifier to use for this component. If the 154 * component defines a banner, that is used; else, the application banner is 155 * used. 156 * 157 * @return The banner associated with this component. 158 */ getBannerResource()159 public final int getBannerResource() { 160 return banner != 0 ? banner : applicationInfo.banner; 161 } 162 163 /** {@hide} */ getComponentName()164 public ComponentName getComponentName() { 165 return new ComponentName(packageName, name); 166 } 167 dumpFront(Printer pw, String prefix)168 protected void dumpFront(Printer pw, String prefix) { 169 super.dumpFront(pw, prefix); 170 if (processName != null && !packageName.equals(processName)) { 171 pw.println(prefix + "processName=" + processName); 172 } 173 if (splitName != null) { 174 pw.println(prefix + "splitName=" + splitName); 175 } 176 pw.println(prefix + "enabled=" + enabled + " exported=" + exported 177 + " directBootAware=" + directBootAware); 178 if (descriptionRes != 0) { 179 pw.println(prefix + "description=" + descriptionRes); 180 } 181 } 182 dumpBack(Printer pw, String prefix)183 protected void dumpBack(Printer pw, String prefix) { 184 dumpBack(pw, prefix, DUMP_FLAG_ALL); 185 } 186 dumpBack(Printer pw, String prefix, int flags)187 void dumpBack(Printer pw, String prefix, int flags) { 188 if ((flags&DUMP_FLAG_APPLICATION) != 0) { 189 if (applicationInfo != null) { 190 pw.println(prefix + "ApplicationInfo:"); 191 applicationInfo.dump(pw, prefix + " ", flags); 192 } else { 193 pw.println(prefix + "ApplicationInfo: null"); 194 } 195 } 196 super.dumpBack(pw, prefix); 197 } 198 writeToParcel(Parcel dest, int parcelableFlags)199 public void writeToParcel(Parcel dest, int parcelableFlags) { 200 super.writeToParcel(dest, parcelableFlags); 201 if ((parcelableFlags & Parcelable.PARCELABLE_ELIDE_DUPLICATES) != 0) { 202 dest.writeInt(0); 203 } else { 204 dest.writeInt(1); 205 applicationInfo.writeToParcel(dest, parcelableFlags); 206 } 207 dest.writeString(processName); 208 dest.writeString(splitName); 209 dest.writeInt(descriptionRes); 210 dest.writeInt(enabled ? 1 : 0); 211 dest.writeInt(exported ? 1 : 0); 212 dest.writeInt(directBootAware ? 1 : 0); 213 } 214 ComponentInfo(Parcel source)215 protected ComponentInfo(Parcel source) { 216 super(source); 217 final boolean hasApplicationInfo = (source.readInt() != 0); 218 if (hasApplicationInfo) { 219 applicationInfo = ApplicationInfo.CREATOR.createFromParcel(source); 220 } 221 processName = source.readString(); 222 splitName = source.readString(); 223 descriptionRes = source.readInt(); 224 enabled = (source.readInt() != 0); 225 exported = (source.readInt() != 0); 226 encryptionAware = directBootAware = (source.readInt() != 0); 227 } 228 229 /** 230 * @hide 231 */ 232 @Override loadDefaultIcon(PackageManager pm)233 public Drawable loadDefaultIcon(PackageManager pm) { 234 return applicationInfo.loadIcon(pm); 235 } 236 237 /** 238 * @hide 239 */ loadDefaultBanner(PackageManager pm)240 @Override protected Drawable loadDefaultBanner(PackageManager pm) { 241 return applicationInfo.loadBanner(pm); 242 } 243 244 /** 245 * @hide 246 */ 247 @Override loadDefaultLogo(PackageManager pm)248 protected Drawable loadDefaultLogo(PackageManager pm) { 249 return applicationInfo.loadLogo(pm); 250 } 251 252 /** 253 * @hide 254 */ getApplicationInfo()255 @Override protected ApplicationInfo getApplicationInfo() { 256 return applicationInfo; 257 } 258 } 259