2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.launcher;
19 import android.app.Activity;
20 import android.os.Bundle;
21 import android.util.Log;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.view.Window;
26 import android.view.View.OnClickListener;
27 import android.widget.AdapterView;
28 import android.widget.BaseAdapter;
29 import android.widget.Button;
30 import android.widget.Gallery;
31 import android.widget.ImageView;
32 import android.graphics.BitmapFactory;
33 import android.graphics.Bitmap;
34 import android.graphics.drawable.Drawable;
35 import android.content.res.Resources;
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.util.ArrayList;
40 import java.util.Collections;
42 public class WallpaperChooser extends Activity implements AdapterView.OnItemSelectedListener,
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_road_small,
54 R.drawable.wallpaper_jellyfish_small,
55 R.drawable.wallpaper_zanzibar_small,
56 R.drawable.wallpaper_blue_small,
57 R.drawable.wallpaper_grey_small,
58 R.drawable.wallpaper_green_small,
59 R.drawable.wallpaper_pink_small,
62 private static final Integer[] IMAGE_IDS = {
63 com.android.internal.R.drawable.default_wallpaper,
64 R.drawable.wallpaper_sunset,
65 R.drawable.wallpaper_beach,
66 R.drawable.wallpaper_snow_leopard,
67 R.drawable.wallpaper_path,
68 R.drawable.wallpaper_sunrise,
69 R.drawable.wallpaper_mountain,
70 R.drawable.wallpaper_road,
71 R.drawable.wallpaper_jellyfish,
72 R.drawable.wallpaper_zanzibar,
73 R.drawable.wallpaper_blue,
74 R.drawable.wallpaper_grey,
75 R.drawable.wallpaper_green,
76 R.drawable.wallpaper_pink,
79 private Gallery mGallery;
80 private ImageView mImageView;
81 private boolean mIsWallpaperSet;
83 private BitmapFactory.Options mOptions;
84 private Bitmap mBitmap;
86 private ArrayList<Integer> mThumbs;
87 private ArrayList<Integer> mImages;
90 public void onCreate(Bundle icicle) {
91 super.onCreate(icicle);
92 requestWindowFeature(Window.FEATURE_NO_TITLE);
96 setContentView(R.layout.wallpaper_chooser);
98 mOptions = new BitmapFactory.Options();
99 mOptions.inDither = false;
100 mOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
102 mGallery = (Gallery) findViewById(R.id.gallery);
103 mGallery.setAdapter(new ImageAdapter(this));
104 mGallery.setOnItemSelectedListener(this);
105 mGallery.setCallbackDuringFling(false);
107 Button b = (Button) findViewById(R.id.set);
108 b.setOnClickListener(this);
110 mImageView = (ImageView) findViewById(R.id.wallpaper);
113 private void findWallpapers() {
114 mThumbs = new ArrayList<Integer>(THUMB_IDS.length + 4);
115 Collections.addAll(mThumbs, THUMB_IDS);
117 mImages = new ArrayList<Integer>(IMAGE_IDS.length + 4);
118 Collections.addAll(mImages, IMAGE_IDS);
120 final Resources resources = getResources();
121 final String[] extras = resources.getStringArray(R.array.extra_wallpapers);
122 final String packageName = getApplication().getPackageName();
124 for (String extra : extras) {
125 int res = resources.getIdentifier(extra, "drawable", packageName);
127 final int thumbRes = resources.getIdentifier(extra + "_small",
128 "drawable", packageName);
131 mThumbs.add(thumbRes);
139 protected void onResume() {
141 mIsWallpaperSet = false;
144 public void onItemSelected(AdapterView parent, View v, int position, long id) {
145 final ImageView view = mImageView;
146 Bitmap b = BitmapFactory.decodeResource(getResources(), mImages.get(position), mOptions);
147 view.setImageBitmap(b);
150 if (mBitmap != null) {
155 final Drawable drawable = view.getDrawable();
156 drawable.setFilterBitmap(true);
157 drawable.setDither(true);
161 * When using touch if you tap an image it triggers both the onItemClick and
162 * the onTouchEvent causing the wallpaper to be set twice. Ensure we only
163 * set the wallpaper once.
165 private void selectWallpaper(int position) {
166 if (mIsWallpaperSet) {
170 mIsWallpaperSet = true;
172 InputStream stream = getResources().openRawResource(mImages.get(position));
173 setWallpaper(stream);
174 setResult(RESULT_OK);
176 } catch (IOException e) {
177 Log.e(Launcher.LOG_TAG, "Failed to set wallpaper: " + e);
181 public void onNothingSelected(AdapterView parent) {
184 private class ImageAdapter extends BaseAdapter {
185 private LayoutInflater mLayoutInflater;
187 ImageAdapter(WallpaperChooser context) {
188 mLayoutInflater = context.getLayoutInflater();
191 public int getCount() {
192 return mThumbs.size();
195 public Object getItem(int position) {
199 public long getItemId(int position) {
203 public View getView(int position, View convertView, ViewGroup parent) {
206 if (convertView == null) {
207 image = (ImageView) mLayoutInflater.inflate(R.layout.wallpaper_item, parent, false);
209 image = (ImageView) convertView;
212 image.setImageResource(mThumbs.get(position));
213 image.getDrawable().setDither(true);
218 public void onClick(View v) {
219 selectWallpaper(mGallery.getSelectedItemPosition());