Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(840)

Side by Side Diff: ui/arc/notification/arc_notification_item.cc

Issue 1883473002: arc: Support more types of notifications. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/arc/notification/arc_notification_item.h" 5 #include "ui/arc/notification/arc_notification_item.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/strings/string16.h" 10 #include "base/strings/string16.h"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "base/task_runner.h" 12 #include "base/task_runner.h"
13 #include "base/task_runner_util.h" 13 #include "base/task_runner_util.h"
14 #include "base/threading/worker_pool.h" 14 #include "base/threading/worker_pool.h"
15 #include "third_party/skia/include/core/SkCanvas.h"
16 #include "third_party/skia/include/core/SkPaint.h"
15 #include "ui/gfx/codec/png_codec.h" 17 #include "ui/gfx/codec/png_codec.h"
16 #include "ui/gfx/geometry/size.h" 18 #include "ui/gfx/geometry/size.h"
17 #include "ui/gfx/image/image.h" 19 #include "ui/gfx/image/image.h"
20 #include "ui/gfx/text_elider.h"
18 #include "ui/message_center/message_center_style.h" 21 #include "ui/message_center/message_center_style.h"
19 #include "ui/message_center/notification.h" 22 #include "ui/message_center/notification.h"
20 #include "ui/message_center/notification_types.h" 23 #include "ui/message_center/notification_types.h"
21 #include "ui/message_center/notifier_settings.h" 24 #include "ui/message_center/notifier_settings.h"
22 25
23 namespace arc { 26 namespace arc {
24 27
25 namespace { 28 namespace {
26 29
27 static const char kNotifierId[] = "ARC_NOTIFICATION"; 30 static const char kNotifierId[] = "ARC_NOTIFICATION";
28 31
29 static const char kNotificationIdPrefix[] = "ARC_NOTIFICATION_"; 32 static const char kNotificationIdPrefix[] = "ARC_NOTIFICATION_";
30 33
31 SkBitmap DecodeImage(const std::vector<uint8_t>& data) { 34 SkBitmap DecodeImage(const std::vector<uint8_t>& data) {
32 DCHECK(base::WorkerPool::RunsTasksOnCurrentThread()); 35 DCHECK(base::WorkerPool::RunsTasksOnCurrentThread());
33 DCHECK(!data.empty()); // empty string should be handled in caller. 36 DCHECK(!data.empty()); // empty string should be handled in caller.
34 37
35 // We may decode an image in the browser process since it has been generated 38 // We may decode an image in the browser process since it has been generated
36 // in NotificationListerService in Android and should be safe. 39 // in NotificationListerService in Android and should be safe.
37 SkBitmap bitmap; 40 SkBitmap bitmap;
38 gfx::PNGCodec::Decode(&data[0], data.size(), &bitmap); 41 gfx::PNGCodec::Decode(&data[0], data.size(), &bitmap);
39 return bitmap; 42 return bitmap;
40 } 43 }
41 44
45 SkBitmap ConvertBitmap(
46 const mojo::StructPtr<ArcNotificationBitmap>& arcBitmap) {
dcheng 2016/04/13 23:55:20 1) This seems like it should be a type converter o
yoshiki 2016/04/15 00:32:34 Thanks. I made it a type converter.
47 if (arcBitmap.is_null())
48 return SkBitmap();
49
50 // Creates the SkBitmap object which wraps the arc bitmap pixels.
51 SkBitmap bitmap;
52 if (!bitmap.installPixels(
53 SkImageInfo::Make(arcBitmap->width, arcBitmap->height,
54 kRGBA_8888_SkColorType, kPremul_SkAlphaType),
55 &arcBitmap->pixel_data[0], (arcBitmap->width * 4))) {
dcheng 2016/04/13 23:55:20 This doesn't work if pixel_data has no data. mojo
yoshiki 2016/04/15 00:32:34 Done.
56 return SkBitmap();
57 }
58
59 // Copy the pixels with converting color type.
60 SkBitmap nativeColorBitmap;
61 if (!bitmap.copyTo(&nativeColorBitmap, kN32_SkColorType))
62 return SkBitmap();
63
64 return nativeColorBitmap;
65 }
66
67 SkBitmap CropImage(const SkBitmap& original_bitmap) {
68 DCHECK_NE(0, original_bitmap.width());
69 DCHECK_NE(0, original_bitmap.height());
70
71 const SkSize container_size = SkSize::Make(
72 message_center::kNotificationPreferredImageWidth,
73 message_center::kNotificationPreferredImageHeight);
74 const float container_aspect_ratio =
75 static_cast<float>(message_center::kNotificationPreferredImageWidth) /
76 message_center::kNotificationPreferredImageHeight;
77 const float image_aspect_ratio =
78 static_cast<float>(original_bitmap.width()) / original_bitmap.height();
79
80 SkRect source_rect;
81 if (image_aspect_ratio > container_aspect_ratio) {
82 float width = original_bitmap.height() * container_aspect_ratio;
83 source_rect = SkRect::MakeXYWH((original_bitmap.width() - width) / 2,
84 0,
85 width,
86 original_bitmap.height());
87 } else {
88 float height = original_bitmap.width() / container_aspect_ratio;
89 source_rect = SkRect::MakeXYWH(0,
90 (original_bitmap.height() - height) / 2,
91 original_bitmap.width(),
92 height);
93 }
94
95 SkBitmap container_bitmap;
96 container_bitmap.allocN32Pixels(container_size.width(),
97 container_size.height());
98 SkPaint paint;
99 paint.setFilterQuality(kHigh_SkFilterQuality);
100 SkCanvas container_image(container_bitmap);
101 container_image.drawColor(message_center::kImageBackgroundColor);
102 container_image.drawBitmapRect(
103 original_bitmap, source_rect, SkRect::MakeSize(container_size), &paint);
104
105 return container_bitmap;
106 }
107
42 class ArcNotificationDelegate : public message_center::NotificationDelegate { 108 class ArcNotificationDelegate : public message_center::NotificationDelegate {
43 public: 109 public:
44 explicit ArcNotificationDelegate(base::WeakPtr<ArcNotificationItem> item) 110 explicit ArcNotificationDelegate(base::WeakPtr<ArcNotificationItem> item)
45 : item_(item) {} 111 : item_(item) {}
46 112
47 void Close(bool by_user) override { 113 void Close(bool by_user) override {
48 if (item_) 114 if (item_)
49 item_->Close(by_user); 115 item_->Close(by_user);
50 } 116 }
51 117
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 return; 171 return;
106 } 172 }
107 173
108 message_center::RichNotificationData rich_data; 174 message_center::RichNotificationData rich_data;
109 message_center::NotificationType type; 175 message_center::NotificationType type;
110 176
111 switch (data.type) { 177 switch (data.type) {
112 case ArcNotificationType::BASIC: 178 case ArcNotificationType::BASIC:
113 type = message_center::NOTIFICATION_TYPE_BASE_FORMAT; 179 type = message_center::NOTIFICATION_TYPE_BASE_FORMAT;
114 break; 180 break;
181 case ArcNotificationType::LIST:
182 type = message_center::NOTIFICATION_TYPE_MULTIPLE;
183
184 if (data.texts.is_null())
185 break;
186
187 for (size_t i = 0;
188 i < std::min(data.texts.size(),
189 message_center::kNotificationMaximumItems - 1);
190 i++) {
191 rich_data.items.emplace_back(
192 base::string16(), base::UTF8ToUTF16(data.texts.at(i).get()));
193 }
194
195 if (data.texts.size() > message_center::kNotificationMaximumItems) {
196 // Show an elipsis as the 5th item if there are more than 5 items.
197 rich_data.items.emplace_back(base::string16(), gfx::kEllipsisUTF16);
198 } else if (data.texts.size() ==
199 message_center::kNotificationMaximumItems) {
200 // Show the 5th item if there are exact 5 items.
201 rich_data.items.emplace_back(
202 base::string16(),
203 base::UTF8ToUTF16(data.texts.at(data.texts.size() - 1).get()));
204 }
205 break;
115 case ArcNotificationType::IMAGE: 206 case ArcNotificationType::IMAGE:
116 // TODO(yoshiki): Implement this types. 207 type = message_center::NOTIFICATION_TYPE_IMAGE;
117 type = message_center::NOTIFICATION_TYPE_BASE_FORMAT; 208
118 LOG(ERROR) << "Unsupported notification type: image"; 209 if (!data.big_picture.is_null()) {
210 rich_data.image = gfx::Image::CreateFrom1xBitmap(
211 CropImage(ConvertBitmap(data.big_picture)));
212 }
119 break; 213 break;
120 case ArcNotificationType::PROGRESS: 214 case ArcNotificationType::PROGRESS:
121 type = message_center::NOTIFICATION_TYPE_PROGRESS; 215 type = message_center::NOTIFICATION_TYPE_PROGRESS;
122 rich_data.timestamp = base::Time::UnixEpoch() + 216 rich_data.timestamp = base::Time::UnixEpoch() +
123 base::TimeDelta::FromMilliseconds(data.time); 217 base::TimeDelta::FromMilliseconds(data.time);
124 rich_data.progress = std::max( 218 rich_data.progress = std::max(
125 0, std::min(100, static_cast<int>(std::round( 219 0, std::min(100, static_cast<int>(std::round(
126 static_cast<float>(data.progress_current) / 220 static_cast<float>(data.progress_current) /
127 data.progress_max * 100)))); 221 data.progress_max * 100))));
128 break; 222 break;
129 } 223 }
130 DCHECK(IsKnownEnumValue(data.type)) << "Unsupported notification type: " 224 DCHECK(IsKnownEnumValue(data.type)) << "Unsupported notification type: "
131 << data.type; 225 << data.type;
132 226
133 for (size_t i = 0; i < data.buttons.size(); i++) { 227 for (size_t i = 0; i < data.buttons.size(); i++) {
134 rich_data.buttons.push_back(message_center::ButtonInfo( 228 rich_data.buttons.emplace_back(
135 base::UTF8ToUTF16(data.buttons.at(i)->label.get()))); 229 base::UTF8ToUTF16(data.buttons.at(i)->label.get()));
136 } 230 }
137 231
138 // If the client is old (version < 1), both |no_clear| and |ongoing_event| 232 // If the client is old (version < 1), both |no_clear| and |ongoing_event|
139 // are false. 233 // are false.
140 rich_data.pinned = (data.no_clear || data.ongoing_event); 234 rich_data.pinned = (data.no_clear || data.ongoing_event);
141 235
142 // The identifier of the notifier, which is used to distinguish the notifiers 236 // The identifier of the notifier, which is used to distinguish the notifiers
143 // in the message center. 237 // in the message center.
144 message_center::NotifierId notifier_id( 238 message_center::NotifierId notifier_id(
145 message_center::NotifierId::SYSTEM_COMPONENT, kNotifierId); 239 message_center::NotifierId::SYSTEM_COMPONENT, kNotifierId);
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 message_center_->AddNotification(std::move(notification_)); 302 message_center_->AddNotification(std::move(notification_));
209 303
210 if (newer_data_) { 304 if (newer_data_) {
211 // There is the newer data, so updates again. 305 // There is the newer data, so updates again.
212 ArcNotificationDataPtr data(std::move(newer_data_)); 306 ArcNotificationDataPtr data(std::move(newer_data_));
213 UpdateWithArcNotificationData(*data); 307 UpdateWithArcNotificationData(*data);
214 } 308 }
215 } 309 }
216 310
217 } // namespace arc 311 } // namespace arc
OLDNEW
« components/arc/common/notifications.mojom ('K') | « components/arc/common/notifications.mojom ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698