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