Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/apps/system/js/airplane_mode.js
blob: 781a53f56ef0d35a3984f7141ce26add358f4822 (plain)
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
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */

'use strict';

var AirplaneMode = {
  enabled: false,

  init: function apm_init() {
    if (!window.navigator.mozSettings)
      return;

    var mobileDataEnabled = false;
    SettingsListener.observe('ril.data.enabled', false, function(value) {
      mobileDataEnabled = value;
    });

    var bluetoothEnabled = false;
    SettingsListener.observe('bluetooth.enabled', false, function(value) {
      bluetoothEnabled = value;
    });

    var wifiEnabled = false;
    SettingsListener.observe('wifi.enabled', false, function(value) {
      wifiEnabled = value;
    });

    var geolocationEnabled = false;
    SettingsListener.observe('geolocation.enabled', false, function(value) {
      geolocationEnabled = value;
    });

    var bluetooth = window.navigator.mozBluetooth;
    var wifiManager = window.navigator.mozWifiManager;
    var mobileData = window.navigator.mozMobileConnection &&
      window.navigator.mozMobileConnection.data;
    var fmRadio = window.navigator.mozFMRadio;

    var restoreMobileData = false;
    var restoreBluetooth = false;
    var restoreWifi = false;
    var restoreGeolocation = false;
    // Note that we don't restore Wifi tethering when leaving airplane mode
    // because Wifi tethering can't be switched on before data connection is
    // established.

    var self = this;
    SettingsListener.observe('ril.radio.disabled', false, function(value) {
      if (value) {
        // Entering airplane mode.
        self.enabled = true;

        // Turn off mobile data
        // We toggle the mozSettings value here just for the sake of UI,
        // platform ril disconnects mobile data when
        // 'ril.radio.disabled' is true.
        if (mobileData) {
          restoreMobileData = mobileDataEnabled;
          if (mobileDataEnabled) {
            SettingsListener.getSettingsLock().set({
              'ril.data.enabled': false
            });
          }
        }

        // Turn off Bluetooth.
        if (bluetooth) {
          restoreBluetooth = bluetoothEnabled;
          if (bluetoothEnabled) {
            SettingsListener.getSettingsLock().set({
              'bluetooth.enabled': false
            });
          }
        }

        // Turn off Wifi.
        if (wifiManager) {
          restoreWifi = wifiEnabled;
          if (wifiEnabled) {
            SettingsListener.getSettingsLock().set({
              'wifi.enabled': false
            });
          }

          // Turn off Wifi tethering.
          SettingsListener.getSettingsLock().set({
            'tethering.wifi.enabled': false
          });
        }

        // Turn off Geolocation.
        restoreGeolocation = geolocationEnabled;
        if (geolocationEnabled) {
          SettingsListener.getSettingsLock().set({
            'geolocation.enabled': false
          });
        }

        // Turn off FM Radio.
        if (fmRadio && fmRadio.enabled)
          fmRadio.disable();

      } else {
        self.enabled = false;
        // Don't attempt to turn on mobile data if it's already on
        if (mobileData && !mobileDataEnabled && restoreMobileData) {
          SettingsListener.getSettingsLock().set({
            'ril.data.enabled': true
          });
        }

        // Don't attempt to turn on Bluetooth if it's already on
        if (bluetooth && !bluetooth.enabled && restoreBluetooth) {
          SettingsListener.getSettingsLock().set({
            'bluetooth.enabled': true
          });
        }

        // Don't attempt to turn on Wifi if it's already on
        if (wifiManager && !wifiManager.enabled && restoreWifi) {
          SettingsListener.getSettingsLock().set({
            'wifi.enabled': true
          });
        }

        // Don't attempt to turn on Geolocation if it's already on
        if (!geolocationEnabled && restoreGeolocation) {
          SettingsListener.getSettingsLock().set({
            'geolocation.enabled': true
          });
        }
      }
    });
  }
};

AirplaneMode.init();