Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 1 | /* |
| 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 Pascoal | 0521820 | 2014-11-19 11:17:38 +0000 | [diff] [blame] | 17 | package com.fairphone.fplauncher3; |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 18 | |
Michael Jurka | be2f8dd | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 19 | import android.animation.LayoutTransition; |
Sunny Goyal | cdc4dbd | 2014-07-16 16:04:08 -0700 | [diff] [blame] | 20 | import android.annotation.TargetApi; |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 21 | import android.app.ActionBar; |
| 22 | import android.app.Activity; |
Michael Jurka | 998e4ff | 2013-09-18 16:04:36 +0200 | [diff] [blame] | 23 | import android.app.WallpaperInfo; |
| 24 | import android.app.WallpaperManager; |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 25 | import android.content.Context; |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 26 | import android.content.Intent; |
Filipe Gonçalves | 312cb31 | 2016-05-04 15:33:43 +0100 | [diff] [blame] | 27 | import android.content.SharedPreferences; |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 28 | import android.content.pm.ApplicationInfo; |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 29 | import android.content.pm.PackageManager; |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 30 | import android.content.res.Resources; |
Michael Jurka | 42a7174 | 2013-09-18 00:45:45 +0200 | [diff] [blame] | 31 | import android.database.Cursor; |
Michael Jurka | 998e4ff | 2013-09-18 16:04:36 +0200 | [diff] [blame] | 32 | import android.database.DataSetObserver; |
Michael Jurka | be2f8dd | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 33 | import android.graphics.Bitmap; |
Michael Jurka | db84e82 | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 34 | import android.graphics.BitmapFactory; |
Michael Jurka | 12ca613 | 2013-10-15 11:14:44 -0700 | [diff] [blame] | 35 | import android.graphics.Canvas; |
Michael Jurka | 8713a94 | 2013-10-10 21:28:40 -0700 | [diff] [blame] | 36 | import android.graphics.Matrix; |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 37 | import android.graphics.Point; |
Michael Jurka | 998e4ff | 2013-09-18 16:04:36 +0200 | [diff] [blame] | 38 | import android.graphics.PorterDuff; |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 39 | import android.graphics.Rect; |
| 40 | import android.graphics.RectF; |
Michael Jurka | db84e82 | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 41 | import android.graphics.drawable.BitmapDrawable; |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 42 | import android.graphics.drawable.Drawable; |
| 43 | import android.graphics.drawable.LevelListDrawable; |
| 44 | import android.net.Uri; |
Michael Jurka | 862f7e3 | 2013-10-16 18:23:56 -0700 | [diff] [blame] | 45 | import android.os.AsyncTask; |
Michael Jurka | 12ca613 | 2013-10-15 11:14:44 -0700 | [diff] [blame] | 46 | import android.os.Build; |
Michael Jurka | d6dc604 | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 47 | import android.os.Bundle; |
Michael Jurka | 42a7174 | 2013-09-18 00:45:45 +0200 | [diff] [blame] | 48 | import android.provider.MediaStore; |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 49 | import android.util.Log; |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 50 | import android.util.Pair; |
Michael Jurka | d6dc604 | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 51 | import android.view.ActionMode; |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 52 | import android.view.LayoutInflater; |
| 53 | import android.view.Menu; |
Michael Jurka | d6dc604 | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 54 | import android.view.MenuInflater; |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 55 | import android.view.MenuItem; |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 56 | import android.view.View; |
| 57 | import android.view.View.OnClickListener; |
Michael Jurka | 12ca613 | 2013-10-15 11:14:44 -0700 | [diff] [blame] | 58 | import android.view.View.OnLayoutChangeListener; |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 59 | import android.view.ViewGroup; |
Michael Jurka | 7ad868b | 2013-12-12 15:04:25 +0100 | [diff] [blame] | 60 | import android.view.ViewPropertyAnimator; |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 61 | import android.view.ViewTreeObserver; |
| 62 | import android.view.ViewTreeObserver.OnGlobalLayoutListener; |
Selim Cinek | ffed658 | 2014-02-28 17:18:59 +0100 | [diff] [blame] | 63 | import android.view.WindowManager; |
Daniel Sandler | 24f397d | 2013-10-12 00:02:45 -0400 | [diff] [blame] | 64 | import android.view.animation.AccelerateInterpolator; |
Michael Jurka | 4b94060 | 2013-09-05 12:14:12 +0200 | [diff] [blame] | 65 | import android.view.animation.DecelerateInterpolator; |
Sunny Goyal | cdc4dbd | 2014-07-16 16:04:08 -0700 | [diff] [blame] | 66 | import android.widget.ArrayAdapter; |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 67 | import android.widget.BaseAdapter; |
| 68 | import android.widget.FrameLayout; |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 69 | import android.widget.HorizontalScrollView; |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 70 | import android.widget.ImageView; |
| 71 | import android.widget.LinearLayout; |
Filipe Gonçalves | 312cb31 | 2016-05-04 15:33:43 +0100 | [diff] [blame] | 72 | import android.widget.SeekBar; |
Michael Jurka | 619a180 | 2013-10-29 15:50:06 +0100 | [diff] [blame] | 73 | import android.widget.Toast; |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 74 | import com.android.photos.BitmapRegionTileSource; |
Michael Jurka | 619a180 | 2013-10-29 15:50:06 +0100 | [diff] [blame] | 75 | import com.android.photos.BitmapRegionTileSource.BitmapSource; |
Filipe Gonçalves | 312cb31 | 2016-05-04 15:33:43 +0100 | [diff] [blame] | 76 | import com.fairphone.fplauncher3.R; |
Michael Jurka | db84e82 | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 77 | import java.io.File; |
| 78 | import java.io.FileOutputStream; |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 79 | import java.io.IOException; |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 80 | import java.util.ArrayList; |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 81 | |
| 82 | public class WallpaperPickerActivity extends WallpaperCropActivity { |
Michael Jurka | be2f8dd | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 83 | static final String TAG = "Launcher.WallpaperPickerActivity"; |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 84 | |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 85 | 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 Jurka | d6dc604 | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 88 | private static final String TEMP_WALLPAPER_TILES = "TEMP_WALLPAPER_TILES"; |
Jorim Jaggi | 1b921ef | 2014-01-14 16:00:17 -0800 | [diff] [blame] | 89 | private static final String SELECTED_INDEX = "SELECTED_INDEX"; |
Michael Jurka | a7d82b9 | 2013-11-08 15:45:48 +0100 | [diff] [blame] | 90 | 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 Cinek | ffed658 | 2014-02-28 17:18:59 +0100 | [diff] [blame] | 92 | private static final int FLAG_POST_DELAY_MILLIS = 200; |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 93 | |
Michael Jurka | 619a180 | 2013-10-29 15:50:06 +0100 | [diff] [blame] | 94 | private View mSelectedTile; |
Michael Jurka | 4b94060 | 2013-09-05 12:14:12 +0200 | [diff] [blame] | 95 | private boolean mIgnoreNextTap; |
Michael Jurka | 2d8de58 | 2013-09-04 14:42:52 +0200 | [diff] [blame] | 96 | private OnClickListener mThumbnailOnClickListener; |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 97 | |
Michael Jurka | be2f8dd | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 98 | private LinearLayout mWallpapersView; |
Michael Jurka | 7267fa5 | 2013-09-26 15:29:57 -0700 | [diff] [blame] | 99 | private View mWallpaperStrip; |
Michael Jurka | d6dc604 | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 100 | |
| 101 | private ActionMode.Callback mActionModeCallback; |
| 102 | private ActionMode mActionMode; |
| 103 | |
Michael Jurka | be2f8dd | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 104 | private View.OnLongClickListener mLongClickListener; |
| 105 | |
| 106 | ArrayList<Uri> mTempWallpaperTiles = new ArrayList<Uri>(); |
| 107 | private SavedWallpaperImages mSavedImages; |
Michael Jurka | 998e4ff | 2013-09-18 16:04:36 +0200 | [diff] [blame] | 108 | private WallpaperInfo mLiveWallpaperInfoOnPickerLaunch; |
Selim Cinek | ffed658 | 2014-02-28 17:18:59 +0100 | [diff] [blame] | 109 | private int mSelectedIndex = -1; |
| 110 | private WallpaperInfo mLastClickedLiveWallpaperInfo; |
Filipe Gonçalves | 312cb31 | 2016-05-04 15:33:43 +0100 | [diff] [blame] | 111 | private View mDarknessStrip; |
Michael Jurka | be2f8dd | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 112 | |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 113 | public static abstract class WallpaperTileInfo { |
Michael Jurka | 6b783a0 | 2013-10-04 12:26:48 -0700 | [diff] [blame] | 114 | protected View mView; |
Sunny Goyal | cdc4dbd | 2014-07-16 16:04:08 -0700 | [diff] [blame] | 115 | public Drawable mThumb; |
| 116 | |
Michael Jurka | 6b783a0 | 2013-10-04 12:26:48 -0700 | [diff] [blame] | 117 | public void setView(View v) { |
| 118 | mView = v; |
| 119 | } |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 120 | public void onClick(WallpaperPickerActivity a) {} |
| 121 | public void onSave(WallpaperPickerActivity a) {} |
Michael Jurka | 8a34bdb | 2013-09-25 07:57:33 -0700 | [diff] [blame] | 122 | public void onDelete(WallpaperPickerActivity a) {} |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 123 | public boolean isSelectable() { return false; } |
Michael Jurka | 6b783a0 | 2013-10-04 12:26:48 -0700 | [diff] [blame] | 124 | public boolean isNamelessWallpaper() { return false; } |
| 125 | public void onIndexUpdated(CharSequence label) { |
| 126 | if (isNamelessWallpaper()) { |
| 127 | mView.setContentDescription(label); |
| 128 | } |
| 129 | } |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | public static class PickImageInfo extends WallpaperTileInfo { |
Michael Jurka | 8a34bdb | 2013-09-25 07:57:33 -0700 | [diff] [blame] | 133 | @Override |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 134 | public void onClick(WallpaperPickerActivity a) { |
| 135 | Intent intent = new Intent(Intent.ACTION_GET_CONTENT); |
| 136 | intent.setType("image/*"); |
Michael Jurka | 7ad868b | 2013-12-12 15:04:25 +0100 | [diff] [blame] | 137 | a.startActivityForResultSafely(intent, IMAGE_PICK); |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| 141 | public static class UriWallpaperInfo extends WallpaperTileInfo { |
Jose Pascoal | f354dfe | 2015-02-10 19:21:21 +0000 | [diff] [blame] | 142 | private final Uri mUri; |
Michael Jurka | 619a180 | 2013-10-29 15:50:06 +0100 | [diff] [blame] | 143 | private boolean mFirstClick = true; |
| 144 | private BitmapRegionTileSource.UriBitmapSource mBitmapSource; |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 145 | public UriWallpaperInfo(Uri uri) { |
| 146 | mUri = uri; |
| 147 | } |
Michael Jurka | 8a34bdb | 2013-09-25 07:57:33 -0700 | [diff] [blame] | 148 | @Override |
Michael Jurka | 619a180 | 2013-10-29 15:50:06 +0100 | [diff] [blame] | 149 | public void onClick(final WallpaperPickerActivity a) { |
| 150 | final Runnable onLoad; |
| 151 | if (!mFirstClick) { |
| 152 | onLoad = null; |
| 153 | } else { |
| 154 | mFirstClick = false; |
Selim Cinek | ffed658 | 2014-02-28 17:18:59 +0100 | [diff] [blame] | 155 | a.mSetWallpaperButton.setEnabled(false); |
Michael Jurka | 619a180 | 2013-10-29 15:50:06 +0100 | [diff] [blame] | 156 | onLoad = new Runnable() { |
| 157 | public void run() { |
| 158 | if (mBitmapSource != null && |
| 159 | mBitmapSource.getLoadingState() == BitmapSource.State.LOADED) { |
Michael Jurka | 619a180 | 2013-10-29 15:50:06 +0100 | [diff] [blame] | 160 | a.selectTile(mView); |
Selim Cinek | ffed658 | 2014-02-28 17:18:59 +0100 | [diff] [blame] | 161 | a.mSetWallpaperButton.setEnabled(true); |
Michael Jurka | 619a180 | 2013-10-29 15:50:06 +0100 | [diff] [blame] | 162 | } 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 Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 177 | } |
Michael Jurka | 8a34bdb | 2013-09-25 07:57:33 -0700 | [diff] [blame] | 178 | @Override |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 179 | 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 Jurka | 8713a94 | 2013-10-10 21:28:40 -0700 | [diff] [blame] | 184 | // 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 Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 187 | a.getSavedImages().writeImage(thumb, imageBytes); |
| 188 | } |
| 189 | }; |
| 190 | a.cropImageAndSetWallpaper(mUri, h, finishActivityWhenDone); |
| 191 | } |
Michael Jurka | 8a34bdb | 2013-09-25 07:57:33 -0700 | [diff] [blame] | 192 | @Override |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 193 | public boolean isSelectable() { |
| 194 | return true; |
| 195 | } |
Michael Jurka | 6b783a0 | 2013-10-04 12:26:48 -0700 | [diff] [blame] | 196 | @Override |
| 197 | public boolean isNamelessWallpaper() { |
| 198 | return true; |
| 199 | } |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 200 | } |
| 201 | |
Sunny Goyal | cdc4dbd | 2014-07-16 16:04:08 -0700 | [diff] [blame] | 202 | public static class FileWallpaperInfo extends WallpaperTileInfo { |
Jose Pascoal | f354dfe | 2015-02-10 19:21:21 +0000 | [diff] [blame] | 203 | private final File mFile; |
Sunny Goyal | cdc4dbd | 2014-07-16 16:04:08 -0700 | [diff] [blame] | 204 | |
| 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 Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 229 | public static class ResourceWallpaperInfo extends WallpaperTileInfo { |
Jose Pascoal | f354dfe | 2015-02-10 19:21:21 +0000 | [diff] [blame] | 230 | private final Resources mResources; |
| 231 | private final int mResId; |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 232 | |
| 233 | public ResourceWallpaperInfo(Resources res, int resId, Drawable thumb) { |
| 234 | mResources = res; |
| 235 | mResId = resId; |
| 236 | mThumb = thumb; |
| 237 | } |
Michael Jurka | 8a34bdb | 2013-09-25 07:57:33 -0700 | [diff] [blame] | 238 | @Override |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 239 | public void onClick(WallpaperPickerActivity a) { |
Michael Jurka | 862f7e3 | 2013-10-16 18:23:56 -0700 | [diff] [blame] | 240 | BitmapRegionTileSource.ResourceBitmapSource bitmapSource = |
Michael Jurka | 83699e5 | 2013-10-23 20:59:51 +0200 | [diff] [blame] | 241 | new BitmapRegionTileSource.ResourceBitmapSource( |
| 242 | mResources, mResId, BitmapRegionTileSource.MAX_PREVIEW_SIZE); |
Michael Jurka | 862f7e3 | 2013-10-16 18:23:56 -0700 | [diff] [blame] | 243 | bitmapSource.loadInBackground(); |
| 244 | BitmapRegionTileSource source = new BitmapRegionTileSource(a, bitmapSource); |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 245 | 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 Cinek | ffed658 | 2014-02-28 17:18:59 +0100 | [diff] [blame] | 254 | a.setSystemWallpaperVisiblity(false); |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 255 | } |
Michael Jurka | 8a34bdb | 2013-09-25 07:57:33 -0700 | [diff] [blame] | 256 | @Override |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 257 | public void onSave(WallpaperPickerActivity a) { |
| 258 | boolean finishActivityWhenDone = true; |
| 259 | a.cropImageAndSetWallpaper(mResources, mResId, finishActivityWhenDone); |
| 260 | } |
Michael Jurka | 8a34bdb | 2013-09-25 07:57:33 -0700 | [diff] [blame] | 261 | @Override |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 262 | public boolean isSelectable() { |
| 263 | return true; |
| 264 | } |
Michael Jurka | 6b783a0 | 2013-10-04 12:26:48 -0700 | [diff] [blame] | 265 | @Override |
| 266 | public boolean isNamelessWallpaper() { |
| 267 | return true; |
| 268 | } |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Sunny Goyal | cdc4dbd | 2014-07-16 16:04:08 -0700 | [diff] [blame] | 271 | @TargetApi(Build.VERSION_CODES.KITKAT) |
Michael Jurka | 12ca613 | 2013-10-15 11:14:44 -0700 | [diff] [blame] | 272 | public static class DefaultWallpaperInfo extends WallpaperTileInfo { |
Michael Jurka | 12ca613 | 2013-10-15 11:14:44 -0700 | [diff] [blame] | 273 | public DefaultWallpaperInfo(Drawable thumb) { |
| 274 | mThumb = thumb; |
| 275 | } |
| 276 | @Override |
| 277 | public void onClick(WallpaperPickerActivity a) { |
Michael Jurka | 83699e5 | 2013-10-23 20:59:51 +0200 | [diff] [blame] | 278 | CropView c = a.getCropView(); |
| 279 | |
Michael Jurka | 12ca613 | 2013-10-15 11:14:44 -0700 | [diff] [blame] | 280 | Drawable defaultWallpaper = WallpaperManager.getInstance(a).getBuiltInDrawable( |
Michael Jurka | 83699e5 | 2013-10-23 20:59:51 +0200 | [diff] [blame] | 281 | c.getWidth(), c.getHeight(), false, 0.5f, 0.5f); |
| 282 | |
Christoph Studer | 7084c60 | 2014-02-28 15:57:18 +0100 | [diff] [blame] | 283 | if (defaultWallpaper == null) { |
| 284 | Log.w(TAG, "Null default wallpaper encountered."); |
| 285 | c.setTileSource(null, null); |
| 286 | return; |
| 287 | } |
| 288 | |
Michael Jurka | 83699e5 | 2013-10-23 20:59:51 +0200 | [diff] [blame] | 289 | c.setTileSource( |
| 290 | new DrawableTileSource(a, defaultWallpaper, DrawableTileSource.MAX_PREVIEW_SIZE), null); |
| 291 | c.setScale(1f); |
| 292 | c.setTouchEnabled(false); |
Selim Cinek | ffed658 | 2014-02-28 17:18:59 +0100 | [diff] [blame] | 293 | a.setSystemWallpaperVisiblity(false); |
Michael Jurka | 12ca613 | 2013-10-15 11:14:44 -0700 | [diff] [blame] | 294 | } |
| 295 | @Override |
| 296 | public void onSave(WallpaperPickerActivity a) { |
| 297 | try { |
| 298 | WallpaperManager.getInstance(a).clear(); |
Selim Cinek | 3aa2831 | 2014-02-28 18:00:42 +0100 | [diff] [blame] | 299 | a.setResult(RESULT_OK); |
Michael Jurka | 12ca613 | 2013-10-15 11:14:44 -0700 | [diff] [blame] | 300 | } 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 Jurka | 7267fa5 | 2013-09-26 15:29:57 -0700 | [diff] [blame] | 315 | public void setWallpaperStripYOffset(float offset) { |
| 316 | mWallpaperStrip.setPadding(0, 0, 0, (int) offset); |
| 317 | } |
| 318 | |
Selim Cinek | ffed658 | 2014-02-28 17:18:59 +0100 | [diff] [blame] | 319 | /** |
| 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 Jurka | 2d8de58 | 2013-09-04 14:42:52 +0200 | [diff] [blame] | 376 | // called by onCreate; this is subclassed to overwrite WallpaperCropActivity |
| 377 | protected void init() { |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 378 | setContentView(R.layout.wallpaper_picker); |
| 379 | |
| 380 | mCropView = (CropView) findViewById(R.id.cropView); |
Selim Cinek | ffed658 | 2014-02-28 17:18:59 +0100 | [diff] [blame] | 381 | mCropView.setVisibility(View.INVISIBLE); |
| 382 | |
Filipe Gonçalves | 312cb31 | 2016-05-04 15:33:43 +0100 | [diff] [blame] | 383 | mDarknessStrip = findViewById(R.id.darkness_strip); |
Michael Jurka | 7267fa5 | 2013-09-26 15:29:57 -0700 | [diff] [blame] | 384 | mWallpaperStrip = findViewById(R.id.wallpaper_strip); |
Michael Jurka | 4b94060 | 2013-09-05 12:14:12 +0200 | [diff] [blame] | 385 | mCropView.setTouchCallback(new CropView.TouchCallback() { |
Michael Jurka | 7ad868b | 2013-12-12 15:04:25 +0100 | [diff] [blame] | 386 | ViewPropertyAnimator mAnim; |
Michael Jurka | e8115d5 | 2013-09-20 03:52:24 +0200 | [diff] [blame] | 387 | @Override |
Michael Jurka | 4b94060 | 2013-09-05 12:14:12 +0200 | [diff] [blame] | 388 | public void onTouchDown() { |
| 389 | if (mAnim != null) { |
| 390 | mAnim.cancel(); |
| 391 | } |
Daniel Sandler | 24f397d | 2013-10-12 00:02:45 -0400 | [diff] [blame] | 392 | if (mWallpaperStrip.getAlpha() == 1f) { |
Michael Jurka | 4b94060 | 2013-09-05 12:14:12 +0200 | [diff] [blame] | 393 | mIgnoreNextTap = true; |
| 394 | } |
Michael Jurka | 7ad868b | 2013-12-12 15:04:25 +0100 | [diff] [blame] | 395 | mAnim = mWallpaperStrip.animate(); |
Daniel Sandler | 24f397d | 2013-10-12 00:02:45 -0400 | [diff] [blame] | 396 | mAnim.alpha(0f) |
Michael Jurka | 7ad868b | 2013-12-12 15:04:25 +0100 | [diff] [blame] | 397 | .setDuration(150) |
| 398 | .withEndAction(new Runnable() { |
| 399 | public void run() { |
Filipe Gonçalves | 312cb31 | 2016-05-04 15:33:43 +0100 | [diff] [blame] | 400 | mDarknessStrip.setVisibility(View.INVISIBLE); |
Michael Jurka | 7ad868b | 2013-12-12 15:04:25 +0100 | [diff] [blame] | 401 | mWallpaperStrip.setVisibility(View.INVISIBLE); |
| 402 | } |
| 403 | }); |
Daniel Sandler | 24f397d | 2013-10-12 00:02:45 -0400 | [diff] [blame] | 404 | mAnim.setInterpolator(new AccelerateInterpolator(0.75f)); |
Michael Jurka | 4b94060 | 2013-09-05 12:14:12 +0200 | [diff] [blame] | 405 | mAnim.start(); |
| 406 | } |
Michael Jurka | e8115d5 | 2013-09-20 03:52:24 +0200 | [diff] [blame] | 407 | @Override |
| 408 | public void onTouchUp() { |
| 409 | mIgnoreNextTap = false; |
| 410 | } |
| 411 | @Override |
Michael Jurka | 4b94060 | 2013-09-05 12:14:12 +0200 | [diff] [blame] | 412 | public void onTap() { |
| 413 | boolean ignoreTap = mIgnoreNextTap; |
| 414 | mIgnoreNextTap = false; |
| 415 | if (!ignoreTap) { |
| 416 | if (mAnim != null) { |
| 417 | mAnim.cancel(); |
| 418 | } |
Filipe Gonçalves | 312cb31 | 2016-05-04 15:33:43 +0100 | [diff] [blame] | 419 | mDarknessStrip.setVisibility(View.VISIBLE); |
Daniel Sandler | 24f397d | 2013-10-12 00:02:45 -0400 | [diff] [blame] | 420 | mWallpaperStrip.setVisibility(View.VISIBLE); |
Michael Jurka | 7ad868b | 2013-12-12 15:04:25 +0100 | [diff] [blame] | 421 | mAnim = mWallpaperStrip.animate(); |
Daniel Sandler | 24f397d | 2013-10-12 00:02:45 -0400 | [diff] [blame] | 422 | mAnim.alpha(1f) |
| 423 | .setDuration(150) |
| 424 | .setInterpolator(new DecelerateInterpolator(0.75f)); |
Michael Jurka | 4b94060 | 2013-09-05 12:14:12 +0200 | [diff] [blame] | 425 | mAnim.start(); |
| 426 | } |
| 427 | } |
| 428 | }); |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 429 | |
Michael Jurka | 2d8de58 | 2013-09-04 14:42:52 +0200 | [diff] [blame] | 430 | mThumbnailOnClickListener = new OnClickListener() { |
| 431 | public void onClick(View v) { |
Michael Jurka | d6dc604 | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 432 | 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 Cinek | ffed658 | 2014-02-28 17:18:59 +0100 | [diff] [blame] | 439 | mSetWallpaperButton.setEnabled(true); |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 440 | WallpaperTileInfo info = (WallpaperTileInfo) v.getTag(); |
Michael Jurka | 619a180 | 2013-10-29 15:50:06 +0100 | [diff] [blame] | 441 | if (info.isSelectable() && v.getVisibility() == View.VISIBLE) { |
| 442 | selectTile(v); |
Michael Jurka | 2d8de58 | 2013-09-04 14:42:52 +0200 | [diff] [blame] | 443 | } |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 444 | info.onClick(WallpaperPickerActivity.this); |
Michael Jurka | 2d8de58 | 2013-09-04 14:42:52 +0200 | [diff] [blame] | 445 | } |
| 446 | }; |
Michael Jurka | d6dc604 | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 447 | 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 Jurka | 2d8de58 | 2013-09-04 14:42:52 +0200 | [diff] [blame] | 466 | |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 467 | // Populate the built-in wallpapers |
Sunny Goyal | cdc4dbd | 2014-07-16 16:04:08 -0700 | [diff] [blame] | 468 | ArrayList<WallpaperTileInfo> wallpapers = findBundledWallpapers(); |
Michael Jurka | be2f8dd | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 469 | mWallpapersView = (LinearLayout) findViewById(R.id.wallpaper_list); |
Sunny Goyal | cdc4dbd | 2014-07-16 16:04:08 -0700 | [diff] [blame] | 470 | SimpleWallpapersAdapter ia = new SimpleWallpapersAdapter(this, wallpapers); |
Michael Jurka | 12ca613 | 2013-10-15 11:14:44 -0700 | [diff] [blame] | 471 | populateWallpapersFromAdapter(mWallpapersView, ia, false); |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 472 | |
Michael Jurka | be2f8dd | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 473 | // Populate the saved wallpapers |
| 474 | mSavedImages = new SavedWallpaperImages(this); |
| 475 | mSavedImages.loadThumbnailsAndImageIdList(); |
Michael Jurka | 12ca613 | 2013-10-15 11:14:44 -0700 | [diff] [blame] | 476 | populateWallpapersFromAdapter(mWallpapersView, mSavedImages, true); |
Michael Jurka | 998e4ff | 2013-09-18 16:04:36 +0200 | [diff] [blame] | 477 | |
| 478 | // Populate the live wallpapers |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 479 | final LinearLayout liveWallpapersView = |
| 480 | (LinearLayout) findViewById(R.id.live_wallpaper_list); |
Michael Jurka | 998e4ff | 2013-09-18 16:04:36 +0200 | [diff] [blame] | 481 | final LiveWallpaperListAdapter a = new LiveWallpaperListAdapter(this); |
| 482 | a.registerDataSetObserver(new DataSetObserver() { |
| 483 | public void onChanged() { |
| 484 | liveWallpapersView.removeAllViews(); |
Michael Jurka | 12ca613 | 2013-10-15 11:14:44 -0700 | [diff] [blame] | 485 | populateWallpapersFromAdapter(liveWallpapersView, a, false); |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 486 | initializeScrollForRtl(); |
Michael Jurka | 6b783a0 | 2013-10-04 12:26:48 -0700 | [diff] [blame] | 487 | updateTileIndices(); |
Michael Jurka | 998e4ff | 2013-09-18 16:04:36 +0200 | [diff] [blame] | 488 | } |
| 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 Jurka | 12ca613 | 2013-10-15 11:14:44 -0700 | [diff] [blame] | 496 | populateWallpapersFromAdapter(thirdPartyWallpapersView, ta, false); |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 497 | |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 498 | // Add a tile for the Gallery |
Michael Jurka | 998e4ff | 2013-09-18 16:04:36 +0200 | [diff] [blame] | 499 | LinearLayout masterWallpaperList = (LinearLayout) findViewById(R.id.master_wallpaper_list); |
Michael Jurka | 6b783a0 | 2013-10-04 12:26:48 -0700 | [diff] [blame] | 500 | FrameLayout pickImageTile = (FrameLayout) getLayoutInflater(). |
Michael Jurka | 5b4b690 | 2013-10-03 18:15:08 -0700 | [diff] [blame] | 501 | inflate(R.layout.wallpaper_picker_image_picker_item, masterWallpaperList, false); |
Michael Jurka | 6b783a0 | 2013-10-04 12:26:48 -0700 | [diff] [blame] | 502 | setWallpaperItemPaddingToZero(pickImageTile); |
| 503 | masterWallpaperList.addView(pickImageTile, 0); |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 504 | |
Michael Jurka | 42a7174 | 2013-09-18 00:45:45 +0200 | [diff] [blame] | 505 | // Make its background the last photo taken on external storage |
| 506 | Bitmap lastPhoto = getThumbnailOfLastPhoto(); |
| 507 | if (lastPhoto != null) { |
| 508 | ImageView galleryThumbnailBg = |
Michael Jurka | 6b783a0 | 2013-10-04 12:26:48 -0700 | [diff] [blame] | 509 | (ImageView) pickImageTile.findViewById(R.id.wallpaper_image); |
Michael Jurka | 42a7174 | 2013-09-18 00:45:45 +0200 | [diff] [blame] | 510 | galleryThumbnailBg.setImageBitmap(getThumbnailOfLastPhoto()); |
Michael Jurka | 998e4ff | 2013-09-18 16:04:36 +0200 | [diff] [blame] | 511 | int colorOverlay = getResources().getColor(R.color.wallpaper_picker_translucent_gray); |
| 512 | galleryThumbnailBg.setColorFilter(colorOverlay, PorterDuff.Mode.SRC_ATOP); |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 513 | |
Michael Jurka | 42a7174 | 2013-09-18 00:45:45 +0200 | [diff] [blame] | 514 | } |
| 515 | |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 516 | PickImageInfo pickImageInfo = new PickImageInfo(); |
Michael Jurka | 6b783a0 | 2013-10-04 12:26:48 -0700 | [diff] [blame] | 517 | pickImageTile.setTag(pickImageInfo); |
| 518 | pickImageInfo.setView(pickImageTile); |
| 519 | pickImageTile.setOnClickListener(mThumbnailOnClickListener); |
Michael Jurka | 12ca613 | 2013-10-15 11:14:44 -0700 | [diff] [blame] | 520 | |
Michael Jurka | 12ca613 | 2013-10-15 11:14:44 -0700 | [diff] [blame] | 521 | // Select the first item; wait for a layout pass so that we initialize the dimensions of |
| 522 | // cropView or the defaultWallpaperView first |
Michael Jurka | 83699e5 | 2013-10-23 20:59:51 +0200 | [diff] [blame] | 523 | mCropView.addOnLayoutChangeListener(new OnLayoutChangeListener() { |
Michael Jurka | 12ca613 | 2013-10-15 11:14:44 -0700 | [diff] [blame] | 524 | @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 Jaggi | 1b921ef | 2014-01-14 16:00:17 -0800 | [diff] [blame] | 528 | if (mSelectedIndex >= 0 && mSelectedIndex < mWallpapersView.getChildCount()) { |
| 529 | mThumbnailOnClickListener.onClick( |
| 530 | mWallpapersView.getChildAt(mSelectedIndex)); |
Selim Cinek | ffed658 | 2014-02-28 17:18:59 +0100 | [diff] [blame] | 531 | setSystemWallpaperVisiblity(false); |
Michael Jurka | 12ca613 | 2013-10-15 11:14:44 -0700 | [diff] [blame] | 532 | } |
| 533 | v.removeOnLayoutChangeListener(this); |
| 534 | } |
| 535 | } |
| 536 | }); |
Michael Jurka | 6b783a0 | 2013-10-04 12:26:48 -0700 | [diff] [blame] | 537 | |
| 538 | updateTileIndices(); |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 539 | |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 540 | // Update the scroll for RTL |
| 541 | initializeScrollForRtl(); |
| 542 | |
Michael Jurka | d6dc604 | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 543 | // 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 Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 550 | // 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 Jurka | 619a180 | 2013-10-29 15:50:06 +0100 | [diff] [blame] | 558 | if (mSelectedTile != null) { |
| 559 | WallpaperTileInfo info = (WallpaperTileInfo) mSelectedTile.getTag(); |
Michael Jurka | b92f433 | 2013-09-24 12:53:49 +0100 | [diff] [blame] | 560 | info.onSave(WallpaperPickerActivity.this); |
Selim Cinek | ffed658 | 2014-02-28 17:18:59 +0100 | [diff] [blame] | 561 | } else { |
Filipe Gonçalves | 312cb31 | 2016-05-04 15:33:43 +0100 | [diff] [blame] | 562 | 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 Cinek | ffed658 | 2014-02-28 17:18:59 +0100 | [diff] [blame] | 568 | // no tile was selected, so we just finish the activity and go back |
| 569 | setResult(Activity.RESULT_OK); |
| 570 | finish(); |
Michael Jurka | b92f433 | 2013-09-24 12:53:49 +0100 | [diff] [blame] | 571 | } |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 572 | } |
| 573 | }); |
Michael Jurka | 849aff7 | 2014-01-16 14:58:00 +0100 | [diff] [blame] | 574 | mSetWallpaperButton = findViewById(R.id.set_wallpaper_button); |
Michael Jurka | d6dc604 | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 575 | |
| 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 Bringert | c639f8c | 2013-09-13 13:39:12 +0100 | [diff] [blame] | 617 | int itemId = item.getItemId(); |
| 618 | if (itemId == R.id.menu_delete) { |
| 619 | int childCount = mWallpapersView.getChildCount(); |
| 620 | ArrayList<View> viewsToRemove = new ArrayList<View>(); |
Selim Cinek | ffed658 | 2014-02-28 17:18:59 +0100 | [diff] [blame] | 621 | boolean selectedTileRemoved = false; |
Bjorn Bringert | c639f8c | 2013-09-13 13:39:12 +0100 | [diff] [blame] | 622 | for (int i = 0; i < childCount; i++) { |
| 623 | CheckableFrameLayout c = |
| 624 | (CheckableFrameLayout) mWallpapersView.getChildAt(i); |
| 625 | if (c.isChecked()) { |
Michael Jurka | 8a34bdb | 2013-09-25 07:57:33 -0700 | [diff] [blame] | 626 | WallpaperTileInfo info = (WallpaperTileInfo) c.getTag(); |
| 627 | info.onDelete(WallpaperPickerActivity.this); |
Bjorn Bringert | c639f8c | 2013-09-13 13:39:12 +0100 | [diff] [blame] | 628 | viewsToRemove.add(c); |
Selim Cinek | ffed658 | 2014-02-28 17:18:59 +0100 | [diff] [blame] | 629 | if (i == mSelectedIndex) { |
| 630 | selectedTileRemoved = true; |
| 631 | } |
Michael Jurka | d6dc604 | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 632 | } |
Bjorn Bringert | c639f8c | 2013-09-13 13:39:12 +0100 | [diff] [blame] | 633 | } |
| 634 | for (View v : viewsToRemove) { |
| 635 | mWallpapersView.removeView(v); |
| 636 | } |
Selim Cinek | ffed658 | 2014-02-28 17:18:59 +0100 | [diff] [blame] | 637 | if (selectedTileRemoved) { |
| 638 | mSelectedIndex = -1; |
| 639 | mSelectedTile = null; |
| 640 | setSystemWallpaperVisiblity(true); |
| 641 | } |
Michael Jurka | 6b783a0 | 2013-10-04 12:26:48 -0700 | [diff] [blame] | 642 | updateTileIndices(); |
Bjorn Bringert | c639f8c | 2013-09-13 13:39:12 +0100 | [diff] [blame] | 643 | mode.finish(); // Action picked, so close the CAB |
| 644 | return true; |
| 645 | } else { |
| 646 | return false; |
Michael Jurka | d6dc604 | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 647 | } |
| 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 Cinek | ffed658 | 2014-02-28 17:18:59 +0100 | [diff] [blame] | 658 | if (mSelectedTile != null) { |
| 659 | mSelectedTile.setSelected(true); |
| 660 | } |
Michael Jurka | d6dc604 | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 661 | mActionMode = null; |
| 662 | } |
| 663 | }; |
Filipe Gonçalves | 312cb31 | 2016-05-04 15:33:43 +0100 | [diff] [blame] | 664 | 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 Jurka | d6dc604 | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 686 | } |
Michael Jurka | 619a180 | 2013-10-29 15:50:06 +0100 | [diff] [blame] | 687 | |
| 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 Jaggi | 1b921ef | 2014-01-14 16:00:17 -0800 | [diff] [blame] | 695 | mSelectedIndex = mWallpapersView.indexOfChild(v); |
Michael Jurka | 619a180 | 2013-10-29 15:50:06 +0100 | [diff] [blame] | 696 | // 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 Jurka | 862f7e3 | 2013-10-16 18:23:56 -0700 | [diff] [blame] | 700 | } |
Michael Jurka | 42a7174 | 2013-09-18 00:45:45 +0200 | [diff] [blame] | 701 | |
Michael Jurka | fe0ace3 | 2013-10-03 01:05:14 -0700 | [diff] [blame] | 702 | 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 Jurka | 42a7174 | 2013-09-18 00:45:45 +0200 | [diff] [blame] | 719 | 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 Cohen | c8d31ff | 2014-06-12 12:29:50 -0700 | [diff] [blame] | 725 | |
Michael Jurka | 42a7174 | 2013-09-18 00:45:45 +0200 | [diff] [blame] | 726 | Bitmap thumb = null; |
Adam Cohen | c8d31ff | 2014-06-12 12:29:50 -0700 | [diff] [blame] | 727 | 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 Jurka | 42a7174 | 2013-09-18 00:45:45 +0200 | [diff] [blame] | 734 | } |
Michael Jurka | 42a7174 | 2013-09-18 00:45:45 +0200 | [diff] [blame] | 735 | return thumb; |
| 736 | } |
Michael Jurka | 5664de2 | 2013-09-12 02:53:22 +0200 | [diff] [blame] | 737 | |
| 738 | protected void onStop() { |
| 739 | super.onStop(); |
Michael Jurka | 7267fa5 | 2013-09-26 15:29:57 -0700 | [diff] [blame] | 740 | mWallpaperStrip = findViewById(R.id.wallpaper_strip); |
Daniel Sandler | 24f397d | 2013-10-12 00:02:45 -0400 | [diff] [blame] | 741 | if (mWallpaperStrip.getAlpha() < 1f) { |
Michael Jurka | 7267fa5 | 2013-09-26 15:29:57 -0700 | [diff] [blame] | 742 | mWallpaperStrip.setAlpha(1f); |
Daniel Sandler | 24f397d | 2013-10-12 00:02:45 -0400 | [diff] [blame] | 743 | mWallpaperStrip.setVisibility(View.VISIBLE); |
Michael Jurka | 5664de2 | 2013-09-12 02:53:22 +0200 | [diff] [blame] | 744 | } |
| 745 | } |
| 746 | |
Michael Jurka | d6dc604 | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 747 | protected void onSaveInstanceState(Bundle outState) { |
| 748 | outState.putParcelableArrayList(TEMP_WALLPAPER_TILES, mTempWallpaperTiles); |
Jorim Jaggi | 1b921ef | 2014-01-14 16:00:17 -0800 | [diff] [blame] | 749 | outState.putInt(SELECTED_INDEX, mSelectedIndex); |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 750 | } |
| 751 | |
Michael Jurka | d6dc604 | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 752 | protected void onRestoreInstanceState(Bundle savedInstanceState) { |
| 753 | ArrayList<Uri> uris = savedInstanceState.getParcelableArrayList(TEMP_WALLPAPER_TILES); |
| 754 | for (Uri uri : uris) { |
Jorim Jaggi | 1b921ef | 2014-01-14 16:00:17 -0800 | [diff] [blame] | 755 | addTemporaryWallpaperTile(uri, true); |
Michael Jurka | d6dc604 | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 756 | } |
Selim Cinek | ffed658 | 2014-02-28 17:18:59 +0100 | [diff] [blame] | 757 | mSelectedIndex = savedInstanceState.getInt(SELECTED_INDEX, -1); |
Michael Jurka | d6dc604 | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 758 | } |
Michael Jurka | be2f8dd | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 759 | |
Michael Jurka | 998e4ff | 2013-09-18 16:04:36 +0200 | [diff] [blame] | 760 | private void populateWallpapersFromAdapter(ViewGroup parent, BaseAdapter adapter, |
Michael Jurka | 12ca613 | 2013-10-15 11:14:44 -0700 | [diff] [blame] | 761 | boolean addLongPressHandler) { |
Michael Jurka | 998e4ff | 2013-09-18 16:04:36 +0200 | [diff] [blame] | 762 | for (int i = 0; i < adapter.getCount(); i++) { |
| 763 | FrameLayout thumbnail = (FrameLayout) adapter.getView(i, null, parent); |
Michael Jurka | be2f8dd | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 764 | parent.addView(thumbnail, i); |
Michael Jurka | 6b783a0 | 2013-10-04 12:26:48 -0700 | [diff] [blame] | 765 | WallpaperTileInfo info = (WallpaperTileInfo) adapter.getItem(i); |
| 766 | thumbnail.setTag(info); |
| 767 | info.setView(thumbnail); |
Michael Jurka | be2f8dd | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 768 | if (addLongPressHandler) { |
| 769 | addLongPressHandler(thumbnail); |
| 770 | } |
| 771 | thumbnail.setOnClickListener(mThumbnailOnClickListener); |
Michael Jurka | be2f8dd | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 772 | } |
| 773 | } |
| 774 | |
Michael Jurka | 6b783a0 | 2013-10-04 12:26:48 -0700 | [diff] [blame] | 775 | private void updateTileIndices() { |
| 776 | LinearLayout masterWallpaperList = (LinearLayout) findViewById(R.id.master_wallpaper_list); |
| 777 | final int childCount = masterWallpaperList.getChildCount(); |
Michael Jurka | 6b783a0 | 2013-10-04 12:26:48 -0700 | [diff] [blame] | 778 | 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 Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 816 | 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 Jurka | 8713a94 | 2013-10-10 21:28:40 -0700 | [diff] [blame] | 823 | Resources res, int resId, int rotation, boolean leftAligned) { |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 824 | int width = size.x; |
| 825 | int height = size.y; |
Michael Jurka | be2f8dd | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 826 | |
| 827 | BitmapCropTask cropTask; |
| 828 | if (uri != null) { |
Michael Jurka | 8713a94 | 2013-10-10 21:28:40 -0700 | [diff] [blame] | 829 | cropTask = new BitmapCropTask( |
| 830 | context, uri, null, rotation, width, height, false, true, null); |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 831 | } else if (imageBytes != null) { |
Michael Jurka | 8713a94 | 2013-10-10 21:28:40 -0700 | [diff] [blame] | 832 | cropTask = new BitmapCropTask( |
| 833 | imageBytes, null, rotation, width, height, false, true, null); |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 834 | } else { |
Michael Jurka | 8713a94 | 2013-10-10 21:28:40 -0700 | [diff] [blame] | 835 | cropTask = new BitmapCropTask( |
| 836 | context, res, resId, null, rotation, width, height, false, true, null); |
Michael Jurka | be2f8dd | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 837 | } |
| 838 | Point bounds = cropTask.getImageBounds(); |
Michael Jurka | db84e82 | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 839 | if (bounds == null || bounds.x == 0 || bounds.y == 0) { |
Michael Jurka | be2f8dd | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 840 | return null; |
| 841 | } |
| 842 | |
Michael Jurka | 8713a94 | 2013-10-10 21:28:40 -0700 | [diff] [blame] | 843 | Matrix rotateMatrix = new Matrix(); |
| 844 | rotateMatrix.setRotate(rotation); |
Jose Pascoal | 0d9e9d0 | 2015-02-10 15:55:39 +0000 | [diff] [blame] | 845 | float[] rotatedBounds = { bounds.x, bounds.y }; |
Michael Jurka | 8713a94 | 2013-10-10 21:28:40 -0700 | [diff] [blame] | 846 | rotateMatrix.mapPoints(rotatedBounds); |
| 847 | rotatedBounds[0] = Math.abs(rotatedBounds[0]); |
| 848 | rotatedBounds[1] = Math.abs(rotatedBounds[1]); |
| 849 | |
Michael Jurka | be2f8dd | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 850 | RectF cropRect = WallpaperCropActivity.getMaxCropRect( |
Michael Jurka | 8713a94 | 2013-10-10 21:28:40 -0700 | [diff] [blame] | 851 | (int) rotatedBounds[0], (int) rotatedBounds[1], width, height, leftAligned); |
Michael Jurka | be2f8dd | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 852 | cropTask.setCropBounds(cropRect); |
| 853 | |
| 854 | if (cropTask.cropBitmap()) { |
| 855 | return cropTask.getCroppedBitmap(); |
| 856 | } else { |
| 857 | return null; |
| 858 | } |
| 859 | } |
| 860 | |
Jorim Jaggi | 1b921ef | 2014-01-14 16:00:17 -0800 | [diff] [blame] | 861 | private void addTemporaryWallpaperTile(final Uri uri, boolean fromRestore) { |
Michael Jurka | be2f8dd | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 862 | mTempWallpaperTiles.add(uri); |
| 863 | // Add a tile for the image picked from Gallery |
Michael Jurka | 619a180 | 2013-10-29 15:50:06 +0100 | [diff] [blame] | 864 | final FrameLayout pickedImageThumbnail = (FrameLayout) getLayoutInflater(). |
Michael Jurka | be2f8dd | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 865 | inflate(R.layout.wallpaper_picker_item, mWallpapersView, false); |
Michael Jurka | 619a180 | 2013-10-29 15:50:06 +0100 | [diff] [blame] | 866 | pickedImageThumbnail.setVisibility(View.GONE); |
Michael Jurka | be2f8dd | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 867 | setWallpaperItemPaddingToZero(pickedImageThumbnail); |
Michael Jurka | 862f7e3 | 2013-10-16 18:23:56 -0700 | [diff] [blame] | 868 | mWallpapersView.addView(pickedImageThumbnail, 0); |
Michael Jurka | be2f8dd | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 869 | |
| 870 | // Load the thumbnail |
Michael Jurka | 862f7e3 | 2013-10-16 18:23:56 -0700 | [diff] [blame] | 871 | 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 Cohen | 8dc6e1b | 2014-07-24 10:39:57 -0700 | [diff] [blame] | 876 | 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 Jurka | 862f7e3 | 2013-10-16 18:23:56 -0700 | [diff] [blame] | 893 | } |
| 894 | protected void onPostExecute(Bitmap thumb) { |
Adam Cohen | 8dc6e1b | 2014-07-24 10:39:57 -0700 | [diff] [blame] | 895 | if (!isCancelled() && thumb != null) { |
Michael Jurka | 862f7e3 | 2013-10-16 18:23:56 -0700 | [diff] [blame] | 896 | image.setImageBitmap(thumb); |
| 897 | Drawable thumbDrawable = image.getDrawable(); |
| 898 | thumbDrawable.setDither(true); |
Jorim Jaggi | 1b921ef | 2014-01-14 16:00:17 -0800 | [diff] [blame] | 899 | pickedImageThumbnail.setVisibility(View.VISIBLE); |
Michael Jurka | 862f7e3 | 2013-10-16 18:23:56 -0700 | [diff] [blame] | 900 | } else { |
| 901 | Log.e(TAG, "Error loading thumbnail for uri=" + uri); |
| 902 | } |
| 903 | } |
| 904 | }.execute(); |
Michael Jurka | be2f8dd | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 905 | |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 906 | UriWallpaperInfo info = new UriWallpaperInfo(uri); |
| 907 | pickedImageThumbnail.setTag(info); |
Michael Jurka | 6b783a0 | 2013-10-04 12:26:48 -0700 | [diff] [blame] | 908 | info.setView(pickedImageThumbnail); |
Michael Jurka | 8713a94 | 2013-10-10 21:28:40 -0700 | [diff] [blame] | 909 | addLongPressHandler(pickedImageThumbnail); |
Michael Jurka | 7b71549 | 2013-10-09 11:22:34 -0700 | [diff] [blame] | 910 | updateTileIndices(); |
Michael Jurka | be2f8dd | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 911 | pickedImageThumbnail.setOnClickListener(mThumbnailOnClickListener); |
Jorim Jaggi | 1b921ef | 2014-01-14 16:00:17 -0800 | [diff] [blame] | 912 | if (!fromRestore) { |
| 913 | mThumbnailOnClickListener.onClick(pickedImageThumbnail); |
| 914 | } |
Michael Jurka | be2f8dd | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 915 | } |
| 916 | |
Michael Jurka | 2d8de58 | 2013-09-04 14:42:52 +0200 | [diff] [blame] | 917 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 918 | if (requestCode == IMAGE_PICK && resultCode == RESULT_OK) { |
Michael Jurka | 2fe1530 | 2013-10-04 12:35:27 -0700 | [diff] [blame] | 919 | if (data != null && data.getData() != null) { |
| 920 | Uri uri = data.getData(); |
Jorim Jaggi | 1b921ef | 2014-01-14 16:00:17 -0800 | [diff] [blame] | 921 | addTemporaryWallpaperTile(uri, false); |
Michael Jurka | 2fe1530 | 2013-10-04 12:35:27 -0700 | [diff] [blame] | 922 | } |
Michael Jurka | 2d8de58 | 2013-09-04 14:42:52 +0200 | [diff] [blame] | 923 | } else if (requestCode == PICK_WALLPAPER_THIRD_PARTY_ACTIVITY) { |
Michael Jurka | 2d8de58 | 2013-09-04 14:42:52 +0200 | [diff] [blame] | 924 | setResult(RESULT_OK); |
| 925 | finish(); |
Michael Jurka | 998e4ff | 2013-09-18 16:04:36 +0200 | [diff] [blame] | 926 | } else if (requestCode == PICK_LIVE_WALLPAPER) { |
| 927 | WallpaperManager wm = WallpaperManager.getInstance(this); |
| 928 | final WallpaperInfo oldLiveWallpaper = mLiveWallpaperInfoOnPickerLaunch; |
Selim Cinek | ffed658 | 2014-02-28 17:18:59 +0100 | [diff] [blame] | 929 | final WallpaperInfo clickedWallpaper = mLastClickedLiveWallpaperInfo; |
Michael Jurka | 998e4ff | 2013-09-18 16:04:36 +0200 | [diff] [blame] | 930 | WallpaperInfo newLiveWallpaper = wm.getWallpaperInfo(); |
| 931 | // Try to figure out if a live wallpaper was set; |
| 932 | if (newLiveWallpaper != null && |
Selim Cinek | ffed658 | 2014-02-28 17:18:59 +0100 | [diff] [blame] | 933 | (oldLiveWallpaper == null |
| 934 | || !oldLiveWallpaper.getComponent() |
| 935 | .equals(newLiveWallpaper.getComponent()) |
| 936 | || clickedWallpaper.getComponent() |
| 937 | .equals(oldLiveWallpaper.getComponent()))) { |
Michael Jurka | 998e4ff | 2013-09-18 16:04:36 +0200 | [diff] [blame] | 938 | // Return if a live wallpaper was set |
| 939 | setResult(RESULT_OK); |
| 940 | finish(); |
| 941 | } |
Michael Jurka | 2d8de58 | 2013-09-04 14:42:52 +0200 | [diff] [blame] | 942 | } |
| 943 | } |
| 944 | |
Michael Jurka | 998e4ff | 2013-09-18 16:04:36 +0200 | [diff] [blame] | 945 | static void setWallpaperItemPaddingToZero(FrameLayout frameLayout) { |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 946 | frameLayout.setPadding(0, 0, 0, 0); |
| 947 | frameLayout.setForeground(new ZeroPaddingDrawable(frameLayout.getForeground())); |
| 948 | } |
| 949 | |
Michael Jurka | be2f8dd | 2013-09-10 13:39:59 +0200 | [diff] [blame] | 950 | private void addLongPressHandler(View v) { |
| 951 | v.setOnLongClickListener(mLongClickListener); |
| 952 | } |
| 953 | |
Sunny Goyal | cdc4dbd | 2014-07-16 16:04:08 -0700 | [diff] [blame] | 954 | private ArrayList<WallpaperTileInfo> findBundledWallpapers() { |
Jeff Sharkey | 5aeef58 | 2014-04-14 13:26:42 -0700 | [diff] [blame] | 955 | final PackageManager pm = getPackageManager(); |
Sunny Goyal | cdc4dbd | 2014-07-16 16:04:08 -0700 | [diff] [blame] | 956 | final ArrayList<WallpaperTileInfo> bundled = new ArrayList<WallpaperTileInfo>(24); |
Jeff Sharkey | 5aeef58 | 2014-04-14 13:26:42 -0700 | [diff] [blame] | 957 | |
| 958 | Partner partner = Partner.get(pm); |
| 959 | if (partner != null) { |
| 960 | final Resources partnerRes = partner.getResources(); |
Adam Cohen | 4ae96ce | 2014-08-29 15:05:48 -0700 | [diff] [blame] | 961 | final int resId = partnerRes.getIdentifier(Partner.RES_WALLPAPERS, "array", |
Jeff Sharkey | 5aeef58 | 2014-04-14 13:26:42 -0700 | [diff] [blame] | 962 | partner.getPackageName()); |
| 963 | if (resId != 0) { |
| 964 | addWallpapers(bundled, partnerRes, partner.getPackageName(), resId); |
| 965 | } |
Sunny Goyal | cdc4dbd | 2014-07-16 16:04:08 -0700 | [diff] [blame] | 966 | |
| 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 Pascoal | 0521820 | 2014-11-19 11:17:38 +0000 | [diff] [blame] | 990 | bundled.add(new FileWallpaperInfo(file, new BitmapDrawable(getResources(), thumb))); |
Sunny Goyal | cdc4dbd | 2014-07-16 16:04:08 -0700 | [diff] [blame] | 991 | } |
| 992 | } |
| 993 | } |
Jeff Sharkey | 5aeef58 | 2014-04-14 13:26:42 -0700 | [diff] [blame] | 994 | } |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 995 | |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 996 | Pair<ApplicationInfo, Integer> r = getWallpaperArrayResourceId(); |
| 997 | if (r != null) { |
| 998 | try { |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 999 | Resources wallpaperRes = getPackageManager().getResourcesForApplication(r.first); |
Jeff Sharkey | 5aeef58 | 2014-04-14 13:26:42 -0700 | [diff] [blame] | 1000 | addWallpapers(bundled, wallpaperRes, r.first.packageName, r.second); |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 1001 | } catch (PackageManager.NameNotFoundException e) { |
| 1002 | } |
| 1003 | } |
Michael Jurka | db84e82 | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 1004 | |
Sunny Goyal | cdc4dbd | 2014-07-16 16:04:08 -0700 | [diff] [blame] | 1005 | 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 Jurka | 12ca613 | 2013-10-15 11:14:44 -0700 | [diff] [blame] | 1011 | if (defaultWallpaperInfo != null) { |
Jeff Sharkey | 5aeef58 | 2014-04-14 13:26:42 -0700 | [diff] [blame] | 1012 | bundled.add(0, defaultWallpaperInfo); |
Michael Jurka | 12ca613 | 2013-10-15 11:14:44 -0700 | [diff] [blame] | 1013 | } |
Michael Jurka | db84e82 | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 1014 | } |
Jeff Sharkey | 5aeef58 | 2014-04-14 13:26:42 -0700 | [diff] [blame] | 1015 | return bundled; |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 1016 | } |
| 1017 | |
Michael Jurka | 12ca613 | 2013-10-15 11:14:44 -0700 | [diff] [blame] | 1018 | 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 Goyal | 0af0d84 | 2014-08-13 12:12:48 -0700 | [diff] [blame] | 1033 | 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 Jurka | 12ca613 | 2013-10-15 11:14:44 -0700 | [diff] [blame] | 1049 | private ResourceWallpaperInfo getPreKKDefaultWallpaperInfo() { |
Michael Jurka | db84e82 | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 1050 | Resources sysRes = Resources.getSystem(); |
| 1051 | int resId = sysRes.getIdentifier("default_wallpaper", "drawable", "android"); |
| 1052 | |
Sunny Goyal | 0af0d84 | 2014-08-13 12:12:48 -0700 | [diff] [blame] | 1053 | File defaultThumbFile = getDefaultThumbFile(); |
Jose Pascoal | fe3b08b | 2015-02-11 18:01:38 +0000 | [diff] [blame] | 1054 | Bitmap thumb; |
Michael Jurka | db84e82 | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 1055 | boolean defaultWallpaperExists = false; |
| 1056 | if (defaultThumbFile.exists()) { |
| 1057 | thumb = BitmapFactory.decodeFile(defaultThumbFile.getAbsolutePath()); |
| 1058 | defaultWallpaperExists = true; |
| 1059 | } else { |
Michael Jurka | 8713a94 | 2013-10-10 21:28:40 -0700 | [diff] [blame] | 1060 | 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 Jurka | db84e82 | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 1065 | if (thumb != null) { |
Sunny Goyal | 0af0d84 | 2014-08-13 12:12:48 -0700 | [diff] [blame] | 1066 | defaultWallpaperExists = saveDefaultWallpaperThumb(thumb); |
Michael Jurka | db84e82 | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 1067 | } |
| 1068 | } |
| 1069 | if (defaultWallpaperExists) { |
| 1070 | return new ResourceWallpaperInfo(sysRes, resId, new BitmapDrawable(thumb)); |
| 1071 | } |
| 1072 | return null; |
| 1073 | } |
| 1074 | |
Sunny Goyal | cdc4dbd | 2014-07-16 16:04:08 -0700 | [diff] [blame] | 1075 | @TargetApi(Build.VERSION_CODES.KITKAT) |
Michael Jurka | 12ca613 | 2013-10-15 11:14:44 -0700 | [diff] [blame] | 1076 | private DefaultWallpaperInfo getDefaultWallpaper() { |
Sunny Goyal | 0af0d84 | 2014-08-13 12:12:48 -0700 | [diff] [blame] | 1077 | File defaultThumbFile = getDefaultThumbFile(); |
Michael Jurka | 12ca613 | 2013-10-15 11:14:44 -0700 | [diff] [blame] | 1078 | 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 Jurka | 12ca613 | 2013-10-15 11:14:44 -0700 | [diff] [blame] | 1086 | 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 Jurka | a7d82b9 | 2013-11-08 15:45:48 +0100 | [diff] [blame] | 1092 | wallpaperDrawable.setBounds(0, 0, defaultThumbSize.x, defaultThumbSize.y); |
Michael Jurka | 12ca613 | 2013-10-15 11:14:44 -0700 | [diff] [blame] | 1093 | wallpaperDrawable.draw(c); |
| 1094 | c.setBitmap(null); |
| 1095 | } |
| 1096 | if (thumb != null) { |
Sunny Goyal | 0af0d84 | 2014-08-13 12:12:48 -0700 | [diff] [blame] | 1097 | defaultWallpaperExists = saveDefaultWallpaperThumb(thumb); |
Michael Jurka | 12ca613 | 2013-10-15 11:14:44 -0700 | [diff] [blame] | 1098 | } |
| 1099 | } |
| 1100 | if (defaultWallpaperExists) { |
| 1101 | return new DefaultWallpaperInfo(new BitmapDrawable(thumb)); |
| 1102 | } |
| 1103 | return null; |
| 1104 | } |
| 1105 | |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 1106 | public Pair<ApplicationInfo, Integer> getWallpaperArrayResourceId() { |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 1107 | // 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 Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 1111 | 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 Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 1118 | } |
| 1119 | |
Jose Pascoal | 8bdb364 | 2015-02-13 13:15:11 +0000 | [diff] [blame] | 1120 | private static void addWallpapers(ArrayList<WallpaperTileInfo> known, Resources res, |
| 1121 | String packageName, int listResId) { |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 1122 | final String[] extras = res.getStringArray(listResId); |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 1123 | for (String extra : extras) { |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 1124 | int resId = res.getIdentifier(extra, "drawable", packageName); |
| 1125 | if (resId != 0) { |
| 1126 | final int thumbRes = res.getIdentifier(extra + "_small", "drawable", packageName); |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 1127 | |
| 1128 | if (thumbRes != 0) { |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 1129 | ResourceWallpaperInfo wallpaperInfo = |
| 1130 | new ResourceWallpaperInfo(res, resId, res.getDrawable(thumbRes)); |
Jeff Sharkey | 5aeef58 | 2014-04-14 13:26:42 -0700 | [diff] [blame] | 1131 | known.add(wallpaperInfo); |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 1132 | // Log.d(TAG, "add: [" + packageName + "]: " + extra + " (" + res + ")"); |
| 1133 | } |
Michael Jurka | 6218e5b | 2013-09-10 14:07:20 +0200 | [diff] [blame] | 1134 | } else { |
| 1135 | Log.e(TAG, "Couldn't find wallpaper " + extra); |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 1136 | } |
| 1137 | } |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | public CropView getCropView() { |
| 1141 | return mCropView; |
| 1142 | } |
| 1143 | |
| 1144 | public SavedWallpaperImages getSavedImages() { |
| 1145 | return mSavedImages; |
| 1146 | } |
| 1147 | |
Selim Cinek | ffed658 | 2014-02-28 17:18:59 +0100 | [diff] [blame] | 1148 | public void onLiveWallpaperPickerLaunch(WallpaperInfo info) { |
| 1149 | mLastClickedLiveWallpaperInfo = info; |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 1150 | mLiveWallpaperInfoOnPickerLaunch = WallpaperManager.getInstance(this).getWallpaperInfo(); |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 1151 | } |
| 1152 | |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 1153 | 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 Goyal | cdc4dbd | 2014-07-16 16:04:08 -0700 | [diff] [blame] | 1167 | private static class SimpleWallpapersAdapter extends ArrayAdapter<WallpaperTileInfo> { |
| 1168 | private final LayoutInflater mLayoutInflater; |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 1169 | |
Sunny Goyal | cdc4dbd | 2014-07-16 16:04:08 -0700 | [diff] [blame] | 1170 | SimpleWallpapersAdapter(Activity activity, ArrayList<WallpaperTileInfo> wallpapers) { |
| 1171 | super(activity, R.layout.wallpaper_picker_item, wallpapers); |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 1172 | mLayoutInflater = activity.getLayoutInflater(); |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | public View getView(int position, View convertView, ViewGroup parent) { |
Sunny Goyal | cdc4dbd | 2014-07-16 16:04:08 -0700 | [diff] [blame] | 1176 | Drawable thumb = getItem(position).mThumb; |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 1177 | if (thumb == null) { |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 1178 | Log.e(TAG, "Error decoding thumbnail for wallpaper #" + position); |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 1179 | } |
Sunny Goyal | cdc4dbd | 2014-07-16 16:04:08 -0700 | [diff] [blame] | 1180 | return createImageTileView(mLayoutInflater, convertView, parent, thumb); |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 1181 | } |
| 1182 | } |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 1183 | |
Sunny Goyal | cdc4dbd | 2014-07-16 16:04:08 -0700 | [diff] [blame] | 1184 | public static View createImageTileView(LayoutInflater layoutInflater, |
Michael Jurka | 1e4e6dd | 2013-09-23 23:02:05 +0100 | [diff] [blame] | 1185 | 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 Jurka | 7ad868b | 2013-12-12 15:04:25 +0100 | [diff] [blame] | 1205 | |
| 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 Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 1211 | } |