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