1 /*
2  * Copyright (C) 2024 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.systemui.car.systembar;
18 
19 import com.android.systemui.dagger.SysUISingleton;
20 
21 import java.util.ArrayList;
22 import java.util.List;
23 
24 import javax.inject.Inject;
25 
26 /**
27  * Central tracker to notify registered listeners of restart lifecycles within the CarSystemBar.
28  */
29 @SysUISingleton
30 public class CarSystemBarRestartTracker {
31     public final List<Listener> mListeners = new ArrayList<>();
32 
33     @Inject
CarSystemBarRestartTracker()34     public CarSystemBarRestartTracker() {
35     }
36 
37     /** Register a restart listener */
addListener(Listener listener)38     public void addListener(Listener listener) {
39         synchronized (mListeners) {
40             mListeners.add(listener);
41         }
42     }
43 
44     /** Unregister a restart listener */
removeListener(Listener listener)45     public void removeListener(Listener listener) {
46         synchronized (mListeners) {
47             mListeners.remove(listener);
48         }
49     }
50 
notifyPendingRestart(boolean recreateWindows, boolean provisionedStateChanged)51     void notifyPendingRestart(boolean recreateWindows, boolean provisionedStateChanged) {
52         synchronized (mListeners) {
53             for (Listener listener : mListeners) {
54                 listener.onPendingRestart(recreateWindows, provisionedStateChanged);
55             }
56         }
57     }
58 
notifyRestartComplete(boolean windowsRecreated, boolean provisionedStateChanged)59     void notifyRestartComplete(boolean windowsRecreated, boolean provisionedStateChanged) {
60         synchronized (mListeners) {
61             for (Listener listener : mListeners) {
62                 listener.onRestartComplete(windowsRecreated, provisionedStateChanged);
63             }
64         }
65     }
66 
67     public interface Listener {
68         /**
69          * Notify that a restart is about to occur. This will be called prior to the destruction
70          * of any windows or views.
71          * @param recreateWindows whether this restart will recreate the system bar windows (in
72          *                        addition to the views)
73          * @param provisionedStateChanged whether this restart was caused by a provisioned state
74          *                                change
75          */
onPendingRestart(boolean recreateWindows, boolean provisionedStateChanged)76         void onPendingRestart(boolean recreateWindows, boolean provisionedStateChanged);
77         /**
78          * Notify that a restart has just happened and the views (and potentially windows) were
79          * recreated
80          * @param windowsRecreated whether this restart recreated the system bar windows
81          * @param provisionedStateChanged whether this restart was caused by a provisioned state
82          *                                change
83          */
onRestartComplete(boolean windowsRecreated, boolean provisionedStateChanged)84         void onRestartComplete(boolean windowsRecreated, boolean provisionedStateChanged);
85     }
86 }
87