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