blob: 8919ecef309e81c1cda2f6b3981ce4ccdf141d97 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
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 Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
19import android.app.Activity;
Dianne Hackborn107f8392009-08-05 21:32:16 -070020import android.app.WallpaperManager;
Joe Onorato1b126452009-07-28 18:26:47 -070021import android.content.res.Resources;
22import android.graphics.BitmapFactory;
23import android.graphics.Bitmap;
24import android.graphics.drawable.Drawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.os.Bundle;
26import android.util.Log;
27import android.view.LayoutInflater;
28import android.view.View;
29import android.view.ViewGroup;
30import android.view.Window;
31import android.view.View.OnClickListener;
32import android.widget.AdapterView;
33import android.widget.BaseAdapter;
34import android.widget.Button;
35import android.widget.Gallery;
36import android.widget.ImageView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037
38import java.io.IOException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import java.util.ArrayList;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040
41public class WallpaperChooser extends Activity implements AdapterView.OnItemSelectedListener,
42 OnClickListener {
43
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044 private Gallery mGallery;
45 private ImageView mImageView;
46 private boolean mIsWallpaperSet;
47
48 private BitmapFactory.Options mOptions;
49 private Bitmap mBitmap;
50
51 private ArrayList<Integer> mThumbs;
52 private ArrayList<Integer> mImages;
53
54 @Override
55 public void onCreate(Bundle icicle) {
56 super.onCreate(icicle);
57 requestWindowFeature(Window.FEATURE_NO_TITLE);
58
59 findWallpapers();
60
61 setContentView(R.layout.wallpaper_chooser);
62
63 mOptions = new BitmapFactory.Options();
64 mOptions.inDither = false;
65 mOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
66
67 mGallery = (Gallery) findViewById(R.id.gallery);
68 mGallery.setAdapter(new ImageAdapter(this));
69 mGallery.setOnItemSelectedListener(this);
70 mGallery.setCallbackDuringFling(false);
71
72 Button b = (Button) findViewById(R.id.set);
73 b.setOnClickListener(this);
74
75 mImageView = (ImageView) findViewById(R.id.wallpaper);
76 }
77
78 private void findWallpapers() {
Romain Guy8eb50952009-09-02 15:57:21 -070079 mThumbs = new ArrayList<Integer>(24);
80 mImages = new ArrayList<Integer>(24);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081
82 final Resources resources = getResources();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083 final String packageName = getApplication().getPackageName();
84
Romain Guy8eb50952009-09-02 15:57:21 -070085 addWallpapers(resources, packageName, R.array.wallpapers);
86 addWallpapers(resources, packageName, R.array.extra_wallpapers);
87 }
88
89 private void addWallpapers(Resources resources, String packageName, int list) {
90 final String[] extras = resources.getStringArray(list);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091 for (String extra : extras) {
92 int res = resources.getIdentifier(extra, "drawable", packageName);
93 if (res != 0) {
94 final int thumbRes = resources.getIdentifier(extra + "_small",
95 "drawable", packageName);
96
97 if (thumbRes != 0) {
98 mThumbs.add(thumbRes);
99 mImages.add(res);
100 }
101 }
102 }
103 }
104
105 @Override
106 protected void onResume() {
107 super.onResume();
108 mIsWallpaperSet = false;
109 }
110
111 public void onItemSelected(AdapterView parent, View v, int position, long id) {
112 final ImageView view = mImageView;
113 Bitmap b = BitmapFactory.decodeResource(getResources(), mImages.get(position), mOptions);
114 view.setImageBitmap(b);
115
116 // Help the GC
117 if (mBitmap != null) {
118 mBitmap.recycle();
119 }
120 mBitmap = b;
121
122 final Drawable drawable = view.getDrawable();
123 drawable.setFilterBitmap(true);
124 drawable.setDither(true);
125 }
126
127 /*
128 * When using touch if you tap an image it triggers both the onItemClick and
129 * the onTouchEvent causing the wallpaper to be set twice. Ensure we only
130 * set the wallpaper once.
131 */
132 private void selectWallpaper(int position) {
133 if (mIsWallpaperSet) {
134 return;
135 }
136
137 mIsWallpaperSet = true;
138 try {
Romain Guy8eb50952009-09-02 15:57:21 -0700139 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
Dianne Hackborn64271802009-08-08 20:54:24 -0700140 wpm.setResource(mImages.get(position));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141 setResult(RESULT_OK);
142 finish();
143 } catch (IOException e) {
144 Log.e(Launcher.LOG_TAG, "Failed to set wallpaper: " + e);
145 }
146 }
147
148 public void onNothingSelected(AdapterView parent) {
149 }
150
151 private class ImageAdapter extends BaseAdapter {
152 private LayoutInflater mLayoutInflater;
153
154 ImageAdapter(WallpaperChooser context) {
155 mLayoutInflater = context.getLayoutInflater();
156 }
157
158 public int getCount() {
159 return mThumbs.size();
160 }
161
162 public Object getItem(int position) {
163 return position;
164 }
165
166 public long getItemId(int position) {
167 return position;
168 }
169
170 public View getView(int position, View convertView, ViewGroup parent) {
171 ImageView image;
172
173 if (convertView == null) {
174 image = (ImageView) mLayoutInflater.inflate(R.layout.wallpaper_item, parent, false);
175 } else {
176 image = (ImageView) convertView;
177 }
178
179 image.setImageResource(mThumbs.get(position));
180 image.getDrawable().setDither(true);
181 return image;
182 }
183 }
184
185 public void onClick(View v) {
186 selectWallpaper(mGallery.getSelectedItemPosition());
187 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188}