-
Notifications
You must be signed in to change notification settings - Fork 1
/
extension.js
161 lines (138 loc) · 4.4 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const Main = imports.ui.main;
const MessageTray = imports.ui.messageTray;
const PowerIndicator = Main.panel.statusArea.aggregateMenu._power;
const Gettext = imports.gettext;
const _ = Gettext.domain('fullbattery').gettext;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const UPower = imports.gi.UPowerGlib;
const INDICATOR_ICON = 'battery-full-charged-symbolic';
let _notifSource = null;
let signals = [];
let data_method = "native";
let notification = null;
function _initNotifSource() {
if (!_notifSource) {
_notifSource = new MessageTray.Source('FullBatteryIndicator', INDICATOR_ICON);
_notifSource.connect('destroy', function() {
_notifSource = null;
});
Main.messageTray.add(_notifSource);
}
}
function _showNotification(message, urgent) {
_initNotifSource();
if (_notifSource.count === 0) {
notification = new MessageTray.Notification(_notifSource, message);
} else {
notification = _notifSource.notifications[0];
notification.update(message, '', { clear: true });
}
if (urgent) {
notification.setUrgency(MessageTray.Urgency.CRITICAL);
} else {
notification.setUrgency(MessageTray.Urgency.NORMAL);
}
notification.setTransient(true);
_notifSource.notify(notification);
}
function _hideNotification() {
if (notification) {
notification.destroy(MessageTray.NotificationDestroyedReason.SOURCE_CLOSED);
notification = null;
}
}
function read_battery() {
switch (data_method) {
default:
case "native":
return [PowerIndicator._proxy.TimeToEmpty,
PowerIndicator._proxy.TimeToFull,
PowerIndicator._proxy.Percentage,
PowerIndicator._proxy.IsPresent,
PowerIndicator._proxy.State];
case "device":
let devices = PowerIndicator._proxy.GetDevicesSync();
let n_devs = 0;
let is_present = false;
let tte_s = 0;
let ttf_s = 0;
let per_c = 0;
let out_state = UPower.DeviceState.EMPTY;
for (let i = 0; i < devices.length; ++i) {
for (let j = 0; j < devices[i].length; ++j) {
let [id, type, icon, percent, state, time] = devices[i][j];
if (type != UPower.DeviceKind.BATTERY) {
continue;
}
++n_devs;
is_present = true;
tte_s += time;
ttf_s = tte_s;
// Round the total percentage for multiple batteries
per_c = ((per_c * (n_devs - 1)) + percent) / n_devs;
// charging > discharging > full > empty
// Ignore the other states.
switch (state) {
case UPower.DeviceState.DISCHARGING:
case UPower.DeviceState.PENDING_DISCHARGE:
if (out_state != UPower.DeviceState.CHARGING) {
out_state = UPower.DeviceState.DISCHARGING;
}
break;
case UPower.DeviceState.CHARGING:
case UPower.DeviceState.PENDING_CHARGE:
out_state = UPower.DeviceState.CHARGING;
break;
case UPower.DeviceState.FULLY_CHARGED:
if (out_state != UPower.DeviceState.CHARGING
&& out_state != UPower.DeviceState.DISCHARGING) {
out_state = UPower.DeviceState.FULLY_CHARGED;
}
break;
default:
break;
}
}
}
return [tte_s, ttf_s, per_c, is_present, out_state];
}
}
function _update() {
let [tte_s, ttf_s, per_c, is_present, state] = read_battery();
/*
CHARGING : 1
DISCHARGING : 2
FULLY_CHARGED : 4
PENDING_CHARGE : 5
PENDING_DISCHARGE : 6
*/
if (state == UPower.DeviceState.FULLY_CHARGED || per_c == 100) {
_showNotification(_('Battery fully charged. Disconnect charger'), true);
} else if (state == UPower.DeviceState.CHARGING && Math.abs(100-per_c) < 3){
_showNotification(_('Battery close to full charge: %d%%').format(per_c));
} else {
_hideNotification();
}
}
function init() {
let localeDir = Me.dir.get_child('locale');
Gettext.bindtextdomain('fullbattery', localeDir.get_path());
}
function enable() {
if ("GetDevicesSync" in PowerIndicator._proxy) {
data_method = "device";
} else {
data_method = "native";
}
signals.push([
PowerIndicator._proxy,
PowerIndicator._proxy.connect('g-properties-changed', _update),
]);
}
function disable() {
while (signals.length > 0) {
let [obj, sig] = signals.pop();
obj.disconnect(sig);
}
}