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