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