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

Commit

Permalink
s
Browse files Browse the repository at this point in the history
  • Loading branch information
gasolin committed Nov 5, 2014
1 parent 467d08f commit c744afc
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 31 deletions.
37 changes: 25 additions & 12 deletions apps/system/js/bluetooth.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ var Bluetooth = {
if (!window.navigator.mozSettings || !window.navigator.mozBluetooth) {
return;
}
// not observe or listen states in APIv2
if (typeof(window.navigator.mozBluetooth.onattributechanged) !==
'undefined') {
return;
}

var bluetooth = window.navigator.mozBluetooth;
var self = this;
Expand Down Expand Up @@ -137,6 +142,26 @@ var Bluetooth = {
window.dispatchEvent(evt);
}
);

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

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

// Get adapter for BluetoothTransfer when everytime bluetooth is enabled
Expand Down Expand Up @@ -211,18 +236,6 @@ var Bluetooth = {
return this.defaultAdapter;
},

enable: function bt_enable() {
SettingsListener.getSettingsLock().set({
'bluetooth.enabled': true
});
},

disable: function bt_disable() {
SettingsListener.getSettingsLock().set({
'bluetooth.enabled': false
});
},

/**
* maintain bluetooth enable/disable stat.
*/
Expand Down
8 changes: 4 additions & 4 deletions apps/system/js/power_save.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
/* global batteryOverlay, MozActivity, NotificationHelper, SettingsListener,
Bluetooth */
/* global batteryOverlay, MozActivity, NotificationHelper,
SettingsListener */

(function(exports) {

Expand Down Expand Up @@ -75,7 +75,7 @@

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

this._powerSaveEnabledLock = false;
},
Expand All @@ -89,7 +89,7 @@
settingsToSet[state] = true;
}
}
Bluetooth.enable();
window.dispatchEvent(new CustomEvent('bluetooth-enable'));

this.setMozSettings(settingsToSet);
},
Expand Down
6 changes: 3 additions & 3 deletions apps/system/js/quick_settings.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* jshint loopfunc: true */
/* global SettingsHelper, SettingsListener, AirplaneMode, applications,
UtilityTray, MozActivity, Bluetooth */
UtilityTray, MozActivity */

'use strict';

Expand Down Expand Up @@ -331,9 +331,9 @@

enabled = !!this.bluetooth.dataset.enabled;
if (enabled) {
Bluetooth.disable();
window.dispatchEvent(new CustomEvent('bluetooth-disable'));
} else {
Bluetooth.enable();
window.dispatchEvent(new CustomEvent('bluetooth-enable'));
}
break;

Expand Down
8 changes: 0 additions & 8 deletions apps/system/test/unit/mock_bluetooth.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,6 @@ var MockBluetooth = {
return this.mExpectedProfile === profile;
},

enable: function mbt_enable() {
this.enabled = true;
},

disable: function mbt_disable() {
this.enabled = false;
},

get isEnabled() {
return this.enabled;
}
Expand Down
7 changes: 3 additions & 4 deletions apps/system/test/unit/power_save_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ suite('power save >', function() {

suite('restores state >', function() {
test('restores all states', function() {
sinon.stub(window.Bluetooth, 'enable');
sinon.stub(window.Bluetooth, 'disable');
sinon.spy(window, 'dispatchEvent');
subject.start();
var state;
for (state in subject._states) {
Expand All @@ -40,7 +39,7 @@ suite('power save >', function() {

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

assert.ok(window.Bluetooth.disable.called);
assert.ok(window.dispatchEvent.calledOnce);
// States should be false now.
for (state in subject._states) {
if ('bluetooth.enabled' !== state) {
Expand All @@ -50,7 +49,7 @@ suite('power save >', function() {

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

assert.ok(window.Bluetooth.enable.called);
assert.ok(window.dispatchEvent.calledTwice);
// States should be restored.
for (state in subject._states) {
if ('bluetooth.enabled' !== state) {
Expand Down

0 comments on commit c744afc

Please sign in to comment.