• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.NonNull;
19 import android.annotation.UserIdInt;
20 import android.car.settings.CarSettings;
21 import android.content.ContentResolver;
22 import android.provider.Settings;
23 
24 import java.util.Objects;
25 
26 /**
27  * Use to save/load car volume settings
28  */
29 public class CarAudioSettings {
30 
31     // The trailing slash forms a directory-liked hierarchy and
32     // allows listening for both GROUP/MEDIA and GROUP/NAVIGATION.
33     private static final String VOLUME_SETTINGS_KEY_FOR_GROUP_PREFIX = "android.car.VOLUME_GROUP/";
34 
35     // The trailing slash forms a directory-liked hierarchy and
36     // allows listening for both GROUP/MEDIA and GROUP/NAVIGATION.
37     private static final String VOLUME_SETTINGS_KEY_FOR_GROUP_MUTE_PREFIX =
38             "android.car.VOLUME_GROUP_MUTE/";
39 
40     // Key to persist master mute state in system settings
41     private static final String VOLUME_SETTINGS_KEY_MASTER_MUTE = "android.car.MASTER_MUTE";
42 
43     private final ContentResolver mContentResolver;
44 
CarAudioSettings(@onNull ContentResolver contentResolver)45     CarAudioSettings(@NonNull ContentResolver contentResolver) {
46         mContentResolver = Objects.requireNonNull(contentResolver);
47     }
48 
getStoredVolumeGainIndexForUser(int userId, int zoneId, int groupId)49     int getStoredVolumeGainIndexForUser(int userId, int zoneId, int groupId) {
50         return Settings.System.getIntForUser(mContentResolver,
51                 getVolumeSettingsKeyForGroup(zoneId, groupId), -1, userId);
52     }
53 
storeVolumeGainIndexForUser(int userId, int zoneId, int groupId, int gainIndex)54     void storeVolumeGainIndexForUser(int userId, int zoneId, int groupId, int gainIndex) {
55         Settings.System.putIntForUser(mContentResolver,
56                 getVolumeSettingsKeyForGroup(zoneId, groupId),
57                 gainIndex, userId);
58     }
59 
storeMasterMute(Boolean masterMuteValue)60     void storeMasterMute(Boolean masterMuteValue) {
61         Settings.Global.putInt(mContentResolver,
62                 VOLUME_SETTINGS_KEY_MASTER_MUTE,
63                 masterMuteValue ? 1 : 0);
64     }
65 
getMasterMute()66     boolean getMasterMute() {
67         return Settings.Global.getInt(mContentResolver,
68                 VOLUME_SETTINGS_KEY_MASTER_MUTE, 0) != 0;
69     }
70 
storeVolumeGroupMuteForUser(@serIdInt int userId, int zoneId, int groupId, boolean isMuted)71     void storeVolumeGroupMuteForUser(@UserIdInt int userId, int zoneId, int groupId,
72             boolean isMuted) {
73         Settings.System.putIntForUser(mContentResolver,
74                 getMuteSettingsKeyForGroup(zoneId, groupId),
75                 isMuted ? 1 : 0, userId);
76     }
77 
getVolumeGroupMuteForUser(@serIdInt int userId, int zoneId, int groupId)78     boolean getVolumeGroupMuteForUser(@UserIdInt int userId, int zoneId, int groupId) {
79         return Settings.System.getIntForUser(mContentResolver,
80                 getMuteSettingsKeyForGroup(zoneId, groupId),
81                 /*disabled by default*/ 0, userId) != 0;
82     }
83 
isPersistVolumeGroupMuteEnabled(@serIdInt int userId)84     boolean isPersistVolumeGroupMuteEnabled(@UserIdInt int userId) {
85         return Settings.Secure.getIntForUser(mContentResolver,
86                 CarSettings.Secure.KEY_AUDIO_PERSIST_VOLUME_GROUP_MUTE_STATES,
87                 /*disabled by default*/ 0, userId) == 1;
88     }
89 
90     /**
91      * Determines if for a given userId the reject navigation on call setting is enabled
92      */
isRejectNavigationOnCallEnabledInSettings(@serIdInt int userId)93     public boolean isRejectNavigationOnCallEnabledInSettings(@UserIdInt int userId) {
94         return Settings.Secure.getIntForUser(mContentResolver,
95                 CarSettings.Secure.KEY_AUDIO_FOCUS_NAVIGATION_REJECTED_DURING_CALL,
96                 /*disabled by default*/ 0, userId) == 1;
97     }
98 
getVolumeSettingsKeyForGroup(int zoneId, int groupId)99     private static String getVolumeSettingsKeyForGroup(int zoneId, int groupId) {
100         return VOLUME_SETTINGS_KEY_FOR_GROUP_PREFIX
101                 + getFormattedZoneIdAndGroupIdKey(zoneId, groupId);
102     }
103 
getMuteSettingsKeyForGroup(int zoneId, int groupId)104     private static String getMuteSettingsKeyForGroup(int zoneId, int groupId) {
105         return VOLUME_SETTINGS_KEY_FOR_GROUP_MUTE_PREFIX
106                 + getFormattedZoneIdAndGroupIdKey(zoneId, groupId);
107     }
108 
getFormattedZoneIdAndGroupIdKey(int zoneId, int groupId)109     private static String getFormattedZoneIdAndGroupIdKey(int zoneId, int groupId) {
110         return new StringBuilder().append(zoneId).append('/').append(groupId).toString();
111     }
112 
getContentResolver()113     public ContentResolver getContentResolver() {
114         return mContentResolver;
115     }
116 }
117