1 /*
2  * Copyright (C) 2019 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 package com.android.car.audio;
17 
18 import android.annotation.UserIdInt;
19 import android.car.settings.CarSettings;
20 import android.content.ContentResolver;
21 import android.provider.Settings;
22 
23 import androidx.annotation.NonNull;
24 
25 import java.util.Objects;
26 
27 /**
28  * Use to save/load car volume settings
29  */
30 public class CarAudioSettings {
31 
32     // The trailing slash forms a directory-liked hierarchy and
33     // allows listening for both GROUP/MEDIA and GROUP/NAVIGATION.
34     private static final String VOLUME_SETTINGS_KEY_FOR_GROUP_PREFIX = "android.car.VOLUME_GROUP/";
35 
36     // Key to persist master mute state in system settings
37     private static final String VOLUME_SETTINGS_KEY_MASTER_MUTE = "android.car.MASTER_MUTE";
38 
39     /**
40      * Gets the key to persist volume for a volume group in settings
41      *
42      * @param zoneId The audio zone id
43      * @param groupId The volume group id
44      * @return Key to persist volume index for volume group in system settings
45      */
getVolumeSettingsKeyForGroup(int zoneId, int groupId)46     private static String getVolumeSettingsKeyForGroup(int zoneId, int groupId) {
47         final int maskedGroupId = (zoneId << 8) + groupId;
48         return VOLUME_SETTINGS_KEY_FOR_GROUP_PREFIX + maskedGroupId;
49     }
50 
51     private final ContentResolver mContentResolver;
52 
CarAudioSettings(@onNull ContentResolver contentResolver)53     CarAudioSettings(@NonNull ContentResolver contentResolver) {
54         mContentResolver = Objects.requireNonNull(contentResolver);
55     }
56 
getStoredVolumeGainIndexForUser(int userId, int zoneId, int id)57     int getStoredVolumeGainIndexForUser(int userId, int zoneId, int id) {
58         return Settings.System.getIntForUser(mContentResolver,
59                 getVolumeSettingsKeyForGroup(zoneId, id), -1, userId);
60     }
61 
storeVolumeGainIndexForUser(int userId, int zoneId, int id, int gainIndex)62     void storeVolumeGainIndexForUser(int userId, int zoneId, int id, int gainIndex) {
63         Settings.System.putIntForUser(mContentResolver,
64                 getVolumeSettingsKeyForGroup(zoneId, id),
65                 gainIndex, userId);
66     }
67 
storeMasterMute(Boolean masterMuteValue)68     void storeMasterMute(Boolean masterMuteValue) {
69         Settings.Global.putInt(mContentResolver,
70                 VOLUME_SETTINGS_KEY_MASTER_MUTE,
71                 masterMuteValue ? 1 : 0);
72     }
73 
getMasterMute()74     boolean getMasterMute() {
75         return Settings.Global.getInt(mContentResolver,
76                 VOLUME_SETTINGS_KEY_MASTER_MUTE, 0) != 0;
77     }
78 
79     /**
80      * Determines if for a given userId the reject navigation on call setting is enabled
81      */
isRejectNavigationOnCallEnabledInSettings(@serIdInt int userId)82     public boolean isRejectNavigationOnCallEnabledInSettings(@UserIdInt int userId) {
83         return Settings.Secure.getIntForUser(mContentResolver,
84                 CarSettings.Secure.KEY_AUDIO_FOCUS_NAVIGATION_REJECTED_DURING_CALL,
85                 /*disabled by default*/ 0, userId) == 1;
86     }
87 
getContentResolver()88     public ContentResolver getContentResolver() {
89         return mContentResolver;
90     }
91 }
92