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 | |
James Cook
2017/02/10 17:36:38
#include sms_observer.h here
yiyix
2017/02/13 23:34:00
So sorry, I missed this obvious include.
| |
5 #include <utility> | |
6 | |
7 #include "ash/common/material_design/material_design_controller.h" | |
James Cook
2017/02/10 17:36:37
Is this needed?
Are there other unused includes b
yiyix
2017/02/13 23:34:00
Sorry, after addressing comments and experiments,
| |
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 // Send the |message| to notification center to display to users. Note that each | |
30 // notification will be assigned with different |message_id| as notification id. | |
31 void SendMessage(const base::DictionaryValue* message, | |
James Cook
2017/02/10 17:36:38
put utility functions in an anonymous namespace
yiyix
2017/02/13 23:34:00
Done.
| |
32 std::string message_text, | |
James Cook
2017/02/10 17:36:37
const std::string& ?
yiyix
2017/02/13 23:34:00
You are right, constant makes more sense.
| |
33 std::string message_number, | |
34 int message_id) { | |
35 const char kNotificationId[] = "chrome://network/sms"; | |
36 std::unique_ptr<message_center::Notification> notification; | |
James Cook
2017/02/10 17:36:38
instead of reset() below, just do notification = b
yiyix
2017/02/13 23:34:00
Done.
| |
37 | |
38 notification.reset(new message_center::Notification( | |
39 message_center::NOTIFICATION_TYPE_SIMPLE, | |
40 kNotificationId + std::to_string(message_id), | |
41 base::ASCIIToUTF16(message_number), base::ASCIIToUTF16(message_text), | |
42 gfx::Image( | |
43 CreateVectorIcon(kSystemMenuSmsIcon, kMenuIconSize, kMenuIconColor)), | |
44 base::string16(), GURL(), | |
45 message_center::NotifierId(message_center::NotifierId::APPLICATION, | |
46 system_notifier::kNotifierSms), | |
47 message_center::RichNotificationData(), nullptr)); | |
48 message_center::MessageCenter::Get()->AddNotification( | |
James Cook
2017/02/10 17:36:38
optional: I'm OK with "using message_center::Foo"
yiyix
2017/02/13 23:34:00
As we have discussed offline, i will use NetworkHa
| |
49 std::move(notification)); | |
50 } | |
51 | |
52 SmsObserver::SmsObserver() : message_id_(0) { | |
53 // TODO(armansito): SMS could be a special case for cellular that requires a | |
54 // user (perhaps the owner) to be logged in. If that is the case, then an | |
55 // additional check should be done before subscribing for SMS notifications. | |
56 if (chromeos::NetworkHandler::IsInitialized()) | |
57 chromeos::NetworkHandler::Get()->network_sms_handler()->AddObserver(this); | |
58 } | |
59 | |
60 SmsObserver::~SmsObserver() { | |
61 if (chromeos::NetworkHandler::IsInitialized()) { | |
62 chromeos::NetworkHandler::Get()->network_sms_handler()->RemoveObserver( | |
63 this); | |
64 } | |
65 } | |
66 | |
67 void SmsObserver::MessageReceived(const base::DictionaryValue& message) { | |
68 std::string message_text; | |
69 if (!message.GetStringWithoutPathExpansion( | |
70 chromeos::NetworkSmsHandler::kTextKey, &message_text)) { | |
71 NET_LOG_ERROR("SMS message contains no content.", ""); | |
stevenjb
2017/02/10 17:15:23
While we are moving this code, could you change th
yiyix
2017/02/13 23:34:00
I can update it as a part of this cl for sure.
| |
72 return; | |
73 } | |
74 // TODO(armansito): A message might be due to a special "Message Waiting" | |
75 // state that the message is in. Once SMS handling moves to shill, such | |
76 // messages should be filtered there so that this check becomes unnecessary. | |
77 if (message_text.empty()) { | |
78 NET_LOG_DEBUG("SMS has empty content text. Ignoring.", ""); | |
79 return; | |
80 } | |
81 std::string message_number; | |
82 if (!message.GetStringWithoutPathExpansion( | |
83 chromeos::NetworkSmsHandler::kNumberKey, &message_number)) { | |
84 NET_LOG_DEBUG("SMS contains no number. Ignoring.", ""); | |
85 return; | |
86 } | |
87 | |
88 NET_LOG_DEBUG( | |
89 "Received SMS from: " + message_number + " with text: " + message_text, | |
90 ""); | |
stevenjb
2017/02/10 17:15:23
This would be:
NTE_LOG(DEBUG) << "Received SMS fr
yiyix
2017/02/13 23:34:00
Done.
| |
91 message_id_++; | |
92 SendMessage(&message, message_text, message_number, message_id_); | |
James Cook
2017/02/10 17:36:37
SendMessage() would be better if it was called Sho
yiyix
2017/02/13 23:34:00
I will use ShowNotification(), so it's clear that
| |
93 } | |
94 | |
95 } // namespace ash | |
OLD | NEW |