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