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

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: Fix build failure 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 "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 SkBitmap CropImage(const SkBitmap& original_bitmap) {
hidehiko 2016/04/15 07:09:10 Any function comments? How to be cropped?
yoshiki 2016/04/15 14:36:39 Added comment.
47 DCHECK_NE(0, original_bitmap.width());
48 DCHECK_NE(0, original_bitmap.height());
49
50 const SkSize container_size = SkSize::Make(
51 message_center::kNotificationPreferredImageWidth,
52 message_center::kNotificationPreferredImageHeight);
53 const float container_aspect_ratio =
54 static_cast<float>(message_center::kNotificationPreferredImageWidth) /
55 message_center::kNotificationPreferredImageHeight;
56 const float image_aspect_ratio =
57 static_cast<float>(original_bitmap.width()) / original_bitmap.height();
58
59 SkRect source_rect;
60 if (image_aspect_ratio > container_aspect_ratio) {
61 float width = original_bitmap.height() * container_aspect_ratio;
62 source_rect = SkRect::MakeXYWH((original_bitmap.width() - width) / 2,
63 0,
64 width,
65 original_bitmap.height());
66 } else {
67 float height = original_bitmap.width() / container_aspect_ratio;
68 source_rect = SkRect::MakeXYWH(0,
69 (original_bitmap.height() - height) / 2,
70 original_bitmap.width(),
71 height);
72 }
73
74 SkBitmap container_bitmap;
75 container_bitmap.allocN32Pixels(container_size.width(),
76 container_size.height());
77 SkPaint paint;
78 paint.setFilterQuality(kHigh_SkFilterQuality);
79 SkCanvas container_image(container_bitmap);
80 container_image.drawColor(message_center::kImageBackgroundColor);
81 container_image.drawBitmapRect(
82 original_bitmap, source_rect, SkRect::MakeSize(container_size), &paint);
83
84 return container_bitmap;
85 }
86
42 class ArcNotificationDelegate : public message_center::NotificationDelegate { 87 class ArcNotificationDelegate : public message_center::NotificationDelegate {
43 public: 88 public:
44 explicit ArcNotificationDelegate(base::WeakPtr<ArcNotificationItem> item) 89 explicit ArcNotificationDelegate(base::WeakPtr<ArcNotificationItem> item)
45 : item_(item) {} 90 : item_(item) {}
46 91
47 void Close(bool by_user) override { 92 void Close(bool by_user) override {
48 if (item_) 93 if (item_)
49 item_->Close(by_user); 94 item_->Close(by_user);
50 } 95 }
51 96
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 return; 150 return;
106 } 151 }
107 152
108 message_center::RichNotificationData rich_data; 153 message_center::RichNotificationData rich_data;
109 message_center::NotificationType type; 154 message_center::NotificationType type;
110 155
111 switch (data.type) { 156 switch (data.type) {
112 case ArcNotificationType::BASIC: 157 case ArcNotificationType::BASIC:
113 type = message_center::NOTIFICATION_TYPE_BASE_FORMAT; 158 type = message_center::NOTIFICATION_TYPE_BASE_FORMAT;
114 break; 159 break;
160 case ArcNotificationType::LIST:
161 type = message_center::NOTIFICATION_TYPE_MULTIPLE;
162
163 if (data.texts.is_null())
164 break;
165
166 for (size_t i = 0;
167 i < std::min(data.texts.size(),
168 message_center::kNotificationMaximumItems - 1);
169 i++) {
170 rich_data.items.emplace_back(
171 base::string16(), base::UTF8ToUTF16(data.texts.at(i).get()));
172 }
173
174 if (data.texts.size() > message_center::kNotificationMaximumItems) {
175 // Show an elipsis as the 5th item if there are more than 5 items.
176 rich_data.items.emplace_back(base::string16(), gfx::kEllipsisUTF16);
177 } else if (data.texts.size() ==
178 message_center::kNotificationMaximumItems) {
179 // Show the 5th item if there are exact 5 items.
180 rich_data.items.emplace_back(
181 base::string16(),
182 base::UTF8ToUTF16(data.texts.at(data.texts.size() - 1).get()));
183 }
184 break;
115 case ArcNotificationType::IMAGE: 185 case ArcNotificationType::IMAGE:
116 // TODO(yoshiki): Implement this types. 186 type = message_center::NOTIFICATION_TYPE_IMAGE;
117 type = message_center::NOTIFICATION_TYPE_BASE_FORMAT; 187
118 LOG(ERROR) << "Unsupported notification type: image"; 188 if (!data.big_picture.is_null()) {
189 rich_data.image = gfx::Image::CreateFrom1xBitmap(
190 CropImage(data.big_picture.To<SkBitmap>()));
191 }
119 break; 192 break;
120 case ArcNotificationType::PROGRESS: 193 case ArcNotificationType::PROGRESS:
121 type = message_center::NOTIFICATION_TYPE_PROGRESS; 194 type = message_center::NOTIFICATION_TYPE_PROGRESS;
122 rich_data.timestamp = base::Time::UnixEpoch() + 195 rich_data.timestamp = base::Time::UnixEpoch() +
123 base::TimeDelta::FromMilliseconds(data.time); 196 base::TimeDelta::FromMilliseconds(data.time);
124 rich_data.progress = std::max( 197 rich_data.progress = std::max(
125 0, std::min(100, static_cast<int>(std::round( 198 0, std::min(100, static_cast<int>(std::round(
126 static_cast<float>(data.progress_current) / 199 static_cast<float>(data.progress_current) /
127 data.progress_max * 100)))); 200 data.progress_max * 100))));
128 break; 201 break;
129 } 202 }
130 DCHECK(IsKnownEnumValue(data.type)) << "Unsupported notification type: " 203 DCHECK(IsKnownEnumValue(data.type)) << "Unsupported notification type: "
131 << data.type; 204 << data.type;
132 205
133 for (size_t i = 0; i < data.buttons.size(); i++) { 206 for (size_t i = 0; i < data.buttons.size(); i++) {
134 rich_data.buttons.push_back(message_center::ButtonInfo( 207 rich_data.buttons.emplace_back(
135 base::UTF8ToUTF16(data.buttons.at(i)->label.get()))); 208 base::UTF8ToUTF16(data.buttons.at(i)->label.get()));
136 } 209 }
137 210
138 // If the client is old (version < 1), both |no_clear| and |ongoing_event| 211 // If the client is old (version < 1), both |no_clear| and |ongoing_event|
139 // are false. 212 // are false.
140 rich_data.pinned = (data.no_clear || data.ongoing_event); 213 rich_data.pinned = (data.no_clear || data.ongoing_event);
141 214
142 // The identifier of the notifier, which is used to distinguish the notifiers 215 // The identifier of the notifier, which is used to distinguish the notifiers
143 // in the message center. 216 // in the message center.
144 message_center::NotifierId notifier_id( 217 message_center::NotifierId notifier_id(
145 message_center::NotifierId::SYSTEM_COMPONENT, kNotifierId); 218 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_)); 281 message_center_->AddNotification(std::move(notification_));
209 282
210 if (newer_data_) { 283 if (newer_data_) {
211 // There is the newer data, so updates again. 284 // There is the newer data, so updates again.
212 ArcNotificationDataPtr data(std::move(newer_data_)); 285 ArcNotificationDataPtr data(std::move(newer_data_));
213 UpdateWithArcNotificationData(*data); 286 UpdateWithArcNotificationData(*data);
214 } 287 }
215 } 288 }
216 289
217 } // namespace arc 290 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698