• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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             int screenId = presenterPos.screenId;
59             x = getHotseat().getCellXFromOrder(screenId);
60             y = getHotseat().getCellYFromOrder(screenId);
61         }
62         addInScreen(child, info.container, presenterPos.screenId, x, y, info.spanX, info.spanY);
63     }
64 
65     /**
66      * Adds the specified child in the specified screen based on the {@param info}
67      * See {@link #addInScreen(View, int, int, int, int, int, int)}.
68      */
addInScreen(View child, ItemInfo info)69     default void addInScreen(View child, ItemInfo info) {
70         CellPos presenterPos = getCellPosMapper().mapModelToPresenter(info);
71         addInScreen(child, info.container,
72                 presenterPos.screenId, presenterPos.cellX, presenterPos.cellY,
73                 info.spanX, info.spanY);
74     }
75 
76     /**
77      * Adds the specified child in the specified screen. The position and dimension of
78      * the child are defined by x, y, spanX and spanY.
79      *
80      * @param child The child to add in one of the workspace's screens.
81      * @param screenId The screen in which to add the child.
82      * @param x The X position of the child in the screen's grid.
83      * @param y The Y position of the child in the screen's grid.
84      * @param spanX The number of cells spanned horizontally by the child.
85      * @param spanY The number of cells spanned vertically by the child.
86      */
addInScreen(View child, int container, int screenId, int x, int y, int spanX, int spanY)87     default void addInScreen(View child, int container, int screenId, int x, int y,
88             int spanX, int spanY) {
89         if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
90             if (getScreenWithId(screenId) == null) {
91                 Log.e(TAG, "Skipping child, screenId " + screenId + " not found");
92                 // DEBUGGING - Print out the stack trace to see where we are adding from
93                 new Throwable().printStackTrace();
94                 return;
95             }
96         }
97         if (EXTRA_EMPTY_SCREEN_IDS.contains(screenId)) {
98             // This should never happen
99             throw new RuntimeException("Screen id should not be extra empty screen: " + screenId);
100         }
101 
102         final CellLayout layout;
103         if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT
104                 || container == LauncherSettings.Favorites.CONTAINER_HOTSEAT_PREDICTION) {
105             layout = getHotseat();
106 
107             // Hide folder title in the hotseat
108             if (child instanceof FolderIcon) {
109                 ((FolderIcon) child).setTextVisible(false);
110             }
111         } else {
112             // Show folder title if not in the hotseat
113             if (child instanceof FolderIcon) {
114                 ((FolderIcon) child).setTextVisible(true);
115             }
116             layout = getScreenWithId(screenId);
117         }
118 
119         ViewGroup.LayoutParams genericLp = child.getLayoutParams();
120         CellLayoutLayoutParams lp;
121         if (genericLp == null || !(genericLp instanceof CellLayoutLayoutParams)) {
122             lp = new CellLayoutLayoutParams(x, y, spanX, spanY);
123         } else {
124             lp = (CellLayoutLayoutParams) genericLp;
125             lp.setCellX(x);
126             lp.setCellY(y);
127             lp.cellHSpan = spanX;
128             lp.cellVSpan = spanY;
129         }
130 
131         if (spanX < 0 && spanY < 0) {
132             lp.isLockedToGrid = false;
133         }
134 
135         // Get the canonical child id to uniquely represent this view in this screen
136         ItemInfo info = (ItemInfo) child.getTag();
137         int childId = info.getViewId();
138 
139         boolean markCellsAsOccupied = !(child instanceof Folder);
140         if (!layout.addViewToCellLayout(child, -1, childId, lp, markCellsAsOccupied)) {
141             // TODO: This branch occurs when the workspace is adding views
142             // outside of the defined grid
143             // maybe we should be deleting these items from the LauncherModel?
144             Log.e(TAG, "Failed to add to item at (" + lp.getCellX() + "," + lp.getCellY()
145                     + ") to CellLayout");
146         }
147 
148         child.setHapticFeedbackEnabled(false);
149         child.setOnLongClickListener(getWorkspaceChildOnLongClickListener());
150         if (child instanceof DropTarget) {
151             onAddDropTarget((DropTarget) child);
152         }
153     }
154 
getWorkspaceChildOnLongClickListener()155     default View.OnLongClickListener getWorkspaceChildOnLongClickListener() {
156         return ItemLongClickListener.INSTANCE_WORKSPACE;
157     }
158 
159     /**
160      * Returns the mapper for converting between model and presenter
161      */
getCellPosMapper()162     CellPosMapper getCellPosMapper();
163 
getHotseat()164     Hotseat getHotseat();
165 
getScreenWithId(int screenId)166     CellLayout getScreenWithId(int screenId);
167 
onAddDropTarget(DropTarget target)168     default void onAddDropTarget(DropTarget target) { }
169 }
170