OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromeos/audio/audio_devices_pref_handler_impl.h" | 5 #include "chromeos/audio/audio_devices_pref_handler_impl.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 | 10 |
(...skipping 16 matching lines...) Expand all Loading... | |
27 // Gets the device id string for storing audio preference. The format of | 27 // Gets the device id string for storing audio preference. The format of |
28 // device string is a string consisting of 3 parts. | 28 // device string is a string consisting of 3 parts. |
29 // |device_name| : |integer from lower 32 bit of device id| : | 29 // |device_name| : |integer from lower 32 bit of device id| : |
30 // |0(output device) or 1(input device)| | 30 // |0(output device) or 1(input device)| |
31 // If an audio device has both integrated input and output devices, the first 2 | 31 // If an audio device has both integrated input and output devices, the first 2 |
32 // parts of the string could be identical, only the last part will differentiate | 32 // parts of the string could be identical, only the last part will differentiate |
33 // them. | 33 // them. |
34 std::string GetDeviceIdString(const chromeos::AudioDevice& device) { | 34 std::string GetDeviceIdString(const chromeos::AudioDevice& device) { |
35 std::string device_id_string = | 35 std::string device_id_string = |
36 device.device_name + " : " + | 36 device.device_name + " : " + |
37 base::Uint64ToString(device.id & static_cast<uint64_t>(0xffffffff)) + | 37 base::Uint64ToString(device.stable_device_id & |
Daniel Erat
2016/01/07 17:31:12
is this going to break everyone's existing volume
hychao
2016/01/11 07:02:32
The device id doesn't work as it supposed to, due
| |
38 static_cast<uint64_t>(0xffffffff)) + | |
38 " : " + (device.is_input ? "1" : "0"); | 39 " : " + (device.is_input ? "1" : "0"); |
39 // Replace any periods from the device id string with a space, since setting | 40 // Replace any periods from the device id string with a space, since setting |
40 // names cannot contain periods. | 41 // names cannot contain periods. |
41 std::replace(device_id_string.begin(), device_id_string.end(), '.', ' '); | 42 std::replace(device_id_string.begin(), device_id_string.end(), '.', ' '); |
42 return device_id_string; | 43 return device_id_string; |
43 } | 44 } |
44 | 45 |
45 } // namespace | 46 } // namespace |
46 | 47 |
47 namespace chromeos { | 48 namespace chromeos { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
80 return (mute == kPrefMuteOn); | 81 return (mute == kPrefMuteOn); |
81 } | 82 } |
82 | 83 |
83 void AudioDevicesPrefHandlerImpl::SetMuteValue(const AudioDevice& device, | 84 void AudioDevicesPrefHandlerImpl::SetMuteValue(const AudioDevice& device, |
84 bool mute) { | 85 bool mute) { |
85 device_mute_settings_->SetInteger(GetDeviceIdString(device), | 86 device_mute_settings_->SetInteger(GetDeviceIdString(device), |
86 mute ? kPrefMuteOn : kPrefMuteOff); | 87 mute ? kPrefMuteOn : kPrefMuteOff); |
87 SaveDevicesMutePref(); | 88 SaveDevicesMutePref(); |
88 } | 89 } |
89 | 90 |
91 AudioDeviceState AudioDevicesPrefHandlerImpl::GetDeviceState( | |
92 const AudioDevice& device) { | |
93 UpdateDevicesStatePref(); | |
Daniel Erat
2016/01/07 17:31:12
whey are you calling this every time in GetDeviceS
hychao
2016/01/11 07:02:32
Done.
Yes you're right, call this once in construc
| |
94 std::string device_id_str = GetDeviceIdString(device); | |
95 if (!device_state_settings_->HasKey(device_id_str)) { | |
96 device_state_settings_->SetInteger(device_id_str, | |
97 (int)AUDIO_STATE_NOT_AVAILABLE); | |
Daniel Erat
2016/01/07 17:31:12
use c++ static_casts and/or reinterpret_casts rath
hychao
2016/01/11 07:02:32
Done.
| |
98 SaveDevicesStatePref(); | |
99 } | |
100 int state = (int)AUDIO_STATE_NOT_AVAILABLE; | |
101 device_state_settings_->GetInteger(device_id_str, &state); | |
102 return (AudioDeviceState)state; | |
103 } | |
104 | |
105 void AudioDevicesPrefHandlerImpl::SetDeviceState(const AudioDevice& device, | |
106 AudioDeviceState state) { | |
107 device_state_settings_->SetInteger(GetDeviceIdString(device), | |
108 (int)state); | |
109 SaveDevicesStatePref(); | |
110 } | |
111 | |
90 bool AudioDevicesPrefHandlerImpl::GetAudioOutputAllowedValue() { | 112 bool AudioDevicesPrefHandlerImpl::GetAudioOutputAllowedValue() { |
91 return local_state_->GetBoolean(prefs::kAudioOutputAllowed); | 113 return local_state_->GetBoolean(prefs::kAudioOutputAllowed); |
92 } | 114 } |
93 | 115 |
94 void AudioDevicesPrefHandlerImpl::AddAudioPrefObserver( | 116 void AudioDevicesPrefHandlerImpl::AddAudioPrefObserver( |
95 AudioPrefObserver* observer) { | 117 AudioPrefObserver* observer) { |
96 observers_.AddObserver(observer); | 118 observers_.AddObserver(observer); |
97 } | 119 } |
98 | 120 |
99 void AudioDevicesPrefHandlerImpl::RemoveAudioPrefObserver( | 121 void AudioDevicesPrefHandlerImpl::RemoveAudioPrefObserver( |
(...skipping 25 matching lines...) Expand all Loading... | |
125 if (device.type == AUDIO_TYPE_HDMI) | 147 if (device.type == AUDIO_TYPE_HDMI) |
126 return kDefaultHdmiOutputVolumePercent; | 148 return kDefaultHdmiOutputVolumePercent; |
127 else | 149 else |
128 return kDefaultOutputVolumePercent; | 150 return kDefaultOutputVolumePercent; |
129 } | 151 } |
130 | 152 |
131 AudioDevicesPrefHandlerImpl::AudioDevicesPrefHandlerImpl( | 153 AudioDevicesPrefHandlerImpl::AudioDevicesPrefHandlerImpl( |
132 PrefService* local_state) | 154 PrefService* local_state) |
133 : device_mute_settings_(new base::DictionaryValue()), | 155 : device_mute_settings_(new base::DictionaryValue()), |
134 device_volume_settings_(new base::DictionaryValue()), | 156 device_volume_settings_(new base::DictionaryValue()), |
157 device_state_settings_(new base::DictionaryValue()), | |
135 local_state_(local_state) { | 158 local_state_(local_state) { |
136 InitializePrefObservers(); | 159 InitializePrefObservers(); |
137 | 160 |
138 UpdateDevicesMutePref(); | 161 UpdateDevicesMutePref(); |
139 UpdateDevicesVolumePref(); | 162 UpdateDevicesVolumePref(); |
140 } | 163 } |
141 | 164 |
142 AudioDevicesPrefHandlerImpl::~AudioDevicesPrefHandlerImpl() { | 165 AudioDevicesPrefHandlerImpl::~AudioDevicesPrefHandlerImpl() { |
143 } | 166 } |
144 | 167 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
181 base::DictionaryValue::Iterator it(*device_volume_settings_); | 204 base::DictionaryValue::Iterator it(*device_volume_settings_); |
182 while (!it.IsAtEnd()) { | 205 while (!it.IsAtEnd()) { |
183 double volume = kDefaultOutputVolumePercent; | 206 double volume = kDefaultOutputVolumePercent; |
184 bool success = it.value().GetAsDouble(&volume); | 207 bool success = it.value().GetAsDouble(&volume); |
185 DCHECK(success); | 208 DCHECK(success); |
186 dict_update->SetDouble(it.key(), volume); | 209 dict_update->SetDouble(it.key(), volume); |
187 it.Advance(); | 210 it.Advance(); |
188 } | 211 } |
189 } | 212 } |
190 | 213 |
214 void AudioDevicesPrefHandlerImpl::UpdateDevicesStatePref() { | |
Daniel Erat
2016/01/07 17:31:12
ReadDevicesStatePref() or LoadDevicesStatePref() s
hychao
2016/01/11 07:02:32
Done. Thanks for the suggestion, modified other fu
| |
215 const base::DictionaryValue* state_prefs = | |
216 local_state_->GetDictionary(prefs::kAudioDevicesState); | |
217 if (state_prefs) | |
218 device_state_settings_.reset(state_prefs->DeepCopy()); | |
219 } | |
220 | |
221 void AudioDevicesPrefHandlerImpl::SaveDevicesStatePref() { | |
222 DictionaryPrefUpdate dict_update(local_state_, | |
223 prefs::kAudioDevicesState); | |
224 base::DictionaryValue::Iterator it(*device_state_settings_); | |
225 while (!it.IsAtEnd()) { | |
Daniel Erat
2016/01/07 17:31:12
can you just call DictionaryValue::MergeDictionary
hychao
2016/01/11 07:02:32
Done. Thanks!
| |
226 int state = AUDIO_STATE_NOT_AVAILABLE; | |
227 bool success = it.value().GetAsInteger(&state); | |
228 DCHECK(success); | |
229 dict_update->SetInteger(it.key(), state); | |
230 it.Advance(); | |
231 } | |
232 } | |
233 | |
191 void AudioDevicesPrefHandlerImpl::MigrateDeviceMuteSettings( | 234 void AudioDevicesPrefHandlerImpl::MigrateDeviceMuteSettings( |
192 const std::string& active_device) { | 235 const std::string& active_device) { |
193 int old_mute = local_state_->GetInteger(prefs::kAudioMute); | 236 int old_mute = local_state_->GetInteger(prefs::kAudioMute); |
194 device_mute_settings_->SetInteger(active_device, old_mute); | 237 device_mute_settings_->SetInteger(active_device, old_mute); |
195 SaveDevicesMutePref(); | 238 SaveDevicesMutePref(); |
196 } | 239 } |
197 | 240 |
198 void AudioDevicesPrefHandlerImpl::MigrateDeviceVolumeSettings( | 241 void AudioDevicesPrefHandlerImpl::MigrateDeviceVolumeSettings( |
199 const std::string& active_device) { | 242 const std::string& active_device) { |
200 double old_volume = local_state_->GetDouble(prefs::kAudioVolumePercent); | 243 double old_volume = local_state_->GetDouble(prefs::kAudioVolumePercent); |
201 device_volume_settings_->SetDouble(active_device, old_volume); | 244 device_volume_settings_->SetDouble(active_device, old_volume); |
202 SaveDevicesVolumePref(); | 245 SaveDevicesVolumePref(); |
203 } | 246 } |
204 | 247 |
205 void AudioDevicesPrefHandlerImpl::NotifyAudioPolicyChange() { | 248 void AudioDevicesPrefHandlerImpl::NotifyAudioPolicyChange() { |
206 FOR_EACH_OBSERVER(AudioPrefObserver, | 249 FOR_EACH_OBSERVER(AudioPrefObserver, |
207 observers_, | 250 observers_, |
208 OnAudioPolicyPrefChanged()); | 251 OnAudioPolicyPrefChanged()); |
209 } | 252 } |
210 | 253 |
211 // static | 254 // static |
212 void AudioDevicesPrefHandlerImpl::RegisterPrefs(PrefRegistrySimple* registry) { | 255 void AudioDevicesPrefHandlerImpl::RegisterPrefs(PrefRegistrySimple* registry) { |
213 registry->RegisterDictionaryPref(prefs::kAudioDevicesVolumePercent); | 256 registry->RegisterDictionaryPref(prefs::kAudioDevicesVolumePercent); |
214 registry->RegisterDictionaryPref(prefs::kAudioDevicesMute); | 257 registry->RegisterDictionaryPref(prefs::kAudioDevicesMute); |
258 registry->RegisterDictionaryPref(prefs::kAudioDevicesState); | |
215 | 259 |
216 // Register the prefs backing the audio muting policies. | 260 // Register the prefs backing the audio muting policies. |
217 // Policy for audio input is handled by kAudioCaptureAllowed in the Chrome | 261 // Policy for audio input is handled by kAudioCaptureAllowed in the Chrome |
218 // media system. | 262 // media system. |
219 registry->RegisterBooleanPref(prefs::kAudioOutputAllowed, true); | 263 registry->RegisterBooleanPref(prefs::kAudioOutputAllowed, true); |
220 | 264 |
221 // Register the legacy audio prefs for migration. | 265 // Register the legacy audio prefs for migration. |
222 registry->RegisterDoublePref(prefs::kAudioVolumePercent, | 266 registry->RegisterDoublePref(prefs::kAudioVolumePercent, |
223 kDefaultOutputVolumePercent); | 267 kDefaultOutputVolumePercent); |
224 registry->RegisterIntegerPref(prefs::kAudioMute, kPrefMuteOff); | 268 registry->RegisterIntegerPref(prefs::kAudioMute, kPrefMuteOff); |
225 } | 269 } |
226 | 270 |
227 } // namespace chromeos | 271 } // namespace chromeos |
OLD | NEW |