WallpaperInfo.java revision 274b120f3975c8db6daeeab51a5fe08d6dede479
1package android.app;
2
3import org.xmlpull.v1.XmlPullParser;
4import org.xmlpull.v1.XmlPullParserException;
5
6import android.content.ComponentName;
7import android.content.Context;
8import android.content.pm.PackageManager;
9import android.content.pm.ResolveInfo;
10import android.content.pm.ServiceInfo;
11import android.content.res.Resources.NotFoundException;
12import android.content.res.TypedArray;
13import android.content.res.XmlResourceParser;
14import android.graphics.drawable.Drawable;
15import android.os.Parcel;
16import android.os.Parcelable;
17import android.service.wallpaper.WallpaperService;
18import android.util.AttributeSet;
19import android.util.Printer;
20import android.util.Xml;
21
22import java.io.IOException;
23
24/**
25 * This class is used to specify meta information of a wallpaper service.
26 */
27public final class WallpaperInfo implements Parcelable {
28    static final String TAG = "WallpaperInfo";
29
30    /**
31     * The Service that implements this wallpaper component.
32     */
33    final ResolveInfo mService;
34
35    /**
36     * The wallpaper setting activity's name, to
37     * launch the setting activity of this wallpaper.
38     */
39    final String mSettingsActivityName;
40
41    /**
42     * Resource identifier for this wallpaper's thumbnail image.
43     */
44    final int mThumbnailResource;
45
46    /**
47     * Resource identifier for a string indicating the author of the wallpaper.
48     */
49    final int mAuthorResource;
50
51    /**
52     * Resource identifier for a string containing a short description of the wallpaper.
53     */
54    final int mDescriptionResource;
55
56    /**
57     * Constructor.
58     *
59     * @param context The Context in which we are parsing the wallpaper.
60     * @param service The ResolveInfo returned from the package manager about
61     * this wallpaper's component.
62     */
63    public WallpaperInfo(Context context, ResolveInfo service)
64            throws XmlPullParserException, IOException {
65        mService = service;
66        ServiceInfo si = service.serviceInfo;
67
68        PackageManager pm = context.getPackageManager();
69        String settingsActivityComponent = null;
70        int thumbnailRes = -1;
71        int authorRes = -1;
72        int descriptionRes = -1;
73
74        XmlResourceParser parser = null;
75        try {
76            parser = si.loadXmlMetaData(pm, WallpaperService.SERVICE_META_DATA);
77            if (parser == null) {
78                throw new XmlPullParserException("No "
79                        + WallpaperService.SERVICE_META_DATA + " meta-data");
80            }
81
82            AttributeSet attrs = Xml.asAttributeSet(parser);
83
84            int type;
85            while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
86                    && type != XmlPullParser.START_TAG) {
87            }
88
89            String nodeName = parser.getName();
90            if (!"wallpaper".equals(nodeName)) {
91                throw new XmlPullParserException(
92                        "Meta-data does not start with wallpaper tag");
93            }
94
95            TypedArray sa = context.getResources().obtainAttributes(attrs,
96                    com.android.internal.R.styleable.Wallpaper);
97            settingsActivityComponent = sa.getString(
98                    com.android.internal.R.styleable.Wallpaper_settingsActivity);
99
100            thumbnailRes = sa.getResourceId(
101                    com.android.internal.R.styleable.Wallpaper_thumbnail,
102                    -1);
103            authorRes = sa.getResourceId(
104                    com.android.internal.R.styleable.Wallpaper_wallpaperAuthor,
105                    -1);
106            descriptionRes = sa.getResourceId(
107                    com.android.internal.R.styleable.Wallpaper_wallpaperDescription,
108                    -1);
109
110            sa.recycle();
111        } finally {
112            if (parser != null) parser.close();
113        }
114
115        mSettingsActivityName = settingsActivityComponent;
116        mThumbnailResource = thumbnailRes;
117        mAuthorResource = authorRes;
118        mDescriptionResource = descriptionRes;
119    }
120
121    WallpaperInfo(Parcel source) {
122        mSettingsActivityName = source.readString();
123        mThumbnailResource = source.readInt();
124        mAuthorResource = source.readInt();
125        mDescriptionResource = source.readInt();
126        mService = ResolveInfo.CREATOR.createFromParcel(source);
127    }
128
129    /**
130     * Return the .apk package that implements this wallpaper.
131     */
132    public String getPackageName() {
133        return mService.serviceInfo.packageName;
134    }
135
136    /**
137     * Return the class name of the service component that implements
138     * this wallpaper.
139     */
140    public String getServiceName() {
141        return mService.serviceInfo.name;
142    }
143
144    /**
145     * Return the raw information about the Service implementing this
146     * wallpaper.  Do not modify the returned object.
147     */
148    public ServiceInfo getServiceInfo() {
149        return mService.serviceInfo;
150    }
151
152    /**
153     * Return the component of the service that implements this wallpaper.
154     */
155    public ComponentName getComponent() {
156        return new ComponentName(mService.serviceInfo.packageName,
157                mService.serviceInfo.name);
158    }
159
160    /**
161     * Load the user-displayed label for this wallpaper.
162     *
163     * @param pm Supply a PackageManager used to load the wallpaper's
164     * resources.
165     */
166    public CharSequence loadLabel(PackageManager pm) {
167        return mService.loadLabel(pm);
168    }
169
170    /**
171     * Load the user-displayed icon for this wallpaper.
172     *
173     * @param pm Supply a PackageManager used to load the wallpaper's
174     * resources.
175     */
176    public Drawable loadIcon(PackageManager pm) {
177        return mService.loadIcon(pm);
178    }
179
180    /**
181     * Load the thumbnail image for this wallpaper.
182     *
183     * @param pm Supply a PackageManager used to load the wallpaper's
184     * resources.
185     */
186    public Drawable loadThumbnail(PackageManager pm) {
187        if (mThumbnailResource < 0) return null;
188
189        return pm.getDrawable(mService.serviceInfo.packageName,
190                              mThumbnailResource,
191                              null);
192    }
193
194    /**
195     * Return a string indicating the author(s) of this wallpaper.
196     */
197    public CharSequence loadAuthor(PackageManager pm) throws NotFoundException {
198        if (mAuthorResource <= 0) throw new NotFoundException();
199        return pm.getText(
200            (mService.resolvePackageName != null)
201                ? mService.resolvePackageName
202                : getPackageName(),
203            mAuthorResource,
204            null);
205    }
206
207    /**
208     * Return a brief summary of this wallpaper's behavior.
209     */
210    public CharSequence loadDescription(PackageManager pm) throws NotFoundException {
211        if (mDescriptionResource <= 0) throw new NotFoundException();
212        return pm.getText(
213            (mService.resolvePackageName != null)
214                ? mService.resolvePackageName
215                : getPackageName(),
216            mDescriptionResource,
217            null);
218    }
219
220    /**
221     * Return the class name of an activity that provides a settings UI for
222     * the wallpaper.  You can launch this activity be starting it with
223     * an {@link android.content.Intent} whose action is MAIN and with an
224     * explicit {@link android.content.ComponentName}
225     * composed of {@link #getPackageName} and the class name returned here.
226     *
227     * <p>A null will be returned if there is no settings activity associated
228     * with the wallpaper.
229     */
230    public String getSettingsActivity() {
231        return mSettingsActivityName;
232    }
233
234    public void dump(Printer pw, String prefix) {
235        pw.println(prefix + "Service:");
236        mService.dump(pw, prefix + "  ");
237        pw.println(prefix + "mSettingsActivityName=" + mSettingsActivityName);
238    }
239
240    @Override
241    public String toString() {
242        return "WallpaperInfo{" + mService.serviceInfo.name
243                + ", settings: "
244                + mSettingsActivityName + "}";
245    }
246
247    /**
248     * Used to package this object into a {@link Parcel}.
249     *
250     * @param dest The {@link Parcel} to be written.
251     * @param flags The flags used for parceling.
252     */
253    public void writeToParcel(Parcel dest, int flags) {
254        dest.writeString(mSettingsActivityName);
255        dest.writeInt(mThumbnailResource);
256        dest.writeInt(mAuthorResource);
257        dest.writeInt(mDescriptionResource);
258        mService.writeToParcel(dest, flags);
259    }
260
261    /**
262     * Used to make this class parcelable.
263     */
264    public static final Parcelable.Creator<WallpaperInfo> CREATOR = new Parcelable.Creator<WallpaperInfo>() {
265        public WallpaperInfo createFromParcel(Parcel source) {
266            return new WallpaperInfo(source);
267        }
268
269        public WallpaperInfo[] newArray(int size) {
270            return new WallpaperInfo[size];
271        }
272    };
273
274    public int describeContents() {
275        return 0;
276    }
277}
278