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