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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 18 | |
Winson Chung | 55b6550 | 2011-05-26 12:03:43 -0700 | [diff] [blame] | 19 | import android.animation.AnimatorSet; |
Winson Chung | d2e87b3 | 2011-06-02 10:53:07 -0700 | [diff] [blame] | 20 | import android.animation.ValueAnimator; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 21 | import android.appwidget.AppWidgetHostView; |
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; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 26 | import android.content.pm.PackageManager; |
| 27 | import android.content.pm.ResolveInfo; |
| 28 | import android.content.res.Resources; |
| 29 | import android.content.res.TypedArray; |
| 30 | import android.graphics.Bitmap; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 31 | import android.graphics.Point; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 32 | import android.graphics.Rect; |
| 33 | import android.graphics.drawable.Drawable; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 34 | import android.os.AsyncTask; |
Adam Cohen | 9e05a5e | 2012-09-10 15:53:09 -0700 | [diff] [blame] | 35 | import android.os.Build; |
| 36 | import android.os.Bundle; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 37 | import android.os.Process; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 38 | import android.util.AttributeSet; |
| 39 | import android.util.Log; |
Winson Chung | 72d8b39 | 2011-07-29 13:56:44 -0700 | [diff] [blame] | 40 | import android.view.Gravity; |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 41 | import android.view.KeyEvent; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 42 | import android.view.LayoutInflater; |
| 43 | import android.view.View; |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 44 | import android.view.ViewGroup; |
Winson Chung | 55b6550 | 2011-05-26 12:03:43 -0700 | [diff] [blame] | 45 | import android.view.animation.AccelerateInterpolator; |
Winson Chung | fd3385f | 2011-06-15 19:51:24 -0700 | [diff] [blame] | 46 | import android.widget.GridLayout; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 47 | import android.widget.ImageView; |
Winson Chung | 55b6550 | 2011-05-26 12:03:43 -0700 | [diff] [blame] | 48 | import android.widget.Toast; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 49 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 50 | import com.android.launcher3.DropTarget.DragObject; |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 51 | import com.android.launcher3.compat.AppWidgetManagerCompat; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 52 | |
| 53 | import java.util.ArrayList; |
| 54 | import java.util.Collections; |
| 55 | import java.util.Iterator; |
| 56 | import java.util.List; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 57 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 58 | /** |
| 59 | * A simple callback interface which also provides the results of the task. |
| 60 | */ |
| 61 | interface AsyncTaskCallback { |
| 62 | void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data); |
| 63 | } |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 64 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 65 | /** |
| 66 | * The data needed to perform either of the custom AsyncTasks. |
| 67 | */ |
| 68 | class AsyncTaskPageData { |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 69 | enum Type { |
Michael Jurka | 82369a1 | 2012-01-12 08:08:38 -0800 | [diff] [blame] | 70 | LoadWidgetPreviewData |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 73 | AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, AsyncTaskCallback bgR, |
Michael Jurka | 3f4e070 | 2013-02-05 11:21:28 +0100 | [diff] [blame] | 74 | AsyncTaskCallback postR, WidgetPreviewLoader w) { |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 75 | page = p; |
| 76 | items = l; |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 77 | generatedImages = new ArrayList<Bitmap>(); |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 78 | maxImageWidth = cw; |
| 79 | maxImageHeight = ch; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 80 | doInBackgroundCallback = bgR; |
| 81 | postExecuteCallback = postR; |
Michael Jurka | 3f4e070 | 2013-02-05 11:21:28 +0100 | [diff] [blame] | 82 | widgetPreviewLoader = w; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 83 | } |
Winson Chung | 0994593 | 2011-09-20 14:22:40 -0700 | [diff] [blame] | 84 | void cleanup(boolean cancelled) { |
| 85 | // Clean up any references to source/generated bitmaps |
Winson Chung | 0994593 | 2011-09-20 14:22:40 -0700 | [diff] [blame] | 86 | if (generatedImages != null) { |
| 87 | if (cancelled) { |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 88 | for (int i = 0; i < generatedImages.size(); i++) { |
Michael Jurka | ee8e99f | 2013-02-07 13:27:06 +0100 | [diff] [blame] | 89 | widgetPreviewLoader.recycleBitmap(items.get(i), generatedImages.get(i)); |
Winson Chung | 0994593 | 2011-09-20 14:22:40 -0700 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | generatedImages.clear(); |
| 93 | } |
| 94 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 95 | int page; |
| 96 | ArrayList<Object> items; |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 97 | ArrayList<Bitmap> sourceImages; |
| 98 | ArrayList<Bitmap> generatedImages; |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 99 | int maxImageWidth; |
| 100 | int maxImageHeight; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 101 | AsyncTaskCallback doInBackgroundCallback; |
| 102 | AsyncTaskCallback postExecuteCallback; |
Michael Jurka | 3f4e070 | 2013-02-05 11:21:28 +0100 | [diff] [blame] | 103 | WidgetPreviewLoader widgetPreviewLoader; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 104 | } |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 105 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 106 | /** |
| 107 | * A generic template for an async task used in AppsCustomize. |
| 108 | */ |
| 109 | class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> { |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 110 | AppsCustomizeAsyncTask(int p, AsyncTaskPageData.Type ty) { |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 111 | page = p; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 112 | threadPriority = Process.THREAD_PRIORITY_DEFAULT; |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 113 | dataType = ty; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 114 | } |
| 115 | @Override |
| 116 | protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) { |
| 117 | if (params.length != 1) return null; |
| 118 | // Load each of the widget previews in the background |
| 119 | params[0].doInBackgroundCallback.run(this, params[0]); |
| 120 | return params[0]; |
| 121 | } |
| 122 | @Override |
| 123 | protected void onPostExecute(AsyncTaskPageData result) { |
| 124 | // All the widget previews are loaded, so we can just callback to inflate the page |
| 125 | result.postExecuteCallback.run(this, result); |
| 126 | } |
| 127 | |
| 128 | void setThreadPriority(int p) { |
| 129 | threadPriority = p; |
| 130 | } |
| 131 | void syncThreadPriority() { |
| 132 | Process.setThreadPriority(threadPriority); |
| 133 | } |
| 134 | |
| 135 | // The page that this async task is associated with |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 136 | AsyncTaskPageData.Type dataType; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 137 | int page; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 138 | int threadPriority; |
| 139 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 140 | |
| 141 | /** |
| 142 | * The Apps/Customize page that displays all the applications, widgets, and shortcuts. |
| 143 | */ |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 144 | public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements |
Winson Chung | cd81073 | 2012-06-18 16:45:43 -0700 | [diff] [blame] | 145 | View.OnClickListener, View.OnKeyListener, DragSource, |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 146 | PagedViewWidget.ShortPressListener, LauncherTransitionable { |
Adam Cohen | 0e56cc9 | 2012-05-11 15:57:05 -0700 | [diff] [blame] | 147 | static final String TAG = "AppsCustomizePagedView"; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 148 | |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 149 | private static Rect sTmpRect = new Rect(); |
| 150 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 151 | /** |
| 152 | * The different content types that this paged view can show. |
| 153 | */ |
| 154 | public enum ContentType { |
| 155 | Applications, |
Winson Chung | 6a26e5b | 2011-05-26 14:36:06 -0700 | [diff] [blame] | 156 | Widgets |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 157 | } |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 158 | private ContentType mContentType = ContentType.Applications; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 159 | |
| 160 | // Refs |
| 161 | private Launcher mLauncher; |
| 162 | private DragController mDragController; |
| 163 | private final LayoutInflater mLayoutInflater; |
| 164 | private final PackageManager mPackageManager; |
| 165 | |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 166 | // Save and Restore |
| 167 | private int mSaveInstanceStateItemIndex = -1; |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 168 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 169 | // Content |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 170 | private ArrayList<AppInfo> mApps; |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 171 | private ArrayList<Object> mWidgets; |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 172 | |
| 173 | // Caching |
Winson Chung | 4dbea79 | 2011-05-05 14:21:32 -0700 | [diff] [blame] | 174 | private IconCache mIconCache; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 175 | |
| 176 | // Dimens |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 177 | private int mContentWidth, mContentHeight; |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 178 | private int mWidgetCountX, mWidgetCountY; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 179 | private PagedViewCellLayout mWidgetSpacingLayout; |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 180 | private int mNumAppsPages; |
| 181 | private int mNumWidgetPages; |
Winson Chung | 67ca7e4 | 2013-10-31 16:53:19 -0700 | [diff] [blame] | 182 | private Rect mAllAppsPadding = new Rect(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 183 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 184 | // Previews & outlines |
| 185 | ArrayList<AppsCustomizeAsyncTask> mRunningTasks; |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 186 | private static final int sPageSleepDelay = 200; |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 187 | |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 188 | private Runnable mInflateWidgetRunnable = null; |
| 189 | private Runnable mBindWidgetRunnable = null; |
| 190 | static final int WIDGET_NO_CLEANUP_REQUIRED = -1; |
Adam Cohen | 21a170b | 2012-05-30 15:17:06 -0700 | [diff] [blame] | 191 | static final int WIDGET_PRELOAD_PENDING = 0; |
| 192 | static final int WIDGET_BOUND = 1; |
| 193 | static final int WIDGET_INFLATED = 2; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 194 | int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED; |
| 195 | int mWidgetLoadingId = -1; |
Adam Cohen | 1b36dc3 | 2012-02-13 19:27:37 -0800 | [diff] [blame] | 196 | PendingAddWidgetInfo mCreateWidgetInfo = null; |
Adam Cohen | 7a32664 | 2012-02-22 12:03:22 -0800 | [diff] [blame] | 197 | private boolean mDraggingWidget = false; |
Adam Cohen | a00673c | 2014-08-14 12:57:28 -0700 | [diff] [blame] | 198 | boolean mPageBackgroundsVisible = true; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 199 | |
Winson Chung | cb9ab4f | 2012-07-02 11:47:27 -0700 | [diff] [blame] | 200 | private Toast mWidgetInstructionToast; |
| 201 | |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 202 | // Deferral of loading widget previews during launcher transitions |
| 203 | private boolean mInTransition; |
| 204 | private ArrayList<AsyncTaskPageData> mDeferredSyncWidgetPageItems = |
| 205 | new ArrayList<AsyncTaskPageData>(); |
Michael Jurka | f6a9690 | 2012-06-06 11:48:13 -0700 | [diff] [blame] | 206 | private ArrayList<Runnable> mDeferredPrepareLoadWidgetPreviewsTasks = |
| 207 | new ArrayList<Runnable>(); |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 208 | |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 209 | WidgetPreviewLoader mWidgetPreviewLoader; |
| 210 | |
Michael Jurka | c402cd9 | 2013-05-20 15:49:32 +0200 | [diff] [blame] | 211 | private boolean mInBulkBind; |
| 212 | private boolean mNeedToUpdatePageCountsAndInvalidateData; |
| 213 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 214 | public AppsCustomizePagedView(Context context, AttributeSet attrs) { |
| 215 | super(context, attrs); |
| 216 | mLayoutInflater = LayoutInflater.from(context); |
| 217 | mPackageManager = context.getPackageManager(); |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 218 | mApps = new ArrayList<AppInfo>(); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 219 | mWidgets = new ArrayList<Object>(); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 220 | mIconCache = (LauncherAppState.getInstance()).getIconCache(); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 221 | mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>(); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 222 | |
| 223 | // Save the default widget preview background |
Winson Chung | 6032e7e | 2011-11-08 15:47:17 -0800 | [diff] [blame] | 224 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 225 | mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2); |
| 226 | mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2); |
| 227 | a.recycle(); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 228 | mWidgetSpacingLayout = new PagedViewCellLayout(getContext()); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 229 | |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 230 | // The padding on the non-matched dimension for the default widget preview icons |
| 231 | // (top + bottom) |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 232 | mFadeInAdjacentScreens = false; |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 233 | |
| 234 | // Unless otherwise specified this view is important for accessibility. |
| 235 | if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) { |
| 236 | setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES); |
| 237 | } |
Adam Cohen | 1d3d4f1 | 2014-08-14 19:14:52 -0700 | [diff] [blame] | 238 | setSinglePageInViewport(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | @Override |
| 242 | protected void init() { |
| 243 | super.init(); |
Winson Chung | 6a87740 | 2011-10-26 14:51:44 -0700 | [diff] [blame] | 244 | mCenterPagesVertically = false; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 245 | |
| 246 | Context context = getContext(); |
| 247 | Resources r = context.getResources(); |
| 248 | setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f); |
| 249 | } |
| 250 | |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 251 | public void onFinishInflate() { |
| 252 | super.onFinishInflate(); |
| 253 | |
| 254 | LauncherAppState app = LauncherAppState.getInstance(); |
| 255 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
| 256 | setPadding(grid.edgeMarginPx, 2 * grid.edgeMarginPx, |
| 257 | grid.edgeMarginPx, 2 * grid.edgeMarginPx); |
| 258 | } |
| 259 | |
Winson Chung | 67ca7e4 | 2013-10-31 16:53:19 -0700 | [diff] [blame] | 260 | void setAllAppsPadding(Rect r) { |
| 261 | mAllAppsPadding.set(r); |
| 262 | } |
Adam Cohen | 4e243a2 | 2014-08-10 18:30:55 -0700 | [diff] [blame] | 263 | |
Winson Chung | 67ca7e4 | 2013-10-31 16:53:19 -0700 | [diff] [blame] | 264 | void setWidgetsPageIndicatorPadding(int pageIndicatorHeight) { |
Adam Cohen | 4e243a2 | 2014-08-10 18:30:55 -0700 | [diff] [blame] | 265 | setPadding(getPaddingLeft(), getPaddingTop(), getPaddingRight(), pageIndicatorHeight); |
Winson Chung | 67ca7e4 | 2013-10-31 16:53:19 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Michael Jurka | 9c5cc5a | 2014-01-09 14:59:22 +0100 | [diff] [blame] | 268 | WidgetPreviewLoader getWidgetPreviewLoader() { |
| 269 | if (mWidgetPreviewLoader == null) { |
| 270 | mWidgetPreviewLoader = new WidgetPreviewLoader(mLauncher); |
| 271 | } |
| 272 | return mWidgetPreviewLoader; |
| 273 | } |
| 274 | |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 275 | /** Returns the item index of the center item on this page so that we can restore to this |
| 276 | * item index when we rotate. */ |
| 277 | private int getMiddleComponentIndexOnCurrentPage() { |
| 278 | int i = -1; |
| 279 | if (getPageCount() > 0) { |
| 280 | int currentPage = getCurrentPage(); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 281 | if (mContentType == ContentType.Applications) { |
| 282 | AppsCustomizeCellLayout layout = (AppsCustomizeCellLayout) getPageAt(currentPage); |
| 283 | ShortcutAndWidgetContainer childrenLayout = layout.getShortcutsAndWidgets(); |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 284 | int numItemsPerPage = mCellCountX * mCellCountY; |
| 285 | int childCount = childrenLayout.getChildCount(); |
| 286 | if (childCount > 0) { |
| 287 | i = (currentPage * numItemsPerPage) + (childCount / 2); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 288 | } |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 289 | } else if (mContentType == ContentType.Widgets) { |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 290 | int numApps = mApps.size(); |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 291 | PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage); |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 292 | int numItemsPerPage = mWidgetCountX * mWidgetCountY; |
| 293 | int childCount = layout.getChildCount(); |
| 294 | if (childCount > 0) { |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 295 | i = numApps + |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 296 | (currentPage * numItemsPerPage) + (childCount / 2); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 297 | } |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 298 | } else { |
| 299 | throw new RuntimeException("Invalid ContentType"); |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | return i; |
| 303 | } |
| 304 | |
| 305 | /** Get the index of the item to restore to if we need to restore the current page. */ |
| 306 | int getSaveInstanceStateIndex() { |
| 307 | if (mSaveInstanceStateItemIndex == -1) { |
| 308 | mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage(); |
| 309 | } |
| 310 | return mSaveInstanceStateItemIndex; |
| 311 | } |
| 312 | |
| 313 | /** Returns the page in the current orientation which is expected to contain the specified |
| 314 | * item index. */ |
| 315 | int getPageForComponent(int index) { |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 316 | if (index < 0) return 0; |
| 317 | |
| 318 | if (index < mApps.size()) { |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 319 | int numItemsPerPage = mCellCountX * mCellCountY; |
| 320 | return (index / numItemsPerPage); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 321 | } else { |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 322 | int numItemsPerPage = mWidgetCountX * mWidgetCountY; |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 323 | return (index - mApps.size()) / numItemsPerPage; |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 324 | } |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 327 | /** Restores the page for an item at the specified index */ |
| 328 | void restorePageForIndex(int index) { |
| 329 | if (index < 0) return; |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 330 | mSaveInstanceStateItemIndex = index; |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Winson Chung | 4b0ed8c | 2011-10-19 15:24:49 -0700 | [diff] [blame] | 333 | private void updatePageCounts() { |
| 334 | mNumWidgetPages = (int) Math.ceil(mWidgets.size() / |
| 335 | (float) (mWidgetCountX * mWidgetCountY)); |
| 336 | mNumAppsPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY)); |
| 337 | } |
| 338 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 339 | protected void onDataReady(int width, int height) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 340 | // Now that the data is ready, we can calculate the content width, the number of cells to |
| 341 | // use for each page |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 342 | LauncherAppState app = LauncherAppState.getInstance(); |
| 343 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 344 | mCellCountX = (int) grid.allAppsNumCols; |
| 345 | mCellCountY = (int) grid.allAppsNumRows; |
Winson Chung | 4b0ed8c | 2011-10-19 15:24:49 -0700 | [diff] [blame] | 346 | updatePageCounts(); |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 347 | |
Winson Chung | db1138b | 2011-06-30 14:39:35 -0700 | [diff] [blame] | 348 | // Force a measure to update recalculate the gaps |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 349 | mContentWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight(); |
| 350 | mContentHeight = getMeasuredHeight() - getPaddingTop() - getPaddingBottom(); |
| 351 | int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST); |
| 352 | int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST); |
Winson Chung | db1138b | 2011-06-30 14:39:35 -0700 | [diff] [blame] | 353 | mWidgetSpacingLayout.measure(widthSpec, heightSpec); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 354 | |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 355 | final boolean hostIsTransitioning = getTabHost().isInTransition(); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 356 | int page = getPageForComponent(mSaveInstanceStateItemIndex); |
Michael Jurka | e326f18 | 2011-11-21 14:05:46 -0800 | [diff] [blame] | 357 | invalidatePageData(Math.max(0, page), hostIsTransitioning); |
Winson Chung | 3f4e142 | 2011-11-17 14:58:51 -0800 | [diff] [blame] | 358 | } |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 359 | |
Adam Cohen | a00673c | 2014-08-14 12:57:28 -0700 | [diff] [blame] | 360 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 361 | super.onLayout(changed, l, t, r, b); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 362 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 363 | if (!isDataReady()) { |
Nilesh Agrawal | 16f3ea8 | 2014-01-09 17:14:01 -0800 | [diff] [blame] | 364 | if ((LauncherAppState.isDisableAllApps() || !mApps.isEmpty()) && !mWidgets.isEmpty()) { |
Adam Cohen | 69ed200 | 2014-08-27 21:27:01 -0700 | [diff] [blame^] | 365 | post(new Runnable() { |
| 366 | // This code triggers requestLayout so must be posted outside of the |
| 367 | // layout pass. |
| 368 | public void run() { |
| 369 | setDataIsReady(); |
| 370 | onDataReady(getMeasuredWidth(), getMeasuredHeight()); |
| 371 | } |
| 372 | }); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 373 | } |
| 374 | } |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Michael Jurka | c402cd9 | 2013-05-20 15:49:32 +0200 | [diff] [blame] | 377 | public void onPackagesUpdated(ArrayList<Object> widgetsAndShortcuts) { |
Winson Chung | 892c74d | 2013-08-22 16:15:50 -0700 | [diff] [blame] | 378 | LauncherAppState app = LauncherAppState.getInstance(); |
| 379 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
| 380 | |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 381 | // Get the list of widgets and shortcuts |
| 382 | mWidgets.clear(); |
Michael Jurka | c402cd9 | 2013-05-20 15:49:32 +0200 | [diff] [blame] | 383 | for (Object o : widgetsAndShortcuts) { |
| 384 | if (o instanceof AppWidgetProviderInfo) { |
| 385 | AppWidgetProviderInfo widget = (AppWidgetProviderInfo) o; |
Bjorn Bringert | 1307f63 | 2013-10-03 22:31:03 +0100 | [diff] [blame] | 386 | if (!app.shouldShowAppOrWidgetProvider(widget.provider)) { |
| 387 | continue; |
| 388 | } |
Michael Jurka | c402cd9 | 2013-05-20 15:49:32 +0200 | [diff] [blame] | 389 | if (widget.minWidth > 0 && widget.minHeight > 0) { |
| 390 | // Ensure that all widgets we show can be added on a workspace of this size |
| 391 | int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget); |
| 392 | int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget); |
| 393 | int minSpanX = Math.min(spanXY[0], minSpanXY[0]); |
| 394 | int minSpanY = Math.min(spanXY[1], minSpanXY[1]); |
Winson Chung | 892c74d | 2013-08-22 16:15:50 -0700 | [diff] [blame] | 395 | if (minSpanX <= (int) grid.numColumns && |
| 396 | minSpanY <= (int) grid.numRows) { |
Michael Jurka | c402cd9 | 2013-05-20 15:49:32 +0200 | [diff] [blame] | 397 | mWidgets.add(widget); |
| 398 | } else { |
| 399 | Log.e(TAG, "Widget " + widget.provider + " can not fit on this device (" + |
| 400 | widget.minWidth + ", " + widget.minHeight + ")"); |
| 401 | } |
Winson Chung | fd39d8e | 2012-06-05 10:12:48 -0700 | [diff] [blame] | 402 | } else { |
Michael Jurka | c402cd9 | 2013-05-20 15:49:32 +0200 | [diff] [blame] | 403 | Log.e(TAG, "Widget " + widget.provider + " has invalid dimensions (" + |
| 404 | widget.minWidth + ", " + widget.minHeight + ")"); |
Winson Chung | a5c9636 | 2012-04-12 14:04:41 -0700 | [diff] [blame] | 405 | } |
Michael Jurka | dbc1f65 | 2011-11-10 17:02:56 -0800 | [diff] [blame] | 406 | } else { |
Michael Jurka | c402cd9 | 2013-05-20 15:49:32 +0200 | [diff] [blame] | 407 | // just add shortcuts |
| 408 | mWidgets.add(o); |
Michael Jurka | dbc1f65 | 2011-11-10 17:02:56 -0800 | [diff] [blame] | 409 | } |
| 410 | } |
Michael Jurka | c402cd9 | 2013-05-20 15:49:32 +0200 | [diff] [blame] | 411 | updatePageCountsAndInvalidateData(); |
| 412 | } |
| 413 | |
| 414 | public void setBulkBind(boolean bulkBind) { |
| 415 | if (bulkBind) { |
| 416 | mInBulkBind = true; |
| 417 | } else { |
| 418 | mInBulkBind = false; |
| 419 | if (mNeedToUpdatePageCountsAndInvalidateData) { |
| 420 | updatePageCountsAndInvalidateData(); |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | private void updatePageCountsAndInvalidateData() { |
| 426 | if (mInBulkBind) { |
| 427 | mNeedToUpdatePageCountsAndInvalidateData = true; |
| 428 | } else { |
| 429 | updatePageCounts(); |
| 430 | invalidateOnDataChange(); |
| 431 | mNeedToUpdatePageCountsAndInvalidateData = false; |
| 432 | } |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | @Override |
| 436 | public void onClick(View v) { |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 437 | // When we have exited all apps or are in transition, disregard clicks |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 438 | if (!mLauncher.isAllAppsVisible() |
| 439 | || mLauncher.getWorkspace().isSwitchingState() |
| 440 | || !(v instanceof PagedViewWidget)) return; |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 441 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 442 | // Let the user know that they have to long press to add a widget |
| 443 | if (mWidgetInstructionToast != null) { |
| 444 | mWidgetInstructionToast.cancel(); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 445 | } |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 446 | mWidgetInstructionToast = Toast.makeText(getContext(),R.string.long_press_widget_to_add, |
| 447 | Toast.LENGTH_SHORT); |
| 448 | mWidgetInstructionToast.show(); |
| 449 | |
| 450 | // Create a little animation to show that the widget can move |
| 451 | float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY); |
| 452 | final ImageView p = (ImageView) v.findViewById(R.id.widget_preview); |
| 453 | AnimatorSet bounce = LauncherAnimUtils.createAnimatorSet(); |
| 454 | ValueAnimator tyuAnim = LauncherAnimUtils.ofFloat(p, "translationY", offsetY); |
| 455 | tyuAnim.setDuration(125); |
| 456 | ValueAnimator tydAnim = LauncherAnimUtils.ofFloat(p, "translationY", 0f); |
| 457 | tydAnim.setDuration(100); |
| 458 | bounce.play(tyuAnim).before(tydAnim); |
| 459 | bounce.setInterpolator(new AccelerateInterpolator()); |
| 460 | bounce.start(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 463 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
| 464 | return FocusHelper.handleAppsCustomizeKeyEvent(v, keyCode, event); |
| 465 | } |
| 466 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 467 | /* |
| 468 | * PagedViewWithDraggableItems implementation |
| 469 | */ |
| 470 | @Override |
| 471 | protected void determineDraggingStart(android.view.MotionEvent ev) { |
| 472 | // Disable dragging by pulling an app down for now. |
| 473 | } |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 474 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 475 | private void beginDraggingApplication(View v) { |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 476 | mLauncher.getWorkspace().beginDragShared(v, this); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 477 | } |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 478 | |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 479 | static Bundle getDefaultOptionsForWidget(Launcher launcher, PendingAddWidgetInfo info) { |
Adam Cohen | 9e05a5e | 2012-09-10 15:53:09 -0700 | [diff] [blame] | 480 | Bundle options = null; |
| 481 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 482 | AppWidgetResizeFrame.getWidgetSizeRanges(launcher, info.spanX, info.spanY, sTmpRect); |
| 483 | Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(launcher, |
Adam Cohen | aaa5c21 | 2012-10-05 18:14:31 -0700 | [diff] [blame] | 484 | info.componentName, null); |
| 485 | |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 486 | float density = launcher.getResources().getDisplayMetrics().density; |
Adam Cohen | aaa5c21 | 2012-10-05 18:14:31 -0700 | [diff] [blame] | 487 | int xPaddingDips = (int) ((padding.left + padding.right) / density); |
| 488 | int yPaddingDips = (int) ((padding.top + padding.bottom) / density); |
| 489 | |
Adam Cohen | 9e05a5e | 2012-09-10 15:53:09 -0700 | [diff] [blame] | 490 | options = new Bundle(); |
Adam Cohen | aaa5c21 | 2012-10-05 18:14:31 -0700 | [diff] [blame] | 491 | options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 492 | sTmpRect.left - xPaddingDips); |
Adam Cohen | aaa5c21 | 2012-10-05 18:14:31 -0700 | [diff] [blame] | 493 | options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 494 | sTmpRect.top - yPaddingDips); |
Adam Cohen | aaa5c21 | 2012-10-05 18:14:31 -0700 | [diff] [blame] | 495 | options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 496 | sTmpRect.right - xPaddingDips); |
Adam Cohen | aaa5c21 | 2012-10-05 18:14:31 -0700 | [diff] [blame] | 497 | options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 498 | sTmpRect.bottom - yPaddingDips); |
Adam Cohen | 9e05a5e | 2012-09-10 15:53:09 -0700 | [diff] [blame] | 499 | } |
| 500 | return options; |
| 501 | } |
| 502 | |
Adam Cohen | f1dcdf6 | 2012-05-10 16:51:52 -0700 | [diff] [blame] | 503 | private void preloadWidget(final PendingAddWidgetInfo info) { |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 504 | final AppWidgetProviderInfo pInfo = info.info; |
Adam Cohen | dd70d66 | 2012-10-04 16:53:44 -0700 | [diff] [blame] | 505 | final Bundle options = getDefaultOptionsForWidget(mLauncher, info); |
| 506 | |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 507 | if (pInfo.configure != null) { |
Adam Cohen | dd70d66 | 2012-10-04 16:53:44 -0700 | [diff] [blame] | 508 | info.bindOptions = options; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 509 | return; |
| 510 | } |
| 511 | |
Adam Cohen | 21a170b | 2012-05-30 15:17:06 -0700 | [diff] [blame] | 512 | mWidgetCleanupState = WIDGET_PRELOAD_PENDING; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 513 | mBindWidgetRunnable = new Runnable() { |
| 514 | @Override |
| 515 | public void run() { |
| 516 | mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId(); |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 517 | if(AppWidgetManagerCompat.getInstance(mLauncher).bindAppWidgetIdIfAllowed( |
| 518 | mWidgetLoadingId, pInfo, options)) { |
| 519 | mWidgetCleanupState = WIDGET_BOUND; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 520 | } |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 521 | } |
| 522 | }; |
| 523 | post(mBindWidgetRunnable); |
| 524 | |
| 525 | mInflateWidgetRunnable = new Runnable() { |
| 526 | @Override |
| 527 | public void run() { |
Michael Jurka | 1637d6d | 2012-08-03 13:35:01 -0700 | [diff] [blame] | 528 | if (mWidgetCleanupState != WIDGET_BOUND) { |
| 529 | return; |
| 530 | } |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 531 | AppWidgetHostView hostView = mLauncher. |
| 532 | getAppWidgetHost().createView(getContext(), mWidgetLoadingId, pInfo); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 533 | info.boundWidget = hostView; |
| 534 | mWidgetCleanupState = WIDGET_INFLATED; |
Adam Cohen | ef3dd6e | 2012-02-14 20:54:05 -0800 | [diff] [blame] | 535 | hostView.setVisibility(INVISIBLE); |
Adam Cohen | 1f36270 | 2012-04-04 14:58:12 -0700 | [diff] [blame] | 536 | int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info.spanX, |
| 537 | info.spanY, info, false); |
| 538 | |
| 539 | // We want the first widget layout to be the correct size. This will be important |
| 540 | // for width size reporting to the AppWidgetManager. |
| 541 | DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0], |
| 542 | unScaledSize[1]); |
| 543 | lp.x = lp.y = 0; |
| 544 | lp.customPosition = true; |
| 545 | hostView.setLayoutParams(lp); |
Adam Cohen | ef3dd6e | 2012-02-14 20:54:05 -0800 | [diff] [blame] | 546 | mLauncher.getDragLayer().addView(hostView); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 547 | } |
| 548 | }; |
| 549 | post(mInflateWidgetRunnable); |
| 550 | } |
| 551 | |
| 552 | @Override |
| 553 | public void onShortPress(View v) { |
| 554 | // We are anticipating a long press, and we use this time to load bind and instantiate |
| 555 | // the widget. This will need to be cleaned up if it turns out no long press occurs. |
Adam Cohen | 0e56cc9 | 2012-05-11 15:57:05 -0700 | [diff] [blame] | 556 | if (mCreateWidgetInfo != null) { |
| 557 | // Just in case the cleanup process wasn't properly executed. This shouldn't happen. |
| 558 | cleanupWidgetPreloading(false); |
| 559 | } |
Adam Cohen | 1b36dc3 | 2012-02-13 19:27:37 -0800 | [diff] [blame] | 560 | mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag()); |
Adam Cohen | f1dcdf6 | 2012-05-10 16:51:52 -0700 | [diff] [blame] | 561 | preloadWidget(mCreateWidgetInfo); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 562 | } |
| 563 | |
Adam Cohen | 0e56cc9 | 2012-05-11 15:57:05 -0700 | [diff] [blame] | 564 | private void cleanupWidgetPreloading(boolean widgetWasAdded) { |
| 565 | if (!widgetWasAdded) { |
| 566 | // If the widget was not added, we may need to do further cleanup. |
| 567 | PendingAddWidgetInfo info = mCreateWidgetInfo; |
| 568 | mCreateWidgetInfo = null; |
Adam Cohen | 21a170b | 2012-05-30 15:17:06 -0700 | [diff] [blame] | 569 | |
| 570 | if (mWidgetCleanupState == WIDGET_PRELOAD_PENDING) { |
Adam Cohen | 21a170b | 2012-05-30 15:17:06 -0700 | [diff] [blame] | 571 | // We never did any preloading, so just remove pending callbacks to do so |
| 572 | removeCallbacks(mBindWidgetRunnable); |
| 573 | removeCallbacks(mInflateWidgetRunnable); |
| 574 | } else if (mWidgetCleanupState == WIDGET_BOUND) { |
| 575 | // Delete the widget id which was allocated |
| 576 | if (mWidgetLoadingId != -1) { |
Adam Cohen | 21a170b | 2012-05-30 15:17:06 -0700 | [diff] [blame] | 577 | mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId); |
| 578 | } |
| 579 | |
| 580 | // We never got around to inflating the widget, so remove the callback to do so. |
Adam Cohen | 0e56cc9 | 2012-05-11 15:57:05 -0700 | [diff] [blame] | 581 | removeCallbacks(mInflateWidgetRunnable); |
| 582 | } else if (mWidgetCleanupState == WIDGET_INFLATED) { |
Adam Cohen | 21a170b | 2012-05-30 15:17:06 -0700 | [diff] [blame] | 583 | // Delete the widget id which was allocated |
| 584 | if (mWidgetLoadingId != -1) { |
Adam Cohen | 21a170b | 2012-05-30 15:17:06 -0700 | [diff] [blame] | 585 | mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId); |
| 586 | } |
| 587 | |
Adam Cohen | 0e56cc9 | 2012-05-11 15:57:05 -0700 | [diff] [blame] | 588 | // The widget was inflated and added to the DragLayer -- remove it. |
| 589 | AppWidgetHostView widget = info.boundWidget; |
| 590 | mLauncher.getDragLayer().removeView(widget); |
| 591 | } |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 592 | } |
| 593 | mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED; |
| 594 | mWidgetLoadingId = -1; |
Adam Cohen | 0e56cc9 | 2012-05-11 15:57:05 -0700 | [diff] [blame] | 595 | mCreateWidgetInfo = null; |
| 596 | PagedViewWidget.resetShortPressTarget(); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 597 | } |
| 598 | |
Adam Cohen | 7a32664 | 2012-02-22 12:03:22 -0800 | [diff] [blame] | 599 | @Override |
| 600 | public void cleanUpShortPress(View v) { |
| 601 | if (!mDraggingWidget) { |
Adam Cohen | 0e56cc9 | 2012-05-11 15:57:05 -0700 | [diff] [blame] | 602 | cleanupWidgetPreloading(false); |
Adam Cohen | 7a32664 | 2012-02-22 12:03:22 -0800 | [diff] [blame] | 603 | } |
| 604 | } |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 605 | |
Adam Cohen | 88c5d2d | 2012-05-09 21:34:33 -0700 | [diff] [blame] | 606 | private boolean beginDraggingWidget(View v) { |
Adam Cohen | 7a32664 | 2012-02-22 12:03:22 -0800 | [diff] [blame] | 607 | mDraggingWidget = true; |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 608 | // Get the widget preview as the drag representation |
| 609 | ImageView image = (ImageView) v.findViewById(R.id.widget_preview); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 610 | PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag(); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 611 | |
Adam Cohen | 88c5d2d | 2012-05-09 21:34:33 -0700 | [diff] [blame] | 612 | // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and |
| 613 | // we abort the drag. |
| 614 | if (image.getDrawable() == null) { |
| 615 | mDraggingWidget = false; |
| 616 | return false; |
| 617 | } |
| 618 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 619 | // Compose the drag image |
Winson Chung | 1120e03 | 2011-11-22 16:11:31 -0800 | [diff] [blame] | 620 | Bitmap preview; |
| 621 | Bitmap outline; |
Winson Chung | 72d5984 | 2012-02-22 13:51:36 -0800 | [diff] [blame] | 622 | float scale = 1f; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 623 | Point previewPadding = null; |
| 624 | |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 625 | if (createItemInfo instanceof PendingAddWidgetInfo) { |
Adam Cohen | 9247892 | 2012-05-17 13:43:29 -0700 | [diff] [blame] | 626 | // This can happen in some weird cases involving multi-touch. We can't start dragging |
| 627 | // the widget if this is null, so we break out. |
| 628 | if (mCreateWidgetInfo == null) { |
| 629 | return false; |
| 630 | } |
| 631 | |
Adam Cohen | 1b36dc3 | 2012-02-13 19:27:37 -0800 | [diff] [blame] | 632 | PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo; |
| 633 | createItemInfo = createWidgetInfo; |
Adam Cohen | 1f36270 | 2012-04-04 14:58:12 -0700 | [diff] [blame] | 634 | int spanX = createItemInfo.spanX; |
| 635 | int spanY = createItemInfo.spanY; |
| 636 | int[] size = mLauncher.getWorkspace().estimateItemSize(spanX, spanY, |
| 637 | createWidgetInfo, true); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 638 | |
Winson Chung | 72d5984 | 2012-02-22 13:51:36 -0800 | [diff] [blame] | 639 | FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable(); |
| 640 | float minScale = 1.25f; |
Michael Jurka | dac8591 | 2012-05-18 15:04:49 -0700 | [diff] [blame] | 641 | int maxWidth, maxHeight; |
| 642 | maxWidth = Math.min((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]); |
| 643 | maxHeight = Math.min((int) (previewDrawable.getIntrinsicHeight() * minScale), size[1]); |
Winson Chung | 72d5984 | 2012-02-22 13:51:36 -0800 | [diff] [blame] | 644 | |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 645 | int[] previewSizeBeforeScale = new int[1]; |
| 646 | |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 647 | preview = getWidgetPreviewLoader().generateWidgetPreview(createWidgetInfo.info, |
| 648 | spanX, spanY, maxWidth, maxHeight, null, previewSizeBeforeScale); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 649 | |
| 650 | // Compare the size of the drag preview to the preview in the AppsCustomize tray |
| 651 | int previewWidthInAppsCustomize = Math.min(previewSizeBeforeScale[0], |
Michael Jurka | 9c5cc5a | 2014-01-09 14:59:22 +0100 | [diff] [blame] | 652 | getWidgetPreviewLoader().maxWidthForWidgetPreview(spanX)); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 653 | scale = previewWidthInAppsCustomize / (float) preview.getWidth(); |
| 654 | |
| 655 | // The bitmap in the AppsCustomize tray is always the the same size, so there |
| 656 | // might be extra pixels around the preview itself - this accounts for that |
| 657 | if (previewWidthInAppsCustomize < previewDrawable.getIntrinsicWidth()) { |
| 658 | int padding = |
| 659 | (previewDrawable.getIntrinsicWidth() - previewWidthInAppsCustomize) / 2; |
| 660 | previewPadding = new Point(padding, 0); |
| 661 | } |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 662 | } else { |
Michael Jurka | dac8591 | 2012-05-18 15:04:49 -0700 | [diff] [blame] | 663 | PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) v.getTag(); |
| 664 | Drawable icon = mIconCache.getFullResIcon(createShortcutInfo.shortcutActivityInfo); |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 665 | preview = Utilities.createIconBitmap(icon, mLauncher); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 666 | createItemInfo.spanX = createItemInfo.spanY = 1; |
| 667 | } |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 668 | |
Michael Jurka | 8c3339b | 2012-06-14 16:18:21 -0700 | [diff] [blame] | 669 | // Don't clip alpha values for the drag outline if we're using the default widget preview |
| 670 | boolean clipAlpha = !(createItemInfo instanceof PendingAddWidgetInfo && |
| 671 | (((PendingAddWidgetInfo) createItemInfo).previewImage == 0)); |
Peter Ng | 8db7000 | 2011-10-25 15:40:08 -0700 | [diff] [blame] | 672 | |
Winson Chung | 1120e03 | 2011-11-22 16:11:31 -0800 | [diff] [blame] | 673 | // Save the preview for the outline generation, then dim the preview |
| 674 | outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(), |
| 675 | false); |
Winson Chung | 1120e03 | 2011-11-22 16:11:31 -0800 | [diff] [blame] | 676 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 677 | // Start the drag |
Winson Chung | 641d71d | 2012-04-26 15:58:01 -0700 | [diff] [blame] | 678 | mLauncher.lockScreenOrientation(); |
Michael Jurka | 8c3339b | 2012-06-14 16:18:21 -0700 | [diff] [blame] | 679 | mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, clipAlpha); |
Winson Chung | 1120e03 | 2011-11-22 16:11:31 -0800 | [diff] [blame] | 680 | mDragController.startDrag(image, preview, this, createItemInfo, |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 681 | DragController.DRAG_ACTION_COPY, previewPadding, scale); |
Winson Chung | 1120e03 | 2011-11-22 16:11:31 -0800 | [diff] [blame] | 682 | outline.recycle(); |
| 683 | preview.recycle(); |
Adam Cohen | 88c5d2d | 2012-05-09 21:34:33 -0700 | [diff] [blame] | 684 | return true; |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 685 | } |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 686 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 687 | @Override |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 688 | protected boolean beginDragging(final View v) { |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 689 | if (!super.beginDragging(v)) return false; |
| 690 | |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 691 | if (v instanceof BubbleTextView) { |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 692 | beginDraggingApplication(v); |
| 693 | } else if (v instanceof PagedViewWidget) { |
Adam Cohen | 88c5d2d | 2012-05-09 21:34:33 -0700 | [diff] [blame] | 694 | if (!beginDraggingWidget(v)) { |
| 695 | return false; |
| 696 | } |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 697 | } |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 698 | |
| 699 | // We delay entering spring-loaded mode slightly to make sure the UI |
| 700 | // thready is free of any work. |
| 701 | postDelayed(new Runnable() { |
| 702 | @Override |
| 703 | public void run() { |
Adam Cohen | 1b36dc3 | 2012-02-13 19:27:37 -0800 | [diff] [blame] | 704 | // We don't enter spring-loaded mode if the drag has been cancelled |
| 705 | if (mLauncher.getDragController().isDragging()) { |
Adam Cohen | 1b36dc3 | 2012-02-13 19:27:37 -0800 | [diff] [blame] | 706 | // Go into spring loaded mode (must happen before we startDrag()) |
| 707 | mLauncher.enterSpringLoadedDragMode(); |
| 708 | } |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 709 | } |
Winson Chung | 72d5984 | 2012-02-22 13:51:36 -0800 | [diff] [blame] | 710 | }, 150); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 711 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 712 | return true; |
| 713 | } |
Adam Cohen | 1b36dc3 | 2012-02-13 19:27:37 -0800 | [diff] [blame] | 714 | |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 715 | /** |
| 716 | * Clean up after dragging. |
| 717 | * |
| 718 | * @param target where the item was dragged to (can be null if the item was flung) |
| 719 | */ |
| 720 | private void endDragging(View target, boolean isFlingToDelete, boolean success) { |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 721 | if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() && |
Jorim Jaggi | 55bd972 | 2014-01-16 15:30:42 -0800 | [diff] [blame] | 722 | !(target instanceof DeleteDropTarget) && !(target instanceof Folder))) { |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 723 | // Exit spring loaded mode if we have not successfully dropped or have not handled the |
| 724 | // drop in Workspace |
Adam Cohen | 689ff16 | 2014-05-08 17:27:56 -0700 | [diff] [blame] | 725 | mLauncher.exitSpringLoadedDragMode(); |
| 726 | mLauncher.unlockScreenOrientation(false); |
Adam Cohen | e97a3b3 | 2013-10-23 16:11:50 -0700 | [diff] [blame] | 727 | } else { |
| 728 | mLauncher.unlockScreenOrientation(false); |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 729 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 730 | } |
| 731 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 732 | @Override |
Michael Jurka | a35e35a | 2012-04-26 15:04:28 -0700 | [diff] [blame] | 733 | public View getContent() { |
Winson Chung | 7bb3752 | 2013-10-28 11:07:57 -0700 | [diff] [blame] | 734 | if (getChildCount() > 0) { |
| 735 | return getChildAt(0); |
| 736 | } |
Michael Jurka | a35e35a | 2012-04-26 15:04:28 -0700 | [diff] [blame] | 737 | return null; |
| 738 | } |
| 739 | |
| 740 | @Override |
| 741 | public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) { |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 742 | mInTransition = true; |
| 743 | if (toWorkspace) { |
| 744 | cancelAllTasks(); |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | @Override |
Michael Jurka | a35e35a | 2012-04-26 15:04:28 -0700 | [diff] [blame] | 749 | public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) { |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | @Override |
| 753 | public void onLauncherTransitionStep(Launcher l, float t) { |
| 754 | } |
| 755 | |
| 756 | @Override |
| 757 | public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) { |
| 758 | mInTransition = false; |
| 759 | for (AsyncTaskPageData d : mDeferredSyncWidgetPageItems) { |
Winson Chung | 7bb3752 | 2013-10-28 11:07:57 -0700 | [diff] [blame] | 760 | onSyncWidgetPageItems(d, false); |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 761 | } |
| 762 | mDeferredSyncWidgetPageItems.clear(); |
Michael Jurka | f6a9690 | 2012-06-06 11:48:13 -0700 | [diff] [blame] | 763 | for (Runnable r : mDeferredPrepareLoadWidgetPreviewsTasks) { |
| 764 | r.run(); |
| 765 | } |
| 766 | mDeferredPrepareLoadWidgetPreviewsTasks.clear(); |
Michael Jurka | 5e368ff | 2012-05-14 23:13:15 -0700 | [diff] [blame] | 767 | mForceDrawAllChildrenNextFrame = !toWorkspace; |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | @Override |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 771 | public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete, |
| 772 | boolean success) { |
| 773 | // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling |
| 774 | if (isFlingToDelete) return; |
| 775 | |
| 776 | endDragging(target, false, success); |
Winson Chung | fc79c80 | 2011-05-02 13:35:34 -0700 | [diff] [blame] | 777 | |
| 778 | // Display an error message if the drag failed due to there not being enough space on the |
| 779 | // target layout we were dropping on. |
| 780 | if (!success) { |
| 781 | boolean showOutOfSpaceMessage = false; |
| 782 | if (target instanceof Workspace) { |
| 783 | int currentScreen = mLauncher.getCurrentWorkspaceScreen(); |
| 784 | Workspace workspace = (Workspace) target; |
| 785 | CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 786 | ItemInfo itemInfo = (ItemInfo) d.dragInfo; |
Winson Chung | fc79c80 | 2011-05-02 13:35:34 -0700 | [diff] [blame] | 787 | if (layout != null) { |
| 788 | layout.calculateSpans(itemInfo); |
| 789 | showOutOfSpaceMessage = |
| 790 | !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY); |
| 791 | } |
| 792 | } |
Winson Chung | fc79c80 | 2011-05-02 13:35:34 -0700 | [diff] [blame] | 793 | if (showOutOfSpaceMessage) { |
Winson Chung | 93eef08 | 2012-03-23 15:59:27 -0700 | [diff] [blame] | 794 | mLauncher.showOutOfSpaceMessage(false); |
Winson Chung | fc79c80 | 2011-05-02 13:35:34 -0700 | [diff] [blame] | 795 | } |
Adam Cohen | 7a32664 | 2012-02-22 12:03:22 -0800 | [diff] [blame] | 796 | |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 797 | d.deferDragViewCleanupPostAnimation = false; |
Winson Chung | fc79c80 | 2011-05-02 13:35:34 -0700 | [diff] [blame] | 798 | } |
Adam Cohen | 0e56cc9 | 2012-05-11 15:57:05 -0700 | [diff] [blame] | 799 | cleanupWidgetPreloading(success); |
Adam Cohen | 7a32664 | 2012-02-22 12:03:22 -0800 | [diff] [blame] | 800 | mDraggingWidget = false; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 801 | } |
| 802 | |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 803 | @Override |
| 804 | public void onFlingToDeleteCompleted() { |
| 805 | // We just dismiss the drag when we fling, so cleanup here |
| 806 | endDragging(null, true, true); |
Adam Cohen | 0e56cc9 | 2012-05-11 15:57:05 -0700 | [diff] [blame] | 807 | cleanupWidgetPreloading(false); |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 808 | mDraggingWidget = false; |
| 809 | } |
| 810 | |
| 811 | @Override |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 812 | public boolean supportsFlingToDelete() { |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 813 | return true; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 814 | } |
| 815 | |
Winson Chung | 7f0acdd | 2011-09-19 18:34:19 -0700 | [diff] [blame] | 816 | @Override |
Mathew Inwood | 1eeb3fc | 2013-11-25 17:01:34 +0000 | [diff] [blame] | 817 | public boolean supportsAppInfoDropTarget() { |
| 818 | return true; |
| 819 | } |
| 820 | |
| 821 | @Override |
| 822 | public boolean supportsDeleteDropTarget() { |
| 823 | return false; |
| 824 | } |
| 825 | |
| 826 | @Override |
Winson Chung | eeb5bbc | 2013-11-13 15:47:05 -0800 | [diff] [blame] | 827 | public float getIntrinsicIconScaleFactor() { |
| 828 | LauncherAppState app = LauncherAppState.getInstance(); |
| 829 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
| 830 | return (float) grid.allAppsIconSizePx / grid.iconSizePx; |
| 831 | } |
| 832 | |
| 833 | @Override |
Winson Chung | 7f0acdd | 2011-09-19 18:34:19 -0700 | [diff] [blame] | 834 | protected void onDetachedFromWindow() { |
| 835 | super.onDetachedFromWindow(); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 836 | cancelAllTasks(); |
| 837 | } |
Winson Chung | 7f0acdd | 2011-09-19 18:34:19 -0700 | [diff] [blame] | 838 | |
Michael Jurka | e326f18 | 2011-11-21 14:05:46 -0800 | [diff] [blame] | 839 | public void clearAllWidgetPages() { |
| 840 | cancelAllTasks(); |
| 841 | int count = getChildCount(); |
| 842 | for (int i = 0; i < count; i++) { |
| 843 | View v = getPageAt(i); |
| 844 | if (v instanceof PagedViewGridLayout) { |
| 845 | ((PagedViewGridLayout) v).removeAllViewsOnPage(); |
| 846 | mDirtyPageContent.set(i, true); |
| 847 | } |
| 848 | } |
| 849 | } |
| 850 | |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 851 | private void cancelAllTasks() { |
Winson Chung | 7f0acdd | 2011-09-19 18:34:19 -0700 | [diff] [blame] | 852 | // Clean up all the async tasks |
| 853 | Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator(); |
| 854 | while (iter.hasNext()) { |
| 855 | AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next(); |
| 856 | task.cancel(false); |
| 857 | iter.remove(); |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 858 | mDirtyPageContent.set(task.page, true); |
Winson Chung | 7ce9985 | 2012-05-24 17:34:08 -0700 | [diff] [blame] | 859 | |
| 860 | // We've already preallocated the views for the data to load into, so clear them as well |
| 861 | View v = getPageAt(task.page); |
| 862 | if (v instanceof PagedViewGridLayout) { |
| 863 | ((PagedViewGridLayout) v).removeAllViewsOnPage(); |
| 864 | } |
Winson Chung | 7f0acdd | 2011-09-19 18:34:19 -0700 | [diff] [blame] | 865 | } |
Winson Chung | 83687b1 | 2012-04-25 16:01:01 -0700 | [diff] [blame] | 866 | mDeferredSyncWidgetPageItems.clear(); |
Michael Jurka | f6a9690 | 2012-06-06 11:48:13 -0700 | [diff] [blame] | 867 | mDeferredPrepareLoadWidgetPreviewsTasks.clear(); |
Winson Chung | 7f0acdd | 2011-09-19 18:34:19 -0700 | [diff] [blame] | 868 | } |
| 869 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 870 | public void setContentType(ContentType type) { |
Michael Jurka | d9546fc | 2013-10-23 15:38:48 +0200 | [diff] [blame] | 871 | // Widgets appear to be cleared every time you leave, always force invalidate for them |
| 872 | if (mContentType != type || type == ContentType.Widgets) { |
| 873 | int page = (mContentType != type) ? 0 : getCurrentPage(); |
| 874 | mContentType = type; |
| 875 | invalidatePageData(page, true); |
Winson Chung | 7819a56 | 2013-09-19 15:55:45 -0700 | [diff] [blame] | 876 | } |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | public ContentType getContentType() { |
| 880 | return mContentType; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 881 | } |
| 882 | |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 883 | protected void snapToPage(int whichPage, int delta, int duration) { |
| 884 | super.snapToPage(whichPage, delta, duration); |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 885 | |
| 886 | // Update the thread priorities given the direction lookahead |
| 887 | Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator(); |
| 888 | while (iter.hasNext()) { |
| 889 | AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next(); |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 890 | int pageIndex = task.page; |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 891 | if ((mNextPage > mCurrentPage && pageIndex >= mCurrentPage) || |
| 892 | (mNextPage < mCurrentPage && pageIndex <= mCurrentPage)) { |
| 893 | task.setThreadPriority(getThreadPriorityForPage(pageIndex)); |
| 894 | } else { |
| 895 | task.setThreadPriority(Process.THREAD_PRIORITY_LOWEST); |
| 896 | } |
| 897 | } |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 898 | } |
| 899 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 900 | /* |
| 901 | * Apps PagedView implementation |
| 902 | */ |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 903 | private void setVisibilityOnChildren(ViewGroup layout, int visibility) { |
| 904 | int childCount = layout.getChildCount(); |
| 905 | for (int i = 0; i < childCount; ++i) { |
| 906 | layout.getChildAt(i).setVisibility(visibility); |
| 907 | } |
| 908 | } |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 909 | private void setupPage(AppsCustomizeCellLayout layout) { |
| 910 | layout.setGridSize(mCellCountX, mCellCountY); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 911 | |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 912 | // Note: We force a measure here to get around the fact that when we do layout calculations |
| 913 | // immediately after syncing, we don't have a proper width. That said, we already know the |
| 914 | // expected page width, so we can actually optimize by hiding all the TextView-based |
| 915 | // children that are expensive to measure, and let that happen naturally later. |
| 916 | setVisibilityOnChildren(layout, View.GONE); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 917 | int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST); |
| 918 | int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 919 | layout.measure(widthSpec, heightSpec); |
Adam Cohen | 96bb798 | 2014-07-07 11:58:56 -0700 | [diff] [blame] | 920 | |
Adam Cohen | 4e243a2 | 2014-08-10 18:30:55 -0700 | [diff] [blame] | 921 | Drawable bg = getContext().getResources().getDrawable(R.drawable.quantum_panel); |
Adam Cohen | 9bfdb76 | 2014-07-21 17:44:06 -0700 | [diff] [blame] | 922 | if (bg != null) { |
Adam Cohen | 63f1ec0 | 2014-08-12 09:23:13 -0700 | [diff] [blame] | 923 | bg.setAlpha(mPageBackgroundsVisible ? 255: 0); |
Adam Cohen | 9bfdb76 | 2014-07-21 17:44:06 -0700 | [diff] [blame] | 924 | layout.setBackground(bg); |
Adam Cohen | 9bfdb76 | 2014-07-21 17:44:06 -0700 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | setVisibilityOnChildren(layout, View.VISIBLE); |
| 928 | } |
| 929 | |
| 930 | public void setPageBackgroundsVisible(boolean visible) { |
| 931 | mPageBackgroundsVisible = visible; |
| 932 | int childCount = getChildCount(); |
| 933 | for (int i = 0; i < childCount; ++i) { |
| 934 | Drawable bg = getChildAt(i).getBackground(); |
| 935 | if (bg != null) { |
Adam Cohen | 63f1ec0 | 2014-08-12 09:23:13 -0700 | [diff] [blame] | 936 | bg.setAlpha(visible ? 255 : 0); |
Adam Cohen | 9bfdb76 | 2014-07-21 17:44:06 -0700 | [diff] [blame] | 937 | } |
| 938 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 939 | } |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 940 | |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 941 | public void syncAppsPageItems(int page, boolean immediate) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 942 | // ensure that we have the right number of items on the pages |
Winson Chung | fe1fe26 | 2013-04-01 16:52:31 -0700 | [diff] [blame] | 943 | final boolean isRtl = isLayoutRtl(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 944 | int numCells = mCellCountX * mCellCountY; |
| 945 | int startIndex = page * numCells; |
| 946 | int endIndex = Math.min(startIndex + numCells, mApps.size()); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 947 | AppsCustomizeCellLayout layout = (AppsCustomizeCellLayout) getPageAt(page); |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 948 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 949 | layout.removeAllViewsOnPage(); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 950 | ArrayList<Object> items = new ArrayList<Object>(); |
| 951 | ArrayList<Bitmap> images = new ArrayList<Bitmap>(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 952 | for (int i = startIndex; i < endIndex; ++i) { |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 953 | AppInfo info = mApps.get(i); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 954 | BubbleTextView icon = (BubbleTextView) mLayoutInflater.inflate( |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 955 | R.layout.apps_customize_application, layout, false); |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame] | 956 | icon.applyFromApplicationInfo(info); |
| 957 | icon.setOnClickListener(mLauncher); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 958 | icon.setOnLongClickListener(this); |
| 959 | icon.setOnTouchListener(this); |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 960 | icon.setOnKeyListener(this); |
Sunny Goyal | dcbcc86 | 2014-08-12 15:58:36 -0700 | [diff] [blame] | 961 | icon.setOnFocusChangeListener(layout.mFocusHandlerView); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 962 | |
| 963 | int index = i - startIndex; |
| 964 | int x = index % mCellCountX; |
| 965 | int y = index / mCellCountX; |
Winson Chung | fe1fe26 | 2013-04-01 16:52:31 -0700 | [diff] [blame] | 966 | if (isRtl) { |
| 967 | x = mCellCountX - x - 1; |
| 968 | } |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 969 | layout.addViewToCellLayout(icon, -1, i, new CellLayout.LayoutParams(x,y, 1,1), false); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 970 | |
| 971 | items.add(info); |
| 972 | images.add(info.iconBitmap); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 973 | } |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 974 | |
Michael Jurka | 47639b9 | 2013-01-14 12:42:27 +0100 | [diff] [blame] | 975 | enableHwLayersOnVisiblePages(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 976 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 977 | |
| 978 | /** |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 979 | * A helper to return the priority for loading of the specified widget page. |
| 980 | */ |
| 981 | private int getWidgetPageLoadPriority(int page) { |
| 982 | // If we are snapping to another page, use that index as the target page index |
| 983 | int toPage = mCurrentPage; |
| 984 | if (mNextPage > -1) { |
| 985 | toPage = mNextPage; |
| 986 | } |
| 987 | |
| 988 | // We use the distance from the target page as an initial guess of priority, but if there |
| 989 | // are no pages of higher priority than the page specified, then bump up the priority of |
| 990 | // the specified page. |
| 991 | Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator(); |
| 992 | int minPageDiff = Integer.MAX_VALUE; |
| 993 | while (iter.hasNext()) { |
| 994 | AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next(); |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 995 | minPageDiff = Math.abs(task.page - toPage); |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | int rawPageDiff = Math.abs(page - toPage); |
| 999 | return rawPageDiff - Math.min(rawPageDiff, minPageDiff); |
| 1000 | } |
| 1001 | /** |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1002 | * Return the appropriate thread priority for loading for a given page (we give the current |
| 1003 | * page much higher priority) |
| 1004 | */ |
| 1005 | private int getThreadPriorityForPage(int page) { |
| 1006 | // 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] | 1007 | int pageDiff = getWidgetPageLoadPriority(page); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1008 | if (pageDiff <= 0) { |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1009 | return Process.THREAD_PRIORITY_LESS_FAVORABLE; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1010 | } else if (pageDiff <= 1) { |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1011 | return Process.THREAD_PRIORITY_LOWEST; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1012 | } else { |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1013 | return Process.THREAD_PRIORITY_LOWEST; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1014 | } |
| 1015 | } |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1016 | private int getSleepForPage(int page) { |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1017 | int pageDiff = getWidgetPageLoadPriority(page); |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1018 | return Math.max(0, pageDiff * sPageSleepDelay); |
| 1019 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1020 | /** |
| 1021 | * Creates and executes a new AsyncTask to load a page of widget previews. |
| 1022 | */ |
| 1023 | private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets, |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 1024 | int cellWidth, int cellHeight, int cellCountX) { |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1025 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1026 | // Prune all tasks that are no longer needed |
| 1027 | Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator(); |
| 1028 | while (iter.hasNext()) { |
| 1029 | AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next(); |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 1030 | int taskPage = task.page; |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1031 | if (taskPage < getAssociatedLowerPageBound(mCurrentPage) || |
| 1032 | taskPage > getAssociatedUpperPageBound(mCurrentPage)) { |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1033 | task.cancel(false); |
| 1034 | iter.remove(); |
| 1035 | } else { |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1036 | task.setThreadPriority(getThreadPriorityForPage(taskPage)); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1037 | } |
| 1038 | } |
| 1039 | |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1040 | // We introduce a slight delay to order the loading of side pages so that we don't thrash |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 1041 | final int sleepMs = getSleepForPage(page); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1042 | AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight, |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1043 | new AsyncTaskCallback() { |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1044 | @Override |
| 1045 | public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) { |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1046 | try { |
Winson Chung | 0994593 | 2011-09-20 14:22:40 -0700 | [diff] [blame] | 1047 | try { |
| 1048 | Thread.sleep(sleepMs); |
| 1049 | } catch (Exception e) {} |
| 1050 | loadWidgetPreviewsInBackground(task, data); |
| 1051 | } finally { |
| 1052 | if (task.isCancelled()) { |
| 1053 | data.cleanup(true); |
| 1054 | } |
| 1055 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1056 | } |
| 1057 | }, |
| 1058 | new AsyncTaskCallback() { |
| 1059 | @Override |
| 1060 | public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) { |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 1061 | mRunningTasks.remove(task); |
| 1062 | if (task.isCancelled()) return; |
| 1063 | // do cleanup inside onSyncWidgetPageItems |
Winson Chung | 7bb3752 | 2013-10-28 11:07:57 -0700 | [diff] [blame] | 1064 | onSyncWidgetPageItems(data, false); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1065 | } |
Michael Jurka | 9c5cc5a | 2014-01-09 14:59:22 +0100 | [diff] [blame] | 1066 | }, getWidgetPreviewLoader()); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1067 | |
| 1068 | // Ensure that the task is appropriately prioritized and runs in parallel |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1069 | AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page, |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 1070 | AsyncTaskPageData.Type.LoadWidgetPreviewData); |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 1071 | t.setThreadPriority(getThreadPriorityForPage(page)); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1072 | t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData); |
| 1073 | mRunningTasks.add(t); |
| 1074 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1075 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1076 | /* |
| 1077 | * Widgets PagedView implementation |
| 1078 | */ |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 1079 | private void setupPage(PagedViewGridLayout layout) { |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 1080 | // 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] | 1081 | // immediately after syncing, we don't have a proper width. |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 1082 | int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST); |
| 1083 | int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST); |
Adam Cohen | 63f1ec0 | 2014-08-12 09:23:13 -0700 | [diff] [blame] | 1084 | |
| 1085 | Drawable bg = getContext().getResources().getDrawable(R.drawable.quantum_panel_dark); |
| 1086 | if (bg != null) { |
| 1087 | bg.setAlpha(mPageBackgroundsVisible ? 255 : 0); |
| 1088 | layout.setBackground(bg); |
| 1089 | } |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 1090 | layout.measure(widthSpec, heightSpec); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1091 | } |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1092 | |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1093 | public void syncWidgetPageItems(final int page, final boolean immediate) { |
Winson Chung | 6a3fd3f | 2011-08-02 14:03:26 -0700 | [diff] [blame] | 1094 | int numItemsPerPage = mWidgetCountX * mWidgetCountY; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1095 | |
Adam Cohen | 4e243a2 | 2014-08-10 18:30:55 -0700 | [diff] [blame] | 1096 | final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page); |
| 1097 | |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 1098 | // Calculate the dimensions of each cell we are giving to each widget |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1099 | final ArrayList<Object> items = new ArrayList<Object>(); |
Adam Cohen | a00673c | 2014-08-14 12:57:28 -0700 | [diff] [blame] | 1100 | int contentWidth = mContentWidth - layout.getPaddingLeft() - layout.getPaddingRight(); |
Adam Cohen | 4e243a2 | 2014-08-10 18:30:55 -0700 | [diff] [blame] | 1101 | final int cellWidth = contentWidth / mWidgetCountX; |
Adam Cohen | a00673c | 2014-08-14 12:57:28 -0700 | [diff] [blame] | 1102 | int contentHeight = mContentHeight - layout.getPaddingTop() - layout.getPaddingBottom(); |
| 1103 | |
Adam Cohen | 4e243a2 | 2014-08-10 18:30:55 -0700 | [diff] [blame] | 1104 | final int cellHeight = contentHeight / mWidgetCountY; |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 1105 | |
Winson Chung | e4a647f | 2011-09-30 14:41:25 -0700 | [diff] [blame] | 1106 | // Prepare the set of widgets to load previews for in the background |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 1107 | int offset = page * numItemsPerPage; |
Winson Chung | 6a3fd3f | 2011-08-02 14:03:26 -0700 | [diff] [blame] | 1108 | for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) { |
| 1109 | items.add(mWidgets.get(i)); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
Winson Chung | e4a647f | 2011-09-30 14:41:25 -0700 | [diff] [blame] | 1112 | // Prepopulate the pages with the other widget info, and fill in the previews later |
Winson Chung | e4a647f | 2011-09-30 14:41:25 -0700 | [diff] [blame] | 1113 | layout.setColumnCount(layout.getCellCountX()); |
| 1114 | for (int i = 0; i < items.size(); ++i) { |
| 1115 | Object rawInfo = items.get(i); |
| 1116 | PendingAddItemInfo createItemInfo = null; |
| 1117 | PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate( |
| 1118 | R.layout.apps_customize_widget, layout, false); |
| 1119 | if (rawInfo instanceof AppWidgetProviderInfo) { |
| 1120 | // Fill in the widget information |
| 1121 | AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo; |
| 1122 | createItemInfo = new PendingAddWidgetInfo(info, null, null); |
Adam Cohen | 1f36270 | 2012-04-04 14:58:12 -0700 | [diff] [blame] | 1123 | |
| 1124 | // Determine the widget spans and min resize spans. |
Adam Cohen | 2f093b6 | 2012-04-30 18:59:53 -0700 | [diff] [blame] | 1125 | int[] spanXY = Launcher.getSpanForWidget(mLauncher, info); |
Adam Cohen | 1f36270 | 2012-04-04 14:58:12 -0700 | [diff] [blame] | 1126 | createItemInfo.spanX = spanXY[0]; |
| 1127 | createItemInfo.spanY = spanXY[1]; |
Adam Cohen | 2f093b6 | 2012-04-30 18:59:53 -0700 | [diff] [blame] | 1128 | int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, info); |
Adam Cohen | 1f36270 | 2012-04-04 14:58:12 -0700 | [diff] [blame] | 1129 | createItemInfo.minSpanX = minSpanXY[0]; |
| 1130 | createItemInfo.minSpanY = minSpanXY[1]; |
| 1131 | |
Michael Jurka | 9c5cc5a | 2014-01-09 14:59:22 +0100 | [diff] [blame] | 1132 | widget.applyFromAppWidgetProviderInfo(info, -1, spanXY, getWidgetPreviewLoader()); |
Winson Chung | e4a647f | 2011-09-30 14:41:25 -0700 | [diff] [blame] | 1133 | widget.setTag(createItemInfo); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 1134 | widget.setShortPressListener(this); |
Winson Chung | e4a647f | 2011-09-30 14:41:25 -0700 | [diff] [blame] | 1135 | } else if (rawInfo instanceof ResolveInfo) { |
| 1136 | // Fill in the shortcuts information |
| 1137 | ResolveInfo info = (ResolveInfo) rawInfo; |
Michael Jurka | dac8591 | 2012-05-18 15:04:49 -0700 | [diff] [blame] | 1138 | createItemInfo = new PendingAddShortcutInfo(info.activityInfo); |
Winson Chung | e4a647f | 2011-09-30 14:41:25 -0700 | [diff] [blame] | 1139 | createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT; |
| 1140 | createItemInfo.componentName = new ComponentName(info.activityInfo.packageName, |
| 1141 | info.activityInfo.name); |
Michael Jurka | 9c5cc5a | 2014-01-09 14:59:22 +0100 | [diff] [blame] | 1142 | widget.applyFromResolveInfo(mPackageManager, info, getWidgetPreviewLoader()); |
Winson Chung | e4a647f | 2011-09-30 14:41:25 -0700 | [diff] [blame] | 1143 | widget.setTag(createItemInfo); |
| 1144 | } |
| 1145 | widget.setOnClickListener(this); |
| 1146 | widget.setOnLongClickListener(this); |
| 1147 | widget.setOnTouchListener(this); |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 1148 | widget.setOnKeyListener(this); |
Winson Chung | e4a647f | 2011-09-30 14:41:25 -0700 | [diff] [blame] | 1149 | |
| 1150 | // Layout each widget |
| 1151 | int ix = i % mWidgetCountX; |
| 1152 | int iy = i / mWidgetCountX; |
Adam Cohen | 4e243a2 | 2014-08-10 18:30:55 -0700 | [diff] [blame] | 1153 | |
| 1154 | if (ix > 0) { |
| 1155 | View border = widget.findViewById(R.id.left_border); |
| 1156 | border.setVisibility(View.VISIBLE); |
| 1157 | } |
| 1158 | if (ix < mWidgetCountX - 1) { |
| 1159 | View border = widget.findViewById(R.id.right_border); |
| 1160 | border.setVisibility(View.VISIBLE); |
| 1161 | } |
| 1162 | |
Winson Chung | e4a647f | 2011-09-30 14:41:25 -0700 | [diff] [blame] | 1163 | GridLayout.LayoutParams lp = new GridLayout.LayoutParams( |
Fabrice Di Meglio | cc11f74 | 2012-12-18 16:25:49 -0800 | [diff] [blame] | 1164 | GridLayout.spec(iy, GridLayout.START), |
Winson Chung | e4a647f | 2011-09-30 14:41:25 -0700 | [diff] [blame] | 1165 | GridLayout.spec(ix, GridLayout.TOP)); |
| 1166 | lp.width = cellWidth; |
| 1167 | lp.height = cellHeight; |
Fabrice Di Meglio | cc11f74 | 2012-12-18 16:25:49 -0800 | [diff] [blame] | 1168 | lp.setGravity(Gravity.TOP | Gravity.START); |
Winson Chung | e4a647f | 2011-09-30 14:41:25 -0700 | [diff] [blame] | 1169 | layout.addView(widget, lp); |
| 1170 | } |
| 1171 | |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1172 | // wait until a call on onLayout to start loading, because |
| 1173 | // PagedViewWidget.getPreviewSize() will return 0 if it hasn't been laid out |
| 1174 | // TODO: can we do a measure/layout immediately? |
| 1175 | layout.setOnLayoutListener(new Runnable() { |
| 1176 | public void run() { |
| 1177 | // Load the widget previews |
| 1178 | int maxPreviewWidth = cellWidth; |
| 1179 | int maxPreviewHeight = cellHeight; |
| 1180 | if (layout.getChildCount() > 0) { |
| 1181 | PagedViewWidget w = (PagedViewWidget) layout.getChildAt(0); |
| 1182 | int[] maxSize = w.getPreviewSize(); |
| 1183 | maxPreviewWidth = maxSize[0]; |
| 1184 | maxPreviewHeight = maxSize[1]; |
| 1185 | } |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 1186 | |
Michael Jurka | 9c5cc5a | 2014-01-09 14:59:22 +0100 | [diff] [blame] | 1187 | getWidgetPreviewLoader().setPreviewSize( |
Michael Jurka | 3f4e070 | 2013-02-05 11:21:28 +0100 | [diff] [blame] | 1188 | maxPreviewWidth, maxPreviewHeight, mWidgetSpacingLayout); |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1189 | if (immediate) { |
| 1190 | AsyncTaskPageData data = new AsyncTaskPageData(page, items, |
Michael Jurka | 9c5cc5a | 2014-01-09 14:59:22 +0100 | [diff] [blame] | 1191 | maxPreviewWidth, maxPreviewHeight, null, null, getWidgetPreviewLoader()); |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1192 | loadWidgetPreviewsInBackground(null, data); |
Winson Chung | 7bb3752 | 2013-10-28 11:07:57 -0700 | [diff] [blame] | 1193 | onSyncWidgetPageItems(data, immediate); |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1194 | } else { |
Michael Jurka | f6a9690 | 2012-06-06 11:48:13 -0700 | [diff] [blame] | 1195 | if (mInTransition) { |
| 1196 | mDeferredPrepareLoadWidgetPreviewsTasks.add(this); |
| 1197 | } else { |
| 1198 | prepareLoadWidgetPreviewsTask(page, items, |
| 1199 | maxPreviewWidth, maxPreviewHeight, mWidgetCountX); |
| 1200 | } |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1201 | } |
Michael Jurka | 3c69dec | 2013-02-06 13:43:54 +0100 | [diff] [blame] | 1202 | layout.setOnLayoutListener(null); |
Michael Jurka | 038f9d8 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1203 | } |
| 1204 | }); |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1205 | } |
| 1206 | private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task, |
| 1207 | AsyncTaskPageData data) { |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1208 | // loadWidgetPreviewsInBackground can be called without a task to load a set of widget |
| 1209 | // previews synchronously |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1210 | if (task != null) { |
| 1211 | // Ensure that this task starts running at the correct priority |
| 1212 | task.syncThreadPriority(); |
| 1213 | } |
| 1214 | |
| 1215 | // Load each of the widget/shortcut previews |
| 1216 | ArrayList<Object> items = data.items; |
| 1217 | ArrayList<Bitmap> images = data.generatedImages; |
| 1218 | int count = items.size(); |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1219 | for (int i = 0; i < count; ++i) { |
| 1220 | if (task != null) { |
| 1221 | // Ensure we haven't been cancelled yet |
| 1222 | if (task.isCancelled()) break; |
| 1223 | // Before work on each item, ensure that this task is running at the correct |
| 1224 | // priority |
| 1225 | task.syncThreadPriority(); |
| 1226 | } |
| 1227 | |
Michael Jurka | 9c5cc5a | 2014-01-09 14:59:22 +0100 | [diff] [blame] | 1228 | images.add(getWidgetPreviewLoader().getPreview(items.get(i))); |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1229 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1230 | } |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 1231 | |
Winson Chung | 7bb3752 | 2013-10-28 11:07:57 -0700 | [diff] [blame] | 1232 | private void onSyncWidgetPageItems(AsyncTaskPageData data, boolean immediatelySyncItems) { |
| 1233 | if (!immediatelySyncItems && mInTransition) { |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 1234 | mDeferredSyncWidgetPageItems.add(data); |
| 1235 | return; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1236 | } |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 1237 | try { |
| 1238 | int page = data.page; |
| 1239 | PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1240 | |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 1241 | ArrayList<Object> items = data.items; |
| 1242 | int count = items.size(); |
| 1243 | for (int i = 0; i < count; ++i) { |
| 1244 | PagedViewWidget widget = (PagedViewWidget) layout.getChildAt(i); |
| 1245 | if (widget != null) { |
| 1246 | Bitmap preview = data.generatedImages.get(i); |
| 1247 | widget.applyPreview(new FastBitmapDrawable(preview), i); |
| 1248 | } |
| 1249 | } |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1250 | |
Michael Jurka | 47639b9 | 2013-01-14 12:42:27 +0100 | [diff] [blame] | 1251 | enableHwLayersOnVisiblePages(); |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 1252 | |
| 1253 | // Update all thread priorities |
| 1254 | Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator(); |
| 1255 | while (iter.hasNext()) { |
| 1256 | AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next(); |
| 1257 | int pageIndex = task.page; |
| 1258 | task.setThreadPriority(getThreadPriorityForPage(pageIndex)); |
| 1259 | } |
| 1260 | } finally { |
| 1261 | data.cleanup(false); |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1262 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1263 | } |
Winson Chung | 46af2e8 | 2011-05-09 16:00:53 -0700 | [diff] [blame] | 1264 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1265 | @Override |
| 1266 | public void syncPages() { |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 1267 | disablePagedViewAnimations(); |
| 1268 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1269 | removeAllViews(); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1270 | cancelAllTasks(); |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 1271 | |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1272 | Context context = getContext(); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 1273 | if (mContentType == ContentType.Applications) { |
| 1274 | for (int i = 0; i < mNumAppsPages; ++i) { |
| 1275 | AppsCustomizeCellLayout layout = new AppsCustomizeCellLayout(context); |
| 1276 | setupPage(layout); |
| 1277 | addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT, |
| 1278 | LayoutParams.MATCH_PARENT)); |
| 1279 | } |
| 1280 | } else if (mContentType == ContentType.Widgets) { |
| 1281 | for (int j = 0; j < mNumWidgetPages; ++j) { |
| 1282 | PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX, |
| 1283 | mWidgetCountY); |
| 1284 | setupPage(layout); |
| 1285 | addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT, |
| 1286 | LayoutParams.MATCH_PARENT)); |
| 1287 | } |
| 1288 | } else { |
| 1289 | throw new RuntimeException("Invalid ContentType"); |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 1290 | } |
| 1291 | |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 1292 | enablePagedViewAnimations(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1293 | } |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1294 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1295 | @Override |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1296 | public void syncPageItems(int page, boolean immediate) { |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 1297 | if (mContentType == ContentType.Widgets) { |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 1298 | syncWidgetPageItems(page, immediate); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 1299 | } else { |
| 1300 | syncAppsPageItems(page, immediate); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1301 | } |
| 1302 | } |
| 1303 | |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1304 | // We want our pages to be z-ordered such that the further a page is to the left, the higher |
| 1305 | // it is in the z-order. This is important to insure touch events are handled correctly. |
| 1306 | View getPageAt(int index) { |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 1307 | return getChildAt(indexToPage(index)); |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1308 | } |
| 1309 | |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 1310 | @Override |
| 1311 | protected int indexToPage(int index) { |
| 1312 | return getChildCount() - index - 1; |
| 1313 | } |
| 1314 | |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1315 | // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack. |
| 1316 | @Override |
| 1317 | protected void screenScrolled(int screenCenter) { |
| 1318 | super.screenScrolled(screenCenter); |
Michael Jurka | 47639b9 | 2013-01-14 12:42:27 +0100 | [diff] [blame] | 1319 | enableHwLayersOnVisiblePages(); |
| 1320 | } |
| 1321 | |
| 1322 | private void enableHwLayersOnVisiblePages() { |
| 1323 | final int screenCount = getChildCount(); |
| 1324 | |
| 1325 | getVisiblePages(mTempVisiblePagesRange); |
| 1326 | int leftScreen = mTempVisiblePagesRange[0]; |
| 1327 | int rightScreen = mTempVisiblePagesRange[1]; |
| 1328 | int forceDrawScreen = -1; |
| 1329 | if (leftScreen == rightScreen) { |
| 1330 | // make sure we're caching at least two pages always |
| 1331 | if (rightScreen < screenCount - 1) { |
| 1332 | rightScreen++; |
| 1333 | forceDrawScreen = rightScreen; |
| 1334 | } else if (leftScreen > 0) { |
| 1335 | leftScreen--; |
| 1336 | forceDrawScreen = leftScreen; |
| 1337 | } |
| 1338 | } else { |
| 1339 | forceDrawScreen = leftScreen + 1; |
| 1340 | } |
| 1341 | |
| 1342 | for (int i = 0; i < screenCount; i++) { |
| 1343 | final View layout = (View) getPageAt(i); |
| 1344 | if (!(leftScreen <= i && i <= rightScreen && |
| 1345 | (i == forceDrawScreen || shouldDrawChild(layout)))) { |
| 1346 | layout.setLayerType(LAYER_TYPE_NONE, null); |
| 1347 | } |
| 1348 | } |
| 1349 | |
Michael Jurka | 47639b9 | 2013-01-14 12:42:27 +0100 | [diff] [blame] | 1350 | for (int i = 0; i < screenCount; i++) { |
| 1351 | final View layout = (View) getPageAt(i); |
| 1352 | |
| 1353 | if (leftScreen <= i && i <= rightScreen && |
| 1354 | (i == forceDrawScreen || shouldDrawChild(layout))) { |
| 1355 | if (layout.getLayerType() != LAYER_TYPE_HARDWARE) { |
| 1356 | layout.setLayerType(LAYER_TYPE_HARDWARE, null); |
| 1357 | } |
| 1358 | } |
| 1359 | } |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | protected void overScroll(float amount) { |
Adam Cohen | 1e4359c | 2014-08-18 13:12:16 -0700 | [diff] [blame] | 1363 | dampedOverScroll(amount); |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1364 | } |
| 1365 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1366 | /** |
| 1367 | * Used by the parent to get the content width to set the tab bar to |
| 1368 | * @return |
| 1369 | */ |
| 1370 | public int getPageContentWidth() { |
| 1371 | return mContentWidth; |
| 1372 | } |
| 1373 | |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 1374 | @Override |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 1375 | protected void onPageEndMoving() { |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 1376 | super.onPageEndMoving(); |
Michael Jurka | 5e368ff | 2012-05-14 23:13:15 -0700 | [diff] [blame] | 1377 | mForceDrawAllChildrenNextFrame = true; |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 1378 | // We reset the save index when we change pages so that it will be recalculated on next |
| 1379 | // rotation |
| 1380 | mSaveInstanceStateItemIndex = -1; |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 1381 | } |
| 1382 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1383 | /* |
| 1384 | * AllAppsView implementation |
| 1385 | */ |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1386 | public void setup(Launcher launcher, DragController dragController) { |
| 1387 | mLauncher = launcher; |
| 1388 | mDragController = dragController; |
| 1389 | } |
Winson Chung | 9802ac9 | 2012-06-08 16:01:58 -0700 | [diff] [blame] | 1390 | |
| 1391 | /** |
| 1392 | * We should call thise method whenever the core data changes (mApps, mWidgets) so that we can |
| 1393 | * appropriately determine when to invalidate the PagedView page data. In cases where the data |
| 1394 | * has yet to be set, we can requestLayout() and wait for onDataReady() to be called in the |
| 1395 | * next onMeasure() pass, which will trigger an invalidatePageData() itself. |
| 1396 | */ |
| 1397 | private void invalidateOnDataChange() { |
| 1398 | if (!isDataReady()) { |
| 1399 | // The next layout pass will trigger data-ready if both widgets and apps are set, so |
| 1400 | // request a layout to trigger the page data when ready. |
| 1401 | requestLayout(); |
| 1402 | } else { |
| 1403 | cancelAllTasks(); |
| 1404 | invalidatePageData(); |
| 1405 | } |
| 1406 | } |
| 1407 | |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 1408 | public void setApps(ArrayList<AppInfo> list) { |
Nilesh Agrawal | 16f3ea8 | 2014-01-09 17:14:01 -0800 | [diff] [blame] | 1409 | if (!LauncherAppState.isDisableAllApps()) { |
Adam Cohen | 947dc54 | 2013-06-06 22:43:33 -0700 | [diff] [blame] | 1410 | mApps = list; |
| 1411 | Collections.sort(mApps, LauncherModel.getAppNameComparator()); |
| 1412 | updatePageCountsAndInvalidateData(); |
| 1413 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1414 | } |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 1415 | private void addAppsWithoutInvalidate(ArrayList<AppInfo> list) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1416 | // We add it in place, in alphabetical order |
| 1417 | int count = list.size(); |
| 1418 | for (int i = 0; i < count; ++i) { |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 1419 | AppInfo info = list.get(i); |
Winson Chung | 1190487 | 2012-09-17 16:58:46 -0700 | [diff] [blame] | 1420 | int index = Collections.binarySearch(mApps, info, LauncherModel.getAppNameComparator()); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1421 | if (index < 0) { |
| 1422 | mApps.add(-(index + 1), info); |
| 1423 | } |
| 1424 | } |
| 1425 | } |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 1426 | public void addApps(ArrayList<AppInfo> list) { |
Nilesh Agrawal | 16f3ea8 | 2014-01-09 17:14:01 -0800 | [diff] [blame] | 1427 | if (!LauncherAppState.isDisableAllApps()) { |
Adam Cohen | 947dc54 | 2013-06-06 22:43:33 -0700 | [diff] [blame] | 1428 | addAppsWithoutInvalidate(list); |
| 1429 | updatePageCountsAndInvalidateData(); |
Adam Cohen | 947dc54 | 2013-06-06 22:43:33 -0700 | [diff] [blame] | 1430 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1431 | } |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 1432 | private int findAppByComponent(List<AppInfo> list, AppInfo item) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1433 | ComponentName removeComponent = item.intent.getComponent(); |
| 1434 | int length = list.size(); |
| 1435 | for (int i = 0; i < length; ++i) { |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 1436 | AppInfo info = list.get(i); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1437 | if (info.user.equals(item.user) |
| 1438 | && info.intent.getComponent().equals(removeComponent)) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1439 | return i; |
| 1440 | } |
| 1441 | } |
| 1442 | return -1; |
| 1443 | } |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 1444 | private void removeAppsWithoutInvalidate(ArrayList<AppInfo> list) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1445 | // loop through all the apps and remove apps that have the same component |
| 1446 | int length = list.size(); |
| 1447 | for (int i = 0; i < length; ++i) { |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 1448 | AppInfo info = list.get(i); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1449 | int removeIndex = findAppByComponent(mApps, info); |
| 1450 | if (removeIndex > -1) { |
| 1451 | mApps.remove(removeIndex); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1452 | } |
| 1453 | } |
| 1454 | } |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 1455 | public void removeApps(ArrayList<AppInfo> appInfos) { |
Nilesh Agrawal | 16f3ea8 | 2014-01-09 17:14:01 -0800 | [diff] [blame] | 1456 | if (!LauncherAppState.isDisableAllApps()) { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1457 | removeAppsWithoutInvalidate(appInfos); |
| 1458 | updatePageCountsAndInvalidateData(); |
| 1459 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1460 | } |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 1461 | public void updateApps(ArrayList<AppInfo> list) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1462 | // We remove and re-add the updated applications list because it's properties may have |
| 1463 | // changed (ie. the title), and this will ensure that the items will be in their proper |
| 1464 | // place in the list. |
Nilesh Agrawal | 16f3ea8 | 2014-01-09 17:14:01 -0800 | [diff] [blame] | 1465 | if (!LauncherAppState.isDisableAllApps()) { |
Adam Cohen | 947dc54 | 2013-06-06 22:43:33 -0700 | [diff] [blame] | 1466 | removeAppsWithoutInvalidate(list); |
| 1467 | addAppsWithoutInvalidate(list); |
| 1468 | updatePageCountsAndInvalidateData(); |
| 1469 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1470 | } |
Michael Jurka | 35aa14d | 2011-07-07 17:01:08 -0700 | [diff] [blame] | 1471 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1472 | public void reset() { |
Winson Chung | 649668f | 2012-01-10 13:07:16 -0800 | [diff] [blame] | 1473 | // If we have reset, then we should not continue to restore the previous state |
| 1474 | mSaveInstanceStateItemIndex = -1; |
| 1475 | |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 1476 | if (mContentType != ContentType.Applications) { |
| 1477 | setContentType(ContentType.Applications); |
Adam Cohen | b64d36e | 2011-10-17 21:48:02 -0700 | [diff] [blame] | 1478 | } |
Winson Chung | 649668f | 2012-01-10 13:07:16 -0800 | [diff] [blame] | 1479 | |
Adam Cohen | b64d36e | 2011-10-17 21:48:02 -0700 | [diff] [blame] | 1480 | if (mCurrentPage != 0) { |
| 1481 | invalidatePageData(0); |
| 1482 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1483 | } |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1484 | |
| 1485 | private AppsCustomizeTabHost getTabHost() { |
| 1486 | return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane); |
| 1487 | } |
| 1488 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1489 | public void dumpState() { |
| 1490 | // TODO: Dump information related to current list of Applications, Widgets, etc. |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 1491 | AppInfo.dumpApplicationInfoList(TAG, "mApps", mApps); |
Adam Cohen | 0e56cc9 | 2012-05-11 15:57:05 -0700 | [diff] [blame] | 1492 | dumpAppWidgetProviderInfoList(TAG, "mWidgets", mWidgets); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1493 | } |
Adam Cohen | 4e84401 | 2011-11-09 13:48:04 -0800 | [diff] [blame] | 1494 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1495 | private void dumpAppWidgetProviderInfoList(String tag, String label, |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 1496 | ArrayList<Object> list) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1497 | Log.d(tag, label + " size=" + list.size()); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 1498 | for (Object i: list) { |
| 1499 | if (i instanceof AppWidgetProviderInfo) { |
| 1500 | AppWidgetProviderInfo info = (AppWidgetProviderInfo) i; |
| 1501 | Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage |
| 1502 | + " resizeMode=" + info.resizeMode + " configure=" + info.configure |
| 1503 | + " initialLayout=" + info.initialLayout |
| 1504 | + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight); |
| 1505 | } else if (i instanceof ResolveInfo) { |
| 1506 | ResolveInfo info = (ResolveInfo) i; |
| 1507 | Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon=" |
| 1508 | + info.icon); |
| 1509 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1510 | } |
| 1511 | } |
Adam Cohen | 4e84401 | 2011-11-09 13:48:04 -0800 | [diff] [blame] | 1512 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1513 | public void surrender() { |
| 1514 | // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we |
| 1515 | // should stop this now. |
Winson Chung | 4b0ed8c | 2011-10-19 15:24:49 -0700 | [diff] [blame] | 1516 | |
| 1517 | // Stop all background tasks |
| 1518 | cancelAllTasks(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1519 | } |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1520 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1521 | /* |
| 1522 | * We load an extra page on each side to prevent flashes from scrolling and loading of the |
| 1523 | * widget previews in the background with the AsyncTasks. |
| 1524 | */ |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1525 | final static int sLookBehindPageCount = 2; |
| 1526 | final static int sLookAheadPageCount = 2; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1527 | protected int getAssociatedLowerPageBound(int page) { |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1528 | final int count = getChildCount(); |
| 1529 | int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1); |
| 1530 | int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0); |
| 1531 | return windowMinIndex; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1532 | } |
| 1533 | protected int getAssociatedUpperPageBound(int page) { |
| 1534 | final int count = getChildCount(); |
Winson Chung | 68e4c64 | 2011-11-10 15:48:25 -0800 | [diff] [blame] | 1535 | int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1); |
| 1536 | int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1), |
| 1537 | count - 1); |
| 1538 | return windowMaxIndex; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1539 | } |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1540 | |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1541 | protected String getCurrentPageDescription() { |
| 1542 | int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage; |
| 1543 | int stringId = R.string.default_scroll_format; |
Adam Cohen | d3357b1 | 2011-10-18 14:58:11 -0700 | [diff] [blame] | 1544 | int count = 0; |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 1545 | |
| 1546 | if (mContentType == ContentType.Applications) { |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1547 | stringId = R.string.apps_customize_apps_scroll_format; |
Adam Cohen | d3357b1 | 2011-10-18 14:58:11 -0700 | [diff] [blame] | 1548 | count = mNumAppsPages; |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 1549 | } else if (mContentType == ContentType.Widgets) { |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1550 | stringId = R.string.apps_customize_widgets_scroll_format; |
Adam Cohen | d3357b1 | 2011-10-18 14:58:11 -0700 | [diff] [blame] | 1551 | count = mNumWidgetPages; |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 1552 | } else { |
| 1553 | throw new RuntimeException("Invalid ContentType"); |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1554 | } |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1555 | |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1556 | return String.format(getContext().getString(stringId), page + 1, count); |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1557 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1558 | } |