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 | |
Joe Onorato | a590252 | 2009-07-30 13:37:37 -0700 | [diff] [blame] | 17 | package com.android.launcher2; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
| 19 | import android.app.Activity; |
Dianne Hackborn | 107f839 | 2009-08-05 21:32:16 -0700 | [diff] [blame] | 20 | import android.app.WallpaperManager; |
Joe Onorato | 1b12645 | 2009-07-28 18:26:47 -0700 | [diff] [blame] | 21 | import android.content.res.Resources; |
| 22 | import android.graphics.BitmapFactory; |
| 23 | import android.graphics.Bitmap; |
| 24 | import android.graphics.drawable.Drawable; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 25 | import android.os.Bundle; |
| 26 | import android.util.Log; |
| 27 | import android.view.LayoutInflater; |
| 28 | import android.view.View; |
| 29 | import android.view.ViewGroup; |
| 30 | import android.view.Window; |
| 31 | import android.view.View.OnClickListener; |
| 32 | import android.widget.AdapterView; |
| 33 | import android.widget.BaseAdapter; |
| 34 | import android.widget.Button; |
| 35 | import android.widget.Gallery; |
| 36 | import android.widget.ImageView; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 37 | |
| 38 | import java.io.IOException; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 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(); |
Joe Onorato | 1b12645 | 2009-07-28 18:26:47 -0700 | [diff] [blame] | 121 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 122 | final String[] extras = resources.getStringArray(R.array.extra_wallpapers); |
| 123 | final String packageName = getApplication().getPackageName(); |
| 124 | |
| 125 | for (String extra : extras) { |
| 126 | int res = resources.getIdentifier(extra, "drawable", packageName); |
| 127 | if (res != 0) { |
| 128 | final int thumbRes = resources.getIdentifier(extra + "_small", |
| 129 | "drawable", packageName); |
| 130 | |
| 131 | if (thumbRes != 0) { |
| 132 | mThumbs.add(thumbRes); |
| 133 | mImages.add(res); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | protected void onResume() { |
| 141 | super.onResume(); |
| 142 | mIsWallpaperSet = false; |
| 143 | } |
| 144 | |
| 145 | public void onItemSelected(AdapterView parent, View v, int position, long id) { |
| 146 | final ImageView view = mImageView; |
| 147 | Bitmap b = BitmapFactory.decodeResource(getResources(), mImages.get(position), mOptions); |
| 148 | view.setImageBitmap(b); |
| 149 | |
| 150 | // Help the GC |
| 151 | if (mBitmap != null) { |
| 152 | mBitmap.recycle(); |
| 153 | } |
| 154 | mBitmap = b; |
| 155 | |
| 156 | final Drawable drawable = view.getDrawable(); |
| 157 | drawable.setFilterBitmap(true); |
| 158 | drawable.setDither(true); |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * When using touch if you tap an image it triggers both the onItemClick and |
| 163 | * the onTouchEvent causing the wallpaper to be set twice. Ensure we only |
| 164 | * set the wallpaper once. |
| 165 | */ |
| 166 | private void selectWallpaper(int position) { |
| 167 | if (mIsWallpaperSet) { |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | mIsWallpaperSet = true; |
| 172 | try { |
Dianne Hackborn | 107f839 | 2009-08-05 21:32:16 -0700 | [diff] [blame] | 173 | WallpaperManager wpm = (WallpaperManager)getSystemService( |
| 174 | WALLPAPER_SERVICE); |
Dianne Hackborn | 6427180 | 2009-08-08 20:54:24 -0700 | [diff] [blame^] | 175 | wpm.setResource(mImages.get(position)); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 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() { |
| 194 | return mThumbs.size(); |
| 195 | } |
| 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 | |
| 214 | image.setImageResource(mThumbs.get(position)); |
| 215 | image.getDrawable().setDither(true); |
| 216 | return image; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | public void onClick(View v) { |
| 221 | selectWallpaper(mGallery.getSelectedItemPosition()); |
| 222 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 223 | } |