• 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.media.CarAudioManager.PRIMARY_AUDIO_ZONE;
20 import static android.util.Log.DEBUG;
21 import static android.util.Log.VERBOSE;
22 
23 import static com.android.car.CarLog.TAG_AUDIO;
24 
25 import android.annotation.NonNull;
26 import android.media.AudioManager;
27 import android.media.audiopolicy.AudioPolicy;
28 import android.media.audiopolicy.AudioPolicy.Builder;
29 import android.util.Log;
30 import android.util.Slog;
31 
32 import com.android.car.audio.CarAudioContext.AudioContext;
33 import com.android.internal.annotations.VisibleForTesting;
34 
35 import java.util.Objects;
36 
37 final class CarAudioPolicyVolumeCallback extends AudioPolicy.AudioPolicyVolumeCallback{
38     private final CarAudioService mCarAudioService;
39     private final AudioManager mAudioManager;
40     private final boolean mUseCarVolumeGroupMuting;
41 
addVolumeCallbackToPolicy(@onNull Builder policyBuilder, @NonNull CarAudioService carAudioService, @NonNull AudioManager audioManager, boolean useCarVolumeGroupMuting)42     static void addVolumeCallbackToPolicy(@NonNull Builder policyBuilder,
43             @NonNull CarAudioService carAudioService,
44             @NonNull AudioManager audioManager, boolean useCarVolumeGroupMuting) {
45         Objects.requireNonNull(policyBuilder, "AudioPolicy.Builder cannot be null");
46         policyBuilder.setAudioPolicyVolumeCallback(
47                 new CarAudioPolicyVolumeCallback(carAudioService, audioManager,
48                         useCarVolumeGroupMuting));
49         if (Log.isLoggable(TAG_AUDIO, DEBUG)) {
50             Slog.d(TAG_AUDIO, "Registered car audio policy volume callback");
51         }
52     }
53 
54     @VisibleForTesting
CarAudioPolicyVolumeCallback(@onNull CarAudioService carAudioService, @NonNull AudioManager audioManager, boolean useCarVolumeGroupMuting)55     CarAudioPolicyVolumeCallback(@NonNull CarAudioService carAudioService,
56             @NonNull AudioManager audioManager, boolean useCarVolumeGroupMuting) {
57         mCarAudioService = Objects.requireNonNull(carAudioService,
58                 "CarAudioService cannot be null");
59         mAudioManager = Objects.requireNonNull(audioManager, "AudioManager cannot be null");
60         mUseCarVolumeGroupMuting = useCarVolumeGroupMuting;
61     }
62 
63     @Override
onVolumeAdjustment(int adjustment)64     public void onVolumeAdjustment(int adjustment) {
65         @AudioContext int suggestedContext = mCarAudioService
66                 .getSuggestedAudioContextForPrimaryZone();
67 
68         int zoneId = PRIMARY_AUDIO_ZONE;
69         int groupId = mCarAudioService.getVolumeGroupIdForAudioContext(zoneId, suggestedContext);
70 
71         if (Log.isLoggable(TAG_AUDIO, VERBOSE)) {
72             Slog.v(TAG_AUDIO, "onVolumeAdjustment: "
73                     + AudioManager.adjustToString(adjustment) + " suggested audio context: "
74                     + CarAudioContext.toString(suggestedContext) + " suggested volume group: "
75                     + groupId);
76         }
77 
78         final int currentVolume = mCarAudioService.getGroupVolume(zoneId, groupId);
79         final int flags = AudioManager.FLAG_FROM_KEY | AudioManager.FLAG_SHOW_UI;
80         switch (adjustment) {
81             case AudioManager.ADJUST_LOWER:
82                 int minValue = Math.max(currentVolume - 1,
83                         mCarAudioService.getGroupMinVolume(zoneId, groupId));
84                 mCarAudioService.setGroupVolume(zoneId, groupId, minValue, flags);
85                 break;
86             case AudioManager.ADJUST_RAISE:
87                 int maxValue = Math.min(currentVolume + 1,
88                         mCarAudioService.getGroupMaxVolume(zoneId, groupId));
89                 mCarAudioService.setGroupVolume(zoneId, groupId, maxValue, flags);
90                 break;
91             case AudioManager.ADJUST_MUTE:
92             case AudioManager.ADJUST_UNMUTE:
93                 setMute(adjustment == AudioManager.ADJUST_MUTE, groupId, flags);
94                 break;
95             case AudioManager.ADJUST_TOGGLE_MUTE:
96                 toggleMute(groupId, flags);
97                 break;
98             case AudioManager.ADJUST_SAME:
99             default:
100                 break;
101         }
102     }
103 
toggleMute(int groupId, int flags)104     private void toggleMute(int groupId, int flags) {
105         if (mUseCarVolumeGroupMuting) {
106             setMute(!mCarAudioService.isVolumeGroupMuted(PRIMARY_AUDIO_ZONE, groupId), groupId,
107                     flags);
108             return;
109         }
110         setMute(!mAudioManager.isMasterMute(), groupId, flags);
111     }
112 
setMute(boolean mute, int groupId, int flags)113     private void setMute(boolean mute, int groupId, int flags) {
114         if (mUseCarVolumeGroupMuting) {
115             mCarAudioService.setVolumeGroupMute(PRIMARY_AUDIO_ZONE, groupId, mute, flags);
116             return;
117         }
118         mCarAudioService.setMasterMute(mute, flags);
119     }
120 }
121