1 /*
2  * Copyright (C) 2022 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.pip;
18 
19 import android.app.RemoteAction;
20 
21 import java.util.ArrayList;
22 import java.util.List;
23 
24 /**
25  * Forwards changes to the Picture-in-Picture params to all listeners.
26  */
27 public class PipParamsChangedForwarder {
28 
29     private final List<PipParamsChangedCallback>
30             mPipParamsChangedListeners = new ArrayList<>();
31 
32     /**
33      * Add a listener that implements at least one of the callbacks.
34      */
addListener(PipParamsChangedCallback listener)35     public void addListener(PipParamsChangedCallback listener) {
36         if (mPipParamsChangedListeners.contains(listener)) {
37             return;
38         }
39         mPipParamsChangedListeners.add(listener);
40     }
41 
42     /**
43      * Call to notify all listeners of the changed aspect ratio.
44      */
notifyAspectRatioChanged(float aspectRatio)45     public void notifyAspectRatioChanged(float aspectRatio) {
46         for (PipParamsChangedCallback listener : mPipParamsChangedListeners) {
47             listener.onAspectRatioChanged(aspectRatio);
48         }
49     }
50 
51     /**
52      * Call to notify all listeners of the changed expanded aspect ratio.
53      */
notifyExpandedAspectRatioChanged(float aspectRatio)54     public void notifyExpandedAspectRatioChanged(float aspectRatio) {
55         for (PipParamsChangedCallback listener : mPipParamsChangedListeners) {
56             listener.onExpandedAspectRatioChanged(aspectRatio);
57         }
58     }
59 
60     /**
61      * Call to notify all listeners of the changed title.
62      */
notifyTitleChanged(CharSequence title)63     public void notifyTitleChanged(CharSequence title) {
64         String value = title == null ? null : title.toString();
65         for (PipParamsChangedCallback listener : mPipParamsChangedListeners) {
66             listener.onTitleChanged(value);
67         }
68     }
69 
70     /**
71      * Call to notify all listeners of the changed subtitle.
72      */
notifySubtitleChanged(CharSequence subtitle)73     public void notifySubtitleChanged(CharSequence subtitle) {
74         String value = subtitle == null ? null : subtitle.toString();
75         for (PipParamsChangedCallback listener : mPipParamsChangedListeners) {
76             listener.onSubtitleChanged(value);
77         }
78     }
79 
80     /**
81      * Call to notify all listeners of the changed app actions or close action.
82      */
notifyActionsChanged(List<RemoteAction> actions, RemoteAction closeAction)83     public void notifyActionsChanged(List<RemoteAction> actions, RemoteAction closeAction) {
84         for (PipParamsChangedCallback listener : mPipParamsChangedListeners) {
85             listener.onActionsChanged(actions, closeAction);
86         }
87     }
88 
89     /**
90      * Contains callbacks for PiP params changes. Subclasses can choose which changes they want to
91      * listen to by only overriding those selectively.
92      */
93     public interface PipParamsChangedCallback {
94 
95         /**
96          * Called if aspect ratio changed.
97          */
onAspectRatioChanged(float aspectRatio)98         default void onAspectRatioChanged(float aspectRatio) {
99         }
100 
101         /**
102          * Called if expanded aspect ratio changed.
103          */
onExpandedAspectRatioChanged(float aspectRatio)104         default void onExpandedAspectRatioChanged(float aspectRatio) {
105         }
106 
107         /**
108          * Called if either the actions or the close action changed.
109          */
onActionsChanged(List<RemoteAction> actions, RemoteAction closeAction)110         default void onActionsChanged(List<RemoteAction> actions, RemoteAction closeAction) {
111         }
112 
113         /**
114          * Called if the title changed.
115          */
onTitleChanged(String title)116         default void onTitleChanged(String title) {
117         }
118 
119         /**
120          * Called if the subtitle changed.
121          */
onSubtitleChanged(String subtitle)122         default void onSubtitleChanged(String subtitle) {
123         }
124     }
125 }
126