1 /*
2  * Copyright (C) 2023 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.splitscreen
18 
19 import android.content.Context
20 import android.content.Intent
21 import android.graphics.Bitmap
22 import android.graphics.drawable.BitmapDrawable
23 import android.util.Log
24 import android.view.View
25 import com.android.launcher3.model.data.ItemInfo
26 import com.android.launcher3.model.data.WorkspaceItemInfo
27 import com.android.launcher3.popup.QuickstepSystemShortcut
28 import com.android.launcher3.popup.SystemShortcut
29 import com.android.launcher3.util.SplitConfigurationOptions
30 import com.android.launcher3.util.SplitConfigurationOptions.SplitPositionOption
31 import com.android.launcher3.util.SplitConfigurationOptions.SplitSelectSource
32 import com.android.launcher3.views.ActivityContext
33 
34 /**
35  * Shortcut to allow starting split. Default interaction for [onClick] is to launch split selection
36  * mode
37  */
38 abstract class SplitShortcut<T>(
39     iconResId: Int,
40     labelResId: Int,
41     target: T,
42     itemInfo: ItemInfo?,
43     originalView: View?,
44     protected val position: SplitPositionOption
45 ) : SystemShortcut<T>(iconResId, labelResId, target, itemInfo, originalView) where
46 T : Context?,
47 T : ActivityContext? {
48     private val TAG = "SplitShortcut"
49 
50     // Initiate splitscreen from the Home screen or Home All Apps
51     protected val splitSelectSource: SplitSelectSource?
52         get() {
53             // Initiate splitscreen from the Home screen or Home All Apps
54             val bitmap: Bitmap
55             val intent: Intent
56             when (mItemInfo) {
57                 is WorkspaceItemInfo -> {
58                     val workspaceItemInfo = mItemInfo
59                     bitmap = workspaceItemInfo.bitmap.icon
60                     intent = workspaceItemInfo.intent
61                 }
62                 is com.android.launcher3.model.data.AppInfo -> {
63                     val appInfo = mItemInfo
64                     bitmap = appInfo.bitmap.icon
65                     intent = appInfo.intent
66                 }
67                 else -> {
68                     Log.e(TAG, "unknown item type")
69                     return null
70                 }
71             }
72             val splitEvent =
73                 SplitConfigurationOptions.getLogEventForPosition(position.stagePosition)
74             return SplitSelectSource(
75                 mOriginalView,
76                 BitmapDrawable(bitmap),
77                 intent,
78                 position,
79                 mItemInfo,
80                 splitEvent
81             )
82         }
83 
84     /** Starts split selection on the provided [mTarget] */
onClicknull85     override fun onClick(view: View?) {
86         val splitSelectSource = splitSelectSource
87         if (splitSelectSource == null) {
88             Log.w(QuickstepSystemShortcut.TAG, "no split selection source")
89             return
90         }
91         mTarget!!.startSplitSelection(splitSelectSource)
92     }
93 }
94