Home | History | Annotate | Download | only in audio
      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 tray {
     53 
     54 AudioDetailedView::AudioDetailedView(SystemTrayItem* owner,
     55                                      user::LoginStatus login)
     56     : TrayDetailsView(owner),
     57       login_(login) {
     58   CreateItems();
     59   Update();
     60 }
     61 
     62 AudioDetailedView::~AudioDetailedView() {
     63 }
     64 
     65 void AudioDetailedView::Update() {
     66   UpdateAudioDevices();
     67   Layout();
     68 }
     69 
     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));
     75 
     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;
     85 
     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));
     93 
     94   scroll_content()->AddChildView(label);
     95 }
     96 
     97 HoverHighlightView* AudioDetailedView::AddScrollListItem(
     98     const base::string16& text,
     99     gfx::Font::FontStyle style,
    100     bool checked) {
    101   HoverHighlightView* container = new HoverHighlightView(this);
    102   container->AddCheckableLabel(text, style, checked);
    103   scroll_content()->AddChildView(container);
    104   return container;
    105 }
    106 
    107 void AudioDetailedView::CreateHeaderEntry() {
    108   CreateSpecialRow(IDS_ASH_STATUS_TRAY_AUDIO, this);
    109 }
    110 
    111 void AudioDetailedView::CreateItems() {
    112   CreateScrollableList();
    113   CreateHeaderEntry();
    114 }
    115 
    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     if (devices[i].is_input)
    123       input_devices_.push_back(devices[i]);
    124     else
    125       output_devices_.push_back(devices[i]);
    126   }
    127   UpdateScrollableList();
    128 }
    129 
    130 void AudioDetailedView::UpdateScrollableList() {
    131   scroll_content()->RemoveAllChildViews(true);
    132   device_map_.clear();
    133 
    134   // Add audio output devices.
    135   AddScrollListInfoItem(
    136       l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_AUDIO_OUTPUT));
    137   for (size_t i = 0; i < output_devices_.size(); ++i) {
    138     HoverHighlightView* container = AddScrollListItem(
    139         GetAudioDeviceName(output_devices_[i]),
    140         gfx::Font::NORMAL,
    141         output_devices_[i].active);  /* checkmark if active */
    142     device_map_[container] = output_devices_[i];
    143   }
    144 
    145   AddScrollSeparator();
    146 
    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]),
    153         gfx::Font::NORMAL,
    154         input_devices_[i].active);  /* checkmark if active */
    155     device_map_[container] = input_devices_[i];
    156   }
    157 
    158   scroll_content()->SizeToPreferredSize();
    159   scroller()->Layout();
    160 }
    161 
    162 void AudioDetailedView::OnViewClicked(views::View* sender) {
    163   if (sender == footer()->content()) {
    164     TransitionToDefaultView();
    165   } else {
    166     AudioDeviceMap::iterator iter = device_map_.find(sender);
    167     if (iter == device_map_.end())
    168       return;
    169     chromeos::AudioDevice& device = iter->second;
    170     CrasAudioHandler::Get()->SwitchToDevice(device);
    171   }
    172 }
    173 
    174 }  // namespace tray
    175 }  // namespace ash
    176