OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <utility> | |
6 | |
7 #include "ash/common/material_design/material_design_controller.h" | |
8 #include "ash/common/metrics/user_metrics_action.h" | |
9 #include "ash/common/system/chromeos/network/sms_observer.h" | |
10 #include "ash/common/system/system_notifier.h" | |
11 #include "ash/common/system/tray/tray_constants.h" | |
12 #include "ash/common/wm_shell.h" | |
13 #include "ash/resources/vector_icons/vector_icons.h" | |
14 #include "base/memory/ptr_util.h" | |
15 #include "base/strings/string_number_conversions.h" | |
16 #include "base/strings/utf_string_conversions.h" | |
17 #include "chromeos/network/network_event_log.h" | |
18 #include "chromeos/network/network_handler.h" | |
19 #include "grit/ash_resources.h" | |
20 #include "grit/ash_strings.h" | |
21 #include "ui/base/l10n/l10n_util.h" | |
22 #include "ui/base/resource/resource_bundle.h" | |
23 #include "ui/gfx/paint_vector_icon.h" | |
24 #include "ui/message_center/message_center.h" | |
25 #include "ui/views/view.h" | |
26 | |
27 namespace ash { | |
28 | |
29 SmsObserver::SmsObserver() : message_id_(0) { | |
30 // TODO(armansito): SMS could be a special case for cellular that requires a | |
31 // user (perhaps the owner) to be logged in. If that is the case, then an | |
32 // additional check should be done before subscribing for SMS notifications. | |
33 if (chromeos::NetworkHandler::IsInitialized()) | |
34 chromeos::NetworkHandler::Get()->network_sms_handler()->AddObserver(this); | |
35 } | |
36 | |
37 SmsObserver::~SmsObserver() { | |
38 if (chromeos::NetworkHandler::IsInitialized()) { | |
39 chromeos::NetworkHandler::Get()->network_sms_handler()->RemoveObserver( | |
40 this); | |
41 } | |
42 } | |
43 | |
44 void SmsObserver::MessageReceived(const base::DictionaryValue& message) { | |
45 std::string message_text; | |
46 if (!message.GetStringWithoutPathExpansion( | |
47 chromeos::NetworkSmsHandler::kTextKey, &message_text)) { | |
48 NET_LOG_ERROR("SMS message contains no content.", ""); | |
49 return; | |
50 } | |
51 // TODO(armansito): A message might be due to a special "Message Waiting" | |
52 // state that the message is in. Once SMS handling moves to shill, such | |
53 // messages should be filtered there so that this check becomes unnecessary. | |
54 if (message_text.empty()) { | |
55 NET_LOG_DEBUG("SMS has empty content text. Ignoring.", ""); | |
56 return; | |
57 } | |
58 std::string message_number; | |
59 if (!message.GetStringWithoutPathExpansion( | |
60 chromeos::NetworkSmsHandler::kNumberKey, &message_number)) { | |
61 NET_LOG_DEBUG("SMS contains no number. Ignoring.", ""); | |
62 return; | |
63 } | |
64 | |
65 NET_LOG_DEBUG( | |
66 "Received SMS from: " + message_number + " with text: " + message_text, | |
67 ""); | |
68 message_id_++; | |
69 Update(&message, message_text, message_number); | |
stevenjb
2017/02/07 22:30:25
We should pass message_id_ to Update() and rename
yiyix
2017/02/09 02:01:48
Done.
| |
70 } | |
71 | |
72 void SmsObserver::Update(const base::DictionaryValue* message, | |
73 std::string message_text, | |
74 std::string message_number) { | |
75 const char kNotificationId[] = "chrome://network/sms"; | |
76 std::unique_ptr<message_center::Notification> notification; | |
77 | |
78 notification.reset(new message_center::Notification( | |
79 message_center::NOTIFICATION_TYPE_SIMPLE, | |
80 kNotificationId + std::to_string(message_id_), | |
81 base::ASCIIToUTF16(message_number), base::ASCIIToUTF16(message_text), | |
82 gfx::Image( | |
83 CreateVectorIcon(kSystemMenuSmsIcon, kMenuIconSize, kMenuIconColor)), | |
84 base::string16(), GURL(), | |
85 message_center::NotifierId(message_center::NotifierId::APPLICATION, | |
86 system_notifier::kNotifierSms), | |
87 message_center::RichNotificationData(), nullptr)); | |
88 message_center::MessageCenter::Get()->AddNotification( | |
89 std::move(notification)); | |
90 } | |
91 | |
92 } // namespace ash | |
OLD | NEW |