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