blob: 268b571dbcf290698b2f18e168156411eb5cc4f1 [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;
35
36import java.io.IOException;
37import java.io.InputStream;
38
39public class WallpaperChooser extends Activity implements AdapterView.OnItemSelectedListener,
40 OnClickListener {
41
42 private static final Integer[] THUMB_IDS = {
43 R.drawable.wallpaper_lake_small,
44 R.drawable.wallpaper_sunset_small,
45 R.drawable.wallpaper_beach_small,
46 R.drawable.wallpaper_snow_leopard_small,
47 R.drawable.wallpaper_path_small,
48 R.drawable.wallpaper_sunrise_small,
49 R.drawable.wallpaper_mountain_small,
50 R.drawable.wallpaper_ripples_small,
51 R.drawable.wallpaper_road_small,
52 R.drawable.wallpaper_jellyfish_small,
53 R.drawable.wallpaper_zanzibar_small,
54 R.drawable.wallpaper_blue_small,
55 R.drawable.wallpaper_grey_small,
56 R.drawable.wallpaper_green_small,
57 R.drawable.wallpaper_pink_small,
58 R.drawable.wallpaper_dale_chihuly_small,
59 R.drawable.wallpaper_john_maeda_small,
60 R.drawable.wallpaper_marc_ecko_small,
61 };
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,
79 R.drawable.wallpaper_dale_chihuly,
80 R.drawable.wallpaper_john_maeda,
81 R.drawable.wallpaper_marc_ecko,
82 };
83
84 private Gallery mGallery;
85 private ImageView mImageView;
86 private boolean mIsWallpaperSet;
87
88 private BitmapFactory.Options mOptions;
89 private Bitmap mBitmap;
90
91 @Override
92 public void onCreate(Bundle icicle) {
93 super.onCreate(icicle);
94 requestWindowFeature(Window.FEATURE_NO_TITLE);
95
96 setContentView(R.layout.wallpaper_chooser);
97
98 mOptions = new BitmapFactory.Options();
99 mOptions.inDither = false;
100 mOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
101
102 mGallery = (Gallery) findViewById(R.id.gallery);
103 mGallery.setAdapter(new ImageAdapter(this));
104 mGallery.setOnItemSelectedListener(this);
105
106 Button b = (Button) findViewById(R.id.set);
107 b.setOnClickListener(this);
108
109 mImageView = (ImageView) findViewById(R.id.wallpaper);
110 }
111
112 @Override
113 protected void onResume() {
114 super.onResume();
115 mIsWallpaperSet = false;
116 }
117
118 public void onItemSelected(AdapterView parent, View v, int position, long id) {
119 final ImageView view = mImageView;
120 Bitmap b = BitmapFactory.decodeResource(getResources(), IMAGE_IDS[position], mOptions);
121 view.setImageBitmap(b);
122
123 // Help the GC
124 if (mBitmap != null) {
125 mBitmap.recycle();
126 }
127 mBitmap = b;
128
129 final Drawable drawable = view.getDrawable();
130 drawable.setFilterBitmap(true);
131 drawable.setDither(true);
132 }
133
134 /*
135 * When using touch if you tap an image it triggers both the onItemClick and
136 * the onTouchEvent causing the wallpaper to be set twice. Ensure we only
137 * set the wallpaper once.
138 */
139 private void selectWallpaper(int position) {
140 if (mIsWallpaperSet) {
141 return;
142 }
143
144 mIsWallpaperSet = true;
145 try {
146 InputStream stream = getResources().openRawResource(IMAGE_IDS[position]);
147 setWallpaper(stream);
148 setResult(RESULT_OK);
149 finish();
150 } catch (IOException e) {
151 Log.e(Launcher.LOG_TAG, "Failed to set wallpaper: " + e);
152 }
153 }
154
155 public void onNothingSelected(AdapterView parent) {
156 }
157
158 private class ImageAdapter extends BaseAdapter {
159 private LayoutInflater mLayoutInflater;
160
161 ImageAdapter(WallpaperChooser context) {
162 mLayoutInflater = context.getLayoutInflater();
163 }
164
165 public int getCount() {
166 return THUMB_IDS.length;
167 }
168
169 public Object getItem(int position) {
170 return position;
171 }
172
173 public long getItemId(int position) {
174 return position;
175 }
176
177 public View getView(int position, View convertView, ViewGroup parent) {
178 ImageView image;
179
180 if (convertView == null) {
181 image = (ImageView) mLayoutInflater.inflate(R.layout.wallpaper_item, parent, false);
182 } else {
183 image = (ImageView) convertView;
184 }
185
186 image.setImageResource(THUMB_IDS[position]);
187 image.getDrawable().setDither(true);
188 return image;
189 }
190 }
191
192 public void onClick(View v) {
193 selectWallpaper(mGallery.getSelectedItemPosition());
194 }
195}