The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher; |
| 18 | |
| 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; |
| 36 | |
| 37 | import java.io.IOException; |
| 38 | import java.io.InputStream; |
| 39 | import java.util.ArrayList; |
| 40 | import java.util.Collections; |
| 41 | |
| 42 | public 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_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, |
| 60 | }; |
| 61 | |
| 62 | private static final Integer[] IMAGE_IDS = { |
Ramanan Rajeswaran | c4c1f32 | 2009-05-08 13:15:08 -0700 | [diff] [blame] | 63 | R.drawable.wallpaper_lake, |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 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, |
| 77 | }; |
| 78 | |
| 79 | private Gallery mGallery; |
| 80 | private ImageView mImageView; |
| 81 | private boolean mIsWallpaperSet; |
| 82 | |
| 83 | private BitmapFactory.Options mOptions; |
| 84 | private Bitmap mBitmap; |
| 85 | |
| 86 | private ArrayList<Integer> mThumbs; |
| 87 | private ArrayList<Integer> mImages; |
| 88 | |
| 89 | @Override |
| 90 | public void onCreate(Bundle icicle) { |
| 91 | super.onCreate(icicle); |
| 92 | requestWindowFeature(Window.FEATURE_NO_TITLE); |
| 93 | |
| 94 | findWallpapers(); |
| 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 | mGallery.setCallbackDuringFling(false); |
| 106 | |
| 107 | Button b = (Button) findViewById(R.id.set); |
| 108 | b.setOnClickListener(this); |
| 109 | |
| 110 | mImageView = (ImageView) findViewById(R.id.wallpaper); |
| 111 | } |
| 112 | |
| 113 | private void findWallpapers() { |
| 114 | mThumbs = new ArrayList<Integer>(THUMB_IDS.length + 4); |
| 115 | Collections.addAll(mThumbs, THUMB_IDS); |
| 116 | |
| 117 | mImages = new ArrayList<Integer>(IMAGE_IDS.length + 4); |
| 118 | Collections.addAll(mImages, IMAGE_IDS); |
| 119 | |
| 120 | final Resources resources = getResources(); |
| 121 | final String[] extras = resources.getStringArray(R.array.extra_wallpapers); |
| 122 | final String packageName = getApplication().getPackageName(); |
| 123 | |
| 124 | for (String extra : extras) { |
| 125 | int res = resources.getIdentifier(extra, "drawable", packageName); |
| 126 | if (res != 0) { |
| 127 | final int thumbRes = resources.getIdentifier(extra + "_small", |
| 128 | "drawable", packageName); |
| 129 | |
| 130 | if (thumbRes != 0) { |
| 131 | mThumbs.add(thumbRes); |
| 132 | mImages.add(res); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | protected void onResume() { |
| 140 | super.onResume(); |
| 141 | mIsWallpaperSet = false; |
| 142 | } |
| 143 | |
| 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); |
| 148 | |
| 149 | // Help the GC |
| 150 | if (mBitmap != null) { |
| 151 | mBitmap.recycle(); |
| 152 | } |
| 153 | mBitmap = b; |
| 154 | |
| 155 | final Drawable drawable = view.getDrawable(); |
| 156 | drawable.setFilterBitmap(true); |
| 157 | drawable.setDither(true); |
| 158 | } |
| 159 | |
| 160 | /* |
| 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. |
| 164 | */ |
| 165 | private void selectWallpaper(int position) { |
| 166 | if (mIsWallpaperSet) { |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | mIsWallpaperSet = true; |
| 171 | try { |
| 172 | InputStream stream = getResources().openRawResource(mImages.get(position)); |
| 173 | setWallpaper(stream); |
| 174 | setResult(RESULT_OK); |
| 175 | finish(); |
| 176 | } catch (IOException e) { |
| 177 | Log.e(Launcher.LOG_TAG, "Failed to set wallpaper: " + e); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | public void onNothingSelected(AdapterView parent) { |
| 182 | } |
| 183 | |
| 184 | private class ImageAdapter extends BaseAdapter { |
| 185 | private LayoutInflater mLayoutInflater; |
| 186 | |
| 187 | ImageAdapter(WallpaperChooser context) { |
| 188 | mLayoutInflater = context.getLayoutInflater(); |
| 189 | } |
| 190 | |
| 191 | public int getCount() { |
| 192 | return mThumbs.size(); |
| 193 | } |
| 194 | |
| 195 | public Object getItem(int position) { |
| 196 | return position; |
| 197 | } |
| 198 | |
| 199 | public long getItemId(int position) { |
| 200 | return position; |
| 201 | } |
| 202 | |
| 203 | public View getView(int position, View convertView, ViewGroup parent) { |
| 204 | ImageView image; |
| 205 | |
| 206 | if (convertView == null) { |
| 207 | image = (ImageView) mLayoutInflater.inflate(R.layout.wallpaper_item, parent, false); |
| 208 | } else { |
| 209 | image = (ImageView) convertView; |
| 210 | } |
| 211 | |
| 212 | image.setImageResource(mThumbs.get(position)); |
| 213 | image.getDrawable().setDither(true); |
| 214 | return image; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | public void onClick(View v) { |
| 219 | selectWallpaper(mGallery.getSelectedItemPosition()); |
| 220 | } |
| 221 | } |