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.util.Utils; 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 groupId)51 int getStoredVolumeGainIndexForUser(@UserIdInt int userId, int zoneId, int groupId) { 52 return getIntForUser(getVolumeSettingsKeyForGroup(zoneId, groupId), -1, userId); 53 } 54 storeVolumeGainIndexForUser(@serIdInt int userId, int zoneId, int groupId, int gainIndex)55 void storeVolumeGainIndexForUser(@UserIdInt int userId, int zoneId, int groupId, 56 int gainIndex) { 57 putIntForUser(getVolumeSettingsKeyForGroup(zoneId, groupId), 58 gainIndex, 59 userId); 60 } 61 storeMasterMute(Boolean masterMuteValue)62 void storeMasterMute(Boolean masterMuteValue) { 63 Settings.Global.putInt(mContext.getContentResolver(), 64 VOLUME_SETTINGS_KEY_MASTER_MUTE, 65 masterMuteValue ? 1 : 0); 66 } 67 getMasterMute()68 boolean getMasterMute() { 69 return Settings.Global.getInt(mContext.getContentResolver(), 70 VOLUME_SETTINGS_KEY_MASTER_MUTE, 0) != 0; 71 } 72 storeVolumeGroupMuteForUser(@serIdInt int userId, int zoneId, int groupId, boolean isMuted)73 void storeVolumeGroupMuteForUser(@UserIdInt int userId, int zoneId, int groupId, 74 boolean isMuted) { 75 putIntForUser(getMuteSettingsKeyForGroup(zoneId, groupId), 76 isMuted ? 1 : 0, userId); 77 } 78 getVolumeGroupMuteForUser(@serIdInt int userId, int zoneId, int groupId)79 boolean getVolumeGroupMuteForUser(@UserIdInt int userId, int zoneId, int groupId) { 80 return getIntForUser(getMuteSettingsKeyForGroup(zoneId, groupId), 81 /* defaultValue= */ 0, userId) != 0; 82 } 83 isPersistVolumeGroupMuteEnabled(@serIdInt int userId)84 boolean isPersistVolumeGroupMuteEnabled(@UserIdInt int userId) { 85 return getSecureIntForUser(CarSettings.Secure.KEY_AUDIO_PERSIST_VOLUME_GROUP_MUTE_STATES, 86 /* defaultValue= */ 0, userId) == 1; 87 } 88 89 /** 90 * Determines if for a given userId the reject navigation on call setting is enabled 91 */ isRejectNavigationOnCallEnabledInSettings(@serIdInt int userId)92 public boolean isRejectNavigationOnCallEnabledInSettings(@UserIdInt int userId) { 93 return getSecureIntForUser( 94 CarSettings.Secure.KEY_AUDIO_FOCUS_NAVIGATION_REJECTED_DURING_CALL, 95 /* defaultValue= */ 0, userId) == 1; 96 } 97 getIntForUser(String name, int defaultValue, @UserIdInt int userId)98 private int getIntForUser(String name, int defaultValue, @UserIdInt int userId) { 99 return Settings.System.getInt(getContentResolverForUser(userId), name, defaultValue); 100 } 101 putIntForUser(String name, int value, @UserIdInt int userId)102 private void putIntForUser(String name, int value, @UserIdInt int userId) { 103 Settings.System.putInt(getContentResolverForUser(userId), name, value); 104 } 105 getSecureIntForUser(String name, int defaultValue, @UserIdInt int userId)106 private int getSecureIntForUser(String name, int defaultValue, @UserIdInt int userId) { 107 return Settings.Secure.getInt(getContentResolverForUser(userId), name, defaultValue); 108 } 109 getVolumeSettingsKeyForGroup(int zoneId, int groupId)110 private static String getVolumeSettingsKeyForGroup(int zoneId, int groupId) { 111 return VOLUME_SETTINGS_KEY_FOR_GROUP_PREFIX 112 + getFormattedZoneIdAndGroupIdKey(zoneId, groupId); 113 } 114 getMuteSettingsKeyForGroup(int zoneId, int groupId)115 private static String getMuteSettingsKeyForGroup(int zoneId, int groupId) { 116 return VOLUME_SETTINGS_KEY_FOR_GROUP_MUTE_PREFIX 117 + getFormattedZoneIdAndGroupIdKey(zoneId, groupId); 118 } 119 getFormattedZoneIdAndGroupIdKey(int zoneId, int groupId)120 private static String getFormattedZoneIdAndGroupIdKey(int zoneId, int groupId) { 121 return new StringBuilder().append(zoneId).append('/').append(groupId).toString(); 122 } 123 getContentResolverForUser(@serIdInt int userId)124 ContentResolver getContentResolverForUser(@UserIdInt int userId) { 125 return Utils.getContentResolverForUser(mContext, userId); 126 } 127 } 128