1 /* 2 * Copyright (C) 2021 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.extensions; 18 19 import android.app.Activity; 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 ExtensionInterface}. An OEM can choose to use it as the base 28 * class for their implementation. 29 */ 30 abstract class StubExtension implements ExtensionInterface { 31 32 private ExtensionCallback mExtensionCallback; 33 private final Set<Activity> mWindowLayoutChangeListenerActivities = new HashSet<>(); 34 StubExtension()35 StubExtension() { 36 } 37 38 @Override setExtensionCallback(@onNull ExtensionCallback extensionCallback)39 public void setExtensionCallback(@NonNull ExtensionCallback extensionCallback) { 40 this.mExtensionCallback = extensionCallback; 41 } 42 43 @Override onWindowLayoutChangeListenerAdded(@onNull Activity activity)44 public void onWindowLayoutChangeListenerAdded(@NonNull Activity activity) { 45 this.mWindowLayoutChangeListenerActivities.add(activity); 46 this.onListenersChanged(); 47 } 48 49 @Override onWindowLayoutChangeListenerRemoved(@onNull Activity activity)50 public void onWindowLayoutChangeListenerRemoved(@NonNull Activity activity) { 51 this.mWindowLayoutChangeListenerActivities.remove(activity); 52 this.onListenersChanged(); 53 } 54 updateWindowLayout(@onNull Activity activity, @NonNull ExtensionWindowLayoutInfo newLayout)55 void updateWindowLayout(@NonNull Activity activity, 56 @NonNull ExtensionWindowLayoutInfo newLayout) { 57 if (this.mExtensionCallback != null) { 58 mExtensionCallback.onWindowLayoutChanged(activity, newLayout); 59 } 60 } 61 62 @NonNull getActivitiesListeningForLayoutChanges()63 Set<Activity> getActivitiesListeningForLayoutChanges() { 64 return mWindowLayoutChangeListenerActivities; 65 } 66 hasListeners()67 protected boolean hasListeners() { 68 return !mWindowLayoutChangeListenerActivities.isEmpty(); 69 } 70 onListenersChanged()71 protected abstract void onListenersChanged(); 72 } 73