| 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 <utility> |
| 8 #include <vector> | 9 #include <vector> |
| 9 | 10 |
| 10 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 11 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 13 #include "base/task_runner.h" | 14 #include "base/task_runner.h" |
| 14 #include "base/task_runner_util.h" | 15 #include "base/task_runner_util.h" |
| 15 #include "base/threading/worker_pool.h" | 16 #include "base/threading/worker_pool.h" |
| 16 #include "third_party/skia/include/core/SkCanvas.h" | 17 #include "third_party/skia/include/core/SkCanvas.h" |
| 17 #include "third_party/skia/include/core/SkPaint.h" | 18 #include "third_party/skia/include/core/SkPaint.h" |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 message_center::RichNotificationData rich_data; | 153 message_center::RichNotificationData rich_data; |
| 153 message_center::NotificationType type; | 154 message_center::NotificationType type; |
| 154 | 155 |
| 155 switch (data->type) { | 156 switch (data->type) { |
| 156 case mojom::ArcNotificationType::BASIC: | 157 case mojom::ArcNotificationType::BASIC: |
| 157 type = message_center::NOTIFICATION_TYPE_BASE_FORMAT; | 158 type = message_center::NOTIFICATION_TYPE_BASE_FORMAT; |
| 158 break; | 159 break; |
| 159 case mojom::ArcNotificationType::LIST: | 160 case mojom::ArcNotificationType::LIST: |
| 160 type = message_center::NOTIFICATION_TYPE_MULTIPLE; | 161 type = message_center::NOTIFICATION_TYPE_MULTIPLE; |
| 161 | 162 |
| 162 if (data->texts.is_null()) | 163 if (!data->texts.has_value()) |
| 163 break; | 164 break; |
| 164 | 165 |
| 165 for (size_t i = 0; | 166 for (size_t i = 0; |
| 166 i < std::min(data->texts.size(), | 167 i < std::min(data->texts->size(), |
| 167 message_center::kNotificationMaximumItems - 1); | 168 message_center::kNotificationMaximumItems - 1); |
| 168 i++) { | 169 i++) { |
| 169 rich_data.items.emplace_back( | 170 rich_data.items.emplace_back(base::string16(), |
| 170 base::string16(), base::UTF8ToUTF16(data->texts.at(i).get())); | 171 base::UTF8ToUTF16(data->texts->at(i))); |
| 171 } | 172 } |
| 172 | 173 |
| 173 if (data->texts.size() > message_center::kNotificationMaximumItems) { | 174 if (data->texts->size() > message_center::kNotificationMaximumItems) { |
| 174 // Show an elipsis as the 5th item if there are more than 5 items. | 175 // Show an elipsis as the 5th item if there are more than 5 items. |
| 175 rich_data.items.emplace_back(base::string16(), gfx::kEllipsisUTF16); | 176 rich_data.items.emplace_back(base::string16(), gfx::kEllipsisUTF16); |
| 176 } else if (data->texts.size() == | 177 } else if (data->texts->size() == |
| 177 message_center::kNotificationMaximumItems) { | 178 message_center::kNotificationMaximumItems) { |
| 178 // Show the 5th item if there are exact 5 items. | 179 // Show the 5th item if there are exact 5 items. |
| 179 rich_data.items.emplace_back( | 180 rich_data.items.emplace_back( |
| 180 base::string16(), | 181 base::string16(), |
| 181 base::UTF8ToUTF16(data->texts.at(data->texts.size() - 1).get())); | 182 base::UTF8ToUTF16(data->texts->at(data->texts->size() - 1))); |
| 182 } | 183 } |
| 183 break; | 184 break; |
| 184 case mojom::ArcNotificationType::IMAGE: | 185 case mojom::ArcNotificationType::IMAGE: |
| 185 type = message_center::NOTIFICATION_TYPE_IMAGE; | 186 type = message_center::NOTIFICATION_TYPE_IMAGE; |
| 186 | 187 |
| 187 if (data->big_picture && !data->big_picture->isNull()) { | 188 if (data->big_picture && !data->big_picture->isNull()) { |
| 188 rich_data.image = gfx::Image::CreateFrom1xBitmap( | 189 rich_data.image = gfx::Image::CreateFrom1xBitmap( |
| 189 CropImage(*data->big_picture)); | 190 CropImage(*data->big_picture)); |
| 190 } | 191 } |
| 191 break; | 192 break; |
| 192 case mojom::ArcNotificationType::PROGRESS: | 193 case mojom::ArcNotificationType::PROGRESS: |
| 193 type = message_center::NOTIFICATION_TYPE_PROGRESS; | 194 type = message_center::NOTIFICATION_TYPE_PROGRESS; |
| 194 rich_data.timestamp = base::Time::UnixEpoch() + | 195 rich_data.timestamp = base::Time::UnixEpoch() + |
| 195 base::TimeDelta::FromMilliseconds(data->time); | 196 base::TimeDelta::FromMilliseconds(data->time); |
| 196 rich_data.progress = std::max( | 197 rich_data.progress = std::max( |
| 197 0, std::min(100, static_cast<int>(std::round( | 198 0, std::min(100, static_cast<int>(std::round( |
| 198 static_cast<float>(data->progress_current) / | 199 static_cast<float>(data->progress_current) / |
| 199 data->progress_max * 100)))); | 200 data->progress_max * 100)))); |
| 200 break; | 201 break; |
| 201 } | 202 } |
| 202 DCHECK(IsKnownEnumValue(data->type)) << "Unsupported notification type: " | 203 DCHECK(IsKnownEnumValue(data->type)) << "Unsupported notification type: " |
| 203 << data->type; | 204 << data->type; |
| 204 | 205 |
| 205 for (size_t i = 0; i < data->buttons.size(); i++) { | 206 for (size_t i = 0; i < data->buttons.size(); i++) { |
| 206 rich_data.buttons.emplace_back( | 207 rich_data.buttons.emplace_back( |
| 207 base::UTF8ToUTF16(data->buttons.at(i)->label.get())); | 208 base::UTF8ToUTF16(data->buttons.at(i)->label)); |
| 208 } | 209 } |
| 209 | 210 |
| 210 // 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| |
| 211 // are false. | 212 // are false. |
| 212 rich_data.pinned = (data->no_clear || data->ongoing_event); | 213 rich_data.pinned = (data->no_clear || data->ongoing_event); |
| 213 | 214 |
| 214 rich_data.priority = ConvertAndroidPriority(data->priority); | 215 rich_data.priority = ConvertAndroidPriority(data->priority); |
| 215 if (data->small_icon) | 216 if (data->small_icon) |
| 216 rich_data.small_image = gfx::Image::CreateFrom1xBitmap(*data->small_icon); | 217 rich_data.small_image = gfx::Image::CreateFrom1xBitmap(*data->small_icon); |
| 217 | 218 |
| 218 // The identifier of the notifier, which is used to distinguish the notifiers | 219 // The identifier of the notifier, which is used to distinguish the notifiers |
| 219 // in the message center. | 220 // in the message center. |
| 220 message_center::NotifierId notifier_id( | 221 message_center::NotifierId notifier_id( |
| 221 message_center::NotifierId::SYSTEM_COMPONENT, kNotifierId); | 222 message_center::NotifierId::SYSTEM_COMPONENT, kNotifierId); |
| 222 notifier_id.profile_id = profile_id_.GetUserEmail(); | 223 notifier_id.profile_id = profile_id_.GetUserEmail(); |
| 223 | 224 |
| 224 DCHECK(!data->title.is_null()); | |
| 225 DCHECK(!data->message.is_null()); | |
| 226 SetNotification(base::MakeUnique<message_center::Notification>( | 225 SetNotification(base::MakeUnique<message_center::Notification>( |
| 227 type, notification_id_, base::UTF8ToUTF16(data->title.get()), | 226 type, notification_id_, base::UTF8ToUTF16(data->title), |
| 228 base::UTF8ToUTF16(data->message.get()), | 227 base::UTF8ToUTF16(data->message), |
| 229 gfx::Image(), // icon image: Will be overriden later. | 228 gfx::Image(), // icon image: Will be overriden later. |
| 230 base::UTF8ToUTF16("arc"), // display source | 229 base::UTF8ToUTF16("arc"), // display source |
| 231 GURL(), // empty origin url, for system component | 230 GURL(), // empty origin url, for system component |
| 232 notifier_id, rich_data, | 231 notifier_id, rich_data, |
| 233 new ArcNotificationDelegate(weak_ptr_factory_.GetWeakPtr()))); | 232 new ArcNotificationDelegate(weak_ptr_factory_.GetWeakPtr()))); |
| 234 | 233 |
| 235 DCHECK(!data->icon_data.is_null()); | |
| 236 if (data->icon_data.size() == 0) { | 234 if (data->icon_data.size() == 0) { |
| 237 OnImageDecoded(SkBitmap()); // Pass an empty bitmap. | 235 OnImageDecoded(SkBitmap()); // Pass an empty bitmap. |
| 238 return; | 236 return; |
| 239 } | 237 } |
| 240 | 238 |
| 241 // TODO(yoshiki): Remove decoding by passing a bitmap directly from Android. | 239 // TODO(yoshiki): Remove decoding by passing a bitmap directly from Android. |
| 242 base::PostTaskAndReplyWithResult( | 240 base::PostTaskAndReplyWithResult( |
| 243 base::WorkerPool::GetTaskRunner(true).get(), FROM_HERE, | 241 base::WorkerPool::GetTaskRunner(true).get(), FROM_HERE, |
| 244 base::Bind(&DecodeImage, data->icon_data.storage()), | 242 base::Bind(&DecodeImage, data->icon_data), |
| 245 base::Bind(&ArcNotificationItem::OnImageDecoded, | 243 base::Bind(&ArcNotificationItem::OnImageDecoded, |
| 246 weak_ptr_factory_.GetWeakPtr())); | 244 weak_ptr_factory_.GetWeakPtr())); |
| 247 } | 245 } |
| 248 | 246 |
| 249 ArcNotificationItem::~ArcNotificationItem() {} | 247 ArcNotificationItem::~ArcNotificationItem() {} |
| 250 | 248 |
| 251 void ArcNotificationItem::OnClosedFromAndroid() { | 249 void ArcNotificationItem::OnClosedFromAndroid() { |
| 252 being_removed_by_manager_ = true; // Closing is initiated by the manager. | 250 being_removed_by_manager_ = true; // Closing is initiated by the manager. |
| 253 message_center_->RemoveNotification(notification_id_, false /* by_user */); | 251 message_center_->RemoveNotification(notification_id_, false /* by_user */); |
| 254 } | 252 } |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 | 326 |
| 329 void ArcNotificationItem::OnImageDecoded(const SkBitmap& bitmap) { | 327 void ArcNotificationItem::OnImageDecoded(const SkBitmap& bitmap) { |
| 330 DCHECK(thread_checker_.CalledOnValidThread()); | 328 DCHECK(thread_checker_.CalledOnValidThread()); |
| 331 | 329 |
| 332 gfx::Image image = gfx::Image::CreateFrom1xBitmap(bitmap); | 330 gfx::Image image = gfx::Image::CreateFrom1xBitmap(bitmap); |
| 333 notification_->set_icon(image); | 331 notification_->set_icon(image); |
| 334 AddToMessageCenter(); | 332 AddToMessageCenter(); |
| 335 } | 333 } |
| 336 | 334 |
| 337 } // namespace arc | 335 } // namespace arc |
| OLD | NEW |