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

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
« no previous file with comments | « components/arc/common/notifications.mojom ('k') | ui/message_center/message_center_style.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "components/arc/bitmap/bitmap_type_converters.h"
16 #include "third_party/skia/include/core/SkCanvas.h"
17 #include "third_party/skia/include/core/SkPaint.h"
15 #include "ui/gfx/codec/png_codec.h" 18 #include "ui/gfx/codec/png_codec.h"
16 #include "ui/gfx/geometry/size.h" 19 #include "ui/gfx/geometry/size.h"
17 #include "ui/gfx/image/image.h" 20 #include "ui/gfx/image/image.h"
21 #include "ui/gfx/text_elider.h"
18 #include "ui/message_center/message_center_style.h" 22 #include "ui/message_center/message_center_style.h"
19 #include "ui/message_center/notification.h" 23 #include "ui/message_center/notification.h"
20 #include "ui/message_center/notification_types.h" 24 #include "ui/message_center/notification_types.h"
21 #include "ui/message_center/notifier_settings.h" 25 #include "ui/message_center/notifier_settings.h"
22 26
23 namespace arc { 27 namespace arc {
24 28
25 namespace { 29 namespace {
26 30
27 static const char kNotifierId[] = "ARC_NOTIFICATION"; 31 static const char kNotifierId[] = "ARC_NOTIFICATION";
28 32
29 static const char kNotificationIdPrefix[] = "ARC_NOTIFICATION_"; 33 static const char kNotificationIdPrefix[] = "ARC_NOTIFICATION_";
30 34
31 SkBitmap DecodeImage(const std::vector<uint8_t>& data) { 35 SkBitmap DecodeImage(const std::vector<uint8_t>& data) {
32 DCHECK(base::WorkerPool::RunsTasksOnCurrentThread()); 36 DCHECK(base::WorkerPool::RunsTasksOnCurrentThread());
33 DCHECK(!data.empty()); // empty string should be handled in caller. 37 DCHECK(!data.empty()); // empty string should be handled in caller.
34 38
35 // We may decode an image in the browser process since it has been generated 39 // We may decode an image in the browser process since it has been generated
36 // in NotificationListerService in Android and should be safe. 40 // in NotificationListerService in Android and should be safe.
37 SkBitmap bitmap; 41 SkBitmap bitmap;
38 gfx::PNGCodec::Decode(&data[0], data.size(), &bitmap); 42 gfx::PNGCodec::Decode(&data[0], data.size(), &bitmap);
39 return bitmap; 43 return bitmap;
40 } 44 }
41 45
46 // Crop the image to proper size for Chrome Notification. It accepts only
hidehiko 2016/04/18 17:54:41 s/Crop/Crops/
yoshiki 2016/04/18 19:21:22 Done.
47 // specified aspect ratio. Otherwise, it might be letterboxed.
48 SkBitmap CropImage(const SkBitmap& original_bitmap) {
49 DCHECK_NE(0, original_bitmap.width());
50 DCHECK_NE(0, original_bitmap.height());
51
52 const SkSize container_size = SkSize::Make(
53 message_center::kNotificationPreferredImageWidth,
54 message_center::kNotificationPreferredImageHeight);
55 const float container_aspect_ratio =
56 static_cast<float>(message_center::kNotificationPreferredImageWidth) /
57 message_center::kNotificationPreferredImageHeight;
58 const float image_aspect_ratio =
59 static_cast<float>(original_bitmap.width()) / original_bitmap.height();
60
61 SkRect source_rect;
62 if (image_aspect_ratio > container_aspect_ratio) {
63 float width = original_bitmap.height() * container_aspect_ratio;
64 source_rect = SkRect::MakeXYWH((original_bitmap.width() - width) / 2,
65 0,
66 width,
67 original_bitmap.height());
68 } else {
69 float height = original_bitmap.width() / container_aspect_ratio;
70 source_rect = SkRect::MakeXYWH(0,
71 (original_bitmap.height() - height) / 2,
72 original_bitmap.width(),
73 height);
74 }
75
76 SkBitmap container_bitmap;
77 container_bitmap.allocN32Pixels(container_size.width(),
78 container_size.height());
79 SkPaint paint;
80 paint.setFilterQuality(kHigh_SkFilterQuality);
81 SkCanvas container_image(container_bitmap);
82 container_image.drawColor(message_center::kImageBackgroundColor);
83 container_image.drawBitmapRect(
84 original_bitmap, source_rect, SkRect::MakeSize(container_size), &paint);
85
86 return container_bitmap;
87 }
88
42 class ArcNotificationDelegate : public message_center::NotificationDelegate { 89 class ArcNotificationDelegate : public message_center::NotificationDelegate {
43 public: 90 public:
44 explicit ArcNotificationDelegate(base::WeakPtr<ArcNotificationItem> item) 91 explicit ArcNotificationDelegate(base::WeakPtr<ArcNotificationItem> item)
45 : item_(item) {} 92 : item_(item) {}
46 93
47 void Close(bool by_user) override { 94 void Close(bool by_user) override {
48 if (item_) 95 if (item_)
49 item_->Close(by_user); 96 item_->Close(by_user);
50 } 97 }
51 98
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 return; 152 return;
106 } 153 }
107 154
108 message_center::RichNotificationData rich_data; 155 message_center::RichNotificationData rich_data;
109 message_center::NotificationType type; 156 message_center::NotificationType type;
110 157
111 switch (data.type) { 158 switch (data.type) {
112 case ArcNotificationType::BASIC: 159 case ArcNotificationType::BASIC:
113 type = message_center::NOTIFICATION_TYPE_BASE_FORMAT; 160 type = message_center::NOTIFICATION_TYPE_BASE_FORMAT;
114 break; 161 break;
162 case ArcNotificationType::LIST:
163 type = message_center::NOTIFICATION_TYPE_MULTIPLE;
164
165 if (data.texts.is_null())
166 break;
167
168 for (size_t i = 0;
169 i < std::min(data.texts.size(),
170 message_center::kNotificationMaximumItems - 1);
171 i++) {
172 rich_data.items.emplace_back(
173 base::string16(), base::UTF8ToUTF16(data.texts.at(i).get()));
174 }
175
176 if (data.texts.size() > message_center::kNotificationMaximumItems) {
177 // Show an elipsis as the 5th item if there are more than 5 items.
178 rich_data.items.emplace_back(base::string16(), gfx::kEllipsisUTF16);
179 } else if (data.texts.size() ==
180 message_center::kNotificationMaximumItems) {
181 // Show the 5th item if there are exact 5 items.
182 rich_data.items.emplace_back(
183 base::string16(),
184 base::UTF8ToUTF16(data.texts.at(data.texts.size() - 1).get()));
185 }
186 break;
115 case ArcNotificationType::IMAGE: 187 case ArcNotificationType::IMAGE:
116 // TODO(yoshiki): Implement this types. 188 type = message_center::NOTIFICATION_TYPE_IMAGE;
117 type = message_center::NOTIFICATION_TYPE_BASE_FORMAT; 189
118 LOG(ERROR) << "Unsupported notification type: image"; 190 if (!data.big_picture.is_null()) {
191 rich_data.image = gfx::Image::CreateFrom1xBitmap(
192 CropImage(data.big_picture.To<SkBitmap>()));
193 }
119 break; 194 break;
120 case ArcNotificationType::PROGRESS: 195 case ArcNotificationType::PROGRESS:
121 type = message_center::NOTIFICATION_TYPE_PROGRESS; 196 type = message_center::NOTIFICATION_TYPE_PROGRESS;
122 rich_data.timestamp = base::Time::UnixEpoch() + 197 rich_data.timestamp = base::Time::UnixEpoch() +
123 base::TimeDelta::FromMilliseconds(data.time); 198 base::TimeDelta::FromMilliseconds(data.time);
124 rich_data.progress = std::max( 199 rich_data.progress = std::max(
125 0, std::min(100, static_cast<int>(std::round( 200 0, std::min(100, static_cast<int>(std::round(
126 static_cast<float>(data.progress_current) / 201 static_cast<float>(data.progress_current) /
127 data.progress_max * 100)))); 202 data.progress_max * 100))));
128 break; 203 break;
129 } 204 }
130 DCHECK(IsKnownEnumValue(data.type)) << "Unsupported notification type: " 205 DCHECK(IsKnownEnumValue(data.type)) << "Unsupported notification type: "
131 << data.type; 206 << data.type;
132 207
133 for (size_t i = 0; i < data.buttons.size(); i++) { 208 for (size_t i = 0; i < data.buttons.size(); i++) {
134 rich_data.buttons.push_back(message_center::ButtonInfo( 209 rich_data.buttons.emplace_back(
135 base::UTF8ToUTF16(data.buttons.at(i)->label.get()))); 210 base::UTF8ToUTF16(data.buttons.at(i)->label.get()));
136 } 211 }
137 212
138 // If the client is old (version < 1), both |no_clear| and |ongoing_event| 213 // If the client is old (version < 1), both |no_clear| and |ongoing_event|
139 // are false. 214 // are false.
140 rich_data.pinned = (data.no_clear || data.ongoing_event); 215 rich_data.pinned = (data.no_clear || data.ongoing_event);
141 216
142 // The identifier of the notifier, which is used to distinguish the notifiers 217 // The identifier of the notifier, which is used to distinguish the notifiers
143 // in the message center. 218 // in the message center.
144 message_center::NotifierId notifier_id( 219 message_center::NotifierId notifier_id(
145 message_center::NotifierId::SYSTEM_COMPONENT, kNotifierId); 220 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_)); 283 message_center_->AddNotification(std::move(notification_));
209 284
210 if (newer_data_) { 285 if (newer_data_) {
211 // There is the newer data, so updates again. 286 // There is the newer data, so updates again.
212 ArcNotificationDataPtr data(std::move(newer_data_)); 287 ArcNotificationDataPtr data(std::move(newer_data_));
213 UpdateWithArcNotificationData(*data); 288 UpdateWithArcNotificationData(*data);
214 } 289 }
215 } 290 }
216 291
217 } // namespace arc 292 } // namespace arc
OLDNEW
« no previous file with comments | « components/arc/common/notifications.mojom ('k') | ui/message_center/message_center_style.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698