blob: 70396d0406e8a349d565c01ecde401480dc78198 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 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.launcher;
18
19import android.app.Activity;
Joe Onorato1b126452009-07-28 18:26:47 -070020import android.app.IWallpaperService;
21import android.content.res.Resources;
22import android.graphics.BitmapFactory;
23import android.graphics.Bitmap;
24import android.graphics.drawable.Drawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.os.Bundle;
Joe Onorato1b126452009-07-28 18:26:47 -070026import android.os.ParcelFileDescriptor;
27import android.os.RemoteException;
28import android.os.ServiceManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.util.Log;
30import android.view.LayoutInflater;
31import android.view.View;
32import android.view.ViewGroup;
33import android.view.Window;
34import android.view.View.OnClickListener;
35import android.widget.AdapterView;
36import android.widget.BaseAdapter;
37import android.widget.Button;
38import android.widget.Gallery;
39import android.widget.ImageView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040
41import java.io.IOException;
42import java.io.InputStream;
Joe Onorato1b126452009-07-28 18:26:47 -070043import java.io.FileOutputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import java.util.ArrayList;
45import java.util.Collections;
46
47public class WallpaperChooser extends Activity implements AdapterView.OnItemSelectedListener,
48 OnClickListener {
49
50 private static final Integer[] THUMB_IDS = {
51 R.drawable.wallpaper_lake_small,
52 R.drawable.wallpaper_sunset_small,
53 R.drawable.wallpaper_beach_small,
54 R.drawable.wallpaper_snow_leopard_small,
55 R.drawable.wallpaper_path_small,
56 R.drawable.wallpaper_sunrise_small,
57 R.drawable.wallpaper_mountain_small,
58 R.drawable.wallpaper_road_small,
59 R.drawable.wallpaper_jellyfish_small,
60 R.drawable.wallpaper_zanzibar_small,
61 R.drawable.wallpaper_blue_small,
62 R.drawable.wallpaper_grey_small,
63 R.drawable.wallpaper_green_small,
64 R.drawable.wallpaper_pink_small,
65 };
66
67 private static final Integer[] IMAGE_IDS = {
Ramanan Rajeswaranc4c1f322009-05-08 13:15:08 -070068 R.drawable.wallpaper_lake,
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069 R.drawable.wallpaper_sunset,
70 R.drawable.wallpaper_beach,
71 R.drawable.wallpaper_snow_leopard,
72 R.drawable.wallpaper_path,
73 R.drawable.wallpaper_sunrise,
74 R.drawable.wallpaper_mountain,
75 R.drawable.wallpaper_road,
76 R.drawable.wallpaper_jellyfish,
77 R.drawable.wallpaper_zanzibar,
78 R.drawable.wallpaper_blue,
79 R.drawable.wallpaper_grey,
80 R.drawable.wallpaper_green,
81 R.drawable.wallpaper_pink,
82 };
83
84 private Gallery mGallery;
85 private ImageView mImageView;
86 private boolean mIsWallpaperSet;
87
88 private BitmapFactory.Options mOptions;
89 private Bitmap mBitmap;
90
91 private ArrayList<Integer> mThumbs;
92 private ArrayList<Integer> mImages;
Joe Onorato1b126452009-07-28 18:26:47 -070093 private ArrayList<String> mNames;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094
95 @Override
96 public void onCreate(Bundle icicle) {
97 super.onCreate(icicle);
98 requestWindowFeature(Window.FEATURE_NO_TITLE);
99
100 findWallpapers();
101
102 setContentView(R.layout.wallpaper_chooser);
103
104 mOptions = new BitmapFactory.Options();
105 mOptions.inDither = false;
106 mOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
107
108 mGallery = (Gallery) findViewById(R.id.gallery);
109 mGallery.setAdapter(new ImageAdapter(this));
110 mGallery.setOnItemSelectedListener(this);
111 mGallery.setCallbackDuringFling(false);
112
113 Button b = (Button) findViewById(R.id.set);
114 b.setOnClickListener(this);
115
116 mImageView = (ImageView) findViewById(R.id.wallpaper);
117 }
118
119 private void findWallpapers() {
120 mThumbs = new ArrayList<Integer>(THUMB_IDS.length + 4);
121 Collections.addAll(mThumbs, THUMB_IDS);
122
123 mImages = new ArrayList<Integer>(IMAGE_IDS.length + 4);
124 Collections.addAll(mImages, IMAGE_IDS);
125
126 final Resources resources = getResources();
Joe Onorato1b126452009-07-28 18:26:47 -0700127
128 mNames = new ArrayList<String>(IMAGE_IDS.length + 4);
129 for (int res: mImages) {
130 mNames.add("res:" + resources.getResourceName(res));
131 }
132
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133 final String[] extras = resources.getStringArray(R.array.extra_wallpapers);
134 final String packageName = getApplication().getPackageName();
135
136 for (String extra : extras) {
137 int res = resources.getIdentifier(extra, "drawable", packageName);
138 if (res != 0) {
139 final int thumbRes = resources.getIdentifier(extra + "_small",
140 "drawable", packageName);
141
142 if (thumbRes != 0) {
143 mThumbs.add(thumbRes);
144 mImages.add(res);
Joe Onorato1b126452009-07-28 18:26:47 -0700145 mNames.add("res:" + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 }
147 }
148 }
149 }
150
151 @Override
152 protected void onResume() {
153 super.onResume();
154 mIsWallpaperSet = false;
155 }
156
157 public void onItemSelected(AdapterView parent, View v, int position, long id) {
158 final ImageView view = mImageView;
159 Bitmap b = BitmapFactory.decodeResource(getResources(), mImages.get(position), mOptions);
160 view.setImageBitmap(b);
161
162 // Help the GC
163 if (mBitmap != null) {
164 mBitmap.recycle();
165 }
166 mBitmap = b;
167
168 final Drawable drawable = view.getDrawable();
169 drawable.setFilterBitmap(true);
170 drawable.setDither(true);
171 }
172
173 /*
174 * When using touch if you tap an image it triggers both the onItemClick and
175 * the onTouchEvent causing the wallpaper to be set twice. Ensure we only
176 * set the wallpaper once.
177 */
178 private void selectWallpaper(int position) {
179 if (mIsWallpaperSet) {
180 return;
181 }
182
183 mIsWallpaperSet = true;
184 try {
185 InputStream stream = getResources().openRawResource(mImages.get(position));
Joe Onorato1b126452009-07-28 18:26:47 -0700186 setWallpaper(stream, mNames.get(position));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 setResult(RESULT_OK);
188 finish();
189 } catch (IOException e) {
190 Log.e(Launcher.LOG_TAG, "Failed to set wallpaper: " + e);
191 }
192 }
193
194 public void onNothingSelected(AdapterView parent) {
195 }
196
197 private class ImageAdapter extends BaseAdapter {
198 private LayoutInflater mLayoutInflater;
199
200 ImageAdapter(WallpaperChooser context) {
201 mLayoutInflater = context.getLayoutInflater();
202 }
203
204 public int getCount() {
205 return mThumbs.size();
206 }
207
208 public Object getItem(int position) {
209 return position;
210 }
211
212 public long getItemId(int position) {
213 return position;
214 }
215
216 public View getView(int position, View convertView, ViewGroup parent) {
217 ImageView image;
218
219 if (convertView == null) {
220 image = (ImageView) mLayoutInflater.inflate(R.layout.wallpaper_item, parent, false);
221 } else {
222 image = (ImageView) convertView;
223 }
224
225 image.setImageResource(mThumbs.get(position));
226 image.getDrawable().setDither(true);
227 return image;
228 }
229 }
230
231 public void onClick(View v) {
232 selectWallpaper(mGallery.getSelectedItemPosition());
233 }
Joe Onorato1b126452009-07-28 18:26:47 -0700234
235 private void setWallpaper(InputStream data, String name) throws IOException {
236 try {
237 IWallpaperService svc = IWallpaperService.Stub.asInterface(
238 ServiceManager.getService(WALLPAPER_SERVICE));
239 ParcelFileDescriptor fd = svc.setWallpaper(name);
240 if (fd == null) {
241 return;
242 }
243 FileOutputStream fos = null;
244 try {
245 fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
246 setWallpaper(data, fos);
247 } finally {
248 if (fos != null) {
249 fos.close();
250 }
251 }
252 } catch (RemoteException e) {
253 }
254 }
255
256 private void setWallpaper(InputStream data, FileOutputStream fos)
257 throws IOException {
258 byte[] buffer = new byte[32768];
259 int amt;
260 while ((amt=data.read(buffer)) > 0) {
261 fos.write(buffer, 0, amt);
262 }
263 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264}