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.celllayout.CellLayoutLayoutParams; 23 import com.android.launcher3.celllayout.CellPosMapper; 24 import com.android.launcher3.celllayout.CellPosMapper.CellPos; 25 import com.android.launcher3.folder.Folder; 26 import com.android.launcher3.folder.FolderIcon; 27 import com.android.launcher3.model.data.ItemInfo; 28 import com.android.launcher3.touch.ItemLongClickListener; 29 import com.android.launcher3.util.IntSet; 30 31 public interface WorkspaceLayoutManager { 32 33 String TAG = "Launcher.Workspace"; 34 35 // The screen id used for the empty screen always present at the end. 36 int EXTRA_EMPTY_SCREEN_ID = -201; 37 // The screen id used for the second empty screen always present at the end for two panel home. 38 int EXTRA_EMPTY_SCREEN_SECOND_ID = -200; 39 // The screen ids used for the empty screens at the end of the workspaces. 40 IntSet EXTRA_EMPTY_SCREEN_IDS = 41 IntSet.wrap(EXTRA_EMPTY_SCREEN_ID, EXTRA_EMPTY_SCREEN_SECOND_ID); 42 43 // The is the first screen. It is always present, even if its empty. 44 int FIRST_SCREEN_ID = 0; 45 // This is the second page. On two panel home it is always present, even if its empty. 46 int SECOND_SCREEN_ID = 1; 47 48 /** 49 * At bind time, we use the rank (screenId) to compute x and y for hotseat items. 50 * See {@link #addInScreen}. 51 */ addInScreenFromBind(View child, ItemInfo info)52 default void addInScreenFromBind(View child, ItemInfo info) { 53 CellPos presenterPos = getCellPosMapper().mapModelToPresenter(info); 54 int x = presenterPos.cellX; 55 int y = presenterPos.cellY; 56 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT 57 || info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT_PREDICTION) { 58 Log.d(TAG, "add predicted icon " + child.getTag().toString() + " to home screen"); 59 int screenId = presenterPos.screenId; 60 x = getHotseat().getCellXFromOrder(screenId); 61 y = getHotseat().getCellYFromOrder(screenId); 62 } 63 addInScreen(child, info.container, presenterPos.screenId, x, y, info.spanX, info.spanY); 64 } 65 66 /** 67 * Adds the specified child in the specified screen based on the {@param info} 68 * See {@link #addInScreen(View, int, int, int, int, int, int)}. 69 */ addInScreen(View child, ItemInfo info)70 default void addInScreen(View child, ItemInfo info) { 71 CellPos presenterPos = getCellPosMapper().mapModelToPresenter(info); 72 addInScreen(child, info.container, 73 presenterPos.screenId, presenterPos.cellX, presenterPos.cellY, 74 info.spanX, info.spanY); 75 } 76 77 /** 78 * Adds the specified child in the specified screen. The position and dimension of 79 * the child are defined by x, y, spanX and spanY. 80 * 81 * @param child The child to add in one of the workspace's screens. 82 * @param screenId The screen in which to add the child. 83 * @param x The X position of the child in the screen's grid. 84 * @param y The Y position of the child in the screen's grid. 85 * @param spanX The number of cells spanned horizontally by the child. 86 * @param spanY The number of cells spanned vertically by the child. 87 */ addInScreen(View child, int container, int screenId, int x, int y, int spanX, int spanY)88 default void addInScreen(View child, int container, int screenId, int x, int y, 89 int spanX, int spanY) { 90 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) { 91 if (getScreenWithId(screenId) == null) { 92 Log.e(TAG, "Skipping child, screenId " + screenId + " not found"); 93 // DEBUGGING - Print out the stack trace to see where we are adding from 94 new Throwable().printStackTrace(); 95 return; 96 } 97 } 98 if (EXTRA_EMPTY_SCREEN_IDS.contains(screenId)) { 99 // This should never happen 100 throw new RuntimeException("Screen id should not be extra empty screen: " + screenId); 101 } 102 103 final CellLayout layout; 104 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT 105 || container == LauncherSettings.Favorites.CONTAINER_HOTSEAT_PREDICTION) { 106 layout = getHotseat(); 107 108 // Hide folder title in the hotseat 109 if (child instanceof FolderIcon) { 110 ((FolderIcon) child).setTextVisible(false); 111 } 112 } else { 113 // Show folder title if not in the hotseat 114 if (child instanceof FolderIcon) { 115 ((FolderIcon) child).setTextVisible(true); 116 } 117 layout = getScreenWithId(screenId); 118 } 119 120 ViewGroup.LayoutParams genericLp = child.getLayoutParams(); 121 CellLayoutLayoutParams lp; 122 if (genericLp == null || !(genericLp instanceof CellLayoutLayoutParams)) { 123 lp = new CellLayoutLayoutParams(x, y, spanX, spanY); 124 } else { 125 lp = (CellLayoutLayoutParams) genericLp; 126 lp.setCellX(x); 127 lp.setCellY(y); 128 lp.cellHSpan = spanX; 129 lp.cellVSpan = spanY; 130 } 131 132 if (spanX < 0 && spanY < 0) { 133 lp.isLockedToGrid = false; 134 } 135 136 // Get the canonical child id to uniquely represent this view in this screen 137 ItemInfo info = (ItemInfo) child.getTag(); 138 int childId = info.getViewId(); 139 140 boolean markCellsAsOccupied = !(child instanceof Folder); 141 if (!layout.addViewToCellLayout(child, -1, childId, lp, markCellsAsOccupied)) { 142 // TODO: This branch occurs when the workspace is adding views 143 // outside of the defined grid 144 // maybe we should be deleting these items from the LauncherModel? 145 Log.e(TAG, "Failed to add to item at (" + lp.getCellX() + "," + lp.getCellY() 146 + ") to CellLayout"); 147 } 148 149 child.setHapticFeedbackEnabled(false); 150 child.setOnLongClickListener(getWorkspaceChildOnLongClickListener()); 151 if (child instanceof DropTarget) { 152 onAddDropTarget((DropTarget) child); 153 } 154 } 155 getWorkspaceChildOnLongClickListener()156 default View.OnLongClickListener getWorkspaceChildOnLongClickListener() { 157 return ItemLongClickListener.INSTANCE_WORKSPACE; 158 } 159 160 /** 161 * Returns the mapper for converting between model and presenter 162 */ getCellPosMapper()163 CellPosMapper getCellPosMapper(); 164 getHotseat()165 Hotseat getHotseat(); 166 getScreenWithId(int screenId)167 CellLayout getScreenWithId(int screenId); 168 onAddDropTarget(DropTarget target)169 default void onAddDropTarget(DropTarget target) { } 170 } 171