Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: ash/common/system/chromeos/power/battery_notification.cc

Issue 2732813002: chromeos: Move files in //ash/common to //ash, part 1 (Closed)
Patch Set: rebase Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "ash/common/system/chromeos/power/battery_notification.h"
6
7 #include "ash/common/system/chromeos/power/power_status.h"
8 #include "ash/common/system/system_notifier.h"
9 #include "ash/resources/grit/ash_resources.h"
10 #include "ash/strings/grit/ash_strings.h"
11 #include "base/i18n/message_formatter.h"
12 #include "base/i18n/time_formatting.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/time/time.h"
15 #include "ui/base/l10n/l10n_util.h"
16 #include "ui/base/l10n/time_format.h"
17 #include "ui/base/resource/resource_bundle.h"
18 #include "ui/gfx/image/image.h"
19 #include "ui/message_center/message_center.h"
20 #include "ui/message_center/notification.h"
21
22 using message_center::MessageCenter;
23 using message_center::Notification;
24
25 namespace ash {
26
27 namespace {
28
29 const char kBatteryNotificationId[] = "battery";
30
31 gfx::Image& GetBatteryImage(TrayPower::NotificationState notification_state) {
32 int resource_id;
33 if (PowerStatus::Get()->IsUsbChargerConnected()) {
34 resource_id = IDR_AURA_NOTIFICATION_BATTERY_FLUCTUATING;
35 } else if (notification_state == TrayPower::NOTIFICATION_LOW_POWER) {
36 resource_id = IDR_AURA_NOTIFICATION_BATTERY_LOW;
37 } else if (notification_state == TrayPower::NOTIFICATION_CRITICAL) {
38 resource_id = IDR_AURA_NOTIFICATION_BATTERY_CRITICAL;
39 } else {
40 NOTREACHED();
41 resource_id = 0;
42 }
43
44 return ui::ResourceBundle::GetSharedInstance().GetImageNamed(resource_id);
45 }
46
47 std::unique_ptr<Notification> CreateNotification(
48 TrayPower::NotificationState notification_state) {
49 const PowerStatus& status = *PowerStatus::Get();
50
51 base::string16 message = base::i18n::MessageFormatter::FormatWithNumberedArgs(
52 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BATTERY_PERCENT),
53 static_cast<double>(status.GetRoundedBatteryPercent()) / 100.0);
54
55 const base::TimeDelta time = status.IsBatteryCharging()
56 ? status.GetBatteryTimeToFull()
57 : status.GetBatteryTimeToEmpty();
58 base::string16 time_message;
59 if (status.IsUsbChargerConnected()) {
60 time_message = l10n_util::GetStringUTF16(
61 IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE);
62 } else if (PowerStatus::ShouldDisplayBatteryTime(time) &&
63 !status.IsBatteryDischargingOnLinePower()) {
64 if (status.IsBatteryCharging()) {
65 time_message = l10n_util::GetStringFUTF16(
66 IDS_ASH_STATUS_TRAY_BATTERY_TIME_UNTIL_FULL,
67 TimeDurationFormat(time, base::DURATION_WIDTH_NARROW));
68 } else {
69 // This is a low battery warning prompting the user in minutes.
70 time_message = ui::TimeFormat::Simple(ui::TimeFormat::FORMAT_REMAINING,
71 ui::TimeFormat::LENGTH_LONG, time);
72 }
73 }
74
75 if (!time_message.empty())
76 message = message + base::ASCIIToUTF16("\n") + time_message;
77
78 std::unique_ptr<Notification> notification(new Notification(
79 message_center::NOTIFICATION_TYPE_SIMPLE, kBatteryNotificationId,
80 base::string16(), message, GetBatteryImage(notification_state),
81 base::string16(), GURL(),
82 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT,
83 system_notifier::kNotifierBattery),
84 message_center::RichNotificationData(), NULL));
85 notification->SetSystemPriority();
86 return notification;
87 }
88
89 } // namespace
90
91 BatteryNotification::BatteryNotification(
92 MessageCenter* message_center,
93 TrayPower::NotificationState notification_state)
94 : message_center_(message_center) {
95 message_center_->AddNotification(CreateNotification(notification_state));
96 }
97
98 BatteryNotification::~BatteryNotification() {
99 if (message_center_->FindVisibleNotificationById(kBatteryNotificationId))
100 message_center_->RemoveNotification(kBatteryNotificationId, false);
101 }
102
103 void BatteryNotification::Update(
104 TrayPower::NotificationState notification_state) {
105 if (message_center_->FindVisibleNotificationById(kBatteryNotificationId)) {
106 message_center_->UpdateNotification(kBatteryNotificationId,
107 CreateNotification(notification_state));
108 }
109 }
110
111 } // namespace ash
OLDNEW
« no previous file with comments | « ash/common/system/chromeos/power/battery_notification.h ('k') | ash/common/system/chromeos/power/dual_role_notification.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698