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 * A string resource identifier (in the package's resources) containing 49 * a user-readable description of the component. From the "description" 50 * attribute or, if not set, 0. 51 */ 52 public int descriptionRes; 53 54 /** 55 * Indicates whether or not this component may be instantiated. Note that this value can be 56 * overriden by the one in its parent {@link ApplicationInfo}. 57 */ 58 public boolean enabled = true; 59 60 /** 61 * Set to true if this component is available for use by other applications. 62 * Comes from {@link android.R.attr#exported android:exported} of the 63 * <activity>, <receiver>, <service>, or 64 * <provider> tag. 65 */ 66 public boolean exported = false; 67 68 /** 69 * Indicates if this component is aware of direct boot lifecycle, and can be 70 * safely run before the user has entered their credentials (such as a lock 71 * pattern or PIN). 72 */ 73 public boolean directBootAware = false; 74 75 /** @removed */ 76 @Deprecated 77 public boolean encryptionAware = false; 78 ComponentInfo()79 public ComponentInfo() { 80 } 81 ComponentInfo(ComponentInfo orig)82 public ComponentInfo(ComponentInfo orig) { 83 super(orig); 84 applicationInfo = orig.applicationInfo; 85 processName = orig.processName; 86 descriptionRes = orig.descriptionRes; 87 enabled = orig.enabled; 88 exported = orig.exported; 89 encryptionAware = directBootAware = orig.directBootAware; 90 } 91 loadLabel(PackageManager pm)92 @Override public CharSequence loadLabel(PackageManager pm) { 93 if (nonLocalizedLabel != null) { 94 return nonLocalizedLabel; 95 } 96 ApplicationInfo ai = applicationInfo; 97 CharSequence label; 98 if (labelRes != 0) { 99 label = pm.getText(packageName, labelRes, ai); 100 if (label != null) { 101 return label; 102 } 103 } 104 if (ai.nonLocalizedLabel != null) { 105 return ai.nonLocalizedLabel; 106 } 107 if (ai.labelRes != 0) { 108 label = pm.getText(packageName, ai.labelRes, ai); 109 if (label != null) { 110 return label; 111 } 112 } 113 return name; 114 } 115 116 /** 117 * Return whether this component and its enclosing application are enabled. 118 */ isEnabled()119 public boolean isEnabled() { 120 return enabled && applicationInfo.enabled; 121 } 122 123 /** 124 * Return the icon resource identifier to use for this component. If 125 * the component defines an icon, that is used; else, the application 126 * icon is used. 127 * 128 * @return The icon associated with this component. 129 */ getIconResource()130 public final int getIconResource() { 131 return icon != 0 ? icon : applicationInfo.icon; 132 } 133 134 /** 135 * Return the logo resource identifier to use for this component. If 136 * the component defines a logo, that is used; else, the application 137 * logo is used. 138 * 139 * @return The logo associated with this component. 140 */ getLogoResource()141 public final int getLogoResource() { 142 return logo != 0 ? logo : applicationInfo.logo; 143 } 144 145 /** 146 * Return the banner resource identifier to use for this component. If the 147 * component defines a banner, that is used; else, the application banner is 148 * used. 149 * 150 * @return The banner associated with this component. 151 */ getBannerResource()152 public final int getBannerResource() { 153 return banner != 0 ? banner : applicationInfo.banner; 154 } 155 156 /** {@hide} */ getComponentName()157 public ComponentName getComponentName() { 158 return new ComponentName(packageName, name); 159 } 160 dumpFront(Printer pw, String prefix)161 protected void dumpFront(Printer pw, String prefix) { 162 super.dumpFront(pw, prefix); 163 if (processName != null && !packageName.equals(processName)) { 164 pw.println(prefix + "processName=" + processName); 165 } 166 pw.println(prefix + "enabled=" + enabled + " exported=" + exported 167 + " directBootAware=" + directBootAware); 168 if (descriptionRes != 0) { 169 pw.println(prefix + "description=" + descriptionRes); 170 } 171 } 172 dumpBack(Printer pw, String prefix)173 protected void dumpBack(Printer pw, String prefix) { 174 dumpBack(pw, prefix, DUMP_FLAG_ALL); 175 } 176 dumpBack(Printer pw, String prefix, int flags)177 void dumpBack(Printer pw, String prefix, int flags) { 178 if ((flags&DUMP_FLAG_APPLICATION) != 0) { 179 if (applicationInfo != null) { 180 pw.println(prefix + "ApplicationInfo:"); 181 applicationInfo.dump(pw, prefix + " ", flags); 182 } else { 183 pw.println(prefix + "ApplicationInfo: null"); 184 } 185 } 186 super.dumpBack(pw, prefix); 187 } 188 writeToParcel(Parcel dest, int parcelableFlags)189 public void writeToParcel(Parcel dest, int parcelableFlags) { 190 super.writeToParcel(dest, parcelableFlags); 191 if ((parcelableFlags & Parcelable.PARCELABLE_ELIDE_DUPLICATES) != 0) { 192 dest.writeInt(0); 193 } else { 194 dest.writeInt(1); 195 applicationInfo.writeToParcel(dest, parcelableFlags); 196 } 197 dest.writeString(processName); 198 dest.writeInt(descriptionRes); 199 dest.writeInt(enabled ? 1 : 0); 200 dest.writeInt(exported ? 1 : 0); 201 dest.writeInt(directBootAware ? 1 : 0); 202 } 203 ComponentInfo(Parcel source)204 protected ComponentInfo(Parcel source) { 205 super(source); 206 final boolean hasApplicationInfo = (source.readInt() != 0); 207 if (hasApplicationInfo) { 208 applicationInfo = ApplicationInfo.CREATOR.createFromParcel(source); 209 } 210 processName = source.readString(); 211 descriptionRes = source.readInt(); 212 enabled = (source.readInt() != 0); 213 exported = (source.readInt() != 0); 214 encryptionAware = directBootAware = (source.readInt() != 0); 215 } 216 217 /** 218 * @hide 219 */ 220 @Override loadDefaultIcon(PackageManager pm)221 public Drawable loadDefaultIcon(PackageManager pm) { 222 return applicationInfo.loadIcon(pm); 223 } 224 225 /** 226 * @hide 227 */ loadDefaultBanner(PackageManager pm)228 @Override protected Drawable loadDefaultBanner(PackageManager pm) { 229 return applicationInfo.loadBanner(pm); 230 } 231 232 /** 233 * @hide 234 */ 235 @Override loadDefaultLogo(PackageManager pm)236 protected Drawable loadDefaultLogo(PackageManager pm) { 237 return applicationInfo.loadLogo(pm); 238 } 239 240 /** 241 * @hide 242 */ getApplicationInfo()243 @Override protected ApplicationInfo getApplicationInfo() { 244 return applicationInfo; 245 } 246 } 247