-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
149 lines (125 loc) · 4.34 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
import Gio from 'gi://Gio';
import GObject from 'gi://GObject';
import St from 'gi://St';
import GLib from 'gi://GLib';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import * as QuickSettings from 'resource:///org/gnome/shell/ui/quickSettings.js';
import { Extension } from 'resource:///org/gnome/shell/extensions/extension.js';
const ICON_LOADING = "process-working-symbolic";
const ICON_STOPPED = "system-shutdown-symbolic";
const ICON_FROZEN = "media-playback-pause-symbolic";
const ICON_RUNNING = "phone-symbolic";
const WaydroidToggle = GObject.registerClass(
class WaydroidToggle extends QuickSettings.QuickToggle {
constructor() {
super({
title: 'Waydroid',
iconName: ICON_LOADING,
toggleMode: true,
});
this._isFrozen = false;
this._isStopped = false;
this._checkServiceStatus();
this._statusCheckId = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 5000, () => {
this._checkServiceStatus();
return GLib.SOURCE_CONTINUE;
});
this.connect('clicked', () => {
if (!this.checked) {
this._stopWaydroid();
} else if (this._isFrozen) {
this._stopWaydroid();
} else {
this._startWaydroid();
}
});
}
_startWaydroid() {
try {
GLib.spawn_command_line_async('waydroid session start');
} catch (e) {
console.error(e);
}
}
_stopWaydroid() {
try {
GLib.spawn_command_line_async('waydroid session stop');
} catch (e) {
console.error(e);
}
}
_updateIcon(isFrozen, isStopped) {
if (isStopped) {
this.iconName = ICON_STOPPED;
} else if (isFrozen) {
this.iconName = ICON_FROZEN;
} else {
this.iconName = ICON_RUNNING;
}
}
_checkServiceStatus() {
try {
let [success, stdout, stderr] = GLib.spawn_command_line_sync('waydroid status');
if (success) {
let output = new TextDecoder().decode(stdout);
let lines = output.split('\n');
let session_line = lines.find(line =>
line.trim().startsWith('Session:')
);
let session_running = session_line?.includes('RUNNING') ?? false;
let session_stopped = session_line?.includes('STOPPED') ?? false;
let container_line = lines.find(line =>
line.trim().startsWith('Container:')
);
let container_running = container_line?.includes('RUNNING') ?? false;
let container_frozen = container_line?.includes('FROZEN') ?? false;
this._isFrozen = container_frozen;
this._isStopped = session_stopped;
this._updateIcon(container_frozen, session_stopped);
this.set_checked(session_running && (container_running || container_frozen));
} else {
this._isFrozen = false;
this._isStopped = true;
this._updateIcon(false, true);
this.set_checked(false);
}
} catch (e) {
console.error(e);
this._isFrozen = false;
this._isStopped = true;
this._updateIcon(false, true);
this.set_checked(false);
}
}
destroy() {
if (this._statusCheckId) {
GLib.source_remove(this._statusCheckId);
this._statusCheckId = null;
}
super.destroy();
}
});
const WaydroidIndicator = GObject.registerClass(
class WaydroidIndicator extends QuickSettings.SystemIndicator {
constructor(toggle) {
super();
this.quickSettingsItems.push(toggle);
}
destroy() {
this.quickSettingsItems.forEach(item => item.destroy());
super.destroy();
}
});
export default class WaydroidExtension extends Extension {
enable() {
const toggle = new WaydroidToggle();
this._indicator = new WaydroidIndicator(toggle);
Main.panel.statusArea.quickSettings.addExternalIndicator(this._indicator);
}
disable() {
if (this._indicator) {
this._indicator.destroy();
this._indicator = null;
}
}
}