1 /* 2 * Copyright (C) 2021 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.taskbar; 17 18 import android.view.View; 19 20 import com.android.launcher3.BaseQuickstepLauncher; 21 import com.android.launcher3.CellLayout; 22 import com.android.launcher3.DropTarget; 23 import com.android.launcher3.Hotseat; 24 import com.android.launcher3.ShortcutAndWidgetContainer; 25 import com.android.launcher3.dragndrop.DragController; 26 import com.android.launcher3.dragndrop.DragOptions; 27 import com.android.launcher3.model.data.ItemInfo; 28 29 import java.util.function.Consumer; 30 31 /** 32 * Works with TaskbarController to update the TaskbarView's Hotseat items. 33 */ 34 public class TaskbarHotseatController { 35 36 private final BaseQuickstepLauncher mLauncher; 37 private final Hotseat mHotseat; 38 private final Consumer<ItemInfo[]> mTaskbarCallbacks; 39 private final int mNumHotseatIcons; 40 41 private final DragController.DragListener mDragListener = new DragController.DragListener() { 42 @Override 43 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) { } 44 45 @Override 46 public void onDragEnd() { 47 onHotseatUpdated(); 48 } 49 }; 50 TaskbarHotseatController( BaseQuickstepLauncher launcher, Consumer<ItemInfo[]> taskbarCallbacks)51 public TaskbarHotseatController( 52 BaseQuickstepLauncher launcher, Consumer<ItemInfo[]> taskbarCallbacks) { 53 mLauncher = launcher; 54 mHotseat = mLauncher.getHotseat(); 55 mTaskbarCallbacks = taskbarCallbacks; 56 mNumHotseatIcons = mLauncher.getDeviceProfile().numShownHotseatIcons; 57 } 58 init()59 protected void init() { 60 mLauncher.getDragController().addDragListener(mDragListener); 61 onHotseatUpdated(); 62 } 63 cleanup()64 protected void cleanup() { 65 mLauncher.getDragController().removeDragListener(mDragListener); 66 } 67 68 /** 69 * Called when any Hotseat item changes, and reports the new list of items to TaskbarController. 70 */ onHotseatUpdated()71 protected void onHotseatUpdated() { 72 ShortcutAndWidgetContainer shortcutsAndWidgets = mHotseat.getShortcutsAndWidgets(); 73 ItemInfo[] hotseatItemInfos = new ItemInfo[mNumHotseatIcons]; 74 for (int i = 0; i < shortcutsAndWidgets.getChildCount(); i++) { 75 View child = shortcutsAndWidgets.getChildAt(i); 76 Object tag = shortcutsAndWidgets.getChildAt(i).getTag(); 77 if (tag instanceof ItemInfo) { 78 ItemInfo itemInfo = (ItemInfo) tag; 79 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); 80 // Since the hotseat might be laid out vertically or horizontally, use whichever 81 // index is higher. 82 int index = Math.max(lp.cellX, lp.cellY); 83 if (0 <= index && index < hotseatItemInfos.length) { 84 hotseatItemInfos[index] = itemInfo; 85 } 86 } 87 } 88 89 mTaskbarCallbacks.accept(hotseatItemInfos); 90 } 91 } 92