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_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"
     17 
     18 namespace {
     19 
     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);
     43   }
     44 }
     45 
     46 }  // namespace
     47 
     48 using chromeos::CrasAudioHandler;
     49 
     50 namespace ash {
     51 namespace tray {
     52 
     53 AudioDetailedView::AudioDetailedView(SystemTrayItem* owner,
     54                                      user::LoginStatus login)
     55     : TrayDetailsView(owner),
     56       login_(login) {
     57   CreateItems();
     58   Update();
     59 }
     60 
     61 AudioDetailedView::~AudioDetailedView() {
     62 }
     63 
     64 void AudioDetailedView::Update() {
     65   UpdateAudioDevices();
     66   Layout();
     67 }
     68 
     69 void AudioDetailedView::AddScrollListInfoItem(const base::string16& text) {
     70   views::Label* label = new views::Label(
     71       text,
     72       ui::ResourceBundle::GetSharedInstance().GetFontList(
     73           ui::ResourceBundle::BoldFont));
     74 
     75   //  Align info item with checkbox items
     76   int margin = kTrayPopupPaddingHorizontal +
     77       kTrayPopupDetailsLabelExtraLeftMargin;
     78   int left_margin = 0;
     79   int right_margin = 0;
     80   if (base::i18n::IsRTL())
     81     right_margin = margin;
     82   else
     83     left_margin = margin;
     84 
     85   label->SetBorder(views::Border::CreateEmptyBorder(
     86       ash::kTrayPopupPaddingBetweenItems,
     87       left_margin,
     88       ash::kTrayPopupPaddingBetweenItems,
     89       right_margin));
     90   label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
     91   label->SetEnabledColor(SkColorSetARGB(192, 0, 0, 0));
     92 
     93   scroll_content()->AddChildView(label);
     94 }
     95 
     96 HoverHighlightView* AudioDetailedView::AddScrollListItem(
     97     const base::string16& text,
     98     gfx::Font::FontStyle style,
     99     bool checked) {
    100   HoverHighlightView* container = new HoverHighlightView(this);
    101   container->AddCheckableLabel(text, style, checked);
    102   scroll_content()->AddChildView(container);
    103   return container;
    104 }
    105 
    106 void AudioDetailedView::CreateHeaderEntry() {
    107   CreateSpecialRow(IDS_ASH_STATUS_TRAY_AUDIO, this);
    108 }
    109 
    110 void AudioDetailedView::CreateItems() {
    111   CreateScrollableList();
    112   CreateHeaderEntry();
    113 }
    114 
    115 void AudioDetailedView::UpdateAudioDevices() {
    116   output_devices_.clear();
    117   input_devices_.clear();
    118   chromeos::AudioDeviceList devices;
    119   CrasAudioHandler::Get()->GetAudioDevices(&devices);
    120   for (size_t i = 0; i < devices.size(); ++i) {
    121     // Don't display keyboard mic.
    122     if (devices[i].type == chromeos::AUDIO_TYPE_KEYBOARD_MIC)
    123       continue;
    124     if (devices[i].is_input)
    125       input_devices_.push_back(devices[i]);
    126     else
    127       output_devices_.push_back(devices[i]);
    128   }
    129   UpdateScrollableList();
    130 }
    131 
    132 void AudioDetailedView::UpdateScrollableList() {
    133   scroll_content()->RemoveAllChildViews(true);
    134   device_map_.clear();
    135 
    136   // Add audio output devices.
    137   AddScrollListInfoItem(
    138       l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_AUDIO_OUTPUT));
    139   for (size_t i = 0; i < output_devices_.size(); ++i) {
    140     HoverHighlightView* container = AddScrollListItem(
    141         GetAudioDeviceName(output_devices_[i]),
    142         gfx::Font::NORMAL,
    143         output_devices_[i].active);  /* checkmark if active */
    144     device_map_[container] = output_devices_[i];
    145   }
    146 
    147   AddScrollSeparator();
    148 
    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]),
    155         gfx::Font::NORMAL,
    156         input_devices_[i].active);  /* checkmark if active */
    157     device_map_[container] = input_devices_[i];
    158   }
    159 
    160   scroll_content()->SizeToPreferredSize();
    161   scroller()->Layout();
    162 }
    163 
    164 void AudioDetailedView::OnViewClicked(views::View* sender) {
    165   if (sender == footer()->content()) {
    166     TransitionToDefaultView();
    167   } else {
    168     AudioDeviceMap::iterator iter = device_map_.find(sender);
    169     if (iter == device_map_.end())
    170       return;
    171     chromeos::AudioDevice& device = iter->second;
    172     CrasAudioHandler::Get()->SwitchToDevice(device, true);
    173   }
    174 }
    175 
    176 }  // namespace tray
    177 }  // namespace ash
    178