| OLD | NEW |
| 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 // Crops the image to proper size for Chrome Notification. It accepts only |
| 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 // Converts from Android notification priority to Chrome notification priority. | 89 // Converts from Android notification priority to Chrome notification priority. |
| 43 // On Android, PRIORITY_DEFAULT does not pop up, so this maps PRIORITY_DEFAULT | 90 // On Android, PRIORITY_DEFAULT does not pop up, so this maps PRIORITY_DEFAULT |
| 44 // to Chrome's -1 to adapt that behavior. Also, this maps PRIORITY_LOW and _HIGH | 91 // to Chrome's -1 to adapt that behavior. Also, this maps PRIORITY_LOW and _HIGH |
| 45 // to -2 and 0 respectively to adjust the value with keeping the order among | 92 // to -2 and 0 respectively to adjust the value with keeping the order among |
| 46 // _LOW, _DEFAULT and _HIGH. | 93 // _LOW, _DEFAULT and _HIGH. |
| 47 int convertAndroidPriority(const int android_priority) { | 94 int convertAndroidPriority(const int android_priority) { |
| 48 switch (android_priority) { | 95 switch (android_priority) { |
| 49 case -2: // PRIORITY_MIN | 96 case -2: // PRIORITY_MIN |
| 50 case -1: // PRIORITY_LOW | 97 case -1: // PRIORITY_LOW |
| 51 return -2; | 98 return -2; |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 return; | 174 return; |
| 128 } | 175 } |
| 129 | 176 |
| 130 message_center::RichNotificationData rich_data; | 177 message_center::RichNotificationData rich_data; |
| 131 message_center::NotificationType type; | 178 message_center::NotificationType type; |
| 132 | 179 |
| 133 switch (data.type) { | 180 switch (data.type) { |
| 134 case mojom::ArcNotificationType::BASIC: | 181 case mojom::ArcNotificationType::BASIC: |
| 135 type = message_center::NOTIFICATION_TYPE_BASE_FORMAT; | 182 type = message_center::NOTIFICATION_TYPE_BASE_FORMAT; |
| 136 break; | 183 break; |
| 184 case mojom::ArcNotificationType::LIST: |
| 185 type = message_center::NOTIFICATION_TYPE_MULTIPLE; |
| 186 |
| 187 if (data.texts.is_null()) |
| 188 break; |
| 189 |
| 190 for (size_t i = 0; |
| 191 i < std::min(data.texts.size(), |
| 192 message_center::kNotificationMaximumItems - 1); |
| 193 i++) { |
| 194 rich_data.items.emplace_back( |
| 195 base::string16(), base::UTF8ToUTF16(data.texts.at(i).get())); |
| 196 } |
| 197 |
| 198 if (data.texts.size() > message_center::kNotificationMaximumItems) { |
| 199 // Show an elipsis as the 5th item if there are more than 5 items. |
| 200 rich_data.items.emplace_back(base::string16(), gfx::kEllipsisUTF16); |
| 201 } else if (data.texts.size() == |
| 202 message_center::kNotificationMaximumItems) { |
| 203 // Show the 5th item if there are exact 5 items. |
| 204 rich_data.items.emplace_back( |
| 205 base::string16(), |
| 206 base::UTF8ToUTF16(data.texts.at(data.texts.size() - 1).get())); |
| 207 } |
| 208 break; |
| 137 case mojom::ArcNotificationType::IMAGE: | 209 case mojom::ArcNotificationType::IMAGE: |
| 138 // TODO(yoshiki): Implement this types. | 210 type = message_center::NOTIFICATION_TYPE_IMAGE; |
| 139 type = message_center::NOTIFICATION_TYPE_BASE_FORMAT; | 211 |
| 140 LOG(ERROR) << "Unsupported notification type: image"; | 212 if (!data.big_picture.is_null()) { |
| 213 rich_data.image = gfx::Image::CreateFrom1xBitmap( |
| 214 CropImage(data.big_picture.To<SkBitmap>())); |
| 215 } |
| 141 break; | 216 break; |
| 142 case mojom::ArcNotificationType::PROGRESS: | 217 case mojom::ArcNotificationType::PROGRESS: |
| 143 type = message_center::NOTIFICATION_TYPE_PROGRESS; | 218 type = message_center::NOTIFICATION_TYPE_PROGRESS; |
| 144 rich_data.timestamp = base::Time::UnixEpoch() + | 219 rich_data.timestamp = base::Time::UnixEpoch() + |
| 145 base::TimeDelta::FromMilliseconds(data.time); | 220 base::TimeDelta::FromMilliseconds(data.time); |
| 146 rich_data.progress = std::max( | 221 rich_data.progress = std::max( |
| 147 0, std::min(100, static_cast<int>(std::round( | 222 0, std::min(100, static_cast<int>(std::round( |
| 148 static_cast<float>(data.progress_current) / | 223 static_cast<float>(data.progress_current) / |
| 149 data.progress_max * 100)))); | 224 data.progress_max * 100)))); |
| 150 break; | 225 break; |
| 151 } | 226 } |
| 152 DCHECK(IsKnownEnumValue(data.type)) << "Unsupported notification type: " | 227 DCHECK(IsKnownEnumValue(data.type)) << "Unsupported notification type: " |
| 153 << data.type; | 228 << data.type; |
| 154 | 229 |
| 155 for (size_t i = 0; i < data.buttons.size(); i++) { | 230 for (size_t i = 0; i < data.buttons.size(); i++) { |
| 156 rich_data.buttons.push_back(message_center::ButtonInfo( | 231 rich_data.buttons.emplace_back( |
| 157 base::UTF8ToUTF16(data.buttons.at(i)->label.get()))); | 232 base::UTF8ToUTF16(data.buttons.at(i)->label.get())); |
| 158 } | 233 } |
| 159 | 234 |
| 160 // If the client is old (version < 1), both |no_clear| and |ongoing_event| | 235 // If the client is old (version < 1), both |no_clear| and |ongoing_event| |
| 161 // are false. | 236 // are false. |
| 162 rich_data.pinned = (data.no_clear || data.ongoing_event); | 237 rich_data.pinned = (data.no_clear || data.ongoing_event); |
| 163 | 238 |
| 164 rich_data.priority = convertAndroidPriority(data.priority); | 239 rich_data.priority = convertAndroidPriority(data.priority); |
| 165 | 240 |
| 166 // The identifier of the notifier, which is used to distinguish the notifiers | 241 // The identifier of the notifier, which is used to distinguish the notifiers |
| 167 // in the message center. | 242 // in the message center. |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 message_center_->AddNotification(std::move(notification_)); | 307 message_center_->AddNotification(std::move(notification_)); |
| 233 | 308 |
| 234 if (newer_data_) { | 309 if (newer_data_) { |
| 235 // There is the newer data, so updates again. | 310 // There is the newer data, so updates again. |
| 236 mojom::ArcNotificationDataPtr data(std::move(newer_data_)); | 311 mojom::ArcNotificationDataPtr data(std::move(newer_data_)); |
| 237 UpdateWithArcNotificationData(*data); | 312 UpdateWithArcNotificationData(*data); |
| 238 } | 313 } |
| 239 } | 314 } |
| 240 | 315 |
| 241 } // namespace arc | 316 } // namespace arc |
| OLD | NEW |