1 /*
2  * Copyright (C) 2018 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.car.settings;
18 
19 import static android.car.CarOccupantZoneManager.DISPLAY_TYPE_MAIN;
20 
21 import android.annotation.Nullable;
22 import android.app.Application;
23 import android.car.Car;
24 import android.car.Car.CarServiceLifecycleListener;
25 import android.car.CarOccupantZoneManager;
26 import android.car.CarOccupantZoneManager.OccupantZoneConfigChangeListener;
27 import android.car.CarOccupantZoneManager.OccupantZoneInfo;
28 import android.car.media.CarAudioManager;
29 import android.car.wifi.CarWifiManager;
30 import android.view.Display;
31 
32 import androidx.annotation.GuardedBy;
33 
34 /**
35  * Application class for CarSettings.
36  */
37 public class CarSettingsApplication extends Application {
38 
39     private CarOccupantZoneManager mCarOccupantZoneManager;
40 
41     private final Object mInfoLock = new Object();
42     private final Object mCarAudioManagerLock = new Object();
43     private final Object mCarWifiManagerLock = new Object();
44 
45     @GuardedBy("mInfoLock")
46     private int mOccupantZoneDisplayId = Display.DEFAULT_DISPLAY;
47     @GuardedBy("mInfoLock")
48     private int mAudioZoneId = CarAudioManager.INVALID_AUDIO_ZONE;
49     @GuardedBy("mInfoLock")
50     private int mOccupantZoneType = CarOccupantZoneManager.OCCUPANT_TYPE_INVALID;
51     @GuardedBy("mCarAudioManagerLock")
52     private CarAudioManager mCarAudioManager = null;
53     @GuardedBy("mCarWifiManagerLock")
54     private CarWifiManager mCarWifiManager = null;
55 
56     /**
57      * Listener to monitor any Occupant Zone configuration change.
58      */
59     private final OccupantZoneConfigChangeListener mConfigChangeListener = flags -> {
60         synchronized (mInfoLock) {
61             updateZoneInfoLocked();
62         }
63     };
64 
65     /**
66      * Listener to monitor the Lifecycle of car service.
67      */
68     private final CarServiceLifecycleListener mCarServiceLifecycleListener = (car, ready) -> {
69         if (!ready) {
70             mCarOccupantZoneManager = null;
71             synchronized (mCarAudioManagerLock) {
72                 mCarAudioManager = null;
73             }
74             synchronized (mCarWifiManagerLock) {
75                 mCarWifiManager = null;
76             }
77             return;
78         }
79         mCarOccupantZoneManager = (CarOccupantZoneManager) car.getCarManager(
80                 Car.CAR_OCCUPANT_ZONE_SERVICE);
81         if (mCarOccupantZoneManager != null) {
82             mCarOccupantZoneManager.registerOccupantZoneConfigChangeListener(
83                     mConfigChangeListener);
84         }
85         synchronized (mCarAudioManagerLock) {
86             mCarAudioManager = (CarAudioManager) car.getCarManager(Car.AUDIO_SERVICE);
87         }
88         synchronized (mCarWifiManagerLock) {
89             mCarWifiManager = (CarWifiManager) car.getCarManager(Car.CAR_WIFI_SERVICE);
90         }
91         synchronized (mInfoLock) {
92             updateZoneInfoLocked();
93         }
94     };
95 
96     @Override
onCreate()97     public void onCreate() {
98         super.onCreate();
99 
100         Car.createCar(this, /* handler= */ null , Car.CAR_WAIT_TIMEOUT_WAIT_FOREVER,
101                 mCarServiceLifecycleListener);
102     }
103 
104     /**
105      * Returns zone type assigned for the current user.
106      * The zone type is used to determine whether the settings preferences
107      * should be available or not.
108      */
getMyOccupantZoneType()109     public final int getMyOccupantZoneType() {
110         synchronized (mInfoLock) {
111             return mOccupantZoneType;
112         }
113     }
114 
115     /**
116      * Returns displayId assigned for the current user.
117      */
getMyOccupantZoneDisplayId()118     public final int getMyOccupantZoneDisplayId() {
119         synchronized (mInfoLock) {
120             return mOccupantZoneDisplayId;
121         }
122     }
123 
124     /**
125      * Returns audio zone id assigned for the current user.
126      */
getMyAudioZoneId()127     public final int getMyAudioZoneId() {
128         synchronized (mInfoLock) {
129             return mAudioZoneId;
130         }
131     }
132 
133     /**
134      * Returns CarAudioManager instance.
135      */
136     @Nullable
getCarAudioManager()137     public final CarAudioManager getCarAudioManager() {
138         synchronized (mCarAudioManagerLock) {
139             return mCarAudioManager;
140         }
141     }
142 
143     /**
144      * Returns CarAudioManager instance.
145      */
146     @Nullable
getCarWifiManager()147     public final CarWifiManager getCarWifiManager() {
148         synchronized (mCarWifiManagerLock) {
149             return mCarWifiManager;
150         }
151     }
152 
153     @GuardedBy("mInfoLock")
updateZoneInfoLocked()154     private void updateZoneInfoLocked() {
155         if (mCarOccupantZoneManager == null) {
156             return;
157         }
158         OccupantZoneInfo info = mCarOccupantZoneManager.getMyOccupantZone();
159         if (info != null) {
160             mOccupantZoneType = info.occupantType;
161             mAudioZoneId = mCarOccupantZoneManager.getAudioZoneIdForOccupant(info);
162             Display display = mCarOccupantZoneManager
163                     .getDisplayForOccupant(info, DISPLAY_TYPE_MAIN);
164             if (display != null) {
165                 mOccupantZoneDisplayId = display.getDisplayId();
166             }
167         }
168     }
169 }
170