blob: afbe6f352b0055a58125f7fb5bfbff2bc3620e25 [file] [log] [blame]
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001/*
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
17package com.android.launcher;
18
19import android.app.Activity;
20import android.os.Bundle;
21import android.util.Log;
22import android.view.LayoutInflater;
23import android.view.View;
24import android.view.ViewGroup;
25import android.view.Window;
26import android.view.View.OnClickListener;
27import android.widget.AdapterView;
28import android.widget.BaseAdapter;
29import android.widget.Button;
30import android.widget.Gallery;
31import android.widget.ImageView;
32import android.graphics.BitmapFactory;
33import android.graphics.Bitmap;
34import android.graphics.drawable.Drawable;
The Android Open Source Project62b49bb2009-01-20 14:04:00 -080035import android.content.res.Resources;
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -070036
37import java.io.IOException;
38import java.io.InputStream;
The Android Open Source Project62b49bb2009-01-20 14:04:00 -080039import java.util.ArrayList;
40import java.util.Collections;
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -070041
42public class WallpaperChooser extends Activity implements AdapterView.OnItemSelectedListener,
43 OnClickListener {
44
45 private static final Integer[] THUMB_IDS = {
46 R.drawable.wallpaper_lake_small,
47 R.drawable.wallpaper_sunset_small,
48 R.drawable.wallpaper_beach_small,
49 R.drawable.wallpaper_snow_leopard_small,
50 R.drawable.wallpaper_path_small,
51 R.drawable.wallpaper_sunrise_small,
52 R.drawable.wallpaper_mountain_small,
53 R.drawable.wallpaper_ripples_small,
54 R.drawable.wallpaper_road_small,
55 R.drawable.wallpaper_jellyfish_small,
56 R.drawable.wallpaper_zanzibar_small,
57 R.drawable.wallpaper_blue_small,
58 R.drawable.wallpaper_grey_small,
59 R.drawable.wallpaper_green_small,
60 R.drawable.wallpaper_pink_small,
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -070061 };
62
63 private static final Integer[] IMAGE_IDS = {
64 R.drawable.wallpaper_lake,
65 R.drawable.wallpaper_sunset,
66 R.drawable.wallpaper_beach,
67 R.drawable.wallpaper_snow_leopard,
68 R.drawable.wallpaper_path,
69 R.drawable.wallpaper_sunrise,
70 R.drawable.wallpaper_mountain,
71 R.drawable.wallpaper_ripples,
72 R.drawable.wallpaper_road,
73 R.drawable.wallpaper_jellyfish,
74 R.drawable.wallpaper_zanzibar,
75 R.drawable.wallpaper_blue,
76 R.drawable.wallpaper_grey,
77 R.drawable.wallpaper_green,
78 R.drawable.wallpaper_pink,
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -070079 };
80
81 private Gallery mGallery;
82 private ImageView mImageView;
83 private boolean mIsWallpaperSet;
84
85 private BitmapFactory.Options mOptions;
86 private Bitmap mBitmap;
87
The Android Open Source Project62b49bb2009-01-20 14:04:00 -080088 private ArrayList<Integer> mThumbs;
89 private ArrayList<Integer> mImages;
90
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -070091 @Override
92 public void onCreate(Bundle icicle) {
93 super.onCreate(icicle);
94 requestWindowFeature(Window.FEATURE_NO_TITLE);
95
The Android Open Source Project62b49bb2009-01-20 14:04:00 -080096 findWallpapers();
97
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -070098 setContentView(R.layout.wallpaper_chooser);
99
100 mOptions = new BitmapFactory.Options();
101 mOptions.inDither = false;
102 mOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
103
104 mGallery = (Gallery) findViewById(R.id.gallery);
105 mGallery.setAdapter(new ImageAdapter(this));
106 mGallery.setOnItemSelectedListener(this);
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800107 mGallery.setCallbackDuringFling(false);
The Android Open Source Project62b49bb2009-01-20 14:04:00 -0800108
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700109 Button b = (Button) findViewById(R.id.set);
110 b.setOnClickListener(this);
111
112 mImageView = (ImageView) findViewById(R.id.wallpaper);
113 }
114
The Android Open Source Project62b49bb2009-01-20 14:04:00 -0800115 private void findWallpapers() {
116 mThumbs = new ArrayList<Integer>(THUMB_IDS.length + 4);
117 Collections.addAll(mThumbs, THUMB_IDS);
118
119 mImages = new ArrayList<Integer>(IMAGE_IDS.length + 4);
120 Collections.addAll(mImages, IMAGE_IDS);
121
122 final Resources resources = getResources();
123 final String[] extras = resources.getStringArray(R.array.extra_wallpapers);
124 final String packageName = getApplication().getPackageName();
125
126 final ArrayList<Integer> images = mImages;
127 final ArrayList<Integer> thumbs = mThumbs;
128
129 for (String extra : extras) {
130 int res = resources.getIdentifier(extra, "drawable", packageName);
131 if (res != 0) {
132 final int thumbRes = resources.getIdentifier(extra + "_small",
133 "drawable", packageName);
134
135 if (thumbRes != 0) {
136 images.add(res);
137 thumbs.add(res);
138 }
139 }
140 }
141 }
142
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700143 @Override
144 protected void onResume() {
145 super.onResume();
146 mIsWallpaperSet = false;
147 }
148
149 public void onItemSelected(AdapterView parent, View v, int position, long id) {
150 final ImageView view = mImageView;
151 Bitmap b = BitmapFactory.decodeResource(getResources(), IMAGE_IDS[position], mOptions);
152 view.setImageBitmap(b);
153
154 // Help the GC
155 if (mBitmap != null) {
156 mBitmap.recycle();
157 }
158 mBitmap = b;
159
160 final Drawable drawable = view.getDrawable();
161 drawable.setFilterBitmap(true);
162 drawable.setDither(true);
163 }
164
165 /*
166 * When using touch if you tap an image it triggers both the onItemClick and
167 * the onTouchEvent causing the wallpaper to be set twice. Ensure we only
168 * set the wallpaper once.
169 */
170 private void selectWallpaper(int position) {
171 if (mIsWallpaperSet) {
172 return;
173 }
174
175 mIsWallpaperSet = true;
176 try {
The Android Open Source Project62b49bb2009-01-20 14:04:00 -0800177 InputStream stream = getResources().openRawResource(mImages.get(position));
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700178 setWallpaper(stream);
179 setResult(RESULT_OK);
180 finish();
181 } catch (IOException e) {
182 Log.e(Launcher.LOG_TAG, "Failed to set wallpaper: " + e);
183 }
184 }
185
186 public void onNothingSelected(AdapterView parent) {
187 }
188
189 private class ImageAdapter extends BaseAdapter {
190 private LayoutInflater mLayoutInflater;
191
192 ImageAdapter(WallpaperChooser context) {
193 mLayoutInflater = context.getLayoutInflater();
194 }
195
196 public int getCount() {
The Android Open Source Project62b49bb2009-01-20 14:04:00 -0800197 return mThumbs.size();
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700198 }
199
200 public Object getItem(int position) {
201 return position;
202 }
203
204 public long getItemId(int position) {
205 return position;
206 }
207
208 public View getView(int position, View convertView, ViewGroup parent) {
209 ImageView image;
210
211 if (convertView == null) {
212 image = (ImageView) mLayoutInflater.inflate(R.layout.wallpaper_item, parent, false);
213 } else {
214 image = (ImageView) convertView;
215 }
216
The Android Open Source Project62b49bb2009-01-20 14:04:00 -0800217 image.setImageResource(mThumbs.get(position));
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700218 image.getDrawable().setDither(true);
219 return image;
220 }
221 }
222
223 public void onClick(View v) {
224 selectWallpaper(mGallery.getSelectedItemPosition());
225 }
226}