forked from ba0f3/gnome-bluetooth-smartlock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmartlock.js
118 lines (98 loc) · 3.76 KB
/
smartlock.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
import GnomeBluetooth from "gi://GnomeBluetooth";
import GLib from 'gi://GLib';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import * as ExtensionUtils from 'resource:///org/gnome/shell/misc/extensionUtils.js';
const Me = ExtensionUtils.getCurrentExtension();
const {Settings} = Me.imports.settings;
// eslint-disable-next-line no-unused-vars
var SmartLock = class SmartLock {
constructor() {
this._client = new GnomeBluetooth.Client();
this._settings = new Settings();
this._deviceAddress = null;
this._deviceChangeHandlerId = 0;
this._lastSeen = 0;
}
_log(message) {
log(`[bluetooth-smartlock] ${message}`);
}
_runLoop() {
const interval = this._settings.getScanInterval();
this.scan();
this._loop = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, interval, this._runLoop.bind(this));
}
lock_screen() {
Main.overview.hide();
Main.screenShield.lock(true);
}
enable() {
this._log('Enabling extension');
this._deviceAddress = this._settings.getDevice();
this._deviceChangeHandlerId = this._settings._settings.connect('changed::mac', () => {
// reset last seen when device changed
if (this._deviceAddress !== this._settings.getDevice()) {
this._log('Device changed');
this._deviceAddress = this._settings.getDevice();
this._lastSeen = 0;
}
});
this._runLoop();
}
disable() {
this._log('Disabling extension');
this._deviceAddress = null;
this._lastSeen = 0;
if (this._deviceChangeHandlerId)
this._settings._settings.disconnect(this._deviceChangeHandlerId);
if (this._loop) {
GLib.source_remove(this._loop);
this._loop = null;
}
}
connect(device) {
this._client.connect_service(device.get_object_path(), true, null, (sourceObject, res) => {
try {
if (this._client.connect_service_finish(res)) {
this._log('Connected to device');
this._lastSeen = new Date().getTime();
}
} catch (error) {
this._log(`Error: ${error}`);
}
});
}
scan() {
// If not active, do nothing
if (!this._settings.getActive() || this._deviceAddress === '')
return;
try {
let store = this._client.get_devices();
let nIitems = store.get_n_items();
for (let i = 0; i < nIitems; i++) {
let device = store.get_item(i);
if (device.address === this._deviceAddress) {
let now = new Date().getTime();
if (!device.connected) {
// Only check for timeout if we ever seen device once
if (this._lastSeen !== 0) {
let duration = (now - this._lastSeen) / 1000;
if (duration >= this._settings.getAwayDuration()) {
this._lastSeen = 0;
this._log(`User stepped away for ${duration} seconds, locking the screen`);
this.lock_screen();
}
}
// Try to connect to target device, cause Linux wont auto reconnect on some devices like smart phones
this.connect(device);
} else {
this._lastSeen = now;
}
break;
}
}
} catch (error) {
this._log(`Error: ${error}`);
}
}
};
export default SmartLock