blob: 8045059e789a4ba4d8e4cb745c3a944a99410969 [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 {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080043 private static final String TAG = "Launcher.WallpaperChooser";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045 private Gallery mGallery;
46 private ImageView mImageView;
47 private boolean mIsWallpaperSet;
48
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049 private Bitmap mBitmap;
50
51 private ArrayList<Integer> mThumbs;
52 private ArrayList<Integer> mImages;
Romain Guye82140f2009-09-16 16:54:21 -070053 private WallpaperLoader mLoader;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054
55 @Override
56 public void onCreate(Bundle icicle) {
57 super.onCreate(icicle);
58 requestWindowFeature(Window.FEATURE_NO_TITLE);
59
60 findWallpapers();
61
62 setContentView(R.layout.wallpaper_chooser);
63
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064 mGallery = (Gallery) findViewById(R.id.gallery);
65 mGallery.setAdapter(new ImageAdapter(this));
66 mGallery.setOnItemSelectedListener(this);
67 mGallery.setCallbackDuringFling(false);
68
Romain Guy8c724f52009-09-14 15:18:12 -070069 findViewById(R.id.set).setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070
71 mImageView = (ImageView) findViewById(R.id.wallpaper);
72 }
73
74 private void findWallpapers() {
Romain Guy8eb50952009-09-02 15:57:21 -070075 mThumbs = new ArrayList<Integer>(24);
76 mImages = new ArrayList<Integer>(24);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077
78 final Resources resources = getResources();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079 final String packageName = getApplication().getPackageName();
80
Romain Guy8eb50952009-09-02 15:57:21 -070081 addWallpapers(resources, packageName, R.array.wallpapers);
82 addWallpapers(resources, packageName, R.array.extra_wallpapers);
83 }
84
85 private void addWallpapers(Resources resources, String packageName, int list) {
86 final String[] extras = resources.getStringArray(list);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087 for (String extra : extras) {
88 int res = resources.getIdentifier(extra, "drawable", packageName);
89 if (res != 0) {
90 final int thumbRes = resources.getIdentifier(extra + "_small",
91 "drawable", packageName);
92
93 if (thumbRes != 0) {
94 mThumbs.add(thumbRes);
95 mImages.add(res);
96 }
97 }
98 }
99 }
100
101 @Override
102 protected void onResume() {
103 super.onResume();
104 mIsWallpaperSet = false;
105 }
106
Romain Guy4e55c772009-09-14 15:26:18 -0700107 @Override
108 protected void onDestroy() {
109 super.onDestroy();
110
111 if (mLoader != null && mLoader.getStatus() != WallpaperLoader.Status.FINISHED) {
112 mLoader.cancel(true);
113 mLoader = null;
114 }
115 }
116
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117 public void onItemSelected(AdapterView parent, View v, int position, long id) {
Romain Guy8c724f52009-09-14 15:18:12 -0700118 if (mLoader != null && mLoader.getStatus() != WallpaperLoader.Status.FINISHED) {
Romain Guye82140f2009-09-16 16:54:21 -0700119 mLoader.cancel();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120 }
Romain Guye82140f2009-09-16 16:54:21 -0700121 mLoader = (WallpaperLoader) new WallpaperLoader().execute(position);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122 }
123
124 /*
125 * When using touch if you tap an image it triggers both the onItemClick and
126 * the onTouchEvent causing the wallpaper to be set twice. Ensure we only
127 * set the wallpaper once.
128 */
129 private void selectWallpaper(int position) {
130 if (mIsWallpaperSet) {
131 return;
132 }
133
134 mIsWallpaperSet = true;
135 try {
Romain Guy8eb50952009-09-02 15:57:21 -0700136 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
Dianne Hackborn64271802009-08-08 20:54:24 -0700137 wpm.setResource(mImages.get(position));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138 setResult(RESULT_OK);
139 finish();
140 } catch (IOException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800141 Log.e(TAG, "Failed to set wallpaper: " + e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142 }
143 }
144
145 public void onNothingSelected(AdapterView parent) {
146 }
147
148 private class ImageAdapter extends BaseAdapter {
149 private LayoutInflater mLayoutInflater;
150
151 ImageAdapter(WallpaperChooser context) {
152 mLayoutInflater = context.getLayoutInflater();
153 }
154
155 public int getCount() {
156 return mThumbs.size();
157 }
158
159 public Object getItem(int position) {
160 return position;
161 }
162
163 public long getItemId(int position) {
164 return position;
165 }
166
167 public View getView(int position, View convertView, ViewGroup parent) {
168 ImageView image;
169
170 if (convertView == null) {
171 image = (ImageView) mLayoutInflater.inflate(R.layout.wallpaper_item, parent, false);
172 } else {
173 image = (ImageView) convertView;
174 }
Daniel Sandler1325f552009-09-21 15:13:31 -0400175
176 int thumbRes = mThumbs.get(position);
177 image.setImageResource(thumbRes);
178 Drawable thumbDrawable = image.getDrawable();
179 if (thumbDrawable != null) {
180 thumbDrawable.setDither(true);
181 } else {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800182 Log.e(TAG, "Error decoding thumbnail resId=" + thumbRes + " for wallpaper #"
183 + position);
Daniel Sandler1325f552009-09-21 15:13:31 -0400184 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 return image;
186 }
187 }
188
189 public void onClick(View v) {
190 selectWallpaper(mGallery.getSelectedItemPosition());
191 }
Romain Guy8c724f52009-09-14 15:18:12 -0700192
193 class WallpaperLoader extends AsyncTask<Integer, Void, Bitmap> {
Romain Guye82140f2009-09-16 16:54:21 -0700194 BitmapFactory.Options mOptions;
195
196 WallpaperLoader() {
197 mOptions = new BitmapFactory.Options();
198 mOptions.inDither = false;
199 mOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
200 }
201
Romain Guy8c724f52009-09-14 15:18:12 -0700202 protected Bitmap doInBackground(Integer... params) {
203 if (isCancelled()) return null;
Romain Guye82140f2009-09-16 16:54:21 -0700204 try {
205 return BitmapFactory.decodeResource(getResources(),
206 mImages.get(params[0]), mOptions);
207 } catch (OutOfMemoryError e) {
208 return null;
209 }
Romain Guy8c724f52009-09-14 15:18:12 -0700210 }
211
212 @Override
213 protected void onPostExecute(Bitmap b) {
214 if (b == null) return;
215
Romain Guye82140f2009-09-16 16:54:21 -0700216 if (!isCancelled() && !mOptions.mCancel) {
Romain Guy8c724f52009-09-14 15:18:12 -0700217 // Help the GC
218 if (mBitmap != null) {
219 mBitmap.recycle();
220 }
221
222 final ImageView view = mImageView;
223 view.setImageBitmap(b);
224
225 mBitmap = b;
226
227 final Drawable drawable = view.getDrawable();
228 drawable.setFilterBitmap(true);
229 drawable.setDither(true);
230
231 view.postInvalidate();
Romain Guy4e55c772009-09-14 15:26:18 -0700232
233 mLoader = null;
234 } else {
235 b.recycle();
Romain Guy8c724f52009-09-14 15:18:12 -0700236 }
237 }
Romain Guye82140f2009-09-16 16:54:21 -0700238
239 void cancel() {
240 mOptions.requestCancelDecode();
241 super.cancel(true);
242 }
Romain Guy8c724f52009-09-14 15:18:12 -0700243 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244}