| 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 } | |
| 78 | |
| 79 void SpeechInputExtensionNotification::DrawVolume( | |
| 80 SkCanvas* canvas, | |
| 81 const SkBitmap& bitmap, | |
| 82 float volume) { | |
| 83 buffer_image_->eraseARGB(0, 0, 0, 0); | |
| 84 | |
| 85 int width = mic_image_->width(); | |
| 86 int height = mic_image_->height(); | |
| 87 SkCanvas buffer_canvas(*buffer_image_); | |
| 88 | |
| 89 SkScalar clip_top = | |
| 90 (((1.0f - volume) * (height * (kVolumeSteps + 1))) - height) / | |
| 91 kVolumeSteps; | |
| 92 buffer_canvas.clipRect(SkRect::MakeLTRB(0, clip_top, | |
| 93 SkIntToScalar(width), SkIntToScalar(height))); | |
| 94 buffer_canvas.drawBitmap(bitmap, 0, 0); | |
| 95 | |
| 96 canvas->drawBitmap(*buffer_image_.get(), 0, 0); | |
| 97 } | |
| 98 | |
| 99 void SpeechInputExtensionNotification::SetVUMeterVolume(float volume) { | |
| 100 if (!tray_icon_) | |
| 101 return; | |
| 102 | |
| 103 mic_image_->eraseARGB(0, 0, 0, 0); | |
| 104 SkCanvas canvas(*mic_image_); | |
| 105 | |
| 106 // Draw the empty volume image first and the current volume image on top. | |
| 107 canvas.drawBitmap(*g_images.Get().mic_empty(), 0, 0); | |
| 108 DrawVolume(&canvas, *g_images.Get().mic_full(), volume); | |
| 109 | |
| 110 tray_icon_->SetImage(*mic_image_.get()); | |
| 111 } | |
| 112 | |
| 113 void SpeechInputExtensionNotification::Show(const Extension* extension, | |
| 114 bool show_balloon) { | |
| 115 if (StatusTray* status_tray = g_browser_process->status_tray()) { | |
| 116 DCHECK(!tray_icon_); | |
| 117 tray_icon_ = status_tray->CreateStatusIcon(); | |
| 118 DCHECK(tray_icon_); | |
| 119 VLOG(1) << "Tray icon added."; | |
| 120 | |
| 121 SetVUMeterVolume(0.0f); | |
| 122 tray_icon_->SetToolTip(l10n_util::GetStringFUTF16( | |
| 123 IDS_SPEECH_INPUT_TRAY_TOOLTIP, | |
| 124 UTF8ToUTF16(extension->name()), | |
| 125 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME))); | |
| 126 } else { | |
| 127 VLOG(1) << "This platform doesn't support notification icons."; | |
| 128 return; | |
| 129 } | |
| 130 | |
| 131 if (show_balloon) | |
| 132 ShowNotificationBalloon(extension); | |
| 133 } | |
| 134 | |
| 135 void SpeechInputExtensionNotification::Hide() { | |
| 136 if (StatusTray* status_tray = g_browser_process->status_tray()) { | |
| 137 DCHECK(tray_icon_); | |
| 138 status_tray->RemoveStatusIcon(tray_icon_); | |
| 139 tray_icon_ = NULL; | |
| 140 VLOG(1) << "Tray icon removed."; | |
| 141 } | |
| 142 } | |
| 143 | |
| 144 void SpeechInputExtensionNotification::ShowNotificationBalloon( | |
| 145 const Extension* extension) { | |
| 146 string16 title = l10n_util::GetStringUTF16( | |
| 147 IDS_SPEECH_INPUT_TRAY_BALLOON_TITLE); | |
| 148 string16 message = l10n_util::GetStringFUTF16( | |
| 149 IDS_SPEECH_INPUT_TRAY_BALLOON_BODY, | |
| 150 UTF8ToUTF16(extension->name()), | |
| 151 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)); | |
| 152 | |
| 153 tray_icon_->DisplayBalloon(*g_images.Get().balloon_icon(), title, message); | |
| 154 } | |
| OLD | NEW |