Skip to content

Commit 45fe9b8

Browse files
committed
Bug 1102796 - [Bluetooth] bluetooth app support AMD for BT v2 API implementation
1 parent d8ce5f3 commit 45fe9b8

23 files changed

+1577
-78
lines changed

.jshintignore

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ apps/findmydevice/js/lib/*
4646
apps/findmydevice/js/config.js
4747
apps/system/js/uuid.js
4848
apps/camera/bower_components/**
49+
apps/bluetooth/js/vendor/alameda.js
4950
apps/bluetooth/js/settings.js
5051
tests/atoms/remote_date.js
5152
tests/atoms/screenshot.js

apps/bluetooth/.jshintrc

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../../.jshintrc",
3+
"predef": [
4+
"define",
5+
"require",
6+
"Promise",
7+
"Notification",
8+
"console"
9+
]
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
appDir: '..',
3+
baseUrl: 'js',
4+
mainConfigFile: '../js/config/require.js',
5+
dir: '../../../build_stage/bluetooth',
6+
7+
// Set the path to "empty" to prevent the scripts defining global objects
8+
// from being merged or they are removed after the optimization process,
9+
// which makes the objects inaccessible by reference.
10+
// If the inquiries to the object are all performed by requirejs, we can
11+
// remove the path of the object from the following list.
12+
paths: {
13+
'shared/bluetooth_helper': 'empty:'
14+
},
15+
16+
findNestedDependencies: true,
17+
18+
// Be sure to normalize all define() calls by extracting
19+
// dependencies so Function toString is not needed, and
20+
// lower capability devices like Tarako can optimize
21+
// memory by discarding function sources. This is
22+
// automatically done when an 'optimize' value other than
23+
// 'none' is used. This setting makes sure it happens for
24+
// builds where 'none' is used for 'optimize'.
25+
normalizeDirDefines: 'all',
26+
27+
// optimize is now passed via Makefile's GAIA_SETTINGS_MINIFY
28+
// default is none if not passed at all.
29+
// optimize: 'none',
30+
31+
// Just strip comments, no code compression or mangling.
32+
// Only active if optimize: 'uglify2'
33+
uglify2: {
34+
// Comment out the output section to get rid of line
35+
// returns and tabs spacing.
36+
output: {
37+
beautify: false
38+
},
39+
compress: true,
40+
mangle: true
41+
},
42+
43+
fileExclusionRegExp: /^\.|^test$|^build$/,
44+
45+
// Keeping build dir since Makefile cleans it up and
46+
// preps build dir with the shared directory
47+
keepBuildDir: true,
48+
removeCombined: true
49+
}

apps/bluetooth/build/build.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
/* global require, exports, dump */
4+
var utils = require('utils');
5+
6+
var BluetoothAppBuilder = function() {
7+
};
8+
9+
BluetoothAppBuilder.prototype.execute = function(options) {
10+
var optimize = 'optimize=' +
11+
(options.GAIA_OPTIMIZE === '1' ? 'uglify2' : 'none');
12+
var configFile = utils.getFile(options.APP_DIR, 'build',
13+
'bluetooth.build.jslike');
14+
var r = require('r-wrapper').get(options.GAIA_DIR);
15+
r.optimize([configFile.path, optimize], function() {
16+
dump('require.js optimize ok\n');
17+
}, function(err) {
18+
dump('require.js optmize failed:\n');
19+
dump(err + '\n');
20+
});
21+
};
22+
23+
exports.execute = function(options) {
24+
(new BluetoothAppBuilder()).execute(options);
25+
};

apps/bluetooth/js/config/require.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require.config({
2+
baseUrl: '/js',
3+
paths: {
4+
'modules': 'modules',
5+
'views': 'views',
6+
'shared': '../shared/js'
7+
},
8+
shim: {
9+
'shared/bluetooth_helper': {
10+
exports: 'BluetoothHelper'
11+
}
12+
}
13+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* VersionDetector:
3+
* - VersionDetector is an detector that identify the version of platform
4+
* Bluetooth object.
5+
* - It has only one method: getVersion.
6+
* VersionDetector only identify version and does not involve in any UI logic.
7+
*
8+
* @module modules/bluetooth/version_detector
9+
*/
10+
define(function(require) {
11+
'use strict';
12+
13+
var NavigatorBluetooth = require('modules/navigator/mozBluetooth');
14+
15+
var _debug = false;
16+
var Debug = function() {};
17+
if (_debug) {
18+
Debug = function vd_debug(msg) {
19+
console.log('--> [VersionDetector]: ' + msg);
20+
};
21+
}
22+
23+
var VersionDetector = {
24+
/**
25+
* The value indicates whether the API version is responding.
26+
*
27+
* @access public
28+
* @memberOf VersionDetector
29+
* @return {number}
30+
*/
31+
getVersion: function vd_getVersion() {
32+
if (!NavigatorBluetooth) {
33+
Debug('[VersionDetector]: navigator.mozBluetooth is not existed!!');
34+
// Since there is no navigator.mozBluetooth on B2G Desktop,
35+
// we workaround to return version 1 for Gaia UI test case.
36+
return 1;
37+
} else if
38+
(typeof(NavigatorBluetooth.onattributechanged) !== 'undefined') {
39+
Debug('[VersionDetector]: navigator.mozBluetooth is version 2!!');
40+
return 2;
41+
} else {
42+
Debug('[VersionDetector]: navigator.mozBluetooth is version 1!!');
43+
return 1;
44+
}
45+
}
46+
};
47+
48+
return VersionDetector;
49+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Wraps navigator.bluetooth for replacing it in unit tests more easily.
3+
*/
4+
define(function() {
5+
'use strict';
6+
7+
if (navigator.mozBluetooth) {
8+
return navigator.mozBluetooth;
9+
} else {
10+
return null;
11+
}
12+
});

apps/bluetooth/js/pair_manager.js apps/bluetooth/js/modules/pair_manager.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/* -*- Mode: js; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
22
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
3-
/* global BluetoothHelper, PairManager, PairExpiredDialog, Notification */
43

5-
'use strict';
4+
define(function(require) {
5+
'use strict';
66

7-
(function(exports) {
7+
var PairExpiredDialog = require('views/pair_expired_dialog');
8+
var BluetoothHelper = require('shared/bluetooth_helper');
89

910
var _ = window.navigator.mozL10n.get;
1011
var _debug = false;
@@ -15,7 +16,7 @@
1516
* incoming/outgoing pairing request.
1617
* 2. handling system message 'bluetooth-cancel' while some remote devices
1718
* request for canceling an overdue pairing request. The reason could be
18-
* cancel from remote devices, timeout, or other..w
19+
* cancel from remote devices, timeout, or other..
1920
*/
2021
var PairManager = {
2122
init: function() {
@@ -269,8 +270,5 @@
269270
}
270271
};
271272

272-
exports.PairManager = PairManager;
273-
274-
})(window);
275-
276-
navigator.mozL10n.once(PairManager.init.bind(PairManager));
273+
return PairManager;
274+
});

apps/bluetooth/js/startup_pair.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* global console */
2+
3+
'use strict';
4+
5+
require(['config/require'], function() {
6+
// Bluetooth API version detect
7+
require(['modules/bluetooth/version_detector'], function(versionDetector) {
8+
var version = versionDetector.getVersion();
9+
if (version === 1) {
10+
// load pair manager with version 1
11+
require(['modules/pair_manager'], function(PairManager) {
12+
navigator.mozL10n.once(() => PairManager.init());
13+
});
14+
} else if (version === 2) {
15+
// TODO: Load pair manager with API version 2.
16+
console.log('[startup_pair]: ' +
17+
'Load pair manager with API version 2.');
18+
}
19+
});
20+
});

apps/bluetooth/js/startup_transfer.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
require(['config/require'], function() {
4+
// Bluetooth API version detect
5+
require(['modules/bluetooth/version_detector'], function(versionDetector) {
6+
var version = versionDetector.getVersion();
7+
if (version === 1) {
8+
// load pair manager with version 1
9+
require(['deviceList'], function() {
10+
require(['transfer'], function() {
11+
});
12+
console.log('[startup_transfer]: ' +
13+
'Load deviceList module completely.');
14+
});
15+
} else if (version === 2) {
16+
// TODO: Load new script for transfer/device with API version 2.
17+
console.log('[startup_transfer]: ' +
18+
'Load new script for transfer/device with API version 2.');
19+
}
20+
});
21+
});

0 commit comments

Comments
 (0)