Skip to content

Commit

Permalink
Added "Don't enable airplane mode on Wi-Fi disable" feature and decre…
Browse files Browse the repository at this point in the history
…ased timeout to 50ms
  • Loading branch information
AntiKippi committed Jan 6, 2022
1 parent 929c487 commit 0b42873
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 30 deletions.
5 changes: 3 additions & 2 deletions constants.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var Fields = {
ENABLE_WIFI : 'enable-wifi',
ENABLE_BLUETOOTH : 'enable-bluetooth',
ENABLE_WIFI : 'enable-wifi',
ENABLE_BLUETOOTH : 'enable-bluetooth',
ENABLE_AIRPLANE_MODE : 'enable-airplane-mode',
};

var SCHEMA_NAME = 'org.gnome.shell.extensions.sane-airplane-mode';
72 changes: 51 additions & 21 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ const _ = Gettext.domain('sane-airplane-mode').gettext;

const Constants = ExtensionUtils.getCurrentExtension().imports.constants;

let ENABLE_WIFI = false;
let ENABLE_BLUETOOTH = true;
let ENABLE_WIFI = false;
let ENABLE_BLUETOOTH = true;
let ENABLE_AIRPLANE_MODE = true;

const setTimeout = (func, millis) => {
return GLib.timeout_add(GLib.PRIORITY_DEFAULT, millis, () => {
Expand Down Expand Up @@ -39,6 +40,9 @@ const SaneAirplaneMode = class SaneAirplaneMode {
// Initialize oldAirplaneMode
this._oldAirplaneMode = this._rfkillManager.airplaneMode;

// Initialize skipOnce
this._skipOnce = false;

// Connect to the airplane-mode-changed signal
this._airplaneHandlerId = this._rfkillManager.connect('airplane-mode-changed', this._handleAirplaneModeChange.bind(this));
}
Expand All @@ -50,28 +54,49 @@ const SaneAirplaneMode = class SaneAirplaneMode {
}

_handleAirplaneModeChange() {
if (!this._rfkillManager.airplaneMode && // Airplane mode is off and
this._oldAirplaneMode && // it was previously on, hence it must have been disabled
!this._rfkillManager._proxy.BluetoothAirplaneMode && // If Bluetooth is in airplane mode it can't have been disabled
!this._client.wireless_enabled // When genuinely disabling airplane mode wireless_enabled is false
if (this._skipOnce) {
this._skipOnce = false;
} else {
if (!this._rfkillManager.airplaneMode && // Airplane mode is off and
this._oldAirplaneMode && // it was previously on, hence it must have been disabled
!this._rfkillManager._proxy.BluetoothAirplaneMode && // If Bluetooth is in airplane mode it can't have been disabled
!this._client.wireless_enabled // When genuinely disabling airplane mode wireless_enabled is false
) {
// Both Wi-Fi and Bluetooth are disabled immediately after airplane mode is disabled
// and Bluetooth gets activated shortly afterwards without raising any event
// thus we need a little time delay to apply our settings.
// (I am not very happy with this but this is the only solution I can think of)
let index = this._timeouts.push(setTimeout(() => {
// Remove our timeout
this._timeouts.splice(index, 1);

// If Wi-Fi is enabled but Bluetooth isn't, airplane mode has been disabled
// as a side effect of Wi-Fi activation thus we don't apply our settings.
if (this._client.wireless_enabled && this._rfkillManager._proxy.BluetoothAirplaneMode) {
return;
}

this._client.wireless_enabled = ENABLE_WIFI;
this._rfkillManager._proxy.BluetoothAirplaneMode = !ENABLE_BLUETOOTH;
}, 50)) - 1;
}
}

if (!ENABLE_AIRPLANE_MODE && // Only do if the user disabled ENABLE_AIRPLANE_MODE in the settings
this._rfkillManager.airplaneMode && // Airplane mode is on and
!this._oldAirplaneMode && // it was previously off, hence it must have been enabled
this._client.wireless_enabled // Paradoxically if wireless_enabled is true, airplane mode was enabled by disabling Wi-Fi
) {
// Both Wi-Fi and Bluetooth are disabled immediately after airplane mode is disabled
// and Bluetooth gets activated shortly afterwards without raising any event
// thus we need a little time delay to apply our settings.
// (I am not very happy with this but this is the only solution I can think of)
this._skipOnce = true;
this._rfkillManager.airplaneMode = false;

// We need a timeout again here because Bluetooth gets activated shortly afterwards without any event
let index = this._timeouts.push(setTimeout(() => {
// Remove our timeout
this._timeouts.splice(index, 1);

// If Wi-Fi is enabled but Bluetooth isn't, airplane mode has been disabled
// as a side effect of Wi-Fi activation thus we don't apply our settings.
if (this._client.wireless_enabled && this._rfkillManager._proxy.BluetoothAirplaneMode) {
return;
}

this._client.wireless_enabled = ENABLE_WIFI;
this._rfkillManager._proxy.BluetoothAirplaneMode = !ENABLE_BLUETOOTH;
}, 100)) - 1;
this._rfkillManager._proxy.BluetoothAirplaneMode = true;
}, 50)) - 1;
}

this._oldAirplaneMode = this._rfkillManager.airplaneMode;
Expand All @@ -85,8 +110,9 @@ const SaneAirplaneMode = class SaneAirplaneMode {
}

_fetchSettings() {
ENABLE_WIFI = this._settings.get_boolean(Constants.Fields.ENABLE_WIFI);
ENABLE_BLUETOOTH = this._settings.get_boolean(Constants.Fields.ENABLE_BLUETOOTH);
ENABLE_WIFI = this._settings.get_boolean(Constants.Fields.ENABLE_WIFI);
ENABLE_BLUETOOTH = this._settings.get_boolean(Constants.Fields.ENABLE_BLUETOOTH);
ENABLE_AIRPLANE_MODE = this._settings.get_boolean(Constants.Fields.ENABLE_AIRPLANE_MODE);
}

_disconnectSettings() {
Expand All @@ -109,6 +135,10 @@ const SaneAirplaneMode = class SaneAirplaneMode {

_disconnectTimeouts() {
// Remove all active timeouts
if (!this._timeouts) {
return;
}

for (let i = 0; i < this._timeouts.length; i++) {
try {
if (this._timeouts[i]) {
Expand Down
2 changes: 1 addition & 1 deletion metadata.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": 6,
"version": 7,
"shell-version": [
"40", "41"
],
Expand Down
27 changes: 21 additions & 6 deletions prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ const App = GObject.registerClass(class Settings extends GObject.Object {
});
this.field_wifi_toggle = new Gtk.Switch();
this.field_bluetooth_toggle = new Gtk.Switch();
this.field_airplane_toogle = new Gtk.Switch();

let titleLabel = new Gtk.Label({
let airplaneTitleLabel = new Gtk.Label({
label: '<b>' + _('When disabling airplane mode') + ':</b>',
halign: Gtk.Align.START,
use_markup: true,
Expand All @@ -43,6 +44,17 @@ const App = GObject.registerClass(class Settings extends GObject.Object {
hexpand: true,
halign: Gtk.Align.START,
});
let wifiTitleLabel = new Gtk.Label({
label: '<b>' + _('When disabling Wi-FI') + ':</b>',
halign: Gtk.Align.START,
use_markup: true,
visible: true,
});
let enableAirplaneLabel = new Gtk.Label({
label: _('Enable airplane mode'),
hexpand: true,
halign: Gtk.Align.START,
});

const addRow = (main => {
let row = 0;
Expand Down Expand Up @@ -71,12 +83,15 @@ const App = GObject.registerClass(class Settings extends GObject.Object {
};
})(this.main);

addRow(titleLabel, undefined);
addRow(wifiLabel, this.field_wifi_toggle);
addRow(bluetoothLabel, this.field_bluetooth_toggle);
addRow(airplaneTitleLabel, undefined);
addRow(wifiLabel, this.field_wifi_toggle);
addRow(bluetoothLabel, this.field_bluetooth_toggle);
addRow(wifiTitleLabel, undefined);
addRow(enableAirplaneLabel, this.field_airplane_toogle);

SettingsSchema.bind(Constants.Fields.ENABLE_WIFI, this.field_wifi_toggle, 'active', Gio.SettingsBindFlags.DEFAULT);
SettingsSchema.bind(Constants.Fields.ENABLE_BLUETOOTH, this.field_bluetooth_toggle, 'active', Gio.SettingsBindFlags.DEFAULT);
SettingsSchema.bind(Constants.Fields.ENABLE_WIFI, this.field_wifi_toggle, 'active', Gio.SettingsBindFlags.DEFAULT);
SettingsSchema.bind(Constants.Fields.ENABLE_BLUETOOTH, this.field_bluetooth_toggle, 'active', Gio.SettingsBindFlags.DEFAULT);
SettingsSchema.bind(Constants.Fields.ENABLE_AIRPLANE_MODE, this.field_airplane_toogle, 'active', Gio.SettingsBindFlags.DEFAULT);
}
});

Expand Down
Binary file modified schemas/gschemas.compiled
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,10 @@
<summary>Enable Bluetooth</summary>
<description>If set, Bluetooth will be automatically turned on when airplane mode is disabled.</description>
</key>
<key type="b" name="enable-airplane-mode">
<default>true</default>
<summary>Enable airplane mode</summary>
<description>If set, airplane mode will be automatically turned on when Wi-Fi is disabled.</description>
</key>
</schema>
</schemalist>

0 comments on commit 0b42873

Please sign in to comment.