1 /* 2 * Copyright (C) 2018 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 package com.android.launcher3; 17 18 import android.util.Log; 19 import android.view.View; 20 import android.view.ViewGroup; 21 22 import com.android.launcher3.folder.Folder; 23 import com.android.launcher3.folder.FolderIcon; 24 import com.android.launcher3.touch.ItemLongClickListener; 25 26 public interface WorkspaceLayoutManager { 27 28 String TAG = "Launcher.Workspace"; 29 30 // The screen id used for the empty screen always present to the right. 31 int EXTRA_EMPTY_SCREEN_ID = -201; 32 // The is the first screen. It is always present, even if its empty. 33 int FIRST_SCREEN_ID = 0; 34 35 /** 36 * At bind time, we use the rank (screenId) to compute x and y for hotseat items. 37 * See {@link #addInScreen}. 38 */ addInScreenFromBind(View child, ItemInfo info)39 default void addInScreenFromBind(View child, ItemInfo info) { 40 int x = info.cellX; 41 int y = info.cellY; 42 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { 43 int screenId = info.screenId; 44 x = getHotseat().getCellXFromOrder(screenId); 45 y = getHotseat().getCellYFromOrder(screenId); 46 } 47 addInScreen(child, info.container, info.screenId, x, y, info.spanX, info.spanY); 48 } 49 50 /** 51 * Adds the specified child in the specified screen based on the {@param info} 52 * See {@link #addInScreen(View, int, int, int, int, int, int)}. 53 */ addInScreen(View child, ItemInfo info)54 default void addInScreen(View child, ItemInfo info) { 55 addInScreen(child, info.container, info.screenId, info.cellX, info.cellY, 56 info.spanX, info.spanY); 57 } 58 59 /** 60 * Adds the specified child in the specified screen. The position and dimension of 61 * the child are defined by x, y, spanX and spanY. 62 * 63 * @param child The child to add in one of the workspace's screens. 64 * @param screenId The screen in which to add the child. 65 * @param x The X position of the child in the screen's grid. 66 * @param y The Y position of the child in the screen's grid. 67 * @param spanX The number of cells spanned horizontally by the child. 68 * @param spanY The number of cells spanned vertically by the child. 69 */ addInScreen(View child, int container, int screenId, int x, int y, int spanX, int spanY)70 default void addInScreen(View child, int container, int screenId, int x, int y, 71 int spanX, int spanY) { 72 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) { 73 if (getScreenWithId(screenId) == null) { 74 Log.e(TAG, "Skipping child, screenId " + screenId + " not found"); 75 // DEBUGGING - Print out the stack trace to see where we are adding from 76 new Throwable().printStackTrace(); 77 return; 78 } 79 } 80 if (screenId == EXTRA_EMPTY_SCREEN_ID) { 81 // This should never happen 82 throw new RuntimeException("Screen id should not be EXTRA_EMPTY_SCREEN_ID"); 83 } 84 85 final CellLayout layout; 86 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { 87 layout = getHotseat(); 88 89 // Hide folder title in the hotseat 90 if (child instanceof FolderIcon) { 91 ((FolderIcon) child).setTextVisible(false); 92 } 93 } else { 94 // Show folder title if not in the hotseat 95 if (child instanceof FolderIcon) { 96 ((FolderIcon) child).setTextVisible(true); 97 } 98 layout = getScreenWithId(screenId); 99 } 100 101 ViewGroup.LayoutParams genericLp = child.getLayoutParams(); 102 CellLayout.LayoutParams lp; 103 if (genericLp == null || !(genericLp instanceof CellLayout.LayoutParams)) { 104 lp = new CellLayout.LayoutParams(x, y, spanX, spanY); 105 } else { 106 lp = (CellLayout.LayoutParams) genericLp; 107 lp.cellX = x; 108 lp.cellY = y; 109 lp.cellHSpan = spanX; 110 lp.cellVSpan = spanY; 111 } 112 113 if (spanX < 0 && spanY < 0) { 114 lp.isLockedToGrid = false; 115 } 116 117 // Get the canonical child id to uniquely represent this view in this screen 118 ItemInfo info = (ItemInfo) child.getTag(); 119 int childId = info.getViewId(); 120 121 boolean markCellsAsOccupied = !(child instanceof Folder); 122 if (!layout.addViewToCellLayout(child, -1, childId, lp, markCellsAsOccupied)) { 123 // TODO: This branch occurs when the workspace is adding views 124 // outside of the defined grid 125 // maybe we should be deleting these items from the LauncherModel? 126 Log.e(TAG, "Failed to add to item at (" + lp.cellX + "," + lp.cellY + ") to CellLayout"); 127 } 128 129 child.setHapticFeedbackEnabled(false); 130 child.setOnLongClickListener(ItemLongClickListener.INSTANCE_WORKSPACE); 131 if (child instanceof DropTarget) { 132 onAddDropTarget((DropTarget) child); 133 } 134 } 135 getHotseat()136 Hotseat getHotseat(); 137 getScreenWithId(int screenId)138 CellLayout getScreenWithId(int screenId); 139 onAddDropTarget(DropTarget target)140 default void onAddDropTarget(DropTarget target) { } 141 } 142