Search code examples
androidchatquickblox

Get chat dialogs after login to chat Quickblox


I am trying to invoke a method to get chat dialogs after logging in succesfully on QBChatService but when I do it, it triggers onError method, the method that gets dialogs works fine if I invoke it separately. Here is my code:

    public class ChatService {

      public static final int AUTO_PRESENCE_INTERVAL_IN_SECONDS = 30;
      private static ChatService instance;
      private QBChatService qbChatService;
      private ArrayList<QBUser> opponentsUsers = new ArrayList<QBUser>();
      private Map<List<Integer>, QBDialog> chatDialogsHashMap = new HashMap<List<Integer>, QBDialog>();
      private ArrayList<QBDialog> chatDialogsArrayList = new ArrayList<QBDialog>();

      private ConnectionListener chatConnectionListener = new ConnectionListener() {
        @Override
        public void connected(XMPPConnection connection) {
            Log.w("CHAT SERVICE", "connected");
        }

        @Override
        public void authenticated(XMPPConnection connection) {
            Log.w("CHAT SERVICE", "authenticated");
        }

        @Override
        public void connectionClosed() {
            Log.w("CHAT SERVICE", "connectionClosed");
        }

        @Override
        public void connectionClosedOnError(final Exception e) {
            Log.w("CHAT SERVICE", "connectionClosedOnError: " + e.getLocalizedMessage());
        }

        @Override
        public void reconnectingIn(final int seconds) {
            if(seconds % 5 == 0) {
                Log.w("CHAT SERVICE", "reconnectingIn: " + seconds);
            }
        }

        @Override
        public void reconnectionSuccessful() {
            Log.w("CHAT SERVICE", "reconnectionSuccessful");
        }

        @Override
        public void reconnectionFailed(final Exception error) {
            Log.w("CHAT SERVICE", "reconnectionFailed: " + error.getLocalizedMessage());
        }
    };



    public ChatService(){
        this.qbChatService = QBChatService.getInstance();
        this.qbChatService.addConnectionListener(this.chatConnectionListener);
    }

    public static synchronized ChatService getInstance(){
        if(instance == null){
           instance = new ChatService();
        }
        return instance;
    }

    public static boolean initializedIfNeeded(Context context){
        if(!QBChatService.isInitialized()){
            QBChatService.init(context);
            QBChatService.setDebugEnabled(true);
            Log.w("INITIALIZED QBCHAT", "SUCCESS");
            return true;
        }
        return false;
    }

    public void loginToChat(){
        QBAuth.getSession(new QBEntityCallbackImpl<QBSession>() {
            @Override
            public void onSuccess(QBSession qbSession, Bundle bundle) {
                Log.w("GET SESSION", "SUCCESS");
                QBUser currentSessionUser = new QBUser();
                currentSessionUser.setId(qbSession.getUserId());
                currentSessionUser.setLogin(MenuActivity.sharedPreferences.getString("USERNAME", ""));
                currentSessionUser.setPassword(MenuActivity.sharedPreferences.getString("PASSWORD", ""));
                ChatService.this.qbChatService.login(currentSessionUser, new QBEntityCallbackImpl() {
                    @Override
                    public void onSuccess() {
                        Log.w("LOGIN TO CHAT", "SUCCESS");
                        ChatService.this.storeChatDialogs();
                        try {
                            ChatService.this.qbChatService.startAutoSendPresence(AUTO_PRESENCE_INTERVAL_IN_SECONDS);
                        } catch (SmackException.NotLoggedInException e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void onError(List list) {
                        Log.w("LOGIN TO CHAT", "ERROR");
                    }
                });
            }


            @Override
            public void onError(List<String> list) {
                Log.w("GET SESSION", "ERROR");
            }
        });


    }

    public void createPrivateDialog(final Activity activity, QBDialog dialogToCreate){
        this.qbChatService.getGroupChatManager().createDialog(dialogToCreate, new QBEntityCallbackImpl<QBDialog>() {
            @Override
            public void onSuccess(QBDialog qbDialog, Bundle bundle) {
                Log.w("CREATE DIALOG", "SUCCESS");
                ((MenuActivity) activity).changeDialog(qbDialog);
            }

            @Override
            public void onError(List<String> list) {
                Log.w("CREATE DIALOG", "EXITO");
            }
        });
    }

    public void storeChatDialogs(){
        QBChatService.getChatDialogs(QBDialogType.PRIVATE, new QBRequestGetBuilder(), new QBEntityCallbackImpl<ArrayList<QBDialog>>() {
            @Override
            public void onSuccess(ArrayList<QBDialog> qbDialogs, Bundle bundle) {
                Log.w("STORE CHAT DIALOGS", "SUCCESS");
                ArrayList<QBDialog> dialogsFromCurrentUser = new ArrayList<QBDialog>();
                for (QBDialog dialog : qbDialogs) {
                    ArrayList<Integer> occupantsIDs = dialog.getOccupants();
                    for (Integer ID : occupantsIDs) {
                        if (ChatService.getInstance().getCurrentChatUser().getId() == ID.intValue()) {
                            dialogsFromCurrentUser.add(dialog);
                        }
                    }
                }
                ChatService.this.storeOpponentsUsersOfPrivateDialogs(dialogsFromCurrentUser);
                ChatService.this.setChatDialogsHashMap(dialogsFromCurrentUser);
                ChatService.this.setChatDialogsArrayList(dialogsFromCurrentUser);
            }

            @Override
            public void onError(List<String> list) {
                Log.w("STORE CHAT DIALOGS", "ERROR");
            }
        });
    }

    public QBUser getCurrentChatUser(){
        return this.qbChatService.getUser();
    }

    public Integer getOpponentIDForPrivateDialog(QBDialog dialog){
        Integer opponentID = 0;
        for(Integer userID : dialog.getOccupants()){
            if(!userID.equals(getCurrentChatUser().getId())){
                opponentID = userID;
            }
        }
        return opponentID;
    }

    public void storeOpponentsUsersOfPrivateDialogs(ArrayList<QBDialog> privateDialogs){
        this.opponentsUsers.clear();
        QBUsers.getUsersByIDs(this.getOpponentsIDsOfPrivateDialogs(privateDialogs), new QBPagedRequestBuilder(), new QBEntityCallbackImpl<ArrayList<QBUser>>() {
            @Override
            public void onSuccess(ArrayList<QBUser> qbUsers, Bundle bundle) {
                Log.w("GET USERS BY IDS", "SUCCESS");
                ChatService.this.opponentsUsers = qbUsers;
            }

            @Override
            public void onError(List<String> list) {
                Log.w("GET USERS BY IDS", "ERROR");
            }
        });
    }

    public ArrayList<Integer> getOpponentsIDsOfPrivateDialogs(ArrayList<QBDialog> privateDialogs){
        ArrayList<Integer> opponentsIDs = new ArrayList<Integer>();
        for(QBDialog dialog : privateDialogs){
            ArrayList<Integer> occupantsIDs = dialog.getOccupants();
            for(Integer ID : occupantsIDs){
                if(this.getCurrentChatUser().getId() != ID.intValue()){
                    opponentsIDs.add(ID);
                }
            }
        }
        return opponentsIDs;
    }

    public QBDialog getDialog(List<Integer> occupantsIDs){
        return this.chatDialogsHashMap.get(occupantsIDs);
    }

    public ArrayList<QBDialog> getChatDialogsArrayList(){
        return this.chatDialogsArrayList;
    }

    public void setChatDialogsArrayList(List<QBDialog> dialogs){
        this.chatDialogsArrayList.clear();
        for(QBDialog dialog : dialogs){
            this.chatDialogsArrayList.add(dialog);
        }
    }

    public Map<List<Integer>, QBDialog> getChatDialogsHashMap(){
        return this.chatDialogsHashMap;
    }

    public void setChatDialogsHashMap(List<QBDialog> dialogs){
        this.chatDialogsHashMap.clear();
        for(QBDialog dialog : dialogs){
            this.chatDialogsHashMap.put(dialog.getOccupants(), dialog);
        }
    }

    public void setAndRefreshAdapter(Context context, ListView dialogsList){
        ArrayList<String> opponentsNames = new ArrayList<String>();
        for(QBUser opponentUser : this.opponentsUsers){
            opponentsNames.add(opponentUser.getLogin());
            Log.w("SET ADAPTER", opponentUser.getLogin());
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, R.layout.option_player, R.id.textView_PlayersFragment_textOption, opponentsNames);
        dialogsList.setAdapter(adapter);
    }
}

It doesn't show any error, but I put logcat here:

07-28 17:06:59.217  28003-28003/com.example.jozumaster.myapplication W/INITIALIZED QBCHAT﹕ SUCCESS
07-28 17:06:59.630  28003-28051/com.example.jozumaster.myapplication E/NativeCrypto﹕ ssl=0x6186c9d8 cert_verify_callback x509_store_ctx=0x625de940 arg=0x0
07-28 17:06:59.630  28003-28051/com.example.jozumaster.myapplication E/NativeCrypto﹕ ssl=0x6186c9d8 cert_verify_callback calling verifyCertificateChain authMethod=DHE_RSA
07-28 17:07:00.027  28003-28003/com.example.jozumaster.myapplication W/CREATE SESSION﹕ SUCCESS
07-28 17:07:08.832  28003-28522/com.example.jozumaster.myapplication E/NativeCrypto﹕ ssl=0x61d0bdb0 cert_verify_callback x509_store_ctx=0x64cf6940 arg=0x0
07-28 17:07:08.832  28003-28522/com.example.jozumaster.myapplication E/NativeCrypto﹕ ssl=0x61d0bdb0 cert_verify_callback calling verifyCertificateChain authMethod=DHE_RSA
07-28 17:07:08.970  28003-28003/com.example.jozumaster.myapplication W/dalvikvm﹕ VFY: unable to resolve virtual method 457: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
07-28 17:07:08.971  28003-28003/com.example.jozumaster.myapplication W/dalvikvm﹕ VFY: unable to resolve virtual method 479: Landroid/content/res/TypedArray;.getType (I)I
07-28 17:07:09.415  28003-28529/com.example.jozumaster.myapplication E/NativeCrypto﹕ ssl=0x615d1a88 cert_verify_callback x509_store_ctx=0x64ef2940 arg=0x0
07-28 17:07:09.415  28003-28529/com.example.jozumaster.myapplication E/NativeCrypto﹕ ssl=0x615d1a88 cert_verify_callback calling verifyCertificateChain authMethod=DHE_RSA
07-28 17:07:09.506  28003-28003/com.example.jozumaster.myapplication W/SIGN IN﹕ SUCCESS
07-28 17:07:09.852  28003-28003/com.example.jozumaster.myapplication W/GET SESSION﹕ SUCCESS
07-28 17:07:10.314  28003-28538/com.example.jozumaster.myapplication W/CHAT SERVICE﹕ connected
07-28 17:07:11.008  28003-28538/com.example.jozumaster.myapplication W/CHAT SERVICE﹕ authenticated
07-28 17:07:11.011  28003-28538/com.example.jozumaster.myapplication W/LOGIN TO CHAT﹕ SUCCESS
07-28 17:07:11.017  28003-28538/com.example.jozumaster.myapplication W/LOGIN TO CHAT﹕ ERROR

Solution

  • I found the solution, so I post here to help other people who have same problem as me.

    Callbacks from Quickblox works on secondary thread so if you want invoke a method that create another secondary thread, you need to do it from UI thread so you need to create a Handler object to connect with UI thread, Quickblox callback make this automatic. The problem was that secondary threads doesn't have a message queue (Handlers queue) by default (UI thread does), you have to create it using Looper class. Looper | Android Deve

    Here is the code working:

    public void loginToChat(final QBUser currentSessionUser){
                QBChatService.getInstance().login(currentSessionUser, new QBEntityCallbackImpl() {
                    @Override
                    public void onSuccess() {
                        Looper.prepare();
                        Log.w("LOGIN TO CHAT", "SUCCESS");
                        try {
                            QBChatService.getInstance().startAutoSendPresence(ChatService.AUTO_PRESENCE_INTERVAL_IN_SECONDS);
                        } catch (SmackException.NotLoggedInException e) {
                            e.printStackTrace();
                        }
                        ChatService.getInstance().storeChatDialogs();
                        Looper.loop();
                    }
    
                    @Override
                    public void onError(List errors) {
                        Log.w("LOGIN TO CHAT", "ERROR" + errors);
                    }
                });
    }