VulpesOS/gaia/apps/communications/contacts/js/nfc.js
OverSu b38dea0597 Publish Vulpes Preview 15 sources for desktop, Android and Tundra
Share Gaia assets and adapters, keep private profiles and development documents local, and update both public READMEs.
2026-09-30 23:13:03 +02:00

94 lines
2.2 KiB
JavaScript
Executable file

'use strict';
/* global LazyLoader, ContactToVcard, MozNDEFRecord, fb, utils,
NDEF, NfcUtils, utils*/
/* exported NFC */
(function(exports) {
var mozNfc = window.navigator.mozNfc;
var currentContact;
var vCardContact;
var mozNfcPeer;
var startListening = function(contact) {
if (!mozNfc) {
return;
}
currentContact = contact;
// We cannot share Facebook data via NFC so we check if the contact
// is an FB contacts. However, if the contact is linked to a regular
// mozContact, we can share linked data
if (fb && !(fb.isFbContact(contact) && !fb.isFbLinked(contact))) {
mozNfc.onpeerready = handlePeerReady;
} else {
mozNfc.onpeerready = handlePeerReadyForFb;
}
};
var stopListening = function() {
if (!mozNfc) {
return;
}
mozNfc.onpeerready = null;
currentContact = null;
vCardContact = null;
mozNfcPeer = null;
};
var handlePeerReady = function(event) {
mozNfcPeer = event.peer;
LazyLoader.load([
'/shared/js/contact2vcard.js',
'/shared/js/setImmediate.js',
'/shared/js/nfc_utils.js'
], function() {
ContactToVcard(
[currentContact],
function append(vcard) {
vCardContact = vcard;
},
function success() {
sendContact();
},
// use default batch size
null,
// We don't want to share a profile photo via NFC,
// like on Android:
// https://bugzilla.mozilla.org/show_bug.cgi?id=1003767#c5
true
);
});
};
var sendContact = function() {
var nfcUtils = new NfcUtils();
var NDEFRecord = new MozNDEFRecord({
tnf: NDEF.TNF_MIME_MEDIA,
type: nfcUtils.fromUTF8('text/vcard'),
payload: nfcUtils.fromUTF8(vCardContact)
});
var promise = mozNfcPeer.sendNDEF([NDEFRecord]);
promise.then(() => {
console.log('Contact succesfuly sent');
}).catch(e => {
console.log('Something goes wrong : ' + e);
});
};
var handlePeerReadyForFb = function() {
utils.status.show({
id: 'facebook-export-forbidden'
});
};
var nfc_tools = {
startListening: startListening,
stopListening: stopListening
};
exports.NFC = nfc_tools;
}(window));