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