blob: ec0f8a2ef37fdf7eecd66926dc8537fe12731ec2 [file] [log] [blame]
Adam Lesinski2a898a02010-12-09 21:04:15 -08001/*
2 * Copyright (C) 2010 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 */
16package com.android.launcher2;
17
18import com.android.launcher.R;
19
20import android.app.Activity;
21import android.app.AlertDialog;
22import android.app.Dialog;
23import android.app.DialogFragment;
24import android.app.WallpaperManager;
25import android.content.Context;
26import android.content.DialogInterface;
27import android.content.res.Resources;
28import android.graphics.Bitmap;
29import android.graphics.BitmapFactory;
30import android.graphics.drawable.Drawable;
31import android.os.AsyncTask;
32import android.os.Bundle;
33import android.util.Log;
34import android.view.LayoutInflater;
35import android.view.View;
36import android.view.View.OnClickListener;
37import android.view.ViewGroup;
38import android.widget.AdapterView;
39import android.widget.BaseAdapter;
Adam Cohena6612cd2010-12-21 17:34:25 -080040import android.widget.FrameLayout;
Adam Lesinski2a898a02010-12-09 21:04:15 -080041import android.widget.Gallery;
42import android.widget.GridView;
43import android.widget.ImageView;
44import android.widget.ListAdapter;
45import android.widget.SpinnerAdapter;
46
47import java.io.IOException;
48import java.util.ArrayList;
49
50public class WallpaperChooserDialogFragment extends DialogFragment implements
51 AdapterView.OnItemSelectedListener, AdapterView.OnItemClickListener {
52
53 private static final String TAG = "Launcher.WallpaperChooserDialogFragment";
54 private static final String EMBEDDED_KEY = "com.android.launcher2."
55 + "WallpaperChooserDialogFragment.EMBEDDED_KEY";
56
57 private boolean mEmbedded;
58 private ImageView mImageView = null;
59 private Bitmap mBitmap = null;
60
61 private ArrayList<Integer> mThumbs;
62 private ArrayList<Integer> mImages;
63 private WallpaperLoader mLoader;
64
65 public static WallpaperChooserDialogFragment newInstance() {
66 WallpaperChooserDialogFragment fragment = new WallpaperChooserDialogFragment();
67 fragment.setCancelable(true);
68 return fragment;
69 }
70
71 @Override
72 public void onCreate(Bundle savedInstanceState) {
73 super.onCreate(savedInstanceState);
74 if (savedInstanceState != null && savedInstanceState.containsKey(EMBEDDED_KEY)) {
75 mEmbedded = savedInstanceState.getBoolean(EMBEDDED_KEY);
76 } else {
77 mEmbedded = isInLayout();
78 }
79 }
80
81 @Override
82 public void onSaveInstanceState(Bundle outState) {
83 outState.putBoolean(EMBEDDED_KEY, mEmbedded);
84 }
85
86 @Override
87 public void onDestroy() {
88 super.onDestroy();
89
90 if (mLoader != null && mLoader.getStatus() != WallpaperLoader.Status.FINISHED) {
91 mLoader.cancel(true);
92 mLoader = null;
93 }
94 }
95
96 @Override
97 public void onDismiss(DialogInterface dialog) {
98 super.onDismiss(dialog);
99 /* On orientation changes, the dialog is effectively "dismissed" so this is called
100 * when the activity is no longer associated with this dying dialog fragment. We
101 * should just safely ignore this case by checking if getActivity() returns null
102 */
103 Activity activity = getActivity();
104 if (activity != null) {
105 activity.finish();
106 }
107 }
108
109 /* This will only be called when in XLarge mode, since this Fragment is invoked like
110 * a dialog in that mode
111 */
112 @Override
113 public Dialog onCreateDialog(Bundle savedInstanceState) {
114 final View v = getActivity().getLayoutInflater().inflate(
115 R.layout.wallpaper_chooser, null, false);
116
117 GridView gridView = (GridView) v.findViewById(R.id.gallery);
118 gridView.setOnItemClickListener(this);
119 gridView.setAdapter(new ImageAdapter(getActivity()));
120
121 final int viewInset =
122 getResources().getDimensionPixelSize(R.dimen.alert_dialog_content_inset);
123
Adam Cohena6612cd2010-12-21 17:34:25 -0800124 FrameLayout wallPaperList = (FrameLayout) v.findViewById(R.id.wallpaper_list);
Adam Lesinski2a898a02010-12-09 21:04:15 -0800125 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
126 builder.setNegativeButton(R.string.wallpaper_cancel, null);
127 builder.setTitle(R.string.wallpaper_dialog_title);
Adam Cohena6612cd2010-12-21 17:34:25 -0800128 builder.setView(wallPaperList, viewInset, viewInset, viewInset, viewInset);
Adam Lesinski2a898a02010-12-09 21:04:15 -0800129 return builder.create();
130 }
131
132 @Override
133 public View onCreateView(LayoutInflater inflater, ViewGroup container,
134 Bundle savedInstanceState) {
135 findWallpapers();
136
137 /* If this fragment is embedded in the layout of this activity, then we should
138 * generate a view to display. Otherwise, a dialog will be created in
139 * onCreateDialog()
140 */
141 if (mEmbedded) {
142 View view = inflater.inflate(R.layout.wallpaper_chooser, container, false);
143
144 final Gallery gallery = (Gallery) view.findViewById(R.id.gallery);
145 gallery.setCallbackDuringFling(false);
146 gallery.setOnItemSelectedListener(this);
147 gallery.setAdapter(new ImageAdapter(getActivity()));
148
149 View setButton = view.findViewById(R.id.set);
150 setButton.setOnClickListener(new OnClickListener() {
151 @Override
152 public void onClick(View v) {
153 selectWallpaper(gallery.getSelectedItemPosition());
154 }
155 });
156 mImageView = (ImageView) view.findViewById(R.id.wallpaper);
157 return view;
158 }
159 return null;
160 }
161
162 private void selectWallpaper(int position) {
163 try {
164 WallpaperManager wpm = (WallpaperManager) getActivity().getSystemService(
165 Context.WALLPAPER_SERVICE);
166 wpm.setResource(mImages.get(position));
167 Activity activity = getActivity();
168 activity.setResult(Activity.RESULT_OK);
169 activity.finish();
170 } catch (IOException e) {
171 Log.e(TAG, "Failed to set wallpaper: " + e);
172 }
173 }
174
175 // Click handler for the Dialog's GridView
176 @Override
177 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
178 selectWallpaper(position);
179 }
180
181 // Selection handler for the embedded Gallery view
182 @Override
183 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
184 if (mLoader != null && mLoader.getStatus() != WallpaperLoader.Status.FINISHED) {
185 mLoader.cancel();
186 }
187 mLoader = (WallpaperLoader) new WallpaperLoader().execute(position);
188 }
189
190 @Override
191 public void onNothingSelected(AdapterView<?> parent) {
192 }
193
194 private void findWallpapers() {
195 mThumbs = new ArrayList<Integer>(24);
196 mImages = new ArrayList<Integer>(24);
197
198 final Resources resources = getResources();
199 // Context.getPackageName() may return the "original" package name,
200 // com.android.launcher2; Resources needs the real package name,
201 // com.android.launcher. So we ask Resources for what it thinks the
202 // package name should be.
203 final String packageName = resources.getResourcePackageName(R.array.wallpapers);
204
205 addWallpapers(resources, packageName, R.array.wallpapers);
206 addWallpapers(resources, packageName, R.array.extra_wallpapers);
207 }
208
209 private void addWallpapers(Resources resources, String packageName, int list) {
210 final String[] extras = resources.getStringArray(list);
211 for (String extra : extras) {
212 int res = resources.getIdentifier(extra, "drawable", packageName);
213 if (res != 0) {
214 final int thumbRes = resources.getIdentifier(extra + "_small",
215 "drawable", packageName);
216
217 if (thumbRes != 0) {
218 mThumbs.add(thumbRes);
219 mImages.add(res);
220 // Log.d(TAG, "add: [" + packageName + "]: " + extra + " (" + res + ")");
221 }
222 }
223 }
224 }
225
226 private class ImageAdapter extends BaseAdapter implements ListAdapter, SpinnerAdapter {
227 private LayoutInflater mLayoutInflater;
228
229 ImageAdapter(Activity activity) {
230 mLayoutInflater = activity.getLayoutInflater();
231 }
232
233 public int getCount() {
234 return mThumbs.size();
235 }
236
237 public Object getItem(int position) {
238 return position;
239 }
240
241 public long getItemId(int position) {
242 return position;
243 }
244
245 public View getView(int position, View convertView, ViewGroup parent) {
Adam Cohena6612cd2010-12-21 17:34:25 -0800246 View view;
Adam Lesinski2a898a02010-12-09 21:04:15 -0800247
248 if (convertView == null) {
Adam Cohena6612cd2010-12-21 17:34:25 -0800249 view = mLayoutInflater.inflate(R.layout.wallpaper_item, parent, false);
Adam Lesinski2a898a02010-12-09 21:04:15 -0800250 } else {
Adam Cohena6612cd2010-12-21 17:34:25 -0800251 view = convertView;
Adam Lesinski2a898a02010-12-09 21:04:15 -0800252 }
253
Adam Cohena6612cd2010-12-21 17:34:25 -0800254 ImageView image = (ImageView) view.findViewById(R.id.wallpaper_image);
255
Adam Lesinski2a898a02010-12-09 21:04:15 -0800256 int thumbRes = mThumbs.get(position);
257 image.setImageResource(thumbRes);
258 Drawable thumbDrawable = image.getDrawable();
259 if (thumbDrawable != null) {
260 thumbDrawable.setDither(true);
261 } else {
262 Log.e(TAG, "Error decoding thumbnail resId=" + thumbRes + " for wallpaper #"
263 + position);
264 }
265
Adam Cohena6612cd2010-12-21 17:34:25 -0800266 return view;
Adam Lesinski2a898a02010-12-09 21:04:15 -0800267 }
268 }
269
270 class WallpaperLoader extends AsyncTask<Integer, Void, Bitmap> {
271 BitmapFactory.Options mOptions;
272
273 WallpaperLoader() {
274 mOptions = new BitmapFactory.Options();
275 mOptions.inDither = false;
276 mOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
277 }
278
279 @Override
280 protected Bitmap doInBackground(Integer... params) {
281 if (isCancelled()) return null;
282 try {
283 return BitmapFactory.decodeResource(getResources(),
284 mImages.get(params[0]), mOptions);
285 } catch (OutOfMemoryError e) {
286 return null;
287 }
288 }
289
290 @Override
291 protected void onPostExecute(Bitmap b) {
292 if (b == null) return;
293
294 if (!isCancelled() && !mOptions.mCancel) {
295 // Help the GC
296 if (mBitmap != null) {
297 mBitmap.recycle();
298 }
299
300 // This should always be the case, but check anyways
301 final ImageView view = mImageView;
302 if (view != null) {
303 view.setImageBitmap(b);
304
305 mBitmap = b;
306
307 final Drawable drawable = view.getDrawable();
308 drawable.setFilterBitmap(true);
309 drawable.setDither(true);
310
311 view.postInvalidate();
312 }
313
314 mLoader = null;
315 } else {
316 b.recycle();
317 }
318 }
319
320 void cancel() {
321 mOptions.requestCancelDecode();
322 super.cancel(true);
323 }
324 }
325}