Advertisement
Guest User

Untitled

a guest
Nov 6th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 65.22 KB | None | 0 0
  1. package com.revesoft.itelmobiledialer.service;
  2.  
  3. import android.content.Intent;
  4. import android.os.Build;
  5. import android.support.v4.content.LocalBroadcastManager;
  6. import android.text.TextUtils;
  7. import android.util.Log;
  8.  
  9. import com.revesoft.itelmobiledialer.data.UserDataManager;
  10. import com.revesoft.itelmobiledialer.e2eencryption.E2EConstants;
  11. import com.revesoft.itelmobiledialer.e2eencryption.E2EPublicKeyResponseReceiver;
  12. import com.revesoft.itelmobiledialer.phonebook.ContactEntry;
  13. import com.revesoft.itelmobiledialer.phonebook.DirectorySearchActivity;
  14. import com.revesoft.itelmobiledialer.phonebook.SearchContactUtil;
  15. import com.revesoft.itelmobiledialer.service.contactblock.ContactBlockHelper;
  16. import com.revesoft.itelmobiledialer.service.historyfetching.IMHistoryFetchingHelper;
  17. import com.revesoft.itelmobiledialer.service.phoneemailverification.PhoneEmailVerificationHelper;
  18. import com.revesoft.itelmobiledialer.signalling.SIPProvider;
  19. import com.revesoft.itelmobiledialer.util.ByteArray;
  20. import com.revesoft.itelmobiledialer.util.Configuration;
  21. import com.revesoft.itelmobiledialer.util.Constants;
  22. import com.revesoft.itelmobiledialer.util.MD5;
  23. import com.revesoft.itelmobiledialer.util.TaggedLogger;
  24. import com.revesoft.itelmobiledialer.util.Util;
  25.  
  26. import java.net.DatagramPacket;
  27. import java.util.ArrayList;
  28. import java.util.Collections;
  29. import java.util.Comparator;
  30. import java.util.HashSet;
  31. import java.util.Locale;
  32.  
  33. /**
  34.  * @author dhiman on 5/3/16.
  35.  */
  36. @SuppressWarnings("all")
  37. public class SignupManager {
  38.  
  39.  
  40.     /***************
  41.      * Message ID
  42.      *****************/
  43.     public static final byte MESSAGE_TYPE_SIGNUP = 1;
  44.     public static final byte MESSAGE_TYPE_SIGNUP_BY_EMAIL = 51;
  45.     public static final byte SEND_PASS_TO_MOBILE = 92;
  46.     public static final byte MESSAGE_TYPE_PASSWORD_BY_CALL = 2;
  47.     public static final byte MESSAGE_TYPE_PASSWORD_VERIFICATION = 3;
  48.     public static final int SIGN_UP_REQUEST_STATUS = 5;
  49.     private static final int ACK_MESSAGE_TYPE_SIGNUP = 5;
  50.     private static final int ACK_MESSAGE_TYPE_PASSWORD_BY_CALL = 6;
  51.     public static final int GET_PREFERENCE_LIST = 18;
  52.     public static final int GET_PREFERENCE_LIST_RESPONSE = 19;
  53.     public static final int GET_OFFICE_CONTACT_LIST = 20;
  54.     public static final int GET_OFFICE_CONTACT_LIST_RESPONSE = 21;
  55.     public static final int SET_DESIGNATION = 22;
  56.     public static final int SET_DESIGNATION_RESPONSE = 23;
  57.     public static final int SELECT_DESIGNATION_REQUEST = 24;
  58.     public static final int SELECT_DESIGNATION_RESPONSE = 25;
  59.     public static final int PROJAPI_CREDENTIAL_REQUEST = 26;
  60.     public static final int PROJAPI_CREDENTIAL_RESPONSE = 27;
  61.     public static final int RESPONSE_TYPE_ACK = 28;
  62.  
  63.     /***************
  64.      * Attribute ID
  65.      *****************/
  66.     public static final byte PHONE_NUMBER = 1;
  67.     public static final byte EMAIL_TO_VERIFY = 38;
  68.     public static final byte PASSWORD = 2;
  69.     public static final int STATUS = 3;
  70.     public static final int OPERATOR_CODE = 4;
  71.     public static final int SIGN_UP_STATUS = 4;
  72.     public static final int PASSWORD_NONCE = 5;
  73.     public static final int RESPONSE = 6;
  74.     public static final int PHONE_NUMBER_DETECTED = 7;
  75.     public static final int ENCRYPTED_PASSWORD = 8;
  76.     public static final int SEND_DEVICE_ID = 9;
  77.     public static final int NAME = 20;
  78.     public static final int HANDSET_LANGUAGE = 205;
  79.     public static final int VENDOR_NAME = 203;
  80.     public static final int MODEL_NAME = 204;
  81.  
  82.  
  83.     public static final int DESIGNATION = 21;
  84.     public static final int OFFICE_NAME = 22;
  85.     public static final int TOTAL_PACKET_COUNT = 23;
  86.     public static final int CURRENT_PACKET_SEQ = 24;
  87.     public static final int ERROR_DESCRIPTION = 25;
  88.     public static final int OFFICER_ID = 26;
  89.     public static final int OFFICER_NAME = 22;
  90.     public static final int NTIONAL_ID = 27;
  91.     public static final int OFFICE_ID = 29;
  92.     public static final int PROJAPOTI_USER_NAME = 30;
  93.     public static final int PROJAPOTI_USER_PASSWORD = 31;
  94.     public static final int DESIGNATION_ID = 32;
  95.     public static final int MISSING_PACKET_NO = 33;
  96.  
  97.     public static final int GET_MISSING_CONTACT_LIST_PACKET = 31;
  98.     public static final int GET_MISSING_CONTACT_LIST_PACKET_RESPONSE = 32;
  99.     public static final int GET_MISSING_PREFERENCE_LIST_PACKET = 29;
  100.     public static final int GET_MISSING_PREFERENCE_LIST_PACKET_RESPONSE = 30;
  101.     public static final int ACTUAL_PIN = 40;
  102.     public static final int IS_UPDATE = 46;
  103.     public static final int PROFILE_PICTURE = 53;
  104.     public static final int PROFILE_HASH = 54;
  105.  
  106.     public static final int GET_MISSING_DESIGNATION = 63;
  107.     public static final int GET_MISSING_DESIGNATION_RESPONSE = 64;
  108.  
  109.  
  110.     public static final int SEARCH_CONTACT = 33;
  111.     public static final int SEARCH_CONTACT_RESPONSE = 34;
  112.     public static final int GET_MISSING_SEARCH_CONTACT_PACKET = 35;
  113.     public static final int GET_MISSING_SEARCH_CONTACT_PACKET_RESPONSE = 36;
  114.  
  115.  
  116.     public static final int MINISTRY_NAME = 34;
  117.     public static final int MINISTRY_ID = 35;
  118.     public static final int LEVEL_NAME = 38;
  119.     public static final int LEVEL_ID = 39;
  120.     public static final int OFFICE_UNIT_ID = 37;
  121.     public static final int OFFICE_UNIT_NAME = 36;
  122.  
  123.     public static final int GET_MINISTRY_LIST = 41;
  124.     public static final int GET_MINISTRY_LIST_RESPONSE = 42;
  125.     public static final int GET_MISSING_MINISTRY_LIST_PACKET = 43;
  126.     public static final int GET_MISSING_MINISTRY_LIST_PACKET_RESPONSE = 44;
  127.  
  128.     public static final int GET_LEVEL_LIST = 45;
  129.     public static final int GET_LEVEL_LIST_RESPONSE = 46;
  130.     public static final int GET_MISSING_LEVEL_LIST_PACKET = 47;
  131.     public static final int GET_MISSING_LEVEL_LIST_PACKET_RESPONSE = 48;
  132.  
  133.     public static final int GET_OFFICE_LIST = 49;
  134.     public static final int GET_OFFICE_LIST_RESPONSE = 50;
  135.     public static final int GET_MISSING_OFFICE_LIST_PACKET = 51;
  136.     public static final int GET_MISSING_OFFICE_LIST_PACKET_RESPONSE = 52;
  137.  
  138.     public static final int GET_UNIT_LIST = 53;
  139.     public static final int GET_UNIT_LIST_RESPONSE = 54;
  140.     public static final int GET_MISSING_UNIT_LIST_PACKET = 55;
  141.     public static final int GET_MISSING_UNIT_LIST_PACKET_RESPONSE = 56;
  142.  
  143.     public static final int GET_DESIGNATION_LIST = 57;
  144.     public static final int GET_DESIGNATION_LIST_RESPONSE = 58;
  145.     public static final int GET_MISSING_DESIGNATION_LIST_PACKET = 59;
  146.     public static final int GET_MISSING_DESIGNATION_LIST_PACKET_RESPONSE = 60;
  147.  
  148.  
  149.     public static final int GET_OFFICER_BY_DESIGNATION = 61;
  150.     public static final int GET_OFFICER_BY_DESIGNATION_RESPONSE = 62;
  151.     public static final int GET_OFFICER_BY_DESIGNATION_MISSING_RESPONSE = 87;
  152.     private static final int GET_OFFICER_INFO_BY_PIN = 73;
  153.     private static final int GET_OFFICER_INFO_BY_PIN_RESPONSE = 74;
  154.  
  155.     private static final int ADVANCE_SEARCH_REQUEST = 77;
  156.     private static final int ADVANCE_SEARCH_MISSING_REQUEST = 78;
  157.     private static final int ADVANCE_SEARCH_RESPONSE = 79;
  158.     private static final int ADVANCE_SEARCH_MISSING_RESPONSE = 80;
  159.  
  160.  
  161.     private static final int REQUEST_BY_LABEL = 81;
  162.     private static final int MISSING_REQUEST_BY_LABEL = 82;
  163.     private static final int REQUEST_BY_LABEL_RESPONSE = 83;
  164.     private static final int REQUEST_BY_LABEL_MISSING_RESPONSE = 84;
  165.  
  166.     private static final int LABEL = 47;
  167.     private static final int LABEL_ID = 48;
  168.     private static final int VALUE = 49;
  169.     private static final int VALUE_ID = 50;
  170.     public static final int NUMBER_OF_SEARCH_RESULT = 51;
  171.     public static final int MAX_CONTACT_TO_SHOW = 52;
  172.  
  173.  
  174.     private static final int UPDATE_PROFILE = 85;
  175.     private static final int UPDATE_PROFILE_RESPONSE = 86;
  176.  
  177.     private static final int BUDDY_LIST_ADDED = 88;
  178.     public static final int GET_ALL_INFO_BY_PIN = 89;
  179.     public static final int GET_ALL_INFO_BY_PIN_RESPONSE = 90;
  180.     public static final int GET_SEARCH_RESULT = 91;
  181.  
  182.  
  183.     /**
  184.      * * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
  185.      **/
  186.  
  187.     public static boolean isPhoneNumberAutoDetected = false;
  188.     public static boolean sendSMSFromPhone = false;
  189.     public static boolean passverificationNeeded = false;
  190.  
  191.     private SIPProvider sipProvider;
  192.     private DialerService mService;
  193.  
  194.     public static HashSet<String> fetchedNumbers = new HashSet<>();
  195.  
  196.     private ByteArray keyAutoSignUp = new ByteArray(100);
  197.     private ByteArray encryptedPassword = new ByteArray(100);
  198.     private ByteArray pinNumber = new ByteArray(100);
  199.  
  200.     private ContactBlockHelper contactBlockHelper;
  201.     private PhoneEmailVerificationHelper phoneEmailVerificationHelper;
  202.     IMHistoryFetchingHelper imHistoryFetchingHelper;
  203.  
  204.     public SignupManager(SIPProvider sipProvider, DialerService mService) {
  205.         this.sipProvider = sipProvider;
  206.         this.mService = mService;
  207.         fetchedNumbers.clear();
  208. //      handler = new Handler(mService.getMainLooper());
  209.     }
  210.  
  211.     public void setContactBlockHelper(ContactBlockHelper contactBlockHelper) {
  212.         this.contactBlockHelper = contactBlockHelper;
  213.     }
  214.  
  215.     public void setPhoneEmailVerificationHelper(PhoneEmailVerificationHelper phoneEmailVerificationHelper) {
  216.         this.phoneEmailVerificationHelper = phoneEmailVerificationHelper;
  217.     }
  218.  
  219.     private static final String TAG = "Signup";
  220.  
  221.     public void sendSignupPacket(int type, String password) {
  222.         try {
  223.             byte[] packet = new byte[1000];
  224.             int length = 0;
  225.             if (type == Constants.SIGNUP_REQUEST) {
  226.                 if (Util.isValidEmail(Util.getProperEmail(UserDataManager.getUserName()))) {
  227.                     length = buildPacketForEmailSignUp(packet);
  228.                 } else {
  229.                     length = buildPacketForSignUp(packet);
  230.                 }
  231.             } else if (type == Constants.REGISTRATION_VERIFICATION_REQUEST) {
  232.                 length = buildPacketForRegistrationVerification(packet, password);
  233.             } else if (type == Constants.VOICE_CALL_REQUEST) {
  234.                 length = buildPacketForVoiceCall(packet);
  235.             }
  236.  
  237.             int code = (packet[0] << 8)  & packet[1];
  238.             logger.log("sendSignupPacket type code = "+code);
  239.  
  240.             Log.i("Signup", "Sending signup packet to : " + sipProvider.autoSignUpAddress + " Data length: " + length);
  241.             if (sipProvider.autoSignUpAddress != null) {
  242.                 Log.i("Signup", "Sending signup packet : " + new String(packet));
  243.                 DatagramPacket outDatagramPacket = new DatagramPacket(packet, length);
  244.                 outDatagramPacket.setSocketAddress(sipProvider.autoSignUpAddress);
  245.                 sipProvider.sendSignupPacket(outDatagramPacket);
  246.             }
  247.         } catch (Exception ex) {
  248.             System.out.println("error to send signup packet:" + ex);
  249.         }
  250.     }
  251.  
  252.     public void processAutoSignUp(byte[] udpSignalRecvBuffer, int udpSignalRecvBufferLength) {
  253.         Log.i("SignupManager", "Response processAutoSignUp: " + new String(udpSignalRecvBuffer));
  254.         if (udpSignalRecvBufferLength > 0) {
  255.             int index = 0;
  256.             int messageType = udpSignalRecvBuffer[index++] & 0x00ff;
  257.             messageType = (messageType << 8) | (udpSignalRecvBuffer[index++] & 0x00ff);
  258.             int messageLength = udpSignalRecvBuffer[index++] & 0x00ff;
  259.             messageLength = (messageLength << 8) | (udpSignalRecvBuffer[index++] & 0x00ff);
  260.  
  261.             Log.i("SignupManager", "processAutoSignUp: messageType: " + messageType + " MessageLength: " + messageLength);
  262.             if (messageType == SIGN_UP_STATUS && messageLength > 0) {
  263.                 parseSignupRequestStatusRespponse(udpSignalRecvBuffer);
  264.             } else if (messageType == SIGN_UP_REQUEST_STATUS) {
  265.                 parseSignupACKResponse(udpSignalRecvBuffer);
  266.             } else if (messageType == ACK_MESSAGE_TYPE_PASSWORD_BY_CALL) {
  267.                 if (SIPProvider.DEBUG)
  268.                     Log.d("SignupManager", "processAutoSignUp: got ACK_MESSAGE_TYPE_PASSWORD_BY_CALL");
  269.                 // signupRunning = true;
  270.             } else if (messageType == RESPONSE_TYPE_ACK) {
  271.                 Log.w("SignupMananger", "RESPONSE_TYPE_ACK");
  272.             } else if (messageType == SEARCH_CONTACT_RESPONSE) {
  273.                 processSearchContactResponse(udpSignalRecvBuffer);
  274.             } else if (messageType == GET_MISSING_SEARCH_CONTACT_PACKET_RESPONSE) {
  275.                 processMissingSearchContactResponse(udpSignalRecvBuffer);
  276.             } else if (messageType == ContactBlockHelper.BLOCK_CONTACT_RESPONSE) {
  277.                 contactBlockHelper.processBlockContactResponse(udpSignalRecvBuffer);
  278.             } else if (messageType == ContactBlockHelper.UNBLOCK_CONTACT_RESPONSE) {
  279.                 contactBlockHelper.processUnblockContactResponse(udpSignalRecvBuffer);
  280.             } else if (messageType == ContactBlockHelper.GET_BLOCK_CONTACT_LIST_RESPONSE) {
  281.                 contactBlockHelper.processBlockListResponse(udpSignalRecvBuffer);
  282.             } else if (messageType == ContactBlockHelper.GET_MISSING_BLOCK_CONTACT_LIST_RESPONSE) {
  283.                 contactBlockHelper.processBlockListResponse(udpSignalRecvBuffer);
  284.             } else if (messageType == PhoneEmailVerificationHelper.EMAIL_OR_PHONE_VERIFICATION_CODE_RESPONSE) {
  285.                 phoneEmailVerificationHelper.processPhoneVerificationResponse(udpSignalRecvBuffer);
  286.             } else if (messageType == PhoneEmailVerificationHelper.EMAIL_OR_PHONE_VERIFICATION_RESPONSE) {
  287.                 phoneEmailVerificationHelper.processPhoneVerificationResponse(udpSignalRecvBuffer);
  288.             } else if (messageType == E2EConstants.GET_PUBLIC_KEY_STATUS) {
  289.                 E2EPublicKeyResponseReceiver.processE2EPublicKeyResponse(udpSignalRecvBuffer);
  290.             } else if (messageType == IMHistoryFetchingHelper.GET_IM_HISTORY_RESPONSE) {
  291.                 if (imHistoryFetchingHelper != null)
  292.                     imHistoryFetchingHelper.processIMHistoryResponse(udpSignalRecvBuffer);
  293.             } else if (messageType == IMHistoryFetchingHelper.GET_MISSING_IM_HISTORY_RESPONSE) {
  294.                 if (imHistoryFetchingHelper != null)
  295.                     imHistoryFetchingHelper.processIMHistoryResponse(udpSignalRecvBuffer);
  296.             }
  297.         }
  298.     }
  299.  
  300.     private int buildPacketForSignUp(byte[] packet) {
  301.         byte[] opcode = mService.getOperatorCode().getBytes();
  302.         byte[] mobileNumber = mService.getUsername().getBytes();
  303.         String handsetLanguageISO = Locale.getDefault().getLanguage();
  304.         if (TextUtils.isEmpty(handsetLanguageISO)) {
  305.             handsetLanguageISO = "en";
  306.         }
  307.         byte[] handsetLanguageISOAsByte = handsetLanguageISO.getBytes();
  308.         logger.log("buildPacketForSignUp handsetLanguageISO = " + handsetLanguageISO);
  309.         ByteArray imeiNumber = new ByteArray(Util.getIMEI(mService));
  310.  
  311.         int index = 0;
  312.  
  313.         Log.d("Signup", "buildPacketForSignUp");
  314.  
  315.         packet[index++] = 0x00; // packet type==sign up
  316.         packet[index++] = MESSAGE_TYPE_SIGNUP;
  317.  
  318.         // packet[index++]=0x00; //total packet length
  319.         // packet[index++]=(byte)(4+mobileNumber.length+4);
  320.         index += 2;
  321.  
  322.         if (SIPProvider.getStunInfo().signupNumbers.size() > 0) {
  323.             sendSMSFromPhone = true;
  324.             isPhoneNumberAutoDetected = true;
  325.         }
  326.  
  327.         if (!sendSMSFromPhone) {
  328.             packet[index++] = 0x00; // attribute type==mobile number
  329.             packet[index++] = PHONE_NUMBER;
  330.  
  331.             packet[index++] = 0x00; // attribute length
  332.             packet[index++] = (byte) (mobileNumber.length);
  333.  
  334.             for (int m = 0; m < mobileNumber.length; ++m) // attribute value
  335.             {
  336.                 packet[index++] = mobileNumber[m];
  337.             }
  338.         }
  339.         packet[index++] = 0x00; // attribute type==opcode
  340.         packet[index++] = OPERATOR_CODE;
  341.  
  342.         packet[index++] = 0x00; // attribute length
  343.         packet[index++] = (byte) (opcode.length);
  344.  
  345.         for (int m = 0; m < opcode.length; ++m) // attribute value
  346.         {
  347.             packet[index++] = opcode[m];
  348.         }
  349.  
  350.         //language iso
  351.         packet[index++] = HANDSET_LANGUAGE >> 8;
  352.         packet[index++] = (byte) (HANDSET_LANGUAGE & 0x00FF);
  353.  
  354.         packet[index++] = 0x00; // attribute length
  355.         packet[index++] = (byte) (handsetLanguageISOAsByte.length);
  356.  
  357.         for (int m = 0; m < handsetLanguageISOAsByte.length; ++m) // attribute value
  358.         {
  359.             packet[index++] = handsetLanguageISOAsByte[m];
  360.         }
  361.  
  362.         //vendor
  363.         packet[index++] = VENDOR_NAME >> 8;
  364.         packet[index++] = (byte) (VENDOR_NAME & 0x00FF);
  365.  
  366.         String vendor = Build.MANUFACTURER;
  367.  
  368.         packet[index++] = 0x00; // attribute length
  369.         packet[index++] = (byte) (vendor.getBytes().length);
  370.  
  371.         for (int m = 0; m < vendor.getBytes().length; ++m) // attribute value
  372.         {
  373.             packet[index++] = vendor.getBytes()[m];
  374.         }
  375.  
  376.         //model
  377.         packet[index++] = MODEL_NAME >> 8;
  378.         packet[index++] = (byte) (MODEL_NAME & 0x00FF);
  379.  
  380.         String model = Build.MODEL;
  381.  
  382.         packet[index++] = 0x00; // attribute length
  383.         packet[index++] = (byte) (model.getBytes().length);
  384.  
  385.         for (int m = 0; m < model.getBytes().length; ++m) // attribute value
  386.         {
  387.             packet[index++] = model.getBytes()[m];
  388.         }
  389.  
  390.         if (isPhoneNumberAutoDetected || passverificationNeeded) {
  391.             packet[index++] = 0x00;
  392.             packet[index++] = PHONE_NUMBER_DETECTED;
  393.  
  394.             packet[index++] = 0x00; // attribute length
  395.             packet[index++] = (byte) (1);
  396.             packet[index++] = 1;
  397.         }
  398.  
  399.         final int DEVICE_OS = 11;
  400.         final int ANDROID = 0x3800;
  401.         packet[index++] = (byte) ((DEVICE_OS >> 8) & 0x00FF);
  402.         packet[index++] = (byte) (DEVICE_OS & 0x00FF);
  403.         packet[index++] = (byte) ((2 >> 8) & 0x00FF);
  404.         packet[index++] = (byte) (2 & 0x00FF);
  405.         packet[index++] = (byte) ((ANDROID >> 8) & 0x00FF);
  406.         packet[index++] = (byte) (ANDROID & 0x00FF);
  407.  
  408.         // if (sendSMSFromPhone) {
  409.         packet[index++] = (byte) ((SEND_DEVICE_ID >> 8) & 0x00FF);
  410.         packet[index++] = (byte) (SEND_DEVICE_ID & 0x00FF);
  411.         ;
  412.         packet[index++] = (byte) ((imeiNumber.length >> 8) & 0x00FF); // attribute
  413.         // lengt
  414.         packet[index++] = (byte) (imeiNumber.length & 0x00FF);
  415.  
  416.         for (int m = 0; m < imeiNumber.length; ++m) // attribute value
  417.         {
  418.             packet[index++] = imeiNumber.getCharacterAt(m);
  419.         }
  420.         // }
  421.  
  422.         final int VERSION_ID = 0x9011;
  423.  
  424.         ByteArray version = new ByteArray(Configuration.VERSION);
  425.         packet[index++] = (byte) ((VERSION_ID >> 8) & 0x00FF);
  426.         packet[index++] = (byte) (VERSION_ID & 0x00FF);
  427.         packet[index++] = (byte) ((version.length >> 8) & 0x00FF); // attribute
  428.         // lengt
  429.         packet[index++] = (byte) (version.length & 0x00FF);
  430.  
  431.         for (int m = 0; m < version.length; ++m) // attribute value
  432.         {
  433.             packet[index++] = version.getCharacterAt(m);
  434.         }
  435.  
  436.         int l = index - 4;
  437.         packet[2] = (byte) ((l >> 8) & 0x00FF);
  438.         packet[3] = (byte) (l & 0x00FF);
  439.         return index;
  440.     }
  441.  
  442.     TaggedLogger logger = new TaggedLogger("signUpManager");
  443.  
  444.     private int buildPacketForEmailSignUp(byte[] packet) {
  445.         logger.log("buildPacketForEmailSignUp called");
  446.         byte[] opcode = mService.getOperatorCode().getBytes();
  447.         byte[] formattedEmail = mService.getUsername().getBytes();
  448.         byte[] properEmail = Util.getProperEmail(mService.getUsername()).getBytes();
  449.         String handsetLanguageISO = Locale.getDefault().getLanguage();
  450.         if (TextUtils.isEmpty(handsetLanguageISO)) {
  451.             handsetLanguageISO = "en";
  452.         }
  453.         logger.log("buildPacketForEmailSignUp handsetLanguageISO = " + handsetLanguageISO);
  454.         byte[] handsetLanguageISOAsByte = handsetLanguageISO.getBytes();
  455.         logger.log("buildPacketForEmailSignUp proper email = " + Util.getProperEmail(mService.getUsername()));
  456.         logger.log("buildPacketForEmailSignUp formatted email = " + mService.getUsername());
  457.         ByteArray imeiNumber = new ByteArray(Util.getIMEI(mService));
  458.  
  459.         int index = 0;
  460.  
  461.         Log.d("Signup", "buildPacketForSignUp");
  462.  
  463.         packet[index++] = 0x00; // packet type==sign up
  464.         packet[index++] = MESSAGE_TYPE_SIGNUP_BY_EMAIL;
  465.  
  466.         // packet[index++]=0x00; //total packet length
  467.         // packet[index++]=(byte)(4+mobileNumber.length+4);
  468.         index += 2;
  469.  
  470.  
  471.         //phone number field e formatted email jabe
  472.         packet[index++] = 0x00; // attribute type mobile number
  473.         packet[index++] = PHONE_NUMBER;
  474.  
  475.         packet[index++] = 0x00; // attribute length
  476.         packet[index++] = (byte) (formattedEmail.length);
  477.  
  478.         for (int m = 0; m < formattedEmail.length; ++m) // attribute value
  479.         {
  480.             packet[index++] = formattedEmail[m];
  481.         }
  482.  
  483.         //language iso
  484.         packet[index++] = HANDSET_LANGUAGE >> 8;
  485.         packet[index++] = (byte) (HANDSET_LANGUAGE & 0x00FF);
  486.  
  487.         packet[index++] = 0x00; // attribute length
  488.         packet[index++] = (byte) (handsetLanguageISOAsByte.length);
  489.  
  490.         for (int m = 0; m < handsetLanguageISOAsByte.length; ++m) // attribute value
  491.         {
  492.             packet[index++] = handsetLanguageISOAsByte[m];
  493.         }
  494.  
  495.         //vendor
  496.         packet[index++] = VENDOR_NAME >> 8;
  497.         packet[index++] = (byte) (VENDOR_NAME & 0x00FF);
  498.  
  499.         String vendor = Build.MANUFACTURER;
  500.  
  501.         packet[index++] = 0x00; // attribute length
  502.         packet[index++] = (byte) (vendor.getBytes().length);
  503.  
  504.         for (int m = 0; m < vendor.getBytes().length; ++m) // attribute value
  505.         {
  506.             packet[index++] = vendor.getBytes()[m];
  507.         }
  508.  
  509.         //model
  510.         packet[index++] = MODEL_NAME >> 8;
  511.         packet[index++] = (byte) (MODEL_NAME & 0x00FF);
  512.  
  513.         String model = Build.MODEL;
  514.  
  515.         packet[index++] = 0x00; // attribute length
  516.         packet[index++] = (byte) (model.getBytes().length);
  517.  
  518.         for (int m = 0; m < model.getBytes().length; ++m) // attribute value
  519.         {
  520.             packet[index++] = model.getBytes()[m];
  521.         }
  522.  
  523.         //email to verify field e  field e actual email jabe
  524.         packet[index++] = 0x00; // actual email on which otp will be sent
  525.         packet[index++] = EMAIL_TO_VERIFY;
  526.  
  527.         packet[index++] = 0x00; // attribute length
  528.         packet[index++] = (byte) (properEmail.length);
  529.  
  530.         for (int m = 0; m < properEmail.length; ++m) // attribute value
  531.         {
  532.             packet[index++] = properEmail[m];
  533.         }
  534.  
  535.         packet[index++] = 0x00; // attribute type==opcode
  536.         packet[index++] = OPERATOR_CODE;
  537.  
  538.         packet[index++] = 0x00; // attribute length
  539.         packet[index++] = (byte) (opcode.length);
  540.  
  541.         for (int m = 0; m < opcode.length; ++m) // attribute value
  542.         {
  543.             packet[index++] = opcode[m];
  544.         }
  545.  
  546.         //??? ki korbo eituk?
  547.         if (isPhoneNumberAutoDetected || passverificationNeeded) {
  548.             packet[index++] = 0x00;
  549.             packet[index++] = PHONE_NUMBER_DETECTED;
  550.  
  551.             packet[index++] = 0x00; // attribute length
  552.             packet[index++] = (byte) (1);
  553.             packet[index++] = 1;
  554.         }
  555.  
  556.         final int DEVICE_OS = 11;
  557.         final int ANDROID = 0x3800;
  558.         packet[index++] = (byte) ((DEVICE_OS >> 8) & 0x00FF);
  559.         packet[index++] = (byte) (DEVICE_OS & 0x00FF);
  560.         packet[index++] = (byte) ((2 >> 8) & 0x00FF);
  561.         packet[index++] = (byte) (2 & 0x00FF);
  562.         packet[index++] = (byte) ((ANDROID >> 8) & 0x00FF);
  563.         packet[index++] = (byte) (ANDROID & 0x00FF);
  564.  
  565.         // if (sendSMSFromPhone) {
  566.         packet[index++] = (byte) ((SEND_DEVICE_ID >> 8) & 0x00FF);
  567.         packet[index++] = (byte) (SEND_DEVICE_ID & 0x00FF);
  568.         ;
  569.         packet[index++] = (byte) ((imeiNumber.length >> 8) & 0x00FF); // attribute
  570.         // lengt
  571.         packet[index++] = (byte) (imeiNumber.length & 0x00FF);
  572.  
  573.         for (int m = 0; m < imeiNumber.length; ++m) // attribute value
  574.         {
  575.             packet[index++] = imeiNumber.getCharacterAt(m);
  576.         }
  577.         // }
  578.  
  579.         final int VERSION_ID = 0x9011;
  580.  
  581.         ByteArray version = new ByteArray(Configuration.VERSION);
  582.         packet[index++] = (byte) ((VERSION_ID >> 8) & 0x00FF);
  583.         packet[index++] = (byte) (VERSION_ID & 0x00FF);
  584.         packet[index++] = (byte) ((version.length >> 8) & 0x00FF); // attribute
  585.         // lengt
  586.         packet[index++] = (byte) (version.length & 0x00FF);
  587.  
  588.         for (int m = 0; m < version.length; ++m) // attribute value
  589.         {
  590.             packet[index++] = version.getCharacterAt(m);
  591.         }
  592.  
  593.         int l = index - 4;
  594.         packet[2] = (byte) ((l >> 8) & 0x00FF);
  595.         packet[3] = (byte) (l & 0x00FF);
  596.         return index;
  597.     }
  598.  
  599.     private int buildPacketForVoiceCall(byte[] packet) {
  600.         byte[] opcode = mService.getOperatorCode().getBytes();
  601.         byte[] mobileNumber = mService.getUsername().getBytes();
  602.         logger.log("opcode = "+mService.getOperatorCode());
  603.         logger.log("mobileNumber = "+mService.getUsername());
  604.         int index = 0;
  605.         int payload = 0;
  606.  
  607.         Log.d("Signup", "buildPacketForVoiceCall");
  608.  
  609.         packet[index++] = 0x00; // packet type==Voice Call
  610.         packet[index++] = MESSAGE_TYPE_PASSWORD_BY_CALL;
  611.         index+=2;
  612.  
  613.         if (!keyAutoSignUp.isEmpty()) {
  614.             logger.log("sending IVR req with nonce");
  615.             packet[index++] = 0x00; // attribute type==password
  616.             packet[index++] = PASSWORD_NONCE;
  617.             packet[index++] = 0x00; // attribute length
  618.             packet[index++] = (byte) (keyAutoSignUp.length);
  619.             for (int m = 0; m < keyAutoSignUp.length; m++) {
  620.                 packet[index++] = keyAutoSignUp.getCharacterAt(m);
  621.             }
  622.             ByteArray result = new ByteArray(100);
  623.             ByteArray buffer = new ByteArray(100);
  624.             Log.d(TAG, "buildPacketForSearchContactRequest: keyAutoSignUp = " + keyAutoSignUp + " sign up pin = " + mService.getSignupPin() + " password = " + UserDataManager.getUserPassword());
  625.             buffer.append(keyAutoSignUp).append(mService.getSignupPin()).append(UserDataManager.getUserPassword());
  626.             MD5.hash(result, buffer);
  627.             packet[index++] = 0x00; // attribute type==password
  628.             packet[index++] = PASSWORD;
  629.             packet[index++] = 0x00; // attribute length
  630.             packet[index++] = (byte) (result.length);
  631.             for (int m = 0; m < result.length; m++) // attribute value
  632.             {
  633.                 packet[index++] = result.getCharacterAt(m);
  634.             }
  635.         } else {
  636.             logger.log("sending nonce req");
  637.             Log.d(TAG, "buildPacketForSearchContactRequest: empty key");
  638.         }
  639.  
  640.         //actual pin
  641.         logger.log("sending IVR req : actual pin");
  642.         packet[index++] = 0x00; // attribute type mobile number
  643.         packet[index++] = ACTUAL_PIN;
  644.  
  645.         byte[] formattedUserName = UserDataManager.getUserName().getBytes();
  646.         packet[index++] = 0x00; // attribute length
  647.         packet[index++] = (byte) (formattedUserName.length);
  648.         for (int m = 0; m < formattedUserName.length; ++m) // attribute value
  649.         {
  650.             packet[index++] = formattedUserName[m];
  651.         }
  652.  
  653.         //phone number
  654.         logger.log("sending IVR req : phone number");
  655.         packet[index++] = 0x00; // attribute type==mobile number
  656.         packet[index++] = PHONE_NUMBER;
  657.  
  658.         packet[index++] = 0x00; // attribute length
  659.         packet[index++] = (byte) (mobileNumber.length);
  660.  
  661.         for (int m = 0; m < mobileNumber.length; ++m) // attribute value
  662.         {
  663.             packet[index++] = mobileNumber[m];
  664.         }
  665.         //op code
  666.         logger.log("sending IVR req : op code");
  667.         packet[index++] = 0x00; // attribute type==opcode
  668.         packet[index++] = OPERATOR_CODE;
  669.  
  670.         packet[index++] = 0x00; // attribute length
  671.         packet[index++] = (byte) (opcode.length);
  672.  
  673.         for (int m = 0; m < opcode.length; ++m) // attribute value
  674.         {
  675.             packet[index++] = opcode[m];
  676.         }
  677.  
  678.         packet[2] = (byte) (((index - 4) >> 8) & 0xff);
  679.         packet[3] = (byte) (index - 4);
  680.  
  681.         return index;
  682.     }
  683.  
  684.     private int buildPacketForRegistrationVerification(byte[] packet, String password) {
  685.         byte[] mobileNumberOrFormattedEmailAsUserName = mService.getUsername().getBytes();
  686.         byte[] pinNumberAsPassword = password.getBytes();
  687.  
  688.         Log.d("Signup", "buildPacketForRegistrationVerification");
  689.  
  690.         int index = 0;
  691.         int payload = 0;
  692.  
  693.         packet[index++] = 0x00; // packet type==registration verification
  694.         packet[index++] = MESSAGE_TYPE_PASSWORD_VERIFICATION;
  695.         index += 2;
  696.  
  697.         packet[index++] = 0x00; // attribute type==userName
  698. //        if(Util.isValidEmail(Util.getProperEmail(mService.getUsername()))){
  699. //            logger.log("buildPacketForRegistrationVerification email otp verification");
  700. //            packet[index++] = EMAIL_TO_VERIFY;
  701. //        }else {
  702.         logger.log("buildPacketForRegistrationVerification phone otp verification");
  703.         packet[index++] = PHONE_NUMBER;
  704. //        }
  705.         packet[index++] = 0x00; // attribute length
  706.         packet[index++] = (byte) (mobileNumberOrFormattedEmailAsUserName.length);
  707.         for (int m = 0; m < mobileNumberOrFormattedEmailAsUserName.length; ++m) {
  708.             packet[index++] = mobileNumberOrFormattedEmailAsUserName[m];
  709.         }
  710.  
  711.         if (!keyAutoSignUp.isEmpty()) {
  712.             packet[index++] = 0x00; // attribute type==password
  713.             packet[index++] = PASSWORD_NONCE;
  714.             packet[index++] = 0x00; // attribute length
  715.             packet[index++] = (byte) (keyAutoSignUp.length);
  716.             for (int m = 0; m < keyAutoSignUp.length; m++) {
  717.                 packet[index++] = keyAutoSignUp.getCharacterAt(m);
  718.             }
  719.             ByteArray result = new ByteArray(100);
  720.             ByteArray buffer = new ByteArray(100);
  721.             Log.d(TAG, "buildPacketForRegistrationVerification: keyAutoSignUp = " + keyAutoSignUp + " sign up pin = " + mService.getSignupPin() + " password = " + password);
  722.             buffer.append(keyAutoSignUp).append(mService.getSignupPin()).append(password);
  723.             MD5.hash(result, buffer);
  724.             packet[index++] = 0x00; // attribute type==password
  725.             packet[index++] = RESPONSE;
  726.             packet[index++] = 0x00; // attribute length
  727.             packet[index++] = (byte) (result.length);
  728.             for (int m = 0; m < result.length; m++) // attribute value
  729.             {
  730.                 packet[index++] = result.getCharacterAt(m);
  731.             }
  732.         } else {
  733.             Log.d(TAG, "buildPacketForRegistrationVerification: empty key");
  734.         }
  735.  
  736.  
  737.         packet[2] = (byte) (((index - 4) >> 8) & 0xff);
  738.         packet[3] = (byte) (index - 4);
  739.         return index;
  740.     }
  741.  
  742.     private void decryptSignUpPassword() {
  743.         byte[] mobileNumber;
  744.         if (pinNumber.isEmpty())
  745.             mobileNumber = mService.getUsername().getBytes();
  746.         else
  747.             mobileNumber = pinNumber.arr;
  748.         // first time decoding using the key 1
  749.         for (int i = 0; i < encryptedPassword.length; i++) {
  750.             encryptedPassword.arr[i] = (byte) (encryptedPassword.arr[i] & 0x00FF); // trim
  751.             // to
  752.             // 8
  753.             // byte
  754.             encryptedPassword.arr[i] = (byte) (encryptedPassword.arr[i] ^ keyAutoSignUp.arr[i
  755.                     % mobileNumber.length]);
  756.         }
  757.  
  758.         // second time decoding using the key 2
  759.         for (int i = 0; i < encryptedPassword.length; i++) {
  760.             encryptedPassword.arr[i] = (byte) (encryptedPassword.arr[i] & 0x00FF); // trim
  761.             // to
  762.             // 8
  763.             // byte
  764.             encryptedPassword.arr[i] = (byte) (encryptedPassword.arr[i] ^ mobileNumber[i
  765.                     % keyAutoSignUp.length]);
  766.         }
  767.         String s = pinNumber.toString();
  768.         mService.onSignupPinReceived(s, encryptedPassword.toString());
  769.     }
  770.  
  771.  
  772.     private void parseSignupRequestStatusRespponse(byte[] udpSignalRecvBuffer) {
  773.         int index = 2;
  774.  
  775.         int messageLength = udpSignalRecvBuffer[index++] & 0x00ff;
  776.         messageLength = (messageLength << 8) | (udpSignalRecvBuffer[index++] & 0x00ff);
  777.         int _status = -1;
  778.         ByteArray errorDesc = new ByteArray();
  779.         while (index < messageLength) {
  780.             int attributeType = udpSignalRecvBuffer[index++] & 0x00ff;
  781.             attributeType = (attributeType << 8) | (udpSignalRecvBuffer[index++] & 0x00ff);
  782.  
  783.             int attributeLength = udpSignalRecvBuffer[index++] & 0x00ff;
  784.             attributeLength = (attributeLength << 8) | (udpSignalRecvBuffer[index++] & 0x00ff);
  785.  
  786.             Log.i("Signup", "parseSignupRequestStatusRespponse: attributeType: " + attributeType + " attributeLength: " + attributeLength);
  787. //            keyAutoSignUp.reset();
  788.  
  789.             if (attributeType == PASSWORD_NONCE && attributeLength > 0) {
  790.                 mService.onSignupAckReceived();
  791.                 keyAutoSignUp.reset();
  792.                 keyAutoSignUp.append(udpSignalRecvBuffer, index, attributeLength);
  793.                 index += attributeLength;
  794.                 if (SIPProvider.DEBUG)
  795.                     Log.d("Signup", "parseSignupRequestStatusRespponse: got PASSWORD_NONCE: " + keyAutoSignUp.toString());
  796.             } else if (attributeType == STATUS && attributeLength > 0) {
  797.                 _status = udpSignalRecvBuffer[index++] & 0x00ff;
  798.                 _status = (_status << 8) | (udpSignalRecvBuffer[index++] & 0x00ff);
  799.                 if (SIPProvider.DEBUG)
  800.                     Log.d("Signup", "parseSignupRequestStatusRespponse: got SIGN_UP_STATUS: " + _status);
  801.             } else if (attributeType == ERROR_DESCRIPTION && attributeLength > 0) {
  802.                 errorDesc = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
  803.                 index += attributeLength;
  804.                 if (SIPProvider.DEBUG)
  805.                     Log.d("Signup", "parseSignupRequestStatusRespponse: got ERROR_DESCRIPTION: " + errorDesc.toString());
  806.             } else if (attributeType == PASSWORD && attributeLength > 0) {
  807.                 ByteArray password = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
  808.                 index += attributeLength;
  809.                 if (SIPProvider.DEBUG)
  810.                     Log.d("Signup", "parseSignupRequestStatusRespponse: got PASSWORD: " + password.toString());
  811.             } else if (attributeType == PHONE_NUMBER && attributeLength > 0) {
  812.                 pinNumber.reset();
  813.                 pinNumber.append(udpSignalRecvBuffer, index, attributeLength);
  814.                 index += attributeLength;
  815.                 Log.d("SIPProvider", "parseSignupRequestStatusRespponse: got PHONE_NUMBER: " + pinNumber.toString());
  816.             } else if (attributeType == ENCRYPTED_PASSWORD && attributeLength > 0) {
  817.                 encryptedPassword.reset();
  818.                 encryptedPassword.append(udpSignalRecvBuffer, index, attributeLength);
  819.                 index += attributeLength;
  820.                 Log.d("SIPProvider", "parseSignupRequestStatusRespponse: got ENCRYPTED_PASSWORD: " + encryptedPassword.toString());
  821.             } else {
  822.                 index += attributeLength;
  823.             }
  824.         }
  825.         if (_status == 1) {
  826.             if (!encryptedPassword.isEmpty())
  827.                 decryptSignUpPassword();
  828.             mService.onSuccessfulSignup();
  829.             sipProvider.StartRegistration();
  830.         } else {
  831.             mService.sendSignupfailedBroadcast(errorDesc.toString());
  832.         }
  833.     }
  834.  
  835.     private void parseSignupACKResponse(byte[] udpSignalRecvBuffer) {
  836.         int index = 2;
  837.  
  838.         int messageLength = udpSignalRecvBuffer[index++] & 0x00ff;
  839.         messageLength = (messageLength << 8) | (udpSignalRecvBuffer[index++] & 0x00ff);
  840.         encryptedPassword.reset();
  841. //        pinNumber.reset();
  842. //        keyAutoSignUp.reset();
  843.  
  844.         int _status = 1;
  845.         ByteArray errorDesc = new ByteArray();
  846.         while (index < messageLength) {
  847.             int attributeType = udpSignalRecvBuffer[index++] & 0x00ff;
  848.             attributeType = (attributeType << 8)
  849.                     | (udpSignalRecvBuffer[index++] & 0x00ff);
  850.             int attributeLength = udpSignalRecvBuffer[index++] & 0x00ff;
  851.             attributeLength = (attributeLength << 8)
  852.                     | (udpSignalRecvBuffer[index++] & 0x00ff);
  853.             Log.i("Signup", "parseSignupACKResponse : AttubuteType: " + attributeType + " AttubuteLength: " + attributeLength);
  854.             if (attributeType == PASSWORD_NONCE && attributeLength > 0) {
  855.                 mService.onSignupAckReceived();
  856.                 keyAutoSignUp.reset();
  857.                 keyAutoSignUp.append(udpSignalRecvBuffer, index, attributeLength);
  858.                 index += attributeLength;
  859.                 if (SIPProvider.DEBUG)
  860.                     Log.d("Signup", "parseSignupACKResponse: got PASSWORD_NONCE: " + keyAutoSignUp.toString());
  861.  
  862.             } else if (attributeType == ENCRYPTED_PASSWORD && attributeLength > 0) {
  863.                 encryptedPassword.reset();
  864.                 encryptedPassword.append(udpSignalRecvBuffer, index, attributeLength);
  865.                 index += attributeLength;
  866.                 if (SIPProvider.DEBUG)
  867.                     Log.d("Signup", "parseSignupACKResponse: got ENCRYPTED_PASSWORD: " + encryptedPassword.toString());
  868.  
  869.             } else if (attributeType == ACTUAL_PIN && attributeLength > 0) {
  870.                 pinNumber.reset();
  871.                 for (int i = 0; i < attributeLength; i++)
  872.                     pinNumber.appendByte(udpSignalRecvBuffer[index++]);
  873.                 if (SIPProvider.DEBUG)
  874.                     Log.d("Signup", "parseSignupACKResponse: got ACTUAL_PIN: " + pinNumber.toString());
  875.  
  876.             } else if (attributeType == PHONE_NUMBER && attributeLength > 0) {
  877.                 pinNumber.reset();
  878.                 for (int i = 0; i < attributeLength; i++)
  879.                     pinNumber.appendByte(udpSignalRecvBuffer[index++]);
  880.                 if (SIPProvider.DEBUG)
  881.                     Log.d("Signup", "parseSignupACKResponse: got PHONE_NUMBER: " + pinNumber.toString());
  882.  
  883.             } else if (attributeType == STATUS && attributeLength > 0) {
  884.                 _status = udpSignalRecvBuffer[index++] & 0x00ff;
  885.                 _status = (_status << 8) | (udpSignalRecvBuffer[index++] & 0x00ff);
  886.                 if (SIPProvider.DEBUG)
  887.                     Log.d("Signup", "parseSignupACKResponse: got SIGN_UP_STATUS: " + _status);
  888.  
  889.             } else if (attributeType == ERROR_DESCRIPTION && attributeLength > 0) {
  890.                 errorDesc = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
  891.                 index += attributeLength;
  892.                 if (SIPProvider.DEBUG)
  893.                     Log.d("Signup", "parseSignupRequestStatusRespponse: got ERROR_DESCRIPTION: " + errorDesc.toString());
  894.  
  895.             } else if (attributeType == PASSWORD && attributeLength > 0) {
  896.                 ByteArray password = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
  897.                 index += attributeLength;
  898.                 if (SIPProvider.DEBUG)
  899.                     Log.d("Signup", "parseSignupRequestStatusRespponse: got PASSWORD: " + password.toString());
  900.  
  901.             } else {
  902.                 index += attributeLength;
  903.             }
  904.         }
  905.  
  906.  
  907.         if (_status != 1) {
  908.             mService.sendSignupfailedBroadcast(errorDesc.toString());
  909.         } else if (!encryptedPassword.isEmpty() && !keyAutoSignUp.isEmpty()) {
  910.             decryptSignUpPassword();
  911.             mService.onSuccessfulSignup();
  912.         }
  913.     }
  914.  
  915.     public void processSearchContactResponse(byte[] udpSignalRecvBuffer) {
  916.         int currentPacketSequence = 1;
  917.         ContactEntry mContactEntry = null;
  918.  
  919.         String phoneNumber = null;
  920.         String name = null;
  921.         String designation = null;
  922.         String officeName = null;
  923.         String actualPin = null;
  924. //        String profilePicture = null;
  925. //        String profileHash = null;
  926.  
  927.         int index = 2;
  928.         int messageLength = udpSignalRecvBuffer[index++] & 0x00ff;
  929.         messageLength = (messageLength << 8)
  930.                 | (udpSignalRecvBuffer[index++] & 0x00ff);
  931.         while (index < messageLength) {
  932.             int attributeType = udpSignalRecvBuffer[index++] & 0x00ff;
  933.             attributeType = (attributeType << 8) | (udpSignalRecvBuffer[index++] & 0x00ff);
  934.  
  935.             int attributeLength = udpSignalRecvBuffer[index++] & 0x00ff;
  936.             attributeLength = (attributeLength << 8) | (udpSignalRecvBuffer[index++] & 0x00ff);
  937.             Log.v("SignupManager", "attributeType: " + attributeType + " attributeLength: " + attributeLength);
  938.             if (attributeType == PASSWORD_NONCE && attributeLength > 0) {
  939.                 keyAutoSignUp.reset();
  940.                 keyAutoSignUp.append(udpSignalRecvBuffer, index, attributeLength);
  941.                 index += attributeLength;
  942.                 byte[] packet = new byte[1000];
  943.                 int length = buildPacketForSearchContactRequest(packet, lastSearchName);
  944.                 DatagramPacket outDatagramPacket = new DatagramPacket(packet, length);
  945.                 outDatagramPacket.setSocketAddress(sipProvider.autoSignUpAddress);
  946.                 sipProvider.sendSignupPacket(outDatagramPacket);
  947.                 return;
  948.             } else if (attributeType == TOTAL_PACKET_COUNT && attributeLength > 0) {
  949. //                ByteArray totalPacketCount = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
  950. //                index += attributeLength;
  951.  
  952.                 int totalPacketCount = udpSignalRecvBuffer[index++] & 0x00ff;
  953.                 totalPacketCount = (totalPacketCount << 8) | (udpSignalRecvBuffer[index++] & 0x00ff);
  954.  
  955.                 if (!searchContactUtil.isTotalPacketCountReceived) {
  956.                     searchContactUtil.setTotalPacketCount(totalPacketCount);
  957.                     searchContactUtil.initReceivedPacketArray();
  958.                 }
  959.  
  960.                 Log.v("SignupManager", "TOTAL PACKET COUNT: " + totalPacketCount);
  961.             } else if (attributeType == CURRENT_PACKET_SEQ && attributeLength > 0) {
  962.                 currentPacketSequence = udpSignalRecvBuffer[index++] & 0x00ff;
  963.                 currentPacketSequence = (currentPacketSequence << 8) | (udpSignalRecvBuffer[index++] & 0x00ff);
  964.  
  965.                 synchronized (searchContactUtil.lock) {
  966.                     if (searchContactUtil.lastSeenMaxPacketSequence < currentPacketSequence) {
  967.                         searchContactUtil.lastSeenMaxPacketSequence = currentPacketSequence;
  968.                     }
  969.                 }
  970.  
  971.                 Log.v("SignupManager", "CURRENT PACKET SEQ: " + currentPacketSequence);
  972.             } else if (attributeType == PHONE_NUMBER) {
  973.                 ByteArray data = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
  974.                 index += attributeLength;
  975.                 phoneNumber = data.toString();
  976.                 Log.v("SignupManager", "PHONE_NUMBER: " + phoneNumber + " " + (phoneNumber == null));
  977.             } else if (attributeType == NAME) {
  978.                 ByteArray data = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
  979.                 index += attributeLength;
  980.                 name = data.toString().trim();
  981.                 Log.v("SignupManager", "NAME: " + name + " " + (name == null));
  982.             } else if (attributeType == DESIGNATION) {
  983.                 ByteArray data = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
  984.                 index += attributeLength;
  985.                 designation = data.toString().trim();
  986.                 Log.v("SignupManager", "DESIGNATION: " + designation + " " + (designation == null));
  987.             } else if (attributeType == OFFICE_NAME) {
  988.                 ByteArray data = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
  989.                 index += attributeLength;
  990.                 officeName = data.toString();
  991.                 Log.v("SignupManager", "OFFICE_NAME: " + officeName + " " + (officeName == null));
  992.             } else if (attributeType == ACTUAL_PIN) {
  993.                 ByteArray data = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
  994.                 index += attributeLength;
  995.                 actualPin = data.toString();
  996.                 fetchedNumbers.add(actualPin);
  997.                 Log.v("SignupManager", "ACTUAL_PIN: " + actualPin + " " + (actualPin == null));
  998.             }
  999. //            else if (attributeType == PROFILE_PICTURE) {
  1000. //                ByteArray value = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
  1001. //                index += attributeLength;
  1002. //                profilePicture = value.toString();
  1003. //            } else if (attributeType == PROFILE_HASH) {
  1004. //                ByteArray value = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
  1005. //                index += attributeLength;
  1006. //                profileHash = value.toString();
  1007. //            }
  1008.             else {
  1009.                 index += attributeLength;
  1010.             }
  1011.  
  1012.             if (phoneNumber != null && name != null /*&& designation != null && officeName != null && actualPin != null && profilePicture != null && profileHash != null*/) {
  1013. //                mContactEntry = new ContactEntry("", value, name, designation, officeName, actualPin, profilePicture, profileHash);
  1014.                 mContactEntry = new ContactEntry("", phoneNumber, name, designation, officeName, actualPin, "", "");
  1015.                 if (!searchContactUtil.mSearchContactEntries.contains(mContactEntry) && !name.equals("null") && !phoneNumber.equals("null")) {
  1016.                     Log.v("SignupManager", "Creating search contactEntry");
  1017.                     searchContactUtil.mSearchContactEntries.add(mContactEntry);
  1018.                 }
  1019.  
  1020. //                if(profileHash.length()>0)
  1021. //                    mService.downloadProfilePicture(actualPin, profileHash);
  1022. //
  1023. //                profilePicture = null;
  1024. //                profileHash = null;
  1025.  
  1026.                 phoneNumber = null;
  1027.                 name = null;
  1028.                 designation = null;
  1029.                 officeName = null;
  1030.                 actualPin = null;
  1031.             }
  1032.  
  1033.         }
  1034.  
  1035.         synchronized (searchContactUtil.lock) {
  1036.             if (searchContactUtil.totalPacketCount == 0) {
  1037.                 if (SIPProvider.DEBUG) Log.d("Signup", "Creating contact from search result gcm");
  1038. //                DataHelper.getInstance(mService).createContactEntry(searchContactUtil.mSearchContactEntries, mService.getUsername());
  1039.                 DirectorySearchActivity.searchResult.addAll(searchContactUtil.mSearchContactEntries);
  1040.                 runnableMissingSearchContact.shouldSendSearchContactResponse = false;
  1041.                 searchContactThread.interrupt();
  1042.             } else if (searchContactUtil.receivedPacket.length > 0 && !searchContactUtil.receivedPacket[currentPacketSequence - 1]) {
  1043.                 searchContactUtil.receivedPacket[currentPacketSequence - 1] = true;
  1044.                 Collections.sort(searchContactUtil.mSearchContactEntries, comparator);
  1045.                 searchContactUtil.receivedPacketCount++;
  1046.  
  1047.                 if (searchContactUtil.receivedPacketCount == searchContactUtil.totalPacketCount) {
  1048.                     runnableMissingSearchContact.shouldSendSearchContactResponse = false;
  1049.                     DirectorySearchActivity.searchResult.addAll(searchContactUtil.mSearchContactEntries);
  1050. //                    DataHelper.getInstance(mService).createContactEntry(searchContactUtil.mSearchContactEntries, mService.getUsername());
  1051.                 } else {
  1052.                     runnableMissingSearchContact.shouldSendSearchContactResponse = true;
  1053.                 }
  1054.                 searchContactThread.interrupt();
  1055.             }
  1056.         }
  1057.  
  1058.         Collections.sort(DirectorySearchActivity.searchResult, comparator);
  1059.  
  1060.         Intent intent = new Intent(Constants.DIRECTORY_SEARCH_INTENT_FILTER);
  1061.         intent.putExtra(Constants.UPDATE_SEARCH_RESULT, searchContactUtil);
  1062.         LocalBroadcastManager.getInstance(mService).sendBroadcast(intent);
  1063.     }
  1064.  
  1065.     public void processMissingSearchContactResponse(byte[] udpSignalRecvBuffer) {
  1066.         if (!searchContactUtil.isTotalPacketCountReceived) {
  1067.             return;
  1068.         }
  1069.  
  1070.         int currentPacketSequence = 1;
  1071.         ContactEntry mContactEntry = null;
  1072.         String phoneNumber = null;
  1073.         String name = null;
  1074.         String designation = null;
  1075.         String officeName = null;
  1076.         String actualPin = null;
  1077. //        String profilePicture = null;
  1078. //        String profileHash = null;
  1079.  
  1080.         int index = 2;
  1081.         int messageLength = udpSignalRecvBuffer[index++] & 0x00ff;
  1082.         messageLength = (messageLength << 8)
  1083.                 | (udpSignalRecvBuffer[index++] & 0x00ff);
  1084.         while (index < messageLength) {
  1085.             int attributeType = udpSignalRecvBuffer[index++] & 0x00ff;
  1086.             attributeType = (attributeType << 8) | (udpSignalRecvBuffer[index++] & 0x00ff);
  1087.  
  1088.             int attributeLength = udpSignalRecvBuffer[index++] & 0x00ff;
  1089.             attributeLength = (attributeLength << 8) | (udpSignalRecvBuffer[index++] & 0x00ff);
  1090.  
  1091.             Log.v("SignupManager", "attributeType: " + attributeType + " attributeLength: " + attributeLength);
  1092.             if (attributeType == MISSING_PACKET_NO && attributeLength > 0) {
  1093.                 currentPacketSequence = udpSignalRecvBuffer[index++] & 0x00ff;
  1094.                 currentPacketSequence = (currentPacketSequence << 8) | (udpSignalRecvBuffer[index++] & 0x00ff);
  1095.  
  1096.                 synchronized (searchContactUtil.lock) {
  1097.                     if (searchContactUtil.lastSeenMaxPacketSequence < currentPacketSequence) {
  1098.                         searchContactUtil.lastSeenMaxPacketSequence = currentPacketSequence;
  1099.                     }
  1100.                 }
  1101.  
  1102.                 Log.v("SignupManager", "Missing PACKET SEQ: " + currentPacketSequence);
  1103.             } else if (attributeType == PHONE_NUMBER) {
  1104.                 ByteArray data = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
  1105.                 index += attributeLength;
  1106.                 phoneNumber = data.toString();
  1107.                 Log.v("SignupManager", "Missing PHONE_NUMBER: " + phoneNumber + " " + (phoneNumber == null));
  1108.             } else if (attributeType == NAME) {
  1109.                 ByteArray data = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
  1110.                 index += attributeLength;
  1111.                 name = data.toString().trim();
  1112.                 Log.v("SignupManager", "Missing NAME: " + name + " " + (name == null));
  1113.             } else if (attributeType == DESIGNATION) {
  1114.                 ByteArray data = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
  1115.                 index += attributeLength;
  1116.                 designation = data.toString().trim();
  1117.                 Log.v("SignupManager", "Missing DESIGNATION: " + designation + " " + (designation == null));
  1118.             } else if (attributeType == OFFICE_NAME) {
  1119.                 ByteArray data = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
  1120.                 index += attributeLength;
  1121.                 officeName = data.toString();
  1122.                 Log.v("SignupManager", "Missing OFFICE_NAME: " + officeName + " " + (officeName == null));
  1123.             } else if (attributeType == ACTUAL_PIN) {
  1124.                 ByteArray data = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
  1125.                 index += attributeLength;
  1126.                 actualPin = data.toString();
  1127.                 fetchedNumbers.add(actualPin);
  1128.                 Log.v("SignupManager", "Missing ACTUAL_PIN: " + actualPin + " " + (actualPin == null));
  1129.             }
  1130. //            else if (attributeType == PROFILE_PICTURE) {
  1131. //                ByteArray value = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
  1132. //                index += attributeLength;
  1133. //                profilePicture = value.toString();
  1134. //            } else if (attributeType == PROFILE_HASH) {
  1135. //                ByteArray value = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
  1136. //                index += attributeLength;
  1137. //                profileHash = value.toString();
  1138. //            }
  1139.             else {
  1140.                 index += attributeLength;
  1141.             }
  1142.  
  1143.             if (phoneNumber != null && name != null /*&& designation != null && officeName != null && actualPin != null && profilePicture != null && profileHash != null*/) {
  1144. //                mContactEntry = new ContactEntry("", value, name, designation, officeName, actualPin, profilePicture, profileHash);
  1145.                 mContactEntry = new ContactEntry("", phoneNumber, name, designation, officeName, actualPin, "", "");
  1146.                 if (!searchContactUtil.mSearchContactEntries.contains(mContactEntry) && !name.equals("null") && !phoneNumber.equals("null")) {
  1147.                     Log.v("SignupManager", "Creating missing search contactEntry");
  1148.                     searchContactUtil.mSearchContactEntries.add(mContactEntry);
  1149.                 }
  1150.  
  1151. //                if(profileHash.length()>0)
  1152. //                    mService.downloadProfilePicture(actualPin, profileHash);
  1153. //
  1154. //                profilePicture = null;
  1155. //                profileHash = null;
  1156.  
  1157.                 phoneNumber = null;
  1158.                 name = null;
  1159.                 designation = null;
  1160.                 officeName = null;
  1161.                 actualPin = null;
  1162.             }
  1163.  
  1164.         }
  1165.  
  1166.         synchronized (searchContactUtil.lock) {
  1167.             if (searchContactUtil.totalPacketCount == 0) {
  1168. //                DataHelper.getInstance(mService).createContactEntry(searchContactUtil.mSearchContactEntries, mService.getUsername());
  1169.                 DirectorySearchActivity.searchResult.addAll(searchContactUtil.mSearchContactEntries);
  1170.                 runnableMissingSearchContact.shouldSendSearchContactResponse = false;
  1171.                 searchContactThread.interrupt();
  1172.             } else if (searchContactUtil.receivedPacket.length > 0 && !searchContactUtil.receivedPacket[currentPacketSequence - 1]) {
  1173.                 searchContactUtil.receivedPacket[currentPacketSequence - 1] = true;
  1174.                 Collections.sort(searchContactUtil.mSearchContactEntries, comparator);
  1175.                 searchContactUtil.receivedPacketCount++;
  1176.  
  1177.                 if (searchContactUtil.receivedPacketCount == searchContactUtil.totalPacketCount) {
  1178.                     runnableMissingSearchContact.shouldSendSearchContactResponse = false;
  1179.                     DirectorySearchActivity.searchResult.addAll(searchContactUtil.mSearchContactEntries);
  1180. //                    DataHelper.getInstance(mService).createContactEntry(searchContactUtil.mSearchContactEntries, mService.getUsername());
  1181.                 } else {
  1182.                     runnableMissingSearchContact.shouldSendSearchContactResponse = true;
  1183.                 }
  1184.                 searchContactThread.interrupt();
  1185.             }
  1186.         }
  1187.  
  1188.         Collections.sort(DirectorySearchActivity.searchResult, comparator);
  1189.  
  1190.         Intent intent = new Intent(Constants.DIRECTORY_SEARCH_INTENT_FILTER);
  1191.         intent.putExtra(Constants.UPDATE_SEARCH_RESULT, searchContactUtil);
  1192.         LocalBroadcastManager.getInstance(mService).sendBroadcast(intent);
  1193.     }
  1194.  
  1195.     //search contact
  1196.     SearchContactUtil searchContactUtil;
  1197.     Thread searchContactThread;
  1198.     FetchMissingSearchContactPackets runnableMissingSearchContact;
  1199.     long mFetchMissingSearchContactRunnableId;
  1200.     static final int RETRY_DELAY = 5000;
  1201.     static final int MAX_RETRY_COUNT = 5;
  1202.     static final int RETRY_MISSING_REQUEST_DELAY = 20;
  1203.     String searchName;
  1204.  
  1205.     class FetchMissingSearchContactPackets implements Runnable {
  1206.         public boolean shouldSendSearchContactResponse = true;
  1207.         long runnableId;
  1208.  
  1209.         public FetchMissingSearchContactPackets(long runnableId) {
  1210.             this.runnableId = runnableId;
  1211.             mFetchMissingSearchContactRunnableId = runnableId;
  1212.         }
  1213.  
  1214.         @Override
  1215.         public void run() {
  1216.             shouldSendSearchContactResponse = true;
  1217.             boolean searchContactFlag = true;
  1218.             int retryCounter = 0;
  1219.  
  1220.             while (searchContactFlag && retryCounter < MAX_RETRY_COUNT && shouldSendSearchContactResponse) {
  1221.                 try {
  1222.                     try {
  1223.                         Thread.sleep(RETRY_DELAY);
  1224.                     } catch (Exception e) {
  1225.                     }
  1226.                     if (runnableId == mFetchMissingSearchContactRunnableId) {
  1227.                         if (searchContactUtil.receivedPacket != null) {
  1228.                             Log.v("MissingContactPackets", "Got response");
  1229.                             ArrayList<Integer> missingPacketSequences = searchContactUtil.getMissingPacketSequence();
  1230.                             if (missingPacketSequences.size() > 0) {
  1231.                                 Log.v("MissingContactPackets", "Got some missing packets : " + missingPacketSequences.size());
  1232.                                 for (int sequence : missingPacketSequences) {
  1233.                                     try {
  1234.                                         Thread.sleep(RETRY_MISSING_REQUEST_DELAY);
  1235.                                     } catch (Exception e) {
  1236.                                     }
  1237.                                     sendMissingSearchContactPacketRequest(searchName, sequence);
  1238.                                     Log.v("MissingContactPackets", "sending missing packet request for : " + sequence);
  1239.                                 }
  1240.                                 searchContactFlag = true;
  1241.                                 retryCounter++;
  1242.                             }
  1243.                         } else {
  1244.                             Log.v("MissingContactPackets", "Did not got response yet");
  1245.                             retryCounter++;
  1246.                             if (Util.hasConnection(mService)) {
  1247.                                 Log.v("MissingContactPackets", "Sending Search contact request again");
  1248.                                 searchContactFlag = true;
  1249.                                 byte[] packet = new byte[1000];
  1250.                                 int length = buildPacketForSearchContactRequest(packet, searchName);
  1251.                                 DatagramPacket outDatagramPacket = new DatagramPacket(packet, length);
  1252.                                 outDatagramPacket.setSocketAddress(sipProvider.autoSignUpAddress);
  1253.                                 sipProvider.sendSignupPacket(outDatagramPacket);
  1254.                             } else {
  1255.                                 searchContactFlag = false;
  1256.                             }
  1257.                         }
  1258.                     } else {
  1259.                         searchContactFlag = false;
  1260.                     }
  1261.                 } catch (Exception e) {
  1262. //                    searchContactFlag = true;
  1263. //                    retryCounter = 0;
  1264.                     e.printStackTrace();
  1265.                 }
  1266.             }
  1267.  
  1268.             if (shouldSendSearchContactResponse && runnableId == mFetchMissingSearchContactRunnableId) {
  1269.                 Intent intent = new Intent(Constants.DIRECTORY_SEARCH_INTENT_FILTER);
  1270.                 intent.putExtra(Constants.UPDATE_SEARCH_RESULT, searchContactUtil);
  1271.                 LocalBroadcastManager.getInstance(mService).sendBroadcast(intent);
  1272.             }
  1273.  
  1274.         }
  1275.     }
  1276.  
  1277.     String lastSearchName;
  1278.  
  1279.     public void sendSearchContactRequest(String name) {
  1280.         try {
  1281.             lastSearchName = name;
  1282.             Log.v("sendLabelRequest", "sendSearchContactRequest " + name);
  1283.             DirectorySearchActivity.searchResult.clear();
  1284.             runnableMissingSearchContact = new FetchMissingSearchContactPackets(System.currentTimeMillis());
  1285.             searchContactUtil = new SearchContactUtil();
  1286.             searchName = name;
  1287.  
  1288.             byte[] packet = new byte[1000];
  1289.             int length = buildPacketForSearchContactRequest(packet, name);
  1290.             DatagramPacket outDatagramPacket = new DatagramPacket(packet, length);
  1291.             outDatagramPacket.setSocketAddress(sipProvider.autoSignUpAddress);
  1292.             sipProvider.sendSignupPacket(outDatagramPacket);
  1293.  
  1294.             searchContactThread = new Thread(runnableMissingSearchContact);
  1295.             searchContactThread.start();
  1296.         } catch (Exception e) {
  1297.             e.printStackTrace();
  1298.         }
  1299.     }
  1300.  
  1301.     public void sendMissingSearchContactPacketRequest(String name, int missingPacketSequence) {
  1302.         try {
  1303.             Log.v("searchContactFlag", "sending missing request for " + missingPacketSequence);
  1304.             byte[] packet = new byte[1000];
  1305.             int length = buildPacketForMissingSearchContactPacketRequest(packet, name, missingPacketSequence);
  1306.             DatagramPacket outDatagramPacket = new DatagramPacket(packet, length);
  1307.             outDatagramPacket.setSocketAddress(sipProvider.autoSignUpAddress);
  1308.             sipProvider.sendSignupPacket(outDatagramPacket);
  1309.         } catch (Exception e) {
  1310.             e.printStackTrace();
  1311.         }
  1312.     }
  1313.  
  1314.     public int buildPacketForSearchContactRequest(byte[] packet, String name) {
  1315.         int index = 4;
  1316.         packet[0] = 0x00;
  1317.         packet[1] = SEARCH_CONTACT;
  1318.         if (!keyAutoSignUp.isEmpty()) {
  1319.             packet[index++] = 0x00; // attribute type==password
  1320.             packet[index++] = PASSWORD_NONCE;
  1321.             packet[index++] = 0x00; // attribute length
  1322.             packet[index++] = (byte) (keyAutoSignUp.length);
  1323.             for (int m = 0; m < keyAutoSignUp.length; m++) {
  1324.                 packet[index++] = keyAutoSignUp.getCharacterAt(m);
  1325.             }
  1326.             ByteArray result = new ByteArray(100);
  1327.             ByteArray buffer = new ByteArray(100);
  1328.             Log.d(TAG, "buildPacketForSearchContactRequest: keyAutoSignUp = " + keyAutoSignUp + " sign up pin = " + mService.getSignupPin() + " password = " + UserDataManager.getUserPassword());
  1329.             buffer.append(keyAutoSignUp).append(mService.getSignupPin()).append(UserDataManager.getUserPassword());
  1330.             MD5.hash(result, buffer);
  1331.             packet[index++] = 0x00; // attribute type==password
  1332.             packet[index++] = PASSWORD;
  1333.             packet[index++] = 0x00; // attribute length
  1334.             packet[index++] = (byte) (result.length);
  1335.             for (int m = 0; m < result.length; m++) // attribute value
  1336.             {
  1337.                 packet[index++] = result.getCharacterAt(m);
  1338.             }
  1339.         } else {
  1340.             Log.d(TAG, "buildPacketForSearchContactRequest: empty key");
  1341.         }
  1342.         packet[index++] = 0x00; // attribute type mobile number
  1343.         packet[index++] = ACTUAL_PIN;
  1344.  
  1345.         byte[] formattedUserName = UserDataManager.getUserName().getBytes();
  1346.         packet[index++] = 0x00; // attribute length
  1347.         packet[index++] = (byte) (formattedUserName.length);
  1348.         for (int m = 0; m < formattedUserName.length; ++m) // attribute value
  1349.         {
  1350.             packet[index++] = formattedUserName[m];
  1351.         }
  1352.  
  1353.         if (name.length() > 0) {
  1354.             packet[index++] = 0x00;
  1355.             packet[index++] = NAME;
  1356.  
  1357.  
  1358.             // attribute value
  1359.             byte[] byteArrayName = name.getBytes();
  1360.             packet[index++] = 0x00;
  1361.             packet[index++] = (byte) byteArrayName.length;
  1362.  
  1363.             for (int i = 0; i < byteArrayName.length; ++i) {
  1364.                 packet[index++] = byteArrayName[i];
  1365.             }
  1366.         }
  1367.  
  1368.         packet[2] = (byte) ((index >> 8) & 0xff);
  1369.         packet[3] = (byte) index;
  1370.         return index;
  1371.     }
  1372.  
  1373.     public int buildPacketForMissingSearchContactPacketRequest(byte[] packet, String name, int missingPacketSequence) {
  1374.         int index = 4;
  1375.         packet[0] = 0x00;
  1376.         packet[1] = GET_MISSING_SEARCH_CONTACT_PACKET;
  1377.  
  1378.         if (name.length() > 0) {
  1379.             packet[index++] = 0x00;
  1380.             packet[index++] = NAME;
  1381.  
  1382.  
  1383.             // attribute value
  1384.             byte[] byteArrayName = name.getBytes();
  1385.             packet[index++] = 0x00;
  1386.             packet[index++] = (byte) byteArrayName.length;
  1387.  
  1388.             for (int i = 0; i < byteArrayName.length; ++i) {
  1389.                 packet[index++] = byteArrayName[i];
  1390.             }
  1391.         }
  1392.  
  1393.         packet[index++] = 0x00;
  1394.         packet[index++] = MISSING_PACKET_NO;
  1395.  
  1396.  
  1397.         // attribute value
  1398. //        byte[] sequence = missingPacketSequence.getBytes();
  1399. //        packet[index++] = 0x00;
  1400. //        packet[index++] = (byte) sequence.length;
  1401. //        for (int i = 0; i < sequence.length; ++i) {
  1402. //            packet[index++] = sequence[i];
  1403. //        }
  1404.  
  1405.         packet[index++] = 0x00;
  1406.         packet[index++] = (byte) 2;
  1407.  
  1408.         packet[index++] = (byte) ((missingPacketSequence >> 8) & 0xFF);
  1409.         packet[index++] = (byte) (missingPacketSequence & 0xFF);
  1410.  
  1411.         packet[2] = (byte) ((index >> 8) & 0xff);
  1412.         packet[3] = (byte) index;
  1413.         return index;
  1414.     }
  1415.  
  1416.     Comparator<ContactEntry> comparator = new Comparator<ContactEntry>() {
  1417.         @Override
  1418.         public int compare(ContactEntry lhs, ContactEntry rhs) {
  1419.             return lhs.name.compareTo(rhs.name);
  1420.         }
  1421.     };
  1422.  
  1423.     public void setImHistoryFetchingHelper(IMHistoryFetchingHelper imHistoryFetchingHelper) {
  1424.         this.imHistoryFetchingHelper = imHistoryFetchingHelper;
  1425.     }
  1426.  
  1427. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement