Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ash / system / chromeos / audio / audio_detailed_view.cc
blob2e769434773f2a65b11d9689f6b9f3361ec4215b
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"
18 namespace {
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);
44 default:
45 return base::UTF8ToUTF16(device.display_name);
49 } // namespace
51 using chromeos::CrasAudioHandler;
53 namespace ash {
54 namespace tray {
56 AudioDetailedView::AudioDetailedView(SystemTrayItem* owner)
57 : TrayDetailsView(owner) {
58 CreateItems();
59 Update();
62 AudioDetailedView::~AudioDetailedView() {
65 void AudioDetailedView::Update() {
66 UpdateAudioDevices();
67 Layout();
70 void AudioDetailedView::AddScrollListInfoItem(const base::string16& text) {
71 views::Label* label = new views::Label(
72 text,
73 ui::ResourceBundle::GetSharedInstance().GetFontList(
74 ui::ResourceBundle::BoldFont));
76 // Align info item with checkbox items
77 int margin = kTrayPopupPaddingHorizontal +
78 kTrayPopupDetailsLabelExtraLeftMargin;
79 int left_margin = 0;
80 int right_margin = 0;
81 if (base::i18n::IsRTL())
82 right_margin = margin;
83 else
84 left_margin = margin;
86 label->SetBorder(views::Border::CreateEmptyBorder(
87 ash::kTrayPopupPaddingBetweenItems,
88 left_margin,
89 ash::kTrayPopupPaddingBetweenItems,
90 right_margin));
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 bool highlight,
100 bool checked) {
101 HoverHighlightView* container = new HoverHighlightView(this);
102 container->AddCheckableLabel(text, highlight, checked);
103 scroll_content()->AddChildView(container);
104 return container;
107 void AudioDetailedView::CreateHeaderEntry() {
108 CreateSpecialRow(IDS_ASH_STATUS_TRAY_AUDIO, this);
111 void AudioDetailedView::CreateItems() {
112 CreateScrollableList();
113 CreateHeaderEntry();
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())
124 continue;
125 if (devices[i].is_input)
126 input_devices_.push_back(devices[i]);
127 else
128 output_devices_.push_back(devices[i]);
130 UpdateScrollableList();
133 void AudioDetailedView::UpdateScrollableList() {
134 scroll_content()->RemoveAllChildViews(true);
135 device_map_.clear();
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();
166 } else {
167 AudioDeviceMap::iterator iter = device_map_.find(sender);
168 if (iter == device_map_.end())
169 return;
170 chromeos::AudioDevice& device = iter->second;
171 CrasAudioHandler::Get()->SwitchToDevice(device, true);
175 } // namespace tray
176 } // namespace ash