Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
| 17 | package com.android.launcher2; |
| 18 | |
Winson Chung | 55b6550 | 2011-05-26 12:03:43 -0700 | [diff] [blame] | 19 | import android.animation.AnimatorSet; |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 20 | import android.animation.ObjectAnimator; |
Winson Chung | d2e87b3 | 2011-06-02 10:53:07 -0700 | [diff] [blame] | 21 | import android.animation.ValueAnimator; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 22 | import android.appwidget.AppWidgetManager; |
| 23 | import android.appwidget.AppWidgetProviderInfo; |
| 24 | import android.content.ComponentName; |
| 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
Winson Chung | 46af2e8 | 2011-05-09 16:00:53 -0700 | [diff] [blame] | 27 | import android.content.pm.ActivityInfo; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 28 | import android.content.pm.PackageManager; |
| 29 | import android.content.pm.ResolveInfo; |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 30 | import android.content.res.Configuration; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 31 | import android.content.res.Resources; |
| 32 | import android.content.res.TypedArray; |
| 33 | import android.graphics.Bitmap; |
Adam Cohen | 4e84401 | 2011-11-09 13:48:04 -0800 | [diff] [blame] | 34 | import android.graphics.Bitmap.Config; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 35 | import android.graphics.Canvas; |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 36 | import android.graphics.MaskFilter; |
| 37 | import android.graphics.Paint; |
Winson Chung | 70fc438 | 2011-08-08 15:31:33 -0700 | [diff] [blame] | 38 | import android.graphics.PorterDuff; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 39 | import android.graphics.Rect; |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 40 | import android.graphics.TableMaskFilter; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 41 | import android.graphics.drawable.Drawable; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 42 | import android.os.AsyncTask; |
| 43 | import android.os.Process; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 44 | import android.util.AttributeSet; |
| 45 | import android.util.Log; |
Winson Chung | 72d8b39 | 2011-07-29 13:56:44 -0700 | [diff] [blame] | 46 | import android.view.Gravity; |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 47 | import android.view.KeyEvent; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 48 | import android.view.LayoutInflater; |
Winson Chung | de1af76 | 2011-07-21 16:44:07 -0700 | [diff] [blame] | 49 | import android.view.MotionEvent; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 50 | import android.view.View; |
Adam Cohen | 4e84401 | 2011-11-09 13:48:04 -0800 | [diff] [blame] | 51 | import android.view.ViewConfiguration; |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 52 | import android.view.ViewGroup; |
Winson Chung | 55b6550 | 2011-05-26 12:03:43 -0700 | [diff] [blame] | 53 | import android.view.animation.AccelerateInterpolator; |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 54 | import android.view.animation.DecelerateInterpolator; |
Winson Chung | fd3385f | 2011-06-15 19:51:24 -0700 | [diff] [blame] | 55 | import android.widget.GridLayout; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 56 | import android.widget.ImageView; |
Winson Chung | 55b6550 | 2011-05-26 12:03:43 -0700 | [diff] [blame] | 57 | import android.widget.Toast; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 58 | |
| 59 | import com.android.launcher.R; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 60 | import com.android.launcher2.DropTarget.DragObject; |
| 61 | |
| 62 | import java.util.ArrayList; |
| 63 | import java.util.Collections; |
| 64 | import java.util.Iterator; |
| 65 | import java.util.List; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 66 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 67 | /** |
| 68 | * A simple callback interface which also provides the results of the task. |
| 69 | */ |
| 70 | interface AsyncTaskCallback { |
| 71 | void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data); |
| 72 | } |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 73 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 74 | /** |
| 75 | * The data needed to perform either of the custom AsyncTasks. |
| 76 | */ |
| 77 | class AsyncTaskPageData { |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 78 | enum Type { |
| 79 | LoadWidgetPreviewData, |
| 80 | LoadHolographicIconsData |
| 81 | } |
| 82 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 83 | AsyncTaskPageData(int p, ArrayList<Object> l, ArrayList<Bitmap> si, AsyncTaskCallback bgR, |
| 84 | AsyncTaskCallback postR) { |
| 85 | page = p; |
| 86 | items = l; |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 87 | sourceImages = si; |
| 88 | generatedImages = new ArrayList<Bitmap>(); |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 89 | maxImageWidth = maxImageHeight = -1; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 90 | doInBackgroundCallback = bgR; |
| 91 | postExecuteCallback = postR; |
| 92 | } |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 93 | AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, AsyncTaskCallback bgR, |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 94 | AsyncTaskCallback postR) { |
| 95 | page = p; |
| 96 | items = l; |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 97 | generatedImages = new ArrayList<Bitmap>(); |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 98 | maxImageWidth = cw; |
| 99 | maxImageHeight = ch; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 100 | doInBackgroundCallback = bgR; |
| 101 | postExecuteCallback = postR; |
| 102 | } |
Winson Chung | 0994593 | 2011-09-20 14:22:40 -0700 | [diff] [blame] | 103 | void cleanup(boolean cancelled) { |
| 104 | // Clean up any references to source/generated bitmaps |
| 105 | if (sourceImages != null) { |
| 106 | if (cancelled) { |
| 107 | for (Bitmap b : sourceImages) { |
| 108 | b.recycle(); |
| 109 | } |
| 110 | } |
| 111 | sourceImages.clear(); |
| 112 | } |
| 113 | if (generatedImages != null) { |
| 114 | if (cancelled) { |
| 115 | for (Bitmap b : generatedImages) { |
| 116 | b.recycle(); |
| 117 | } |
| 118 | } |
| 119 | generatedImages.clear(); |
| 120 | } |
| 121 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 122 | int page; |
| 123 | ArrayList<Object> items; |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 124 | ArrayList<Bitmap> sourceImages; |
| 125 | ArrayList<Bitmap> generatedImages; |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 126 | int maxImageWidth; |
| 127 | int maxImageHeight; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 128 | AsyncTaskCallback doInBackgroundCallback; |
| 129 | AsyncTaskCallback postExecuteCallback; |
| 130 | } |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 131 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 132 | /** |
| 133 | * A generic template for an async task used in AppsCustomize. |
| 134 | */ |
| 135 | class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> { |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 136 | AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) { |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 137 | page = p; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 138 | threadPriority = Process.THREAD_PRIORITY_DEFAULT; |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 139 | dataType = ty; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 140 | } |
| 141 | @Override |
| 142 | protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) { |
| 143 | if (params.length != 1) return null; |
| 144 | // Load each of the widget previews in the background |
| 145 | params[0].doInBackgroundCallback.run(this, params[0]); |
| 146 | return params[0]; |
| 147 | } |
| 148 | @Override |
| 149 | protected void onPostExecute(AsyncTaskPageData result) { |
| 150 | // All the widget previews are loaded, so we can just callback to inflate the page |
| 151 | result.postExecuteCallback.run(this, result); |
| 152 | } |
| 153 | |
| 154 | void setThreadPriority(int p) { |
| 155 | threadPriority = p; |
| 156 | } |
| 157 | void syncThreadPriority() { |
| 158 | Process.setThreadPriority(threadPriority); |
| 159 | } |
| 160 | |
| 161 | // The page that this async task is associated with |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 162 | AsyncTaskPageData.Type dataType; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 163 | int page; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 164 | int threadPriority; |
| 165 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 166 | |
| 167 | /** |
| 168 | * The Apps/Customize page that displays all the applications, widgets, and shortcuts. |
| 169 | */ |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 170 | public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 171 | AllAppsView, View.OnClickListener, View.OnKeyListener, DragSource { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 172 | static final String LOG_TAG = "AppsCustomizePagedView"; |
| 173 | |
| 174 | /** |
| 175 | * The different content types that this paged view can show. |
| 176 | */ |
| 177 | public enum ContentType { |
| 178 | Applications, |
Winson Chung | 6a26e5b | 2011-05-26 14:36:06 -0700 | [diff] [blame] | 179 | Widgets |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | // Refs |
| 183 | private Launcher mLauncher; |
| 184 | private DragController mDragController; |
| 185 | private final LayoutInflater mLayoutInflater; |
| 186 | private final PackageManager mPackageManager; |
| 187 | |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 188 | // Save and Restore |
| 189 | private int mSaveInstanceStateItemIndex = -1; |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 190 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 191 | // Content |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 192 | private ArrayList<ApplicationInfo> mApps; |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 193 | private ArrayList<Object> mWidgets; |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 194 | |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 195 | // Cling |
Winson Chung | 3f4e142 | 2011-11-17 14:58:51 -0800 | [diff] [blame] | 196 | private boolean mHasShownAllAppsCling; |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 197 | private int mClingFocusedX; |
| 198 | private int mClingFocusedY; |
| 199 | |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 200 | // Caching |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 201 | private Canvas mCanvas; |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 202 | private Drawable mDefaultWidgetBackground; |
Winson Chung | 4dbea79 | 2011-05-05 14:21:32 -0700 | [diff] [blame] | 203 | private IconCache mIconCache; |
Winson Chung | 70fc438 | 2011-08-08 15:31:33 -0700 | [diff] [blame] | 204 | private int mDragViewMultiplyColor; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 205 | |
| 206 | // Dimens |
| 207 | private int mContentWidth; |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 208 | private int mAppIconSize; |
Winson Chung | 6032e7e | 2011-11-08 15:47:17 -0800 | [diff] [blame] | 209 | private int mMaxAppCellCountX, mMaxAppCellCountY; |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 210 | private int mWidgetCountX, mWidgetCountY; |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 211 | private int mWidgetWidthGap, mWidgetHeightGap; |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 212 | private final int mWidgetPreviewIconPaddedDimension; |
| 213 | private final float sWidgetPreviewIconPaddingPercentage = 0.25f; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 214 | private PagedViewCellLayout mWidgetSpacingLayout; |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 215 | private int mNumAppsPages; |
| 216 | private int mNumWidgetPages; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 217 | |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 218 | // Relating to the scroll and overscroll effects |
| 219 | Workspace.ZInterpolator mZInterpolator = new Workspace.ZInterpolator(0.5f); |
Adam Cohen | cff6af8 | 2011-09-13 14:51:53 -0700 | [diff] [blame] | 220 | private static float CAMERA_DISTANCE = 6500; |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 221 | private static float TRANSITION_SCALE_FACTOR = 0.74f; |
Adam Cohen | cff6af8 | 2011-09-13 14:51:53 -0700 | [diff] [blame] | 222 | private static float TRANSITION_PIVOT = 0.65f; |
| 223 | private static float TRANSITION_MAX_ROTATION = 22; |
| 224 | private static final boolean PERFORM_OVERSCROLL_ROTATION = true; |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 225 | private AccelerateInterpolator mAlphaInterpolator = new AccelerateInterpolator(0.9f); |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 226 | private DecelerateInterpolator mLeftScreenAlphaInterpolator = new DecelerateInterpolator(4); |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 227 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 228 | // Previews & outlines |
| 229 | ArrayList<AppsCustomizeAsyncTask> mRunningTasks; |
| 230 | private HolographicOutlineHelper mHolographicOutlineHelper; |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 231 | private static final int sPageSleepDelay = 200; |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 232 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 233 | public AppsCustomizePagedView(Context context, AttributeSet attrs) { |
| 234 | super(context, attrs); |
| 235 | mLayoutInflater = LayoutInflater.from(context); |
| 236 | mPackageManager = context.getPackageManager(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 237 | mApps = new ArrayList<ApplicationInfo>(); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 238 | mWidgets = new ArrayList<Object>(); |
Winson Chung | 4dbea79 | 2011-05-05 14:21:32 -0700 | [diff] [blame] | 239 | mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache(); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 240 | mHolographicOutlineHelper = new HolographicOutlineHelper(); |
| 241 | mCanvas = new Canvas(); |
| 242 | mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>(); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 243 | |
| 244 | // Save the default widget preview background |
| 245 | Resources resources = context.getResources(); |
Winson Chung | 967289b | 2011-06-30 18:09:30 -0700 | [diff] [blame] | 246 | mDefaultWidgetBackground = resources.getDrawable(R.drawable.default_widget_preview_holo); |
Winson Chung | 70fc438 | 2011-08-08 15:31:33 -0700 | [diff] [blame] | 247 | mAppIconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size); |
| 248 | mDragViewMultiplyColor = resources.getColor(R.color.drag_view_multiply_color); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 249 | |
Winson Chung | 6032e7e | 2011-11-08 15:47:17 -0800 | [diff] [blame] | 250 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0); |
| 251 | mMaxAppCellCountX = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountX, -1); |
| 252 | mMaxAppCellCountY = a.getInt(R.styleable.AppsCustomizePagedView_maxAppCellCountY, -1); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 253 | mWidgetWidthGap = |
| 254 | a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0); |
| 255 | mWidgetHeightGap = |
| 256 | a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 257 | mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2); |
| 258 | mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2); |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 259 | mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0); |
| 260 | mClingFocusedY = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedY, 0); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 261 | a.recycle(); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 262 | mWidgetSpacingLayout = new PagedViewCellLayout(getContext()); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 263 | |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 264 | // The padding on the non-matched dimension for the default widget preview icons |
| 265 | // (top + bottom) |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 266 | mWidgetPreviewIconPaddedDimension = |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 267 | (int) (mAppIconSize * (1 + (2 * sWidgetPreviewIconPaddingPercentage))); |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 268 | mFadeInAdjacentScreens = false; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | @Override |
| 272 | protected void init() { |
| 273 | super.init(); |
Winson Chung | 6a87740 | 2011-10-26 14:51:44 -0700 | [diff] [blame] | 274 | mCenterPagesVertically = false; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 275 | |
| 276 | Context context = getContext(); |
| 277 | Resources r = context.getResources(); |
| 278 | setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f); |
| 279 | } |
| 280 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 281 | @Override |
Michael Jurka | d771c96 | 2011-08-09 15:00:48 -0700 | [diff] [blame] | 282 | protected void onUnhandledTap(MotionEvent ev) { |
| 283 | if (LauncherApplication.isScreenLarge()) { |
Winson Chung | de1af76 | 2011-07-21 16:44:07 -0700 | [diff] [blame] | 284 | // Dismiss AppsCustomize if we tap |
| 285 | mLauncher.showWorkspace(true); |
| 286 | } |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 289 | /** Returns the item index of the center item on this page so that we can restore to this |
| 290 | * item index when we rotate. */ |
| 291 | private int getMiddleComponentIndexOnCurrentPage() { |
| 292 | int i = -1; |
| 293 | if (getPageCount() > 0) { |
| 294 | int currentPage = getCurrentPage(); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 295 | if (currentPage < mNumAppsPages) { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 296 | PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(currentPage); |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 297 | PagedViewCellLayoutChildren childrenLayout = layout.getChildrenLayout(); |
| 298 | int numItemsPerPage = mCellCountX * mCellCountY; |
| 299 | int childCount = childrenLayout.getChildCount(); |
| 300 | if (childCount > 0) { |
| 301 | i = (currentPage * numItemsPerPage) + (childCount / 2); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 302 | } |
| 303 | } else { |
| 304 | int numApps = mApps.size(); |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 305 | PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage); |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 306 | int numItemsPerPage = mWidgetCountX * mWidgetCountY; |
| 307 | int childCount = layout.getChildCount(); |
| 308 | if (childCount > 0) { |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 309 | i = numApps + |
| 310 | ((currentPage - mNumAppsPages) * numItemsPerPage) + (childCount / 2); |
| 311 | } |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | return i; |
| 315 | } |
| 316 | |
| 317 | /** Get the index of the item to restore to if we need to restore the current page. */ |
| 318 | int getSaveInstanceStateIndex() { |
| 319 | if (mSaveInstanceStateItemIndex == -1) { |
| 320 | mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage(); |
| 321 | } |
| 322 | return mSaveInstanceStateItemIndex; |
| 323 | } |
| 324 | |
| 325 | /** Returns the page in the current orientation which is expected to contain the specified |
| 326 | * item index. */ |
| 327 | int getPageForComponent(int index) { |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 328 | if (index < 0) return 0; |
| 329 | |
| 330 | if (index < mApps.size()) { |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 331 | int numItemsPerPage = mCellCountX * mCellCountY; |
| 332 | return (index / numItemsPerPage); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 333 | } else { |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 334 | int numItemsPerPage = mWidgetCountX * mWidgetCountY; |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 335 | return mNumAppsPages + ((index - mApps.size()) / numItemsPerPage); |
| 336 | } |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 339 | /** |
| 340 | * This differs from isDataReady as this is the test done if isDataReady is not set. |
| 341 | */ |
| 342 | private boolean testDataReady() { |
Winson Chung | fd3385f | 2011-06-15 19:51:24 -0700 | [diff] [blame] | 343 | // We only do this test once, and we default to the Applications page, so we only really |
| 344 | // have to wait for there to be apps. |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 345 | // TODO: What if one of them is validly empty |
| 346 | return !mApps.isEmpty() && !mWidgets.isEmpty(); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 349 | /** Restores the page for an item at the specified index */ |
| 350 | void restorePageForIndex(int index) { |
| 351 | if (index < 0) return; |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 352 | mSaveInstanceStateItemIndex = index; |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Winson Chung | 4b0ed8c | 2011-10-19 15:24:49 -0700 | [diff] [blame] | 355 | private void updatePageCounts() { |
| 356 | mNumWidgetPages = (int) Math.ceil(mWidgets.size() / |
| 357 | (float) (mWidgetCountX * mWidgetCountY)); |
| 358 | mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY)); |
| 359 | } |
| 360 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 361 | protected void onDataReady(int width, int height) { |
| 362 | // Note that we transpose the counts in portrait so that we get a similar layout |
| 363 | boolean isLandscape = getResources().getConfiguration().orientation == |
| 364 | Configuration.ORIENTATION_LANDSCAPE; |
| 365 | int maxCellCountX = Integer.MAX_VALUE; |
| 366 | int maxCellCountY = Integer.MAX_VALUE; |
| 367 | if (LauncherApplication.isScreenLarge()) { |
| 368 | maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() : |
| 369 | LauncherModel.getCellCountY()); |
| 370 | maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() : |
| 371 | LauncherModel.getCellCountX()); |
| 372 | } |
Winson Chung | 6032e7e | 2011-11-08 15:47:17 -0800 | [diff] [blame] | 373 | if (mMaxAppCellCountX > -1) { |
| 374 | maxCellCountX = Math.min(maxCellCountX, mMaxAppCellCountX); |
| 375 | } |
| 376 | if (mMaxAppCellCountY > -1) { |
| 377 | maxCellCountY = Math.min(maxCellCountY, mMaxAppCellCountY); |
| 378 | } |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 379 | |
| 380 | // Now that the data is ready, we can calculate the content width, the number of cells to |
| 381 | // use for each page |
| 382 | mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap); |
| 383 | mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, |
| 384 | mPageLayoutPaddingRight, mPageLayoutPaddingBottom); |
| 385 | mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY); |
| 386 | mCellCountX = mWidgetSpacingLayout.getCellCountX(); |
| 387 | mCellCountY = mWidgetSpacingLayout.getCellCountY(); |
Winson Chung | 4b0ed8c | 2011-10-19 15:24:49 -0700 | [diff] [blame] | 388 | updatePageCounts(); |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 389 | |
Winson Chung | db1138b | 2011-06-30 14:39:35 -0700 | [diff] [blame] | 390 | // Force a measure to update recalculate the gaps |
| 391 | int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST); |
| 392 | int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST); |
| 393 | mWidgetSpacingLayout.measure(widthSpec, heightSpec); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 394 | mContentWidth = mWidgetSpacingLayout.getContentWidth(); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 395 | |
Michael Jurka | e326f18 | 2011-11-21 14:05:46 -0800 | [diff] [blame] | 396 | AppsCustomizeTabHost host = (AppsCustomizeTabHost) getTabHost(); |
| 397 | final boolean hostIsTransitioning = host.isTransitioning(); |
| 398 | |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 399 | // Restore the page |
| 400 | int page = getPageForComponent(mSaveInstanceStateItemIndex); |
Michael Jurka | e326f18 | 2011-11-21 14:05:46 -0800 | [diff] [blame] | 401 | invalidatePageData(Math.max(0, page), hostIsTransitioning); |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 402 | |
Winson Chung | 3f4e142 | 2011-11-17 14:58:51 -0800 | [diff] [blame] | 403 | // Show All Apps cling if we are finished transitioning, otherwise, we will try again when |
| 404 | // the transition completes in AppsCustomizeTabHost (otherwise the wrong offsets will be |
| 405 | // returned while animating) |
Michael Jurka | e326f18 | 2011-11-21 14:05:46 -0800 | [diff] [blame] | 406 | if (!hostIsTransitioning) { |
Winson Chung | 3f4e142 | 2011-11-17 14:58:51 -0800 | [diff] [blame] | 407 | post(new Runnable() { |
| 408 | @Override |
| 409 | public void run() { |
| 410 | showAllAppsCling(); |
| 411 | } |
| 412 | }); |
| 413 | } |
| 414 | } |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 415 | |
Winson Chung | 3f4e142 | 2011-11-17 14:58:51 -0800 | [diff] [blame] | 416 | void showAllAppsCling() { |
| 417 | if (!mHasShownAllAppsCling && isDataReady() && testDataReady()) { |
| 418 | mHasShownAllAppsCling = true; |
| 419 | // Calculate the position for the cling punch through |
| 420 | int[] offset = new int[2]; |
| 421 | int[] pos = mWidgetSpacingLayout.estimateCellPosition(mClingFocusedX, mClingFocusedY); |
| 422 | mLauncher.getDragLayer().getLocationInDragLayer(this, offset); |
| 423 | // PagedViews are centered horizontally but top aligned |
| 424 | pos[0] += (getMeasuredWidth() - mWidgetSpacingLayout.getMeasuredWidth()) / 2 + |
| 425 | offset[0]; |
| 426 | pos[1] += offset[1]; |
| 427 | mLauncher.showFirstRunAllAppsCling(pos); |
| 428 | } |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | @Override |
| 432 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 433 | int width = MeasureSpec.getSize(widthMeasureSpec); |
| 434 | int height = MeasureSpec.getSize(heightMeasureSpec); |
| 435 | if (!isDataReady()) { |
| 436 | if (testDataReady()) { |
| 437 | setDataIsReady(); |
| 438 | setMeasuredDimension(width, height); |
| 439 | onDataReady(width, height); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 444 | } |
| 445 | |
Winson Chung | 34efdaf | 2011-05-24 14:19:56 -0700 | [diff] [blame] | 446 | /** Removes and returns the ResolveInfo with the specified ComponentName */ |
| 447 | private ResolveInfo removeResolveInfoWithComponentName(List<ResolveInfo> list, |
| 448 | ComponentName cn) { |
| 449 | Iterator<ResolveInfo> iter = list.iterator(); |
| 450 | while (iter.hasNext()) { |
| 451 | ResolveInfo rinfo = iter.next(); |
| 452 | ActivityInfo info = rinfo.activityInfo; |
| 453 | ComponentName c = new ComponentName(info.packageName, info.name); |
| 454 | if (c.equals(cn)) { |
| 455 | iter.remove(); |
| 456 | return rinfo; |
| 457 | } |
| 458 | } |
| 459 | return null; |
| 460 | } |
| 461 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 462 | public void onPackagesUpdated() { |
Adam Cohen | b0fa352 | 2011-08-17 16:10:46 -0700 | [diff] [blame] | 463 | // TODO: this isn't ideal, but we actually need to delay here. This call is triggered |
| 464 | // by a broadcast receiver, and in order for it to work correctly, we need to know that |
| 465 | // the AppWidgetService has already received and processed the same broadcast. Since there |
| 466 | // is no guarantee about ordering of broadcast receipt, we just delay here. Ideally, |
| 467 | // we should have a more precise way of ensuring the AppWidgetService is up to date. |
| 468 | postDelayed(new Runnable() { |
| 469 | public void run() { |
| 470 | updatePackages(); |
| 471 | } |
| 472 | }, 500); |
| 473 | } |
| 474 | |
| 475 | public void updatePackages() { |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 476 | // Get the list of widgets and shortcuts |
Winson Chung | 6a3fd3f | 2011-08-02 14:03:26 -0700 | [diff] [blame] | 477 | boolean wasEmpty = mWidgets.isEmpty(); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 478 | mWidgets.clear(); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 479 | List<AppWidgetProviderInfo> widgets = |
| 480 | AppWidgetManager.getInstance(mLauncher).getInstalledProviders(); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 481 | Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT); |
| 482 | List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0); |
Michael Jurka | dbc1f65 | 2011-11-10 17:02:56 -0800 | [diff] [blame] | 483 | for (AppWidgetProviderInfo widget : widgets) { |
| 484 | if (widget.minWidth > 0 && widget.minHeight > 0) { |
| 485 | mWidgets.add(widget); |
| 486 | } else { |
| 487 | Log.e(LOG_TAG, "Widget " + widget.provider + " has invalid dimensions (" + |
| 488 | widget.minWidth + ", " + widget.minHeight + ")"); |
| 489 | } |
| 490 | } |
Winson Chung | 6a3fd3f | 2011-08-02 14:03:26 -0700 | [diff] [blame] | 491 | mWidgets.addAll(shortcuts); |
| 492 | Collections.sort(mWidgets, |
| 493 | new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager)); |
Winson Chung | 4b0ed8c | 2011-10-19 15:24:49 -0700 | [diff] [blame] | 494 | updatePageCounts(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 495 | |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 496 | if (wasEmpty) { |
| 497 | // The next layout pass will trigger data-ready if both widgets and apps are set, so request |
| 498 | // a layout to do this test and invalidate the page data when ready. |
| 499 | if (testDataReady()) requestLayout(); |
| 500 | } else { |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 501 | cancelAllTasks(); |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 502 | invalidatePageData(); |
| 503 | } |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | @Override |
| 507 | public void onClick(View v) { |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 508 | // When we have exited all apps or are in transition, disregard clicks |
| 509 | if (!mLauncher.isAllAppsCustomizeOpen() || |
| 510 | mLauncher.getWorkspace().isSwitchingState()) return; |
| 511 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 512 | if (v instanceof PagedViewIcon) { |
| 513 | // Animate some feedback to the click |
| 514 | final ApplicationInfo appInfo = (ApplicationInfo) v.getTag(); |
| 515 | animateClickFeedback(v, new Runnable() { |
| 516 | @Override |
| 517 | public void run() { |
| 518 | mLauncher.startActivitySafely(appInfo.intent, appInfo); |
| 519 | } |
| 520 | }); |
| 521 | } else if (v instanceof PagedViewWidget) { |
Winson Chung | d2e87b3 | 2011-06-02 10:53:07 -0700 | [diff] [blame] | 522 | // Let the user know that they have to long press to add a widget |
| 523 | Toast.makeText(getContext(), R.string.long_press_widget_to_add, |
| 524 | Toast.LENGTH_SHORT).show(); |
Winson Chung | 46af2e8 | 2011-05-09 16:00:53 -0700 | [diff] [blame] | 525 | |
Winson Chung | d2e87b3 | 2011-06-02 10:53:07 -0700 | [diff] [blame] | 526 | // Create a little animation to show that the widget can move |
| 527 | float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY); |
| 528 | final ImageView p = (ImageView) v.findViewById(R.id.widget_preview); |
| 529 | AnimatorSet bounce = new AnimatorSet(); |
| 530 | ValueAnimator tyuAnim = ObjectAnimator.ofFloat(p, "translationY", offsetY); |
| 531 | tyuAnim.setDuration(125); |
| 532 | ValueAnimator tydAnim = ObjectAnimator.ofFloat(p, "translationY", 0f); |
| 533 | tydAnim.setDuration(100); |
| 534 | bounce.play(tyuAnim).before(tydAnim); |
| 535 | bounce.setInterpolator(new AccelerateInterpolator()); |
| 536 | bounce.start(); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 537 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 540 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
| 541 | return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event); |
| 542 | } |
| 543 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 544 | /* |
| 545 | * PagedViewWithDraggableItems implementation |
| 546 | */ |
| 547 | @Override |
| 548 | protected void determineDraggingStart(android.view.MotionEvent ev) { |
| 549 | // Disable dragging by pulling an app down for now. |
| 550 | } |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 551 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 552 | private void beginDraggingApplication(View v) { |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 553 | mLauncher.getWorkspace().onDragStartedWithItem(v); |
| 554 | mLauncher.getWorkspace().beginDragShared(v, this); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 555 | } |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 556 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 557 | private void beginDraggingWidget(View v) { |
| 558 | // Get the widget preview as the drag representation |
| 559 | ImageView image = (ImageView) v.findViewById(R.id.widget_preview); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 560 | PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag(); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 561 | |
| 562 | // Compose the drag image |
Winson Chung | 1120e03 | 2011-11-22 16:11:31 -0800 | [diff] [blame] | 563 | Bitmap preview; |
| 564 | Bitmap outline; |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 565 | if (createItemInfo instanceof PendingAddWidgetInfo) { |
| 566 | PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) createItemInfo; |
Adam Cohen | f814aa0 | 2011-09-01 13:48:05 -0700 | [diff] [blame] | 567 | int[] spanXY = mLauncher.getSpanForWidget(createWidgetInfo, null); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 568 | createItemInfo.spanX = spanXY[0]; |
| 569 | createItemInfo.spanY = spanXY[1]; |
| 570 | |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 571 | int[] maxSize = mLauncher.getWorkspace().estimateItemSize(spanXY[0], spanXY[1], |
| 572 | createWidgetInfo, true); |
Winson Chung | 1120e03 | 2011-11-22 16:11:31 -0800 | [diff] [blame] | 573 | preview = getWidgetPreview(createWidgetInfo.componentName, createWidgetInfo.previewImage, |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 574 | createWidgetInfo.icon, spanXY[0], spanXY[1], maxSize[0], maxSize[1]); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 575 | } else { |
| 576 | // Workaround for the fact that we don't keep the original ResolveInfo associated with |
| 577 | // the shortcut around. To get the icon, we just render the preview image (which has |
| 578 | // the shortcut icon) to a new drag bitmap that clips the non-icon space. |
Winson Chung | 1120e03 | 2011-11-22 16:11:31 -0800 | [diff] [blame] | 579 | preview = Bitmap.createBitmap(mWidgetPreviewIconPaddedDimension, |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 580 | mWidgetPreviewIconPaddedDimension, Bitmap.Config.ARGB_8888); |
Winson Chung | 1120e03 | 2011-11-22 16:11:31 -0800 | [diff] [blame] | 581 | Drawable d = image.getDrawable(); |
| 582 | mCanvas.setBitmap(preview); |
| 583 | d.draw(mCanvas); |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 584 | mCanvas.setBitmap(null); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 585 | createItemInfo.spanX = createItemInfo.spanY = 1; |
| 586 | } |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 587 | |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 588 | // We use a custom alpha clip table for the default widget previews |
| 589 | Paint alphaClipPaint = null; |
| 590 | if (createItemInfo instanceof PendingAddWidgetInfo) { |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 591 | if (((PendingAddWidgetInfo) createItemInfo).previewImage != 0) { |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 592 | MaskFilter alphaClipTable = TableMaskFilter.CreateClipTable(0, 255); |
| 593 | alphaClipPaint = new Paint(); |
| 594 | alphaClipPaint.setMaskFilter(alphaClipTable); |
| 595 | } |
| 596 | } |
| 597 | |
Winson Chung | 1120e03 | 2011-11-22 16:11:31 -0800 | [diff] [blame] | 598 | // Save the preview for the outline generation, then dim the preview |
| 599 | outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(), |
| 600 | false); |
| 601 | mCanvas.setBitmap(preview); |
| 602 | mCanvas.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY); |
| 603 | mCanvas.setBitmap(null); |
| 604 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 605 | // Start the drag |
Winson Chung | 1120e03 | 2011-11-22 16:11:31 -0800 | [diff] [blame] | 606 | alphaClipPaint = null; |
Adam Cohen | 446e940 | 2011-09-15 18:21:21 -0700 | [diff] [blame] | 607 | mLauncher.lockScreenOrientationOnLargeUI(); |
Winson Chung | 1120e03 | 2011-11-22 16:11:31 -0800 | [diff] [blame] | 608 | mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, alphaClipPaint); |
| 609 | mDragController.startDrag(image, preview, this, createItemInfo, |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 610 | DragController.DRAG_ACTION_COPY, null); |
Winson Chung | 1120e03 | 2011-11-22 16:11:31 -0800 | [diff] [blame] | 611 | outline.recycle(); |
| 612 | preview.recycle(); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 613 | } |
| 614 | @Override |
| 615 | protected boolean beginDragging(View v) { |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 616 | // Dismiss the cling |
| 617 | mLauncher.dismissAllAppsCling(null); |
| 618 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 619 | if (!super.beginDragging(v)) return false; |
| 620 | |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 621 | // Go into spring loaded mode (must happen before we startDrag()) |
Adam Cohen | 7777d96 | 2011-08-18 18:58:38 -0700 | [diff] [blame] | 622 | mLauncher.enterSpringLoadedDragMode(); |
Winson Chung | fc79c80 | 2011-05-02 13:35:34 -0700 | [diff] [blame] | 623 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 624 | if (v instanceof PagedViewIcon) { |
| 625 | beginDraggingApplication(v); |
| 626 | } else if (v instanceof PagedViewWidget) { |
| 627 | beginDraggingWidget(v); |
| 628 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 629 | return true; |
| 630 | } |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 631 | private void endDragging(View target, boolean success) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 632 | mLauncher.getWorkspace().onDragStopped(success); |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 633 | if (!success || (target != mLauncher.getWorkspace() && |
| 634 | !(target instanceof DeleteDropTarget))) { |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 635 | // Exit spring loaded mode if we have not successfully dropped or have not handled the |
| 636 | // drop in Workspace |
| 637 | mLauncher.exitSpringLoadedDragMode(); |
| 638 | } |
Adam Cohen | 446e940 | 2011-09-15 18:21:21 -0700 | [diff] [blame] | 639 | mLauncher.unlockScreenOrientationOnLargeUI(); |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 640 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 641 | } |
| 642 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 643 | @Override |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 644 | public void onDropCompleted(View target, DragObject d, boolean success) { |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 645 | endDragging(target, success); |
Winson Chung | fc79c80 | 2011-05-02 13:35:34 -0700 | [diff] [blame] | 646 | |
| 647 | // Display an error message if the drag failed due to there not being enough space on the |
| 648 | // target layout we were dropping on. |
| 649 | if (!success) { |
| 650 | boolean showOutOfSpaceMessage = false; |
| 651 | if (target instanceof Workspace) { |
| 652 | int currentScreen = mLauncher.getCurrentWorkspaceScreen(); |
| 653 | Workspace workspace = (Workspace) target; |
| 654 | CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 655 | ItemInfo itemInfo = (ItemInfo) d.dragInfo; |
Winson Chung | fc79c80 | 2011-05-02 13:35:34 -0700 | [diff] [blame] | 656 | if (layout != null) { |
| 657 | layout.calculateSpans(itemInfo); |
| 658 | showOutOfSpaceMessage = |
| 659 | !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY); |
| 660 | } |
| 661 | } |
Winson Chung | fc79c80 | 2011-05-02 13:35:34 -0700 | [diff] [blame] | 662 | if (showOutOfSpaceMessage) { |
| 663 | mLauncher.showOutOfSpaceMessage(); |
| 664 | } |
| 665 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Winson Chung | 7f0acdd | 2011-09-19 18:34:19 -0700 | [diff] [blame] | 668 | @Override |
| 669 | protected void onDetachedFromWindow() { |
| 670 | super.onDetachedFromWindow(); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 671 | cancelAllTasks(); |
| 672 | } |
Winson Chung | 7f0acdd | 2011-09-19 18:34:19 -0700 | [diff] [blame] | 673 | |
Michael Jurka | e326f18 | 2011-11-21 14:05:46 -0800 | [diff] [blame] | 674 | public void clearAllWidgetPages() { |
| 675 | cancelAllTasks(); |
| 676 | int count = getChildCount(); |
| 677 | for (int i = 0; i < count; i++) { |
| 678 | View v = getPageAt(i); |
| 679 | if (v instanceof PagedViewGridLayout) { |
| 680 | ((PagedViewGridLayout) v).removeAllViewsOnPage(); |
| 681 | mDirtyPageContent.set(i, true); |
| 682 | } |
| 683 | } |
| 684 | } |
| 685 | |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 686 | private void cancelAllTasks() { |
Winson Chung | 7f0acdd | 2011-09-19 18:34:19 -0700 | [diff] [blame] | 687 | // Clean up all the async tasks |
| 688 | Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator(); |
| 689 | while (iter.hasNext()) { |
| 690 | AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next(); |
| 691 | task.cancel(false); |
| 692 | iter.remove(); |
| 693 | } |
| 694 | } |
| 695 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 696 | public void setContentType(ContentType type) { |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 697 | if (type == ContentType.Widgets) { |
| 698 | invalidatePageData(mNumAppsPages, true); |
| 699 | } else if (type == ContentType.Applications) { |
| 700 | invalidatePageData(0, true); |
| 701 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 702 | } |
| 703 | |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 704 | protected void snapToPage(int whichPage, int delta, int duration) { |
| 705 | super.snapToPage(whichPage, delta, duration); |
| 706 | updateCurrentTab(whichPage); |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 707 | |
| 708 | // Update the thread priorities given the direction lookahead |
| 709 | Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator(); |
| 710 | while (iter.hasNext()) { |
| 711 | AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next(); |
| 712 | int pageIndex = task.page + mNumAppsPages; |
| 713 | if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) || |
| 714 | (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) { |
| 715 | task.setThreadPriority(getThreadPriorityForPage(pageIndex)); |
| 716 | } else { |
| 717 | task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST); |
| 718 | } |
| 719 | } |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | private void updateCurrentTab(int currentPage) { |
| 723 | AppsCustomizeTabHost tabHost = getTabHost(); |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 724 | if (tabHost != null) { |
| 725 | String tag = tabHost.getCurrentTabTag(); |
| 726 | if (tag != null) { |
| 727 | if (currentPage >= mNumAppsPages && |
| 728 | !tag.equals(tabHost.getTabTagForContentType(ContentType.Widgets))) { |
| 729 | tabHost.setCurrentTabFromContent(ContentType.Widgets); |
| 730 | } else if (currentPage < mNumAppsPages && |
| 731 | !tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) { |
| 732 | tabHost.setCurrentTabFromContent(ContentType.Applications); |
| 733 | } |
Winson Chung | 6a8103c | 2011-10-21 11:08:32 -0700 | [diff] [blame] | 734 | } |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 738 | /* |
| 739 | * Apps PagedView implementation |
| 740 | */ |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 741 | private void setVisibilityOnChildren(ViewGroup layout, int visibility) { |
| 742 | int childCount = layout.getChildCount(); |
| 743 | for (int i = 0; i < childCount; ++i) { |
| 744 | layout.getChildAt(i).setVisibility(visibility); |
| 745 | } |
| 746 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 747 | private void setupPage(PagedViewCellLayout layout) { |
| 748 | layout.setCellCount(mCellCountX, mCellCountY); |
| 749 | layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap); |
| 750 | layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, |
| 751 | mPageLayoutPaddingRight, mPageLayoutPaddingBottom); |
| 752 | |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 753 | // Note: We force a measure here to get around the fact that when we do layout calculations |
| 754 | // immediately after syncing, we don't have a proper width. That said, we already know the |
| 755 | // expected page width, so we can actually optimize by hiding all the TextView-based |
| 756 | // children that are expensive to measure, and let that happen naturally later. |
| 757 | setVisibilityOnChildren(layout, View.GONE); |
Winson Chung | db1138b | 2011-06-30 14:39:35 -0700 | [diff] [blame] | 758 | int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 759 | int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST); |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 760 | layout.setMinimumWidth(getPageContentWidth()); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 761 | layout.measure(widthSpec, heightSpec); |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 762 | setVisibilityOnChildren(layout, View.VISIBLE); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 763 | } |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 764 | |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 765 | public void syncAppsPageItems(int page, boolean immediate) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 766 | // ensure that we have the right number of items on the pages |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 767 | int numCells = mCellCountX * mCellCountY; |
| 768 | int startIndex = page * numCells; |
| 769 | int endIndex = Math.min(startIndex + numCells, mApps.size()); |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 770 | PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page); |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 771 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 772 | layout.removeAllViewsOnPage(); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 773 | ArrayList<Object> items = new ArrayList<Object>(); |
| 774 | ArrayList<Bitmap> images = new ArrayList<Bitmap>(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 775 | for (int i = startIndex; i < endIndex; ++i) { |
| 776 | ApplicationInfo info = mApps.get(i); |
| 777 | PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate( |
| 778 | R.layout.apps_customize_application, layout, false); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 779 | icon.applyFromApplicationInfo(info, true, mHolographicOutlineHelper); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 780 | icon.setOnClickListener(this); |
| 781 | icon.setOnLongClickListener(this); |
| 782 | icon.setOnTouchListener(this); |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 783 | icon.setOnKeyListener(this); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 784 | |
| 785 | int index = i - startIndex; |
| 786 | int x = index % mCellCountX; |
| 787 | int y = index / mCellCountX; |
Winson Chung | 6a70e9f | 2011-05-17 16:24:49 -0700 | [diff] [blame] | 788 | layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1)); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 789 | |
| 790 | items.add(info); |
| 791 | images.add(info.iconBitmap); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 792 | } |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 793 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 794 | layout.createHardwareLayers(); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 795 | |
Winson Chung | 8ff8713 | 2011-09-30 19:40:46 -0700 | [diff] [blame] | 796 | /* TEMPORARILY DISABLE HOLOGRAPHIC ICONS |
Winson Chung | 54c725c | 2011-08-03 12:03:40 -0700 | [diff] [blame] | 797 | if (mFadeInAdjacentScreens) { |
| 798 | prepareGenerateHoloOutlinesTask(page, items, images); |
| 799 | } |
Winson Chung | 8ff8713 | 2011-09-30 19:40:46 -0700 | [diff] [blame] | 800 | */ |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 801 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 802 | |
| 803 | /** |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 804 | * A helper to return the priority for loading of the specified widget page. |
| 805 | */ |
| 806 | private int getWidgetPageLoadPriority(int page) { |
| 807 | // If we are snapping to another page, use that index as the target page index |
| 808 | int toPage = mCurrentPage; |
| 809 | if (mNextPage > -1) { |
| 810 | toPage = mNextPage; |
| 811 | } |
| 812 | |
| 813 | // We use the distance from the target page as an initial guess of priority, but if there |
| 814 | // are no pages of higher priority than the page specified, then bump up the priority of |
| 815 | // the specified page. |
| 816 | Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator(); |
| 817 | int minPageDiff = Integer.MAX_VALUE; |
| 818 | while (iter.hasNext()) { |
| 819 | AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next(); |
| 820 | minPageDiff = Math.abs(task.page + mNumAppsPages - toPage); |
| 821 | } |
| 822 | |
| 823 | int rawPageDiff = Math.abs(page - toPage); |
| 824 | return rawPageDiff - Math.min(rawPageDiff, minPageDiff); |
| 825 | } |
| 826 | /** |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 827 | * Return the appropriate thread priority for loading for a given page (we give the current |
| 828 | * page much higher priority) |
| 829 | */ |
| 830 | private int getThreadPriorityForPage(int page) { |
| 831 | // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 832 | int pageDiff = getWidgetPageLoadPriority(page); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 833 | if (pageDiff <= 0) { |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 834 | return Process.THREAD_PRIORITY_LESS_FAVORABLE; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 835 | } else if (pageDiff <= 1) { |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 836 | return Process.THREAD_PRIORITY_LOWEST; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 837 | } else { |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 838 | return Process.THREAD_PRIORITY_LOWEST; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 839 | } |
| 840 | } |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 841 | private int getSleepForPage(int page) { |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 842 | int pageDiff = getWidgetPageLoadPriority(page); |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 843 | return Math.max(0, pageDiff * sPageSleepDelay); |
| 844 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 845 | /** |
| 846 | * Creates and executes a new AsyncTask to load a page of widget previews. |
| 847 | */ |
| 848 | private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets, |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 849 | int cellWidth, int cellHeight, int cellCountX) { |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 850 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 851 | // Prune all tasks that are no longer needed |
| 852 | Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator(); |
| 853 | while (iter.hasNext()) { |
| 854 | AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next(); |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 855 | int taskPage = task.page + mNumAppsPages; |
| 856 | if (taskPage < getAssociatedLowerPageBound(mCurrentPage) || |
| 857 | taskPage > getAssociatedUpperPageBound(mCurrentPage)) { |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 858 | task.cancel(false); |
| 859 | iter.remove(); |
| 860 | } else { |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 861 | task.setThreadPriority(getThreadPriorityForPage(taskPage)); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 862 | } |
| 863 | } |
| 864 | |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 865 | // We introduce a slight delay to order the loading of side pages so that we don't thrash |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 866 | final int sleepMs = getSleepForPage(page + mNumAppsPages); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 867 | AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight, |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 868 | new AsyncTaskCallback() { |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 869 | @Override |
| 870 | public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) { |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 871 | try { |
Winson Chung | 0994593 | 2011-09-20 14:22:40 -0700 | [diff] [blame] | 872 | try { |
| 873 | Thread.sleep(sleepMs); |
| 874 | } catch (Exception e) {} |
| 875 | loadWidgetPreviewsInBackground(task, data); |
| 876 | } finally { |
| 877 | if (task.isCancelled()) { |
| 878 | data.cleanup(true); |
| 879 | } |
| 880 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 881 | } |
| 882 | }, |
| 883 | new AsyncTaskCallback() { |
| 884 | @Override |
| 885 | public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) { |
Winson Chung | 0994593 | 2011-09-20 14:22:40 -0700 | [diff] [blame] | 886 | try { |
| 887 | mRunningTasks.remove(task); |
| 888 | if (task.isCancelled()) return; |
Winson Chung | 0994593 | 2011-09-20 14:22:40 -0700 | [diff] [blame] | 889 | onSyncWidgetPageItems(data); |
| 890 | } finally { |
| 891 | data.cleanup(task.isCancelled()); |
| 892 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 893 | } |
Winson Chung | 0994593 | 2011-09-20 14:22:40 -0700 | [diff] [blame] | 894 | }); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 895 | |
| 896 | // Ensure that the task is appropriately prioritized and runs in parallel |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 897 | AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page, |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 898 | AsyncTaskPageData.Type.LoadWidgetPreviewData); |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 899 | t.setThreadPriority(getThreadPriorityForPage(page + mNumAppsPages)); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 900 | t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData); |
| 901 | mRunningTasks.add(t); |
| 902 | } |
| 903 | /** |
| 904 | * Creates and executes a new AsyncTask to load the outlines for a page of content. |
| 905 | */ |
| 906 | private void prepareGenerateHoloOutlinesTask(int page, ArrayList<Object> items, |
| 907 | ArrayList<Bitmap> images) { |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 908 | // Prune old tasks for this page |
| 909 | Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator(); |
| 910 | while (iter.hasNext()) { |
| 911 | AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next(); |
| 912 | int taskPage = task.page; |
| 913 | if ((taskPage == page) && |
| 914 | (task.dataType == AsyncTaskPageData.Type.LoadHolographicIconsData)) { |
| 915 | task.cancel(false); |
| 916 | iter.remove(); |
| 917 | } |
| 918 | } |
| 919 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 920 | AsyncTaskPageData pageData = new AsyncTaskPageData(page, items, images, |
| 921 | new AsyncTaskCallback() { |
| 922 | @Override |
| 923 | public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) { |
Winson Chung | 0994593 | 2011-09-20 14:22:40 -0700 | [diff] [blame] | 924 | try { |
| 925 | // Ensure that this task starts running at the correct priority |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 926 | task.syncThreadPriority(); |
| 927 | |
Winson Chung | 0994593 | 2011-09-20 14:22:40 -0700 | [diff] [blame] | 928 | ArrayList<Bitmap> images = data.generatedImages; |
| 929 | ArrayList<Bitmap> srcImages = data.sourceImages; |
| 930 | int count = srcImages.size(); |
| 931 | Canvas c = new Canvas(); |
| 932 | for (int i = 0; i < count && !task.isCancelled(); ++i) { |
| 933 | // Before work on each item, ensure that this task is running at the correct |
| 934 | // priority |
| 935 | task.syncThreadPriority(); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 936 | |
Winson Chung | 0994593 | 2011-09-20 14:22:40 -0700 | [diff] [blame] | 937 | Bitmap b = srcImages.get(i); |
| 938 | Bitmap outline = Bitmap.createBitmap(b.getWidth(), b.getHeight(), |
| 939 | Bitmap.Config.ARGB_8888); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 940 | |
Winson Chung | 0994593 | 2011-09-20 14:22:40 -0700 | [diff] [blame] | 941 | c.setBitmap(outline); |
| 942 | c.save(); |
| 943 | c.drawBitmap(b, 0, 0, null); |
| 944 | c.restore(); |
| 945 | c.setBitmap(null); |
| 946 | |
| 947 | images.add(outline); |
| 948 | } |
| 949 | } finally { |
| 950 | if (task.isCancelled()) { |
| 951 | data.cleanup(true); |
| 952 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 953 | } |
| 954 | } |
| 955 | }, |
| 956 | new AsyncTaskCallback() { |
| 957 | @Override |
| 958 | public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) { |
Winson Chung | 0994593 | 2011-09-20 14:22:40 -0700 | [diff] [blame] | 959 | try { |
| 960 | mRunningTasks.remove(task); |
| 961 | if (task.isCancelled()) return; |
Winson Chung | 0994593 | 2011-09-20 14:22:40 -0700 | [diff] [blame] | 962 | onHolographicPageItemsLoaded(data); |
| 963 | } finally { |
| 964 | data.cleanup(task.isCancelled()); |
| 965 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 966 | } |
| 967 | }); |
| 968 | |
| 969 | // Ensure that the outline task always runs in the background, serially |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 970 | AppsCustomizeAsyncTask t = |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 971 | new AppsCustomizeAsyncTask(page, AsyncTaskPageData.Type.LoadHolographicIconsData); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 972 | t.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 973 | t.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, pageData); |
| 974 | mRunningTasks.add(t); |
| 975 | } |
| 976 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 977 | /* |
| 978 | * Widgets PagedView implementation |
| 979 | */ |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 980 | private void setupPage(PagedViewGridLayout layout) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 981 | layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, |
| 982 | mPageLayoutPaddingRight, mPageLayoutPaddingBottom); |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 983 | |
| 984 | // Note: We force a measure here to get around the fact that when we do layout calculations |
Winson Chung | d52f3d8 | 2011-07-12 14:29:11 -0700 | [diff] [blame] | 985 | // immediately after syncing, we don't have a proper width. |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 986 | int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST); |
| 987 | int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 988 | layout.setMinimumWidth(getPageContentWidth()); |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 989 | layout.measure(widthSpec, heightSpec); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 990 | } |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 991 | |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 992 | private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) { |
| 993 | renderDrawableToBitmap(d, bitmap, x, y, w, h, 1f, 0xFFFFFFFF); |
Winson Chung | 70fc438 | 2011-08-08 15:31:33 -0700 | [diff] [blame] | 994 | } |
| 995 | private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h, |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 996 | float scale) { |
| 997 | renderDrawableToBitmap(d, bitmap, x, y, w, h, scale, 0xFFFFFFFF); |
| 998 | } |
| 999 | private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h, |
| 1000 | float scale, int multiplyColor) { |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 1001 | if (bitmap != null) { |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1002 | Canvas c = new Canvas(bitmap); |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1003 | c.scale(scale, scale); |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 1004 | Rect oldBounds = d.copyBounds(); |
| 1005 | d.setBounds(x, y, x + w, y + h); |
| 1006 | d.draw(c); |
| 1007 | d.setBounds(oldBounds); // Restore the bounds |
Winson Chung | 70fc438 | 2011-08-08 15:31:33 -0700 | [diff] [blame] | 1008 | if (multiplyColor != 0xFFFFFFFF) { |
| 1009 | c.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY); |
| 1010 | } |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 1011 | c.setBitmap(null); |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 1012 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1013 | } |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1014 | private Bitmap getShortcutPreview(ResolveInfo info) { |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1015 | // Render the background |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 1016 | int offset = 0; |
| 1017 | int bitmapSize = mAppIconSize; |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1018 | Bitmap preview = Bitmap.createBitmap(bitmapSize, bitmapSize, Config.ARGB_8888); |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1019 | |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 1020 | // Render the icon |
Winson Chung | 0b9fcf5 | 2011-10-31 13:05:15 -0700 | [diff] [blame] | 1021 | Drawable icon = mIconCache.getFullResIcon(info); |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1022 | renderDrawableToBitmap(icon, preview, offset, offset, mAppIconSize, mAppIconSize); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1023 | return preview; |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 1024 | } |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 1025 | |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1026 | private Bitmap getWidgetPreview(ComponentName provider, int previewImage, int iconId, |
| 1027 | int cellHSpan, int cellVSpan, int maxWidth, int maxHeight) { |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 1028 | // Load the preview image if possible |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1029 | String packageName = provider.getPackageName(); |
| 1030 | if (maxWidth < 0) maxWidth = Integer.MAX_VALUE; |
| 1031 | if (maxHeight < 0) maxHeight = Integer.MAX_VALUE; |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 1032 | |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1033 | Drawable drawable = null; |
| 1034 | if (previewImage != 0) { |
| 1035 | drawable = mPackageManager.getDrawable(packageName, previewImage, null); |
| 1036 | if (drawable == null) { |
| 1037 | Log.w(LOG_TAG, "Can't load widget preview drawable 0x" + |
| 1038 | Integer.toHexString(previewImage) + " for provider: " + provider); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 1039 | } |
| 1040 | } |
| 1041 | |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1042 | int bitmapWidth; |
| 1043 | int bitmapHeight; |
| 1044 | boolean widgetPreviewExists = (drawable != null); |
| 1045 | if (widgetPreviewExists) { |
| 1046 | bitmapWidth = drawable.getIntrinsicWidth(); |
| 1047 | bitmapHeight = drawable.getIntrinsicHeight(); |
| 1048 | |
| 1049 | // Cap the size so widget previews don't appear larger than the actual widget |
| 1050 | maxWidth = Math.min(maxWidth, mWidgetSpacingLayout.estimateCellWidth(cellHSpan)); |
| 1051 | maxHeight = Math.min(maxHeight, mWidgetSpacingLayout.estimateCellHeight(cellVSpan)); |
| 1052 | } else { |
| 1053 | // Determine the size of the bitmap for the preview image we will generate |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1054 | // TODO: This actually uses the apps customize cell layout params, where as we make want |
| 1055 | // the Workspace params for more accuracy. |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1056 | bitmapWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan); |
| 1057 | bitmapHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan); |
Winson Chung | 273c102 | 2011-07-11 13:40:52 -0700 | [diff] [blame] | 1058 | if (cellHSpan == cellVSpan) { |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1059 | // For square widgets, we just have a fixed size for 1x1 and larger-than-1x1 |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1060 | int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage); |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1061 | if (cellHSpan <= 1) { |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 1062 | bitmapWidth = bitmapHeight = mAppIconSize + 2 * minOffset; |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1063 | } else { |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 1064 | bitmapWidth = bitmapHeight = mAppIconSize + 4 * minOffset; |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1065 | } |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 1066 | } |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | float scale = 1f; |
| 1070 | if (bitmapWidth > maxWidth) { |
| 1071 | scale = maxWidth / (float) bitmapWidth; |
| 1072 | } |
| 1073 | if (bitmapHeight * scale > maxHeight) { |
| 1074 | scale = maxHeight / (float) bitmapHeight; |
| 1075 | } |
| 1076 | if (scale != 1f) { |
| 1077 | bitmapWidth = (int) (scale * bitmapWidth); |
| 1078 | bitmapHeight = (int) (scale * bitmapHeight); |
| 1079 | } |
| 1080 | |
| 1081 | Bitmap preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888); |
| 1082 | |
| 1083 | if (widgetPreviewExists) { |
| 1084 | renderDrawableToBitmap(drawable, preview, 0, 0, bitmapWidth, bitmapHeight); |
| 1085 | } else { |
| 1086 | // Generate a preview image if we couldn't load one |
| 1087 | int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage); |
| 1088 | int smallestSide = Math.min(bitmapWidth, bitmapHeight); |
| 1089 | float iconScale = Math.min((float) smallestSide / (mAppIconSize + 2 * minOffset), 1f); |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 1090 | if (cellHSpan != 1 || cellVSpan != 1) { |
| 1091 | renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, bitmapWidth, |
| 1092 | bitmapHeight); |
| 1093 | } |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 1094 | |
| 1095 | // Draw the icon in the top left corner |
| 1096 | try { |
| 1097 | Drawable icon = null; |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 1098 | int hoffset = (int) (bitmapWidth / 2 - mAppIconSize * iconScale / 2); |
| 1099 | int yoffset = (int) (bitmapHeight / 2 - mAppIconSize * iconScale / 2); |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1100 | if (iconId > 0) icon = mIconCache.getFullResIcon(packageName, iconId); |
Michael Jurka | 31234d8 | 2011-11-10 15:15:15 -0800 | [diff] [blame] | 1101 | Resources resources = mLauncher.getResources(); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 1102 | if (icon == null) icon = resources.getDrawable(R.drawable.ic_launcher_application); |
| 1103 | |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 1104 | renderDrawableToBitmap(icon, preview, hoffset, yoffset, |
| 1105 | (int) (mAppIconSize * iconScale), |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1106 | (int) (mAppIconSize * iconScale)); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 1107 | } catch (Resources.NotFoundException e) {} |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 1108 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1109 | return preview; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1110 | } |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1111 | |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1112 | public void syncWidgetPageItems(final int page, final boolean immediate) { |
Winson Chung | 6a3fd3f | 2011-08-02 14:03:26 -0700 | [diff] [blame] | 1113 | int numItemsPerPage = mWidgetCountX * mWidgetCountY; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1114 | |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 1115 | // Calculate the dimensions of each cell we are giving to each widget |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1116 | final ArrayList<Object> items = new ArrayList<Object>(); |
| 1117 | int contentWidth = mWidgetSpacingLayout.getContentWidth(); |
| 1118 | final int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight |
Winson Chung | 6a3fd3f | 2011-08-02 14:03:26 -0700 | [diff] [blame] | 1119 | - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX); |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1120 | int contentHeight = mWidgetSpacingLayout.getContentHeight(); |
| 1121 | final int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom |
Winson Chung | 6a3fd3f | 2011-08-02 14:03:26 -0700 | [diff] [blame] | 1122 | - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY); |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 1123 | |
Winson Chung | e4a647f | 2011-09-30 14:41:25 -0700 | [diff] [blame] | 1124 | // Prepare the set of widgets to load previews for in the background |
Winson Chung | 6a3fd3f | 2011-08-02 14:03:26 -0700 | [diff] [blame] | 1125 | int offset = page * numItemsPerPage; |
| 1126 | for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) { |
| 1127 | items.add(mWidgets.get(i)); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
Winson Chung | e4a647f | 2011-09-30 14:41:25 -0700 | [diff] [blame] | 1130 | // Prepopulate the pages with the other widget info, and fill in the previews later |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1131 | final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page + mNumAppsPages); |
Winson Chung | e4a647f | 2011-09-30 14:41:25 -0700 | [diff] [blame] | 1132 | layout.setColumnCount(layout.getCellCountX()); |
| 1133 | for (int i = 0; i < items.size(); ++i) { |
| 1134 | Object rawInfo = items.get(i); |
| 1135 | PendingAddItemInfo createItemInfo = null; |
| 1136 | PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate( |
| 1137 | R.layout.apps_customize_widget, layout, false); |
| 1138 | if (rawInfo instanceof AppWidgetProviderInfo) { |
| 1139 | // Fill in the widget information |
| 1140 | AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo; |
| 1141 | createItemInfo = new PendingAddWidgetInfo(info, null, null); |
| 1142 | int[] cellSpans = mLauncher.getSpanForWidget(info, null); |
| 1143 | widget.applyFromAppWidgetProviderInfo(info, -1, cellSpans, |
| 1144 | mHolographicOutlineHelper); |
| 1145 | widget.setTag(createItemInfo); |
| 1146 | } else if (rawInfo instanceof ResolveInfo) { |
| 1147 | // Fill in the shortcuts information |
| 1148 | ResolveInfo info = (ResolveInfo) rawInfo; |
| 1149 | createItemInfo = new PendingAddItemInfo(); |
| 1150 | createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT; |
| 1151 | createItemInfo.componentName = new ComponentName(info.activityInfo.packageName, |
| 1152 | info.activityInfo.name); |
| 1153 | widget.applyFromResolveInfo(mPackageManager, info, mHolographicOutlineHelper); |
| 1154 | widget.setTag(createItemInfo); |
| 1155 | } |
| 1156 | widget.setOnClickListener(this); |
| 1157 | widget.setOnLongClickListener(this); |
| 1158 | widget.setOnTouchListener(this); |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 1159 | widget.setOnKeyListener(this); |
Winson Chung | e4a647f | 2011-09-30 14:41:25 -0700 | [diff] [blame] | 1160 | |
| 1161 | // Layout each widget |
| 1162 | int ix = i % mWidgetCountX; |
| 1163 | int iy = i / mWidgetCountX; |
| 1164 | GridLayout.LayoutParams lp = new GridLayout.LayoutParams( |
| 1165 | GridLayout.spec(iy, GridLayout.LEFT), |
| 1166 | GridLayout.spec(ix, GridLayout.TOP)); |
| 1167 | lp.width = cellWidth; |
| 1168 | lp.height = cellHeight; |
| 1169 | lp.setGravity(Gravity.TOP | Gravity.LEFT); |
| 1170 | if (ix > 0) lp.leftMargin = mWidgetWidthGap; |
| 1171 | if (iy > 0) lp.topMargin = mWidgetHeightGap; |
| 1172 | layout.addView(widget, lp); |
| 1173 | } |
| 1174 | |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1175 | // wait until a call on onLayout to start loading, because |
| 1176 | // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out |
| 1177 | // TODO: can we do a measure/layout immediately? |
| 1178 | layout.setOnLayoutListener(new Runnable() { |
| 1179 | public void run() { |
| 1180 | // Load the widget previews |
| 1181 | int maxPreviewWidth = cellWidth; |
| 1182 | int maxPreviewHeight = cellHeight; |
| 1183 | if (layout.getChildCount() > 0) { |
| 1184 | PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0); |
| 1185 | int[] maxSize = w.getPreviewSize(); |
| 1186 | maxPreviewWidth = maxSize[0]; |
| 1187 | maxPreviewHeight = maxSize[1]; |
| 1188 | } |
| 1189 | if (immediate) { |
| 1190 | AsyncTaskPageData data = new AsyncTaskPageData(page, items, |
| 1191 | maxPreviewWidth, maxPreviewHeight, null, null); |
| 1192 | loadWidgetPreviewsInBackground(null, data); |
| 1193 | onSyncWidgetPageItems(data); |
| 1194 | } else { |
| 1195 | prepareLoadWidgetPreviewsTask(page, items, |
| 1196 | maxPreviewWidth, maxPreviewHeight, mWidgetCountX); |
| 1197 | } |
| 1198 | } |
| 1199 | }); |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1200 | } |
| 1201 | private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task, |
| 1202 | AsyncTaskPageData data) { |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1203 | // loadWidgetPreviewsInBackground can be called without a task to load a set of widget |
| 1204 | // previews synchronously |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1205 | if (task != null) { |
| 1206 | // Ensure that this task starts running at the correct priority |
| 1207 | task.syncThreadPriority(); |
| 1208 | } |
| 1209 | |
| 1210 | // Load each of the widget/shortcut previews |
| 1211 | ArrayList<Object> items = data.items; |
| 1212 | ArrayList<Bitmap> images = data.generatedImages; |
| 1213 | int count = items.size(); |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1214 | for (int i = 0; i < count; ++i) { |
| 1215 | if (task != null) { |
| 1216 | // Ensure we haven't been cancelled yet |
| 1217 | if (task.isCancelled()) break; |
| 1218 | // Before work on each item, ensure that this task is running at the correct |
| 1219 | // priority |
| 1220 | task.syncThreadPriority(); |
| 1221 | } |
| 1222 | |
| 1223 | Object rawInfo = items.get(i); |
| 1224 | if (rawInfo instanceof AppWidgetProviderInfo) { |
| 1225 | AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo; |
Adam Cohen | f814aa0 | 2011-09-01 13:48:05 -0700 | [diff] [blame] | 1226 | int[] cellSpans = mLauncher.getSpanForWidget(info, null); |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1227 | Bitmap b = getWidgetPreview(info.provider, info.previewImage, info.icon, |
| 1228 | cellSpans[0], cellSpans[1], data.maxImageWidth, data.maxImageHeight); |
| 1229 | images.add(b); |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1230 | } else if (rawInfo instanceof ResolveInfo) { |
| 1231 | // Fill in the shortcuts information |
| 1232 | ResolveInfo info = (ResolveInfo) rawInfo; |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1233 | images.add(getShortcutPreview(info)); |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1234 | } |
| 1235 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1236 | } |
| 1237 | private void onSyncWidgetPageItems(AsyncTaskPageData data) { |
| 1238 | int page = data.page; |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1239 | PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page + mNumAppsPages); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1240 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1241 | ArrayList<Object> items = data.items; |
| 1242 | int count = items.size(); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1243 | for (int i = 0; i < count; ++i) { |
Winson Chung | e4a647f | 2011-09-30 14:41:25 -0700 | [diff] [blame] | 1244 | PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i); |
| 1245 | if (widget != null) { |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1246 | Bitmap preview = data.generatedImages.get(i); |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1247 | widget.applyPreview(new FastBitmapDrawable(preview), i); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 1248 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1249 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1250 | |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1251 | layout.createHardwareLayer(); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1252 | invalidate(); |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1253 | |
Winson Chung | 8ff8713 | 2011-09-30 19:40:46 -0700 | [diff] [blame] | 1254 | /* TEMPORARILY DISABLE HOLOGRAPHIC ICONS |
Winson Chung | 54c725c | 2011-08-03 12:03:40 -0700 | [diff] [blame] | 1255 | if (mFadeInAdjacentScreens) { |
| 1256 | prepareGenerateHoloOutlinesTask(data.page, data.items, data.generatedImages); |
| 1257 | } |
Winson Chung | 8ff8713 | 2011-09-30 19:40:46 -0700 | [diff] [blame] | 1258 | */ |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1259 | |
| 1260 | // Update all thread priorities |
| 1261 | Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator(); |
| 1262 | while (iter.hasNext()) { |
| 1263 | AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next(); |
| 1264 | int pageIndex = task.page + mNumAppsPages; |
| 1265 | task.setThreadPriority(getThreadPriorityForPage(pageIndex)); |
| 1266 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1267 | } |
| 1268 | private void onHolographicPageItemsLoaded(AsyncTaskPageData data) { |
| 1269 | // Invalidate early to short-circuit children invalidates |
| 1270 | invalidate(); |
| 1271 | |
| 1272 | int page = data.page; |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1273 | ViewGroup layout = (ViewGroup) getPageAt(page); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1274 | if (layout instanceof PagedViewCellLayout) { |
| 1275 | PagedViewCellLayout cl = (PagedViewCellLayout) layout; |
| 1276 | int count = cl.getPageChildCount(); |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 1277 | if (count != data.generatedImages.size()) return; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1278 | for (int i = 0; i < count; ++i) { |
| 1279 | PagedViewIcon icon = (PagedViewIcon) cl.getChildOnPageAt(i); |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 1280 | icon.setHolographicOutline(data.generatedImages.get(i)); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1281 | } |
| 1282 | } else { |
| 1283 | int count = layout.getChildCount(); |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 1284 | if (count != data.generatedImages.size()) return; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1285 | for (int i = 0; i < count; ++i) { |
| 1286 | View v = layout.getChildAt(i); |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 1287 | ((PagedViewWidget) v).setHolographicOutline(data.generatedImages.get(i)); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1288 | } |
| 1289 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1290 | } |
Winson Chung | 46af2e8 | 2011-05-09 16:00:53 -0700 | [diff] [blame] | 1291 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1292 | @Override |
| 1293 | public void syncPages() { |
| 1294 | removeAllViews(); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1295 | cancelAllTasks(); |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 1296 | |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1297 | Context context = getContext(); |
| 1298 | for (int j = 0; j < mNumWidgetPages; ++j) { |
| 1299 | PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX, |
| 1300 | mWidgetCountY); |
| 1301 | setupPage(layout); |
| 1302 | addView(layout, new PagedViewGridLayout.LayoutParams(LayoutParams.MATCH_PARENT, |
| 1303 | LayoutParams.MATCH_PARENT)); |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 1304 | } |
| 1305 | |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1306 | for (int i = 0; i < mNumAppsPages; ++i) { |
| 1307 | PagedViewCellLayout layout = new PagedViewCellLayout(context); |
| 1308 | setupPage(layout); |
| 1309 | addView(layout); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1310 | } |
| 1311 | } |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1312 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1313 | @Override |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1314 | public void syncPageItems(int page, boolean immediate) { |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1315 | if (page < mNumAppsPages) { |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1316 | syncAppsPageItems(page, immediate); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1317 | } else { |
| 1318 | syncWidgetPageItems(page - mNumAppsPages, immediate); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1319 | } |
| 1320 | } |
| 1321 | |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1322 | // We want our pages to be z-ordered such that the further a page is to the left, the higher |
| 1323 | // it is in the z-order. This is important to insure touch events are handled correctly. |
| 1324 | View getPageAt(int index) { |
| 1325 | return getChildAt(getChildCount() - index - 1); |
| 1326 | } |
| 1327 | |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 1328 | @Override |
| 1329 | protected int indexToPage(int index) { |
| 1330 | return getChildCount() - index - 1; |
| 1331 | } |
| 1332 | |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1333 | // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack. |
| 1334 | @Override |
| 1335 | protected void screenScrolled(int screenCenter) { |
| 1336 | super.screenScrolled(screenCenter); |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1337 | |
| 1338 | for (int i = 0; i < getChildCount(); i++) { |
| 1339 | View v = getPageAt(i); |
| 1340 | if (v != null) { |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1341 | float scrollProgress = getScrollProgress(screenCenter, v, i); |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1342 | |
| 1343 | float interpolatedProgress = |
| 1344 | mZInterpolator.getInterpolation(Math.abs(Math.min(scrollProgress, 0))); |
| 1345 | float scale = (1 - interpolatedProgress) + |
| 1346 | interpolatedProgress * TRANSITION_SCALE_FACTOR; |
| 1347 | float translationX = Math.min(0, scrollProgress) * v.getMeasuredWidth(); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1348 | |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 1349 | float alpha; |
| 1350 | |
| 1351 | if (!LauncherApplication.isScreenLarge() || scrollProgress < 0) { |
| 1352 | alpha = scrollProgress < 0 ? mAlphaInterpolator.getInterpolation( |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1353 | 1 - Math.abs(scrollProgress)) : 1.0f; |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 1354 | } else { |
| 1355 | // On large screens we need to fade the page as it nears its leftmost position |
| 1356 | alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress); |
| 1357 | } |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1358 | |
| 1359 | v.setCameraDistance(mDensity * CAMERA_DISTANCE); |
| 1360 | int pageWidth = v.getMeasuredWidth(); |
| 1361 | int pageHeight = v.getMeasuredHeight(); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1362 | |
| 1363 | if (PERFORM_OVERSCROLL_ROTATION) { |
| 1364 | if (i == 0 && scrollProgress < 0) { |
| 1365 | // Overscroll to the left |
| 1366 | v.setPivotX(TRANSITION_PIVOT * pageWidth); |
| 1367 | v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress); |
| 1368 | scale = 1.0f; |
| 1369 | alpha = 1.0f; |
| 1370 | // On the first page, we don't want the page to have any lateral motion |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 1371 | translationX = 0; |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1372 | } else if (i == getChildCount() - 1 && scrollProgress > 0) { |
| 1373 | // Overscroll to the right |
| 1374 | v.setPivotX((1 - TRANSITION_PIVOT) * pageWidth); |
| 1375 | v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress); |
| 1376 | scale = 1.0f; |
| 1377 | alpha = 1.0f; |
| 1378 | // On the last page, we don't want the page to have any lateral motion. |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 1379 | translationX = 0; |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1380 | } else { |
| 1381 | v.setPivotY(pageHeight / 2.0f); |
| 1382 | v.setPivotX(pageWidth / 2.0f); |
| 1383 | v.setRotationY(0f); |
| 1384 | } |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | v.setTranslationX(translationX); |
| 1388 | v.setScaleX(scale); |
| 1389 | v.setScaleY(scale); |
| 1390 | v.setAlpha(alpha); |
Adam Cohen | 4e84401 | 2011-11-09 13:48:04 -0800 | [diff] [blame] | 1391 | |
| 1392 | // If the view has 0 alpha, we set it to be invisible so as to prevent |
| 1393 | // it from accepting touches |
| 1394 | if (alpha < ViewConfiguration.ALPHA_THRESHOLD) { |
| 1395 | v.setVisibility(INVISIBLE); |
| 1396 | } else if (v.getVisibility() != VISIBLE) { |
| 1397 | v.setVisibility(VISIBLE); |
| 1398 | } |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1399 | } |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1403 | protected void overScroll(float amount) { |
Adam Cohen | cff6af8 | 2011-09-13 14:51:53 -0700 | [diff] [blame] | 1404 | acceleratedOverScroll(amount); |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1405 | } |
| 1406 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1407 | /** |
| 1408 | * Used by the parent to get the content width to set the tab bar to |
| 1409 | * @return |
| 1410 | */ |
| 1411 | public int getPageContentWidth() { |
| 1412 | return mContentWidth; |
| 1413 | } |
| 1414 | |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 1415 | @Override |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 1416 | protected void onPageEndMoving() { |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 1417 | super.onPageEndMoving(); |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 1418 | |
| 1419 | // We reset the save index when we change pages so that it will be recalculated on next |
| 1420 | // rotation |
| 1421 | mSaveInstanceStateItemIndex = -1; |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 1422 | } |
| 1423 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1424 | /* |
| 1425 | * AllAppsView implementation |
| 1426 | */ |
| 1427 | @Override |
| 1428 | public void setup(Launcher launcher, DragController dragController) { |
| 1429 | mLauncher = launcher; |
| 1430 | mDragController = dragController; |
| 1431 | } |
| 1432 | @Override |
| 1433 | public void zoom(float zoom, boolean animate) { |
| 1434 | // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed() |
| 1435 | } |
| 1436 | @Override |
| 1437 | public boolean isVisible() { |
| 1438 | return (getVisibility() == VISIBLE); |
| 1439 | } |
| 1440 | @Override |
| 1441 | public boolean isAnimating() { |
| 1442 | return false; |
| 1443 | } |
| 1444 | @Override |
| 1445 | public void setApps(ArrayList<ApplicationInfo> list) { |
| 1446 | mApps = list; |
| 1447 | Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR); |
Winson Chung | 4b0ed8c | 2011-10-19 15:24:49 -0700 | [diff] [blame] | 1448 | updatePageCounts(); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 1449 | |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 1450 | // The next layout pass will trigger data-ready if both widgets and apps are set, so |
| 1451 | // request a layout to do this test and invalidate the page data when ready. |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 1452 | if (testDataReady()) requestLayout(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1453 | } |
| 1454 | private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) { |
| 1455 | // We add it in place, in alphabetical order |
| 1456 | int count = list.size(); |
| 1457 | for (int i = 0; i < count; ++i) { |
| 1458 | ApplicationInfo info = list.get(i); |
| 1459 | int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR); |
| 1460 | if (index < 0) { |
| 1461 | mApps.add(-(index + 1), info); |
| 1462 | } |
| 1463 | } |
| 1464 | } |
| 1465 | @Override |
| 1466 | public void addApps(ArrayList<ApplicationInfo> list) { |
| 1467 | addAppsWithoutInvalidate(list); |
Winson Chung | 4b0ed8c | 2011-10-19 15:24:49 -0700 | [diff] [blame] | 1468 | updatePageCounts(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1469 | invalidatePageData(); |
| 1470 | } |
| 1471 | private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) { |
| 1472 | ComponentName removeComponent = item.intent.getComponent(); |
| 1473 | int length = list.size(); |
| 1474 | for (int i = 0; i < length; ++i) { |
| 1475 | ApplicationInfo info = list.get(i); |
| 1476 | if (info.intent.getComponent().equals(removeComponent)) { |
| 1477 | return i; |
| 1478 | } |
| 1479 | } |
| 1480 | return -1; |
| 1481 | } |
| 1482 | private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) { |
| 1483 | // loop through all the apps and remove apps that have the same component |
| 1484 | int length = list.size(); |
| 1485 | for (int i = 0; i < length; ++i) { |
| 1486 | ApplicationInfo info = list.get(i); |
| 1487 | int removeIndex = findAppByComponent(mApps, info); |
| 1488 | if (removeIndex > -1) { |
| 1489 | mApps.remove(removeIndex); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1490 | } |
| 1491 | } |
| 1492 | } |
| 1493 | @Override |
| 1494 | public void removeApps(ArrayList<ApplicationInfo> list) { |
| 1495 | removeAppsWithoutInvalidate(list); |
Winson Chung | 4b0ed8c | 2011-10-19 15:24:49 -0700 | [diff] [blame] | 1496 | updatePageCounts(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1497 | invalidatePageData(); |
| 1498 | } |
| 1499 | @Override |
| 1500 | public void updateApps(ArrayList<ApplicationInfo> list) { |
| 1501 | // We remove and re-add the updated applications list because it's properties may have |
| 1502 | // changed (ie. the title), and this will ensure that the items will be in their proper |
| 1503 | // place in the list. |
| 1504 | removeAppsWithoutInvalidate(list); |
| 1505 | addAppsWithoutInvalidate(list); |
Winson Chung | 4b0ed8c | 2011-10-19 15:24:49 -0700 | [diff] [blame] | 1506 | updatePageCounts(); |
| 1507 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1508 | invalidatePageData(); |
| 1509 | } |
Michael Jurka | 35aa14d | 2011-07-07 17:01:08 -0700 | [diff] [blame] | 1510 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1511 | @Override |
| 1512 | public void reset() { |
Adam Cohen | b64d36e | 2011-10-17 21:48:02 -0700 | [diff] [blame] | 1513 | AppsCustomizeTabHost tabHost = getTabHost(); |
| 1514 | String tag = tabHost.getCurrentTabTag(); |
Winson Chung | 6a8103c | 2011-10-21 11:08:32 -0700 | [diff] [blame] | 1515 | if (tag != null) { |
| 1516 | if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) { |
| 1517 | tabHost.setCurrentTabFromContent(ContentType.Applications); |
| 1518 | } |
Adam Cohen | b64d36e | 2011-10-17 21:48:02 -0700 | [diff] [blame] | 1519 | } |
| 1520 | if (mCurrentPage != 0) { |
| 1521 | invalidatePageData(0); |
| 1522 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1523 | } |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1524 | |
| 1525 | private AppsCustomizeTabHost getTabHost() { |
| 1526 | return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane); |
| 1527 | } |
| 1528 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1529 | @Override |
| 1530 | public void dumpState() { |
| 1531 | // TODO: Dump information related to current list of Applications, Widgets, etc. |
| 1532 | ApplicationInfo.dumpApplicationInfoList(LOG_TAG, "mApps", mApps); |
| 1533 | dumpAppWidgetProviderInfoList(LOG_TAG, "mWidgets", mWidgets); |
| 1534 | } |
Adam Cohen | 4e84401 | 2011-11-09 13:48:04 -0800 | [diff] [blame] | 1535 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1536 | private void dumpAppWidgetProviderInfoList(String tag, String label, |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 1537 | ArrayList<Object> list) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1538 | Log.d(tag, label + " size=" + list.size()); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 1539 | for (Object i: list) { |
| 1540 | if (i instanceof AppWidgetProviderInfo) { |
| 1541 | AppWidgetProviderInfo info = (AppWidgetProviderInfo) i; |
| 1542 | Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage |
| 1543 | + " resizeMode=" + info.resizeMode + " configure=" + info.configure |
| 1544 | + " initialLayout=" + info.initialLayout |
| 1545 | + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight); |
| 1546 | } else if (i instanceof ResolveInfo) { |
| 1547 | ResolveInfo info = (ResolveInfo) i; |
| 1548 | Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon=" |
| 1549 | + info.icon); |
| 1550 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1551 | } |
| 1552 | } |
Adam Cohen | 4e84401 | 2011-11-09 13:48:04 -0800 | [diff] [blame] | 1553 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1554 | @Override |
| 1555 | public void surrender() { |
| 1556 | // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we |
| 1557 | // should stop this now. |
Winson Chung | 4b0ed8c | 2011-10-19 15:24:49 -0700 | [diff] [blame] | 1558 | |
| 1559 | // Stop all background tasks |
| 1560 | cancelAllTasks(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1561 | } |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1562 | |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1563 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1564 | /* |
| 1565 | * We load an extra page on each side to prevent flashes from scrolling and loading of the |
| 1566 | * widget previews in the background with the AsyncTasks. |
| 1567 | */ |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1568 | final static int sLookBehindPageCount = 2; |
| 1569 | final static int sLookAheadPageCount = 2; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1570 | protected int getAssociatedLowerPageBound(int page) { |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1571 | final int count = getChildCount(); |
| 1572 | int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1); |
| 1573 | int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0); |
| 1574 | return windowMinIndex; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1575 | } |
| 1576 | protected int getAssociatedUpperPageBound(int page) { |
| 1577 | final int count = getChildCount(); |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1578 | int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1); |
| 1579 | int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1), |
| 1580 | count - 1); |
| 1581 | return windowMaxIndex; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1582 | } |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1583 | |
| 1584 | @Override |
| 1585 | protected String getCurrentPageDescription() { |
| 1586 | int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage; |
| 1587 | int stringId = R.string.default_scroll_format; |
Adam Cohen | d3357b1 | 2011-10-18 14:58:11 -0700 | [diff] [blame] | 1588 | int count = 0; |
| 1589 | |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1590 | if (page < mNumAppsPages) { |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1591 | stringId = R.string.apps_customize_apps_scroll_format; |
Adam Cohen | d3357b1 | 2011-10-18 14:58:11 -0700 | [diff] [blame] | 1592 | count = mNumAppsPages; |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1593 | } else { |
| 1594 | page -= mNumAppsPages; |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1595 | stringId = R.string.apps_customize_widgets_scroll_format; |
Adam Cohen | d3357b1 | 2011-10-18 14:58:11 -0700 | [diff] [blame] | 1596 | count = mNumWidgetPages; |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1597 | } |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1598 | |
Adam Cohen | d3357b1 | 2011-10-18 14:58:11 -0700 | [diff] [blame] | 1599 | return String.format(mContext.getString(stringId), page + 1, count); |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1600 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1601 | } |