LabeledIntent.java revision eb034652c2037a47ebfd99779e8383bb8bb528af
1package android.content.pm; 2 3import android.content.Intent; 4import android.graphics.drawable.Drawable; 5import android.os.Parcel; 6import android.text.TextUtils; 7 8/** 9 * A special subclass of Intent that can have a custom label/icon 10 * associated with it. Primarily for use with {@link Intent#ACTION_CHOOSER}. 11 */ 12public class LabeledIntent extends Intent { 13 private String mSourcePackage; 14 private int mLabelRes; 15 private CharSequence mNonLocalizedLabel; 16 private int mIcon; 17 18 /** 19 * Create a labeled intent from the given intent, supplying the label 20 * and icon resources for it. 21 * 22 * @param origIntent The original Intent to copy. 23 * @param sourcePackage The package in which the label and icon live. 24 * @param labelRes Resource containing the label, or 0 if none. 25 * @param icon Resource containing the icon, or 0 if none. 26 */ 27 public LabeledIntent(Intent origIntent, String sourcePackage, 28 int labelRes, int icon) { 29 super(origIntent); 30 mSourcePackage = sourcePackage; 31 mLabelRes = labelRes; 32 mNonLocalizedLabel = null; 33 mIcon = icon; 34 } 35 36 /** 37 * Create a labeled intent from the given intent, supplying a textual 38 * label and icon resource for it. 39 * 40 * @param origIntent The original Intent to copy. 41 * @param sourcePackage The package in which the label and icon live. 42 * @param nonLocalizedLabel Concrete text to use for the label. 43 * @param icon Resource containing the icon, or 0 if none. 44 */ 45 public LabeledIntent(Intent origIntent, String sourcePackage, 46 CharSequence nonLocalizedLabel, int icon) { 47 super(origIntent); 48 mSourcePackage = sourcePackage; 49 mLabelRes = 0; 50 mNonLocalizedLabel = nonLocalizedLabel; 51 mIcon = icon; 52 } 53 54 /** 55 * Create a labeled intent with no intent data but supplying the label 56 * and icon resources for it. 57 * 58 * @param sourcePackage The package in which the label and icon live. 59 * @param labelRes Resource containing the label, or 0 if none. 60 * @param icon Resource containing the icon, or 0 if none. 61 */ 62 public LabeledIntent(String sourcePackage, int labelRes, int icon) { 63 mSourcePackage = sourcePackage; 64 mLabelRes = labelRes; 65 mNonLocalizedLabel = null; 66 mIcon = icon; 67 } 68 69 /** 70 * Create a labeled intent with no intent data but supplying a textual 71 * label and icon resource for it. 72 * 73 * @param sourcePackage The package in which the label and icon live. 74 * @param nonLocalizedLabel Concrete text to use for the label. 75 * @param icon Resource containing the icon, or 0 if none. 76 */ 77 public LabeledIntent(String sourcePackage, 78 CharSequence nonLocalizedLabel, int icon) { 79 mSourcePackage = sourcePackage; 80 mLabelRes = 0; 81 mNonLocalizedLabel = nonLocalizedLabel; 82 mIcon = icon; 83 } 84 85 /** 86 * Return the name of the package holding label and icon resources. 87 */ 88 public String getSourcePackage() { 89 return mSourcePackage; 90 } 91 92 /** 93 * Return any resource identifier that has been given for the label text. 94 */ 95 public int getLabelResource() { 96 return mLabelRes; 97 } 98 99 /** 100 * Return any concrete text that has been given for the label text. 101 */ 102 public CharSequence getNonLocalizedLabel() { 103 return mNonLocalizedLabel; 104 } 105 106 /** 107 * Return any resource identifier that has been given for the label icon. 108 */ 109 public int getIconResource() { 110 return mIcon; 111 } 112 113 /** 114 * Retrieve the label associated with this object. If the object does 115 * not have a label, null will be returned, in which case you will probably 116 * want to load the label from the underlying resolved info for the Intent. 117 */ 118 public CharSequence loadLabel(PackageManager pm) { 119 if (mNonLocalizedLabel != null) { 120 return mNonLocalizedLabel; 121 } 122 if (mLabelRes != 0 && mSourcePackage != null) { 123 CharSequence label = pm.getText(mSourcePackage, mLabelRes, null); 124 if (label != null) { 125 return label; 126 } 127 } 128 return null; 129 } 130 131 /** 132 * Retrieve the icon associated with this object. If the object does 133 * not have a icon, null will be returned, in which case you will probably 134 * want to load the icon from the underlying resolved info for the Intent. 135 */ 136 public Drawable loadIcon(PackageManager pm) { 137 if (mIcon != 0 && mSourcePackage != null) { 138 Drawable icon = pm.getDrawable(mSourcePackage, mIcon, null); 139 if (icon != null) { 140 return icon; 141 } 142 } 143 return null; 144 } 145 146 public void writeToParcel(Parcel dest, int parcelableFlags) { 147 super.writeToParcel(dest, parcelableFlags); 148 dest.writeString(mSourcePackage); 149 dest.writeInt(mLabelRes); 150 TextUtils.writeToParcel(mNonLocalizedLabel, dest, parcelableFlags); 151 dest.writeInt(mIcon); 152 } 153 154 /** @hide */ 155 protected LabeledIntent(Parcel in) { 156 readFromParcel(in); 157 } 158 159 public void readFromParcel(Parcel in) { 160 super.readFromParcel(in); 161 mSourcePackage = in.readString(); 162 mLabelRes = in.readInt(); 163 mNonLocalizedLabel = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); 164 mIcon = in.readInt(); 165 } 166 167 public static final Creator<LabeledIntent> CREATOR 168 = new Creator<LabeledIntent>() { 169 public LabeledIntent createFromParcel(Parcel source) { 170 return new LabeledIntent(source); 171 } 172 public LabeledIntent[] newArray(int size) { 173 return new LabeledIntent[size]; 174 } 175 }; 176 177} 178