1 /* 2 * Copyright (C) 2025 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 17 package com.android.launcher3.taskbar 18 19 import android.content.Context 20 import android.util.SparseArray 21 import android.view.View 22 import com.android.launcher3.DeviceProfile 23 import com.android.launcher3.LauncherAppState 24 import com.android.launcher3.LauncherSettings.Favorites.CONTAINER_HOTSEAT 25 import com.android.launcher3.R 26 import com.android.launcher3.model.BgDataModel 27 import com.android.launcher3.model.data.ItemInfo 28 import com.android.launcher3.model.data.WorkspaceItemInfo 29 import com.android.launcher3.popup.SystemShortcut 30 import com.android.launcher3.views.ActivityContext 31 32 /** 33 * A single menu item shortcut to allow users to pin an item to the taskbar and unpin an item from 34 * the taskbar. 35 */ 36 class PinToTaskbarShortcut<T>( 37 target: T, 38 itemInfo: ItemInfo?, 39 originalView: View, 40 private val mIsPin: Boolean, 41 private val mPinnedInfoList: SparseArray<ItemInfo?>, 42 ) : 43 SystemShortcut<T>( 44 if (mIsPin) R.drawable.ic_pin else R.drawable.ic_unpin, 45 if (mIsPin) R.string.pin_to_taskbar else R.string.unpin_from_taskbar, 46 target, 47 itemInfo, 48 originalView, 49 ) where T : Context?, T : ActivityContext? { 50 onClicknull51 override fun onClick(v: View?) { 52 dismissTaskMenuView() 53 // Create a placeholder callbacks for the writer to notify other launcher model callbacks 54 // after update. 55 val callbacks: BgDataModel.Callbacks = object : BgDataModel.Callbacks {} 56 57 val writer = 58 LauncherAppState.getInstance(mOriginalView.context) 59 .model 60 .getWriter(true, mTarget!!.cellPosMapper, callbacks) 61 62 if (!mIsPin) { 63 writer.deleteItemFromDatabase(mItemInfo, "item unpinned through long-press menu") 64 return 65 } 66 67 val newInfo = 68 if (mItemInfo is com.android.launcher3.model.data.AppInfo) { 69 mItemInfo.makeWorkspaceItem(mOriginalView.context) 70 } else if (mItemInfo is WorkspaceItemInfo) { 71 mItemInfo.clone() 72 } else { 73 return 74 } 75 76 val dp: DeviceProfile = mTarget.deviceProfile 77 var targetIdx = -1 78 79 for (i in 0 until dp.numShownHotseatIcons) { 80 if (mPinnedInfoList[i] == null) { 81 targetIdx = i 82 break 83 } 84 } 85 86 val cellX = if (dp.isVerticalBarLayout()) 0 else targetIdx 87 val cellY = if (dp.isVerticalBarLayout()) (dp.numShownHotseatIcons - (targetIdx + 1)) else 0 88 89 writer.addItemToDatabase(newInfo, CONTAINER_HOTSEAT, mItemInfo.screenId, cellX, cellY) 90 } 91 } 92