blob: a5cce067a6039f96d5722b95c29f0857a480b7ec [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
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
19import android.app.Activity;
Dianne Hackborn107f8392009-08-05 21:32:16 -070020import android.app.WallpaperManager;
Joe Onorato1b126452009-07-28 18:26:47 -070021import 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;
Romain Guy8c724f52009-09-14 15:18:12 -070026import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.util.Log;
28import android.view.LayoutInflater;
29import android.view.View;
30import android.view.ViewGroup;
31import android.view.Window;
32import android.view.View.OnClickListener;
33import android.widget.AdapterView;
34import android.widget.BaseAdapter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.widget.Gallery;
36import android.widget.ImageView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037
38import java.io.IOException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import java.util.ArrayList;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040
41public class WallpaperChooser extends Activity implements AdapterView.OnItemSelectedListener,
42 OnClickListener {
43
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044 private Gallery mGallery;
45 private ImageView mImageView;
46 private boolean mIsWallpaperSet;
47
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048 private Bitmap mBitmap;
49
50 private ArrayList<Integer> mThumbs;
51 private ArrayList<Integer> mImages;
Romain Guye82140f2009-09-16 16:54:21 -070052 private WallpaperLoader mLoader;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053
54 @Override
55 public void onCreate(Bundle icicle) {
56 super.onCreate(icicle);
57 requestWindowFeature(Window.FEATURE_NO_TITLE);
58
59 findWallpapers();
60
61 setContentView(R.layout.wallpaper_chooser);
62
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063 mGallery = (Gallery) findViewById(R.id.gallery);
64 mGallery.setAdapter(new ImageAdapter(this));
65 mGallery.setOnItemSelectedListener(this);
66 mGallery.setCallbackDuringFling(false);
67
Romain Guy8c724f52009-09-14 15:18:12 -070068 findViewById(R.id.set).setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069
70 mImageView = (ImageView) findViewById(R.id.wallpaper);
71 }
72
73 private void findWallpapers() {
Romain Guy8eb50952009-09-02 15:57:21 -070074 mThumbs = new ArrayList<Integer>(24);
75 mImages = new ArrayList<Integer>(24);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076
77 final Resources resources = getResources();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078 final String packageName = getApplication().getPackageName();
79
Romain Guy8eb50952009-09-02 15:57:21 -070080 addWallpapers(resources, packageName, R.array.wallpapers);
81 addWallpapers(resources, packageName, R.array.extra_wallpapers);
82 }
83
84 private void addWallpapers(Resources resources, String packageName, int list) {
85 final String[] extras = resources.getStringArray(list);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 for (String extra : extras) {
87 int res = resources.getIdentifier(extra, "drawable", packageName);
88 if (res != 0) {
89 final int thumbRes = resources.getIdentifier(extra + "_small",
90 "drawable", packageName);
91
92 if (thumbRes != 0) {
93 mThumbs.add(thumbRes);
94 mImages.add(res);
95 }
96 }
97 }
98 }
99
100 @Override
101 protected void onResume() {
102 super.onResume();
103 mIsWallpaperSet = false;
104 }
105
Romain Guy4e55c772009-09-14 15:26:18 -0700106 @Override
107 protected void onDestroy() {
108 super.onDestroy();
109
110 if (mLoader != null && mLoader.getStatus() != WallpaperLoader.Status.FINISHED) {
111 mLoader.cancel(true);
112 mLoader = null;
113 }
114 }
115
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 public void onItemSelected(AdapterView parent, View v, int position, long id) {
Romain Guy8c724f52009-09-14 15:18:12 -0700117 if (mLoader != null && mLoader.getStatus() != WallpaperLoader.Status.FINISHED) {
Romain Guye82140f2009-09-16 16:54:21 -0700118 mLoader.cancel();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119 }
Romain Guye82140f2009-09-16 16:54:21 -0700120 mLoader = (WallpaperLoader) new WallpaperLoader().execute(position);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 }
122
123 /*
124 * When using touch if you tap an image it triggers both the onItemClick and
125 * the onTouchEvent causing the wallpaper to be set twice. Ensure we only
126 * set the wallpaper once.
127 */
128 private void selectWallpaper(int position) {
129 if (mIsWallpaperSet) {
130 return;
131 }
132
133 mIsWallpaperSet = true;
134 try {
Romain Guy8eb50952009-09-02 15:57:21 -0700135 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
Dianne Hackborn64271802009-08-08 20:54:24 -0700136 wpm.setResource(mImages.get(position));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 setResult(RESULT_OK);
138 finish();
139 } catch (IOException e) {
140 Log.e(Launcher.LOG_TAG, "Failed to set wallpaper: " + e);
141 }
142 }
143
144 public void onNothingSelected(AdapterView parent) {
145 }
146
147 private class ImageAdapter extends BaseAdapter {
148 private LayoutInflater mLayoutInflater;
149
150 ImageAdapter(WallpaperChooser context) {
151 mLayoutInflater = context.getLayoutInflater();
152 }
153
154 public int getCount() {
155 return mThumbs.size();
156 }
157
158 public Object getItem(int position) {
159 return position;
160 }
161
162 public long getItemId(int position) {
163 return position;
164 }
165
166 public View getView(int position, View convertView, ViewGroup parent) {
167 ImageView image;
168
169 if (convertView == null) {
170 image = (ImageView) mLayoutInflater.inflate(R.layout.wallpaper_item, parent, false);
171 } else {
172 image = (ImageView) convertView;
173 }
174
175 image.setImageResource(mThumbs.get(position));
176 image.getDrawable().setDither(true);
177 return image;
178 }
179 }
180
181 public void onClick(View v) {
182 selectWallpaper(mGallery.getSelectedItemPosition());
183 }
Romain Guy8c724f52009-09-14 15:18:12 -0700184
185 class WallpaperLoader extends AsyncTask<Integer, Void, Bitmap> {
Romain Guye82140f2009-09-16 16:54:21 -0700186 BitmapFactory.Options mOptions;
187
188 WallpaperLoader() {
189 mOptions = new BitmapFactory.Options();
190 mOptions.inDither = false;
191 mOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
192 }
193
Romain Guy8c724f52009-09-14 15:18:12 -0700194 protected Bitmap doInBackground(Integer... params) {
195 if (isCancelled()) return null;
Romain Guye82140f2009-09-16 16:54:21 -0700196 try {
197 return BitmapFactory.decodeResource(getResources(),
198 mImages.get(params[0]), mOptions);
199 } catch (OutOfMemoryError e) {
200 return null;
201 }
Romain Guy8c724f52009-09-14 15:18:12 -0700202 }
203
204 @Override
205 protected void onPostExecute(Bitmap b) {
206 if (b == null) return;
207
Romain Guye82140f2009-09-16 16:54:21 -0700208 if (!isCancelled() && !mOptions.mCancel) {
Romain Guy8c724f52009-09-14 15:18:12 -0700209 // Help the GC
210 if (mBitmap != null) {
211 mBitmap.recycle();
212 }
213
214 final ImageView view = mImageView;
215 view.setImageBitmap(b);
216
217 mBitmap = b;
218
219 final Drawable drawable = view.getDrawable();
220 drawable.setFilterBitmap(true);
221 drawable.setDither(true);
222
223 view.postInvalidate();
Romain Guy4e55c772009-09-14 15:26:18 -0700224
225 mLoader = null;
226 } else {
227 b.recycle();
Romain Guy8c724f52009-09-14 15:18:12 -0700228 }
229 }
Romain Guye82140f2009-09-16 16:54:21 -0700230
231 void cancel() {
232 mOptions.requestCancelDecode();
233 super.cancel(true);
234 }
Romain Guy8c724f52009-09-14 15:18:12 -0700235 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236}