1 // Copyright 2015 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.
5 #include "ash/system/chromeos/power/battery_notification.h"
7 #include "ash/system/chromeos/power/power_status.h"
8 #include "ash/system/system_notifier.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/time/time.h"
12 #include "grit/ash_resources.h"
13 #include "grit/ash_strings.h"
14 #include "ui/base/l10n/l10n_util.h"
15 #include "ui/base/l10n/time_format.h"
16 #include "ui/base/resource/resource_bundle.h"
17 #include "ui/gfx/image/image.h"
18 #include "ui/message_center/message_center.h"
19 #include "ui/message_center/notification.h"
21 using message_center::MessageCenter
;
22 using message_center::Notification
;
28 const char kBatteryNotificationId
[] = "battery";
30 gfx::Image
& GetBatteryImage(TrayPower::NotificationState notification_state
) {
32 if (PowerStatus::Get()->IsUsbChargerConnected()) {
33 resource_id
= IDR_AURA_NOTIFICATION_BATTERY_FLUCTUATING
;
34 } else if (notification_state
== TrayPower::NOTIFICATION_LOW_POWER
) {
35 resource_id
= IDR_AURA_NOTIFICATION_BATTERY_LOW
;
36 } else if (notification_state
== TrayPower::NOTIFICATION_CRITICAL
) {
37 resource_id
= IDR_AURA_NOTIFICATION_BATTERY_CRITICAL
;
43 return ui::ResourceBundle::GetSharedInstance().GetImageNamed(resource_id
);
46 scoped_ptr
<Notification
> CreateNotification(
47 TrayPower::NotificationState notification_state
) {
48 const PowerStatus
& status
= *PowerStatus::Get();
50 base::string16 message
= l10n_util::GetStringFUTF16(
51 IDS_ASH_STATUS_TRAY_BATTERY_PERCENT
,
52 base::IntToString16(status
.GetRoundedBatteryPercent()));
54 const base::TimeDelta time
= status
.IsBatteryCharging()
55 ? status
.GetBatteryTimeToFull()
56 : status
.GetBatteryTimeToEmpty();
57 base::string16 time_message
;
58 if (status
.IsUsbChargerConnected()) {
59 time_message
= l10n_util::GetStringUTF16(
60 IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE
);
61 } else if (PowerStatus::ShouldDisplayBatteryTime(time
) &&
62 !status
.IsBatteryDischargingOnLinePower()) {
63 int hour
= 0, min
= 0;
64 PowerStatus::SplitTimeIntoHoursAndMinutes(time
, &hour
, &min
);
65 if (status
.IsBatteryCharging()) {
66 time_message
= l10n_util::GetStringFUTF16(
67 IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_FULL
,
68 base::IntToString16(hour
), base::IntToString16(min
));
70 // This is a low battery warning prompting the user in minutes.
71 time_message
= ui::TimeFormat::Simple(
72 ui::TimeFormat::FORMAT_REMAINING
, ui::TimeFormat::LENGTH_LONG
,
73 base::TimeDelta::FromMinutes(hour
* 60 + min
));
77 if (!time_message
.empty())
78 message
= message
+ base::ASCIIToUTF16("\n") + time_message
;
80 scoped_ptr
<Notification
> notification(new Notification(
81 message_center::NOTIFICATION_TYPE_SIMPLE
, kBatteryNotificationId
,
82 base::string16(), message
, GetBatteryImage(notification_state
),
84 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT
,
85 system_notifier::kNotifierBattery
),
86 message_center::RichNotificationData(), NULL
));
87 notification
->SetSystemPriority();
93 BatteryNotification::BatteryNotification(
94 MessageCenter
* message_center
,
95 TrayPower::NotificationState notification_state
)
96 : message_center_(message_center
) {
97 message_center_
->AddNotification(
98 CreateNotification(notification_state
).Pass());
101 BatteryNotification::~BatteryNotification() {
102 if (message_center_
->FindVisibleNotificationById(kBatteryNotificationId
))
103 message_center_
->RemoveNotification(kBatteryNotificationId
, false);
106 void BatteryNotification::Update(
107 TrayPower::NotificationState notification_state
) {
108 if (message_center_
->FindVisibleNotificationById(kBatteryNotificationId
)) {
109 message_center_
->UpdateNotification(
110 kBatteryNotificationId
, CreateNotification(notification_state
).Pass());