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