blob: 1eb8d0cb3f87d3da7ebce920dc8a7d36460dbab0 [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
The Android Open Source Project62b49bb2009-01-20 14:04:00 -0800126 for (String extra : extras) {
127 int res = resources.getIdentifier(extra, "drawable", packageName);
128 if (res != 0) {
129 final int thumbRes = resources.getIdentifier(extra + "_small",
130 "drawable", packageName);
131
132 if (thumbRes != 0) {
The Android Open Source Project15a88802009-02-10 15:44:05 -0800133 mThumbs.add(res);
134 mImages.add(res);
The Android Open Source Project62b49bb2009-01-20 14:04:00 -0800135 }
136 }
137 }
138 }
139
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700140 @Override
141 protected void onResume() {
142 super.onResume();
143 mIsWallpaperSet = false;
144 }
145
146 public void onItemSelected(AdapterView parent, View v, int position, long id) {
147 final ImageView view = mImageView;
The Android Open Source Project15a88802009-02-10 15:44:05 -0800148 Bitmap b = BitmapFactory.decodeResource(getResources(), mImages.get(position), mOptions);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700149 view.setImageBitmap(b);
150
151 // Help the GC
152 if (mBitmap != null) {
153 mBitmap.recycle();
154 }
155 mBitmap = b;
156
157 final Drawable drawable = view.getDrawable();
158 drawable.setFilterBitmap(true);
159 drawable.setDither(true);
160 }
161
162 /*
163 * When using touch if you tap an image it triggers both the onItemClick and
164 * the onTouchEvent causing the wallpaper to be set twice. Ensure we only
165 * set the wallpaper once.
166 */
167 private void selectWallpaper(int position) {
168 if (mIsWallpaperSet) {
169 return;
170 }
171
172 mIsWallpaperSet = true;
173 try {
The Android Open Source Project62b49bb2009-01-20 14:04:00 -0800174 InputStream stream = getResources().openRawResource(mImages.get(position));
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700175 setWallpaper(stream);
176 setResult(RESULT_OK);
177 finish();
178 } catch (IOException e) {
179 Log.e(Launcher.LOG_TAG, "Failed to set wallpaper: " + e);
180 }
181 }
182
183 public void onNothingSelected(AdapterView parent) {
184 }
185
186 private class ImageAdapter extends BaseAdapter {
187 private LayoutInflater mLayoutInflater;
188
189 ImageAdapter(WallpaperChooser context) {
190 mLayoutInflater = context.getLayoutInflater();
191 }
192
193 public int getCount() {
The Android Open Source Project62b49bb2009-01-20 14:04:00 -0800194 return mThumbs.size();
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700195 }
196
197 public Object getItem(int position) {
198 return position;
199 }
200
201 public long getItemId(int position) {
202 return position;
203 }
204
205 public View getView(int position, View convertView, ViewGroup parent) {
206 ImageView image;
207
208 if (convertView == null) {
209 image = (ImageView) mLayoutInflater.inflate(R.layout.wallpaper_item, parent, false);
210 } else {
211 image = (ImageView) convertView;
212 }
213
The Android Open Source Project62b49bb2009-01-20 14:04:00 -0800214 image.setImageResource(mThumbs.get(position));
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700215 image.getDrawable().setDither(true);
216 return image;
217 }
218 }
219
220 public void onClick(View v) {
221 selectWallpaper(mGallery.getSelectedItemPosition());
222 }
223}