OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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/speech/speech_input_extension_notification.h" |
| 6 |
| 7 #include "base/lazy_instance.h" |
| 8 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/status_icons/status_icon.h" |
| 11 #include "chrome/browser/status_icons/status_tray.h" |
| 12 #include "chrome/common/extensions/extension.h" |
| 13 #include "grit/chromium_strings.h" |
| 14 #include "grit/generated_resources.h" |
| 15 #include "grit/theme_resources.h" |
| 16 #include "ui/base/l10n/l10n_util.h" |
| 17 #include "ui/base/resource/resource_bundle.h" |
| 18 #include "ui/gfx/canvas_skia.h" |
| 19 #include "ui/gfx/rect.h" |
| 20 #include "ui/gfx/skbitmap_operations.h" |
| 21 |
| 22 namespace { |
| 23 |
| 24 // Number of volume steps used when rendering the VU meter icon. |
| 25 const int kVolumeSteps = 6; |
| 26 |
| 27 // A lazily initialized singleton to hold all the images used by the |
| 28 // notification icon and safely destroy them on exit. |
| 29 class NotificationTrayImages { |
| 30 public: |
| 31 SkBitmap* mic_full() { return mic_full_; } |
| 32 SkBitmap* mic_empty() { return mic_empty_; } |
| 33 SkBitmap* balloon_icon() { return balloon_icon_; } |
| 34 |
| 35 private: |
| 36 // Private constructor to enforce singleton. |
| 37 friend struct base::DefaultLazyInstanceTraits<NotificationTrayImages>; |
| 38 NotificationTrayImages(); |
| 39 |
| 40 // These bitmaps are owned by ResourceBundle and need not be destroyed. |
| 41 SkBitmap* mic_full_; // Tray mic image with full volume. |
| 42 SkBitmap* mic_empty_; // Tray mic image with zero volume. |
| 43 SkBitmap* balloon_icon_; // High resolution mic for the notification balloon. |
| 44 }; |
| 45 |
| 46 NotificationTrayImages::NotificationTrayImages() { |
| 47 mic_empty_ = ResourceBundle::GetSharedInstance().GetBitmapNamed( |
| 48 IDR_SPEECH_INPUT_TRAY_MIC_EMPTY); |
| 49 mic_full_ = ResourceBundle::GetSharedInstance().GetBitmapNamed( |
| 50 IDR_SPEECH_INPUT_TRAY_MIC_FULL); |
| 51 balloon_icon_ = ResourceBundle::GetSharedInstance().GetBitmapNamed( |
| 52 IDR_SPEECH_INPUT_TRAY_BALLOON_ICON); |
| 53 } |
| 54 |
| 55 base::LazyInstance<NotificationTrayImages> g_images(base::LINKER_INITIALIZED); |
| 56 |
| 57 } // namespace |
| 58 |
| 59 SpeechInputExtensionNotification::SpeechInputExtensionNotification( |
| 60 Profile* profile) |
| 61 : profile_(profile), |
| 62 tray_icon_(NULL) { |
| 63 mic_image_.reset(new SkBitmap()); |
| 64 mic_image_->setConfig(SkBitmap::kARGB_8888_Config, |
| 65 g_images.Get().mic_empty()->width(), |
| 66 g_images.Get().mic_empty()->height()); |
| 67 mic_image_->allocPixels(); |
| 68 |
| 69 buffer_image_.reset(new SkBitmap()); |
| 70 buffer_image_->setConfig(SkBitmap::kARGB_8888_Config, |
| 71 g_images.Get().mic_empty()->width(), |
| 72 g_images.Get().mic_empty()->height()); |
| 73 buffer_image_->allocPixels(); |
| 74 } |
| 75 |
| 76 SpeechInputExtensionNotification::~SpeechInputExtensionNotification() { |
| 77 Hide(); |
| 78 } |
| 79 |
| 80 void SpeechInputExtensionNotification::DrawVolume( |
| 81 SkCanvas* canvas, |
| 82 const SkBitmap& bitmap, |
| 83 float volume) { |
| 84 buffer_image_->eraseARGB(0, 0, 0, 0); |
| 85 |
| 86 int width = mic_image_->width(); |
| 87 int height = mic_image_->height(); |
| 88 SkCanvas buffer_canvas(*buffer_image_); |
| 89 |
| 90 SkScalar clip_top = |
| 91 (((1.0f - volume) * (height * (kVolumeSteps + 1))) - height) / |
| 92 kVolumeSteps; |
| 93 buffer_canvas.clipRect(SkRect::MakeLTRB(0, clip_top, |
| 94 SkIntToScalar(width), SkIntToScalar(height))); |
| 95 buffer_canvas.drawBitmap(bitmap, 0, 0); |
| 96 |
| 97 canvas->drawBitmap(*buffer_image_.get(), 0, 0); |
| 98 } |
| 99 |
| 100 void SpeechInputExtensionNotification::SetVUMeterVolume(float volume) { |
| 101 if (!tray_icon_) |
| 102 return; |
| 103 |
| 104 mic_image_->eraseARGB(0, 0, 0, 0); |
| 105 SkCanvas canvas(*mic_image_); |
| 106 |
| 107 // Draw the empty volume image first and the current volume image on top. |
| 108 canvas.drawBitmap(*g_images.Get().mic_empty(), 0, 0); |
| 109 DrawVolume(&canvas, *g_images.Get().mic_full(), volume); |
| 110 |
| 111 tray_icon_->SetImage(*mic_image_.get()); |
| 112 } |
| 113 |
| 114 void SpeechInputExtensionNotification::Show(const Extension* extension, |
| 115 bool show_balloon) { |
| 116 if (StatusTray* status_tray = g_browser_process->status_tray()) { |
| 117 DCHECK(!tray_icon_); |
| 118 tray_icon_ = status_tray->CreateStatusIcon(); |
| 119 DCHECK(tray_icon_); |
| 120 VLOG(1) << "Tray icon added."; |
| 121 |
| 122 SetVUMeterVolume(0.0f); |
| 123 tray_icon_->SetToolTip(l10n_util::GetStringFUTF16( |
| 124 IDS_SPEECH_INPUT_TRAY_TOOLTIP, |
| 125 UTF8ToUTF16(extension->name()), |
| 126 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME))); |
| 127 } else { |
| 128 VLOG(1) << "This platform doesn't support notification icons."; |
| 129 return; |
| 130 } |
| 131 |
| 132 if (show_balloon) |
| 133 ShowNotificationBalloon(extension); |
| 134 } |
| 135 |
| 136 void SpeechInputExtensionNotification::Hide() { |
| 137 if (!tray_icon_) |
| 138 return; |
| 139 |
| 140 if (StatusTray* status_tray = g_browser_process->status_tray()) { |
| 141 status_tray->RemoveStatusIcon(tray_icon_); |
| 142 tray_icon_ = NULL; |
| 143 VLOG(1) << "Tray icon removed."; |
| 144 } |
| 145 } |
| 146 |
| 147 void SpeechInputExtensionNotification::ShowNotificationBalloon( |
| 148 const Extension* extension) { |
| 149 string16 title = l10n_util::GetStringUTF16( |
| 150 IDS_SPEECH_INPUT_TRAY_BALLOON_TITLE); |
| 151 string16 message = l10n_util::GetStringFUTF16( |
| 152 IDS_SPEECH_INPUT_TRAY_BALLOON_BODY, |
| 153 UTF8ToUTF16(extension->name()), |
| 154 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)); |
| 155 |
| 156 tray_icon_->DisplayBalloon(*g_images.Get().balloon_icon(), title, message); |
| 157 } |
OLD | NEW |