1 /*
2  * Copyright (C) 2024 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.wm.shell.common.pip;
18 
19 import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
20 import static android.view.WindowManager.LayoutParams.FLAG_SLIPPERY;
21 import static android.view.WindowManager.LayoutParams.FLAG_SPLIT_TOUCH;
22 import static android.view.WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
23 import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_TRUSTED_OVERLAY;
24 import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
25 
26 import android.annotation.Nullable;
27 import android.app.ActivityManager.RunningTaskInfo;
28 import android.app.RemoteAction;
29 import android.content.Context;
30 import android.graphics.PixelFormat;
31 import android.graphics.Rect;
32 import android.view.SurfaceControl;
33 import android.view.WindowManager;
34 
35 import com.android.wm.shell.R;
36 import com.android.wm.shell.ShellTaskOrganizer;
37 
38 import java.util.List;
39 
40 /**
41  *  Interface to interact with PiP menu when certain events happen
42  *  (task appear/vanish, PiP move, etc.).
43  */
44 public interface PipMenuController {
45 
46     String MENU_WINDOW_TITLE = "PipMenuView";
47 
48     /**
49      * Used with
50      * {@link PipMenuController#movePipMenu(SurfaceControl, SurfaceControl.Transaction, Rect,
51      * float)} to indicate that we don't want to affect the alpha value of the menu surfaces.
52      */
53     float ALPHA_NO_CHANGE = -1f;
54 
55     /**
56      * Called when out implementation of
57      * {@link ShellTaskOrganizer.TaskListener#onTaskAppeared(RunningTaskInfo, SurfaceControl)}
58      * is called.
59      */
attach(SurfaceControl leash)60     void attach(SurfaceControl leash);
61 
62     /**
63      * Called when our implementation of
64      * {@link ShellTaskOrganizer.TaskListener#onTaskVanished(RunningTaskInfo)} is called.
65      */
detach()66     void detach();
67 
68     /**
69      * Check if menu is visible or not.
70      */
isMenuVisible()71     boolean isMenuVisible();
72 
73     /**
74      * Show the PIP menu.
75      */
showMenu()76     void showMenu();
77 
78     /**
79      * Given a set of actions, update the menu.
80      */
setAppActions(List<RemoteAction> appActions, RemoteAction closeAction)81     void setAppActions(List<RemoteAction> appActions, RemoteAction closeAction);
82 
83     /**
84      * Resize the PiP menu with the given bounds. The PiP SurfaceControl is given if there is a
85      * need to synchronize the movements on the same frame as PiP.
86      */
resizePipMenu(@ullable SurfaceControl pipLeash, @Nullable SurfaceControl.Transaction t, Rect destinationBounds)87     default void resizePipMenu(@Nullable SurfaceControl pipLeash,
88             @Nullable SurfaceControl.Transaction t,
89             Rect destinationBounds) {}
90 
91     /**
92      * Move the PiP menu with the given bounds. The PiP SurfaceControl is given if there is a
93      * need to synchronize the movements on the same frame as PiP.
94      */
movePipMenu(@ullable SurfaceControl pipLeash, @Nullable SurfaceControl.Transaction t, Rect destinationBounds, float alpha)95     default void movePipMenu(@Nullable SurfaceControl pipLeash,
96             @Nullable SurfaceControl.Transaction t, Rect destinationBounds, float alpha) {
97     }
98 
99     /**
100      * Update the PiP menu with the given bounds for re-layout purposes.
101      */
updateMenuBounds(Rect destinationBounds)102     default void updateMenuBounds(Rect destinationBounds) {}
103 
104     /**
105      * Returns a default LayoutParams for the PIP Menu.
106      * @param context the context.
107      * @param width the PIP stack width.
108      * @param height the PIP stack height.
109      */
getPipMenuLayoutParams(Context context, String title, int width, int height)110     default WindowManager.LayoutParams getPipMenuLayoutParams(Context context, String title,
111             int width, int height) {
112         final WindowManager.LayoutParams lp = new WindowManager.LayoutParams(width, height,
113                 TYPE_APPLICATION_OVERLAY,
114                 FLAG_WATCH_OUTSIDE_TOUCH | FLAG_SPLIT_TOUCH | FLAG_SLIPPERY | FLAG_NOT_TOUCHABLE,
115                 PixelFormat.TRANSLUCENT);
116         lp.privateFlags |= PRIVATE_FLAG_TRUSTED_OVERLAY;
117         lp.setTitle(title);
118         lp.accessibilityTitle = context.getResources().getString(
119                 R.string.pip_menu_accessibility_title);
120         return lp;
121     }
122 }
123