Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.revesoft.itelmobiledialer.service;
- import android.content.Intent;
- import android.os.Build;
- import android.support.v4.content.LocalBroadcastManager;
- import android.text.TextUtils;
- import android.util.Log;
- import com.revesoft.itelmobiledialer.data.UserDataManager;
- import com.revesoft.itelmobiledialer.e2eencryption.E2EConstants;
- import com.revesoft.itelmobiledialer.e2eencryption.E2EPublicKeyResponseReceiver;
- import com.revesoft.itelmobiledialer.phonebook.ContactEntry;
- import com.revesoft.itelmobiledialer.phonebook.DirectorySearchActivity;
- import com.revesoft.itelmobiledialer.phonebook.SearchContactUtil;
- import com.revesoft.itelmobiledialer.service.contactblock.ContactBlockHelper;
- import com.revesoft.itelmobiledialer.service.historyfetching.IMHistoryFetchingHelper;
- import com.revesoft.itelmobiledialer.service.phoneemailverification.PhoneEmailVerificationHelper;
- import com.revesoft.itelmobiledialer.signalling.SIPProvider;
- import com.revesoft.itelmobiledialer.util.ByteArray;
- import com.revesoft.itelmobiledialer.util.Configuration;
- import com.revesoft.itelmobiledialer.util.Constants;
- import com.revesoft.itelmobiledialer.util.MD5;
- import com.revesoft.itelmobiledialer.util.TaggedLogger;
- import com.revesoft.itelmobiledialer.util.Util;
- import java.net.DatagramPacket;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.HashSet;
- import java.util.Locale;
- /**
- * @author dhiman on 5/3/16.
- */
- @SuppressWarnings("all")
- public class SignupManager {
- /***************
- * Message ID
- *****************/
- public static final byte MESSAGE_TYPE_SIGNUP = 1;
- public static final byte MESSAGE_TYPE_SIGNUP_BY_EMAIL = 51;
- public static final byte SEND_PASS_TO_MOBILE = 92;
- public static final byte MESSAGE_TYPE_PASSWORD_BY_CALL = 2;
- public static final byte MESSAGE_TYPE_PASSWORD_VERIFICATION = 3;
- public static final int SIGN_UP_REQUEST_STATUS = 5;
- private static final int ACK_MESSAGE_TYPE_SIGNUP = 5;
- private static final int ACK_MESSAGE_TYPE_PASSWORD_BY_CALL = 6;
- public static final int GET_PREFERENCE_LIST = 18;
- public static final int GET_PREFERENCE_LIST_RESPONSE = 19;
- public static final int GET_OFFICE_CONTACT_LIST = 20;
- public static final int GET_OFFICE_CONTACT_LIST_RESPONSE = 21;
- public static final int SET_DESIGNATION = 22;
- public static final int SET_DESIGNATION_RESPONSE = 23;
- public static final int SELECT_DESIGNATION_REQUEST = 24;
- public static final int SELECT_DESIGNATION_RESPONSE = 25;
- public static final int PROJAPI_CREDENTIAL_REQUEST = 26;
- public static final int PROJAPI_CREDENTIAL_RESPONSE = 27;
- public static final int RESPONSE_TYPE_ACK = 28;
- /***************
- * Attribute ID
- *****************/
- public static final byte PHONE_NUMBER = 1;
- public static final byte EMAIL_TO_VERIFY = 38;
- public static final byte PASSWORD = 2;
- public static final int STATUS = 3;
- public static final int OPERATOR_CODE = 4;
- public static final int SIGN_UP_STATUS = 4;
- public static final int PASSWORD_NONCE = 5;
- public static final int RESPONSE = 6;
- public static final int PHONE_NUMBER_DETECTED = 7;
- public static final int ENCRYPTED_PASSWORD = 8;
- public static final int SEND_DEVICE_ID = 9;
- public static final int NAME = 20;
- public static final int HANDSET_LANGUAGE = 205;
- public static final int VENDOR_NAME = 203;
- public static final int MODEL_NAME = 204;
- public static final int DESIGNATION = 21;
- public static final int OFFICE_NAME = 22;
- public static final int TOTAL_PACKET_COUNT = 23;
- public static final int CURRENT_PACKET_SEQ = 24;
- public static final int ERROR_DESCRIPTION = 25;
- public static final int OFFICER_ID = 26;
- public static final int OFFICER_NAME = 22;
- public static final int NTIONAL_ID = 27;
- public static final int OFFICE_ID = 29;
- public static final int PROJAPOTI_USER_NAME = 30;
- public static final int PROJAPOTI_USER_PASSWORD = 31;
- public static final int DESIGNATION_ID = 32;
- public static final int MISSING_PACKET_NO = 33;
- public static final int GET_MISSING_CONTACT_LIST_PACKET = 31;
- public static final int GET_MISSING_CONTACT_LIST_PACKET_RESPONSE = 32;
- public static final int GET_MISSING_PREFERENCE_LIST_PACKET = 29;
- public static final int GET_MISSING_PREFERENCE_LIST_PACKET_RESPONSE = 30;
- public static final int ACTUAL_PIN = 40;
- public static final int IS_UPDATE = 46;
- public static final int PROFILE_PICTURE = 53;
- public static final int PROFILE_HASH = 54;
- public static final int GET_MISSING_DESIGNATION = 63;
- public static final int GET_MISSING_DESIGNATION_RESPONSE = 64;
- public static final int SEARCH_CONTACT = 33;
- public static final int SEARCH_CONTACT_RESPONSE = 34;
- public static final int GET_MISSING_SEARCH_CONTACT_PACKET = 35;
- public static final int GET_MISSING_SEARCH_CONTACT_PACKET_RESPONSE = 36;
- public static final int MINISTRY_NAME = 34;
- public static final int MINISTRY_ID = 35;
- public static final int LEVEL_NAME = 38;
- public static final int LEVEL_ID = 39;
- public static final int OFFICE_UNIT_ID = 37;
- public static final int OFFICE_UNIT_NAME = 36;
- public static final int GET_MINISTRY_LIST = 41;
- public static final int GET_MINISTRY_LIST_RESPONSE = 42;
- public static final int GET_MISSING_MINISTRY_LIST_PACKET = 43;
- public static final int GET_MISSING_MINISTRY_LIST_PACKET_RESPONSE = 44;
- public static final int GET_LEVEL_LIST = 45;
- public static final int GET_LEVEL_LIST_RESPONSE = 46;
- public static final int GET_MISSING_LEVEL_LIST_PACKET = 47;
- public static final int GET_MISSING_LEVEL_LIST_PACKET_RESPONSE = 48;
- public static final int GET_OFFICE_LIST = 49;
- public static final int GET_OFFICE_LIST_RESPONSE = 50;
- public static final int GET_MISSING_OFFICE_LIST_PACKET = 51;
- public static final int GET_MISSING_OFFICE_LIST_PACKET_RESPONSE = 52;
- public static final int GET_UNIT_LIST = 53;
- public static final int GET_UNIT_LIST_RESPONSE = 54;
- public static final int GET_MISSING_UNIT_LIST_PACKET = 55;
- public static final int GET_MISSING_UNIT_LIST_PACKET_RESPONSE = 56;
- public static final int GET_DESIGNATION_LIST = 57;
- public static final int GET_DESIGNATION_LIST_RESPONSE = 58;
- public static final int GET_MISSING_DESIGNATION_LIST_PACKET = 59;
- public static final int GET_MISSING_DESIGNATION_LIST_PACKET_RESPONSE = 60;
- public static final int GET_OFFICER_BY_DESIGNATION = 61;
- public static final int GET_OFFICER_BY_DESIGNATION_RESPONSE = 62;
- public static final int GET_OFFICER_BY_DESIGNATION_MISSING_RESPONSE = 87;
- private static final int GET_OFFICER_INFO_BY_PIN = 73;
- private static final int GET_OFFICER_INFO_BY_PIN_RESPONSE = 74;
- private static final int ADVANCE_SEARCH_REQUEST = 77;
- private static final int ADVANCE_SEARCH_MISSING_REQUEST = 78;
- private static final int ADVANCE_SEARCH_RESPONSE = 79;
- private static final int ADVANCE_SEARCH_MISSING_RESPONSE = 80;
- private static final int REQUEST_BY_LABEL = 81;
- private static final int MISSING_REQUEST_BY_LABEL = 82;
- private static final int REQUEST_BY_LABEL_RESPONSE = 83;
- private static final int REQUEST_BY_LABEL_MISSING_RESPONSE = 84;
- private static final int LABEL = 47;
- private static final int LABEL_ID = 48;
- private static final int VALUE = 49;
- private static final int VALUE_ID = 50;
- public static final int NUMBER_OF_SEARCH_RESULT = 51;
- public static final int MAX_CONTACT_TO_SHOW = 52;
- private static final int UPDATE_PROFILE = 85;
- private static final int UPDATE_PROFILE_RESPONSE = 86;
- private static final int BUDDY_LIST_ADDED = 88;
- public static final int GET_ALL_INFO_BY_PIN = 89;
- public static final int GET_ALL_INFO_BY_PIN_RESPONSE = 90;
- public static final int GET_SEARCH_RESULT = 91;
- /**
- * * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
- **/
- public static boolean isPhoneNumberAutoDetected = false;
- public static boolean sendSMSFromPhone = false;
- public static boolean passverificationNeeded = false;
- private SIPProvider sipProvider;
- private DialerService mService;
- public static HashSet<String> fetchedNumbers = new HashSet<>();
- private ByteArray keyAutoSignUp = new ByteArray(100);
- private ByteArray encryptedPassword = new ByteArray(100);
- private ByteArray pinNumber = new ByteArray(100);
- private ContactBlockHelper contactBlockHelper;
- private PhoneEmailVerificationHelper phoneEmailVerificationHelper;
- IMHistoryFetchingHelper imHistoryFetchingHelper;
- public SignupManager(SIPProvider sipProvider, DialerService mService) {
- this.sipProvider = sipProvider;
- this.mService = mService;
- fetchedNumbers.clear();
- // handler = new Handler(mService.getMainLooper());
- }
- public void setContactBlockHelper(ContactBlockHelper contactBlockHelper) {
- this.contactBlockHelper = contactBlockHelper;
- }
- public void setPhoneEmailVerificationHelper(PhoneEmailVerificationHelper phoneEmailVerificationHelper) {
- this.phoneEmailVerificationHelper = phoneEmailVerificationHelper;
- }
- private static final String TAG = "Signup";
- public void sendSignupPacket(int type, String password) {
- try {
- byte[] packet = new byte[1000];
- int length = 0;
- if (type == Constants.SIGNUP_REQUEST) {
- if (Util.isValidEmail(Util.getProperEmail(UserDataManager.getUserName()))) {
- length = buildPacketForEmailSignUp(packet);
- } else {
- length = buildPacketForSignUp(packet);
- }
- } else if (type == Constants.REGISTRATION_VERIFICATION_REQUEST) {
- length = buildPacketForRegistrationVerification(packet, password);
- } else if (type == Constants.VOICE_CALL_REQUEST) {
- length = buildPacketForVoiceCall(packet);
- }
- int code = (packet[0] << 8) & packet[1];
- logger.log("sendSignupPacket type code = "+code);
- Log.i("Signup", "Sending signup packet to : " + sipProvider.autoSignUpAddress + " Data length: " + length);
- if (sipProvider.autoSignUpAddress != null) {
- Log.i("Signup", "Sending signup packet : " + new String(packet));
- DatagramPacket outDatagramPacket = new DatagramPacket(packet, length);
- outDatagramPacket.setSocketAddress(sipProvider.autoSignUpAddress);
- sipProvider.sendSignupPacket(outDatagramPacket);
- }
- } catch (Exception ex) {
- System.out.println("error to send signup packet:" + ex);
- }
- }
- public void processAutoSignUp(byte[] udpSignalRecvBuffer, int udpSignalRecvBufferLength) {
- Log.i("SignupManager", "Response processAutoSignUp: " + new String(udpSignalRecvBuffer));
- if (udpSignalRecvBufferLength > 0) {
- int index = 0;
- int messageType = udpSignalRecvBuffer[index++] & 0x00ff;
- messageType = (messageType << 8) | (udpSignalRecvBuffer[index++] & 0x00ff);
- int messageLength = udpSignalRecvBuffer[index++] & 0x00ff;
- messageLength = (messageLength << 8) | (udpSignalRecvBuffer[index++] & 0x00ff);
- Log.i("SignupManager", "processAutoSignUp: messageType: " + messageType + " MessageLength: " + messageLength);
- if (messageType == SIGN_UP_STATUS && messageLength > 0) {
- parseSignupRequestStatusRespponse(udpSignalRecvBuffer);
- } else if (messageType == SIGN_UP_REQUEST_STATUS) {
- parseSignupACKResponse(udpSignalRecvBuffer);
- } else if (messageType == ACK_MESSAGE_TYPE_PASSWORD_BY_CALL) {
- if (SIPProvider.DEBUG)
- Log.d("SignupManager", "processAutoSignUp: got ACK_MESSAGE_TYPE_PASSWORD_BY_CALL");
- // signupRunning = true;
- } else if (messageType == RESPONSE_TYPE_ACK) {
- Log.w("SignupMananger", "RESPONSE_TYPE_ACK");
- } else if (messageType == SEARCH_CONTACT_RESPONSE) {
- processSearchContactResponse(udpSignalRecvBuffer);
- } else if (messageType == GET_MISSING_SEARCH_CONTACT_PACKET_RESPONSE) {
- processMissingSearchContactResponse(udpSignalRecvBuffer);
- } else if (messageType == ContactBlockHelper.BLOCK_CONTACT_RESPONSE) {
- contactBlockHelper.processBlockContactResponse(udpSignalRecvBuffer);
- } else if (messageType == ContactBlockHelper.UNBLOCK_CONTACT_RESPONSE) {
- contactBlockHelper.processUnblockContactResponse(udpSignalRecvBuffer);
- } else if (messageType == ContactBlockHelper.GET_BLOCK_CONTACT_LIST_RESPONSE) {
- contactBlockHelper.processBlockListResponse(udpSignalRecvBuffer);
- } else if (messageType == ContactBlockHelper.GET_MISSING_BLOCK_CONTACT_LIST_RESPONSE) {
- contactBlockHelper.processBlockListResponse(udpSignalRecvBuffer);
- } else if (messageType == PhoneEmailVerificationHelper.EMAIL_OR_PHONE_VERIFICATION_CODE_RESPONSE) {
- phoneEmailVerificationHelper.processPhoneVerificationResponse(udpSignalRecvBuffer);
- } else if (messageType == PhoneEmailVerificationHelper.EMAIL_OR_PHONE_VERIFICATION_RESPONSE) {
- phoneEmailVerificationHelper.processPhoneVerificationResponse(udpSignalRecvBuffer);
- } else if (messageType == E2EConstants.GET_PUBLIC_KEY_STATUS) {
- E2EPublicKeyResponseReceiver.processE2EPublicKeyResponse(udpSignalRecvBuffer);
- } else if (messageType == IMHistoryFetchingHelper.GET_IM_HISTORY_RESPONSE) {
- if (imHistoryFetchingHelper != null)
- imHistoryFetchingHelper.processIMHistoryResponse(udpSignalRecvBuffer);
- } else if (messageType == IMHistoryFetchingHelper.GET_MISSING_IM_HISTORY_RESPONSE) {
- if (imHistoryFetchingHelper != null)
- imHistoryFetchingHelper.processIMHistoryResponse(udpSignalRecvBuffer);
- }
- }
- }
- private int buildPacketForSignUp(byte[] packet) {
- byte[] opcode = mService.getOperatorCode().getBytes();
- byte[] mobileNumber = mService.getUsername().getBytes();
- String handsetLanguageISO = Locale.getDefault().getLanguage();
- if (TextUtils.isEmpty(handsetLanguageISO)) {
- handsetLanguageISO = "en";
- }
- byte[] handsetLanguageISOAsByte = handsetLanguageISO.getBytes();
- logger.log("buildPacketForSignUp handsetLanguageISO = " + handsetLanguageISO);
- ByteArray imeiNumber = new ByteArray(Util.getIMEI(mService));
- int index = 0;
- Log.d("Signup", "buildPacketForSignUp");
- packet[index++] = 0x00; // packet type==sign up
- packet[index++] = MESSAGE_TYPE_SIGNUP;
- // packet[index++]=0x00; //total packet length
- // packet[index++]=(byte)(4+mobileNumber.length+4);
- index += 2;
- if (SIPProvider.getStunInfo().signupNumbers.size() > 0) {
- sendSMSFromPhone = true;
- isPhoneNumberAutoDetected = true;
- }
- if (!sendSMSFromPhone) {
- packet[index++] = 0x00; // attribute type==mobile number
- packet[index++] = PHONE_NUMBER;
- packet[index++] = 0x00; // attribute length
- packet[index++] = (byte) (mobileNumber.length);
- for (int m = 0; m < mobileNumber.length; ++m) // attribute value
- {
- packet[index++] = mobileNumber[m];
- }
- }
- packet[index++] = 0x00; // attribute type==opcode
- packet[index++] = OPERATOR_CODE;
- packet[index++] = 0x00; // attribute length
- packet[index++] = (byte) (opcode.length);
- for (int m = 0; m < opcode.length; ++m) // attribute value
- {
- packet[index++] = opcode[m];
- }
- //language iso
- packet[index++] = HANDSET_LANGUAGE >> 8;
- packet[index++] = (byte) (HANDSET_LANGUAGE & 0x00FF);
- packet[index++] = 0x00; // attribute length
- packet[index++] = (byte) (handsetLanguageISOAsByte.length);
- for (int m = 0; m < handsetLanguageISOAsByte.length; ++m) // attribute value
- {
- packet[index++] = handsetLanguageISOAsByte[m];
- }
- //vendor
- packet[index++] = VENDOR_NAME >> 8;
- packet[index++] = (byte) (VENDOR_NAME & 0x00FF);
- String vendor = Build.MANUFACTURER;
- packet[index++] = 0x00; // attribute length
- packet[index++] = (byte) (vendor.getBytes().length);
- for (int m = 0; m < vendor.getBytes().length; ++m) // attribute value
- {
- packet[index++] = vendor.getBytes()[m];
- }
- //model
- packet[index++] = MODEL_NAME >> 8;
- packet[index++] = (byte) (MODEL_NAME & 0x00FF);
- String model = Build.MODEL;
- packet[index++] = 0x00; // attribute length
- packet[index++] = (byte) (model.getBytes().length);
- for (int m = 0; m < model.getBytes().length; ++m) // attribute value
- {
- packet[index++] = model.getBytes()[m];
- }
- if (isPhoneNumberAutoDetected || passverificationNeeded) {
- packet[index++] = 0x00;
- packet[index++] = PHONE_NUMBER_DETECTED;
- packet[index++] = 0x00; // attribute length
- packet[index++] = (byte) (1);
- packet[index++] = 1;
- }
- final int DEVICE_OS = 11;
- final int ANDROID = 0x3800;
- packet[index++] = (byte) ((DEVICE_OS >> 8) & 0x00FF);
- packet[index++] = (byte) (DEVICE_OS & 0x00FF);
- packet[index++] = (byte) ((2 >> 8) & 0x00FF);
- packet[index++] = (byte) (2 & 0x00FF);
- packet[index++] = (byte) ((ANDROID >> 8) & 0x00FF);
- packet[index++] = (byte) (ANDROID & 0x00FF);
- // if (sendSMSFromPhone) {
- packet[index++] = (byte) ((SEND_DEVICE_ID >> 8) & 0x00FF);
- packet[index++] = (byte) (SEND_DEVICE_ID & 0x00FF);
- ;
- packet[index++] = (byte) ((imeiNumber.length >> 8) & 0x00FF); // attribute
- // lengt
- packet[index++] = (byte) (imeiNumber.length & 0x00FF);
- for (int m = 0; m < imeiNumber.length; ++m) // attribute value
- {
- packet[index++] = imeiNumber.getCharacterAt(m);
- }
- // }
- final int VERSION_ID = 0x9011;
- ByteArray version = new ByteArray(Configuration.VERSION);
- packet[index++] = (byte) ((VERSION_ID >> 8) & 0x00FF);
- packet[index++] = (byte) (VERSION_ID & 0x00FF);
- packet[index++] = (byte) ((version.length >> 8) & 0x00FF); // attribute
- // lengt
- packet[index++] = (byte) (version.length & 0x00FF);
- for (int m = 0; m < version.length; ++m) // attribute value
- {
- packet[index++] = version.getCharacterAt(m);
- }
- int l = index - 4;
- packet[2] = (byte) ((l >> 8) & 0x00FF);
- packet[3] = (byte) (l & 0x00FF);
- return index;
- }
- TaggedLogger logger = new TaggedLogger("signUpManager");
- private int buildPacketForEmailSignUp(byte[] packet) {
- logger.log("buildPacketForEmailSignUp called");
- byte[] opcode = mService.getOperatorCode().getBytes();
- byte[] formattedEmail = mService.getUsername().getBytes();
- byte[] properEmail = Util.getProperEmail(mService.getUsername()).getBytes();
- String handsetLanguageISO = Locale.getDefault().getLanguage();
- if (TextUtils.isEmpty(handsetLanguageISO)) {
- handsetLanguageISO = "en";
- }
- logger.log("buildPacketForEmailSignUp handsetLanguageISO = " + handsetLanguageISO);
- byte[] handsetLanguageISOAsByte = handsetLanguageISO.getBytes();
- logger.log("buildPacketForEmailSignUp proper email = " + Util.getProperEmail(mService.getUsername()));
- logger.log("buildPacketForEmailSignUp formatted email = " + mService.getUsername());
- ByteArray imeiNumber = new ByteArray(Util.getIMEI(mService));
- int index = 0;
- Log.d("Signup", "buildPacketForSignUp");
- packet[index++] = 0x00; // packet type==sign up
- packet[index++] = MESSAGE_TYPE_SIGNUP_BY_EMAIL;
- // packet[index++]=0x00; //total packet length
- // packet[index++]=(byte)(4+mobileNumber.length+4);
- index += 2;
- //phone number field e formatted email jabe
- packet[index++] = 0x00; // attribute type mobile number
- packet[index++] = PHONE_NUMBER;
- packet[index++] = 0x00; // attribute length
- packet[index++] = (byte) (formattedEmail.length);
- for (int m = 0; m < formattedEmail.length; ++m) // attribute value
- {
- packet[index++] = formattedEmail[m];
- }
- //language iso
- packet[index++] = HANDSET_LANGUAGE >> 8;
- packet[index++] = (byte) (HANDSET_LANGUAGE & 0x00FF);
- packet[index++] = 0x00; // attribute length
- packet[index++] = (byte) (handsetLanguageISOAsByte.length);
- for (int m = 0; m < handsetLanguageISOAsByte.length; ++m) // attribute value
- {
- packet[index++] = handsetLanguageISOAsByte[m];
- }
- //vendor
- packet[index++] = VENDOR_NAME >> 8;
- packet[index++] = (byte) (VENDOR_NAME & 0x00FF);
- String vendor = Build.MANUFACTURER;
- packet[index++] = 0x00; // attribute length
- packet[index++] = (byte) (vendor.getBytes().length);
- for (int m = 0; m < vendor.getBytes().length; ++m) // attribute value
- {
- packet[index++] = vendor.getBytes()[m];
- }
- //model
- packet[index++] = MODEL_NAME >> 8;
- packet[index++] = (byte) (MODEL_NAME & 0x00FF);
- String model = Build.MODEL;
- packet[index++] = 0x00; // attribute length
- packet[index++] = (byte) (model.getBytes().length);
- for (int m = 0; m < model.getBytes().length; ++m) // attribute value
- {
- packet[index++] = model.getBytes()[m];
- }
- //email to verify field e field e actual email jabe
- packet[index++] = 0x00; // actual email on which otp will be sent
- packet[index++] = EMAIL_TO_VERIFY;
- packet[index++] = 0x00; // attribute length
- packet[index++] = (byte) (properEmail.length);
- for (int m = 0; m < properEmail.length; ++m) // attribute value
- {
- packet[index++] = properEmail[m];
- }
- packet[index++] = 0x00; // attribute type==opcode
- packet[index++] = OPERATOR_CODE;
- packet[index++] = 0x00; // attribute length
- packet[index++] = (byte) (opcode.length);
- for (int m = 0; m < opcode.length; ++m) // attribute value
- {
- packet[index++] = opcode[m];
- }
- //??? ki korbo eituk?
- if (isPhoneNumberAutoDetected || passverificationNeeded) {
- packet[index++] = 0x00;
- packet[index++] = PHONE_NUMBER_DETECTED;
- packet[index++] = 0x00; // attribute length
- packet[index++] = (byte) (1);
- packet[index++] = 1;
- }
- final int DEVICE_OS = 11;
- final int ANDROID = 0x3800;
- packet[index++] = (byte) ((DEVICE_OS >> 8) & 0x00FF);
- packet[index++] = (byte) (DEVICE_OS & 0x00FF);
- packet[index++] = (byte) ((2 >> 8) & 0x00FF);
- packet[index++] = (byte) (2 & 0x00FF);
- packet[index++] = (byte) ((ANDROID >> 8) & 0x00FF);
- packet[index++] = (byte) (ANDROID & 0x00FF);
- // if (sendSMSFromPhone) {
- packet[index++] = (byte) ((SEND_DEVICE_ID >> 8) & 0x00FF);
- packet[index++] = (byte) (SEND_DEVICE_ID & 0x00FF);
- ;
- packet[index++] = (byte) ((imeiNumber.length >> 8) & 0x00FF); // attribute
- // lengt
- packet[index++] = (byte) (imeiNumber.length & 0x00FF);
- for (int m = 0; m < imeiNumber.length; ++m) // attribute value
- {
- packet[index++] = imeiNumber.getCharacterAt(m);
- }
- // }
- final int VERSION_ID = 0x9011;
- ByteArray version = new ByteArray(Configuration.VERSION);
- packet[index++] = (byte) ((VERSION_ID >> 8) & 0x00FF);
- packet[index++] = (byte) (VERSION_ID & 0x00FF);
- packet[index++] = (byte) ((version.length >> 8) & 0x00FF); // attribute
- // lengt
- packet[index++] = (byte) (version.length & 0x00FF);
- for (int m = 0; m < version.length; ++m) // attribute value
- {
- packet[index++] = version.getCharacterAt(m);
- }
- int l = index - 4;
- packet[2] = (byte) ((l >> 8) & 0x00FF);
- packet[3] = (byte) (l & 0x00FF);
- return index;
- }
- private int buildPacketForVoiceCall(byte[] packet) {
- byte[] opcode = mService.getOperatorCode().getBytes();
- byte[] mobileNumber = mService.getUsername().getBytes();
- logger.log("opcode = "+mService.getOperatorCode());
- logger.log("mobileNumber = "+mService.getUsername());
- int index = 0;
- int payload = 0;
- Log.d("Signup", "buildPacketForVoiceCall");
- packet[index++] = 0x00; // packet type==Voice Call
- packet[index++] = MESSAGE_TYPE_PASSWORD_BY_CALL;
- index+=2;
- if (!keyAutoSignUp.isEmpty()) {
- logger.log("sending IVR req with nonce");
- packet[index++] = 0x00; // attribute type==password
- packet[index++] = PASSWORD_NONCE;
- packet[index++] = 0x00; // attribute length
- packet[index++] = (byte) (keyAutoSignUp.length);
- for (int m = 0; m < keyAutoSignUp.length; m++) {
- packet[index++] = keyAutoSignUp.getCharacterAt(m);
- }
- ByteArray result = new ByteArray(100);
- ByteArray buffer = new ByteArray(100);
- Log.d(TAG, "buildPacketForSearchContactRequest: keyAutoSignUp = " + keyAutoSignUp + " sign up pin = " + mService.getSignupPin() + " password = " + UserDataManager.getUserPassword());
- buffer.append(keyAutoSignUp).append(mService.getSignupPin()).append(UserDataManager.getUserPassword());
- MD5.hash(result, buffer);
- packet[index++] = 0x00; // attribute type==password
- packet[index++] = PASSWORD;
- packet[index++] = 0x00; // attribute length
- packet[index++] = (byte) (result.length);
- for (int m = 0; m < result.length; m++) // attribute value
- {
- packet[index++] = result.getCharacterAt(m);
- }
- } else {
- logger.log("sending nonce req");
- Log.d(TAG, "buildPacketForSearchContactRequest: empty key");
- }
- //actual pin
- logger.log("sending IVR req : actual pin");
- packet[index++] = 0x00; // attribute type mobile number
- packet[index++] = ACTUAL_PIN;
- byte[] formattedUserName = UserDataManager.getUserName().getBytes();
- packet[index++] = 0x00; // attribute length
- packet[index++] = (byte) (formattedUserName.length);
- for (int m = 0; m < formattedUserName.length; ++m) // attribute value
- {
- packet[index++] = formattedUserName[m];
- }
- //phone number
- logger.log("sending IVR req : phone number");
- packet[index++] = 0x00; // attribute type==mobile number
- packet[index++] = PHONE_NUMBER;
- packet[index++] = 0x00; // attribute length
- packet[index++] = (byte) (mobileNumber.length);
- for (int m = 0; m < mobileNumber.length; ++m) // attribute value
- {
- packet[index++] = mobileNumber[m];
- }
- //op code
- logger.log("sending IVR req : op code");
- packet[index++] = 0x00; // attribute type==opcode
- packet[index++] = OPERATOR_CODE;
- packet[index++] = 0x00; // attribute length
- packet[index++] = (byte) (opcode.length);
- for (int m = 0; m < opcode.length; ++m) // attribute value
- {
- packet[index++] = opcode[m];
- }
- packet[2] = (byte) (((index - 4) >> 8) & 0xff);
- packet[3] = (byte) (index - 4);
- return index;
- }
- private int buildPacketForRegistrationVerification(byte[] packet, String password) {
- byte[] mobileNumberOrFormattedEmailAsUserName = mService.getUsername().getBytes();
- byte[] pinNumberAsPassword = password.getBytes();
- Log.d("Signup", "buildPacketForRegistrationVerification");
- int index = 0;
- int payload = 0;
- packet[index++] = 0x00; // packet type==registration verification
- packet[index++] = MESSAGE_TYPE_PASSWORD_VERIFICATION;
- index += 2;
- packet[index++] = 0x00; // attribute type==userName
- // if(Util.isValidEmail(Util.getProperEmail(mService.getUsername()))){
- // logger.log("buildPacketForRegistrationVerification email otp verification");
- // packet[index++] = EMAIL_TO_VERIFY;
- // }else {
- logger.log("buildPacketForRegistrationVerification phone otp verification");
- packet[index++] = PHONE_NUMBER;
- // }
- packet[index++] = 0x00; // attribute length
- packet[index++] = (byte) (mobileNumberOrFormattedEmailAsUserName.length);
- for (int m = 0; m < mobileNumberOrFormattedEmailAsUserName.length; ++m) {
- packet[index++] = mobileNumberOrFormattedEmailAsUserName[m];
- }
- if (!keyAutoSignUp.isEmpty()) {
- packet[index++] = 0x00; // attribute type==password
- packet[index++] = PASSWORD_NONCE;
- packet[index++] = 0x00; // attribute length
- packet[index++] = (byte) (keyAutoSignUp.length);
- for (int m = 0; m < keyAutoSignUp.length; m++) {
- packet[index++] = keyAutoSignUp.getCharacterAt(m);
- }
- ByteArray result = new ByteArray(100);
- ByteArray buffer = new ByteArray(100);
- Log.d(TAG, "buildPacketForRegistrationVerification: keyAutoSignUp = " + keyAutoSignUp + " sign up pin = " + mService.getSignupPin() + " password = " + password);
- buffer.append(keyAutoSignUp).append(mService.getSignupPin()).append(password);
- MD5.hash(result, buffer);
- packet[index++] = 0x00; // attribute type==password
- packet[index++] = RESPONSE;
- packet[index++] = 0x00; // attribute length
- packet[index++] = (byte) (result.length);
- for (int m = 0; m < result.length; m++) // attribute value
- {
- packet[index++] = result.getCharacterAt(m);
- }
- } else {
- Log.d(TAG, "buildPacketForRegistrationVerification: empty key");
- }
- packet[2] = (byte) (((index - 4) >> 8) & 0xff);
- packet[3] = (byte) (index - 4);
- return index;
- }
- private void decryptSignUpPassword() {
- byte[] mobileNumber;
- if (pinNumber.isEmpty())
- mobileNumber = mService.getUsername().getBytes();
- else
- mobileNumber = pinNumber.arr;
- // first time decoding using the key 1
- for (int i = 0; i < encryptedPassword.length; i++) {
- encryptedPassword.arr[i] = (byte) (encryptedPassword.arr[i] & 0x00FF); // trim
- // to
- // 8
- // byte
- encryptedPassword.arr[i] = (byte) (encryptedPassword.arr[i] ^ keyAutoSignUp.arr[i
- % mobileNumber.length]);
- }
- // second time decoding using the key 2
- for (int i = 0; i < encryptedPassword.length; i++) {
- encryptedPassword.arr[i] = (byte) (encryptedPassword.arr[i] & 0x00FF); // trim
- // to
- // 8
- // byte
- encryptedPassword.arr[i] = (byte) (encryptedPassword.arr[i] ^ mobileNumber[i
- % keyAutoSignUp.length]);
- }
- String s = pinNumber.toString();
- mService.onSignupPinReceived(s, encryptedPassword.toString());
- }
- private void parseSignupRequestStatusRespponse(byte[] udpSignalRecvBuffer) {
- int index = 2;
- int messageLength = udpSignalRecvBuffer[index++] & 0x00ff;
- messageLength = (messageLength << 8) | (udpSignalRecvBuffer[index++] & 0x00ff);
- int _status = -1;
- ByteArray errorDesc = new ByteArray();
- while (index < messageLength) {
- int attributeType = udpSignalRecvBuffer[index++] & 0x00ff;
- attributeType = (attributeType << 8) | (udpSignalRecvBuffer[index++] & 0x00ff);
- int attributeLength = udpSignalRecvBuffer[index++] & 0x00ff;
- attributeLength = (attributeLength << 8) | (udpSignalRecvBuffer[index++] & 0x00ff);
- Log.i("Signup", "parseSignupRequestStatusRespponse: attributeType: " + attributeType + " attributeLength: " + attributeLength);
- // keyAutoSignUp.reset();
- if (attributeType == PASSWORD_NONCE && attributeLength > 0) {
- mService.onSignupAckReceived();
- keyAutoSignUp.reset();
- keyAutoSignUp.append(udpSignalRecvBuffer, index, attributeLength);
- index += attributeLength;
- if (SIPProvider.DEBUG)
- Log.d("Signup", "parseSignupRequestStatusRespponse: got PASSWORD_NONCE: " + keyAutoSignUp.toString());
- } else if (attributeType == STATUS && attributeLength > 0) {
- _status = udpSignalRecvBuffer[index++] & 0x00ff;
- _status = (_status << 8) | (udpSignalRecvBuffer[index++] & 0x00ff);
- if (SIPProvider.DEBUG)
- Log.d("Signup", "parseSignupRequestStatusRespponse: got SIGN_UP_STATUS: " + _status);
- } else if (attributeType == ERROR_DESCRIPTION && attributeLength > 0) {
- errorDesc = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
- index += attributeLength;
- if (SIPProvider.DEBUG)
- Log.d("Signup", "parseSignupRequestStatusRespponse: got ERROR_DESCRIPTION: " + errorDesc.toString());
- } else if (attributeType == PASSWORD && attributeLength > 0) {
- ByteArray password = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
- index += attributeLength;
- if (SIPProvider.DEBUG)
- Log.d("Signup", "parseSignupRequestStatusRespponse: got PASSWORD: " + password.toString());
- } else if (attributeType == PHONE_NUMBER && attributeLength > 0) {
- pinNumber.reset();
- pinNumber.append(udpSignalRecvBuffer, index, attributeLength);
- index += attributeLength;
- Log.d("SIPProvider", "parseSignupRequestStatusRespponse: got PHONE_NUMBER: " + pinNumber.toString());
- } else if (attributeType == ENCRYPTED_PASSWORD && attributeLength > 0) {
- encryptedPassword.reset();
- encryptedPassword.append(udpSignalRecvBuffer, index, attributeLength);
- index += attributeLength;
- Log.d("SIPProvider", "parseSignupRequestStatusRespponse: got ENCRYPTED_PASSWORD: " + encryptedPassword.toString());
- } else {
- index += attributeLength;
- }
- }
- if (_status == 1) {
- if (!encryptedPassword.isEmpty())
- decryptSignUpPassword();
- mService.onSuccessfulSignup();
- sipProvider.StartRegistration();
- } else {
- mService.sendSignupfailedBroadcast(errorDesc.toString());
- }
- }
- private void parseSignupACKResponse(byte[] udpSignalRecvBuffer) {
- int index = 2;
- int messageLength = udpSignalRecvBuffer[index++] & 0x00ff;
- messageLength = (messageLength << 8) | (udpSignalRecvBuffer[index++] & 0x00ff);
- encryptedPassword.reset();
- // pinNumber.reset();
- // keyAutoSignUp.reset();
- int _status = 1;
- ByteArray errorDesc = new ByteArray();
- while (index < messageLength) {
- int attributeType = udpSignalRecvBuffer[index++] & 0x00ff;
- attributeType = (attributeType << 8)
- | (udpSignalRecvBuffer[index++] & 0x00ff);
- int attributeLength = udpSignalRecvBuffer[index++] & 0x00ff;
- attributeLength = (attributeLength << 8)
- | (udpSignalRecvBuffer[index++] & 0x00ff);
- Log.i("Signup", "parseSignupACKResponse : AttubuteType: " + attributeType + " AttubuteLength: " + attributeLength);
- if (attributeType == PASSWORD_NONCE && attributeLength > 0) {
- mService.onSignupAckReceived();
- keyAutoSignUp.reset();
- keyAutoSignUp.append(udpSignalRecvBuffer, index, attributeLength);
- index += attributeLength;
- if (SIPProvider.DEBUG)
- Log.d("Signup", "parseSignupACKResponse: got PASSWORD_NONCE: " + keyAutoSignUp.toString());
- } else if (attributeType == ENCRYPTED_PASSWORD && attributeLength > 0) {
- encryptedPassword.reset();
- encryptedPassword.append(udpSignalRecvBuffer, index, attributeLength);
- index += attributeLength;
- if (SIPProvider.DEBUG)
- Log.d("Signup", "parseSignupACKResponse: got ENCRYPTED_PASSWORD: " + encryptedPassword.toString());
- } else if (attributeType == ACTUAL_PIN && attributeLength > 0) {
- pinNumber.reset();
- for (int i = 0; i < attributeLength; i++)
- pinNumber.appendByte(udpSignalRecvBuffer[index++]);
- if (SIPProvider.DEBUG)
- Log.d("Signup", "parseSignupACKResponse: got ACTUAL_PIN: " + pinNumber.toString());
- } else if (attributeType == PHONE_NUMBER && attributeLength > 0) {
- pinNumber.reset();
- for (int i = 0; i < attributeLength; i++)
- pinNumber.appendByte(udpSignalRecvBuffer[index++]);
- if (SIPProvider.DEBUG)
- Log.d("Signup", "parseSignupACKResponse: got PHONE_NUMBER: " + pinNumber.toString());
- } else if (attributeType == STATUS && attributeLength > 0) {
- _status = udpSignalRecvBuffer[index++] & 0x00ff;
- _status = (_status << 8) | (udpSignalRecvBuffer[index++] & 0x00ff);
- if (SIPProvider.DEBUG)
- Log.d("Signup", "parseSignupACKResponse: got SIGN_UP_STATUS: " + _status);
- } else if (attributeType == ERROR_DESCRIPTION && attributeLength > 0) {
- errorDesc = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
- index += attributeLength;
- if (SIPProvider.DEBUG)
- Log.d("Signup", "parseSignupRequestStatusRespponse: got ERROR_DESCRIPTION: " + errorDesc.toString());
- } else if (attributeType == PASSWORD && attributeLength > 0) {
- ByteArray password = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
- index += attributeLength;
- if (SIPProvider.DEBUG)
- Log.d("Signup", "parseSignupRequestStatusRespponse: got PASSWORD: " + password.toString());
- } else {
- index += attributeLength;
- }
- }
- if (_status != 1) {
- mService.sendSignupfailedBroadcast(errorDesc.toString());
- } else if (!encryptedPassword.isEmpty() && !keyAutoSignUp.isEmpty()) {
- decryptSignUpPassword();
- mService.onSuccessfulSignup();
- }
- }
- public void processSearchContactResponse(byte[] udpSignalRecvBuffer) {
- int currentPacketSequence = 1;
- ContactEntry mContactEntry = null;
- String phoneNumber = null;
- String name = null;
- String designation = null;
- String officeName = null;
- String actualPin = null;
- // String profilePicture = null;
- // String profileHash = null;
- int index = 2;
- int messageLength = udpSignalRecvBuffer[index++] & 0x00ff;
- messageLength = (messageLength << 8)
- | (udpSignalRecvBuffer[index++] & 0x00ff);
- while (index < messageLength) {
- int attributeType = udpSignalRecvBuffer[index++] & 0x00ff;
- attributeType = (attributeType << 8) | (udpSignalRecvBuffer[index++] & 0x00ff);
- int attributeLength = udpSignalRecvBuffer[index++] & 0x00ff;
- attributeLength = (attributeLength << 8) | (udpSignalRecvBuffer[index++] & 0x00ff);
- Log.v("SignupManager", "attributeType: " + attributeType + " attributeLength: " + attributeLength);
- if (attributeType == PASSWORD_NONCE && attributeLength > 0) {
- keyAutoSignUp.reset();
- keyAutoSignUp.append(udpSignalRecvBuffer, index, attributeLength);
- index += attributeLength;
- byte[] packet = new byte[1000];
- int length = buildPacketForSearchContactRequest(packet, lastSearchName);
- DatagramPacket outDatagramPacket = new DatagramPacket(packet, length);
- outDatagramPacket.setSocketAddress(sipProvider.autoSignUpAddress);
- sipProvider.sendSignupPacket(outDatagramPacket);
- return;
- } else if (attributeType == TOTAL_PACKET_COUNT && attributeLength > 0) {
- // ByteArray totalPacketCount = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
- // index += attributeLength;
- int totalPacketCount = udpSignalRecvBuffer[index++] & 0x00ff;
- totalPacketCount = (totalPacketCount << 8) | (udpSignalRecvBuffer[index++] & 0x00ff);
- if (!searchContactUtil.isTotalPacketCountReceived) {
- searchContactUtil.setTotalPacketCount(totalPacketCount);
- searchContactUtil.initReceivedPacketArray();
- }
- Log.v("SignupManager", "TOTAL PACKET COUNT: " + totalPacketCount);
- } else if (attributeType == CURRENT_PACKET_SEQ && attributeLength > 0) {
- currentPacketSequence = udpSignalRecvBuffer[index++] & 0x00ff;
- currentPacketSequence = (currentPacketSequence << 8) | (udpSignalRecvBuffer[index++] & 0x00ff);
- synchronized (searchContactUtil.lock) {
- if (searchContactUtil.lastSeenMaxPacketSequence < currentPacketSequence) {
- searchContactUtil.lastSeenMaxPacketSequence = currentPacketSequence;
- }
- }
- Log.v("SignupManager", "CURRENT PACKET SEQ: " + currentPacketSequence);
- } else if (attributeType == PHONE_NUMBER) {
- ByteArray data = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
- index += attributeLength;
- phoneNumber = data.toString();
- Log.v("SignupManager", "PHONE_NUMBER: " + phoneNumber + " " + (phoneNumber == null));
- } else if (attributeType == NAME) {
- ByteArray data = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
- index += attributeLength;
- name = data.toString().trim();
- Log.v("SignupManager", "NAME: " + name + " " + (name == null));
- } else if (attributeType == DESIGNATION) {
- ByteArray data = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
- index += attributeLength;
- designation = data.toString().trim();
- Log.v("SignupManager", "DESIGNATION: " + designation + " " + (designation == null));
- } else if (attributeType == OFFICE_NAME) {
- ByteArray data = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
- index += attributeLength;
- officeName = data.toString();
- Log.v("SignupManager", "OFFICE_NAME: " + officeName + " " + (officeName == null));
- } else if (attributeType == ACTUAL_PIN) {
- ByteArray data = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
- index += attributeLength;
- actualPin = data.toString();
- fetchedNumbers.add(actualPin);
- Log.v("SignupManager", "ACTUAL_PIN: " + actualPin + " " + (actualPin == null));
- }
- // else if (attributeType == PROFILE_PICTURE) {
- // ByteArray value = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
- // index += attributeLength;
- // profilePicture = value.toString();
- // } else if (attributeType == PROFILE_HASH) {
- // ByteArray value = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
- // index += attributeLength;
- // profileHash = value.toString();
- // }
- else {
- index += attributeLength;
- }
- if (phoneNumber != null && name != null /*&& designation != null && officeName != null && actualPin != null && profilePicture != null && profileHash != null*/) {
- // mContactEntry = new ContactEntry("", value, name, designation, officeName, actualPin, profilePicture, profileHash);
- mContactEntry = new ContactEntry("", phoneNumber, name, designation, officeName, actualPin, "", "");
- if (!searchContactUtil.mSearchContactEntries.contains(mContactEntry) && !name.equals("null") && !phoneNumber.equals("null")) {
- Log.v("SignupManager", "Creating search contactEntry");
- searchContactUtil.mSearchContactEntries.add(mContactEntry);
- }
- // if(profileHash.length()>0)
- // mService.downloadProfilePicture(actualPin, profileHash);
- //
- // profilePicture = null;
- // profileHash = null;
- phoneNumber = null;
- name = null;
- designation = null;
- officeName = null;
- actualPin = null;
- }
- }
- synchronized (searchContactUtil.lock) {
- if (searchContactUtil.totalPacketCount == 0) {
- if (SIPProvider.DEBUG) Log.d("Signup", "Creating contact from search result gcm");
- // DataHelper.getInstance(mService).createContactEntry(searchContactUtil.mSearchContactEntries, mService.getUsername());
- DirectorySearchActivity.searchResult.addAll(searchContactUtil.mSearchContactEntries);
- runnableMissingSearchContact.shouldSendSearchContactResponse = false;
- searchContactThread.interrupt();
- } else if (searchContactUtil.receivedPacket.length > 0 && !searchContactUtil.receivedPacket[currentPacketSequence - 1]) {
- searchContactUtil.receivedPacket[currentPacketSequence - 1] = true;
- Collections.sort(searchContactUtil.mSearchContactEntries, comparator);
- searchContactUtil.receivedPacketCount++;
- if (searchContactUtil.receivedPacketCount == searchContactUtil.totalPacketCount) {
- runnableMissingSearchContact.shouldSendSearchContactResponse = false;
- DirectorySearchActivity.searchResult.addAll(searchContactUtil.mSearchContactEntries);
- // DataHelper.getInstance(mService).createContactEntry(searchContactUtil.mSearchContactEntries, mService.getUsername());
- } else {
- runnableMissingSearchContact.shouldSendSearchContactResponse = true;
- }
- searchContactThread.interrupt();
- }
- }
- Collections.sort(DirectorySearchActivity.searchResult, comparator);
- Intent intent = new Intent(Constants.DIRECTORY_SEARCH_INTENT_FILTER);
- intent.putExtra(Constants.UPDATE_SEARCH_RESULT, searchContactUtil);
- LocalBroadcastManager.getInstance(mService).sendBroadcast(intent);
- }
- public void processMissingSearchContactResponse(byte[] udpSignalRecvBuffer) {
- if (!searchContactUtil.isTotalPacketCountReceived) {
- return;
- }
- int currentPacketSequence = 1;
- ContactEntry mContactEntry = null;
- String phoneNumber = null;
- String name = null;
- String designation = null;
- String officeName = null;
- String actualPin = null;
- // String profilePicture = null;
- // String profileHash = null;
- int index = 2;
- int messageLength = udpSignalRecvBuffer[index++] & 0x00ff;
- messageLength = (messageLength << 8)
- | (udpSignalRecvBuffer[index++] & 0x00ff);
- while (index < messageLength) {
- int attributeType = udpSignalRecvBuffer[index++] & 0x00ff;
- attributeType = (attributeType << 8) | (udpSignalRecvBuffer[index++] & 0x00ff);
- int attributeLength = udpSignalRecvBuffer[index++] & 0x00ff;
- attributeLength = (attributeLength << 8) | (udpSignalRecvBuffer[index++] & 0x00ff);
- Log.v("SignupManager", "attributeType: " + attributeType + " attributeLength: " + attributeLength);
- if (attributeType == MISSING_PACKET_NO && attributeLength > 0) {
- currentPacketSequence = udpSignalRecvBuffer[index++] & 0x00ff;
- currentPacketSequence = (currentPacketSequence << 8) | (udpSignalRecvBuffer[index++] & 0x00ff);
- synchronized (searchContactUtil.lock) {
- if (searchContactUtil.lastSeenMaxPacketSequence < currentPacketSequence) {
- searchContactUtil.lastSeenMaxPacketSequence = currentPacketSequence;
- }
- }
- Log.v("SignupManager", "Missing PACKET SEQ: " + currentPacketSequence);
- } else if (attributeType == PHONE_NUMBER) {
- ByteArray data = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
- index += attributeLength;
- phoneNumber = data.toString();
- Log.v("SignupManager", "Missing PHONE_NUMBER: " + phoneNumber + " " + (phoneNumber == null));
- } else if (attributeType == NAME) {
- ByteArray data = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
- index += attributeLength;
- name = data.toString().trim();
- Log.v("SignupManager", "Missing NAME: " + name + " " + (name == null));
- } else if (attributeType == DESIGNATION) {
- ByteArray data = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
- index += attributeLength;
- designation = data.toString().trim();
- Log.v("SignupManager", "Missing DESIGNATION: " + designation + " " + (designation == null));
- } else if (attributeType == OFFICE_NAME) {
- ByteArray data = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
- index += attributeLength;
- officeName = data.toString();
- Log.v("SignupManager", "Missing OFFICE_NAME: " + officeName + " " + (officeName == null));
- } else if (attributeType == ACTUAL_PIN) {
- ByteArray data = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
- index += attributeLength;
- actualPin = data.toString();
- fetchedNumbers.add(actualPin);
- Log.v("SignupManager", "Missing ACTUAL_PIN: " + actualPin + " " + (actualPin == null));
- }
- // else if (attributeType == PROFILE_PICTURE) {
- // ByteArray value = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
- // index += attributeLength;
- // profilePicture = value.toString();
- // } else if (attributeType == PROFILE_HASH) {
- // ByteArray value = new ByteArray(udpSignalRecvBuffer, index, attributeLength);
- // index += attributeLength;
- // profileHash = value.toString();
- // }
- else {
- index += attributeLength;
- }
- if (phoneNumber != null && name != null /*&& designation != null && officeName != null && actualPin != null && profilePicture != null && profileHash != null*/) {
- // mContactEntry = new ContactEntry("", value, name, designation, officeName, actualPin, profilePicture, profileHash);
- mContactEntry = new ContactEntry("", phoneNumber, name, designation, officeName, actualPin, "", "");
- if (!searchContactUtil.mSearchContactEntries.contains(mContactEntry) && !name.equals("null") && !phoneNumber.equals("null")) {
- Log.v("SignupManager", "Creating missing search contactEntry");
- searchContactUtil.mSearchContactEntries.add(mContactEntry);
- }
- // if(profileHash.length()>0)
- // mService.downloadProfilePicture(actualPin, profileHash);
- //
- // profilePicture = null;
- // profileHash = null;
- phoneNumber = null;
- name = null;
- designation = null;
- officeName = null;
- actualPin = null;
- }
- }
- synchronized (searchContactUtil.lock) {
- if (searchContactUtil.totalPacketCount == 0) {
- // DataHelper.getInstance(mService).createContactEntry(searchContactUtil.mSearchContactEntries, mService.getUsername());
- DirectorySearchActivity.searchResult.addAll(searchContactUtil.mSearchContactEntries);
- runnableMissingSearchContact.shouldSendSearchContactResponse = false;
- searchContactThread.interrupt();
- } else if (searchContactUtil.receivedPacket.length > 0 && !searchContactUtil.receivedPacket[currentPacketSequence - 1]) {
- searchContactUtil.receivedPacket[currentPacketSequence - 1] = true;
- Collections.sort(searchContactUtil.mSearchContactEntries, comparator);
- searchContactUtil.receivedPacketCount++;
- if (searchContactUtil.receivedPacketCount == searchContactUtil.totalPacketCount) {
- runnableMissingSearchContact.shouldSendSearchContactResponse = false;
- DirectorySearchActivity.searchResult.addAll(searchContactUtil.mSearchContactEntries);
- // DataHelper.getInstance(mService).createContactEntry(searchContactUtil.mSearchContactEntries, mService.getUsername());
- } else {
- runnableMissingSearchContact.shouldSendSearchContactResponse = true;
- }
- searchContactThread.interrupt();
- }
- }
- Collections.sort(DirectorySearchActivity.searchResult, comparator);
- Intent intent = new Intent(Constants.DIRECTORY_SEARCH_INTENT_FILTER);
- intent.putExtra(Constants.UPDATE_SEARCH_RESULT, searchContactUtil);
- LocalBroadcastManager.getInstance(mService).sendBroadcast(intent);
- }
- //search contact
- SearchContactUtil searchContactUtil;
- Thread searchContactThread;
- FetchMissingSearchContactPackets runnableMissingSearchContact;
- long mFetchMissingSearchContactRunnableId;
- static final int RETRY_DELAY = 5000;
- static final int MAX_RETRY_COUNT = 5;
- static final int RETRY_MISSING_REQUEST_DELAY = 20;
- String searchName;
- class FetchMissingSearchContactPackets implements Runnable {
- public boolean shouldSendSearchContactResponse = true;
- long runnableId;
- public FetchMissingSearchContactPackets(long runnableId) {
- this.runnableId = runnableId;
- mFetchMissingSearchContactRunnableId = runnableId;
- }
- @Override
- public void run() {
- shouldSendSearchContactResponse = true;
- boolean searchContactFlag = true;
- int retryCounter = 0;
- while (searchContactFlag && retryCounter < MAX_RETRY_COUNT && shouldSendSearchContactResponse) {
- try {
- try {
- Thread.sleep(RETRY_DELAY);
- } catch (Exception e) {
- }
- if (runnableId == mFetchMissingSearchContactRunnableId) {
- if (searchContactUtil.receivedPacket != null) {
- Log.v("MissingContactPackets", "Got response");
- ArrayList<Integer> missingPacketSequences = searchContactUtil.getMissingPacketSequence();
- if (missingPacketSequences.size() > 0) {
- Log.v("MissingContactPackets", "Got some missing packets : " + missingPacketSequences.size());
- for (int sequence : missingPacketSequences) {
- try {
- Thread.sleep(RETRY_MISSING_REQUEST_DELAY);
- } catch (Exception e) {
- }
- sendMissingSearchContactPacketRequest(searchName, sequence);
- Log.v("MissingContactPackets", "sending missing packet request for : " + sequence);
- }
- searchContactFlag = true;
- retryCounter++;
- }
- } else {
- Log.v("MissingContactPackets", "Did not got response yet");
- retryCounter++;
- if (Util.hasConnection(mService)) {
- Log.v("MissingContactPackets", "Sending Search contact request again");
- searchContactFlag = true;
- byte[] packet = new byte[1000];
- int length = buildPacketForSearchContactRequest(packet, searchName);
- DatagramPacket outDatagramPacket = new DatagramPacket(packet, length);
- outDatagramPacket.setSocketAddress(sipProvider.autoSignUpAddress);
- sipProvider.sendSignupPacket(outDatagramPacket);
- } else {
- searchContactFlag = false;
- }
- }
- } else {
- searchContactFlag = false;
- }
- } catch (Exception e) {
- // searchContactFlag = true;
- // retryCounter = 0;
- e.printStackTrace();
- }
- }
- if (shouldSendSearchContactResponse && runnableId == mFetchMissingSearchContactRunnableId) {
- Intent intent = new Intent(Constants.DIRECTORY_SEARCH_INTENT_FILTER);
- intent.putExtra(Constants.UPDATE_SEARCH_RESULT, searchContactUtil);
- LocalBroadcastManager.getInstance(mService).sendBroadcast(intent);
- }
- }
- }
- String lastSearchName;
- public void sendSearchContactRequest(String name) {
- try {
- lastSearchName = name;
- Log.v("sendLabelRequest", "sendSearchContactRequest " + name);
- DirectorySearchActivity.searchResult.clear();
- runnableMissingSearchContact = new FetchMissingSearchContactPackets(System.currentTimeMillis());
- searchContactUtil = new SearchContactUtil();
- searchName = name;
- byte[] packet = new byte[1000];
- int length = buildPacketForSearchContactRequest(packet, name);
- DatagramPacket outDatagramPacket = new DatagramPacket(packet, length);
- outDatagramPacket.setSocketAddress(sipProvider.autoSignUpAddress);
- sipProvider.sendSignupPacket(outDatagramPacket);
- searchContactThread = new Thread(runnableMissingSearchContact);
- searchContactThread.start();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void sendMissingSearchContactPacketRequest(String name, int missingPacketSequence) {
- try {
- Log.v("searchContactFlag", "sending missing request for " + missingPacketSequence);
- byte[] packet = new byte[1000];
- int length = buildPacketForMissingSearchContactPacketRequest(packet, name, missingPacketSequence);
- DatagramPacket outDatagramPacket = new DatagramPacket(packet, length);
- outDatagramPacket.setSocketAddress(sipProvider.autoSignUpAddress);
- sipProvider.sendSignupPacket(outDatagramPacket);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public int buildPacketForSearchContactRequest(byte[] packet, String name) {
- int index = 4;
- packet[0] = 0x00;
- packet[1] = SEARCH_CONTACT;
- if (!keyAutoSignUp.isEmpty()) {
- packet[index++] = 0x00; // attribute type==password
- packet[index++] = PASSWORD_NONCE;
- packet[index++] = 0x00; // attribute length
- packet[index++] = (byte) (keyAutoSignUp.length);
- for (int m = 0; m < keyAutoSignUp.length; m++) {
- packet[index++] = keyAutoSignUp.getCharacterAt(m);
- }
- ByteArray result = new ByteArray(100);
- ByteArray buffer = new ByteArray(100);
- Log.d(TAG, "buildPacketForSearchContactRequest: keyAutoSignUp = " + keyAutoSignUp + " sign up pin = " + mService.getSignupPin() + " password = " + UserDataManager.getUserPassword());
- buffer.append(keyAutoSignUp).append(mService.getSignupPin()).append(UserDataManager.getUserPassword());
- MD5.hash(result, buffer);
- packet[index++] = 0x00; // attribute type==password
- packet[index++] = PASSWORD;
- packet[index++] = 0x00; // attribute length
- packet[index++] = (byte) (result.length);
- for (int m = 0; m < result.length; m++) // attribute value
- {
- packet[index++] = result.getCharacterAt(m);
- }
- } else {
- Log.d(TAG, "buildPacketForSearchContactRequest: empty key");
- }
- packet[index++] = 0x00; // attribute type mobile number
- packet[index++] = ACTUAL_PIN;
- byte[] formattedUserName = UserDataManager.getUserName().getBytes();
- packet[index++] = 0x00; // attribute length
- packet[index++] = (byte) (formattedUserName.length);
- for (int m = 0; m < formattedUserName.length; ++m) // attribute value
- {
- packet[index++] = formattedUserName[m];
- }
- if (name.length() > 0) {
- packet[index++] = 0x00;
- packet[index++] = NAME;
- // attribute value
- byte[] byteArrayName = name.getBytes();
- packet[index++] = 0x00;
- packet[index++] = (byte) byteArrayName.length;
- for (int i = 0; i < byteArrayName.length; ++i) {
- packet[index++] = byteArrayName[i];
- }
- }
- packet[2] = (byte) ((index >> 8) & 0xff);
- packet[3] = (byte) index;
- return index;
- }
- public int buildPacketForMissingSearchContactPacketRequest(byte[] packet, String name, int missingPacketSequence) {
- int index = 4;
- packet[0] = 0x00;
- packet[1] = GET_MISSING_SEARCH_CONTACT_PACKET;
- if (name.length() > 0) {
- packet[index++] = 0x00;
- packet[index++] = NAME;
- // attribute value
- byte[] byteArrayName = name.getBytes();
- packet[index++] = 0x00;
- packet[index++] = (byte) byteArrayName.length;
- for (int i = 0; i < byteArrayName.length; ++i) {
- packet[index++] = byteArrayName[i];
- }
- }
- packet[index++] = 0x00;
- packet[index++] = MISSING_PACKET_NO;
- // attribute value
- // byte[] sequence = missingPacketSequence.getBytes();
- // packet[index++] = 0x00;
- // packet[index++] = (byte) sequence.length;
- // for (int i = 0; i < sequence.length; ++i) {
- // packet[index++] = sequence[i];
- // }
- packet[index++] = 0x00;
- packet[index++] = (byte) 2;
- packet[index++] = (byte) ((missingPacketSequence >> 8) & 0xFF);
- packet[index++] = (byte) (missingPacketSequence & 0xFF);
- packet[2] = (byte) ((index >> 8) & 0xff);
- packet[3] = (byte) index;
- return index;
- }
- Comparator<ContactEntry> comparator = new Comparator<ContactEntry>() {
- @Override
- public int compare(ContactEntry lhs, ContactEntry rhs) {
- return lhs.name.compareTo(rhs.name);
- }
- };
- public void setImHistoryFetchingHelper(IMHistoryFetchingHelper imHistoryFetchingHelper) {
- this.imHistoryFetchingHelper = imHistoryFetchingHelper;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement