blob: bf8ba2e8dda197f01974a250d66c11032e02126d [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();
Daniel Sandler76c0e142010-03-05 11:04:10 -050081 // Context.getPackageName() may return the "original" package name,
82 // com.android.launcher2; Resources needs the real package name,
83 // com.android.launcher. So we ask Resources for what it thinks the
84 // package name should be.
85 final String packageName = resources.getResourcePackageName(R.array.wallpapers);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086
Romain Guy8eb50952009-09-02 15:57:21 -070087 addWallpapers(resources, packageName, R.array.wallpapers);
88 addWallpapers(resources, packageName, R.array.extra_wallpapers);
89 }
90
91 private void addWallpapers(Resources resources, String packageName, int list) {
92 final String[] extras = resources.getStringArray(list);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093 for (String extra : extras) {
94 int res = resources.getIdentifier(extra, "drawable", packageName);
95 if (res != 0) {
96 final int thumbRes = resources.getIdentifier(extra + "_small",
97 "drawable", packageName);
98
99 if (thumbRes != 0) {
100 mThumbs.add(thumbRes);
101 mImages.add(res);
Daniel Sandler76c0e142010-03-05 11:04:10 -0500102 // Log.d(TAG, "addWallpapers: [" + packageName + "]: " + extra + " (" + res + ")");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103 }
104 }
105 }
106 }
107
108 @Override
109 protected void onResume() {
110 super.onResume();
111 mIsWallpaperSet = false;
112 }
113
Romain Guy4e55c772009-09-14 15:26:18 -0700114 @Override
115 protected void onDestroy() {
116 super.onDestroy();
117
118 if (mLoader != null && mLoader.getStatus() != WallpaperLoader.Status.FINISHED) {
119 mLoader.cancel(true);
120 mLoader = null;
121 }
122 }
123
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124 public void onItemSelected(AdapterView parent, View v, int position, long id) {
Romain Guy8c724f52009-09-14 15:18:12 -0700125 if (mLoader != null && mLoader.getStatus() != WallpaperLoader.Status.FINISHED) {
Romain Guye82140f2009-09-16 16:54:21 -0700126 mLoader.cancel();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127 }
Romain Guye82140f2009-09-16 16:54:21 -0700128 mLoader = (WallpaperLoader) new WallpaperLoader().execute(position);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 }
130
131 /*
132 * When using touch if you tap an image it triggers both the onItemClick and
133 * the onTouchEvent causing the wallpaper to be set twice. Ensure we only
134 * set the wallpaper once.
135 */
136 private void selectWallpaper(int position) {
137 if (mIsWallpaperSet) {
138 return;
139 }
140
141 mIsWallpaperSet = true;
142 try {
Romain Guy8eb50952009-09-02 15:57:21 -0700143 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
Dianne Hackborn64271802009-08-08 20:54:24 -0700144 wpm.setResource(mImages.get(position));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145 setResult(RESULT_OK);
146 finish();
147 } catch (IOException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800148 Log.e(TAG, "Failed to set wallpaper: " + e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 }
150 }
151
152 public void onNothingSelected(AdapterView parent) {
153 }
154
155 private class ImageAdapter extends BaseAdapter {
156 private LayoutInflater mLayoutInflater;
157
158 ImageAdapter(WallpaperChooser context) {
159 mLayoutInflater = context.getLayoutInflater();
160 }
161
162 public int getCount() {
163 return mThumbs.size();
164 }
165
166 public Object getItem(int position) {
167 return position;
168 }
169
170 public long getItemId(int position) {
171 return position;
172 }
173
174 public View getView(int position, View convertView, ViewGroup parent) {
175 ImageView image;
176
177 if (convertView == null) {
178 image = (ImageView) mLayoutInflater.inflate(R.layout.wallpaper_item, parent, false);
179 } else {
180 image = (ImageView) convertView;
181 }
Daniel Sandler1325f552009-09-21 15:13:31 -0400182
183 int thumbRes = mThumbs.get(position);
184 image.setImageResource(thumbRes);
185 Drawable thumbDrawable = image.getDrawable();
186 if (thumbDrawable != null) {
187 thumbDrawable.setDither(true);
188 } else {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800189 Log.e(TAG, "Error decoding thumbnail resId=" + thumbRes + " for wallpaper #"
190 + position);
Daniel Sandler1325f552009-09-21 15:13:31 -0400191 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 return image;
193 }
194 }
195
196 public void onClick(View v) {
197 selectWallpaper(mGallery.getSelectedItemPosition());
198 }
Romain Guy8c724f52009-09-14 15:18:12 -0700199
200 class WallpaperLoader extends AsyncTask<Integer, Void, Bitmap> {
Romain Guye82140f2009-09-16 16:54:21 -0700201 BitmapFactory.Options mOptions;
202
203 WallpaperLoader() {
204 mOptions = new BitmapFactory.Options();
205 mOptions.inDither = false;
206 mOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
207 }
208
Romain Guy8c724f52009-09-14 15:18:12 -0700209 protected Bitmap doInBackground(Integer... params) {
210 if (isCancelled()) return null;
Romain Guye82140f2009-09-16 16:54:21 -0700211 try {
212 return BitmapFactory.decodeResource(getResources(),
213 mImages.get(params[0]), mOptions);
214 } catch (OutOfMemoryError e) {
215 return null;
216 }
Romain Guy8c724f52009-09-14 15:18:12 -0700217 }
218
219 @Override
220 protected void onPostExecute(Bitmap b) {
221 if (b == null) return;
222
Romain Guye82140f2009-09-16 16:54:21 -0700223 if (!isCancelled() && !mOptions.mCancel) {
Romain Guy8c724f52009-09-14 15:18:12 -0700224 // Help the GC
225 if (mBitmap != null) {
226 mBitmap.recycle();
227 }
228
229 final ImageView view = mImageView;
230 view.setImageBitmap(b);
231
232 mBitmap = b;
233
234 final Drawable drawable = view.getDrawable();
235 drawable.setFilterBitmap(true);
236 drawable.setDither(true);
237
238 view.postInvalidate();
Romain Guy4e55c772009-09-14 15:26:18 -0700239
240 mLoader = null;
241 } else {
242 b.recycle();
Romain Guy8c724f52009-09-14 15:18:12 -0700243 }
244 }
Romain Guye82140f2009-09-16 16:54:21 -0700245
246 void cancel() {
247 mOptions.requestCancelDecode();
248 super.cancel(true);
249 }
Romain Guy8c724f52009-09-14 15:18:12 -0700250 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251}