blob: baac33955c7a0d819ef5787812b058a387d62f4a [file] [log] [blame]
Michael Jurka104c4562013-07-08 18:03:46 -07001/*
2 * Copyright (C) 2013 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
Jose Pascoal05218202014-11-19 11:17:38 +000017package com.fairphone.fplauncher3;
Michael Jurka104c4562013-07-08 18:03:46 -070018
Michael Jurkabe2f8dd2013-09-10 13:39:59 +020019import android.animation.LayoutTransition;
Sunny Goyalcdc4dbd2014-07-16 16:04:08 -070020import android.annotation.TargetApi;
Michael Jurka104c4562013-07-08 18:03:46 -070021import android.app.ActionBar;
22import android.app.Activity;
Michael Jurka998e4ff2013-09-18 16:04:36 +020023import android.app.WallpaperInfo;
24import android.app.WallpaperManager;
Michael Jurka1e4e6dd2013-09-23 23:02:05 +010025import android.content.Context;
Michael Jurka104c4562013-07-08 18:03:46 -070026import android.content.Intent;
Filipe Gonçalves312cb312016-05-04 15:33:43 +010027import android.content.SharedPreferences;
Michael Jurkaeadbfc52013-09-04 00:45:37 +020028import android.content.pm.ApplicationInfo;
Michael Jurka104c4562013-07-08 18:03:46 -070029import android.content.pm.PackageManager;
Michael Jurka104c4562013-07-08 18:03:46 -070030import android.content.res.Resources;
Michael Jurka42a71742013-09-18 00:45:45 +020031import android.database.Cursor;
Michael Jurka998e4ff2013-09-18 16:04:36 +020032import android.database.DataSetObserver;
Michael Jurkabe2f8dd2013-09-10 13:39:59 +020033import android.graphics.Bitmap;
Michael Jurkadb84e822013-09-23 23:02:05 +010034import android.graphics.BitmapFactory;
Michael Jurka12ca6132013-10-15 11:14:44 -070035import android.graphics.Canvas;
Michael Jurka8713a942013-10-10 21:28:40 -070036import android.graphics.Matrix;
Michael Jurka104c4562013-07-08 18:03:46 -070037import android.graphics.Point;
Michael Jurka998e4ff2013-09-18 16:04:36 +020038import android.graphics.PorterDuff;
Michael Jurka104c4562013-07-08 18:03:46 -070039import android.graphics.Rect;
40import android.graphics.RectF;
Michael Jurkadb84e822013-09-23 23:02:05 +010041import android.graphics.drawable.BitmapDrawable;
Michael Jurka104c4562013-07-08 18:03:46 -070042import android.graphics.drawable.Drawable;
43import android.graphics.drawable.LevelListDrawable;
44import android.net.Uri;
Michael Jurka862f7e32013-10-16 18:23:56 -070045import android.os.AsyncTask;
Michael Jurka12ca6132013-10-15 11:14:44 -070046import android.os.Build;
Michael Jurkad6dc6042013-09-10 13:39:59 +020047import android.os.Bundle;
Michael Jurka42a71742013-09-18 00:45:45 +020048import android.provider.MediaStore;
Michael Jurka104c4562013-07-08 18:03:46 -070049import android.util.Log;
Michael Jurkaeadbfc52013-09-04 00:45:37 +020050import android.util.Pair;
Michael Jurkad6dc6042013-09-10 13:39:59 +020051import android.view.ActionMode;
Michael Jurka104c4562013-07-08 18:03:46 -070052import android.view.LayoutInflater;
53import android.view.Menu;
Michael Jurkad6dc6042013-09-10 13:39:59 +020054import android.view.MenuInflater;
Michael Jurka104c4562013-07-08 18:03:46 -070055import android.view.MenuItem;
Michael Jurka104c4562013-07-08 18:03:46 -070056import android.view.View;
57import android.view.View.OnClickListener;
Michael Jurka12ca6132013-10-15 11:14:44 -070058import android.view.View.OnLayoutChangeListener;
Michael Jurka104c4562013-07-08 18:03:46 -070059import android.view.ViewGroup;
Michael Jurka7ad868b2013-12-12 15:04:25 +010060import android.view.ViewPropertyAnimator;
Michael Jurkafe0ace32013-10-03 01:05:14 -070061import android.view.ViewTreeObserver;
62import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Selim Cinekffed6582014-02-28 17:18:59 +010063import android.view.WindowManager;
Daniel Sandler24f397d2013-10-12 00:02:45 -040064import android.view.animation.AccelerateInterpolator;
Michael Jurka4b940602013-09-05 12:14:12 +020065import android.view.animation.DecelerateInterpolator;
Sunny Goyalcdc4dbd2014-07-16 16:04:08 -070066import android.widget.ArrayAdapter;
Michael Jurka104c4562013-07-08 18:03:46 -070067import android.widget.BaseAdapter;
68import android.widget.FrameLayout;
Michael Jurkafe0ace32013-10-03 01:05:14 -070069import android.widget.HorizontalScrollView;
Michael Jurka104c4562013-07-08 18:03:46 -070070import android.widget.ImageView;
71import android.widget.LinearLayout;
Filipe Gonçalves312cb312016-05-04 15:33:43 +010072import android.widget.SeekBar;
Michael Jurka619a1802013-10-29 15:50:06 +010073import android.widget.Toast;
Michael Jurka104c4562013-07-08 18:03:46 -070074import com.android.photos.BitmapRegionTileSource;
Michael Jurka619a1802013-10-29 15:50:06 +010075import com.android.photos.BitmapRegionTileSource.BitmapSource;
Filipe Gonçalves312cb312016-05-04 15:33:43 +010076import com.fairphone.fplauncher3.R;
Michael Jurkadb84e822013-09-23 23:02:05 +010077import java.io.File;
78import java.io.FileOutputStream;
Michael Jurka1e4e6dd2013-09-23 23:02:05 +010079import java.io.IOException;
Michael Jurka104c4562013-07-08 18:03:46 -070080import java.util.ArrayList;
Michael Jurka104c4562013-07-08 18:03:46 -070081
82public class WallpaperPickerActivity extends WallpaperCropActivity {
Michael Jurkabe2f8dd2013-09-10 13:39:59 +020083 static final String TAG = "Launcher.WallpaperPickerActivity";
Michael Jurka104c4562013-07-08 18:03:46 -070084
Michael Jurka1e4e6dd2013-09-23 23:02:05 +010085 public static final int IMAGE_PICK = 5;
86 public static final int PICK_WALLPAPER_THIRD_PARTY_ACTIVITY = 6;
87 public static final int PICK_LIVE_WALLPAPER = 7;
Michael Jurkad6dc6042013-09-10 13:39:59 +020088 private static final String TEMP_WALLPAPER_TILES = "TEMP_WALLPAPER_TILES";
Jorim Jaggi1b921ef2014-01-14 16:00:17 -080089 private static final String SELECTED_INDEX = "SELECTED_INDEX";
Michael Jurkaa7d82b92013-11-08 15:45:48 +010090 private static final String OLD_DEFAULT_WALLPAPER_THUMBNAIL_FILENAME = "default_thumb.jpg";
91 private static final String DEFAULT_WALLPAPER_THUMBNAIL_FILENAME = "default_thumb2.jpg";
Selim Cinekffed6582014-02-28 17:18:59 +010092 private static final int FLAG_POST_DELAY_MILLIS = 200;
Michael Jurka104c4562013-07-08 18:03:46 -070093
Michael Jurka619a1802013-10-29 15:50:06 +010094 private View mSelectedTile;
Michael Jurka4b940602013-09-05 12:14:12 +020095 private boolean mIgnoreNextTap;
Michael Jurka2d8de582013-09-04 14:42:52 +020096 private OnClickListener mThumbnailOnClickListener;
Michael Jurka104c4562013-07-08 18:03:46 -070097
Michael Jurkabe2f8dd2013-09-10 13:39:59 +020098 private LinearLayout mWallpapersView;
Michael Jurka7267fa52013-09-26 15:29:57 -070099 private View mWallpaperStrip;
Michael Jurkad6dc6042013-09-10 13:39:59 +0200100
101 private ActionMode.Callback mActionModeCallback;
102 private ActionMode mActionMode;
103
Michael Jurkabe2f8dd2013-09-10 13:39:59 +0200104 private View.OnLongClickListener mLongClickListener;
105
106 ArrayList<Uri> mTempWallpaperTiles = new ArrayList<Uri>();
107 private SavedWallpaperImages mSavedImages;
Michael Jurka998e4ff2013-09-18 16:04:36 +0200108 private WallpaperInfo mLiveWallpaperInfoOnPickerLaunch;
Selim Cinekffed6582014-02-28 17:18:59 +0100109 private int mSelectedIndex = -1;
110 private WallpaperInfo mLastClickedLiveWallpaperInfo;
Filipe Gonçalves312cb312016-05-04 15:33:43 +0100111 private View mDarknessStrip;
Michael Jurkabe2f8dd2013-09-10 13:39:59 +0200112
Michael Jurka1e4e6dd2013-09-23 23:02:05 +0100113 public static abstract class WallpaperTileInfo {
Michael Jurka6b783a02013-10-04 12:26:48 -0700114 protected View mView;
Sunny Goyalcdc4dbd2014-07-16 16:04:08 -0700115 public Drawable mThumb;
116
Michael Jurka6b783a02013-10-04 12:26:48 -0700117 public void setView(View v) {
118 mView = v;
119 }
Michael Jurka1e4e6dd2013-09-23 23:02:05 +0100120 public void onClick(WallpaperPickerActivity a) {}
121 public void onSave(WallpaperPickerActivity a) {}
Michael Jurka8a34bdb2013-09-25 07:57:33 -0700122 public void onDelete(WallpaperPickerActivity a) {}
Michael Jurka1e4e6dd2013-09-23 23:02:05 +0100123 public boolean isSelectable() { return false; }
Michael Jurka6b783a02013-10-04 12:26:48 -0700124 public boolean isNamelessWallpaper() { return false; }
125 public void onIndexUpdated(CharSequence label) {
126 if (isNamelessWallpaper()) {
127 mView.setContentDescription(label);
128 }
129 }
Michael Jurka1e4e6dd2013-09-23 23:02:05 +0100130 }
131
132 public static class PickImageInfo extends WallpaperTileInfo {
Michael Jurka8a34bdb2013-09-25 07:57:33 -0700133 @Override
Michael Jurka1e4e6dd2013-09-23 23:02:05 +0100134 public void onClick(WallpaperPickerActivity a) {
135 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
136 intent.setType("image/*");
Michael Jurka7ad868b2013-12-12 15:04:25 +0100137 a.startActivityForResultSafely(intent, IMAGE_PICK);
Michael Jurka1e4e6dd2013-09-23 23:02:05 +0100138 }
139 }
140
141 public static class UriWallpaperInfo extends WallpaperTileInfo {
Jose Pascoalf354dfe2015-02-10 19:21:21 +0000142 private final Uri mUri;
Michael Jurka619a1802013-10-29 15:50:06 +0100143 private boolean mFirstClick = true;
144 private BitmapRegionTileSource.UriBitmapSource mBitmapSource;
Michael Jurka1e4e6dd2013-09-23 23:02:05 +0100145 public UriWallpaperInfo(Uri uri) {
146 mUri = uri;
147 }
Michael Jurka8a34bdb2013-09-25 07:57:33 -0700148 @Override
Michael Jurka619a1802013-10-29 15:50:06 +0100149 public void onClick(final WallpaperPickerActivity a) {
150 final Runnable onLoad;
151 if (!mFirstClick) {
152 onLoad = null;
153 } else {
154 mFirstClick = false;
Selim Cinekffed6582014-02-28 17:18:59 +0100155 a.mSetWallpaperButton.setEnabled(false);
Michael Jurka619a1802013-10-29 15:50:06 +0100156 onLoad = new Runnable() {
157 public void run() {
158 if (mBitmapSource != null &&
159 mBitmapSource.getLoadingState() == BitmapSource.State.LOADED) {
Michael Jurka619a1802013-10-29 15:50:06 +0100160 a.selectTile(mView);
Selim Cinekffed6582014-02-28 17:18:59 +0100161 a.mSetWallpaperButton.setEnabled(true);
Michael Jurka619a1802013-10-29 15:50:06 +0100162 } else {
163 ViewGroup parent = (ViewGroup) mView.getParent();
164 if (parent != null) {
165 parent.removeView(mView);
166 Toast.makeText(a,
167 a.getString(R.string.image_load_fail),
168 Toast.LENGTH_SHORT).show();
169 }
170 }
171 }
172 };
173 }
174 mBitmapSource = new BitmapRegionTileSource.UriBitmapSource(
175 a, mUri, BitmapRegionTileSource.MAX_PREVIEW_SIZE);
176 a.setCropViewTileSource(mBitmapSource, true, false, onLoad);
Michael Jurka1e4e6dd2013-09-23 23:02:05 +0100177 }
Michael Jurka8a34bdb2013-09-25 07:57:33 -0700178 @Override
Michael Jurka1e4e6dd2013-09-23 23:02:05 +0100179 public void onSave(final WallpaperPickerActivity a) {
180 boolean finishActivityWhenDone = true;
181 OnBitmapCroppedHandler h = new OnBitmapCroppedHandler() {
182 public void onBitmapCropped(byte[] imageBytes) {
183 Point thumbSize = getDefaultThumbnailSize(a.getResources());
Michael Jurka8713a942013-10-10 21:28:40 -0700184 // rotation is set to 0 since imageBytes has already been correctly rotated
185 Bitmap thumb = createThumbnail(
186 thumbSize, null, null, imageBytes, null, 0, 0, true);
Michael Jurka1e4e6dd2013-09-23 23:02:05 +0100187 a.getSavedImages().writeImage(thumb, imageBytes);
188 }
189 };
190 a.cropImageAndSetWallpaper(mUri, h, finishActivityWhenDone);
191 }
Michael Jurka8a34bdb2013-09-25 07:57:33 -0700192 @Override
Michael Jurka1e4e6dd2013-09-23 23:02:05 +0100193 public boolean isSelectable() {
194 return true;
195 }
Michael Jurka6b783a02013-10-04 12:26:48 -0700196 @Override
197 public boolean isNamelessWallpaper() {
198 return true;
199 }
Michael Jurka1e4e6dd2013-09-23 23:02:05 +0100200 }
201
Sunny Goyalcdc4dbd2014-07-16 16:04:08 -0700202 public static class FileWallpaperInfo extends WallpaperTileInfo {
Jose Pascoalf354dfe2015-02-10 19:21:21 +0000203 private final File mFile;
Sunny Goyalcdc4dbd2014-07-16 16:04:08 -0700204
205 public FileWallpaperInfo(File target, Drawable thumb) {
206 mFile = target;
207 mThumb = thumb;
208 }
209 @Override
210 public void onClick(WallpaperPickerActivity a) {
211 BitmapRegionTileSource.UriBitmapSource bitmapSource =
212 new BitmapRegionTileSource.UriBitmapSource(a, Uri.fromFile(mFile), 1024);
213 a.setCropViewTileSource(bitmapSource, false, true, null);
214 }
215 @Override
216 public void onSave(WallpaperPickerActivity a) {
217 a.setWallpaper(Uri.fromFile(mFile), true);
218 }
219 @Override
220 public boolean isSelectable() {
221 return true;
222 }
223 @Override
224 public boolean isNamelessWallpaper() {
225 return true;
226 }
227 }
228
Michael Jurka1e4e6dd2013-09-23 23:02:05 +0100229 public static class ResourceWallpaperInfo extends WallpaperTileInfo {
Jose Pascoalf354dfe2015-02-10 19:21:21 +0000230 private final Resources mResources;
231 private final int mResId;
Michael Jurka1e4e6dd2013-09-23 23:02:05 +0100232
233 public ResourceWallpaperInfo(Resources res, int resId, Drawable thumb) {
234 mResources = res;
235 mResId = resId;
236 mThumb = thumb;
237 }
Michael Jurka8a34bdb2013-09-25 07:57:33 -0700238 @Override
Michael Jurka1e4e6dd2013-09-23 23:02:05 +0100239 public void onClick(WallpaperPickerActivity a) {
Michael Jurka862f7e32013-10-16 18:23:56 -0700240 BitmapRegionTileSource.ResourceBitmapSource bitmapSource =
Michael Jurka83699e52013-10-23 20:59:51 +0200241 new BitmapRegionTileSource.ResourceBitmapSource(
242 mResources, mResId, BitmapRegionTileSource.MAX_PREVIEW_SIZE);
Michael Jurka862f7e32013-10-16 18:23:56 -0700243 bitmapSource.loadInBackground();
244 BitmapRegionTileSource source = new BitmapRegionTileSource(a, bitmapSource);
Michael Jurka1e4e6dd2013-09-23 23:02:05 +0100245 CropView v = a.getCropView();
246 v.setTileSource(source, null);
247 Point wallpaperSize = WallpaperCropActivity.getDefaultWallpaperSize(
248 a.getResources(), a.getWindowManager());
249 RectF crop = WallpaperCropActivity.getMaxCropRect(
250 source.getImageWidth(), source.getImageHeight(),
251 wallpaperSize.x, wallpaperSize.y, false);
252 v.setScale(wallpaperSize.x / crop.width());
253 v.setTouchEnabled(false);
Selim Cinekffed6582014-02-28 17:18:59 +0100254 a.setSystemWallpaperVisiblity(false);
Michael Jurka1e4e6dd2013-09-23 23:02:05 +0100255 }
Michael Jurka8a34bdb2013-09-25 07:57:33 -0700256 @Override
Michael Jurka1e4e6dd2013-09-23 23:02:05 +0100257 public void onSave(WallpaperPickerActivity a) {
258 boolean finishActivityWhenDone = true;
259 a.cropImageAndSetWallpaper(mResources, mResId, finishActivityWhenDone);
260 }
Michael Jurka8a34bdb2013-09-25 07:57:33 -0700261 @Override
Michael Jurka1e4e6dd2013-09-23 23:02:05 +0100262 public boolean isSelectable() {
263 return true;
264 }
Michael Jurka6b783a02013-10-04 12:26:48 -0700265 @Override
266 public boolean isNamelessWallpaper() {
267 return true;
268 }
Michael Jurka104c4562013-07-08 18:03:46 -0700269 }
270
Sunny Goyalcdc4dbd2014-07-16 16:04:08 -0700271 @TargetApi(Build.VERSION_CODES.KITKAT)
Michael Jurka12ca6132013-10-15 11:14:44 -0700272 public static class DefaultWallpaperInfo extends WallpaperTileInfo {
Michael Jurka12ca6132013-10-15 11:14:44 -0700273 public DefaultWallpaperInfo(Drawable thumb) {
274 mThumb = thumb;
275 }
276 @Override
277 public void onClick(WallpaperPickerActivity a) {
Michael Jurka83699e52013-10-23 20:59:51 +0200278 CropView c = a.getCropView();
279
Michael Jurka12ca6132013-10-15 11:14:44 -0700280 Drawable defaultWallpaper = WallpaperManager.getInstance(a).getBuiltInDrawable(
Michael Jurka83699e52013-10-23 20:59:51 +0200281 c.getWidth(), c.getHeight(), false, 0.5f, 0.5f);
282
Christoph Studer7084c602014-02-28 15:57:18 +0100283 if (defaultWallpaper == null) {
284 Log.w(TAG, "Null default wallpaper encountered.");
285 c.setTileSource(null, null);
286 return;
287 }
288
Michael Jurka83699e52013-10-23 20:59:51 +0200289 c.setTileSource(
290 new DrawableTileSource(a, defaultWallpaper, DrawableTileSource.MAX_PREVIEW_SIZE), null);
291 c.setScale(1f);
292 c.setTouchEnabled(false);
Selim Cinekffed6582014-02-28 17:18:59 +0100293 a.setSystemWallpaperVisiblity(false);
Michael Jurka12ca6132013-10-15 11:14:44 -0700294 }
295 @Override
296 public void onSave(WallpaperPickerActivity a) {
297 try {
298 WallpaperManager.getInstance(a).clear();
Selim Cinek3aa28312014-02-28 18:00:42 +0100299 a.setResult(RESULT_OK);
Michael Jurka12ca6132013-10-15 11:14:44 -0700300 } catch (IOException e) {
301 Log.w("Setting wallpaper to default threw exception", e);
302 }
303 a.finish();
304 }
305 @Override
306 public boolean isSelectable() {
307 return true;
308 }
309 @Override
310 public boolean isNamelessWallpaper() {
311 return true;
312 }
313 }
314
Michael Jurka7267fa52013-09-26 15:29:57 -0700315 public void setWallpaperStripYOffset(float offset) {
316 mWallpaperStrip.setPadding(0, 0, 0, (int) offset);
317 }
318
Selim Cinekffed6582014-02-28 17:18:59 +0100319 /**
320 * shows the system wallpaper behind the window and hides the {@link
321 * #mCropView} if visible
322 * @param visible should the system wallpaper be shown
323 */
324 protected void setSystemWallpaperVisiblity(final boolean visible) {
325 // hide our own wallpaper preview if necessary
326 if(!visible) {
327 mCropView.setVisibility(View.VISIBLE);
328 } else {
329 changeWallpaperFlags(visible);
330 }
331 // the change of the flag must be delayed in order to avoid flickering,
332 // a simple post / double post does not suffice here
333 mCropView.postDelayed(new Runnable() {
334 @Override
335 public void run() {
336 if(!visible) {
337 changeWallpaperFlags(visible);
338 } else {
339 mCropView.setVisibility(View.INVISIBLE);
340 }
341 }
342 }, FLAG_POST_DELAY_MILLIS);
343 }
344
345 private void changeWallpaperFlags(boolean visible) {
346 int desiredWallpaperFlag = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
347 int currentWallpaperFlag = getWindow().getAttributes().flags
348 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
349 if (desiredWallpaperFlag != currentWallpaperFlag) {
350 getWindow().setFlags(desiredWallpaperFlag,
351 WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
352 }
353 }
354
355 @Override
356 public void setCropViewTileSource(BitmapSource bitmapSource,
357 boolean touchEnabled,
358 boolean moveToLeft,
359 final Runnable postExecute) {
360 // we also want to show our own wallpaper instead of the one in the background
361 Runnable showPostExecuteRunnable = new Runnable() {
362 @Override
363 public void run() {
364 if(postExecute != null) {
365 postExecute.run();
366 }
367 setSystemWallpaperVisiblity(false);
368 }
369 };
370 super.setCropViewTileSource(bitmapSource,
371 touchEnabled,
372 moveToLeft,
373 showPostExecuteRunnable);
374 }
375
Michael Jurka2d8de582013-09-04 14:42:52 +0200376 // called by onCreate; this is subclassed to overwrite WallpaperCropActivity
377 protected void init() {
Michael Jurka104c4562013-07-08 18:03:46 -0700378 setContentView(R.layout.wallpaper_picker);
379
380 mCropView = (CropView) findViewById(R.id.cropView);
Selim Cinekffed6582014-02-28 17:18:59 +0100381 mCropView.setVisibility(View.INVISIBLE);
382
Filipe Gonçalves312cb312016-05-04 15:33:43 +0100383 mDarknessStrip = findViewById(R.id.darkness_strip);
Michael Jurka7267fa52013-09-26 15:29:57 -0700384 mWallpaperStrip = findViewById(R.id.wallpaper_strip);
Michael Jurka4b940602013-09-05 12:14:12 +0200385 mCropView.setTouchCallback(new CropView.TouchCallback() {
Michael Jurka7ad868b2013-12-12 15:04:25 +0100386 ViewPropertyAnimator mAnim;
Michael Jurkae8115d52013-09-20 03:52:24 +0200387 @Override
Michael Jurka4b940602013-09-05 12:14:12 +0200388 public void onTouchDown() {
389 if (mAnim != null) {
390 mAnim.cancel();
391 }
Daniel Sandler24f397d2013-10-12 00:02:45 -0400392 if (mWallpaperStrip.getAlpha() == 1f) {
Michael Jurka4b940602013-09-05 12:14:12 +0200393 mIgnoreNextTap = true;
394 }
Michael Jurka7ad868b2013-12-12 15:04:25 +0100395 mAnim = mWallpaperStrip.animate();
Daniel Sandler24f397d2013-10-12 00:02:45 -0400396 mAnim.alpha(0f)
Michael Jurka7ad868b2013-12-12 15:04:25 +0100397 .setDuration(150)
398 .withEndAction(new Runnable() {
399 public void run() {
Filipe Gonçalves312cb312016-05-04 15:33:43 +0100400 mDarknessStrip.setVisibility(View.INVISIBLE);
Michael Jurka7ad868b2013-12-12 15:04:25 +0100401 mWallpaperStrip.setVisibility(View.INVISIBLE);
402 }
403 });
Daniel Sandler24f397d2013-10-12 00:02:45 -0400404 mAnim.setInterpolator(new AccelerateInterpolator(0.75f));
Michael Jurka4b940602013-09-05 12:14:12 +0200405 mAnim.start();
406 }
Michael Jurkae8115d52013-09-20 03:52:24 +0200407 @Override
408 public void onTouchUp() {
409 mIgnoreNextTap = false;
410 }
411 @Override
Michael Jurka4b940602013-09-05 12:14:12 +0200412 public void onTap() {
413 boolean ignoreTap = mIgnoreNextTap;
414 mIgnoreNextTap = false;
415 if (!ignoreTap) {
416 if (mAnim != null) {
417 mAnim.cancel();
418 }
Filipe Gonçalves312cb312016-05-04 15:33:43 +0100419 mDarknessStrip.setVisibility(View.VISIBLE);
Daniel Sandler24f397d2013-10-12 00:02:45 -0400420 mWallpaperStrip.setVisibility(View.VISIBLE);
Michael Jurka7ad868b2013-12-12 15:04:25 +0100421 mAnim = mWallpaperStrip.animate();
Daniel Sandler24f397d2013-10-12 00:02:45 -0400422 mAnim.alpha(1f)
423 .setDuration(150)
424 .setInterpolator(new DecelerateInterpolator(0.75f));
Michael Jurka4b940602013-09-05 12:14:12 +0200425 mAnim.start();
426 }
427 }
428 });
Michael Jurka104c4562013-07-08 18:03:46 -0700429
Michael Jurka2d8de582013-09-04 14:42:52 +0200430 mThumbnailOnClickListener = new OnClickListener() {
431 public void onClick(View v) {
Michael Jurkad6dc6042013-09-10 13:39:59 +0200432 if (mActionMode != null) {
433 // When CAB is up, clicking toggles the item instead
434 if (v.isLongClickable()) {
435 mLongClickListener.onLongClick(v);
436 }
437 return;
438 }
Selim Cinekffed6582014-02-28 17:18:59 +0100439 mSetWallpaperButton.setEnabled(true);
Michael Jurka1e4e6dd2013-09-23 23:02:05 +0100440 WallpaperTileInfo info = (WallpaperTileInfo) v.getTag();
Michael Jurka619a1802013-10-29 15:50:06 +0100441 if (info.isSelectable() && v.getVisibility() == View.VISIBLE) {
442 selectTile(v);
Michael Jurka2d8de582013-09-04 14:42:52 +0200443 }
Michael Jurka1e4e6dd2013-09-23 23:02:05 +0100444 info.onClick(WallpaperPickerActivity.this);
Michael Jurka2d8de582013-09-04 14:42:52 +0200445 }
446 };
Michael Jurkad6dc6042013-09-10 13:39:59 +0200447 mLongClickListener = new View.OnLongClickListener() {
448 // Called when the user long-clicks on someView
449 public boolean onLongClick(View view) {
450 CheckableFrameLayout c = (CheckableFrameLayout) view;
451 c.toggle();
452
453 if (mActionMode != null) {
454 mActionMode.invalidate();
455 } else {
456 // Start the CAB using the ActionMode.Callback defined below
457 mActionMode = startActionMode(mActionModeCallback);
458 int childCount = mWallpapersView.getChildCount();
459 for (int i = 0; i < childCount; i++) {
460 mWallpapersView.getChildAt(i).setSelected(false);
461 }
462 }
463 return true;
464 }
465 };
Michael Jurka2d8de582013-09-04 14:42:52 +0200466
Michael Jurka104c4562013-07-08 18:03:46 -0700467 // Populate the built-in wallpapers
Sunny Goyalcdc4dbd2014-07-16 16:04:08 -0700468 ArrayList<WallpaperTileInfo> wallpapers = findBundledWallpapers();
Michael Jurkabe2f8dd2013-09-10 13:39:59 +0200469 mWallpapersView = (LinearLayout) findViewById(R.id.wallpaper_list);
Sunny Goyalcdc4dbd2014-07-16 16:04:08 -0700470 SimpleWallpapersAdapter ia = new SimpleWallpapersAdapter(this, wallpapers);
Michael Jurka12ca6132013-10-15 11:14:44 -0700471 populateWallpapersFromAdapter(mWallpapersView, ia, false);
Michael Jurka104c4562013-07-08 18:03:46 -0700472
Michael Jurkabe2f8dd2013-09-10 13:39:59 +0200473 // Populate the saved wallpapers
474 mSavedImages = new SavedWallpaperImages(this);
475 mSavedImages.loadThumbnailsAndImageIdList();
Michael Jurka12ca6132013-10-15 11:14:44 -0700476 populateWallpapersFromAdapter(mWallpapersView, mSavedImages, true);
Michael Jurka998e4ff2013-09-18 16:04:36 +0200477
478 // Populate the live wallpapers
Michael Jurka1e4e6dd2013-09-23 23:02:05 +0100479 final LinearLayout liveWallpapersView =
480 (LinearLayout) findViewById(R.id.live_wallpaper_list);
Michael Jurka998e4ff2013-09-18 16:04:36 +0200481 final LiveWallpaperListAdapter a = new LiveWallpaperListAdapter(this);
482 a.registerDataSetObserver(new DataSetObserver() {
483 public void onChanged() {
484 liveWallpapersView.removeAllViews();
Michael Jurka12ca6132013-10-15 11:14:44 -0700485 populateWallpapersFromAdapter(liveWallpapersView, a, false);
Michael Jurkafe0ace32013-10-03 01:05:14 -0700486 initializeScrollForRtl();
Michael Jurka6b783a02013-10-04 12:26:48 -0700487 updateTileIndices();
Michael Jurka998e4ff2013-09-18 16:04:36 +0200488 }
489 });
490
491 // Populate the third-party wallpaper pickers
492 final LinearLayout thirdPartyWallpapersView =
493 (LinearLayout) findViewById(R.id.third_party_wallpaper_list);
494 final ThirdPartyWallpaperPickerListAdapter ta =
495 new ThirdPartyWallpaperPickerListAdapter(this);
Michael Jurka12ca6132013-10-15 11:14:44 -0700496 populateWallpapersFromAdapter(thirdPartyWallpapersView, ta, false);
Michael Jurka104c4562013-07-08 18:03:46 -0700497
Michael Jurka104c4562013-07-08 18:03:46 -0700498 // Add a tile for the Gallery
Michael Jurka998e4ff2013-09-18 16:04:36 +0200499 LinearLayout masterWallpaperList = (LinearLayout) findViewById(R.id.master_wallpaper_list);
Michael Jurka6b783a02013-10-04 12:26:48 -0700500 FrameLayout pickImageTile = (FrameLayout) getLayoutInflater().
Michael Jurka5b4b6902013-10-03 18:15:08 -0700501 inflate(R.layout.wallpaper_picker_image_picker_item, masterWallpaperList, false);
Michael Jurka6b783a02013-10-04 12:26:48 -0700502 setWallpaperItemPaddingToZero(pickImageTile);
503 masterWallpaperList.addView(pickImageTile, 0);
Michael Jurka104c4562013-07-08 18:03:46 -0700504
Michael Jurka42a71742013-09-18 00:45:45 +0200505 // Make its background the last photo taken on external storage
506 Bitmap lastPhoto = getThumbnailOfLastPhoto();
507 if (lastPhoto != null) {
508 ImageView galleryThumbnailBg =
Michael Jurka6b783a02013-10-04 12:26:48 -0700509 (ImageView) pickImageTile.findViewById(R.id.wallpaper_image);
Michael Jurka42a71742013-09-18 00:45:45 +0200510 galleryThumbnailBg.setImageBitmap(getThumbnailOfLastPhoto());
Michael Jurka998e4ff2013-09-18 16:04:36 +0200511 int colorOverlay = getResources().getColor(R.color.wallpaper_picker_translucent_gray);
512 galleryThumbnailBg.setColorFilter(colorOverlay, PorterDuff.Mode.SRC_ATOP);
Michael Jurkafe0ace32013-10-03 01:05:14 -0700513
Michael Jurka42a71742013-09-18 00:45:45 +0200514 }
515
Michael Jurka1e4e6dd2013-09-23 23:02:05 +0100516 PickImageInfo pickImageInfo = new PickImageInfo();
Michael Jurka6b783a02013-10-04 12:26:48 -0700517 pickImageTile.setTag(pickImageInfo);
518 pickImageInfo.setView(pickImageTile);
519 pickImageTile.setOnClickListener(mThumbnailOnClickListener);
Michael Jurka12ca6132013-10-15 11:14:44 -0700520
Michael Jurka12ca6132013-10-15 11:14:44 -0700521 // Select the first item; wait for a layout pass so that we initialize the dimensions of
522 // cropView or the defaultWallpaperView first
Michael Jurka83699e52013-10-23 20:59:51 +0200523 mCropView.addOnLayoutChangeListener(new OnLayoutChangeListener() {
Michael Jurka12ca6132013-10-15 11:14:44 -0700524 @Override
525 public void onLayoutChange(View v, int left, int top, int right, int bottom,
526 int oldLeft, int oldTop, int oldRight, int oldBottom) {
527 if ((right - left) > 0 && (bottom - top) > 0) {
Jorim Jaggi1b921ef2014-01-14 16:00:17 -0800528 if (mSelectedIndex >= 0 && mSelectedIndex < mWallpapersView.getChildCount()) {
529 mThumbnailOnClickListener.onClick(
530 mWallpapersView.getChildAt(mSelectedIndex));
Selim Cinekffed6582014-02-28 17:18:59 +0100531 setSystemWallpaperVisiblity(false);
Michael Jurka12ca6132013-10-15 11:14:44 -0700532 }
533 v.removeOnLayoutChangeListener(this);
534 }
535 }
536 });
Michael Jurka6b783a02013-10-04 12:26:48 -0700537
538 updateTileIndices();
Michael Jurka104c4562013-07-08 18:03:46 -0700539
Michael Jurkafe0ace32013-10-03 01:05:14 -0700540 // Update the scroll for RTL
541 initializeScrollForRtl();
542
Michael Jurkad6dc6042013-09-10 13:39:59 +0200543 // Create smooth layout transitions for when items are deleted
544 final LayoutTransition transitioner = new LayoutTransition();
545 transitioner.setDuration(200);
546 transitioner.setStartDelay(LayoutTransition.CHANGE_DISAPPEARING, 0);
547 transitioner.setAnimator(LayoutTransition.DISAPPEARING, null);
548 mWallpapersView.setLayoutTransition(transitioner);
549
Michael Jurka104c4562013-07-08 18:03:46 -0700550 // Action bar
551 // Show the custom action bar view
552 final ActionBar actionBar = getActionBar();
553 actionBar.setCustomView(R.layout.actionbar_set_wallpaper);
554 actionBar.getCustomView().setOnClickListener(
555 new View.OnClickListener() {
556 @Override
557 public void onClick(View v) {
Michael Jurka619a1802013-10-29 15:50:06 +0100558 if (mSelectedTile != null) {
559 WallpaperTileInfo info = (WallpaperTileInfo) mSelectedTile.getTag();
Michael Jurkab92f4332013-09-24 12:53:49 +0100560 info.onSave(WallpaperPickerActivity.this);
Selim Cinekffed6582014-02-28 17:18:59 +0100561 } else {
Filipe Gonçalves312cb312016-05-04 15:33:43 +0100562 final SeekBar slider = ((SeekBar) findViewById(R.id.darkness_slider));
563 if (slider != null){
564 updateDarknessOverlayAlpha(1f-(slider.getProgress()/100f));
565 } else {
566 updateDarknessOverlayAlpha(0f);
567 }
Selim Cinekffed6582014-02-28 17:18:59 +0100568 // no tile was selected, so we just finish the activity and go back
569 setResult(Activity.RESULT_OK);
570 finish();
Michael Jurkab92f4332013-09-24 12:53:49 +0100571 }
Michael Jurka104c4562013-07-08 18:03:46 -0700572 }
573 });
Michael Jurka849aff72014-01-16 14:58:00 +0100574 mSetWallpaperButton = findViewById(R.id.set_wallpaper_button);
Michael Jurkad6dc6042013-09-10 13:39:59 +0200575
576 // CAB for deleting items
577 mActionModeCallback = new ActionMode.Callback() {
578 // Called when the action mode is created; startActionMode() was called
579 @Override
580 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
581 // Inflate a menu resource providing context menu items
582 MenuInflater inflater = mode.getMenuInflater();
583 inflater.inflate(R.menu.cab_delete_wallpapers, menu);
584 return true;
585 }
586
587 private int numCheckedItems() {
588 int childCount = mWallpapersView.getChildCount();
589 int numCheckedItems = 0;
590 for (int i = 0; i < childCount; i++) {
591 CheckableFrameLayout c = (CheckableFrameLayout) mWallpapersView.getChildAt(i);
592 if (c.isChecked()) {
593 numCheckedItems++;
594 }
595 }
596 return numCheckedItems;
597 }
598
599 // Called each time the action mode is shown. Always called after onCreateActionMode,
600 // but may be called multiple times if the mode is invalidated.
601 @Override
602 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
603 int numCheckedItems = numCheckedItems();
604 if (numCheckedItems == 0) {
605 mode.finish();
606 return true;
607 } else {
608 mode.setTitle(getResources().getQuantityString(
609 R.plurals.number_of_items_selected, numCheckedItems, numCheckedItems));
610 return true;
611 }
612 }
613
614 // Called when the user selects a contextual menu item
615 @Override
616 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Bjorn Bringertc639f8c2013-09-13 13:39:12 +0100617 int itemId = item.getItemId();
618 if (itemId == R.id.menu_delete) {
619 int childCount = mWallpapersView.getChildCount();
620 ArrayList<View> viewsToRemove = new ArrayList<View>();
Selim Cinekffed6582014-02-28 17:18:59 +0100621 boolean selectedTileRemoved = false;
Bjorn Bringertc639f8c2013-09-13 13:39:12 +0100622 for (int i = 0; i < childCount; i++) {
623 CheckableFrameLayout c =
624 (CheckableFrameLayout) mWallpapersView.getChildAt(i);
625 if (c.isChecked()) {
Michael Jurka8a34bdb2013-09-25 07:57:33 -0700626 WallpaperTileInfo info = (WallpaperTileInfo) c.getTag();
627 info.onDelete(WallpaperPickerActivity.this);
Bjorn Bringertc639f8c2013-09-13 13:39:12 +0100628 viewsToRemove.add(c);
Selim Cinekffed6582014-02-28 17:18:59 +0100629 if (i == mSelectedIndex) {
630 selectedTileRemoved = true;
631 }
Michael Jurkad6dc6042013-09-10 13:39:59 +0200632 }
Bjorn Bringertc639f8c2013-09-13 13:39:12 +0100633 }
634 for (View v : viewsToRemove) {
635 mWallpapersView.removeView(v);
636 }
Selim Cinekffed6582014-02-28 17:18:59 +0100637 if (selectedTileRemoved) {
638 mSelectedIndex = -1;
639 mSelectedTile = null;
640 setSystemWallpaperVisiblity(true);
641 }
Michael Jurka6b783a02013-10-04 12:26:48 -0700642 updateTileIndices();
Bjorn Bringertc639f8c2013-09-13 13:39:12 +0100643 mode.finish(); // Action picked, so close the CAB
644 return true;
645 } else {
646 return false;
Michael Jurkad6dc6042013-09-10 13:39:59 +0200647 }
648 }
649
650 // Called when the user exits the action mode
651 @Override
652 public void onDestroyActionMode(ActionMode mode) {
653 int childCount = mWallpapersView.getChildCount();
654 for (int i = 0; i < childCount; i++) {
655 CheckableFrameLayout c = (CheckableFrameLayout) mWallpapersView.getChildAt(i);
656 c.setChecked(false);
657 }
Selim Cinekffed6582014-02-28 17:18:59 +0100658 if (mSelectedTile != null) {
659 mSelectedTile.setSelected(true);
660 }
Michael Jurkad6dc6042013-09-10 13:39:59 +0200661 mActionMode = null;
662 }
663 };
Filipe Gonçalves312cb312016-05-04 15:33:43 +0100664 String spKey = getSharedPreferencesKey();
665 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_MULTI_PROCESS);
666 float darknessAlpha = getDarknessOverlayAlpha(sp);
667 SeekBar darknessSlider = (SeekBar) findViewById(R.id.darkness_slider);
668 darknessSlider.setProgress((int)((1-darknessAlpha)*100));
669 findViewById(R.id.darkness_overlay).setAlpha(darknessAlpha);
670 darknessSlider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
671 @Override
672 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
673 findViewById(R.id.darkness_overlay).setAlpha(1f-(progress/100f));
674 }
675
676 @Override
677 public void onStartTrackingTouch(SeekBar seekBar) {
678
679 }
680
681 @Override
682 public void onStopTrackingTouch(SeekBar seekBar) {
683
684 }
685 });
Michael Jurkad6dc6042013-09-10 13:39:59 +0200686 }
Michael Jurka619a1802013-10-29 15:50:06 +0100687
688 private void selectTile(View v) {
689 if (mSelectedTile != null) {
690 mSelectedTile.setSelected(false);
691 mSelectedTile = null;
692 }
693 mSelectedTile = v;
694 v.setSelected(true);
Jorim Jaggi1b921ef2014-01-14 16:00:17 -0800695 mSelectedIndex = mWallpapersView.indexOfChild(v);
Michael Jurka619a1802013-10-29 15:50:06 +0100696 // TODO: Remove this once the accessibility framework and
697 // services have better support for selection state.
698 v.announceForAccessibility(
699 getString(R.string.announce_selection, v.getContentDescription()));
Michael Jurka862f7e32013-10-16 18:23:56 -0700700 }
Michael Jurka42a71742013-09-18 00:45:45 +0200701
Michael Jurkafe0ace32013-10-03 01:05:14 -0700702 private void initializeScrollForRtl() {
703 final HorizontalScrollView scroll =
704 (HorizontalScrollView) findViewById(R.id.wallpaper_scroll_container);
705
706 if (scroll.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
707 final ViewTreeObserver observer = scroll.getViewTreeObserver();
708 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
709 public void onGlobalLayout() {
710 LinearLayout masterWallpaperList =
711 (LinearLayout) findViewById(R.id.master_wallpaper_list);
712 scroll.scrollTo(masterWallpaperList.getWidth(), 0);
713 scroll.getViewTreeObserver().removeOnGlobalLayoutListener(this);
714 }
715 });
716 }
717 }
718
Michael Jurka42a71742013-09-18 00:45:45 +0200719 protected Bitmap getThumbnailOfLastPhoto() {
720 Cursor cursor = MediaStore.Images.Media.query(getContentResolver(),
721 MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
722 new String[] { MediaStore.Images.ImageColumns._ID,
723 MediaStore.Images.ImageColumns.DATE_TAKEN},
724 null, null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC LIMIT 1");
Adam Cohenc8d31ff2014-06-12 12:29:50 -0700725
Michael Jurka42a71742013-09-18 00:45:45 +0200726 Bitmap thumb = null;
Adam Cohenc8d31ff2014-06-12 12:29:50 -0700727 if (cursor != null) {
728 if (cursor.moveToNext()) {
729 int id = cursor.getInt(0);
730 thumb = MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(),
731 id, MediaStore.Images.Thumbnails.MINI_KIND, null);
732 }
733 cursor.close();
Michael Jurka42a71742013-09-18 00:45:45 +0200734 }
Michael Jurka42a71742013-09-18 00:45:45 +0200735 return thumb;
736 }
Michael Jurka5664de22013-09-12 02:53:22 +0200737
738 protected void onStop() {
739 super.onStop();
Michael Jurka7267fa52013-09-26 15:29:57 -0700740 mWallpaperStrip = findViewById(R.id.wallpaper_strip);
Daniel Sandler24f397d2013-10-12 00:02:45 -0400741 if (mWallpaperStrip.getAlpha() < 1f) {
Michael Jurka7267fa52013-09-26 15:29:57 -0700742 mWallpaperStrip.setAlpha(1f);
Daniel Sandler24f397d2013-10-12 00:02:45 -0400743 mWallpaperStrip.setVisibility(View.VISIBLE);
Michael Jurka5664de22013-09-12 02:53:22 +0200744 }
745 }
746
Michael Jurkad6dc6042013-09-10 13:39:59 +0200747 protected void onSaveInstanceState(Bundle outState) {
748 outState.putParcelableArrayList(TEMP_WALLPAPER_TILES, mTempWallpaperTiles);
Jorim Jaggi1b921ef2014-01-14 16:00:17 -0800749 outState.putInt(SELECTED_INDEX, mSelectedIndex);
Michael Jurka104c4562013-07-08 18:03:46 -0700750 }
751
Michael Jurkad6dc6042013-09-10 13:39:59 +0200752 protected void onRestoreInstanceState(Bundle savedInstanceState) {
753 ArrayList<Uri> uris = savedInstanceState.getParcelableArrayList(TEMP_WALLPAPER_TILES);
754 for (Uri uri : uris) {
Jorim Jaggi1b921ef2014-01-14 16:00:17 -0800755 addTemporaryWallpaperTile(uri, true);
Michael Jurkad6dc6042013-09-10 13:39:59 +0200756 }
Selim Cinekffed6582014-02-28 17:18:59 +0100757 mSelectedIndex = savedInstanceState.getInt(SELECTED_INDEX, -1);
Michael Jurkad6dc6042013-09-10 13:39:59 +0200758 }
Michael Jurkabe2f8dd2013-09-10 13:39:59 +0200759
Michael Jurka998e4ff2013-09-18 16:04:36 +0200760 private void populateWallpapersFromAdapter(ViewGroup parent, BaseAdapter adapter,
Michael Jurka12ca6132013-10-15 11:14:44 -0700761 boolean addLongPressHandler) {
Michael Jurka998e4ff2013-09-18 16:04:36 +0200762 for (int i = 0; i < adapter.getCount(); i++) {
763 FrameLayout thumbnail = (FrameLayout) adapter.getView(i, null, parent);
Michael Jurkabe2f8dd2013-09-10 13:39:59 +0200764 parent.addView(thumbnail, i);
Michael Jurka6b783a02013-10-04 12:26:48 -0700765 WallpaperTileInfo info = (WallpaperTileInfo) adapter.getItem(i);
766 thumbnail.setTag(info);
767 info.setView(thumbnail);
Michael Jurkabe2f8dd2013-09-10 13:39:59 +0200768 if (addLongPressHandler) {
769 addLongPressHandler(thumbnail);
770 }
771 thumbnail.setOnClickListener(mThumbnailOnClickListener);
Michael Jurkabe2f8dd2013-09-10 13:39:59 +0200772 }
773 }
774
Michael Jurka6b783a02013-10-04 12:26:48 -0700775 private void updateTileIndices() {
776 LinearLayout masterWallpaperList = (LinearLayout) findViewById(R.id.master_wallpaper_list);
777 final int childCount = masterWallpaperList.getChildCount();
Michael Jurka6b783a02013-10-04 12:26:48 -0700778 final Resources res = getResources();
779
780 // Do two passes; the first pass gets the total number of tiles
781 int numTiles = 0;
782 for (int passNum = 0; passNum < 2; passNum++) {
783 int tileIndex = 0;
784 for (int i = 0; i < childCount; i++) {
785 View child = masterWallpaperList.getChildAt(i);
786 LinearLayout subList;
787
788 int subListStart;
789 int subListEnd;
790 if (child.getTag() instanceof WallpaperTileInfo) {
791 subList = masterWallpaperList;
792 subListStart = i;
793 subListEnd = i + 1;
794 } else { // if (child instanceof LinearLayout) {
795 subList = (LinearLayout) child;
796 subListStart = 0;
797 subListEnd = subList.getChildCount();
798 }
799
800 for (int j = subListStart; j < subListEnd; j++) {
801 WallpaperTileInfo info = (WallpaperTileInfo) subList.getChildAt(j).getTag();
802 if (info.isNamelessWallpaper()) {
803 if (passNum == 0) {
804 numTiles++;
805 } else {
806 CharSequence label = res.getString(
807 R.string.wallpaper_accessibility_name, ++tileIndex, numTiles);
808 info.onIndexUpdated(label);
809 }
810 }
811 }
812 }
813 }
814 }
815
Michael Jurka1e4e6dd2013-09-23 23:02:05 +0100816 private static Point getDefaultThumbnailSize(Resources res) {
817 return new Point(res.getDimensionPixelSize(R.dimen.wallpaperThumbnailWidth),
818 res.getDimensionPixelSize(R.dimen.wallpaperThumbnailHeight));
819
820 }
821
822 private static Bitmap createThumbnail(Point size, Context context, Uri uri, byte[] imageBytes,
Michael Jurka8713a942013-10-10 21:28:40 -0700823 Resources res, int resId, int rotation, boolean leftAligned) {
Michael Jurka1e4e6dd2013-09-23 23:02:05 +0100824 int width = size.x;
825 int height = size.y;
Michael Jurkabe2f8dd2013-09-10 13:39:59 +0200826
827 BitmapCropTask cropTask;
828 if (uri != null) {
Michael Jurka8713a942013-10-10 21:28:40 -0700829 cropTask = new BitmapCropTask(
830 context, uri, null, rotation, width, height, false, true, null);
Michael Jurka1e4e6dd2013-09-23 23:02:05 +0100831 } else if (imageBytes != null) {
Michael Jurka8713a942013-10-10 21:28:40 -0700832 cropTask = new BitmapCropTask(
833 imageBytes, null, rotation, width, height, false, true, null);
Michael Jurka1e4e6dd2013-09-23 23:02:05 +0100834 } else {
Michael Jurka8713a942013-10-10 21:28:40 -0700835 cropTask = new BitmapCropTask(
836 context, res, resId, null, rotation, width, height, false, true, null);
Michael Jurkabe2f8dd2013-09-10 13:39:59 +0200837 }
838 Point bounds = cropTask.getImageBounds();
Michael Jurkadb84e822013-09-23 23:02:05 +0100839 if (bounds == null || bounds.x == 0 || bounds.y == 0) {
Michael Jurkabe2f8dd2013-09-10 13:39:59 +0200840 return null;
841 }
842
Michael Jurka8713a942013-10-10 21:28:40 -0700843 Matrix rotateMatrix = new Matrix();
844 rotateMatrix.setRotate(rotation);
Jose Pascoal0d9e9d02015-02-10 15:55:39 +0000845 float[] rotatedBounds = { bounds.x, bounds.y };
Michael Jurka8713a942013-10-10 21:28:40 -0700846 rotateMatrix.mapPoints(rotatedBounds);
847 rotatedBounds[0] = Math.abs(rotatedBounds[0]);
848 rotatedBounds[1] = Math.abs(rotatedBounds[1]);
849
Michael Jurkabe2f8dd2013-09-10 13:39:59 +0200850 RectF cropRect = WallpaperCropActivity.getMaxCropRect(
Michael Jurka8713a942013-10-10 21:28:40 -0700851 (int) rotatedBounds[0], (int) rotatedBounds[1], width, height, leftAligned);
Michael Jurkabe2f8dd2013-09-10 13:39:59 +0200852 cropTask.setCropBounds(cropRect);
853
854 if (cropTask.cropBitmap()) {
855 return cropTask.getCroppedBitmap();
856 } else {
857 return null;
858 }
859 }
860
Jorim Jaggi1b921ef2014-01-14 16:00:17 -0800861 private void addTemporaryWallpaperTile(final Uri uri, boolean fromRestore) {
Michael Jurkabe2f8dd2013-09-10 13:39:59 +0200862 mTempWallpaperTiles.add(uri);
863 // Add a tile for the image picked from Gallery
Michael Jurka619a1802013-10-29 15:50:06 +0100864 final FrameLayout pickedImageThumbnail = (FrameLayout) getLayoutInflater().
Michael Jurkabe2f8dd2013-09-10 13:39:59 +0200865 inflate(R.layout.wallpaper_picker_item, mWallpapersView, false);
Michael Jurka619a1802013-10-29 15:50:06 +0100866 pickedImageThumbnail.setVisibility(View.GONE);
Michael Jurkabe2f8dd2013-09-10 13:39:59 +0200867 setWallpaperItemPaddingToZero(pickedImageThumbnail);
Michael Jurka862f7e32013-10-16 18:23:56 -0700868 mWallpapersView.addView(pickedImageThumbnail, 0);
Michael Jurkabe2f8dd2013-09-10 13:39:59 +0200869
870 // Load the thumbnail
Michael Jurka862f7e32013-10-16 18:23:56 -0700871 final ImageView image = (ImageView) pickedImageThumbnail.findViewById(R.id.wallpaper_image);
872 final Point defaultSize = getDefaultThumbnailSize(this.getResources());
873 final Context context = this;
874 new AsyncTask<Void, Bitmap, Bitmap>() {
875 protected Bitmap doInBackground(Void...args) {
Adam Cohen8dc6e1b2014-07-24 10:39:57 -0700876 try {
877 int rotation = WallpaperCropActivity.getRotationFromExif(context, uri);
878 return createThumbnail(defaultSize, context, uri, null, null, 0, rotation, false);
879 } catch (SecurityException securityException) {
880 if (isDestroyed()) {
881 // Temporarily granted permissions are revoked when the activity
882 // finishes, potentially resulting in a SecurityException here.
883 // Even though {@link #isDestroyed} might also return true in different
884 // situations where the configuration changes, we are fine with
885 // catching these cases here as well.
886 cancel(false);
887 } else {
888 // otherwise it had a different cause and we throw it further
889 throw securityException;
890 }
891 return null;
892 }
Michael Jurka862f7e32013-10-16 18:23:56 -0700893 }
894 protected void onPostExecute(Bitmap thumb) {
Adam Cohen8dc6e1b2014-07-24 10:39:57 -0700895 if (!isCancelled() && thumb != null) {
Michael Jurka862f7e32013-10-16 18:23:56 -0700896 image.setImageBitmap(thumb);
897 Drawable thumbDrawable = image.getDrawable();
898 thumbDrawable.setDither(true);
Jorim Jaggi1b921ef2014-01-14 16:00:17 -0800899 pickedImageThumbnail.setVisibility(View.VISIBLE);
Michael Jurka862f7e32013-10-16 18:23:56 -0700900 } else {
901 Log.e(TAG, "Error loading thumbnail for uri=" + uri);
902 }
903 }
904 }.execute();
Michael Jurkabe2f8dd2013-09-10 13:39:59 +0200905
Michael Jurka1e4e6dd2013-09-23 23:02:05 +0100906 UriWallpaperInfo info = new UriWallpaperInfo(uri);
907 pickedImageThumbnail.setTag(info);
Michael Jurka6b783a02013-10-04 12:26:48 -0700908 info.setView(pickedImageThumbnail);
Michael Jurka8713a942013-10-10 21:28:40 -0700909 addLongPressHandler(pickedImageThumbnail);
Michael Jurka7b715492013-10-09 11:22:34 -0700910 updateTileIndices();
Michael Jurkabe2f8dd2013-09-10 13:39:59 +0200911 pickedImageThumbnail.setOnClickListener(mThumbnailOnClickListener);
Jorim Jaggi1b921ef2014-01-14 16:00:17 -0800912 if (!fromRestore) {
913 mThumbnailOnClickListener.onClick(pickedImageThumbnail);
914 }
Michael Jurkabe2f8dd2013-09-10 13:39:59 +0200915 }
916
Michael Jurka2d8de582013-09-04 14:42:52 +0200917 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
918 if (requestCode == IMAGE_PICK && resultCode == RESULT_OK) {
Michael Jurka2fe15302013-10-04 12:35:27 -0700919 if (data != null && data.getData() != null) {
920 Uri uri = data.getData();
Jorim Jaggi1b921ef2014-01-14 16:00:17 -0800921 addTemporaryWallpaperTile(uri, false);
Michael Jurka2fe15302013-10-04 12:35:27 -0700922 }
Michael Jurka2d8de582013-09-04 14:42:52 +0200923 } else if (requestCode == PICK_WALLPAPER_THIRD_PARTY_ACTIVITY) {
Michael Jurka2d8de582013-09-04 14:42:52 +0200924 setResult(RESULT_OK);
925 finish();
Michael Jurka998e4ff2013-09-18 16:04:36 +0200926 } else if (requestCode == PICK_LIVE_WALLPAPER) {
927 WallpaperManager wm = WallpaperManager.getInstance(this);
928 final WallpaperInfo oldLiveWallpaper = mLiveWallpaperInfoOnPickerLaunch;
Selim Cinekffed6582014-02-28 17:18:59 +0100929 final WallpaperInfo clickedWallpaper = mLastClickedLiveWallpaperInfo;
Michael Jurka998e4ff2013-09-18 16:04:36 +0200930 WallpaperInfo newLiveWallpaper = wm.getWallpaperInfo();
931 // Try to figure out if a live wallpaper was set;
932 if (newLiveWallpaper != null &&
Selim Cinekffed6582014-02-28 17:18:59 +0100933 (oldLiveWallpaper == null
934 || !oldLiveWallpaper.getComponent()
935 .equals(newLiveWallpaper.getComponent())
936 || clickedWallpaper.getComponent()
937 .equals(oldLiveWallpaper.getComponent()))) {
Michael Jurka998e4ff2013-09-18 16:04:36 +0200938 // Return if a live wallpaper was set
939 setResult(RESULT_OK);
940 finish();
941 }
Michael Jurka2d8de582013-09-04 14:42:52 +0200942 }
943 }
944
Michael Jurka998e4ff2013-09-18 16:04:36 +0200945 static void setWallpaperItemPaddingToZero(FrameLayout frameLayout) {
Michael Jurka104c4562013-07-08 18:03:46 -0700946 frameLayout.setPadding(0, 0, 0, 0);
947 frameLayout.setForeground(new ZeroPaddingDrawable(frameLayout.getForeground()));
948 }
949
Michael Jurkabe2f8dd2013-09-10 13:39:59 +0200950 private void addLongPressHandler(View v) {
951 v.setOnLongClickListener(mLongClickListener);
952 }
953
Sunny Goyalcdc4dbd2014-07-16 16:04:08 -0700954 private ArrayList<WallpaperTileInfo> findBundledWallpapers() {
Jeff Sharkey5aeef582014-04-14 13:26:42 -0700955 final PackageManager pm = getPackageManager();
Sunny Goyalcdc4dbd2014-07-16 16:04:08 -0700956 final ArrayList<WallpaperTileInfo> bundled = new ArrayList<WallpaperTileInfo>(24);
Jeff Sharkey5aeef582014-04-14 13:26:42 -0700957
958 Partner partner = Partner.get(pm);
959 if (partner != null) {
960 final Resources partnerRes = partner.getResources();
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700961 final int resId = partnerRes.getIdentifier(Partner.RES_WALLPAPERS, "array",
Jeff Sharkey5aeef582014-04-14 13:26:42 -0700962 partner.getPackageName());
963 if (resId != 0) {
964 addWallpapers(bundled, partnerRes, partner.getPackageName(), resId);
965 }
Sunny Goyalcdc4dbd2014-07-16 16:04:08 -0700966
967 // Add system wallpapers
968 File systemDir = partner.getWallpaperDirectory();
969 if (systemDir != null && systemDir.isDirectory()) {
970 for (File file : systemDir.listFiles()) {
971 if (!file.isFile()) {
972 continue;
973 }
974 String name = file.getName();
975 int dotPos = name.lastIndexOf('.');
976 String extension = "";
977 if (dotPos >= -1) {
978 extension = name.substring(dotPos);
979 name = name.substring(0, dotPos);
980 }
981
982 if (name.endsWith("_small")) {
983 // it is a thumbnail
984 continue;
985 }
986
987 File thumbnail = new File(systemDir, name + "_small" + extension);
988 Bitmap thumb = BitmapFactory.decodeFile(thumbnail.getAbsolutePath());
989 if (thumb != null) {
Jose Pascoal05218202014-11-19 11:17:38 +0000990 bundled.add(new FileWallpaperInfo(file, new BitmapDrawable(getResources(), thumb)));
Sunny Goyalcdc4dbd2014-07-16 16:04:08 -0700991 }
992 }
993 }
Jeff Sharkey5aeef582014-04-14 13:26:42 -0700994 }
Michael Jurka104c4562013-07-08 18:03:46 -0700995
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200996 Pair<ApplicationInfo, Integer> r = getWallpaperArrayResourceId();
997 if (r != null) {
998 try {
Michael Jurka1e4e6dd2013-09-23 23:02:05 +0100999 Resources wallpaperRes = getPackageManager().getResourcesForApplication(r.first);
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001000 addWallpapers(bundled, wallpaperRes, r.first.packageName, r.second);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001001 } catch (PackageManager.NameNotFoundException e) {
1002 }
1003 }
Michael Jurkadb84e822013-09-23 23:02:05 +01001004
Sunny Goyalcdc4dbd2014-07-16 16:04:08 -07001005 if (partner == null || !partner.hideDefaultWallpaper()) {
1006 // Add an entry for the default wallpaper (stored in system resources)
1007 WallpaperTileInfo defaultWallpaperInfo =
1008 (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)
1009 ? getPreKKDefaultWallpaperInfo()
1010 : getDefaultWallpaper();
Michael Jurka12ca6132013-10-15 11:14:44 -07001011 if (defaultWallpaperInfo != null) {
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001012 bundled.add(0, defaultWallpaperInfo);
Michael Jurka12ca6132013-10-15 11:14:44 -07001013 }
Michael Jurkadb84e822013-09-23 23:02:05 +01001014 }
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001015 return bundled;
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001016 }
1017
Michael Jurka12ca6132013-10-15 11:14:44 -07001018 private boolean writeImageToFileAsJpeg(File f, Bitmap b) {
1019 try {
1020 f.createNewFile();
1021 FileOutputStream thumbFileStream =
1022 openFileOutput(f.getName(), Context.MODE_PRIVATE);
1023 b.compress(Bitmap.CompressFormat.JPEG, 95, thumbFileStream);
1024 thumbFileStream.close();
1025 return true;
1026 } catch (IOException e) {
1027 Log.e(TAG, "Error while writing bitmap to file " + e);
1028 f.delete();
1029 }
1030 return false;
1031 }
1032
Sunny Goyal0af0d842014-08-13 12:12:48 -07001033 private File getDefaultThumbFile() {
1034 return new File(getFilesDir(), Build.VERSION.SDK_INT
1035 + "_" + DEFAULT_WALLPAPER_THUMBNAIL_FILENAME);
1036 }
1037
1038 private boolean saveDefaultWallpaperThumb(Bitmap b) {
1039 // Delete old thumbnails.
1040 new File(getFilesDir(), OLD_DEFAULT_WALLPAPER_THUMBNAIL_FILENAME).delete();
1041 new File(getFilesDir(), DEFAULT_WALLPAPER_THUMBNAIL_FILENAME).delete();
1042
1043 for (int i = Build.VERSION_CODES.JELLY_BEAN; i < Build.VERSION.SDK_INT; i++) {
1044 new File(getFilesDir(), i + "_" + DEFAULT_WALLPAPER_THUMBNAIL_FILENAME).delete();
1045 }
1046 return writeImageToFileAsJpeg(getDefaultThumbFile(), b);
1047 }
1048
Michael Jurka12ca6132013-10-15 11:14:44 -07001049 private ResourceWallpaperInfo getPreKKDefaultWallpaperInfo() {
Michael Jurkadb84e822013-09-23 23:02:05 +01001050 Resources sysRes = Resources.getSystem();
1051 int resId = sysRes.getIdentifier("default_wallpaper", "drawable", "android");
1052
Sunny Goyal0af0d842014-08-13 12:12:48 -07001053 File defaultThumbFile = getDefaultThumbFile();
Jose Pascoalfe3b08b2015-02-11 18:01:38 +00001054 Bitmap thumb;
Michael Jurkadb84e822013-09-23 23:02:05 +01001055 boolean defaultWallpaperExists = false;
1056 if (defaultThumbFile.exists()) {
1057 thumb = BitmapFactory.decodeFile(defaultThumbFile.getAbsolutePath());
1058 defaultWallpaperExists = true;
1059 } else {
Michael Jurka8713a942013-10-10 21:28:40 -07001060 Resources res = getResources();
1061 Point defaultThumbSize = getDefaultThumbnailSize(res);
1062 int rotation = WallpaperCropActivity.getRotationFromExif(res, resId);
1063 thumb = createThumbnail(
1064 defaultThumbSize, this, null, null, sysRes, resId, rotation, false);
Michael Jurkadb84e822013-09-23 23:02:05 +01001065 if (thumb != null) {
Sunny Goyal0af0d842014-08-13 12:12:48 -07001066 defaultWallpaperExists = saveDefaultWallpaperThumb(thumb);
Michael Jurkadb84e822013-09-23 23:02:05 +01001067 }
1068 }
1069 if (defaultWallpaperExists) {
1070 return new ResourceWallpaperInfo(sysRes, resId, new BitmapDrawable(thumb));
1071 }
1072 return null;
1073 }
1074
Sunny Goyalcdc4dbd2014-07-16 16:04:08 -07001075 @TargetApi(Build.VERSION_CODES.KITKAT)
Michael Jurka12ca6132013-10-15 11:14:44 -07001076 private DefaultWallpaperInfo getDefaultWallpaper() {
Sunny Goyal0af0d842014-08-13 12:12:48 -07001077 File defaultThumbFile = getDefaultThumbFile();
Michael Jurka12ca6132013-10-15 11:14:44 -07001078 Bitmap thumb = null;
1079 boolean defaultWallpaperExists = false;
1080 if (defaultThumbFile.exists()) {
1081 thumb = BitmapFactory.decodeFile(defaultThumbFile.getAbsolutePath());
1082 defaultWallpaperExists = true;
1083 } else {
1084 Resources res = getResources();
1085 Point defaultThumbSize = getDefaultThumbnailSize(res);
Michael Jurka12ca6132013-10-15 11:14:44 -07001086 Drawable wallpaperDrawable = WallpaperManager.getInstance(this).getBuiltInDrawable(
1087 defaultThumbSize.x, defaultThumbSize.y, true, 0.5f, 0.5f);
1088 if (wallpaperDrawable != null) {
1089 thumb = Bitmap.createBitmap(
1090 defaultThumbSize.x, defaultThumbSize.y, Bitmap.Config.ARGB_8888);
1091 Canvas c = new Canvas(thumb);
Michael Jurkaa7d82b92013-11-08 15:45:48 +01001092 wallpaperDrawable.setBounds(0, 0, defaultThumbSize.x, defaultThumbSize.y);
Michael Jurka12ca6132013-10-15 11:14:44 -07001093 wallpaperDrawable.draw(c);
1094 c.setBitmap(null);
1095 }
1096 if (thumb != null) {
Sunny Goyal0af0d842014-08-13 12:12:48 -07001097 defaultWallpaperExists = saveDefaultWallpaperThumb(thumb);
Michael Jurka12ca6132013-10-15 11:14:44 -07001098 }
1099 }
1100 if (defaultWallpaperExists) {
1101 return new DefaultWallpaperInfo(new BitmapDrawable(thumb));
1102 }
1103 return null;
1104 }
1105
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001106 public Pair<ApplicationInfo, Integer> getWallpaperArrayResourceId() {
Michael Jurka104c4562013-07-08 18:03:46 -07001107 // Context.getPackageName() may return the "original" package name,
1108 // com.android.launcher3; Resources needs the real package name,
1109 // com.android.launcher3. So we ask Resources for what it thinks the
1110 // package name should be.
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001111 final String packageName = getResources().getResourcePackageName(R.array.wallpapers);
1112 try {
1113 ApplicationInfo info = getPackageManager().getApplicationInfo(packageName, 0);
1114 return new Pair<ApplicationInfo, Integer>(info, R.array.wallpapers);
1115 } catch (PackageManager.NameNotFoundException e) {
1116 return null;
1117 }
Michael Jurka104c4562013-07-08 18:03:46 -07001118 }
1119
Jose Pascoal8bdb3642015-02-13 13:15:11 +00001120 private static void addWallpapers(ArrayList<WallpaperTileInfo> known, Resources res,
1121 String packageName, int listResId) {
Michael Jurka1e4e6dd2013-09-23 23:02:05 +01001122 final String[] extras = res.getStringArray(listResId);
Michael Jurka104c4562013-07-08 18:03:46 -07001123 for (String extra : extras) {
Michael Jurka1e4e6dd2013-09-23 23:02:05 +01001124 int resId = res.getIdentifier(extra, "drawable", packageName);
1125 if (resId != 0) {
1126 final int thumbRes = res.getIdentifier(extra + "_small", "drawable", packageName);
Michael Jurka104c4562013-07-08 18:03:46 -07001127
1128 if (thumbRes != 0) {
Michael Jurka1e4e6dd2013-09-23 23:02:05 +01001129 ResourceWallpaperInfo wallpaperInfo =
1130 new ResourceWallpaperInfo(res, resId, res.getDrawable(thumbRes));
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001131 known.add(wallpaperInfo);
Michael Jurka104c4562013-07-08 18:03:46 -07001132 // Log.d(TAG, "add: [" + packageName + "]: " + extra + " (" + res + ")");
1133 }
Michael Jurka6218e5b2013-09-10 14:07:20 +02001134 } else {
1135 Log.e(TAG, "Couldn't find wallpaper " + extra);
Michael Jurka104c4562013-07-08 18:03:46 -07001136 }
1137 }
Michael Jurka1e4e6dd2013-09-23 23:02:05 +01001138 }
1139
1140 public CropView getCropView() {
1141 return mCropView;
1142 }
1143
1144 public SavedWallpaperImages getSavedImages() {
1145 return mSavedImages;
1146 }
1147
Selim Cinekffed6582014-02-28 17:18:59 +01001148 public void onLiveWallpaperPickerLaunch(WallpaperInfo info) {
1149 mLastClickedLiveWallpaperInfo = info;
Michael Jurka1e4e6dd2013-09-23 23:02:05 +01001150 mLiveWallpaperInfoOnPickerLaunch = WallpaperManager.getInstance(this).getWallpaperInfo();
Michael Jurka104c4562013-07-08 18:03:46 -07001151 }
1152
Michael Jurka104c4562013-07-08 18:03:46 -07001153 static class ZeroPaddingDrawable extends LevelListDrawable {
1154 public ZeroPaddingDrawable(Drawable d) {
1155 super();
1156 addLevel(0, 0, d);
1157 setLevel(0);
1158 }
1159
1160 @Override
1161 public boolean getPadding(Rect padding) {
1162 padding.set(0, 0, 0, 0);
1163 return true;
1164 }
1165 }
1166
Sunny Goyalcdc4dbd2014-07-16 16:04:08 -07001167 private static class SimpleWallpapersAdapter extends ArrayAdapter<WallpaperTileInfo> {
1168 private final LayoutInflater mLayoutInflater;
Michael Jurka104c4562013-07-08 18:03:46 -07001169
Sunny Goyalcdc4dbd2014-07-16 16:04:08 -07001170 SimpleWallpapersAdapter(Activity activity, ArrayList<WallpaperTileInfo> wallpapers) {
1171 super(activity, R.layout.wallpaper_picker_item, wallpapers);
Michael Jurka104c4562013-07-08 18:03:46 -07001172 mLayoutInflater = activity.getLayoutInflater();
Michael Jurka104c4562013-07-08 18:03:46 -07001173 }
1174
1175 public View getView(int position, View convertView, ViewGroup parent) {
Sunny Goyalcdc4dbd2014-07-16 16:04:08 -07001176 Drawable thumb = getItem(position).mThumb;
Michael Jurka1e4e6dd2013-09-23 23:02:05 +01001177 if (thumb == null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001178 Log.e(TAG, "Error decoding thumbnail for wallpaper #" + position);
Michael Jurka104c4562013-07-08 18:03:46 -07001179 }
Sunny Goyalcdc4dbd2014-07-16 16:04:08 -07001180 return createImageTileView(mLayoutInflater, convertView, parent, thumb);
Michael Jurka104c4562013-07-08 18:03:46 -07001181 }
1182 }
Michael Jurka1e4e6dd2013-09-23 23:02:05 +01001183
Sunny Goyalcdc4dbd2014-07-16 16:04:08 -07001184 public static View createImageTileView(LayoutInflater layoutInflater,
Michael Jurka1e4e6dd2013-09-23 23:02:05 +01001185 View convertView, ViewGroup parent, Drawable thumb) {
1186 View view;
1187
1188 if (convertView == null) {
1189 view = layoutInflater.inflate(R.layout.wallpaper_picker_item, parent, false);
1190 } else {
1191 view = convertView;
1192 }
1193
1194 setWallpaperItemPaddingToZero((FrameLayout) view);
1195
1196 ImageView image = (ImageView) view.findViewById(R.id.wallpaper_image);
1197
1198 if (thumb != null) {
1199 image.setImageDrawable(thumb);
1200 thumb.setDither(true);
1201 }
1202
1203 return view;
1204 }
Michael Jurka7ad868b2013-12-12 15:04:25 +01001205
1206 // In Launcher3, we override this with a method that catches exceptions
1207 // from starting activities; didn't want to copy and paste code into here
1208 public void startActivityForResultSafely(Intent intent, int requestCode) {
1209 startActivityForResult(intent, requestCode);
1210 }
Michael Jurka104c4562013-07-08 18:03:46 -07001211}