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