B2G/gaia/apps/sms/js/utils.js
// Please remember to revoke the photoURL after utilizing it.
Please remember to revoke the photoURL after utilizing it.
Please remember to revoke the photoURL after utilizing it .
function ut_getContactDetails(number, contacts, include) {
      var details = {};
      include = include || {};
      function updateDetails(contact) {
        var name, phone, carrier, i, length, subscriber, org;
        name = contact.name[0];
        org = contact.org && contact.org[0];
        length = contact.tel ? contact.tel.length : 0;
        phone = length && contact.tel[0].value ? contact.tel[0] : {
          value: '',
          type: '',
          carrier: ''
        };
        carrier = phone.carrier;
        subscriber = number.length > 7 ? number.substr(-8) : number;
        for (i = 0; i < length; i++) {
          if (contact.tel[i].value.indexOf(subscriber) !== -1) {
            phone = contact.tel[i];
            carrier = phone.carrier;
            break;
          }
        }
        details.isContact = true;
        if (include.photoURL) {
          if (contact.photo && contact.photo[0]) {
            details.photoURL = URL.createObjectURL(contact.photo[0]);
          }
        }
        if (name) {
          for (i = 0; i < length; i++) {
            var telType = contact.tel[i].type && contact.tel[i].type.toString();
            var phoneType = phone.type && phone.type.toString();
            if (contact.tel[i].value !== phone.value &&
                telType === phoneType &&
                contact.tel[i].carrier === phone.carrier) {
              carrier = phone.value;
            }
          }
        }
        details.name = name;
        details.carrier = carrier || phone.value || '';
        details.org = details.org || org;
        if (phone.type) {
          details.carrier = phone.type + ' | ' + details.carrier;
        }
      }
      if (!contacts || (Array.isArray(contacts) && contacts.length === 0)) {
        details.title = '';
      } else if (!Array.isArray(contacts)) {
        updateDetails(contacts);
        details.title = details.name || details.org;
      } else {
        for (var i = 0, l = contacts.length; i < l; i++) {
          updateDetails(contacts[i]);
          if (details.name)
            break;
        }
        details.title = details.name || details.org;
      }
      return details;
    }
function ut_getContactDetails ( number , contacts , include ) { var details = { } ; include = include || { } ; function updateDetails ( contact ) { var name , phone , carrier , i , length , subscriber , org ; name = contact . name [ 0 ] ; org = contact . org && contact . org [ 0 ] ; length = contact . tel ? contact . tel . length : 0 ; phone = length && contact . tel [ 0 ] . value ? contact . tel [ 0 ] : { value : '' , type : '' , carrier : '' } ; carrier = phone . carrier ; subscriber = number . length > 7 ? number . substr ( - 8 ) : number ; for ( i = 0 ; i < length ; i ++ ) { if ( contact . tel [ i ] . value . indexOf ( subscriber ) !== - 1 ) { phone = contact . tel [ i ] ; carrier = phone . carrier ; break ; } } details . isContact = true ; if ( include . photoURL ) { if ( contact . photo && contact . photo [ 0 ] ) { details . photoURL = URL . createObjectURL ( contact . photo [ 0 ] ) ; } } if ( name ) { for ( i = 0 ; i < length ; i ++ ) { var telType = contact . tel [ i ] . type && contact . tel [ i ] . type . toString ( ) ; var phoneType = phone . type && phone . type . toString ( ) ; if ( contact . tel [ i ] . value !== phone . value && telType === phoneType && contact . tel [ i ] . carrier === phone . carrier ) { carrier = phone . value ; } } } details . name = name ; details . carrier = carrier || phone . value || '' ; details . org = details . org || org ; if ( phone . type ) { details . carrier = phone . type + ' | ' + details . carrier ; } } if ( ! contacts || ( Array . isArray ( contacts ) && contacts . length === 0 ) ) { details . title = '' ; } else if ( ! Array . isArray ( contacts ) ) { updateDetails ( contacts ) ; details . title = details . name || details . org ; } else { for ( var i = 0 , l = contacts . length ; i < l ; i ++ ) { updateDetails ( contacts [ i ] ) ; if ( details . name ) break ; } details . title = details . name || details . org ; } return details ; }
Please remember to revoke the photoURL after utilizing it.
Please remember to revoke the photoURL after utilizing it .
// Check which of the contacts phone number are we using // Based on E.164 (http://en.wikipedia.org/wiki/E.164) // Add data values for contact activity interaction // Add photo // Carrier logic // Check if other phones with same type and carrier // Convert the tel-type to string before tel-type comparison. // TODO : We might need to handle multiple tel type in the future. // We pick the first discovered org name as the phone number's detail // org information. // In no contact or contact with empty information cases, we will leave // the title as the empty string and let caller to decide the title. // Rule for fetching details with multiple contact entries: // 1) If we got more than 1 contact entry, find another entry if //    current entry got no name/company. // 2) If we could not get any information from all the entries, //    just display phone number.