Http://www.oschina.net/code/explore/android-2.2-froyo/com/android/wallpaper/livepicker/LiveWallpaperListActivity.java
/**
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License ");
* You may not use this file before t in compliance with the License.
* You may obtain a copy of the License
*
* Http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* Distributed under the License is distributed on an "as is" BASIS,
* Without warranties or conditions of any kind, either express or implied.
* See the License for the specific language governing permissions and
* Limitations under the License.
*/
Package com. android. wallpaper. livepicker;
Import android. app. ListActivity;
Import android. app. WallpaperInfo;
Import android. OS. Bundle;
Import android. content. pm. PackageManager;
Import android. content. pm. ResolveInfo;
Import android. content. pm. ComponentInfo;
Import android. content. Intent;
Import android. content. res. Resources;
Import android. graphics. drawable. Drawable;
Import android. graphics. drawable. BitmapDrawable;
Import android. graphics. Paint;
Import android. graphics. Canvas;
Import android. graphics. Bitmap;
Import android. util. Log;
Import android. view. Gravity;
Import android. view. View;
Import android. view. ViewGroup;
Import android. view. LayoutInflater;
Import android. service. wallpaper. WallpaperService;
Import android. widget. BaseAdapter;
Import android. widget. TextView;
Import android. widget. ImageView;
Import android. widget. AdapterView;
Import android. text. Html;
Import java. util. ArrayList;
Import java. util. List;
Import java. util. Collections;
Import java. util. Comparator;
Import java. io. IOException;
Import java. text. Collator;
Import org. xmlpull. v1.XmlPullParserException;
Public class LiveWallpaperListActivity extends ListActivity implements AdapterView. OnItemClickListener {
Private static final String LOG_TAG = "LiveWallpapersPicker ";
Private static final int REQUEST_PREVIEW = 100;
Private PackageManager mPackageManager;
Private ArrayList <Drawable> mThumbnails;
Private ArrayList <WallpaperInfo> mWallpaperInfos;
Private ArrayList <Intent> mWallpaperIntents;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. live_wallpaper_list );
MPackageManager = getPackageManager ();
FindLiveWallpapers ();
SetListAdapter (new LiveWallpapersAdapter ());
GetListView (). setOnItemClickListener (this );
}
// TODO: THIS shocould HAPPEN IN AN ASYNCTASK
Private void findLiveWallpapers (){
List <ResolveInfo> list = mPackageManager. queryIntentServices (
New Intent (WallpaperService. SERVICE_INTERFACE ),
PackageManager. GET_META_DATA );
Int listSize = list. size ();
MThumbnails = new ArrayList <Drawable> (listSize );
MWallpaperIntents = new ArrayList <Intent> (listSize );
MWallpaperInfos = new ArrayList <WallpaperInfo> (listSize );
Resources res = getResources ();
Drawable galleryIcon = res. getDrawable (R. drawable. livewallpaper_placeholder );
Paint paint = new Paint (Paint. ANTI_ALIAS_FLAG | Paint. DITHER_FLAG );
Paint. setTextAlign (Paint. Align. CENTER );
Canvas canvas = new Canvas ();
Collections. sort (list, new Comparator <ResolveInfo> (){
Final Collator mCollator;
{
MCollator = Collator. getInstance ();
}
Public int compare (ResolveInfo info1, ResolveInfo info2 ){
Return mCollator. compare (info1.loadLabel (mPackageManager ),
Info2.loadLabel (mPackageManager ));
}
});
For (int I = 0; I <listSize; I ++ ){
ResolveInfo resolveInfo = list. get (I );
ComponentInfo ci = resolveInfo. serviceInfo;
WallpaperInfo info;
Try {
Info = new WallpaperInfo (this, resolveInfo );
} Catch (XmlPullParserException e ){
Log. w (LOG_TAG, "Skipping wallpaper" + ci, e );
Continue;
} Catch (IOException e ){
Log. w (LOG_TAG, "Skipping wallpaper" + ci, e );
Continue;
}
String packageName = info. getPackageName ();
String className = info. getServiceName ();
Intent intent = new Intent (WallpaperService. SERVICE_INTERFACE );
Intent. setClassName (packageName, className );
MWallpaperIntents. add (intent );
MWallpaperInfos. add (info );
Drawable thumb = info. loadThumbnail (mPackageManager );
If (thumb = null ){
Int thumbWidth = res. getDimensionPixelSize (R. dimen. live_wallpaper_thumbnail_width );
Int thumbHeight = res. getDimensionPixelSize (R. dimen. live_wallpaper_thumbnail_height );
Bitmap thumbnail = Bitmap. createBitmap (thumbWidth, thumbHeight,
Bitmap. Config. ARGB_8888 );
Paint. setColor (res. getColor (R. color. live_wallpaper_thumbnail_background ));
Canvas. setBitmap (thumbnail );
Canvas. drawPaint (paint );
GalleryIcon. setBounds (0, 0, thumbWidth, thumbHeight );
(BitmapDrawable) galleryIcon). setGravity (Gravity. CENTER );
GalleryIcon. draw (canvas );
String title = info. loadLabel (mPackageManager). toString ();
Paint. setColor (res. getColor (R. color. live_wallpaper_thumbnail_text_color ));
Paint. setTextSize (
Res. getDimensionPixelSize (R. dimen. live_wallpaper_thumbnail_text_size ));
Canvas. drawText (title, (int) (thumbWidth * 0.5 ),
ThumbHeight-res. getDimensionPixelSize (
R. dimen. live_wallpaper_thumbnail_text_offset), paint );
Thumb = new BitmapDrawable (res, thumbnail );
}
Thumb. setDither (true );
MThumbnails. add (thumb );
}
}
@ Override
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
Super. onActivityResult (requestCode, resultCode, data );
If (requestCode = REQUEST_PREVIEW ){
If (resultCode = RESULT_ OK) finish ();
}
}
Public void onItemClick (AdapterView <?> Parent, View view, int position, long id ){
Final Intent intent = mWallpaperIntents. get (position );
Final WallpaperInfo info = mWallpaperInfos. get (position );
LiveWallpaperPreview. showPreview (this, REQUEST_PREVIEW, intent, info );
}
Static class ViewHolder {
TextView titleAuthor;
TextView description;
ImageView thumbnail;
}
Private class LiveWallpapersAdapter extends BaseAdapter {
Private final LayoutInflater mInflater;
LiveWallpapersAdapter (){
MInflater = LayoutInflater. from (LiveWallpaperListActivity. this );
}
Public int getCount (){
Return mWallpaperInfos. size ();
}
Public Object getItem (int position ){
Return mWallpaperInfos. get (position );
}
Public long getItemId (int position ){
Return position;
}
Public View getView (int position, View convertView, ViewGroup parent ){
ViewHolder holder;
If (convertView = null ){
ConvertView = mInflater. inflate (R. layout. live_wallpaper_entry, parent, false );
Holder = new ViewHolder ();
Holder. titleAuthor = (TextView) convertView. findViewById (R. id. title_author );
Holder. description = (TextView) convertView. findViewById (R. id. description );
Holder. thumbnail = (ImageView) convertView. findViewById (R. id. thumbnail );
ConvertView. setTag (holder );
} Else {
Holder = (ViewHolder) convertView. getTag ();
}
WallpaperInfo info = mWallpaperInfos. get (position );
Holder. thumbnail. setImageDrawable (mThumbnails. get (position ));
// Author not currently used
Holder. titleAuthor. setText (info. loadLabel (mPackageManager ));
Try {
Holder. description. setVisibility (View. VISIBLE );
Holder. description. setText (Html. fromHtml (
Info. loadDescription (mPackageManager). toString ()));
} Catch (Resources. NotFoundException e ){
Holder. description. setVisibility (View. GONE );
}
Return convertView;
}
}
}