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); |
Michael Jurka | dbc1f65 | 2011-11-10 17:02:56 -0800 | [diff] [blame^] | 463 | for (AppWidgetProviderInfo widget : widgets) { |
| 464 | if (widget.minWidth > 0 && widget.minHeight > 0) { |
| 465 | mWidgets.add(widget); |
| 466 | } else { |
| 467 | Log.e(LOG_TAG, "Widget " + widget.provider + " has invalid dimensions (" + |
| 468 | widget.minWidth + ", " + widget.minHeight + ")"); |
| 469 | } |
| 470 | } |
Winson Chung | 6a3fd3f | 2011-08-02 14:03:26 -0700 | [diff] [blame] | 471 | mWidgets.addAll(shortcuts); |
| 472 | Collections.sort(mWidgets, |
| 473 | new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager)); |
Winson Chung | 4b0ed8c | 2011-10-19 15:24:49 -0700 | [diff] [blame] | 474 | updatePageCounts(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 475 | |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 476 | if (wasEmpty) { |
| 477 | // The next layout pass will trigger data-ready if both widgets and apps are set, so request |
| 478 | // a layout to do this test and invalidate the page data when ready. |
| 479 | if (testDataReady()) requestLayout(); |
| 480 | } else { |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 481 | cancelAllTasks(); |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 482 | invalidatePageData(); |
| 483 | } |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | @Override |
| 487 | public void onClick(View v) { |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 488 | // When we have exited all apps or are in transition, disregard clicks |
| 489 | if (!mLauncher.isAllAppsCustomizeOpen() || |
| 490 | mLauncher.getWorkspace().isSwitchingState()) return; |
| 491 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 492 | if (v instanceof PagedViewIcon) { |
| 493 | // Animate some feedback to the click |
| 494 | final ApplicationInfo appInfo = (ApplicationInfo) v.getTag(); |
| 495 | animateClickFeedback(v, new Runnable() { |
| 496 | @Override |
| 497 | public void run() { |
| 498 | mLauncher.startActivitySafely(appInfo.intent, appInfo); |
| 499 | } |
| 500 | }); |
| 501 | } else if (v instanceof PagedViewWidget) { |
Winson Chung | d2e87b3 | 2011-06-02 10:53:07 -0700 | [diff] [blame] | 502 | // Let the user know that they have to long press to add a widget |
| 503 | Toast.makeText(getContext(), R.string.long_press_widget_to_add, |
| 504 | Toast.LENGTH_SHORT).show(); |
Winson Chung | 46af2e8 | 2011-05-09 16:00:53 -0700 | [diff] [blame] | 505 | |
Winson Chung | d2e87b3 | 2011-06-02 10:53:07 -0700 | [diff] [blame] | 506 | // Create a little animation to show that the widget can move |
| 507 | float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY); |
| 508 | final ImageView p = (ImageView) v.findViewById(R.id.widget_preview); |
| 509 | AnimatorSet bounce = new AnimatorSet(); |
| 510 | ValueAnimator tyuAnim = ObjectAnimator.ofFloat(p, "translationY", offsetY); |
| 511 | tyuAnim.setDuration(125); |
| 512 | ValueAnimator tydAnim = ObjectAnimator.ofFloat(p, "translationY", 0f); |
| 513 | tydAnim.setDuration(100); |
| 514 | bounce.play(tyuAnim).before(tydAnim); |
| 515 | bounce.setInterpolator(new AccelerateInterpolator()); |
| 516 | bounce.start(); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 517 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | /* |
| 521 | * PagedViewWithDraggableItems implementation |
| 522 | */ |
| 523 | @Override |
| 524 | protected void determineDraggingStart(android.view.MotionEvent ev) { |
| 525 | // Disable dragging by pulling an app down for now. |
| 526 | } |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 527 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 528 | private void beginDraggingApplication(View v) { |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 529 | mLauncher.getWorkspace().onDragStartedWithItem(v); |
| 530 | mLauncher.getWorkspace().beginDragShared(v, this); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 531 | } |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 532 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 533 | private void beginDraggingWidget(View v) { |
| 534 | // Get the widget preview as the drag representation |
| 535 | ImageView image = (ImageView) v.findViewById(R.id.widget_preview); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 536 | PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag(); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 537 | |
| 538 | // Compose the drag image |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 539 | Bitmap b; |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 540 | Drawable preview = image.getDrawable(); |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 541 | RectF mTmpScaleRect = new RectF(0f,0f,1f,1f); |
| 542 | image.getImageMatrix().mapRect(mTmpScaleRect); |
| 543 | float scale = mTmpScaleRect.right; |
| 544 | int w = (int) (preview.getIntrinsicWidth() * scale); |
| 545 | int h = (int) (preview.getIntrinsicHeight() * scale); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 546 | if (createItemInfo instanceof PendingAddWidgetInfo) { |
| 547 | PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) createItemInfo; |
Adam Cohen | f814aa0 | 2011-09-01 13:48:05 -0700 | [diff] [blame] | 548 | int[] spanXY = mLauncher.getSpanForWidget(createWidgetInfo, null); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 549 | createItemInfo.spanX = spanXY[0]; |
| 550 | createItemInfo.spanY = spanXY[1]; |
| 551 | |
| 552 | b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 553 | renderDrawableToBitmap(preview, b, 0, 0, w, h, scale, mDragViewMultiplyColor); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 554 | } else { |
| 555 | // Workaround for the fact that we don't keep the original ResolveInfo associated with |
| 556 | // the shortcut around. To get the icon, we just render the preview image (which has |
| 557 | // the shortcut icon) to a new drag bitmap that clips the non-icon space. |
| 558 | b = Bitmap.createBitmap(mWidgetPreviewIconPaddedDimension, |
| 559 | mWidgetPreviewIconPaddedDimension, Bitmap.Config.ARGB_8888); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 560 | mCanvas.setBitmap(b); |
| 561 | mCanvas.save(); |
| 562 | preview.draw(mCanvas); |
| 563 | mCanvas.restore(); |
Winson Chung | 70fc438 | 2011-08-08 15:31:33 -0700 | [diff] [blame] | 564 | mCanvas.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY); |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 565 | mCanvas.setBitmap(null); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 566 | createItemInfo.spanX = createItemInfo.spanY = 1; |
| 567 | } |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 568 | |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 569 | // We use a custom alpha clip table for the default widget previews |
| 570 | Paint alphaClipPaint = null; |
| 571 | if (createItemInfo instanceof PendingAddWidgetInfo) { |
| 572 | if (((PendingAddWidgetInfo) createItemInfo).hasDefaultPreview) { |
| 573 | MaskFilter alphaClipTable = TableMaskFilter.CreateClipTable(0, 255); |
| 574 | alphaClipPaint = new Paint(); |
| 575 | alphaClipPaint.setMaskFilter(alphaClipTable); |
| 576 | } |
| 577 | } |
| 578 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 579 | // Start the drag |
Adam Cohen | 446e940 | 2011-09-15 18:21:21 -0700 | [diff] [blame] | 580 | mLauncher.lockScreenOrientationOnLargeUI(); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 581 | mLauncher.getWorkspace().onDragStartedWithItemSpans(createItemInfo.spanX, |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 582 | createItemInfo.spanY, b, alphaClipPaint); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 583 | mDragController.startDrag(image, b, this, createItemInfo, |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 584 | DragController.DRAG_ACTION_COPY, null); |
| 585 | b.recycle(); |
| 586 | } |
| 587 | @Override |
| 588 | protected boolean beginDragging(View v) { |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 589 | // Dismiss the cling |
| 590 | mLauncher.dismissAllAppsCling(null); |
| 591 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 592 | if (!super.beginDragging(v)) return false; |
| 593 | |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 594 | // Go into spring loaded mode (must happen before we startDrag()) |
Adam Cohen | 7777d96 | 2011-08-18 18:58:38 -0700 | [diff] [blame] | 595 | mLauncher.enterSpringLoadedDragMode(); |
Winson Chung | fc79c80 | 2011-05-02 13:35:34 -0700 | [diff] [blame] | 596 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 597 | if (v instanceof PagedViewIcon) { |
| 598 | beginDraggingApplication(v); |
| 599 | } else if (v instanceof PagedViewWidget) { |
| 600 | beginDraggingWidget(v); |
| 601 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 602 | return true; |
| 603 | } |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 604 | private void endDragging(View target, boolean success) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 605 | mLauncher.getWorkspace().onDragStopped(success); |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 606 | if (!success || (target != mLauncher.getWorkspace() && |
| 607 | !(target instanceof DeleteDropTarget))) { |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 608 | // Exit spring loaded mode if we have not successfully dropped or have not handled the |
| 609 | // drop in Workspace |
| 610 | mLauncher.exitSpringLoadedDragMode(); |
| 611 | } |
Adam Cohen | 446e940 | 2011-09-15 18:21:21 -0700 | [diff] [blame] | 612 | mLauncher.unlockScreenOrientationOnLargeUI(); |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 613 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 616 | @Override |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 617 | public void onDropCompleted(View target, DragObject d, boolean success) { |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 618 | endDragging(target, success); |
Winson Chung | fc79c80 | 2011-05-02 13:35:34 -0700 | [diff] [blame] | 619 | |
| 620 | // Display an error message if the drag failed due to there not being enough space on the |
| 621 | // target layout we were dropping on. |
| 622 | if (!success) { |
| 623 | boolean showOutOfSpaceMessage = false; |
| 624 | if (target instanceof Workspace) { |
| 625 | int currentScreen = mLauncher.getCurrentWorkspaceScreen(); |
| 626 | Workspace workspace = (Workspace) target; |
| 627 | CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 628 | ItemInfo itemInfo = (ItemInfo) d.dragInfo; |
Winson Chung | fc79c80 | 2011-05-02 13:35:34 -0700 | [diff] [blame] | 629 | if (layout != null) { |
| 630 | layout.calculateSpans(itemInfo); |
| 631 | showOutOfSpaceMessage = |
| 632 | !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY); |
| 633 | } |
| 634 | } |
Winson Chung | fc79c80 | 2011-05-02 13:35:34 -0700 | [diff] [blame] | 635 | if (showOutOfSpaceMessage) { |
| 636 | mLauncher.showOutOfSpaceMessage(); |
| 637 | } |
| 638 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 639 | } |
| 640 | |
Winson Chung | 7f0acdd | 2011-09-19 18:34:19 -0700 | [diff] [blame] | 641 | @Override |
| 642 | protected void onDetachedFromWindow() { |
| 643 | super.onDetachedFromWindow(); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 644 | cancelAllTasks(); |
| 645 | } |
Winson Chung | 7f0acdd | 2011-09-19 18:34:19 -0700 | [diff] [blame] | 646 | |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 647 | private void cancelAllTasks() { |
Winson Chung | 7f0acdd | 2011-09-19 18:34:19 -0700 | [diff] [blame] | 648 | // Clean up all the async tasks |
| 649 | Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator(); |
| 650 | while (iter.hasNext()) { |
| 651 | AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next(); |
| 652 | task.cancel(false); |
| 653 | iter.remove(); |
| 654 | } |
| 655 | } |
| 656 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 657 | public void setContentType(ContentType type) { |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 658 | if (type == ContentType.Widgets) { |
| 659 | invalidatePageData(mNumAppsPages, true); |
| 660 | } else if (type == ContentType.Applications) { |
| 661 | invalidatePageData(0, true); |
| 662 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 665 | protected void snapToPage(int whichPage, int delta, int duration) { |
| 666 | super.snapToPage(whichPage, delta, duration); |
| 667 | updateCurrentTab(whichPage); |
| 668 | } |
| 669 | |
| 670 | private void updateCurrentTab(int currentPage) { |
| 671 | AppsCustomizeTabHost tabHost = getTabHost(); |
| 672 | String tag = tabHost.getCurrentTabTag(); |
Winson Chung | 6a8103c | 2011-10-21 11:08:32 -0700 | [diff] [blame] | 673 | if (tag != null) { |
| 674 | if (currentPage >= mNumAppsPages && |
| 675 | !tag.equals(tabHost.getTabTagForContentType(ContentType.Widgets))) { |
| 676 | tabHost.setCurrentTabFromContent(ContentType.Widgets); |
| 677 | } else if (currentPage < mNumAppsPages && |
| 678 | !tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) { |
| 679 | tabHost.setCurrentTabFromContent(ContentType.Applications); |
| 680 | } |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 681 | } |
| 682 | } |
| 683 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 684 | /* |
| 685 | * Apps PagedView implementation |
| 686 | */ |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 687 | private void setVisibilityOnChildren(ViewGroup layout, int visibility) { |
| 688 | int childCount = layout.getChildCount(); |
| 689 | for (int i = 0; i < childCount; ++i) { |
| 690 | layout.getChildAt(i).setVisibility(visibility); |
| 691 | } |
| 692 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 693 | private void setupPage(PagedViewCellLayout layout) { |
| 694 | layout.setCellCount(mCellCountX, mCellCountY); |
| 695 | layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap); |
| 696 | layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, |
| 697 | mPageLayoutPaddingRight, mPageLayoutPaddingBottom); |
| 698 | |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 699 | // Note: We force a measure here to get around the fact that when we do layout calculations |
| 700 | // immediately after syncing, we don't have a proper width. That said, we already know the |
| 701 | // expected page width, so we can actually optimize by hiding all the TextView-based |
| 702 | // children that are expensive to measure, and let that happen naturally later. |
| 703 | setVisibilityOnChildren(layout, View.GONE); |
Winson Chung | db1138b | 2011-06-30 14:39:35 -0700 | [diff] [blame] | 704 | int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 705 | int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST); |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 706 | layout.setMinimumWidth(getPageContentWidth()); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 707 | layout.measure(widthSpec, heightSpec); |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 708 | setVisibilityOnChildren(layout, View.VISIBLE); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 709 | } |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 710 | |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 711 | public void syncAppsPageItems(int page, boolean immediate) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 712 | // ensure that we have the right number of items on the pages |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 713 | int numCells = mCellCountX * mCellCountY; |
| 714 | int startIndex = page * numCells; |
| 715 | int endIndex = Math.min(startIndex + numCells, mApps.size()); |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 716 | PagedViewCellLayout layout = (PagedViewCellLayout) getPageAt(page); |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 717 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 718 | layout.removeAllViewsOnPage(); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 719 | ArrayList<Object> items = new ArrayList<Object>(); |
| 720 | ArrayList<Bitmap> images = new ArrayList<Bitmap>(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 721 | for (int i = startIndex; i < endIndex; ++i) { |
| 722 | ApplicationInfo info = mApps.get(i); |
| 723 | PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate( |
| 724 | R.layout.apps_customize_application, layout, false); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 725 | icon.applyFromApplicationInfo(info, true, mHolographicOutlineHelper); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 726 | icon.setOnClickListener(this); |
| 727 | icon.setOnLongClickListener(this); |
| 728 | icon.setOnTouchListener(this); |
| 729 | |
| 730 | int index = i - startIndex; |
| 731 | int x = index % mCellCountX; |
| 732 | int y = index / mCellCountX; |
Winson Chung | 6a70e9f | 2011-05-17 16:24:49 -0700 | [diff] [blame] | 733 | layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1)); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 734 | |
| 735 | items.add(info); |
| 736 | images.add(info.iconBitmap); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 737 | } |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 738 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 739 | layout.createHardwareLayers(); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 740 | |
Winson Chung | 8ff8713 | 2011-09-30 19:40:46 -0700 | [diff] [blame] | 741 | /* TEMPORARILY DISABLE HOLOGRAPHIC ICONS |
Winson Chung | 54c725c | 2011-08-03 12:03:40 -0700 | [diff] [blame] | 742 | if (mFadeInAdjacentScreens) { |
| 743 | prepareGenerateHoloOutlinesTask(page, items, images); |
| 744 | } |
Winson Chung | 8ff8713 | 2011-09-30 19:40:46 -0700 | [diff] [blame] | 745 | */ |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 746 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 747 | |
| 748 | /** |
| 749 | * Return the appropriate thread priority for loading for a given page (we give the current |
| 750 | * page much higher priority) |
| 751 | */ |
| 752 | private int getThreadPriorityForPage(int page) { |
| 753 | // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below |
| 754 | int pageDiff = Math.abs(page - mCurrentPage); |
| 755 | if (pageDiff <= 0) { |
| 756 | // return Process.THREAD_PRIORITY_DEFAULT; |
| 757 | return Process.THREAD_PRIORITY_MORE_FAVORABLE; |
| 758 | } else if (pageDiff <= 1) { |
| 759 | // return Process.THREAD_PRIORITY_BACKGROUND; |
| 760 | return Process.THREAD_PRIORITY_DEFAULT; |
| 761 | } else { |
| 762 | // return Process.THREAD_PRIORITY_LOWEST; |
| 763 | return Process.THREAD_PRIORITY_DEFAULT; |
| 764 | } |
| 765 | } |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 766 | private int getSleepForPage(int page) { |
| 767 | int pageDiff = Math.abs(page - mCurrentPage) - 1; |
| 768 | return Math.max(0, pageDiff * sPageSleepDelay); |
| 769 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 770 | /** |
| 771 | * Creates and executes a new AsyncTask to load a page of widget previews. |
| 772 | */ |
| 773 | private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets, |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 774 | int cellWidth, int cellHeight, int cellCountX) { |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 775 | // Prune all tasks that are no longer needed |
| 776 | Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator(); |
| 777 | while (iter.hasNext()) { |
| 778 | AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next(); |
| 779 | int taskPage = task.page; |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 780 | if ((taskPage == page) || |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 781 | taskPage < getAssociatedLowerPageBound(mCurrentPage - mNumAppsPages) || |
| 782 | taskPage > getAssociatedUpperPageBound(mCurrentPage - mNumAppsPages)) { |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 783 | task.cancel(false); |
| 784 | iter.remove(); |
| 785 | } else { |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 786 | task.setThreadPriority(getThreadPriorityForPage(taskPage + mNumAppsPages)); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 787 | } |
| 788 | } |
| 789 | |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 790 | // 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] | 791 | final int sleepMs = getSleepForPage(page + mNumAppsPages); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 792 | AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight, |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 793 | cellCountX, new AsyncTaskCallback() { |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 794 | @Override |
| 795 | public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) { |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 796 | try { |
Winson Chung | 0994593 | 2011-09-20 14:22:40 -0700 | [diff] [blame] | 797 | try { |
| 798 | Thread.sleep(sleepMs); |
| 799 | } catch (Exception e) {} |
| 800 | loadWidgetPreviewsInBackground(task, data); |
| 801 | } finally { |
| 802 | if (task.isCancelled()) { |
| 803 | data.cleanup(true); |
| 804 | } |
| 805 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 806 | } |
| 807 | }, |
| 808 | new AsyncTaskCallback() { |
| 809 | @Override |
| 810 | public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) { |
Winson Chung | 0994593 | 2011-09-20 14:22:40 -0700 | [diff] [blame] | 811 | try { |
| 812 | mRunningTasks.remove(task); |
| 813 | if (task.isCancelled()) return; |
Winson Chung | 0994593 | 2011-09-20 14:22:40 -0700 | [diff] [blame] | 814 | onSyncWidgetPageItems(data); |
| 815 | } finally { |
| 816 | data.cleanup(task.isCancelled()); |
| 817 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 818 | } |
Winson Chung | 0994593 | 2011-09-20 14:22:40 -0700 | [diff] [blame] | 819 | }); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 820 | |
| 821 | // Ensure that the task is appropriately prioritized and runs in parallel |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 822 | AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page, |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 823 | AsyncTaskPageData.Type.LoadWidgetPreviewData); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 824 | t.setThreadPriority(getThreadPriorityForPage(page)); |
| 825 | t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData); |
| 826 | mRunningTasks.add(t); |
| 827 | } |
| 828 | /** |
| 829 | * Creates and executes a new AsyncTask to load the outlines for a page of content. |
| 830 | */ |
| 831 | private void prepareGenerateHoloOutlinesTask(int page, ArrayList<Object> items, |
| 832 | ArrayList<Bitmap> images) { |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 833 | // Prune old tasks for this page |
| 834 | Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator(); |
| 835 | while (iter.hasNext()) { |
| 836 | AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next(); |
| 837 | int taskPage = task.page; |
| 838 | if ((taskPage == page) && |
| 839 | (task.dataType == AsyncTaskPageData.Type.LoadHolographicIconsData)) { |
| 840 | task.cancel(false); |
| 841 | iter.remove(); |
| 842 | } |
| 843 | } |
| 844 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 845 | AsyncTaskPageData pageData = new AsyncTaskPageData(page, items, images, |
| 846 | new AsyncTaskCallback() { |
| 847 | @Override |
| 848 | public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) { |
Winson Chung | 0994593 | 2011-09-20 14:22:40 -0700 | [diff] [blame] | 849 | try { |
| 850 | // Ensure that this task starts running at the correct priority |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 851 | task.syncThreadPriority(); |
| 852 | |
Winson Chung | 0994593 | 2011-09-20 14:22:40 -0700 | [diff] [blame] | 853 | ArrayList<Bitmap> images = data.generatedImages; |
| 854 | ArrayList<Bitmap> srcImages = data.sourceImages; |
| 855 | int count = srcImages.size(); |
| 856 | Canvas c = new Canvas(); |
| 857 | for (int i = 0; i < count && !task.isCancelled(); ++i) { |
| 858 | // Before work on each item, ensure that this task is running at the correct |
| 859 | // priority |
| 860 | task.syncThreadPriority(); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 861 | |
Winson Chung | 0994593 | 2011-09-20 14:22:40 -0700 | [diff] [blame] | 862 | Bitmap b = srcImages.get(i); |
| 863 | Bitmap outline = Bitmap.createBitmap(b.getWidth(), b.getHeight(), |
| 864 | Bitmap.Config.ARGB_8888); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 865 | |
Winson Chung | 0994593 | 2011-09-20 14:22:40 -0700 | [diff] [blame] | 866 | c.setBitmap(outline); |
| 867 | c.save(); |
| 868 | c.drawBitmap(b, 0, 0, null); |
| 869 | c.restore(); |
| 870 | c.setBitmap(null); |
| 871 | |
| 872 | images.add(outline); |
| 873 | } |
| 874 | } finally { |
| 875 | if (task.isCancelled()) { |
| 876 | data.cleanup(true); |
| 877 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 878 | } |
| 879 | } |
| 880 | }, |
| 881 | new AsyncTaskCallback() { |
| 882 | @Override |
| 883 | public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) { |
Winson Chung | 0994593 | 2011-09-20 14:22:40 -0700 | [diff] [blame] | 884 | try { |
| 885 | mRunningTasks.remove(task); |
| 886 | if (task.isCancelled()) return; |
Winson Chung | 0994593 | 2011-09-20 14:22:40 -0700 | [diff] [blame] | 887 | onHolographicPageItemsLoaded(data); |
| 888 | } finally { |
| 889 | data.cleanup(task.isCancelled()); |
| 890 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 891 | } |
| 892 | }); |
| 893 | |
| 894 | // Ensure that the outline task always runs in the background, serially |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 895 | AppsCustomizeAsyncTask t = |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 896 | new AppsCustomizeAsyncTask(page, AsyncTaskPageData.Type.LoadHolographicIconsData); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 897 | t.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 898 | t.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, pageData); |
| 899 | mRunningTasks.add(t); |
| 900 | } |
| 901 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 902 | /* |
| 903 | * Widgets PagedView implementation |
| 904 | */ |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 905 | private void setupPage(PagedViewGridLayout layout) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 906 | layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, |
| 907 | mPageLayoutPaddingRight, mPageLayoutPaddingBottom); |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 908 | |
| 909 | // 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] | 910 | // immediately after syncing, we don't have a proper width. |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 911 | int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST); |
| 912 | int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 913 | layout.setMinimumWidth(getPageContentWidth()); |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 914 | layout.measure(widthSpec, heightSpec); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 915 | } |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 916 | |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 917 | private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) { |
| 918 | renderDrawableToBitmap(d, bitmap, x, y, w, h, 1f, 0xFFFFFFFF); |
Winson Chung | 70fc438 | 2011-08-08 15:31:33 -0700 | [diff] [blame] | 919 | } |
| 920 | 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] | 921 | float scale) { |
| 922 | renderDrawableToBitmap(d, bitmap, x, y, w, h, scale, 0xFFFFFFFF); |
| 923 | } |
| 924 | private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h, |
| 925 | float scale, int multiplyColor) { |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 926 | if (bitmap != null) { |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 927 | Canvas c = new Canvas(bitmap); |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 928 | c.scale(scale, scale); |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 929 | Rect oldBounds = d.copyBounds(); |
| 930 | d.setBounds(x, y, x + w, y + h); |
| 931 | d.draw(c); |
| 932 | d.setBounds(oldBounds); // Restore the bounds |
Winson Chung | 70fc438 | 2011-08-08 15:31:33 -0700 | [diff] [blame] | 933 | if (multiplyColor != 0xFFFFFFFF) { |
| 934 | c.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY); |
| 935 | } |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 936 | c.setBitmap(null); |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 937 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 938 | } |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 939 | private Bitmap getShortcutPreview(ResolveInfo info, int cellWidth, int cellHeight) { |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 940 | // Render the background |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 941 | int offset = 0; |
| 942 | int bitmapSize = mAppIconSize; |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 943 | Bitmap preview = Bitmap.createBitmap(bitmapSize, bitmapSize, Config.ARGB_8888); |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 944 | |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 945 | // Render the icon |
Winson Chung | 0b9fcf5 | 2011-10-31 13:05:15 -0700 | [diff] [blame] | 946 | Drawable icon = mIconCache.getFullResIcon(info); |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 947 | renderDrawableToBitmap(icon, preview, offset, offset, mAppIconSize, mAppIconSize); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 948 | return preview; |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 949 | } |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 950 | private Bitmap getWidgetPreview(AppWidgetProviderInfo info, |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 951 | int cellHSpan, int cellVSpan, int cellWidth, int cellHeight) { |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 952 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 953 | // Load the preview image if possible |
| 954 | String packageName = info.provider.getPackageName(); |
| 955 | Drawable drawable = null; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 956 | Bitmap preview = null; |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 957 | if (info.previewImage != 0) { |
| 958 | drawable = mPackageManager.getDrawable(packageName, info.previewImage, null); |
| 959 | if (drawable == null) { |
| 960 | Log.w(LOG_TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon) |
| 961 | + " for provider: " + info.provider); |
| 962 | } else { |
Winson Chung | b5e74c8 | 2011-10-28 16:39:20 -0700 | [diff] [blame] | 963 | // Map the target width/height to the cell dimensions |
| 964 | int targetWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan); |
| 965 | int targetHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan); |
| 966 | int targetCellWidth; |
| 967 | int targetCellHeight; |
| 968 | if (targetWidth >= targetHeight) { |
| 969 | targetCellWidth = Math.min(targetWidth, cellWidth); |
| 970 | targetCellHeight = (int) (cellHeight * ((float) targetCellWidth / cellWidth)); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 971 | } else { |
Winson Chung | b5e74c8 | 2011-10-28 16:39:20 -0700 | [diff] [blame] | 972 | targetCellHeight = Math.min(targetHeight, cellHeight); |
| 973 | targetCellWidth = (int) (cellWidth * ((float) targetCellHeight / cellHeight)); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 974 | } |
Winson Chung | b5e74c8 | 2011-10-28 16:39:20 -0700 | [diff] [blame] | 975 | // Map the preview to the target cell dimensions |
| 976 | int bitmapWidth = Math.min(targetCellWidth, drawable.getIntrinsicWidth()); |
| 977 | int bitmapHeight = (int) (drawable.getIntrinsicHeight() * |
| 978 | ((float) bitmapWidth / drawable.getIntrinsicWidth())); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 979 | |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 980 | preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888); |
| 981 | renderDrawableToBitmap(drawable, preview, 0, 0, bitmapWidth, bitmapHeight); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 982 | } |
| 983 | } |
| 984 | |
| 985 | // Generate a preview image if we couldn't load one |
| 986 | if (drawable == null) { |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 987 | Resources resources = mLauncher.getResources(); |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 988 | // TODO: This actually uses the apps customize cell layout params, where as we make want |
| 989 | // the Workspace params for more accuracy. |
| 990 | int targetWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan); |
| 991 | int targetHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan); |
| 992 | int bitmapWidth = targetWidth; |
| 993 | int bitmapHeight = targetHeight; |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 994 | int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage); |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 995 | float iconScale = 1f; |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 996 | |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 997 | // Determine the size of the bitmap we want to draw |
Winson Chung | 273c102 | 2011-07-11 13:40:52 -0700 | [diff] [blame] | 998 | if (cellHSpan == cellVSpan) { |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 999 | // For square widgets, we just have a fixed size for 1x1 and larger-than-1x1 |
| 1000 | if (cellHSpan <= 1) { |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 1001 | bitmapWidth = bitmapHeight = mAppIconSize + 2 * minOffset; |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1002 | } else { |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 1003 | bitmapWidth = bitmapHeight = mAppIconSize + 4 * minOffset; |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1004 | } |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 1005 | } else { |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1006 | // Otherwise, ensure that we are properly sized within the cellWidth/Height |
Winson Chung | b5e74c8 | 2011-10-28 16:39:20 -0700 | [diff] [blame] | 1007 | if (targetWidth >= targetHeight) { |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1008 | bitmapWidth = Math.min(targetWidth, cellWidth); |
| 1009 | bitmapHeight = (int) (targetHeight * ((float) bitmapWidth / targetWidth)); |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 1010 | iconScale = Math.min((float) bitmapHeight / (mAppIconSize + 2 * minOffset), 1f); |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1011 | } else { |
| 1012 | bitmapHeight = Math.min(targetHeight, cellHeight); |
| 1013 | bitmapWidth = (int) (targetWidth * ((float) bitmapHeight / targetHeight)); |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 1014 | iconScale = Math.min((float) bitmapWidth / (mAppIconSize + 2 * minOffset), 1f); |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1015 | } |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 1016 | } |
Winson Chung | 273c102 | 2011-07-11 13:40:52 -0700 | [diff] [blame] | 1017 | preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888); |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 1018 | if (cellHSpan != 1 || cellVSpan != 1) { |
| 1019 | renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, bitmapWidth, |
| 1020 | bitmapHeight); |
| 1021 | } |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 1022 | |
| 1023 | // Draw the icon in the top left corner |
| 1024 | try { |
| 1025 | Drawable icon = null; |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 1026 | int hoffset = (int) (bitmapWidth / 2 - mAppIconSize * iconScale / 2); |
| 1027 | int yoffset = (int) (bitmapHeight / 2 - mAppIconSize * iconScale / 2); |
Winson Chung | 0b9fcf5 | 2011-10-31 13:05:15 -0700 | [diff] [blame] | 1028 | if (info.icon > 0) icon = mIconCache.getFullResIcon(packageName, info.icon); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 1029 | if (icon == null) icon = resources.getDrawable(R.drawable.ic_launcher_application); |
| 1030 | |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 1031 | renderDrawableToBitmap(icon, preview, hoffset, yoffset, |
| 1032 | (int) (mAppIconSize * iconScale), |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1033 | (int) (mAppIconSize * iconScale)); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 1034 | } catch (Resources.NotFoundException e) {} |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 1035 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1036 | return preview; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1037 | } |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1038 | |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1039 | public void syncWidgetPageItems(int page, boolean immediate) { |
Winson Chung | 6a3fd3f | 2011-08-02 14:03:26 -0700 | [diff] [blame] | 1040 | int numItemsPerPage = mWidgetCountX * mWidgetCountY; |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 1041 | int contentWidth = mWidgetSpacingLayout.getContentWidth(); |
| 1042 | int contentHeight = mWidgetSpacingLayout.getContentHeight(); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1043 | |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 1044 | // Calculate the dimensions of each cell we are giving to each widget |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 1045 | ArrayList<Object> items = new ArrayList<Object>(); |
| 1046 | int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight |
Winson Chung | 6a3fd3f | 2011-08-02 14:03:26 -0700 | [diff] [blame] | 1047 | - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX); |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 1048 | int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom |
Winson Chung | 6a3fd3f | 2011-08-02 14:03:26 -0700 | [diff] [blame] | 1049 | - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY); |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 1050 | |
Winson Chung | e4a647f | 2011-09-30 14:41:25 -0700 | [diff] [blame] | 1051 | // Prepare the set of widgets to load previews for in the background |
Winson Chung | 6a3fd3f | 2011-08-02 14:03:26 -0700 | [diff] [blame] | 1052 | int offset = page * numItemsPerPage; |
| 1053 | for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) { |
| 1054 | items.add(mWidgets.get(i)); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
Winson Chung | e4a647f | 2011-09-30 14:41:25 -0700 | [diff] [blame] | 1057 | // 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] | 1058 | PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page + mNumAppsPages); |
Winson Chung | e4a647f | 2011-09-30 14:41:25 -0700 | [diff] [blame] | 1059 | layout.setColumnCount(layout.getCellCountX()); |
| 1060 | for (int i = 0; i < items.size(); ++i) { |
| 1061 | Object rawInfo = items.get(i); |
| 1062 | PendingAddItemInfo createItemInfo = null; |
| 1063 | PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate( |
| 1064 | R.layout.apps_customize_widget, layout, false); |
| 1065 | if (rawInfo instanceof AppWidgetProviderInfo) { |
| 1066 | // Fill in the widget information |
| 1067 | AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo; |
| 1068 | createItemInfo = new PendingAddWidgetInfo(info, null, null); |
| 1069 | int[] cellSpans = mLauncher.getSpanForWidget(info, null); |
| 1070 | widget.applyFromAppWidgetProviderInfo(info, -1, cellSpans, |
| 1071 | mHolographicOutlineHelper); |
| 1072 | widget.setTag(createItemInfo); |
| 1073 | } else if (rawInfo instanceof ResolveInfo) { |
| 1074 | // Fill in the shortcuts information |
| 1075 | ResolveInfo info = (ResolveInfo) rawInfo; |
| 1076 | createItemInfo = new PendingAddItemInfo(); |
| 1077 | createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT; |
| 1078 | createItemInfo.componentName = new ComponentName(info.activityInfo.packageName, |
| 1079 | info.activityInfo.name); |
| 1080 | widget.applyFromResolveInfo(mPackageManager, info, mHolographicOutlineHelper); |
| 1081 | widget.setTag(createItemInfo); |
| 1082 | } |
| 1083 | widget.setOnClickListener(this); |
| 1084 | widget.setOnLongClickListener(this); |
| 1085 | widget.setOnTouchListener(this); |
| 1086 | |
| 1087 | // Layout each widget |
| 1088 | int ix = i % mWidgetCountX; |
| 1089 | int iy = i / mWidgetCountX; |
| 1090 | GridLayout.LayoutParams lp = new GridLayout.LayoutParams( |
| 1091 | GridLayout.spec(iy, GridLayout.LEFT), |
| 1092 | GridLayout.spec(ix, GridLayout.TOP)); |
| 1093 | lp.width = cellWidth; |
| 1094 | lp.height = cellHeight; |
| 1095 | lp.setGravity(Gravity.TOP | Gravity.LEFT); |
| 1096 | if (ix > 0) lp.leftMargin = mWidgetWidthGap; |
| 1097 | if (iy > 0) lp.topMargin = mWidgetHeightGap; |
| 1098 | layout.addView(widget, lp); |
| 1099 | } |
| 1100 | |
| 1101 | // Load the widget previews |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1102 | if (immediate) { |
| 1103 | AsyncTaskPageData data = new AsyncTaskPageData(page, items, cellWidth, cellHeight, |
| 1104 | mWidgetCountX, null, null); |
| 1105 | loadWidgetPreviewsInBackground(null, data); |
| 1106 | onSyncWidgetPageItems(data); |
| 1107 | } else { |
| 1108 | prepareLoadWidgetPreviewsTask(page, items, cellWidth, cellHeight, mWidgetCountX); |
| 1109 | } |
| 1110 | } |
| 1111 | private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task, |
| 1112 | AsyncTaskPageData data) { |
| 1113 | if (task != null) { |
| 1114 | // Ensure that this task starts running at the correct priority |
| 1115 | task.syncThreadPriority(); |
| 1116 | } |
| 1117 | |
| 1118 | // Load each of the widget/shortcut previews |
| 1119 | ArrayList<Object> items = data.items; |
| 1120 | ArrayList<Bitmap> images = data.generatedImages; |
| 1121 | int count = items.size(); |
| 1122 | int cellWidth = data.cellWidth; |
| 1123 | int cellHeight = data.cellHeight; |
| 1124 | for (int i = 0; i < count; ++i) { |
| 1125 | if (task != null) { |
| 1126 | // Ensure we haven't been cancelled yet |
| 1127 | if (task.isCancelled()) break; |
| 1128 | // Before work on each item, ensure that this task is running at the correct |
| 1129 | // priority |
| 1130 | task.syncThreadPriority(); |
| 1131 | } |
| 1132 | |
| 1133 | Object rawInfo = items.get(i); |
| 1134 | if (rawInfo instanceof AppWidgetProviderInfo) { |
| 1135 | AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo; |
Adam Cohen | f814aa0 | 2011-09-01 13:48:05 -0700 | [diff] [blame] | 1136 | int[] cellSpans = mLauncher.getSpanForWidget(info, null); |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1137 | images.add(getWidgetPreview(info, cellSpans[0],cellSpans[1], |
| 1138 | cellWidth, cellHeight)); |
| 1139 | } else if (rawInfo instanceof ResolveInfo) { |
| 1140 | // Fill in the shortcuts information |
| 1141 | ResolveInfo info = (ResolveInfo) rawInfo; |
| 1142 | images.add(getShortcutPreview(info, cellWidth, cellHeight)); |
| 1143 | } |
| 1144 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1145 | } |
| 1146 | private void onSyncWidgetPageItems(AsyncTaskPageData data) { |
| 1147 | int page = data.page; |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1148 | PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page + mNumAppsPages); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1149 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1150 | ArrayList<Object> items = data.items; |
| 1151 | int count = items.size(); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1152 | for (int i = 0; i < count; ++i) { |
Winson Chung | e4a647f | 2011-09-30 14:41:25 -0700 | [diff] [blame] | 1153 | PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i); |
| 1154 | if (widget != null) { |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 1155 | Bitmap preview = data.generatedImages.get(i); |
| 1156 | boolean scale = |
| 1157 | (preview.getWidth() >= data.cellWidth || |
| 1158 | preview.getHeight() >= data.cellHeight); |
| 1159 | |
| 1160 | widget.applyPreview(new FastBitmapDrawable(preview), i, scale); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 1161 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1162 | } |
Winson Chung | 7f0acdd | 2011-09-19 18:34:19 -0700 | [diff] [blame] | 1163 | layout.createHardwareLayer(); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1164 | |
| 1165 | invalidate(); |
Winson Chung | 8ff8713 | 2011-09-30 19:40:46 -0700 | [diff] [blame] | 1166 | /* TEMPORARILY DISABLE HOLOGRAPHIC ICONS |
Winson Chung | 54c725c | 2011-08-03 12:03:40 -0700 | [diff] [blame] | 1167 | if (mFadeInAdjacentScreens) { |
| 1168 | prepareGenerateHoloOutlinesTask(data.page, data.items, data.generatedImages); |
| 1169 | } |
Winson Chung | 8ff8713 | 2011-09-30 19:40:46 -0700 | [diff] [blame] | 1170 | */ |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1171 | } |
| 1172 | private void onHolographicPageItemsLoaded(AsyncTaskPageData data) { |
| 1173 | // Invalidate early to short-circuit children invalidates |
| 1174 | invalidate(); |
| 1175 | |
| 1176 | int page = data.page; |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1177 | ViewGroup layout = (ViewGroup) getPageAt(page); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1178 | if (layout instanceof PagedViewCellLayout) { |
| 1179 | PagedViewCellLayout cl = (PagedViewCellLayout) layout; |
| 1180 | int count = cl.getPageChildCount(); |
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 | PagedViewIcon icon = (PagedViewIcon) cl.getChildOnPageAt(i); |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 1184 | icon.setHolographicOutline(data.generatedImages.get(i)); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1185 | } |
| 1186 | } else { |
| 1187 | int count = layout.getChildCount(); |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 1188 | if (count != data.generatedImages.size()) return; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1189 | for (int i = 0; i < count; ++i) { |
| 1190 | View v = layout.getChildAt(i); |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 1191 | ((PagedViewWidget) v).setHolographicOutline(data.generatedImages.get(i)); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1192 | } |
| 1193 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1194 | } |
Winson Chung | 46af2e8 | 2011-05-09 16:00:53 -0700 | [diff] [blame] | 1195 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1196 | @Override |
| 1197 | public void syncPages() { |
| 1198 | removeAllViews(); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1199 | cancelAllTasks(); |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 1200 | |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1201 | Context context = getContext(); |
| 1202 | for (int j = 0; j < mNumWidgetPages; ++j) { |
| 1203 | PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX, |
| 1204 | mWidgetCountY); |
| 1205 | setupPage(layout); |
| 1206 | addView(layout, new PagedViewGridLayout.LayoutParams(LayoutParams.MATCH_PARENT, |
| 1207 | LayoutParams.MATCH_PARENT)); |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 1208 | } |
| 1209 | |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1210 | for (int i = 0; i < mNumAppsPages; ++i) { |
| 1211 | PagedViewCellLayout layout = new PagedViewCellLayout(context); |
| 1212 | setupPage(layout); |
| 1213 | addView(layout); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1214 | } |
| 1215 | } |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1216 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1217 | @Override |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1218 | public void syncPageItems(int page, boolean immediate) { |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1219 | if (page < mNumAppsPages) { |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1220 | syncAppsPageItems(page, immediate); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1221 | } else { |
| 1222 | syncWidgetPageItems(page - mNumAppsPages, immediate); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1223 | } |
| 1224 | } |
| 1225 | |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1226 | // We want our pages to be z-ordered such that the further a page is to the left, the higher |
| 1227 | // it is in the z-order. This is important to insure touch events are handled correctly. |
| 1228 | View getPageAt(int index) { |
| 1229 | return getChildAt(getChildCount() - index - 1); |
| 1230 | } |
| 1231 | |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 1232 | @Override |
| 1233 | protected int indexToPage(int index) { |
| 1234 | return getChildCount() - index - 1; |
| 1235 | } |
| 1236 | |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1237 | // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack. |
| 1238 | @Override |
| 1239 | protected void screenScrolled(int screenCenter) { |
| 1240 | super.screenScrolled(screenCenter); |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1241 | |
| 1242 | for (int i = 0; i < getChildCount(); i++) { |
| 1243 | View v = getPageAt(i); |
| 1244 | if (v != null) { |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1245 | float scrollProgress = getScrollProgress(screenCenter, v, i); |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1246 | |
| 1247 | float interpolatedProgress = |
| 1248 | mZInterpolator.getInterpolation(Math.abs(Math.min(scrollProgress, 0))); |
| 1249 | float scale = (1 - interpolatedProgress) + |
| 1250 | interpolatedProgress * TRANSITION_SCALE_FACTOR; |
| 1251 | float translationX = Math.min(0, scrollProgress) * v.getMeasuredWidth(); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1252 | |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 1253 | float alpha; |
| 1254 | |
| 1255 | if (!LauncherApplication.isScreenLarge() || scrollProgress < 0) { |
| 1256 | alpha = scrollProgress < 0 ? mAlphaInterpolator.getInterpolation( |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1257 | 1 - Math.abs(scrollProgress)) : 1.0f; |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 1258 | } else { |
| 1259 | // On large screens we need to fade the page as it nears its leftmost position |
| 1260 | alpha = mLeftScreenAlphaInterpolator.getInterpolation(1 - scrollProgress); |
| 1261 | } |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1262 | |
| 1263 | v.setCameraDistance(mDensity * CAMERA_DISTANCE); |
| 1264 | int pageWidth = v.getMeasuredWidth(); |
| 1265 | int pageHeight = v.getMeasuredHeight(); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1266 | |
| 1267 | if (PERFORM_OVERSCROLL_ROTATION) { |
| 1268 | if (i == 0 && scrollProgress < 0) { |
| 1269 | // Overscroll to the left |
| 1270 | v.setPivotX(TRANSITION_PIVOT * pageWidth); |
| 1271 | v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress); |
| 1272 | scale = 1.0f; |
| 1273 | alpha = 1.0f; |
| 1274 | // On the first page, we don't want the page to have any lateral motion |
| 1275 | translationX = getScrollX(); |
| 1276 | } else if (i == getChildCount() - 1 && scrollProgress > 0) { |
| 1277 | // Overscroll to the right |
| 1278 | v.setPivotX((1 - TRANSITION_PIVOT) * pageWidth); |
| 1279 | v.setRotationY(-TRANSITION_MAX_ROTATION * scrollProgress); |
| 1280 | scale = 1.0f; |
| 1281 | alpha = 1.0f; |
| 1282 | // On the last page, we don't want the page to have any lateral motion. |
| 1283 | translationX = getScrollX() - mMaxScrollX; |
| 1284 | } else { |
| 1285 | v.setPivotY(pageHeight / 2.0f); |
| 1286 | v.setPivotX(pageWidth / 2.0f); |
| 1287 | v.setRotationY(0f); |
| 1288 | } |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | v.setTranslationX(translationX); |
| 1292 | v.setScaleX(scale); |
| 1293 | v.setScaleY(scale); |
| 1294 | v.setAlpha(alpha); |
| 1295 | } |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | protected void overScroll(float amount) { |
Adam Cohen | cff6af8 | 2011-09-13 14:51:53 -0700 | [diff] [blame] | 1300 | acceleratedOverScroll(amount); |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1301 | } |
| 1302 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1303 | /** |
| 1304 | * Used by the parent to get the content width to set the tab bar to |
| 1305 | * @return |
| 1306 | */ |
| 1307 | public int getPageContentWidth() { |
| 1308 | return mContentWidth; |
| 1309 | } |
| 1310 | |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 1311 | @Override |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 1312 | protected void onPageEndMoving() { |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 1313 | super.onPageEndMoving(); |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 1314 | |
| 1315 | // We reset the save index when we change pages so that it will be recalculated on next |
| 1316 | // rotation |
| 1317 | mSaveInstanceStateItemIndex = -1; |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 1318 | } |
| 1319 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1320 | /* |
| 1321 | * AllAppsView implementation |
| 1322 | */ |
| 1323 | @Override |
| 1324 | public void setup(Launcher launcher, DragController dragController) { |
| 1325 | mLauncher = launcher; |
| 1326 | mDragController = dragController; |
| 1327 | } |
| 1328 | @Override |
| 1329 | public void zoom(float zoom, boolean animate) { |
| 1330 | // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed() |
| 1331 | } |
| 1332 | @Override |
| 1333 | public boolean isVisible() { |
| 1334 | return (getVisibility() == VISIBLE); |
| 1335 | } |
| 1336 | @Override |
| 1337 | public boolean isAnimating() { |
| 1338 | return false; |
| 1339 | } |
| 1340 | @Override |
| 1341 | public void setApps(ArrayList<ApplicationInfo> list) { |
| 1342 | mApps = list; |
| 1343 | Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR); |
Winson Chung | 4b0ed8c | 2011-10-19 15:24:49 -0700 | [diff] [blame] | 1344 | updatePageCounts(); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 1345 | |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 1346 | // The next layout pass will trigger data-ready if both widgets and apps are set, so |
| 1347 | // 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] | 1348 | if (testDataReady()) requestLayout(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1349 | } |
| 1350 | private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) { |
| 1351 | // We add it in place, in alphabetical order |
| 1352 | int count = list.size(); |
| 1353 | for (int i = 0; i < count; ++i) { |
| 1354 | ApplicationInfo info = list.get(i); |
| 1355 | int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR); |
| 1356 | if (index < 0) { |
| 1357 | mApps.add(-(index + 1), info); |
| 1358 | } |
| 1359 | } |
| 1360 | } |
| 1361 | @Override |
| 1362 | public void addApps(ArrayList<ApplicationInfo> list) { |
| 1363 | addAppsWithoutInvalidate(list); |
Winson Chung | 4b0ed8c | 2011-10-19 15:24:49 -0700 | [diff] [blame] | 1364 | updatePageCounts(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1365 | invalidatePageData(); |
| 1366 | } |
| 1367 | private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) { |
| 1368 | ComponentName removeComponent = item.intent.getComponent(); |
| 1369 | int length = list.size(); |
| 1370 | for (int i = 0; i < length; ++i) { |
| 1371 | ApplicationInfo info = list.get(i); |
| 1372 | if (info.intent.getComponent().equals(removeComponent)) { |
| 1373 | return i; |
| 1374 | } |
| 1375 | } |
| 1376 | return -1; |
| 1377 | } |
| 1378 | private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) { |
| 1379 | // loop through all the apps and remove apps that have the same component |
| 1380 | int length = list.size(); |
| 1381 | for (int i = 0; i < length; ++i) { |
| 1382 | ApplicationInfo info = list.get(i); |
| 1383 | int removeIndex = findAppByComponent(mApps, info); |
| 1384 | if (removeIndex > -1) { |
| 1385 | mApps.remove(removeIndex); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1386 | } |
| 1387 | } |
| 1388 | } |
| 1389 | @Override |
| 1390 | public void removeApps(ArrayList<ApplicationInfo> list) { |
| 1391 | removeAppsWithoutInvalidate(list); |
Winson Chung | 4b0ed8c | 2011-10-19 15:24:49 -0700 | [diff] [blame] | 1392 | updatePageCounts(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1393 | invalidatePageData(); |
| 1394 | } |
| 1395 | @Override |
| 1396 | public void updateApps(ArrayList<ApplicationInfo> list) { |
| 1397 | // We remove and re-add the updated applications list because it's properties may have |
| 1398 | // changed (ie. the title), and this will ensure that the items will be in their proper |
| 1399 | // place in the list. |
| 1400 | removeAppsWithoutInvalidate(list); |
| 1401 | addAppsWithoutInvalidate(list); |
Winson Chung | 4b0ed8c | 2011-10-19 15:24:49 -0700 | [diff] [blame] | 1402 | updatePageCounts(); |
| 1403 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1404 | invalidatePageData(); |
| 1405 | } |
Michael Jurka | 35aa14d | 2011-07-07 17:01:08 -0700 | [diff] [blame] | 1406 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1407 | @Override |
| 1408 | public void reset() { |
Adam Cohen | b64d36e | 2011-10-17 21:48:02 -0700 | [diff] [blame] | 1409 | AppsCustomizeTabHost tabHost = getTabHost(); |
| 1410 | String tag = tabHost.getCurrentTabTag(); |
Winson Chung | 6a8103c | 2011-10-21 11:08:32 -0700 | [diff] [blame] | 1411 | if (tag != null) { |
| 1412 | if (!tag.equals(tabHost.getTabTagForContentType(ContentType.Applications))) { |
| 1413 | tabHost.setCurrentTabFromContent(ContentType.Applications); |
| 1414 | } |
Adam Cohen | b64d36e | 2011-10-17 21:48:02 -0700 | [diff] [blame] | 1415 | } |
| 1416 | if (mCurrentPage != 0) { |
| 1417 | invalidatePageData(0); |
| 1418 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1419 | } |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1420 | |
| 1421 | private AppsCustomizeTabHost getTabHost() { |
| 1422 | return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane); |
| 1423 | } |
| 1424 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1425 | @Override |
| 1426 | public void dumpState() { |
| 1427 | // TODO: Dump information related to current list of Applications, Widgets, etc. |
| 1428 | ApplicationInfo.dumpApplicationInfoList(LOG_TAG, "mApps", mApps); |
| 1429 | dumpAppWidgetProviderInfoList(LOG_TAG, "mWidgets", mWidgets); |
| 1430 | } |
| 1431 | private void dumpAppWidgetProviderInfoList(String tag, String label, |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 1432 | ArrayList<Object> list) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1433 | Log.d(tag, label + " size=" + list.size()); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 1434 | for (Object i: list) { |
| 1435 | if (i instanceof AppWidgetProviderInfo) { |
| 1436 | AppWidgetProviderInfo info = (AppWidgetProviderInfo) i; |
| 1437 | Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage |
| 1438 | + " resizeMode=" + info.resizeMode + " configure=" + info.configure |
| 1439 | + " initialLayout=" + info.initialLayout |
| 1440 | + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight); |
| 1441 | } else if (i instanceof ResolveInfo) { |
| 1442 | ResolveInfo info = (ResolveInfo) i; |
| 1443 | Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon=" |
| 1444 | + info.icon); |
| 1445 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1446 | } |
| 1447 | } |
| 1448 | @Override |
| 1449 | public void surrender() { |
| 1450 | // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we |
| 1451 | // should stop this now. |
Winson Chung | 4b0ed8c | 2011-10-19 15:24:49 -0700 | [diff] [blame] | 1452 | |
| 1453 | // Stop all background tasks |
| 1454 | cancelAllTasks(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1455 | } |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1456 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1457 | /* |
| 1458 | * We load an extra page on each side to prevent flashes from scrolling and loading of the |
| 1459 | * widget previews in the background with the AsyncTasks. |
| 1460 | */ |
| 1461 | protected int getAssociatedLowerPageBound(int page) { |
| 1462 | return Math.max(0, page - 2); |
| 1463 | } |
| 1464 | protected int getAssociatedUpperPageBound(int page) { |
| 1465 | final int count = getChildCount(); |
| 1466 | return Math.min(page + 2, count - 1); |
| 1467 | } |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1468 | |
| 1469 | @Override |
| 1470 | protected String getCurrentPageDescription() { |
| 1471 | int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage; |
| 1472 | int stringId = R.string.default_scroll_format; |
Adam Cohen | d3357b1 | 2011-10-18 14:58:11 -0700 | [diff] [blame] | 1473 | int count = 0; |
| 1474 | |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1475 | if (page < mNumAppsPages) { |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1476 | stringId = R.string.apps_customize_apps_scroll_format; |
Adam Cohen | d3357b1 | 2011-10-18 14:58:11 -0700 | [diff] [blame] | 1477 | count = mNumAppsPages; |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1478 | } else { |
| 1479 | page -= mNumAppsPages; |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1480 | stringId = R.string.apps_customize_widgets_scroll_format; |
Adam Cohen | d3357b1 | 2011-10-18 14:58:11 -0700 | [diff] [blame] | 1481 | count = mNumWidgetPages; |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1482 | } |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1483 | |
Adam Cohen | d3357b1 | 2011-10-18 14:58:11 -0700 | [diff] [blame] | 1484 | return String.format(mContext.getString(stringId), page + 1, count); |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1485 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1486 | } |