blob: a4e583e30df031e13a765c2e8ac52443aa30fa09 [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);
The Android Open Source Projectd097a182008-12-17 18:05:58 -0800105 mGallery.setCallbackDuringFling(false);
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -0700106
107 Button b = (Button) findViewById(R.id.set);
108 b.setOnClickListener(this);
109
110 mImageView = (ImageView) findViewById(R.id.wallpaper);
111 }
112
113 @Override
114 protected void onResume() {
115 super.onResume();
116 mIsWallpaperSet = false;
117 }
118
119 public void onItemSelected(AdapterView parent, View v, int position, long id) {
120 final ImageView view = mImageView;
121 Bitmap b = BitmapFactory.decodeResource(getResources(), IMAGE_IDS[position], mOptions);
122 view.setImageBitmap(b);
123
124 // Help the GC
125 if (mBitmap != null) {
126 mBitmap.recycle();
127 }
128 mBitmap = b;
129
130 final Drawable drawable = view.getDrawable();
131 drawable.setFilterBitmap(true);
132 drawable.setDither(true);
133 }
134
135 /*
136 * When using touch if you tap an image it triggers both the onItemClick and
137 * the onTouchEvent causing the wallpaper to be set twice. Ensure we only
138 * set the wallpaper once.
139 */
140 private void selectWallpaper(int position) {
141 if (mIsWallpaperSet) {
142 return;
143 }
144
145 mIsWallpaperSet = true;
146 try {
147 InputStream stream = getResources().openRawResource(IMAGE_IDS[position]);
148 setWallpaper(stream);
149 setResult(RESULT_OK);
150 finish();
151 } catch (IOException e) {
152 Log.e(Launcher.LOG_TAG, "Failed to set wallpaper: " + e);
153 }
154 }
155
156 public void onNothingSelected(AdapterView parent) {
157 }
158
159 private class ImageAdapter extends BaseAdapter {
160 private LayoutInflater mLayoutInflater;
161
162 ImageAdapter(WallpaperChooser context) {
163 mLayoutInflater = context.getLayoutInflater();
164 }
165
166 public int getCount() {
167 return THUMB_IDS.length;
168 }
169
170 public Object getItem(int position) {
171 return position;
172 }
173
174 public long getItemId(int position) {
175 return position;
176 }
177
178 public View getView(int position, View convertView, ViewGroup parent) {
179 ImageView image;
180
181 if (convertView == null) {
182 image = (ImageView) mLayoutInflater.inflate(R.layout.wallpaper_item, parent, false);
183 } else {
184 image = (ImageView) convertView;
185 }
186
187 image.setImageResource(THUMB_IDS[position]);
188 image.getDrawable().setDither(true);
189 return image;
190 }
191 }
192
193 public void onClick(View v) {
194 selectWallpaper(mGallery.getSelectedItemPosition());
195 }
196}