ProfilePolicyConnectorFactory: Refactoring from Profile to BrowserContext.
[chromium-blink-merge.git] / ash / system / chromeos / audio / audio_detailed_view.cc
blob3b023d86fd3711b2d13c42cf733adac7f495e3a4
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 default:
42 return base::UTF8ToUTF16(device.display_name);
46 } // namespace
48 using chromeos::CrasAudioHandler;
50 namespace ash {
51 namespace tray {
53 AudioDetailedView::AudioDetailedView(SystemTrayItem* owner)
54 : TrayDetailsView(owner) {
55 CreateItems();
56 Update();
59 AudioDetailedView::~AudioDetailedView() {
62 void AudioDetailedView::Update() {
63 UpdateAudioDevices();
64 Layout();
67 void AudioDetailedView::AddScrollListInfoItem(const base::string16& text) {
68 views::Label* label = new views::Label(
69 text,
70 ui::ResourceBundle::GetSharedInstance().GetFontList(
71 ui::ResourceBundle::BoldFont));
73 // Align info item with checkbox items
74 int margin = kTrayPopupPaddingHorizontal +
75 kTrayPopupDetailsLabelExtraLeftMargin;
76 int left_margin = 0;
77 int right_margin = 0;
78 if (base::i18n::IsRTL())
79 right_margin = margin;
80 else
81 left_margin = margin;
83 label->SetBorder(views::Border::CreateEmptyBorder(
84 ash::kTrayPopupPaddingBetweenItems,
85 left_margin,
86 ash::kTrayPopupPaddingBetweenItems,
87 right_margin));
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,
96 bool highlight,
97 bool checked) {
98 HoverHighlightView* container = new HoverHighlightView(this);
99 container->AddCheckableLabel(text, highlight, checked);
100 scroll_content()->AddChildView(container);
101 return container;
104 void AudioDetailedView::CreateHeaderEntry() {
105 CreateSpecialRow(IDS_ASH_STATUS_TRAY_AUDIO, this);
108 void AudioDetailedView::CreateItems() {
109 CreateScrollableList();
110 CreateHeaderEntry();
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)
122 continue;
123 if (devices[i].is_input)
124 input_devices_.push_back(devices[i]);
125 else
126 output_devices_.push_back(devices[i]);
128 UpdateScrollableList();
131 void AudioDetailedView::UpdateScrollableList() {
132 scroll_content()->RemoveAllChildViews(true);
133 device_map_.clear();
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();
164 } else {
165 AudioDeviceMap::iterator iter = device_map_.find(sender);
166 if (iter == device_map_.end())
167 return;
168 chromeos::AudioDevice& device = iter->second;
169 CrasAudioHandler::Get()->SwitchToDevice(device, true);
173 } // namespace tray
174 } // namespace ash