1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/system/chromeos/audio/audio_detailed_view.h"
7 #include "ash/system/tray/fixed_sized_scroll_view.h"
8 #include "ash/system/tray/hover_highlight_view.h"
9 #include "ash/system/tray/tray_constants.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chromeos/audio/cras_audio_handler.h"
12 #include "grit/ash_strings.h"
13 #include "ui/base/l10n/l10n_util.h"
14 #include "ui/base/resource/resource_bundle.h"
15 #include "ui/views/border.h"
16 #include "ui/views/controls/label.h"
20 base::string16
GetAudioDeviceName(const chromeos::AudioDevice
& device
) {
21 switch (device
.type
) {
22 case chromeos::AUDIO_TYPE_HEADPHONE
:
23 return l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_AUDIO_HEADPHONE
);
24 case chromeos::AUDIO_TYPE_INTERNAL_SPEAKER
:
25 return l10n_util::GetStringUTF16(
26 IDS_ASH_STATUS_TRAY_AUDIO_INTERNAL_SPEAKER
);
27 case chromeos::AUDIO_TYPE_INTERNAL_MIC
:
28 return l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_AUDIO_INTERNAL_MIC
);
29 case chromeos::AUDIO_TYPE_USB
:
30 return l10n_util::GetStringFUTF16(
31 IDS_ASH_STATUS_TRAY_AUDIO_USB_DEVICE
,
32 base::UTF8ToUTF16(device
.display_name
));
33 case chromeos::AUDIO_TYPE_BLUETOOTH
:
34 return l10n_util::GetStringFUTF16(
35 IDS_ASH_STATUS_TRAY_AUDIO_BLUETOOTH_DEVICE
,
36 base::UTF8ToUTF16(device
.display_name
));
37 case chromeos::AUDIO_TYPE_HDMI
:
38 return l10n_util::GetStringFUTF16(
39 IDS_ASH_STATUS_TRAY_AUDIO_HDMI_DEVICE
,
40 base::UTF8ToUTF16(device
.display_name
));
41 case chromeos::AUDIO_TYPE_MIC
:
42 return l10n_util::GetStringUTF16(
43 IDS_ASH_STATUS_TRAY_AUDIO_MIC_JACK_DEVICE
);
45 return base::UTF8ToUTF16(device
.display_name
);
51 using chromeos::CrasAudioHandler
;
56 AudioDetailedView::AudioDetailedView(SystemTrayItem
* owner
)
57 : TrayDetailsView(owner
) {
62 AudioDetailedView::~AudioDetailedView() {
65 void AudioDetailedView::Update() {
70 void AudioDetailedView::AddScrollListInfoItem(const base::string16
& text
) {
71 views::Label
* label
= new views::Label(
73 ui::ResourceBundle::GetSharedInstance().GetFontList(
74 ui::ResourceBundle::BoldFont
));
76 // Align info item with checkbox items
77 int margin
= kTrayPopupPaddingHorizontal
+
78 kTrayPopupDetailsLabelExtraLeftMargin
;
81 if (base::i18n::IsRTL())
82 right_margin
= margin
;
86 label
->SetBorder(views::Border::CreateEmptyBorder(
87 ash::kTrayPopupPaddingBetweenItems
,
89 ash::kTrayPopupPaddingBetweenItems
,
91 label
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
92 label
->SetEnabledColor(SkColorSetARGB(192, 0, 0, 0));
94 scroll_content()->AddChildView(label
);
97 HoverHighlightView
* AudioDetailedView::AddScrollListItem(
98 const base::string16
& text
,
101 HoverHighlightView
* container
= new HoverHighlightView(this);
102 container
->AddCheckableLabel(text
, highlight
, checked
);
103 scroll_content()->AddChildView(container
);
107 void AudioDetailedView::CreateHeaderEntry() {
108 CreateSpecialRow(IDS_ASH_STATUS_TRAY_AUDIO
, this);
111 void AudioDetailedView::CreateItems() {
112 CreateScrollableList();
116 void AudioDetailedView::UpdateAudioDevices() {
117 output_devices_
.clear();
118 input_devices_
.clear();
119 chromeos::AudioDeviceList devices
;
120 CrasAudioHandler::Get()->GetAudioDevices(&devices
);
121 for (size_t i
= 0; i
< devices
.size(); ++i
) {
122 // Don't display keyboard mic or aokr type.
123 if (!devices
[i
].is_for_simple_usage())
125 if (devices
[i
].is_input
)
126 input_devices_
.push_back(devices
[i
]);
128 output_devices_
.push_back(devices
[i
]);
130 UpdateScrollableList();
133 void AudioDetailedView::UpdateScrollableList() {
134 scroll_content()->RemoveAllChildViews(true);
137 // Add audio output devices.
138 AddScrollListInfoItem(
139 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_AUDIO_OUTPUT
));
140 for (size_t i
= 0; i
< output_devices_
.size(); ++i
) {
141 HoverHighlightView
* container
= AddScrollListItem(
142 GetAudioDeviceName(output_devices_
[i
]), false /* highlight */,
143 output_devices_
[i
].active
); /* checkmark if active */
144 device_map_
[container
] = output_devices_
[i
];
147 AddScrollSeparator();
149 // Add audio input devices.
150 AddScrollListInfoItem(
151 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_AUDIO_INPUT
));
152 for (size_t i
= 0; i
< input_devices_
.size(); ++i
) {
153 HoverHighlightView
* container
= AddScrollListItem(
154 GetAudioDeviceName(input_devices_
[i
]), false /* highlight */,
155 input_devices_
[i
].active
); /* checkmark if active */
156 device_map_
[container
] = input_devices_
[i
];
159 scroll_content()->SizeToPreferredSize();
160 scroller()->Layout();
163 void AudioDetailedView::OnViewClicked(views::View
* sender
) {
164 if (sender
== footer()->content()) {
165 TransitionToDefaultView();
167 AudioDeviceMap::iterator iter
= device_map_
.find(sender
);
168 if (iter
== device_map_
.end())
170 chromeos::AudioDevice
& device
= iter
->second
;
171 CrasAudioHandler::Get()->SwitchToDevice(device
, true);