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 android.car.app; 18 19 20 import com.android.internal.annotations.VisibleForTesting; 21 22 import java.util.ArrayList; 23 import java.util.List; 24 25 26 /** 27 * This class represents a handle to the lifecycle of the host (container) that creates the 28 * {@link CarTaskViewController}. 29 * The container can be an activity, fragment, view or a window. 30 * 31 * @hide 32 */ 33 public final class CarTaskViewControllerHostLifecycle { 34 /** An interface for observing the lifecycle of the container (host). */ 35 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) 36 public interface CarTaskViewControllerHostLifecycleObserver { 37 /** Gets called when the container is destroyed. */ onHostDestroyed(CarTaskViewControllerHostLifecycle lifecycle)38 void onHostDestroyed(CarTaskViewControllerHostLifecycle lifecycle); 39 40 /** Gets called when the container has appeared. */ onHostAppeared(CarTaskViewControllerHostLifecycle lifecycle)41 void onHostAppeared(CarTaskViewControllerHostLifecycle lifecycle); 42 43 /** Gets called when the container has disappeared. */ onHostDisappeared(CarTaskViewControllerHostLifecycle lifecycle)44 void onHostDisappeared(CarTaskViewControllerHostLifecycle lifecycle); 45 } 46 47 private final List<CarTaskViewControllerHostLifecycleObserver> mObserverList = 48 new ArrayList<>(); 49 50 private boolean mIsVisible = false; 51 52 /** 53 * Notifies the lifecycle observers that the host has been destroyed and they can clean their 54 * internal state. 55 */ hostDestroyed()56 public void hostDestroyed() { 57 for (CarTaskViewControllerHostLifecycleObserver observer : mObserverList) { 58 observer.onHostDestroyed(this); 59 } 60 } 61 62 /** Notifies the lifecycle observers that the host has appeared. */ hostAppeared()63 public void hostAppeared() { 64 mIsVisible = true; 65 for (CarTaskViewControllerHostLifecycleObserver observer : mObserverList) { 66 observer.onHostAppeared(this); 67 } 68 } 69 70 /** Notifies the lifecycle observers that the host has disappeared. */ hostDisappeared()71 public void hostDisappeared() { 72 mIsVisible = false; 73 for (CarTaskViewControllerHostLifecycleObserver observer : mObserverList) { 74 observer.onHostDisappeared(this); 75 } 76 } 77 78 /** @return true if the container is visible, false otherwise. */ 79 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) isVisible()80 public boolean isVisible() { 81 return mIsVisible; 82 } 83 84 /** Registers the given observer to listen to lifecycle of the container. */ 85 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) registerObserver(CarTaskViewControllerHostLifecycleObserver observer)86 public void registerObserver(CarTaskViewControllerHostLifecycleObserver observer) { 87 mObserverList.add(observer); 88 } 89 90 /** Unregisters the given observer to stop listening to the lifecycle of the container. */ 91 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) unregisterObserver(CarTaskViewControllerHostLifecycleObserver observer)92 public void unregisterObserver(CarTaskViewControllerHostLifecycleObserver observer) { 93 mObserverList.remove(observer); 94 } 95 } 96 97 98