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