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