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