Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #26408 from gasolin/master
Browse files Browse the repository at this point in the history
Revert "Merge pull request #25791 from gasolin/issue-1089511-3"
  • Loading branch information
gasolin committed Nov 24, 2014
2 parents f09ed5a + 4ceae6c commit 4c32c19
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 159 deletions.
22 changes: 6 additions & 16 deletions apps/system/js/airplane_mode_service_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,9 @@

// switch the state to false if necessary
if (enabled) {
// make sure both BT API and settings key are handled
if ('bluetooth' === key) {
window.dispatchEvent(new CustomEvent('request-disable-bluetooth'));
} else {
var eset = {};
eset[key + '.enabled'] = false;
this.writeSetting(eset);
}
var eset = {};
eset[key + '.enabled'] = false;
this.writeSetting(eset);
}
},
// turn on the mozSetting corresponding to `key'
Expand All @@ -89,14 +84,9 @@

// switch the state to true if it was suspended
if (suspended) {
// make sure both BT API and settings key are handled
if ('bluetooth' === key) {
window.dispatchEvent(new CustomEvent('request-enable-bluetooth'));
} else {
var rset = {};
rset[key + '.enabled'] = true;
this.writeSetting(rset);
}
var rset = {};
rset[key + '.enabled'] = true;
this.writeSetting(rset);
}
},
_unsuspend: function(settingSuspendedID) {
Expand Down
74 changes: 15 additions & 59 deletions apps/system/js/bluetooth.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* global SettingsListener, Service */
/* exported Bluetooth */
/* -*- 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 Bluetooth = {
Expand Down Expand Up @@ -41,13 +42,6 @@ var Bluetooth = {
return connectedProfiles;
},

/**
* check if bluetooth profile is connected.
*
* @public
* @param {String} profile profile name
* @return {Boolean} connected state
*/
isProfileConnected: function bt_isProfileConnected(profile) {
var isConnected = this['_' + profile + 'Connected'];
if (isConnected === undefined) {
Expand All @@ -60,19 +54,19 @@ var Bluetooth = {
/* this property store a reference of the default adapter */
defaultAdapter: null,

/**
* keep a global connected property.
*
* @public
*/
/* keep a global connected property here */
connected: false,

init: function bt_init() {
if (!window.navigator.mozSettings || !window.navigator.mozBluetooth) {
if (!window.navigator.mozSettings)
return;

if (!window.navigator.mozBluetooth)
return;
}

var telephony = window.navigator.mozTelephony;
var bluetooth = window.navigator.mozBluetooth;
var settings = window.navigator.mozSettings;
var self = this;

SettingsListener.observe('bluetooth.enabled', true, function(value) {
Expand All @@ -88,9 +82,8 @@ var Bluetooth = {
}
});

// when bluetooth adapter is ready, a.k.a enabled,
// emit event to notify QuickSettings and try to get
// defaultAdapter at this moment
// when bluetooth adapter is ready, emit event to notify QuickSettings
// and try to get defaultAdapter at this moment
bluetooth.onadapteradded = function bt_onAdapterAdded() {
var evt = document.createEvent('CustomEvent');
evt.initCustomEvent('bluetooth-adapter-added',
Expand Down Expand Up @@ -137,28 +130,6 @@ var Bluetooth = {
window.dispatchEvent(evt);
}
);

window.addEventListener('request-enable-bluetooth', this);
window.addEventListener('request-disable-bluetooth', this);

Service.registerState('isEnabled', this);
},

handleEvent: function bt_handleEvent(evt) {
switch (evt.type) {
// enable bluetooth hardware and update settings value
case 'request-enable-bluetooth':
SettingsListener.getSettingsLock().set({
'bluetooth.enabled': true
});
break;
// disable bluetooth hardware and update settings value
case 'request-disable-bluetooth':
SettingsListener.getSettingsLock().set({
'bluetooth.enabled': false
});
break;
}
},

// Get adapter for BluetoothTransfer when everytime bluetooth is enabled
Expand All @@ -167,9 +138,8 @@ var Bluetooth = {
var self = this;

if (!bluetooth || !bluetooth.enabled ||
!('getDefaultAdapter' in bluetooth)) {
!('getDefaultAdapter' in bluetooth))
return;
}

var req = bluetooth.getDefaultAdapter();
req.onsuccess = function bt_gotDefaultAdapter(evt) {
Expand Down Expand Up @@ -206,9 +176,8 @@ var Bluetooth = {
updateConnected: function bt_updateConnected() {
var bluetooth = window.navigator.mozBluetooth;

if (!bluetooth || !('isConnected' in bluetooth)) {
if (!bluetooth || !('isConnected' in bluetooth))
return;
}

var wasConnected = this.connected;
this.connected = this.isProfileConnected(this.Profiles.HFP) ||
Expand All @@ -224,21 +193,8 @@ var Bluetooth = {
}
},

/**
* called by external for re-use adapter.
*
* @public
*/
// This function is called by external (BluetoothTransfer) for re-use adapter
getAdapter: function bt_getAdapter() {
return this.defaultAdapter;
},

/**
* maintain bluetooth enable/disable stat.
*
* @public
*/
get isEnabled() {
return window.navigator.mozBluetooth.enabled;
}
};
22 changes: 9 additions & 13 deletions apps/system/js/power_save.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';
/* global batteryOverlay, MozActivity, NotificationHelper,
SettingsListener */
/* global batteryOverlay */
/* global MozActivity */
/* global NotificationHelper */
/* global SettingsListener */

(function(exports) {

Expand Down Expand Up @@ -48,13 +50,9 @@
setMozSettings: function(keypairs) {
var setlock = SettingsListener.getSettingsLock();
for (var key in keypairs) {
// not set bluetooth key because we'll handle it separately
// for API compatibility
if ('bluetooth.enabled' !== key) {
var obj = {};
obj[key] = keypairs[key];
setlock.set(obj);
}
var obj = {};
obj[key] = keypairs[key];
setlock.set(obj);
}
},

Expand All @@ -69,13 +67,13 @@
'wifi.enabled': false,
// Turn off Data
'ril.data.enabled': false,
// Turn off Bluetooth
'bluetooth.enabled': false,
// Turn off Geolocation
'geolocation.enabled': false
};

this.setMozSettings(settingsToSet);
// Turn off Bluetooth
window.dispatchEvent(new CustomEvent('request-disable-bluetooth'));

this._powerSaveEnabledLock = false;
},
Expand All @@ -89,8 +87,6 @@
settingsToSet[state] = true;
}
}
// Turn on Bluetooth
window.dispatchEvent(new CustomEvent('request-enable-bluetooth'));

this.setMozSettings(settingsToSet);
},
Expand Down
10 changes: 3 additions & 7 deletions apps/system/js/quick_settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,9 @@
}

enabled = !!this.bluetooth.dataset.enabled;
if (enabled) {
window.dispatchEvent(
new CustomEvent('request-disable-bluetooth'));
} else {
window.dispatchEvent(
new CustomEvent('request-enable-bluetooth'));
}
SettingsListener.getSettingsLock().set({
'bluetooth.enabled': !enabled
});
break;

case this.airplaneMode:
Expand Down
3 changes: 1 addition & 2 deletions apps/system/js/system_nfc_connect_dialog.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* globals Service */
'use strict';

(function(exports) {
Expand All @@ -18,7 +17,7 @@
NfcConnectSystemDialog.prototype.DEBUG = false;

NfcConnectSystemDialog.prototype.setMessage = function ncsd_setMessage(name) {
var enabled = Service.query('Bluetooth.isEnabled');
var enabled = window.navigator.mozBluetooth.enabled;
var l10nArgs = { deviceName: name };

var msgId;
Expand Down
31 changes: 7 additions & 24 deletions apps/system/test/unit/airplane_mode_service_helper_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,24 +99,16 @@ suite('system/airplane_mode_service_helper.js', function() {
settingValue: false
});
});
sinon.spy(window, 'dispatchEvent');
});
teardown(function() {
window.dispatchEvent.restore();
});
test('turn on airplane mode, thus all ".enabled" should be false ' +
'and all ".suspended" should be true', function() {
subject.updateStatus(true);
MockNavigatorSettings.mReplyToRequests();
services.forEach(function(key) {
if (key === 'bluetooth') {
assert.ok(window.dispatchEvent.calledTwice);
} else {
assert.equal(
MockNavigatorSettings.mSettings[key + '.enabled'], false);
assert.equal(
MockNavigatorSettings.mSettings[key + '.suspended'], true);
}
assert.equal(
MockNavigatorSettings.mSettings[key + '.enabled'], false);
assert.equal(
MockNavigatorSettings.mSettings[key + '.suspended'], true);
});
});
});
Expand Down Expand Up @@ -148,25 +140,16 @@ suite('system/airplane_mode_service_helper.js', function() {
settingValue: true
});
});
sinon.spy(window, 'dispatchEvent');
});
teardown(function() {
window.dispatchEvent.restore();
});
test('turn on all services, then turn on airplane mode, ' +
'and turn off airplane mode. All ".suspended" and ".enabled" ' +
'should be just the same', function() {
subject.updateStatus(false);
MockNavigatorSettings.mReplyToRequests();
services.forEach(function(key) {
if (key === 'bluetooth') {
assert.ok(window.dispatchEvent.calledTwice);
} else {
assert.equal(MockNavigatorSettings.mSettings[key + '.enabled'],
true);
assert.equal(
MockNavigatorSettings.mSettings[key + '.suspended'], false);
}
assert.equal(MockNavigatorSettings.mSettings[key + '.enabled'], true);
assert.equal(
MockNavigatorSettings.mSettings[key + '.suspended'], false);
});
});
});
Expand Down
4 changes: 0 additions & 4 deletions apps/system/test/unit/mock_bluetooth.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ var MockBluetooth = {

isProfileConnected: function mbt_isProfileConnected(profile) {
return this.mExpectedProfile === profile;
},

get isEnabled() {
return this.enabled;
}
};

Expand Down
27 changes: 7 additions & 20 deletions apps/system/test/unit/power_save_test.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,29 @@
'use strict';
/* global MocksHelper, MockNavigatorSettings, MockSettingsListener,
PowerSave, MockBluetooth */
/* global MocksHelper */
/* global MockNavigatorSettings */
/* global MockSettingsListener */
/* global PowerSave */

requireApp('system/test/unit/mock_navigator_battery.js');
requireApp('system/shared/test/unit/mocks/mock_settings_listener.js');
requireApp('system/test/unit/mock_bluetooth.js');
requireApp('system/js/power_save.js');


var mocksForPowerSave = new MocksHelper([
'SettingsListener'
]).init();

suite('power save >', function() {

var realBluetooth;
var subject;

mocksForPowerSave.attachTestHelpers();
suiteSetup(function() {
realBluetooth = window.Bluetooth;
window.Bluetooth = MockBluetooth;

subject = new PowerSave();
});

suiteTeardown(function() {
window.Bluetooth = realBluetooth;
});

suite('restores state >', function() {
test('restores all states', function() {
sinon.spy(window, 'dispatchEvent');
subject.start();
var state;
for (state in subject._states) {
Expand All @@ -39,22 +32,16 @@ suite('power save >', function() {

MockSettingsListener.mCallbacks['powersave.enabled'](true);

assert.ok(window.dispatchEvent.calledOnce);
// States should be false now.
for (state in subject._states) {
if ('bluetooth.enabled' !== state) {
assert.equal(false, MockNavigatorSettings.mSettings[state]);
}
assert.equal(false, MockNavigatorSettings.mSettings[state]);
}

MockSettingsListener.mCallbacks['powersave.enabled'](false);

assert.ok(window.dispatchEvent.calledTwice);
// States should be restored.
for (state in subject._states) {
if ('bluetooth.enabled' !== state) {
assert.equal(true, MockNavigatorSettings.mSettings[state]);
}
assert.equal(true, MockNavigatorSettings.mSettings[state]);
}
});
});
Expand Down
Loading

0 comments on commit 4c32c19

Please sign in to comment.