Home | History | Annotate | Download | only in ash
      1 // Copyright (c) 2012 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 "chrome/browser/ui/ash/volume_controller_chromeos.h"
      6 
      7 #include "ash/ash_switches.h"
      8 #include "ash/audio/sounds.h"
      9 #include "base/command_line.h"
     10 #include "chrome/browser/browser_process.h"
     11 #include "chrome/browser/extensions/api/system_private/system_private_api.h"
     12 #include "chromeos/audio/chromeos_sounds.h"
     13 #include "chromeos/chromeos_switches.h"
     14 #include "content/public/browser/user_metrics.h"
     15 #include "grit/browser_resources.h"
     16 #include "media/audio/sounds/sounds_manager.h"
     17 #include "ui/base/resource/resource_bundle.h"
     18 
     19 using chromeos::CrasAudioHandler;
     20 
     21 namespace {
     22 
     23 // Percent by which the volume should be changed when a volume key is pressed.
     24 const double kStepPercentage = 4.0;
     25 
     26 bool VolumeAdjustSoundEnabled() {
     27   return !CommandLine::ForCurrentProcess()->HasSwitch(
     28       chromeos::switches::kDisableVolumeAdjustSound);
     29 }
     30 
     31 void PlayVolumeAdjustSound() {
     32   if (VolumeAdjustSoundEnabled())
     33     ash::PlaySystemSoundIfSpokenFeedback(chromeos::SOUND_VOLUME_ADJUST);
     34 }
     35 
     36 }  // namespace
     37 
     38 VolumeController::VolumeController() {
     39   CrasAudioHandler::Get()->AddAudioObserver(this);
     40   ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
     41   if (VolumeAdjustSoundEnabled()) {
     42     media::SoundsManager::Get()->Initialize(
     43         chromeos::SOUND_VOLUME_ADJUST,
     44         bundle.GetRawDataResource(IDR_SOUND_VOLUME_ADJUST_WAV));
     45   }
     46 }
     47 
     48 VolumeController::~VolumeController() {
     49   if (CrasAudioHandler::IsInitialized())
     50     CrasAudioHandler::Get()->RemoveAudioObserver(this);
     51 }
     52 
     53 bool VolumeController::HandleVolumeMute(const ui::Accelerator& accelerator) {
     54   if (accelerator.key_code() == ui::VKEY_VOLUME_MUTE)
     55     content::RecordAction(base::UserMetricsAction("Accel_VolumeMute_F8"));
     56 
     57   CrasAudioHandler::Get()->SetOutputMute(true);
     58   return true;
     59 }
     60 
     61 bool VolumeController::HandleVolumeDown(const ui::Accelerator& accelerator) {
     62   if (accelerator.key_code() == ui::VKEY_VOLUME_DOWN)
     63     content::RecordAction(base::UserMetricsAction("Accel_VolumeDown_F9"));
     64 
     65   CrasAudioHandler* audio_handler = CrasAudioHandler::Get();
     66   if (audio_handler->IsOutputMuted()) {
     67     audio_handler->SetOutputVolumePercent(0);
     68   } else {
     69     audio_handler->AdjustOutputVolumeByPercent(-kStepPercentage);
     70     if (audio_handler->IsOutputVolumeBelowDefaultMuteLevel())
     71       audio_handler->SetOutputMute(true);
     72     else
     73       PlayVolumeAdjustSound();
     74   }
     75   return true;
     76 }
     77 
     78 bool VolumeController::HandleVolumeUp(const ui::Accelerator& accelerator) {
     79   if (accelerator.key_code() == ui::VKEY_VOLUME_UP)
     80     content::RecordAction(base::UserMetricsAction("Accel_VolumeUp_F10"));
     81 
     82   CrasAudioHandler* audio_handler = CrasAudioHandler::Get();
     83   bool play_sound = false;
     84   if (audio_handler->IsOutputMuted()) {
     85     audio_handler->SetOutputMute(false);
     86     audio_handler->AdjustOutputVolumeToAudibleLevel();
     87     play_sound = true;
     88   } else {
     89     play_sound = audio_handler->GetOutputVolumePercent() != 100;
     90     audio_handler->AdjustOutputVolumeByPercent(kStepPercentage);
     91   }
     92 
     93   if (play_sound)
     94     PlayVolumeAdjustSound();
     95   return true;
     96 }
     97 
     98 void VolumeController::OnOutputVolumeChanged() {
     99   CrasAudioHandler* audio_handler = CrasAudioHandler::Get();
    100   extensions::DispatchVolumeChangedEvent(
    101       audio_handler->GetOutputVolumePercent(),
    102       audio_handler->IsOutputMuted());
    103 }
    104 
    105 void VolumeController::OnOutputMuteChanged() {
    106   CrasAudioHandler* audio_handler = CrasAudioHandler::Get();
    107   extensions::DispatchVolumeChangedEvent(
    108       audio_handler->GetOutputVolumePercent(),
    109       audio_handler->IsOutputMuted());
    110 }
    111