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