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
));
42 return base::UTF8ToUTF16(device
.display_name
);
48 using chromeos::CrasAudioHandler
;
53 AudioDetailedView::AudioDetailedView(SystemTrayItem
* owner
)
54 : TrayDetailsView(owner
) {
59 AudioDetailedView::~AudioDetailedView() {
62 void AudioDetailedView::Update() {
67 void AudioDetailedView::AddScrollListInfoItem(const base::string16
& text
) {
68 views::Label
* label
= new views::Label(
70 ui::ResourceBundle::GetSharedInstance().GetFontList(
71 ui::ResourceBundle::BoldFont
));
73 // Align info item with checkbox items
74 int margin
= kTrayPopupPaddingHorizontal
+
75 kTrayPopupDetailsLabelExtraLeftMargin
;
78 if (base::i18n::IsRTL())
79 right_margin
= margin
;
83 label
->SetBorder(views::Border::CreateEmptyBorder(
84 ash::kTrayPopupPaddingBetweenItems
,
86 ash::kTrayPopupPaddingBetweenItems
,
88 label
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
89 label
->SetEnabledColor(SkColorSetARGB(192, 0, 0, 0));
91 scroll_content()->AddChildView(label
);
94 HoverHighlightView
* AudioDetailedView::AddScrollListItem(
95 const base::string16
& text
,
98 HoverHighlightView
* container
= new HoverHighlightView(this);
99 container
->AddCheckableLabel(text
, highlight
, checked
);
100 scroll_content()->AddChildView(container
);
104 void AudioDetailedView::CreateHeaderEntry() {
105 CreateSpecialRow(IDS_ASH_STATUS_TRAY_AUDIO
, this);
108 void AudioDetailedView::CreateItems() {
109 CreateScrollableList();
113 void AudioDetailedView::UpdateAudioDevices() {
114 output_devices_
.clear();
115 input_devices_
.clear();
116 chromeos::AudioDeviceList devices
;
117 CrasAudioHandler::Get()->GetAudioDevices(&devices
);
118 for (size_t i
= 0; i
< devices
.size(); ++i
) {
119 // Don't display keyboard mic or aokr type.
120 if (devices
[i
].type
== chromeos::AUDIO_TYPE_KEYBOARD_MIC
||
121 devices
[i
].type
== chromeos::AUDIO_TYPE_AOKR
)
123 if (devices
[i
].is_input
)
124 input_devices_
.push_back(devices
[i
]);
126 output_devices_
.push_back(devices
[i
]);
128 UpdateScrollableList();
131 void AudioDetailedView::UpdateScrollableList() {
132 scroll_content()->RemoveAllChildViews(true);
135 // Add audio output devices.
136 AddScrollListInfoItem(
137 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_AUDIO_OUTPUT
));
138 for (size_t i
= 0; i
< output_devices_
.size(); ++i
) {
139 HoverHighlightView
* container
= AddScrollListItem(
140 GetAudioDeviceName(output_devices_
[i
]), false /* highlight */,
141 output_devices_
[i
].active
); /* checkmark if active */
142 device_map_
[container
] = output_devices_
[i
];
145 AddScrollSeparator();
147 // Add audio input devices.
148 AddScrollListInfoItem(
149 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_AUDIO_INPUT
));
150 for (size_t i
= 0; i
< input_devices_
.size(); ++i
) {
151 HoverHighlightView
* container
= AddScrollListItem(
152 GetAudioDeviceName(input_devices_
[i
]), false /* highlight */,
153 input_devices_
[i
].active
); /* checkmark if active */
154 device_map_
[container
] = input_devices_
[i
];
157 scroll_content()->SizeToPreferredSize();
158 scroller()->Layout();
161 void AudioDetailedView::OnViewClicked(views::View
* sender
) {
162 if (sender
== footer()->content()) {
163 TransitionToDefaultView();
165 AudioDeviceMap::iterator iter
= device_map_
.find(sender
);
166 if (iter
== device_map_
.end())
168 chromeos::AudioDevice
& device
= iter
->second
;
169 CrasAudioHandler::Get()->SwitchToDevice(device
, true);