blob: 99a22fe79c4d0b8da92408bf6db83b7c70a6f0c0 [file] [log] [blame]
Romain Guy2f26cce2009-11-08 13:51:50 -08001/*
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 com.android.wallpaper.livepicker;
18
19import android.app.ListActivity;
20import android.app.WallpaperInfo;
21import android.os.Bundle;
22import android.content.pm.PackageManager;
23import android.content.pm.ResolveInfo;
24import android.content.pm.ComponentInfo;
25import android.content.Intent;
26import android.content.res.Resources;
27import android.graphics.drawable.Drawable;
28import android.graphics.drawable.BitmapDrawable;
29import android.graphics.Paint;
30import android.graphics.Canvas;
31import android.graphics.Bitmap;
32import android.util.Log;
33import android.view.Gravity;
34import android.view.View;
35import android.view.ViewGroup;
36import android.view.LayoutInflater;
37import android.service.wallpaper.WallpaperService;
38import android.widget.BaseAdapter;
39import android.widget.TextView;
40import android.widget.ImageView;
41import android.widget.AdapterView;
42import android.text.Html;
43
44import java.util.ArrayList;
45import java.util.List;
Romain Guy8cfd0d22009-11-08 17:21:15 -080046import java.util.Collections;
47import java.util.Comparator;
Romain Guy2f26cce2009-11-08 13:51:50 -080048import java.io.IOException;
Romain Guy8cfd0d22009-11-08 17:21:15 -080049import java.text.Collator;
Romain Guy2f26cce2009-11-08 13:51:50 -080050
51import org.xmlpull.v1.XmlPullParserException;
52
53public class LiveWallpaperListActivity extends ListActivity implements AdapterView.OnItemClickListener {
54 private static final String LOG_TAG = "LiveWallpapersPicker";
55
56 private static final int REQUEST_PREVIEW = 100;
57
58 private PackageManager mPackageManager;
59
60 private ArrayList<Drawable> mThumbnails;
61 private ArrayList<WallpaperInfo> mWallpaperInfos;
62 private ArrayList<Intent> mWallpaperIntents;
63
64 @Override
65 protected void onCreate(Bundle savedInstanceState) {
66 super.onCreate(savedInstanceState);
67 setContentView(R.layout.live_wallpaper_list);
68
69 mPackageManager = getPackageManager();
70
71 findLiveWallpapers();
72
73 setListAdapter(new LiveWallpapersAdapter());
74 getListView().setOnItemClickListener(this);
75 }
76
Romain Guy90e55692009-11-09 13:16:39 -080077 // TODO: THIS SHOULD HAPPEN IN AN ASYNCTASK
Romain Guy2f26cce2009-11-08 13:51:50 -080078 private void findLiveWallpapers() {
79 List<ResolveInfo> list = mPackageManager.queryIntentServices(
80 new Intent(WallpaperService.SERVICE_INTERFACE),
81 PackageManager.GET_META_DATA);
82
83 int listSize = list.size();
84
85 mThumbnails = new ArrayList<Drawable>(listSize);
86 mWallpaperIntents = new ArrayList<Intent>(listSize);
87 mWallpaperInfos = new ArrayList<WallpaperInfo>(listSize);
88
89 Resources res = getResources();
90 Drawable galleryIcon = res.getDrawable(R.drawable.livewallpaper_placeholder);
91
92 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
93 paint.setTextAlign(Paint.Align.CENTER);
94
95 Canvas canvas = new Canvas();
96
Romain Guy8cfd0d22009-11-08 17:21:15 -080097 Collections.sort(list, new Comparator<ResolveInfo>() {
98 final Collator mCollator;
99
100 {
101 mCollator = Collator.getInstance();
102 }
103
104 public int compare(ResolveInfo info1, ResolveInfo info2) {
105 return mCollator.compare(info1.loadLabel(mPackageManager),
106 info2.loadLabel(mPackageManager));
107 }
108 });
109
Romain Guy2f26cce2009-11-08 13:51:50 -0800110 for (int i = 0; i < listSize; i++) {
111 ResolveInfo resolveInfo = list.get(i);
112 ComponentInfo ci = resolveInfo.serviceInfo;
113 WallpaperInfo info;
114 try {
115 info = new WallpaperInfo(this, resolveInfo);
116 } catch (XmlPullParserException e) {
117 Log.w(LOG_TAG, "Skipping wallpaper " + ci, e);
118 continue;
119 } catch (IOException e) {
120 Log.w(LOG_TAG, "Skipping wallpaper " + ci, e);
121 continue;
122 }
123
124 String packageName = info.getPackageName();
125 String className = info.getServiceName();
126
127 Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE);
128 intent.setClassName(packageName, className);
129
130 mWallpaperIntents.add(intent);
131 mWallpaperInfos.add(info);
132
133 Drawable thumb = info.loadThumbnail(mPackageManager);
134 if (thumb == null) {
135 int thumbWidth = res.getDimensionPixelSize(R.dimen.live_wallpaper_thumbnail_width);
136 int thumbHeight = res.getDimensionPixelSize(R.dimen.live_wallpaper_thumbnail_height);
137
138 Bitmap thumbnail = Bitmap.createBitmap(thumbWidth, thumbHeight,
139 Bitmap.Config.ARGB_8888);
140
141 paint.setColor(res.getColor(R.color.live_wallpaper_thumbnail_background));
142 canvas.setBitmap(thumbnail);
143 canvas.drawPaint(paint);
144
145 galleryIcon.setBounds(0, 0, thumbWidth, thumbHeight);
146 ((BitmapDrawable) galleryIcon).setGravity(Gravity.CENTER);
147 galleryIcon.draw(canvas);
148
149 String title = info.loadLabel(mPackageManager).toString();
150
151 paint.setColor(res.getColor(R.color.live_wallpaper_thumbnail_text_color));
152 paint.setTextSize(
153 res.getDimensionPixelSize(R.dimen.live_wallpaper_thumbnail_text_size));
154
155 canvas.drawText(title, (int) (thumbWidth * 0.5),
156 thumbHeight - res.getDimensionPixelSize(
157 R.dimen.live_wallpaper_thumbnail_text_offset), paint);
158
159 thumb = new BitmapDrawable(res, thumbnail);
160 }
161
162 thumb.setDither(true);
163 mThumbnails.add(thumb);
164 }
165 }
166
167 @Override
168 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
169 super.onActivityResult(requestCode, resultCode, data);
170
171 if (requestCode == REQUEST_PREVIEW) {
172 if (resultCode == RESULT_OK) finish();
173 }
174 }
175
176 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
177 final Intent intent = mWallpaperIntents.get(position);
178 final WallpaperInfo info = mWallpaperInfos.get(position);
179 LiveWallpaperPreview.showPreview(this, REQUEST_PREVIEW, intent, info);
180 }
181
182 static class ViewHolder {
183 TextView titleAuthor;
184 TextView description;
185 ImageView thumbnail;
186 }
187
188 private class LiveWallpapersAdapter extends BaseAdapter {
189 private final LayoutInflater mInflater;
190
191 LiveWallpapersAdapter() {
192 mInflater = LayoutInflater.from(LiveWallpaperListActivity.this);
193 }
194
195 public int getCount() {
196 return mWallpaperInfos.size();
197 }
198
199 public Object getItem(int position) {
200 return mWallpaperInfos.get(position);
201 }
202
203 public long getItemId(int position) {
204 return position;
205 }
206
207 public View getView(int position, View convertView, ViewGroup parent) {
208 ViewHolder holder;
209 if (convertView == null) {
210 convertView = mInflater.inflate(R.layout.live_wallpaper_entry, parent, false);
211
212 holder = new ViewHolder();
213 holder.titleAuthor = (TextView) convertView.findViewById(R.id.title_author);
214 holder.description = (TextView) convertView.findViewById(R.id.description);
215 holder.thumbnail = (ImageView) convertView.findViewById(R.id.thumbnail);
216 convertView.setTag(holder);
217 } else {
218 holder = (ViewHolder) convertView.getTag();
219 }
220
221 WallpaperInfo info = mWallpaperInfos.get(position);
222 holder.thumbnail.setImageDrawable(mThumbnails.get(position));
223 holder.titleAuthor.setText(getString(R.string.wallpaper_title_and_author,
224 info.loadLabel(mPackageManager), info.loadAuthor(mPackageManager)));
225 holder.description.setText(Html.fromHtml(
226 info.loadDescription(mPackageManager).toString()));
227
228 return convertView;
229 }
230 }
231}