blob: 46f770fc48daabaa6dd27ee75b4b984fd31799b9 [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
48 private BitmapFactory.Options mOptions;
49 private Bitmap mBitmap;
50
51 private ArrayList<Integer> mThumbs;
52 private ArrayList<Integer> mImages;
Romain Guy8c724f52009-09-14 15:18:12 -070053 private AsyncTask<Integer,Void,Bitmap> 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
107 public void onItemSelected(AdapterView parent, View v, int position, long id) {
Romain Guy8c724f52009-09-14 15:18:12 -0700108 if (mLoader != null && mLoader.getStatus() != WallpaperLoader.Status.FINISHED) {
109 mLoader.cancel(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110 }
Romain Guy8c724f52009-09-14 15:18:12 -0700111 mLoader = new WallpaperLoader().execute(position);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112 }
113
114 /*
115 * When using touch if you tap an image it triggers both the onItemClick and
116 * the onTouchEvent causing the wallpaper to be set twice. Ensure we only
117 * set the wallpaper once.
118 */
119 private void selectWallpaper(int position) {
120 if (mIsWallpaperSet) {
121 return;
122 }
123
124 mIsWallpaperSet = true;
125 try {
Romain Guy8eb50952009-09-02 15:57:21 -0700126 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
Dianne Hackborn64271802009-08-08 20:54:24 -0700127 wpm.setResource(mImages.get(position));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128 setResult(RESULT_OK);
129 finish();
130 } catch (IOException e) {
131 Log.e(Launcher.LOG_TAG, "Failed to set wallpaper: " + e);
132 }
133 }
134
135 public void onNothingSelected(AdapterView parent) {
136 }
137
138 private class ImageAdapter extends BaseAdapter {
139 private LayoutInflater mLayoutInflater;
140
141 ImageAdapter(WallpaperChooser context) {
142 mLayoutInflater = context.getLayoutInflater();
143 }
144
145 public int getCount() {
146 return mThumbs.size();
147 }
148
149 public Object getItem(int position) {
150 return position;
151 }
152
153 public long getItemId(int position) {
154 return position;
155 }
156
157 public View getView(int position, View convertView, ViewGroup parent) {
158 ImageView image;
159
160 if (convertView == null) {
161 image = (ImageView) mLayoutInflater.inflate(R.layout.wallpaper_item, parent, false);
162 } else {
163 image = (ImageView) convertView;
164 }
165
166 image.setImageResource(mThumbs.get(position));
167 image.getDrawable().setDither(true);
168 return image;
169 }
170 }
171
172 public void onClick(View v) {
173 selectWallpaper(mGallery.getSelectedItemPosition());
174 }
Romain Guy8c724f52009-09-14 15:18:12 -0700175
176 class WallpaperLoader extends AsyncTask<Integer, Void, Bitmap> {
177 protected Bitmap doInBackground(Integer... params) {
178 if (isCancelled()) return null;
179 return BitmapFactory.decodeResource(getResources(), mImages.get(params[0]), null);
180 }
181
182 @Override
183 protected void onPostExecute(Bitmap b) {
184 if (b == null) return;
185
186 if (!isCancelled()) {
187 // Help the GC
188 if (mBitmap != null) {
189 mBitmap.recycle();
190 }
191
192 final ImageView view = mImageView;
193 view.setImageBitmap(b);
194
195 mBitmap = b;
196
197 final Drawable drawable = view.getDrawable();
198 drawable.setFilterBitmap(true);
199 drawable.setDither(true);
200
201 view.postInvalidate();
202 }
203 }
204 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205}