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