1 /*
2  * Copyright (C) 2020 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 androidx.window.sidecar;
18 
19 import android.os.IBinder;
20 
21 import androidx.annotation.NonNull;
22 
23 import java.util.HashSet;
24 import java.util.Set;
25 
26 /**
27  * Basic implementation of the {@link SidecarInterface}. An OEM can choose to use it as the base
28  * class for their implementation.
29  */
30 abstract class StubSidecar implements SidecarInterface {
31 
32     private SidecarCallback mSidecarCallback;
33     private final Set<IBinder> mWindowLayoutChangeListenerTokens = new HashSet<>();
34     private boolean mDeviceStateChangeListenerRegistered;
35 
StubSidecar()36     StubSidecar() {
37     }
38 
39     @Override
setSidecarCallback(@onNull SidecarCallback sidecarCallback)40     public void setSidecarCallback(@NonNull SidecarCallback sidecarCallback) {
41         this.mSidecarCallback = sidecarCallback;
42     }
43 
44     @Override
onWindowLayoutChangeListenerAdded(@onNull IBinder iBinder)45     public void onWindowLayoutChangeListenerAdded(@NonNull IBinder iBinder) {
46         this.mWindowLayoutChangeListenerTokens.add(iBinder);
47         this.onListenersChanged();
48     }
49 
50     @Override
onWindowLayoutChangeListenerRemoved(@onNull IBinder iBinder)51     public void onWindowLayoutChangeListenerRemoved(@NonNull IBinder iBinder) {
52         this.mWindowLayoutChangeListenerTokens.remove(iBinder);
53         this.onListenersChanged();
54     }
55 
56     @Override
onDeviceStateListenersChanged(boolean isEmpty)57     public void onDeviceStateListenersChanged(boolean isEmpty) {
58         this.mDeviceStateChangeListenerRegistered = !isEmpty;
59         this.onListenersChanged();
60     }
61 
updateDeviceState(SidecarDeviceState newState)62     void updateDeviceState(SidecarDeviceState newState) {
63         if (this.mSidecarCallback != null) {
64             mSidecarCallback.onDeviceStateChanged(newState);
65         }
66     }
67 
updateWindowLayout(@onNull IBinder windowToken, @NonNull SidecarWindowLayoutInfo newLayout)68     void updateWindowLayout(@NonNull IBinder windowToken,
69             @NonNull SidecarWindowLayoutInfo newLayout) {
70         if (this.mSidecarCallback != null) {
71             mSidecarCallback.onWindowLayoutChanged(windowToken, newLayout);
72         }
73     }
74 
75     @NonNull
getWindowsListeningForLayoutChanges()76     Set<IBinder> getWindowsListeningForLayoutChanges() {
77         return mWindowLayoutChangeListenerTokens;
78     }
79 
hasListeners()80     protected boolean hasListeners() {
81         return !mWindowLayoutChangeListenerTokens.isEmpty() || mDeviceStateChangeListenerRegistered;
82     }
83 
onListenersChanged()84     protected abstract void onListenersChanged();
85 }
86