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

Bug 1089511 - refactor system/js/bluetooth to future compatible format #25791

Merged
merged 1 commit into from Nov 21, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 16 additions & 6 deletions apps/system/js/airplane_mode_service_helper.js
Expand Up @@ -66,9 +66,14 @@

// switch the state to false if necessary
if (enabled) {
var eset = {};
eset[key + '.enabled'] = false;
this.writeSetting(eset);
// 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);
}
}
},
// turn on the mozSetting corresponding to `key'
Expand All @@ -84,9 +89,14 @@

// switch the state to true if it was suspended
if (suspended) {
var rset = {};
rset[key + '.enabled'] = true;
this.writeSetting(rset);
// 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);
}
}
},
_unsuspend: function(settingSuspendedID) {
Expand Down
74 changes: 59 additions & 15 deletions apps/system/js/bluetooth.js
@@ -1,6 +1,5 @@
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */

/* global SettingsListener, Service */
/* exported Bluetooth */
'use strict';

var Bluetooth = {
Expand Down Expand Up @@ -42,6 +41,13 @@ 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 @@ -54,19 +60,19 @@ var Bluetooth = {
/* this property store a reference of the default adapter */
defaultAdapter: null,

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

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

if (!window.navigator.mozBluetooth)
if (!window.navigator.mozSettings || !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 @@ -82,8 +88,9 @@ var Bluetooth = {
}
});

// when bluetooth adapter is ready, emit event to notify QuickSettings
// and try to get defaultAdapter at this moment
// when bluetooth adapter is ready, a.k.a enabled,
// emit event to notify QuickSettings and try to get
// defaultAdapter at this moment
bluetooth.onadapteradded = function bt_onAdapterAdded() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try this out
bluetooth.addEventListener('adapteradded', this);

var evt = document.createEvent('CustomEvent');
evt.initCustomEvent('bluetooth-adapter-added',
Expand Down Expand Up @@ -130,6 +137,28 @@ 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 @@ -138,8 +167,9 @@ 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 @@ -176,8 +206,9 @@ 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 @@ -193,8 +224,21 @@ var Bluetooth = {
}
},

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

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

(function(exports) {

Expand Down Expand Up @@ -50,9 +48,13 @@
setMozSettings: function(keypairs) {
var setlock = SettingsListener.getSettingsLock();
for (var key in keypairs) {
var obj = {};
obj[key] = keypairs[key];
setlock.set(obj);
// 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);
}
}
},

Expand All @@ -67,13 +69,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 @@ -87,6 +89,8 @@
settingsToSet[state] = true;
}
}
// Turn on Bluetooth
window.dispatchEvent(new CustomEvent('request-enable-bluetooth'));

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

enabled = !!this.bluetooth.dataset.enabled;
SettingsListener.getSettingsLock().set({
'bluetooth.enabled': !enabled
});
if (enabled) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, not a good idea to coupling Bluetooth.

window.dispatchEvent(
new CustomEvent('request-disable-bluetooth'));
} else {
window.dispatchEvent(
new CustomEvent('request-enable-bluetooth'));
}
break;

case this.airplaneMode:
Expand Down
3 changes: 2 additions & 1 deletion apps/system/js/system_nfc_connect_dialog.js
@@ -1,3 +1,4 @@
/* globals Service */
'use strict';

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

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

var msgId;
Expand Down
31 changes: 24 additions & 7 deletions apps/system/test/unit/airplane_mode_service_helper_test.js
Expand Up @@ -99,16 +99,24 @@ 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) {
assert.equal(
MockNavigatorSettings.mSettings[key + '.enabled'], false);
assert.equal(
MockNavigatorSettings.mSettings[key + '.suspended'], true);
if (key === 'bluetooth') {
assert.ok(window.dispatchEvent.calledTwice);
} else {
assert.equal(
MockNavigatorSettings.mSettings[key + '.enabled'], false);
assert.equal(
MockNavigatorSettings.mSettings[key + '.suspended'], true);
}
});
});
});
Expand Down Expand Up @@ -140,16 +148,25 @@ 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) {
assert.equal(MockNavigatorSettings.mSettings[key + '.enabled'], true);
assert.equal(
MockNavigatorSettings.mSettings[key + '.suspended'], false);
if (key === 'bluetooth') {
assert.ok(window.dispatchEvent.calledTwice);
} else {
assert.equal(MockNavigatorSettings.mSettings[key + '.enabled'],
true);
assert.equal(
MockNavigatorSettings.mSettings[key + '.suspended'], false);
}
});
});
});
Expand Down
4 changes: 4 additions & 0 deletions apps/system/test/unit/mock_bluetooth.js
Expand Up @@ -60,6 +60,10 @@ var MockBluetooth = {

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

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

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

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 @@ -32,16 +39,22 @@ suite('power save >', function() {

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

assert.ok(window.dispatchEvent.calledOnce);
// States should be false now.
for (state in subject._states) {
assert.equal(false, MockNavigatorSettings.mSettings[state]);
if ('bluetooth.enabled' !== 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) {
assert.equal(true, MockNavigatorSettings.mSettings[state]);
if ('bluetooth.enabled' !== state) {
assert.equal(true, MockNavigatorSettings.mSettings[state]);
}
}
});
});
Expand Down