Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(319)

Side by Side Diff: ash/system/chromeos/audio/audio_detailed_view.cc

Issue 163953007: Refactor the TrayAudio code so that it can be used by other platforms. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix memory leak by using scoped_ptr in TrayAudio Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ash/system/chromeos/audio/audio_detailed_view.h ('k') | ash/system/chromeos/audio/tray_audio.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "ash/system/chromeos/audio/audio_detailed_view.h"
6
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"
18
19 namespace {
20
21 base::string16 GetAudioDeviceName(const chromeos::AudioDevice& device) {
22 switch(device.type) {
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));
42 default:
43 return base::UTF8ToUTF16(device.display_name);
44 }
45 }
46
47 } // namespace
48
49 using chromeos::CrasAudioHandler;
50
51 namespace ash {
52 namespace internal {
53 namespace tray {
54
55 AudioDetailedView::AudioDetailedView(SystemTrayItem* owner,
56 user::LoginStatus login)
57 : TrayDetailsView(owner),
58 login_(login) {
59 CreateItems();
60 Update();
61 }
62
63 AudioDetailedView::~AudioDetailedView() {
64 }
65
66 void AudioDetailedView::Update() {
67 UpdateAudioDevices();
68 Layout();
69 }
70
71 void AudioDetailedView::AddScrollListInfoItem(const base::string16& text) {
72 views::Label* label = new views::Label(
73 text,
74 ui::ResourceBundle::GetSharedInstance().GetFontList(
75 ui::ResourceBundle::BoldFont));
76
77 // Align info item with checkbox items
78 int margin = kTrayPopupPaddingHorizontal +
79 kTrayPopupDetailsLabelExtraLeftMargin;
80 int left_margin = 0;
81 int right_margin = 0;
82 if (base::i18n::IsRTL())
83 right_margin = margin;
84 else
85 left_margin = margin;
86
87 label->SetBorder(views::Border::CreateEmptyBorder(
88 ash::kTrayPopupPaddingBetweenItems,
89 left_margin,
90 ash::kTrayPopupPaddingBetweenItems,
91 right_margin));
92 label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
93 label->SetEnabledColor(SkColorSetARGB(192, 0, 0, 0));
94
95 scroll_content()->AddChildView(label);
96 }
97
98 HoverHighlightView* AudioDetailedView::AddScrollListItem(
99 const base::string16& text,
100 gfx::Font::FontStyle style,
101 bool checked) {
102 HoverHighlightView* container = new HoverHighlightView(this);
103 container->AddCheckableLabel(text, style, checked);
104 scroll_content()->AddChildView(container);
105 return container;
106 }
107
108 void AudioDetailedView::CreateHeaderEntry() {
109 CreateSpecialRow(IDS_ASH_STATUS_TRAY_AUDIO, this);
110 }
111
112 void AudioDetailedView::CreateItems() {
113 CreateScrollableList();
114 CreateHeaderEntry();
115 }
116
117 void AudioDetailedView::UpdateAudioDevices() {
118 output_devices_.clear();
119 input_devices_.clear();
120 chromeos::AudioDeviceList devices;
121 CrasAudioHandler::Get()->GetAudioDevices(&devices);
122 for (size_t i = 0; i < devices.size(); ++i) {
123 if (devices[i].is_input)
124 input_devices_.push_back(devices[i]);
125 else
126 output_devices_.push_back(devices[i]);
127 }
128 UpdateScrollableList();
129 }
130
131 void AudioDetailedView::UpdateScrollableList() {
132 scroll_content()->RemoveAllChildViews(true);
133 device_map_.clear();
134
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]),
141 gfx::Font::NORMAL,
142 output_devices_[i].active); /* checkmark if active */
143 device_map_[container] = output_devices_[i];
144 }
145
146 AddScrollSeparator();
147
148 // Add audio input devices.
149 AddScrollListInfoItem(
150 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_AUDIO_INPUT));
151 for (size_t i = 0; i < input_devices_.size(); ++i) {
152 HoverHighlightView* container = AddScrollListItem(
153 GetAudioDeviceName(input_devices_[i]),
154 gfx::Font::NORMAL,
155 input_devices_[i].active); /* checkmark if active */
156 device_map_[container] = input_devices_[i];
157 }
158
159 scroll_content()->SizeToPreferredSize();
160 scroller()->Layout();
161 }
162
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);
172 }
173 }
174
175 } // namespace tray
176 } // namespace internal
177 } // namespace ash
OLDNEW
« no previous file with comments | « ash/system/chromeos/audio/audio_detailed_view.h ('k') | ash/system/chromeos/audio/tray_audio.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698