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_resources.h"
13 #include "grit/ash_strings.h"
14 #include "ui/base/l10n/l10n_util.h"
15 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/views/border.h"
17 #include "ui/views/controls/label.h"
21 base::string16
GetAudioDeviceName(const chromeos::AudioDevice
& device
) {
23 case chromeos::AUDIO_TYPE_HEADPHONE
:
24 return l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_AUDIO_HEADPHONE
);
25 case chromeos::AUDIO_TYPE_INTERNAL_SPEAKER
:
26 return l10n_util::GetStringUTF16(
27 IDS_ASH_STATUS_TRAY_AUDIO_INTERNAL_SPEAKER
);
28 case chromeos::AUDIO_TYPE_INTERNAL_MIC
:
29 return l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_AUDIO_INTERNAL_MIC
);
30 case chromeos::AUDIO_TYPE_USB
:
31 return l10n_util::GetStringFUTF16(
32 IDS_ASH_STATUS_TRAY_AUDIO_USB_DEVICE
,
33 base::UTF8ToUTF16(device
.display_name
));
34 case chromeos::AUDIO_TYPE_BLUETOOTH
:
35 return l10n_util::GetStringFUTF16(
36 IDS_ASH_STATUS_TRAY_AUDIO_BLUETOOTH_DEVICE
,
37 base::UTF8ToUTF16(device
.display_name
));
38 case chromeos::AUDIO_TYPE_HDMI
:
39 return l10n_util::GetStringFUTF16(
40 IDS_ASH_STATUS_TRAY_AUDIO_HDMI_DEVICE
,
41 base::UTF8ToUTF16(device
.display_name
));
43 return base::UTF8ToUTF16(device
.display_name
);
49 using chromeos::CrasAudioHandler
;
54 AudioDetailedView::AudioDetailedView(SystemTrayItem
* owner
,
55 user::LoginStatus login
)
56 : 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
,
99 gfx::Font::FontStyle style
,
101 HoverHighlightView
* container
= new HoverHighlightView(this);
102 container
->AddCheckableLabel(text
, style
, 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.
123 if (devices
[i
].type
== chromeos::AUDIO_TYPE_KEYBOARD_MIC
)
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
]),
144 output_devices_
[i
].active
); /* checkmark if active */
145 device_map_
[container
] = output_devices_
[i
];
148 AddScrollSeparator();
150 // Add audio input devices.
151 AddScrollListInfoItem(
152 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_AUDIO_INPUT
));
153 for (size_t i
= 0; i
< input_devices_
.size(); ++i
) {
154 HoverHighlightView
* container
= AddScrollListItem(
155 GetAudioDeviceName(input_devices_
[i
]),
157 input_devices_
[i
].active
); /* checkmark if active */
158 device_map_
[container
] = input_devices_
[i
];
161 scroll_content()->SizeToPreferredSize();
162 scroller()->Layout();
165 void AudioDetailedView::OnViewClicked(views::View
* sender
) {
166 if (sender
== footer()->content()) {
167 TransitionToDefaultView();
169 AudioDeviceMap::iterator iter
= device_map_
.find(sender
);
170 if (iter
== device_map_
.end())
172 chromeos::AudioDevice
& device
= iter
->second
;
173 CrasAudioHandler::Get()->SwitchToDevice(device
);