blob: 11621cefad535272d9922abac0f4638a88644dea [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
Romain Guyedcce092010-03-04 13:03:17 -080041import com.android.launcher.R;
42
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043public class WallpaperChooser extends Activity implements AdapterView.OnItemSelectedListener,
44 OnClickListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080045 private static final String TAG = "Launcher.WallpaperChooser";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047 private Gallery mGallery;
48 private ImageView mImageView;
49 private boolean mIsWallpaperSet;
50
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051 private Bitmap mBitmap;
52
53 private ArrayList<Integer> mThumbs;
54 private ArrayList<Integer> mImages;
Romain Guye82140f2009-09-16 16:54:21 -070055 private WallpaperLoader mLoader;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056
57 @Override
58 public void onCreate(Bundle icicle) {
59 super.onCreate(icicle);
60 requestWindowFeature(Window.FEATURE_NO_TITLE);
61
62 findWallpapers();
63
64 setContentView(R.layout.wallpaper_chooser);
65
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066 mGallery = (Gallery) findViewById(R.id.gallery);
67 mGallery.setAdapter(new ImageAdapter(this));
68 mGallery.setOnItemSelectedListener(this);
69 mGallery.setCallbackDuringFling(false);
70
Romain Guy8c724f52009-09-14 15:18:12 -070071 findViewById(R.id.set).setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072
73 mImageView = (ImageView) findViewById(R.id.wallpaper);
74 }
75
76 private void findWallpapers() {
Romain Guy8eb50952009-09-02 15:57:21 -070077 mThumbs = new ArrayList<Integer>(24);
78 mImages = new ArrayList<Integer>(24);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079
80 final Resources resources = getResources();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081 final String packageName = getApplication().getPackageName();
82
Romain Guy8eb50952009-09-02 15:57:21 -070083 addWallpapers(resources, packageName, R.array.wallpapers);
84 addWallpapers(resources, packageName, R.array.extra_wallpapers);
85 }
86
87 private void addWallpapers(Resources resources, String packageName, int list) {
88 final String[] extras = resources.getStringArray(list);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089 for (String extra : extras) {
90 int res = resources.getIdentifier(extra, "drawable", packageName);
91 if (res != 0) {
92 final int thumbRes = resources.getIdentifier(extra + "_small",
93 "drawable", packageName);
94
95 if (thumbRes != 0) {
96 mThumbs.add(thumbRes);
97 mImages.add(res);
98 }
99 }
100 }
101 }
102
103 @Override
104 protected void onResume() {
105 super.onResume();
106 mIsWallpaperSet = false;
107 }
108
Romain Guy4e55c772009-09-14 15:26:18 -0700109 @Override
110 protected void onDestroy() {
111 super.onDestroy();
112
113 if (mLoader != null && mLoader.getStatus() != WallpaperLoader.Status.FINISHED) {
114 mLoader.cancel(true);
115 mLoader = null;
116 }
117 }
118
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119 public void onItemSelected(AdapterView parent, View v, int position, long id) {
Romain Guy8c724f52009-09-14 15:18:12 -0700120 if (mLoader != null && mLoader.getStatus() != WallpaperLoader.Status.FINISHED) {
Romain Guye82140f2009-09-16 16:54:21 -0700121 mLoader.cancel();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122 }
Romain Guye82140f2009-09-16 16:54:21 -0700123 mLoader = (WallpaperLoader) new WallpaperLoader().execute(position);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124 }
125
126 /*
127 * When using touch if you tap an image it triggers both the onItemClick and
128 * the onTouchEvent causing the wallpaper to be set twice. Ensure we only
129 * set the wallpaper once.
130 */
131 private void selectWallpaper(int position) {
132 if (mIsWallpaperSet) {
133 return;
134 }
135
136 mIsWallpaperSet = true;
137 try {
Romain Guy8eb50952009-09-02 15:57:21 -0700138 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
Dianne Hackborn64271802009-08-08 20:54:24 -0700139 wpm.setResource(mImages.get(position));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140 setResult(RESULT_OK);
141 finish();
142 } catch (IOException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800143 Log.e(TAG, "Failed to set wallpaper: " + e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 }
145 }
146
147 public void onNothingSelected(AdapterView parent) {
148 }
149
150 private class ImageAdapter extends BaseAdapter {
151 private LayoutInflater mLayoutInflater;
152
153 ImageAdapter(WallpaperChooser context) {
154 mLayoutInflater = context.getLayoutInflater();
155 }
156
157 public int getCount() {
158 return mThumbs.size();
159 }
160
161 public Object getItem(int position) {
162 return position;
163 }
164
165 public long getItemId(int position) {
166 return position;
167 }
168
169 public View getView(int position, View convertView, ViewGroup parent) {
170 ImageView image;
171
172 if (convertView == null) {
173 image = (ImageView) mLayoutInflater.inflate(R.layout.wallpaper_item, parent, false);
174 } else {
175 image = (ImageView) convertView;
176 }
Daniel Sandler1325f552009-09-21 15:13:31 -0400177
178 int thumbRes = mThumbs.get(position);
179 image.setImageResource(thumbRes);
180 Drawable thumbDrawable = image.getDrawable();
181 if (thumbDrawable != null) {
182 thumbDrawable.setDither(true);
183 } else {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800184 Log.e(TAG, "Error decoding thumbnail resId=" + thumbRes + " for wallpaper #"
185 + position);
Daniel Sandler1325f552009-09-21 15:13:31 -0400186 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 return image;
188 }
189 }
190
191 public void onClick(View v) {
192 selectWallpaper(mGallery.getSelectedItemPosition());
193 }
Romain Guy8c724f52009-09-14 15:18:12 -0700194
195 class WallpaperLoader extends AsyncTask<Integer, Void, Bitmap> {
Romain Guye82140f2009-09-16 16:54:21 -0700196 BitmapFactory.Options mOptions;
197
198 WallpaperLoader() {
199 mOptions = new BitmapFactory.Options();
200 mOptions.inDither = false;
201 mOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
202 }
203
Romain Guy8c724f52009-09-14 15:18:12 -0700204 protected Bitmap doInBackground(Integer... params) {
205 if (isCancelled()) return null;
Romain Guye82140f2009-09-16 16:54:21 -0700206 try {
207 return BitmapFactory.decodeResource(getResources(),
208 mImages.get(params[0]), mOptions);
209 } catch (OutOfMemoryError e) {
210 return null;
211 }
Romain Guy8c724f52009-09-14 15:18:12 -0700212 }
213
214 @Override
215 protected void onPostExecute(Bitmap b) {
216 if (b == null) return;
217
Romain Guye82140f2009-09-16 16:54:21 -0700218 if (!isCancelled() && !mOptions.mCancel) {
Romain Guy8c724f52009-09-14 15:18:12 -0700219 // Help the GC
220 if (mBitmap != null) {
221 mBitmap.recycle();
222 }
223
224 final ImageView view = mImageView;
225 view.setImageBitmap(b);
226
227 mBitmap = b;
228
229 final Drawable drawable = view.getDrawable();
230 drawable.setFilterBitmap(true);
231 drawable.setDither(true);
232
233 view.postInvalidate();
Romain Guy4e55c772009-09-14 15:26:18 -0700234
235 mLoader = null;
236 } else {
237 b.recycle();
Romain Guy8c724f52009-09-14 15:18:12 -0700238 }
239 }
Romain Guye82140f2009-09-16 16:54:21 -0700240
241 void cancel() {
242 mOptions.requestCancelDecode();
243 super.cancel(true);
244 }
Romain Guy8c724f52009-09-14 15:18:12 -0700245 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246}