• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.audio;
18 
19 import static android.car.hardware.power.PowerComponent.AUDIO;
20 
21 import android.annotation.NonNull;
22 import android.car.hardware.power.CarPowerPolicy;
23 import android.car.hardware.power.CarPowerPolicyFilter;
24 import android.car.hardware.power.ICarPowerPolicyListener;
25 import android.util.Slog;
26 
27 import com.android.car.CarLocalServices;
28 import com.android.car.CarLog;
29 import com.android.car.power.CarPowerManagementService;
30 import com.android.internal.annotations.GuardedBy;
31 import com.android.internal.annotations.VisibleForTesting;
32 
33 import java.util.Objects;
34 
35 class CarAudioPowerListener {
36     private static final String TAG = CarLog.tagFor(CarAudioPowerListener.class);
37 
38     private final Object mLock = new Object();
39     private final CarAudioService mCarAudioService;
40     private final CarPowerManagementService mCarPowerManagementService;
41 
42     private final ICarPowerPolicyListener mChangeListener =
43             new ICarPowerPolicyListener.Stub() {
44                 @Override
45                 public void onPolicyChanged(CarPowerPolicy policy,
46                         CarPowerPolicy accumulatedPolicy) {
47                     synchronized (mLock) {
48                         if (mIsAudioEnabled != accumulatedPolicy.isComponentEnabled(AUDIO)) {
49                             updateAudioPowerStateLocked(accumulatedPolicy);
50                         }
51                     }
52                 }
53             };
54 
55     @GuardedBy("mLock")
56     private boolean mIsAudioEnabled;
57 
newCarAudioPowerListener( @onNull CarAudioService carAudioService)58     static CarAudioPowerListener newCarAudioPowerListener(
59             @NonNull CarAudioService carAudioService) {
60         CarPowerManagementService carPowerService = CarLocalServices.getService(
61                 CarPowerManagementService.class);
62         return new CarAudioPowerListener(carAudioService, carPowerService);
63     }
64 
65     @VisibleForTesting
CarAudioPowerListener(@onNull CarAudioService carAudioService, CarPowerManagementService carPowerManagementService)66     CarAudioPowerListener(@NonNull CarAudioService carAudioService,
67             CarPowerManagementService carPowerManagementService) {
68         mCarAudioService = Objects.requireNonNull(carAudioService);
69         mCarPowerManagementService = carPowerManagementService;
70     }
71 
isAudioEnabled()72     boolean isAudioEnabled() {
73         synchronized (mLock) {
74             return mIsAudioEnabled;
75         }
76     }
77 
startListeningForPolicyChanges()78     void startListeningForPolicyChanges() {
79         if (mCarPowerManagementService == null) {
80             Slog.w(TAG, "Cannot find CarPowerManagementService");
81             mCarAudioService.setAudioEnabled(/* isAudioEnabled= */ true);
82             return;
83         }
84 
85         CarPowerPolicyFilter filter = new CarPowerPolicyFilter.Builder()
86                 .setComponents(AUDIO).build();
87         mCarPowerManagementService.addPowerPolicyListener(filter, mChangeListener);
88         initializePowerState();
89     }
90 
stopListeningForPolicyChanges()91     void stopListeningForPolicyChanges() {
92         if (mCarPowerManagementService == null) {
93             return;
94         }
95         mCarPowerManagementService.removePowerPolicyListener(mChangeListener);
96     }
97 
initializePowerState()98     private void initializePowerState() {
99         CarPowerPolicy policy = mCarPowerManagementService.getCurrentPowerPolicy();
100 
101         if (policy == null) {
102             Slog.w(TAG, "Policy is null. Defaulting to enabled");
103             mCarAudioService.setAudioEnabled(/* isAudioEnabled= */ true);
104             return;
105         }
106 
107         synchronized (mLock) {
108             updateAudioPowerStateLocked(policy);
109         }
110     }
111 
112     @GuardedBy("mLock")
updateAudioPowerStateLocked(CarPowerPolicy policy)113     private void updateAudioPowerStateLocked(CarPowerPolicy policy) {
114         mIsAudioEnabled = policy.isComponentEnabled(AUDIO);
115         mCarAudioService.setAudioEnabled(mIsAudioEnabled);
116     }
117 }
118