Vedroid36
2/7/2013 - 9:51 AM

gistfile1.txt

package com.vyke.android.qbchat.utils;

import android.content.Context;
import com.quickblox.core.QBCallback;
import com.quickblox.core.QBCallbackImpl;
import com.quickblox.core.result.Result;
import com.quickblox.module.users.QBUsers;
import com.quickblox.module.users.model.QBUser;
import com.quickblox.module.users.result.QBUserResult;
import com.vyke.android.contact.ContactAPI;
import com.vyke.android.models.Contact;
import com.vyke.android.qbchat.dao.ChatDBAdapter;
import com.vyke.android.qbchat.models.Buddy;
import com.vyke.android.utils.ApiConstants;

/**
 * @author <a href="mailto:oleg@quickblox.com">Oleg Soroka</a>
 */
public class ChatHelper {

    private static final String LOG_TAG = "ChatHelper";

    private Lo lo = new Lo(this);

    private ChatDBAdapter chatDBAdapter;
    private Context context;
    private ContactResult contactResult;
    private String flagChatOrMessageCreated;



    public ChatHelper(Context context) {
        this.context = context;
        chatDBAdapter = ChatDBAdapter.getInstance(context);
    }

    public void synchronizeContact(String jid, String flagChatOrMessage) {
        lo.gt(LOG_TAG, ">>>>  synchronizeContact(String jid, String flagChatOrMessage): " + jid);

        this.flagChatOrMessageCreated = flagChatOrMessage;

        Buddy buddy = chatDBAdapter.fetchBuddyByJid(jid);
        lo.g("buddy=%s", buddy);

        if (buddy != null && buddy.getContactId() == null) {
            String qbId = jid.split("-")[0];
            int id = Integer.parseInt(qbId);
            lo.g("get from QB dy id=%s", id);

            QBUsers.getUser(id, gettingUserById);
            //QBRequestCanceler requestCanceler = QBUsers.getUser(id, gettingUserById);

        } else {
            syncComplete(buddy);
        }
    }

    private QBCallback gettingUserById = new QBCallbackImpl() {

        @Override
        public void onComplete(Result result) {
            lo.gt(LOG_TAG, ">>>>  [QBCallback gettingUserById] onComplete(Result): " + result);

            if (result.isSuccess()) {

                QBUser user = ((QBUserResult) result).getUser();
                String phone = user.getLogin();
                lo.g("QBUser login (phone) = " + phone);

                Contact contact = ContactAPI.getAPI().getContactByPhone(context, phone);
                lo.g("contact = ", contact);

                chatDBAdapter = ChatDBAdapter.getInstance(context);
                String qbUser = getJidFromId(user.getId());

                Buddy buddy = chatDBAdapter.fetchBuddyByJid(qbUser);
                buddy.setPhoneNumber(phone);

                if (contact != null) {
                    buddy.setContactId(contact.getId());
                }
                chatDBAdapter.updateBuddy(buddy);

                syncComplete(buddy);
            } else {
                lo.g(result.getErrors());
                if (contactResult != null) {
                    contactResult.onSyncContactError();
                }
            }
        }
              /*
        @Override
        public void onComplete(Result result, Object o) {
            lo.gt(LOG_TAG, ">>>>  [QBCallback gettingUserById] onComplete(Result result, Object o): " + result);
        }       */
    };

    private void syncComplete(Buddy buddy) {

        if (contactResult != null) {
            contactResult.onSyncContactComplete(buddy, this.flagChatOrMessageCreated);
        }
    }

    public String getBuddyHumanReadableName(Buddy buddy){
        String buddyHumanName = "unknown buddy";

        if (buddy.getContactId() != null) {
            Contact contact = ContactAPI.getAPI().getContact(context, buddy.getContactId(), null);
            buddyHumanName = contact.getDisplayName();
        } else if (buddy.getPhoneNumber() != null) {
            Contact contact = ContactAPI.getAPI().getContactByPhone(context, buddy.getPhoneNumber());
            if (contact != null) {
                buddyHumanName = contact.getDisplayName();
            }
        } else {
            String qbId = buddy.getJid().split("-")[0];
            int id = Integer.parseInt(qbId);
            lo.g("buddy QBUser ID = " + id);
            buddyHumanName = String.valueOf(id);
        }

        return buddyHumanName;
    }

    private String getJidFromId(int qbUserId){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(String.valueOf(qbUserId));
        stringBuilder.append("-").append(ApiConstants.QB_APP_ID).append(ApiConstants.MAIL);

        return stringBuilder.toString();
    }

    public interface ContactResult {
        void onSyncContactComplete(Buddy buddy, String flagChatOrMessage);
        void onSyncContactError();
    }
}