blob: 58bcf1dbece55ecdb0b21868ff5a5f05bd76c09b [file] [log] [blame]
Winson Chung785d2eb2011-04-14 16:08:02 -07001/*
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 Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung785d2eb2011-04-14 16:08:02 -070018
Adam Cohened66b2b2012-01-23 17:28:51 -080019import android.appwidget.AppWidgetHostView;
Winson Chung785d2eb2011-04-14 16:08:02 -070020import android.appwidget.AppWidgetManager;
21import android.appwidget.AppWidgetProviderInfo;
22import android.content.ComponentName;
23import android.content.Context;
Winson Chung785d2eb2011-04-14 16:08:02 -070024import android.content.pm.PackageManager;
25import android.content.pm.ResolveInfo;
26import android.content.res.Resources;
27import android.content.res.TypedArray;
28import android.graphics.Bitmap;
Michael Jurka05713af2013-01-23 12:39:24 +010029import android.graphics.Point;
Winson Chung785d2eb2011-04-14 16:08:02 -070030import android.graphics.Rect;
31import android.graphics.drawable.Drawable;
Adam Cohen9e05a5e2012-09-10 15:53:09 -070032import android.os.Build;
33import android.os.Bundle;
Winson Chung785d2eb2011-04-14 16:08:02 -070034import android.util.AttributeSet;
35import android.util.Log;
Winson Chung72d8b392011-07-29 13:56:44 -070036import android.view.Gravity;
Winson Chung785d2eb2011-04-14 16:08:02 -070037import android.view.LayoutInflater;
38import android.view.View;
Winson Chungfd3385f2011-06-15 19:51:24 -070039import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070040import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070041import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070042
Daniel Sandler325dc232013-06-05 22:57:57 -040043import com.android.launcher3.DropTarget.DragObject;
Sunny Goyal290800b2015-03-05 11:33:33 -080044import com.android.launcher3.FocusHelper.PagedViewKeyListener;
Sunny Goyalffe83f12014-08-14 17:39:34 -070045import com.android.launcher3.compat.AppWidgetManagerCompat;
Adam Cohen091440a2015-03-18 14:16:05 -070046import com.android.launcher3.util.Thunk;
Adam Cohenc0dcf592011-06-01 15:30:43 -070047
48import java.util.ArrayList;
Winson Chungb44b5242011-06-13 11:32:14 -070049
50/**
51 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
52 */
Winson Chung785d2eb2011-04-14 16:08:02 -070053public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
Sunny Goyal290800b2015-03-05 11:33:33 -080054 View.OnClickListener, DragSource,
Sunny Goyal508da152014-08-14 10:53:27 -070055 PagedViewWidget.ShortPressListener, LauncherTransitionable {
Adam Cohen0e56cc92012-05-11 15:57:05 -070056 static final String TAG = "AppsCustomizePagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070057
Sunny Goyalff572272014-07-23 13:58:07 -070058 private static Rect sTmpRect = new Rect();
Sunny Goyal5b0e6692015-03-19 14:31:19 -070059 private static final int[] sTempPosArray = new int[2];
Sunny Goyalff572272014-07-23 13:58:07 -070060
Winson Chung785d2eb2011-04-14 16:08:02 -070061 /**
62 * The different content types that this paged view can show.
63 */
64 public enum ContentType {
Winson Chung6a26e5b2011-05-26 14:36:06 -070065 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -070066 }
Winson Chungb745afb2015-03-02 11:51:23 -080067 private ContentType mContentType = ContentType.Widgets;
Winson Chung785d2eb2011-04-14 16:08:02 -070068
69 // Refs
Adam Cohen091440a2015-03-18 14:16:05 -070070 @Thunk Launcher mLauncher;
Winson Chung785d2eb2011-04-14 16:08:02 -070071 private DragController mDragController;
72 private final LayoutInflater mLayoutInflater;
73 private final PackageManager mPackageManager;
74
Winson Chung5afbf7b2011-07-25 11:53:08 -070075 // Save and Restore
76 private int mSaveInstanceStateItemIndex = -1;
Winson Chung5afbf7b2011-07-25 11:53:08 -070077
Winson Chung785d2eb2011-04-14 16:08:02 -070078 // Content
Winson Chungd2945262011-06-24 15:22:14 -070079 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -070080
81 // Caching
Winson Chung4dbea792011-05-05 14:21:32 -070082 private IconCache mIconCache;
Winson Chung785d2eb2011-04-14 16:08:02 -070083
84 // Dimens
Winson Chungc58497e2013-09-03 17:48:37 -070085 private int mContentWidth, mContentHeight;
Adam Cohen091440a2015-03-18 14:16:05 -070086 @Thunk int mWidgetCountX, mWidgetCountY;
Adam Cohen0cd3b642011-10-14 14:58:00 -070087 private int mNumWidgetPages;
Winson Chung785d2eb2011-04-14 16:08:02 -070088
Sunny Goyal290800b2015-03-05 11:33:33 -080089 private final PagedViewKeyListener mKeyListener = new PagedViewKeyListener();
90
Adam Cohened66b2b2012-01-23 17:28:51 -080091 private Runnable mInflateWidgetRunnable = null;
92 private Runnable mBindWidgetRunnable = null;
93 static final int WIDGET_NO_CLEANUP_REQUIRED = -1;
Adam Cohen21a170b2012-05-30 15:17:06 -070094 static final int WIDGET_PRELOAD_PENDING = 0;
95 static final int WIDGET_BOUND = 1;
96 static final int WIDGET_INFLATED = 2;
Adam Cohened66b2b2012-01-23 17:28:51 -080097 int mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
98 int mWidgetLoadingId = -1;
Adam Cohen1b36dc32012-02-13 19:27:37 -080099 PendingAddWidgetInfo mCreateWidgetInfo = null;
Adam Cohen7a326642012-02-22 12:03:22 -0800100 private boolean mDraggingWidget = false;
Adam Cohena00673c2014-08-14 12:57:28 -0700101 boolean mPageBackgroundsVisible = true;
Adam Cohened66b2b2012-01-23 17:28:51 -0800102
Winson Chungcb9ab4f2012-07-02 11:47:27 -0700103 private Toast mWidgetInstructionToast;
104
Michael Jurka39e5d172012-03-12 18:36:12 -0700105 // Deferral of loading widget previews during launcher transitions
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700106 private boolean mInTransition;
Michael Jurka39e5d172012-03-12 18:36:12 -0700107
Michael Jurka05713af2013-01-23 12:39:24 +0100108 WidgetPreviewLoader mWidgetPreviewLoader;
109
Michael Jurkac402cd92013-05-20 15:49:32 +0200110 private boolean mInBulkBind;
111 private boolean mNeedToUpdatePageCountsAndInvalidateData;
112
Winson Chung785d2eb2011-04-14 16:08:02 -0700113 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
114 super(context, attrs);
115 mLayoutInflater = LayoutInflater.from(context);
116 mPackageManager = context.getPackageManager();
Winson Chungb745afb2015-03-02 11:51:23 -0800117 mWidgets = new ArrayList<>();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400118 mIconCache = (LauncherAppState.getInstance()).getIconCache();
Winson Chung1ed747a2011-05-03 16:18:34 -0700119
120 // Save the default widget preview background
Winson Chung6032e7e2011-11-08 15:47:17 -0800121 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700122 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
123 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
124 a.recycle();
Winson Chung4b576be2011-04-27 17:40:20 -0700125
Winson Chung1ed747a2011-05-03 16:18:34 -0700126 // The padding on the non-matched dimension for the default widget preview icons
127 // (top + bottom)
Adam Cohen2591f6a2011-10-25 14:36:40 -0700128 mFadeInAdjacentScreens = false;
Svetoslav Ganov08055f62012-05-15 11:06:36 -0700129
130 // Unless otherwise specified this view is important for accessibility.
131 if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
132 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
133 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700134 setSinglePageInViewport();
Winson Chung785d2eb2011-04-14 16:08:02 -0700135 }
136
137 @Override
138 protected void init() {
139 super.init();
Winson Chung6a877402011-10-26 14:51:44 -0700140 mCenterPagesVertically = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700141
142 Context context = getContext();
143 Resources r = context.getResources();
144 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
145 }
146
Winson Chungc58497e2013-09-03 17:48:37 -0700147 public void onFinishInflate() {
148 super.onFinishInflate();
149
150 LauncherAppState app = LauncherAppState.getInstance();
151 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
152 setPadding(grid.edgeMarginPx, 2 * grid.edgeMarginPx,
153 grid.edgeMarginPx, 2 * grid.edgeMarginPx);
154 }
155
Winson Chung67ca7e42013-10-31 16:53:19 -0700156 void setWidgetsPageIndicatorPadding(int pageIndicatorHeight) {
Adam Cohen4e243a22014-08-10 18:30:55 -0700157 setPadding(getPaddingLeft(), getPaddingTop(), getPaddingRight(), pageIndicatorHeight);
Winson Chung67ca7e42013-10-31 16:53:19 -0700158 }
159
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100160 WidgetPreviewLoader getWidgetPreviewLoader() {
161 if (mWidgetPreviewLoader == null) {
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700162 mWidgetPreviewLoader = LauncherAppState.getInstance().getWidgetCache();
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100163 }
164 return mWidgetPreviewLoader;
165 }
166
Winson Chung5afbf7b2011-07-25 11:53:08 -0700167 /** Returns the item index of the center item on this page so that we can restore to this
168 * item index when we rotate. */
169 private int getMiddleComponentIndexOnCurrentPage() {
170 int i = -1;
171 if (getPageCount() > 0) {
172 int currentPage = getCurrentPage();
Winson Chungb745afb2015-03-02 11:51:23 -0800173 if (mContentType == ContentType.Widgets) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700174 PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(currentPage);
Winson Chung5afbf7b2011-07-25 11:53:08 -0700175 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
176 int childCount = layout.getChildCount();
177 if (childCount > 0) {
Winson Chungb745afb2015-03-02 11:51:23 -0800178 i = (currentPage * numItemsPerPage) + (childCount / 2);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700179 }
Winson Chungc58497e2013-09-03 17:48:37 -0700180 } else {
181 throw new RuntimeException("Invalid ContentType");
Winson Chung5afbf7b2011-07-25 11:53:08 -0700182 }
183 }
184 return i;
185 }
186
187 /** Get the index of the item to restore to if we need to restore the current page. */
188 int getSaveInstanceStateIndex() {
189 if (mSaveInstanceStateItemIndex == -1) {
190 mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage();
191 }
192 return mSaveInstanceStateItemIndex;
193 }
194
195 /** Returns the page in the current orientation which is expected to contain the specified
196 * item index. */
197 int getPageForComponent(int index) {
Adam Cohen0cd3b642011-10-14 14:58:00 -0700198 if (index < 0) return 0;
199
Winson Chungb745afb2015-03-02 11:51:23 -0800200 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
201 return index / numItemsPerPage;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700202 }
203
Winson Chung5afbf7b2011-07-25 11:53:08 -0700204 /** Restores the page for an item at the specified index */
205 void restorePageForIndex(int index) {
206 if (index < 0) return;
Adam Cohen0cd3b642011-10-14 14:58:00 -0700207 mSaveInstanceStateItemIndex = index;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700208 }
209
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700210 private void updatePageCounts() {
211 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
212 (float) (mWidgetCountX * mWidgetCountY));
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700213 }
214
Winson Chungf0ea4d32011-06-06 14:27:16 -0700215 protected void onDataReady(int width, int height) {
Winson Chung4b0ed8c2011-10-19 15:24:49 -0700216 updatePageCounts();
Winson Chung5a808352011-06-27 19:08:49 -0700217
Winson Chungdb1138b2011-06-30 14:39:35 -0700218 // Force a measure to update recalculate the gaps
Winson Chungc58497e2013-09-03 17:48:37 -0700219 mContentWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
220 mContentHeight = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700221
Adam Cohen6c5891a2014-07-09 23:53:15 -0700222 final boolean hostIsTransitioning = getTabHost().isInTransition();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700223 int page = getPageForComponent(mSaveInstanceStateItemIndex);
Michael Jurkae326f182011-11-21 14:05:46 -0800224 invalidatePageData(Math.max(0, page), hostIsTransitioning);
Winson Chung3f4e1422011-11-17 14:58:51 -0800225 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700226
Adam Cohena00673c2014-08-14 12:57:28 -0700227 protected void onLayout(boolean changed, int l, int t, int r, int b) {
228 super.onLayout(changed, l, t, r, b);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700229
Winson Chungf0ea4d32011-06-06 14:27:16 -0700230 if (!isDataReady()) {
Winson Chungb745afb2015-03-02 11:51:23 -0800231 if (!mWidgets.isEmpty()) {
Adam Cohen69ed2002014-08-27 21:27:01 -0700232 post(new Runnable() {
233 // This code triggers requestLayout so must be posted outside of the
234 // layout pass.
235 public void run() {
Sunny Goyalfafca522014-11-03 11:30:01 -0800236 if (Utilities.isViewAttachedToWindow(AppsCustomizePagedView.this)) {
Adam Cohen0d2adfb2014-09-04 01:27:53 +0200237 setDataIsReady();
238 onDataReady(getMeasuredWidth(), getMeasuredHeight());
239 }
Adam Cohen69ed2002014-08-27 21:27:01 -0700240 }
241 });
Winson Chungf0ea4d32011-06-06 14:27:16 -0700242 }
243 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700244 }
245
Michael Jurkac402cd92013-05-20 15:49:32 +0200246 public void onPackagesUpdated(ArrayList<Object> widgetsAndShortcuts) {
Winson Chung892c74d2013-08-22 16:15:50 -0700247 LauncherAppState app = LauncherAppState.getInstance();
248 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
249
Winson Chung1ed747a2011-05-03 16:18:34 -0700250 // Get the list of widgets and shortcuts
251 mWidgets.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200252 for (Object o : widgetsAndShortcuts) {
Adam Cohen59400422014-03-05 18:07:04 -0800253 if (o instanceof LauncherAppWidgetProviderInfo) {
254 LauncherAppWidgetProviderInfo widget = (LauncherAppWidgetProviderInfo) o;
255 if (!app.shouldShowAppOrWidgetProvider(widget.provider) && !widget.isCustomWidget) {
Bjorn Bringert1307f632013-10-03 22:31:03 +0100256 continue;
257 }
Adam Cohen59400422014-03-05 18:07:04 -0800258
259 if (widget.minSpanX > 0 && widget.minSpanY > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200260 // Ensure that all widgets we show can be added on a workspace of this size
261 int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget);
262 int[] minSpanXY = Launcher.getMinSpanForWidget(mLauncher, widget);
263 int minSpanX = Math.min(spanXY[0], minSpanXY[0]);
264 int minSpanY = Math.min(spanXY[1], minSpanXY[1]);
Winson Chung892c74d2013-08-22 16:15:50 -0700265 if (minSpanX <= (int) grid.numColumns &&
266 minSpanY <= (int) grid.numRows) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200267 mWidgets.add(widget);
268 } else {
269 Log.e(TAG, "Widget " + widget.provider + " can not fit on this device (" +
270 widget.minWidth + ", " + widget.minHeight + ")");
271 }
Winson Chungfd39d8e2012-06-05 10:12:48 -0700272 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200273 Log.e(TAG, "Widget " + widget.provider + " has invalid dimensions (" +
274 widget.minWidth + ", " + widget.minHeight + ")");
Winson Chunga5c96362012-04-12 14:04:41 -0700275 }
Michael Jurkadbc1f652011-11-10 17:02:56 -0800276 } else {
Michael Jurkac402cd92013-05-20 15:49:32 +0200277 // just add shortcuts
278 mWidgets.add(o);
Michael Jurkadbc1f652011-11-10 17:02:56 -0800279 }
280 }
Adam Cohen59400422014-03-05 18:07:04 -0800281
Michael Jurkac402cd92013-05-20 15:49:32 +0200282 updatePageCountsAndInvalidateData();
283 }
284
285 public void setBulkBind(boolean bulkBind) {
286 if (bulkBind) {
287 mInBulkBind = true;
288 } else {
289 mInBulkBind = false;
290 if (mNeedToUpdatePageCountsAndInvalidateData) {
291 updatePageCountsAndInvalidateData();
292 }
293 }
294 }
295
296 private void updatePageCountsAndInvalidateData() {
297 if (mInBulkBind) {
298 mNeedToUpdatePageCountsAndInvalidateData = true;
299 } else {
300 updatePageCounts();
301 invalidateOnDataChange();
302 mNeedToUpdatePageCountsAndInvalidateData = false;
303 }
Winson Chung4b576be2011-04-27 17:40:20 -0700304 }
305
306 @Override
307 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700308 // When we have exited all apps or are in transition, disregard clicks
Winson Chungb745afb2015-03-02 11:51:23 -0800309 if (!mLauncher.isWidgetsViewVisible()
Sunny Goyal508da152014-08-14 10:53:27 -0700310 || mLauncher.getWorkspace().isSwitchingState()
311 || !(v instanceof PagedViewWidget)) return;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700312
Sunny Goyal508da152014-08-14 10:53:27 -0700313 // Let the user know that they have to long press to add a widget
314 if (mWidgetInstructionToast != null) {
315 mWidgetInstructionToast.cancel();
Winson Chung4b576be2011-04-27 17:40:20 -0700316 }
Sunny Goyal508da152014-08-14 10:53:27 -0700317 mWidgetInstructionToast = Toast.makeText(getContext(),R.string.long_press_widget_to_add,
318 Toast.LENGTH_SHORT);
319 mWidgetInstructionToast.show();
Winson Chung785d2eb2011-04-14 16:08:02 -0700320 }
321
322 /*
323 * PagedViewWithDraggableItems implementation
324 */
325 @Override
326 protected void determineDraggingStart(android.view.MotionEvent ev) {
Winson Chung4b576be2011-04-27 17:40:20 -0700327 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700328
Sunny Goyalff572272014-07-23 13:58:07 -0700329 static Bundle getDefaultOptionsForWidget(Launcher launcher, PendingAddWidgetInfo info) {
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700330 Bundle options = null;
331 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Sunny Goyalff572272014-07-23 13:58:07 -0700332 AppWidgetResizeFrame.getWidgetSizeRanges(launcher, info.spanX, info.spanY, sTmpRect);
333 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(launcher,
Adam Cohenaaa5c212012-10-05 18:14:31 -0700334 info.componentName, null);
335
Sunny Goyalff572272014-07-23 13:58:07 -0700336 float density = launcher.getResources().getDisplayMetrics().density;
Adam Cohenaaa5c212012-10-05 18:14:31 -0700337 int xPaddingDips = (int) ((padding.left + padding.right) / density);
338 int yPaddingDips = (int) ((padding.top + padding.bottom) / density);
339
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700340 options = new Bundle();
Adam Cohenaaa5c212012-10-05 18:14:31 -0700341 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH,
Sunny Goyalff572272014-07-23 13:58:07 -0700342 sTmpRect.left - xPaddingDips);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700343 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT,
Sunny Goyalff572272014-07-23 13:58:07 -0700344 sTmpRect.top - yPaddingDips);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700345 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH,
Sunny Goyalff572272014-07-23 13:58:07 -0700346 sTmpRect.right - xPaddingDips);
Adam Cohenaaa5c212012-10-05 18:14:31 -0700347 options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT,
Sunny Goyalff572272014-07-23 13:58:07 -0700348 sTmpRect.bottom - yPaddingDips);
Adam Cohen9e05a5e2012-09-10 15:53:09 -0700349 }
350 return options;
351 }
352
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700353 private void preloadWidget(final PendingAddWidgetInfo info) {
Adam Cohen59400422014-03-05 18:07:04 -0800354 final LauncherAppWidgetProviderInfo pInfo = info.info;
355 final Bundle options = pInfo.isCustomWidget ? null :
356 getDefaultOptionsForWidget(mLauncher, info);
Adam Cohendd70d662012-10-04 16:53:44 -0700357
Adam Cohened66b2b2012-01-23 17:28:51 -0800358 if (pInfo.configure != null) {
Adam Cohendd70d662012-10-04 16:53:44 -0700359 info.bindOptions = options;
Adam Cohened66b2b2012-01-23 17:28:51 -0800360 return;
361 }
362
Adam Cohen21a170b2012-05-30 15:17:06 -0700363 mWidgetCleanupState = WIDGET_PRELOAD_PENDING;
Adam Cohened66b2b2012-01-23 17:28:51 -0800364 mBindWidgetRunnable = new Runnable() {
365 @Override
366 public void run() {
Adam Cohen59400422014-03-05 18:07:04 -0800367 if (pInfo.isCustomWidget) {
368 mWidgetCleanupState = WIDGET_BOUND;
369 return;
370 }
371
Adam Cohened66b2b2012-01-23 17:28:51 -0800372 mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -0700373 if(AppWidgetManagerCompat.getInstance(mLauncher).bindAppWidgetIdIfAllowed(
374 mWidgetLoadingId, pInfo, options)) {
375 mWidgetCleanupState = WIDGET_BOUND;
Michael Jurka8b805b12012-04-18 14:23:14 -0700376 }
Adam Cohen59400422014-03-05 18:07:04 -0800377
Adam Cohened66b2b2012-01-23 17:28:51 -0800378 }
379 };
380 post(mBindWidgetRunnable);
381
382 mInflateWidgetRunnable = new Runnable() {
383 @Override
384 public void run() {
Michael Jurka1637d6d2012-08-03 13:35:01 -0700385 if (mWidgetCleanupState != WIDGET_BOUND) {
386 return;
387 }
Adam Cohen59400422014-03-05 18:07:04 -0800388 AppWidgetHostView hostView = mLauncher.getAppWidgetHost().createView(
389 getContext(), mWidgetLoadingId, pInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800390 info.boundWidget = hostView;
391 mWidgetCleanupState = WIDGET_INFLATED;
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800392 hostView.setVisibility(INVISIBLE);
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700393 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(info, false);
Adam Cohen1f362702012-04-04 14:58:12 -0700394
395 // We want the first widget layout to be the correct size. This will be important
396 // for width size reporting to the AppWidgetManager.
397 DragLayer.LayoutParams lp = new DragLayer.LayoutParams(unScaledSize[0],
398 unScaledSize[1]);
399 lp.x = lp.y = 0;
400 lp.customPosition = true;
401 hostView.setLayoutParams(lp);
Adam Cohenef3dd6e2012-02-14 20:54:05 -0800402 mLauncher.getDragLayer().addView(hostView);
Adam Cohened66b2b2012-01-23 17:28:51 -0800403 }
404 };
405 post(mInflateWidgetRunnable);
406 }
407
408 @Override
409 public void onShortPress(View v) {
410 // We are anticipating a long press, and we use this time to load bind and instantiate
411 // the widget. This will need to be cleaned up if it turns out no long press occurs.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700412 if (mCreateWidgetInfo != null) {
413 // Just in case the cleanup process wasn't properly executed. This shouldn't happen.
414 cleanupWidgetPreloading(false);
415 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800416 mCreateWidgetInfo = new PendingAddWidgetInfo((PendingAddWidgetInfo) v.getTag());
Adam Cohenf1dcdf62012-05-10 16:51:52 -0700417 preloadWidget(mCreateWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800418 }
419
Adam Cohen0e56cc92012-05-11 15:57:05 -0700420 private void cleanupWidgetPreloading(boolean widgetWasAdded) {
421 if (!widgetWasAdded) {
422 // If the widget was not added, we may need to do further cleanup.
423 PendingAddWidgetInfo info = mCreateWidgetInfo;
424 mCreateWidgetInfo = null;
Adam Cohen21a170b2012-05-30 15:17:06 -0700425
426 if (mWidgetCleanupState == WIDGET_PRELOAD_PENDING) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700427 // We never did any preloading, so just remove pending callbacks to do so
428 removeCallbacks(mBindWidgetRunnable);
429 removeCallbacks(mInflateWidgetRunnable);
430 } else if (mWidgetCleanupState == WIDGET_BOUND) {
431 // Delete the widget id which was allocated
Adam Cohen59400422014-03-05 18:07:04 -0800432 if (mWidgetLoadingId != -1 && !info.isCustomWidget()) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700433 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
434 }
435
436 // We never got around to inflating the widget, so remove the callback to do so.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700437 removeCallbacks(mInflateWidgetRunnable);
438 } else if (mWidgetCleanupState == WIDGET_INFLATED) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700439 // Delete the widget id which was allocated
Adam Cohen59400422014-03-05 18:07:04 -0800440 if (mWidgetLoadingId != -1 && !info.isCustomWidget()) {
Adam Cohen21a170b2012-05-30 15:17:06 -0700441 mLauncher.getAppWidgetHost().deleteAppWidgetId(mWidgetLoadingId);
442 }
443
Adam Cohen0e56cc92012-05-11 15:57:05 -0700444 // The widget was inflated and added to the DragLayer -- remove it.
445 AppWidgetHostView widget = info.boundWidget;
446 mLauncher.getDragLayer().removeView(widget);
447 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800448 }
449 mWidgetCleanupState = WIDGET_NO_CLEANUP_REQUIRED;
450 mWidgetLoadingId = -1;
Adam Cohen0e56cc92012-05-11 15:57:05 -0700451 mCreateWidgetInfo = null;
452 PagedViewWidget.resetShortPressTarget();
Adam Cohened66b2b2012-01-23 17:28:51 -0800453 }
454
Adam Cohen7a326642012-02-22 12:03:22 -0800455 @Override
456 public void cleanUpShortPress(View v) {
457 if (!mDraggingWidget) {
Adam Cohen0e56cc92012-05-11 15:57:05 -0700458 cleanupWidgetPreloading(false);
Adam Cohen7a326642012-02-22 12:03:22 -0800459 }
460 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800461
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700462 private boolean beginDraggingWidget(PagedViewWidget v) {
Adam Cohen7a326642012-02-22 12:03:22 -0800463 mDraggingWidget = true;
Winson Chung4b576be2011-04-27 17:40:20 -0700464 // Get the widget preview as the drag representation
465 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700466 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700467
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700468 // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
469 // we abort the drag.
470 if (image.getDrawable() == null) {
471 mDraggingWidget = false;
472 return false;
473 }
474
Winson Chung4b576be2011-04-27 17:40:20 -0700475 // Compose the drag image
Winson Chung1120e032011-11-22 16:11:31 -0800476 Bitmap preview;
477 Bitmap outline;
Winson Chung72d59842012-02-22 13:51:36 -0800478 float scale = 1f;
Michael Jurka05713af2013-01-23 12:39:24 +0100479 Point previewPadding = null;
480
Winson Chung1ed747a2011-05-03 16:18:34 -0700481 if (createItemInfo instanceof PendingAddWidgetInfo) {
Adam Cohen92478922012-05-17 13:43:29 -0700482 // This can happen in some weird cases involving multi-touch. We can't start dragging
483 // the widget if this is null, so we break out.
484 if (mCreateWidgetInfo == null) {
485 return false;
486 }
487
Adam Cohen1b36dc32012-02-13 19:27:37 -0800488 PendingAddWidgetInfo createWidgetInfo = mCreateWidgetInfo;
489 createItemInfo = createWidgetInfo;
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700490 int[] size = mLauncher.getWorkspace().estimateItemSize(createWidgetInfo, true);
Winson Chung1ed747a2011-05-03 16:18:34 -0700491
Winson Chung72d59842012-02-22 13:51:36 -0800492 FastBitmapDrawable previewDrawable = (FastBitmapDrawable) image.getDrawable();
493 float minScale = 1.25f;
Sunny Goyal4cad7532015-03-18 15:56:30 -0700494 int maxWidth = Math.min((int) (previewDrawable.getIntrinsicWidth() * minScale), size[0]);
Winson Chung72d59842012-02-22 13:51:36 -0800495
Michael Jurka05713af2013-01-23 12:39:24 +0100496 int[] previewSizeBeforeScale = new int[1];
Sunny Goyalffe83f12014-08-14 17:39:34 -0700497 preview = getWidgetPreviewLoader().generateWidgetPreview(createWidgetInfo.info,
Sunny Goyal4cad7532015-03-18 15:56:30 -0700498 maxWidth, null, previewSizeBeforeScale);
Michael Jurka05713af2013-01-23 12:39:24 +0100499 // Compare the size of the drag preview to the preview in the AppsCustomize tray
500 int previewWidthInAppsCustomize = Math.min(previewSizeBeforeScale[0],
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700501 v.getActualItemWidth());
Michael Jurka05713af2013-01-23 12:39:24 +0100502 scale = previewWidthInAppsCustomize / (float) preview.getWidth();
503
504 // The bitmap in the AppsCustomize tray is always the the same size, so there
505 // might be extra pixels around the preview itself - this accounts for that
506 if (previewWidthInAppsCustomize < previewDrawable.getIntrinsicWidth()) {
507 int padding =
508 (previewDrawable.getIntrinsicWidth() - previewWidthInAppsCustomize) / 2;
509 previewPadding = new Point(padding, 0);
510 }
Winson Chung1ed747a2011-05-03 16:18:34 -0700511 } else {
Michael Jurkadac85912012-05-18 15:04:49 -0700512 PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) v.getTag();
513 Drawable icon = mIconCache.getFullResIcon(createShortcutInfo.shortcutActivityInfo);
Sunny Goyalffe83f12014-08-14 17:39:34 -0700514 preview = Utilities.createIconBitmap(icon, mLauncher);
Winson Chung1ed747a2011-05-03 16:18:34 -0700515 createItemInfo.spanX = createItemInfo.spanY = 1;
516 }
Winson Chung4b576be2011-04-27 17:40:20 -0700517
Michael Jurka8c3339b2012-06-14 16:18:21 -0700518 // Don't clip alpha values for the drag outline if we're using the default widget preview
519 boolean clipAlpha = !(createItemInfo instanceof PendingAddWidgetInfo &&
520 (((PendingAddWidgetInfo) createItemInfo).previewImage == 0));
Peter Ng8db70002011-10-25 15:40:08 -0700521
Winson Chung1120e032011-11-22 16:11:31 -0800522 // Save the preview for the outline generation, then dim the preview
523 outline = Bitmap.createScaledBitmap(preview, preview.getWidth(), preview.getHeight(),
524 false);
Winson Chung1120e032011-11-22 16:11:31 -0800525
Winson Chung4b576be2011-04-27 17:40:20 -0700526 // Start the drag
Winson Chung641d71d2012-04-26 15:58:01 -0700527 mLauncher.lockScreenOrientation();
Michael Jurka8c3339b2012-06-14 16:18:21 -0700528 mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, outline, clipAlpha);
Winson Chung1120e032011-11-22 16:11:31 -0800529 mDragController.startDrag(image, preview, this, createItemInfo,
Michael Jurka05713af2013-01-23 12:39:24 +0100530 DragController.DRAG_ACTION_COPY, previewPadding, scale);
Winson Chung1120e032011-11-22 16:11:31 -0800531 outline.recycle();
532 preview.recycle();
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700533 return true;
Winson Chung4b576be2011-04-27 17:40:20 -0700534 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800535
Winson Chung4b576be2011-04-27 17:40:20 -0700536 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800537 protected boolean beginDragging(final View v) {
Winson Chung4b576be2011-04-27 17:40:20 -0700538 if (!super.beginDragging(v)) return false;
539
Winson Chungb745afb2015-03-02 11:51:23 -0800540 if (v instanceof PagedViewWidget) {
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700541 if (!beginDraggingWidget((PagedViewWidget) v)) {
Adam Cohen88c5d2d2012-05-09 21:34:33 -0700542 return false;
543 }
Winson Chungb745afb2015-03-02 11:51:23 -0800544 } else {
545 Log.e(TAG, "Unexpected dragging view: " + v);
Winson Chung4b576be2011-04-27 17:40:20 -0700546 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800547
548 // We delay entering spring-loaded mode slightly to make sure the UI
549 // thready is free of any work.
550 postDelayed(new Runnable() {
551 @Override
552 public void run() {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800553 // We don't enter spring-loaded mode if the drag has been cancelled
554 if (mLauncher.getDragController().isDragging()) {
Adam Cohen1b36dc32012-02-13 19:27:37 -0800555 // Go into spring loaded mode (must happen before we startDrag())
556 mLauncher.enterSpringLoadedDragMode();
557 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800558 }
Winson Chung72d59842012-02-22 13:51:36 -0800559 }, 150);
Adam Cohened66b2b2012-01-23 17:28:51 -0800560
Winson Chung785d2eb2011-04-14 16:08:02 -0700561 return true;
562 }
Adam Cohen1b36dc32012-02-13 19:27:37 -0800563
Winson Chunga48487a2012-03-20 16:19:37 -0700564 /**
565 * Clean up after dragging.
566 *
567 * @param target where the item was dragged to (can be null if the item was flung)
568 */
569 private void endDragging(View target, boolean isFlingToDelete, boolean success) {
Winson Chunga48487a2012-03-20 16:19:37 -0700570 if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800571 !(target instanceof DeleteDropTarget) && !(target instanceof Folder))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700572 // Exit spring loaded mode if we have not successfully dropped or have not handled the
573 // drop in Workspace
Sunny Goyal8498eb42014-10-16 12:08:41 -0700574 mLauncher.exitSpringLoadedDragModeDelayed(true,
575 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohen689ff162014-05-08 17:27:56 -0700576 mLauncher.unlockScreenOrientation(false);
Adam Cohene97a3b32013-10-23 16:11:50 -0700577 } else {
578 mLauncher.unlockScreenOrientation(false);
Winson Chung557d6ed2011-07-08 15:34:52 -0700579 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700580 }
581
Winson Chung785d2eb2011-04-14 16:08:02 -0700582 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700583 public View getContent() {
Winson Chung7bb37522013-10-28 11:07:57 -0700584 if (getChildCount() > 0) {
585 return getChildAt(0);
586 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700587 return null;
588 }
589
590 @Override
591 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700592 mInTransition = true;
593 if (toWorkspace) {
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700594 cancelAllTasks(false);
Michael Jurka39e5d172012-03-12 18:36:12 -0700595 }
596 }
597
598 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700599 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700600 }
601
602 @Override
603 public void onLauncherTransitionStep(Launcher l, float t) {
604 }
605
606 @Override
607 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
608 mInTransition = false;
Michael Jurka5e368ff2012-05-14 23:13:15 -0700609 mForceDrawAllChildrenNextFrame = !toWorkspace;
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700610 if (!toWorkspace) {
611 loadPreviewsForPage(getNextPage());
612 }
Michael Jurka39e5d172012-03-12 18:36:12 -0700613 }
614
615 @Override
Winson Chunga48487a2012-03-20 16:19:37 -0700616 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
617 boolean success) {
618 // Return early and wait for onFlingToDeleteCompleted if this was the result of a fling
619 if (isFlingToDelete) return;
620
621 endDragging(target, false, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700622
623 // Display an error message if the drag failed due to there not being enough space on the
624 // target layout we were dropping on.
625 if (!success) {
626 boolean showOutOfSpaceMessage = false;
627 if (target instanceof Workspace) {
628 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
629 Workspace workspace = (Workspace) target;
630 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700631 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700632 if (layout != null) {
633 layout.calculateSpans(itemInfo);
634 showOutOfSpaceMessage =
635 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
636 }
637 }
Winson Chungfc79c802011-05-02 13:35:34 -0700638 if (showOutOfSpaceMessage) {
Winson Chung93eef082012-03-23 15:59:27 -0700639 mLauncher.showOutOfSpaceMessage(false);
Winson Chungfc79c802011-05-02 13:35:34 -0700640 }
Adam Cohen7a326642012-02-22 12:03:22 -0800641
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800642 d.deferDragViewCleanupPostAnimation = false;
Winson Chungfc79c802011-05-02 13:35:34 -0700643 }
Adam Cohen0e56cc92012-05-11 15:57:05 -0700644 cleanupWidgetPreloading(success);
Adam Cohen7a326642012-02-22 12:03:22 -0800645 mDraggingWidget = false;
Winson Chung785d2eb2011-04-14 16:08:02 -0700646 }
647
Winson Chunga48487a2012-03-20 16:19:37 -0700648 @Override
649 public void onFlingToDeleteCompleted() {
650 // We just dismiss the drag when we fling, so cleanup here
651 endDragging(null, true, true);
Adam Cohen0e56cc92012-05-11 15:57:05 -0700652 cleanupWidgetPreloading(false);
Winson Chunga48487a2012-03-20 16:19:37 -0700653 mDraggingWidget = false;
654 }
655
656 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800657 public boolean supportsFlingToDelete() {
Winson Chunga48487a2012-03-20 16:19:37 -0700658 return true;
Winson Chung043f2af2012-03-01 16:09:54 -0800659 }
660
Winson Chung7f0acdd2011-09-19 18:34:19 -0700661 @Override
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000662 public boolean supportsAppInfoDropTarget() {
663 return true;
664 }
665
666 @Override
667 public boolean supportsDeleteDropTarget() {
668 return false;
669 }
670
671 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800672 public float getIntrinsicIconScaleFactor() {
673 LauncherAppState app = LauncherAppState.getInstance();
674 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
675 return (float) grid.allAppsIconSizePx / grid.iconSizePx;
676 }
677
678 @Override
Winson Chung7f0acdd2011-09-19 18:34:19 -0700679 protected void onDetachedFromWindow() {
680 super.onDetachedFromWindow();
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700681 cancelAllTasks(true);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700682 }
Winson Chung7f0acdd2011-09-19 18:34:19 -0700683
Adam Cohenc8f4e1b2014-11-19 16:03:20 -0800684 @Override
685 public void trimMemory() {
686 super.trimMemory();
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700687 cancelAllTasks(true);
Adam Cohenc8f4e1b2014-11-19 16:03:20 -0800688 }
689
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700690 private void cancelAllTasks(boolean clearCompletedTasks) {
691 for (int page = getPageCount() - 1; page >= 0; page--) {
692 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
693 if (layout != null) {
694 for (int i = 0; i < layout.getChildCount(); i++) {
695 ((PagedViewWidget) layout.getChildAt(i)).deletePreview(clearCompletedTasks);
696 }
Michael Jurkae326f182011-11-21 14:05:46 -0800697 }
698 }
699 }
700
Winson Chung7f0acdd2011-09-19 18:34:19 -0700701
Winson Chung785d2eb2011-04-14 16:08:02 -0700702 public void setContentType(ContentType type) {
Michael Jurkad9546fc2013-10-23 15:38:48 +0200703 // Widgets appear to be cleared every time you leave, always force invalidate for them
704 if (mContentType != type || type == ContentType.Widgets) {
705 int page = (mContentType != type) ? 0 : getCurrentPage();
706 mContentType = type;
707 invalidatePageData(page, true);
Winson Chung7819a562013-09-19 15:55:45 -0700708 }
Winson Chungc58497e2013-09-03 17:48:37 -0700709 }
710
711 public ContentType getContentType() {
712 return mContentType;
Winson Chungb44b5242011-06-13 11:32:14 -0700713 }
714
Adam Cohen9bfdb762014-07-21 17:44:06 -0700715 public void setPageBackgroundsVisible(boolean visible) {
716 mPageBackgroundsVisible = visible;
717 int childCount = getChildCount();
718 for (int i = 0; i < childCount; ++i) {
719 Drawable bg = getChildAt(i).getBackground();
720 if (bg != null) {
Adam Cohen63f1ec02014-08-12 09:23:13 -0700721 bg.setAlpha(visible ? 255 : 0);
Adam Cohen9bfdb762014-07-21 17:44:06 -0700722 }
723 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700724 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700725
Winson Chung785d2eb2011-04-14 16:08:02 -0700726 /*
727 * Widgets PagedView implementation
728 */
Winson Chung4e6a9762011-05-09 11:56:34 -0700729 private void setupPage(PagedViewGridLayout layout) {
Winson Chung63257c12011-05-05 17:06:13 -0700730 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -0700731 // immediately after syncing, we don't have a proper width.
Winson Chungc58497e2013-09-03 17:48:37 -0700732 int widthSpec = MeasureSpec.makeMeasureSpec(mContentWidth, MeasureSpec.AT_MOST);
733 int heightSpec = MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.AT_MOST);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700734
735 Drawable bg = getContext().getResources().getDrawable(R.drawable.quantum_panel_dark);
736 if (bg != null) {
737 bg.setAlpha(mPageBackgroundsVisible ? 255 : 0);
738 layout.setBackground(bg);
739 }
Winson Chung63257c12011-05-05 17:06:13 -0700740 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -0700741 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700742
Michael Jurka038f9d82011-11-03 13:50:45 -0700743 public void syncWidgetPageItems(final int page, final boolean immediate) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700744 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungb44b5242011-06-13 11:32:14 -0700745
Adam Cohen4e243a22014-08-10 18:30:55 -0700746 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
747
Winson Chungd2945262011-06-24 15:22:14 -0700748 // Calculate the dimensions of each cell we are giving to each widget
Michael Jurka038f9d82011-11-03 13:50:45 -0700749 final ArrayList<Object> items = new ArrayList<Object>();
Adam Cohena00673c2014-08-14 12:57:28 -0700750 int contentWidth = mContentWidth - layout.getPaddingLeft() - layout.getPaddingRight();
Adam Cohen4e243a22014-08-10 18:30:55 -0700751 final int cellWidth = contentWidth / mWidgetCountX;
Adam Cohena00673c2014-08-14 12:57:28 -0700752 int contentHeight = mContentHeight - layout.getPaddingTop() - layout.getPaddingBottom();
753
Adam Cohen4e243a22014-08-10 18:30:55 -0700754 final int cellHeight = contentHeight / mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700755
Winson Chunge4a647f2011-09-30 14:41:25 -0700756 // Prepare the set of widgets to load previews for in the background
Winson Chungc58497e2013-09-03 17:48:37 -0700757 int offset = page * numItemsPerPage;
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700758 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
759 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -0700760 }
761
Winson Chunge4a647f2011-09-30 14:41:25 -0700762 // Prepopulate the pages with the other widget info, and fill in the previews later
Winson Chunge4a647f2011-09-30 14:41:25 -0700763 layout.setColumnCount(layout.getCellCountX());
764 for (int i = 0; i < items.size(); ++i) {
765 Object rawInfo = items.get(i);
766 PendingAddItemInfo createItemInfo = null;
767 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
768 R.layout.apps_customize_widget, layout, false);
Adam Cohen59400422014-03-05 18:07:04 -0800769
770 if (rawInfo instanceof LauncherAppWidgetProviderInfo) {
Winson Chunge4a647f2011-09-30 14:41:25 -0700771 // Fill in the widget information
Adam Cohen59400422014-03-05 18:07:04 -0800772 LauncherAppWidgetProviderInfo info = (LauncherAppWidgetProviderInfo) rawInfo;
773 createItemInfo = new PendingAddWidgetInfo(info, null);
Adam Cohen1f362702012-04-04 14:58:12 -0700774
Adam Cohen59400422014-03-05 18:07:04 -0800775 widget.applyFromAppWidgetProviderInfo(info, -1, getWidgetPreviewLoader());
Winson Chunge4a647f2011-09-30 14:41:25 -0700776 widget.setTag(createItemInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -0800777 widget.setShortPressListener(this);
Winson Chunge4a647f2011-09-30 14:41:25 -0700778 } else if (rawInfo instanceof ResolveInfo) {
779 // Fill in the shortcuts information
780 ResolveInfo info = (ResolveInfo) rawInfo;
Michael Jurkadac85912012-05-18 15:04:49 -0700781 createItemInfo = new PendingAddShortcutInfo(info.activityInfo);
Winson Chunge4a647f2011-09-30 14:41:25 -0700782 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
783 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
784 info.activityInfo.name);
Michael Jurka9c5cc5a2014-01-09 14:59:22 +0100785 widget.applyFromResolveInfo(mPackageManager, info, getWidgetPreviewLoader());
Winson Chunge4a647f2011-09-30 14:41:25 -0700786 widget.setTag(createItemInfo);
787 }
Adam Cohen59400422014-03-05 18:07:04 -0800788
Winson Chunge4a647f2011-09-30 14:41:25 -0700789 widget.setOnClickListener(this);
790 widget.setOnLongClickListener(this);
791 widget.setOnTouchListener(this);
Sunny Goyal290800b2015-03-05 11:33:33 -0800792 widget.setOnKeyListener(mKeyListener);
Winson Chunge4a647f2011-09-30 14:41:25 -0700793
794 // Layout each widget
795 int ix = i % mWidgetCountX;
796 int iy = i / mWidgetCountX;
Adam Cohen4e243a22014-08-10 18:30:55 -0700797
798 if (ix > 0) {
799 View border = widget.findViewById(R.id.left_border);
800 border.setVisibility(View.VISIBLE);
801 }
802 if (ix < mWidgetCountX - 1) {
803 View border = widget.findViewById(R.id.right_border);
804 border.setVisibility(View.VISIBLE);
805 }
806
Winson Chunge4a647f2011-09-30 14:41:25 -0700807 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
Fabrice Di Megliocc11f742012-12-18 16:25:49 -0800808 GridLayout.spec(iy, GridLayout.START),
Winson Chunge4a647f2011-09-30 14:41:25 -0700809 GridLayout.spec(ix, GridLayout.TOP));
810 lp.width = cellWidth;
811 lp.height = cellHeight;
Fabrice Di Megliocc11f742012-12-18 16:25:49 -0800812 lp.setGravity(Gravity.TOP | Gravity.START);
Winson Chunge4a647f2011-09-30 14:41:25 -0700813 layout.addView(widget, lp);
814 }
815
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700816 if (immediate && !mInTransition) {
817 loadPreviewsForPage(page);
Winson Chungf314b0e2011-08-16 11:54:27 -0700818 }
Winson Chungb44b5242011-06-13 11:32:14 -0700819 }
Michael Jurka39e5d172012-03-12 18:36:12 -0700820
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700821 private void loadPreviewsForPage(int page) {
822 final PagedViewGridLayout layout = (PagedViewGridLayout) getPageAt(page);
Winson Chungb44b5242011-06-13 11:32:14 -0700823
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700824 if (layout != null) {
825 for (int i = 0; i < layout.getChildCount(); i++) {
826 ((PagedViewWidget) layout.getChildAt(i)).ensurePreview();
Michael Jurka39e5d172012-03-12 18:36:12 -0700827 }
Winson Chung68e4c642011-11-10 15:48:25 -0800828 }
Winson Chungb44b5242011-06-13 11:32:14 -0700829 }
Winson Chung46af2e82011-05-09 16:00:53 -0700830
Winson Chung785d2eb2011-04-14 16:08:02 -0700831 @Override
832 public void syncPages() {
Winson Chungc58497e2013-09-03 17:48:37 -0700833 disablePagedViewAnimations();
834
Winson Chung785d2eb2011-04-14 16:08:02 -0700835 removeAllViews();
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700836 cancelAllTasks(true);
Winson Chung875de7e2011-06-28 14:25:17 -0700837
Adam Cohen0cd3b642011-10-14 14:58:00 -0700838 Context context = getContext();
Winson Chungb745afb2015-03-02 11:51:23 -0800839 if (mContentType == ContentType.Widgets) {
Winson Chungc58497e2013-09-03 17:48:37 -0700840 for (int j = 0; j < mNumWidgetPages; ++j) {
841 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
842 mWidgetCountY);
843 setupPage(layout);
844 addView(layout, new PagedView.LayoutParams(LayoutParams.MATCH_PARENT,
845 LayoutParams.MATCH_PARENT));
846 }
847 } else {
848 throw new RuntimeException("Invalid ContentType");
Winson Chung875de7e2011-06-28 14:25:17 -0700849 }
850
Winson Chungc58497e2013-09-03 17:48:37 -0700851 enablePagedViewAnimations();
Winson Chung785d2eb2011-04-14 16:08:02 -0700852 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700853
Winson Chung785d2eb2011-04-14 16:08:02 -0700854 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -0700855 public void syncPageItems(int page, boolean immediate) {
Winson Chungc58497e2013-09-03 17:48:37 -0700856 if (mContentType == ContentType.Widgets) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700857 syncWidgetPageItems(page, immediate);
Winson Chungc58497e2013-09-03 17:48:37 -0700858 } else {
Winson Chungb745afb2015-03-02 11:51:23 -0800859 Log.e(TAG, "Unexpected ContentType");
Winson Chung785d2eb2011-04-14 16:08:02 -0700860 }
861 }
862
Adam Cohen22f823d2011-09-01 17:22:18 -0700863 // We want our pages to be z-ordered such that the further a page is to the left, the higher
864 // it is in the z-order. This is important to insure touch events are handled correctly.
865 View getPageAt(int index) {
Michael Jurka39e5d172012-03-12 18:36:12 -0700866 return getChildAt(indexToPage(index));
Adam Cohen22f823d2011-09-01 17:22:18 -0700867 }
868
Adam Cohenae4f1552011-10-20 00:15:42 -0700869 @Override
870 protected int indexToPage(int index) {
871 return getChildCount() - index - 1;
872 }
873
Adam Cohen22f823d2011-09-01 17:22:18 -0700874 // In apps customize, we have a scrolling effect which emulates pulling cards off of a stack.
875 @Override
876 protected void screenScrolled(int screenCenter) {
877 super.screenScrolled(screenCenter);
Michael Jurka47639b92013-01-14 12:42:27 +0100878 enableHwLayersOnVisiblePages();
879 }
880
881 private void enableHwLayersOnVisiblePages() {
882 final int screenCount = getChildCount();
883
884 getVisiblePages(mTempVisiblePagesRange);
885 int leftScreen = mTempVisiblePagesRange[0];
886 int rightScreen = mTempVisiblePagesRange[1];
887 int forceDrawScreen = -1;
888 if (leftScreen == rightScreen) {
889 // make sure we're caching at least two pages always
890 if (rightScreen < screenCount - 1) {
891 rightScreen++;
892 forceDrawScreen = rightScreen;
893 } else if (leftScreen > 0) {
894 leftScreen--;
895 forceDrawScreen = leftScreen;
896 }
897 } else {
898 forceDrawScreen = leftScreen + 1;
899 }
900
901 for (int i = 0; i < screenCount; i++) {
902 final View layout = (View) getPageAt(i);
903 if (!(leftScreen <= i && i <= rightScreen &&
904 (i == forceDrawScreen || shouldDrawChild(layout)))) {
905 layout.setLayerType(LAYER_TYPE_NONE, null);
906 }
907 }
908
Michael Jurka47639b92013-01-14 12:42:27 +0100909 for (int i = 0; i < screenCount; i++) {
910 final View layout = (View) getPageAt(i);
911
912 if (leftScreen <= i && i <= rightScreen &&
913 (i == forceDrawScreen || shouldDrawChild(layout))) {
914 if (layout.getLayerType() != LAYER_TYPE_HARDWARE) {
915 layout.setLayerType(LAYER_TYPE_HARDWARE, null);
916 }
917 }
918 }
Adam Cohen22f823d2011-09-01 17:22:18 -0700919 }
920
921 protected void overScroll(float amount) {
Adam Cohen1e4359c2014-08-18 13:12:16 -0700922 dampedOverScroll(amount);
Adam Cohen22f823d2011-09-01 17:22:18 -0700923 }
924
Winson Chung785d2eb2011-04-14 16:08:02 -0700925 /**
926 * Used by the parent to get the content width to set the tab bar to
927 * @return
928 */
929 public int getPageContentWidth() {
930 return mContentWidth;
931 }
932
Winson Chungb26f3d62011-06-02 10:49:29 -0700933 @Override
Winson Chungb26f3d62011-06-02 10:49:29 -0700934 protected void onPageEndMoving() {
Winson Chungb26f3d62011-06-02 10:49:29 -0700935 super.onPageEndMoving();
Michael Jurka5e368ff2012-05-14 23:13:15 -0700936 mForceDrawAllChildrenNextFrame = true;
Winson Chung5afbf7b2011-07-25 11:53:08 -0700937 // We reset the save index when we change pages so that it will be recalculated on next
938 // rotation
939 mSaveInstanceStateItemIndex = -1;
Winson Chungb26f3d62011-06-02 10:49:29 -0700940 }
941
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700942 @Override
943 protected void onPageBeginMoving() {
944 super.onPageBeginMoving();
945 if (!mInTransition) {
946 getVisiblePages(sTempPosArray);
947 for (int i = sTempPosArray[0]; i <= sTempPosArray[1]; i++) {
948 loadPreviewsForPage(i);
949 }
950 }
951 }
952
Winson Chung785d2eb2011-04-14 16:08:02 -0700953 /*
954 * AllAppsView implementation
955 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700956 public void setup(Launcher launcher, DragController dragController) {
957 mLauncher = launcher;
958 mDragController = dragController;
959 }
Winson Chung9802ac92012-06-08 16:01:58 -0700960
961 /**
Winson Chungb745afb2015-03-02 11:51:23 -0800962 * We should call thise method whenever the core data changes (mWidgets) so that we can
Winson Chung9802ac92012-06-08 16:01:58 -0700963 * appropriately determine when to invalidate the PagedView page data. In cases where the data
964 * has yet to be set, we can requestLayout() and wait for onDataReady() to be called in the
965 * next onMeasure() pass, which will trigger an invalidatePageData() itself.
966 */
967 private void invalidateOnDataChange() {
968 if (!isDataReady()) {
969 // The next layout pass will trigger data-ready if both widgets and apps are set, so
970 // request a layout to trigger the page data when ready.
971 requestLayout();
972 } else {
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700973 cancelAllTasks(false);
Winson Chung9802ac92012-06-08 16:01:58 -0700974 invalidatePageData();
975 }
976 }
977
Winson Chung785d2eb2011-04-14 16:08:02 -0700978 public void reset() {
Winson Chung649668f2012-01-10 13:07:16 -0800979 // If we have reset, then we should not continue to restore the previous state
980 mSaveInstanceStateItemIndex = -1;
981
Winson Chungb745afb2015-03-02 11:51:23 -0800982 if (mContentType != ContentType.Widgets) {
983 setContentType(ContentType.Widgets);
Adam Cohenb64d36e2011-10-17 21:48:02 -0700984 }
Winson Chung649668f2012-01-10 13:07:16 -0800985
Adam Cohenb64d36e2011-10-17 21:48:02 -0700986 if (mCurrentPage != 0) {
987 invalidatePageData(0);
988 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700989 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700990
991 private AppsCustomizeTabHost getTabHost() {
992 return (AppsCustomizeTabHost) mLauncher.findViewById(R.id.apps_customize_pane);
993 }
994
Winson Chung785d2eb2011-04-14 16:08:02 -0700995 public void dumpState() {
996 // TODO: Dump information related to current list of Applications, Widgets, etc.
Adam Cohen0e56cc92012-05-11 15:57:05 -0700997 dumpAppWidgetProviderInfoList(TAG, "mWidgets", mWidgets);
Winson Chung785d2eb2011-04-14 16:08:02 -0700998 }
Adam Cohen4e844012011-11-09 13:48:04 -0800999
Winson Chung785d2eb2011-04-14 16:08:02 -07001000 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001001 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001002 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001003 for (Object i: list) {
1004 if (i instanceof AppWidgetProviderInfo) {
1005 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1006 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1007 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1008 + " initialLayout=" + info.initialLayout
1009 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1010 } else if (i instanceof ResolveInfo) {
1011 ResolveInfo info = (ResolveInfo) i;
1012 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1013 + info.icon);
1014 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001015 }
1016 }
Adam Cohen4e844012011-11-09 13:48:04 -08001017
Winson Chung785d2eb2011-04-14 16:08:02 -07001018 public void surrender() {
1019 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1020 // should stop this now.
Winson Chung4b0ed8c2011-10-19 15:24:49 -07001021
1022 // Stop all background tasks
Sunny Goyal5b0e6692015-03-19 14:31:19 -07001023 cancelAllTasks(true);
Winson Chung785d2eb2011-04-14 16:08:02 -07001024 }
Winson Chung007c6982011-06-14 13:27:53 -07001025
Winson Chungb44b5242011-06-13 11:32:14 -07001026 /*
1027 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1028 * widget previews in the background with the AsyncTasks.
1029 */
Winson Chung68e4c642011-11-10 15:48:25 -08001030 final static int sLookBehindPageCount = 2;
1031 final static int sLookAheadPageCount = 2;
Winson Chungb44b5242011-06-13 11:32:14 -07001032 protected int getAssociatedLowerPageBound(int page) {
Winson Chung68e4c642011-11-10 15:48:25 -08001033 final int count = getChildCount();
1034 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1035 int windowMinIndex = Math.max(Math.min(page - sLookBehindPageCount, count - windowSize), 0);
1036 return windowMinIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001037 }
1038 protected int getAssociatedUpperPageBound(int page) {
1039 final int count = getChildCount();
Winson Chung68e4c642011-11-10 15:48:25 -08001040 int windowSize = Math.min(count, sLookBehindPageCount + sLookAheadPageCount + 1);
1041 int windowMaxIndex = Math.min(Math.max(page + sLookAheadPageCount, windowSize - 1),
1042 count - 1);
1043 return windowMaxIndex;
Winson Chungb44b5242011-06-13 11:32:14 -07001044 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001045
Winson Chung6a0f57d2011-06-29 20:10:49 -07001046 protected String getCurrentPageDescription() {
1047 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1048 int stringId = R.string.default_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001049 int count = 0;
Winson Chungc58497e2013-09-03 17:48:37 -07001050
Winson Chungb745afb2015-03-02 11:51:23 -08001051 if (mContentType == ContentType.Widgets) {
Winson Chung6a0f57d2011-06-29 20:10:49 -07001052 stringId = R.string.apps_customize_widgets_scroll_format;
Adam Cohend3357b12011-10-18 14:58:11 -07001053 count = mNumWidgetPages;
Winson Chungc58497e2013-09-03 17:48:37 -07001054 } else {
1055 throw new RuntimeException("Invalid ContentType");
Winson Chung6a0f57d2011-06-29 20:10:49 -07001056 }
Adam Cohen0cd3b642011-10-14 14:58:00 -07001057
Michael Jurka8b805b12012-04-18 14:23:14 -07001058 return String.format(getContext().getString(stringId), page + 1, count);
Winson Chung6a0f57d2011-06-29 20:10:49 -07001059 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001060}