<commit_before>package com.quickblox.snippets.modules;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;
import com.quickblox.core.QBCallbackImpl;
import com.quickblox.module.auth.QBAuth;
import com.quickblox.module.chat.QBChat;
import com.quickblox.module.chat.listeners.ChatMessageListener;
import com.quickblox.module.chat.listeners.LoginListener;
import com.quickblox.module.chat.listeners.RoomListener;
import com.quickblox.module.chat.model.QBChatRoom;
import com.quickblox.module.chat.smack.SmackAndroid;
import com.quickblox.module.chat.utils.QBChatUtils;
import com.quickblox.module.users.model.QBUser;
import com.quickblox.snippets.Snippet;
import com.quickblox.snippets.Snippets;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;

import java.util.Collection;

/**
 * User: <NAME>
 * Date: 11.10.12
 * Time: 17:01
 */
public class SnippetsChat extends Snippets {

    public static final String ROOM_NAME = "temp_room_for_snippet_test";
    private Handler handler = new Handler(Looper.getMainLooper());

    //    private static final String TAG = SnippetsChat.class.getSimpleName();
    public static final int USER_ID = 999;
    public static final String TEST_PASSWORD = "<PASSWORD>";

    private ChatMessageListener chatMessageListener;
    private final QBUser qbUser;
    private RoomListener roomReceivingListener;
    private QBChatRoom currentQBChatRoom;
    private PacketListener pMessageListener;

    public SnippetsChat(Context context) {
        super(context);
        SmackAndroid.init(context);

        // init test user
        qbUser = new QBUser();
        qbUser.setId(USER_ID);
        qbUser.setPassword(<PASSWORD>);

        initRoomListener();
        initRoomMessageListener();

        snippets.add(loginInChat);
        snippets.add(isLoggedIn);
        snippets.add(logoutFromChat);
        //
        snippets.add(sendPresence);
        snippets.add(startAutoSendPresence);
        snippets.add(addChatMessageListener);
        snippets.add(sendMessageWithStringParameter);
        snippets.add(sendMessageWithMessageParameter);
        snippets.add(createRoom);
        snippets.add(joinRoom);
        snippets.add(sendMessageToRoom);
        snippets.add(getOnlineRoomUsers);
        snippets.add(leaveRoom);
    }

    //
    ///////////////////////////////////////////// Login/Logout /////////////////////////////////////////////
    //
    Snippet loginInChat = new Snippet("login in Chat") {
        @Override
        public void execute() {
            QBChat.getInstance().loginWithUser(qbUser, new LoginListener() {
                @Override
                public void onLoginSuccess() {
                    System.out.println("success when login");
                    QBChat.getInstance().createRoom(ROOM_NAME, qbUser, false, true, roomReceivingListener);
                }

                @Override
                public void onLoginError() {
                    System.out.println("error when login");
                }
            });
        }
    };

    Snippet isLoggedIn = new Snippet("Is logged In") {
        @Override
        public void execute() {
            boolean isLoggedIn = QBChat.getInstance().isLoggedIn();
            System.out.println("isLoggedIn: " + isLoggedIn);
        }
    };

    Snippet logoutFromChat = new Snippet("Logout from Chat") {
        @Override
        public void execute() {
            QBChat.getInstance().logout();
        }
    };


    Snippet sendPresence = new Snippet("send presence") {
        @Override
        public void execute() {
            QBChat.getInstance().sendPresence();
        }
    };

    Snippet startAutoSendPresence = new Snippet("start auto send presence") {
        @Override
        public void execute() {
            int intervalInSecondsBetweenSending = 10;
            QBChat.getInstance().startAutoSendPresence(intervalInSecondsBetweenSending);
        }
    };

    Snippet addChatMessageListener = new Snippet("add chat message listener") {
        @Override
        public void execute() {
            chatMessageListener = new ChatMessageListener() {
                @Override
                public void processMessage(Message message) {
                    int userId = QBChatUtils.parseQBUser(message.getFrom());
                    String messageBody = message.getBody();
                    Message.Type type = message.getType();
                    String userChatLogin = QBChat.getInstance().getChatLoginFull(new QBUser(userId));
                    final String messageText = String.format("Received message from QuickBlox %s from user (full login: %s) with id %s:'%s'", type, userChatLogin, userId, messageBody);
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(context, messageText, Toast.LENGTH_SHORT).show();
                        }
                    });
                    System.out.println(">>> " + messageText);
                }

                @Override
                public boolean accept(Message.Type type) {
                    switch (type) {
                        case normal:
                        case chat:
                        case groupchat:
                            return true;
                        default:
                            return false;
                    }
                }
            };
            QBChat.getInstance().addChatMessageListener(chatMessageListener);
        }
    };

    Snippet sendMessageWithStringParameter = new Snippet("send message with String parameter") {
        @Override
        public void execute() {
            QBChat.getInstance().sendMessage(USER_ID, "some message as String parameter");
        }
    };

    Snippet sendMessageWithMessageParameter = new Snippet("send message with Message parameter") {
        @Override
        public void execute() {
            Message message = new Message(QBChatUtils.getChatLoginFull(USER_ID), Message.Type.chat);
            String messageText = "some message as Message parameter";
            message.setBody(messageText);
            QBChat.getInstance().sendMessage(USER_ID, message);
        }
    };

    Snippet createRoom = new Snippet("create Room") {
        @Override
        public void execute() {
            QBChat.getInstance().createRoom(ROOM_NAME, qbUser, false, true, roomReceivingListener);
        }
    };

    Snippet joinRoom = new Snippet("join Room") {
        @Override
        public void execute() {
            QBChat.getInstance().joinRoom(ROOM_NAME, qbUser, roomReceivingListener);
        }
    };

<commit_msg>update CHAT snippets (1-1 chat)<commit_after>package com.quickblox.snippets.modules;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;
import com.quickblox.module.chat.QBChat;
import com.quickblox.module.chat.listeners.ChatMessageListener;
import com.quickblox.module.chat.listeners.LoginListener;
import com.quickblox.module.chat.listeners.RoomListener;
import com.quickblox.module.chat.model.QBChatRoom;
import com.quickblox.module.chat.smack.SmackAndroid;
import com.quickblox.module.chat.utils.QBChatUtils;
import com.quickblox.module.users.model.QBUser;
import com.quickblox.snippets.Snippet;
import com.quickblox.snippets.Snippets;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;

import java.util.Collection;

/**
 * User: <NAME>
 * Date: 11.10.12
 * Time: 17:01
 */
public class SnippetsChat extends Snippets {

    private Handler handler = new Handler(Looper.getMainLooper());

    // test Chat user credentials
    public static final int USER_ID = 999;
    public static final String TEST_PASSWORD = "<PASSWORD>";
    private final QBUser qbUser;

    // 1-1 Chat properties
    private ChatMessageListener chatMessageListener;

    // Group Chat properties
    private RoomListener roomReceivingListener;
    private QBChatRoom currentQBChatRoom;
    private PacketListener pMessageListener;
    public static final String ROOM_NAME = "temp_room_for_snippet_test";

    public SnippetsChat(final Context context) {
        super(context);
        SmackAndroid.init(context);

        // init test user
        qbUser = new QBUser();
        qbUser.setId(USER_ID);
        qbUser.setPassword(<PASSWORD>);

        initRoomListener();
        initRoomMessageListener();

        snippets.add(loginInChat);
        snippets.add(isLoggedIn);
        snippets.add(logoutFromChat);
        //
        snippets.add(sendPresence);
        snippets.add(startAutoSendPresence);
        //
        snippets.add(sendMessageWithText);
        snippets.add(sendMessageWithMessage);
        //
        snippets.add(createRoom);
        snippets.add(joinRoom);
        snippets.add(sendMessageToRoom);
        snippets.add(getOnlineRoomUsers);
        snippets.add(leaveRoom);
    }


    //
    ///////////////////////////////////////////// Login/Logout /////////////////////////////////////////////
    //
    Snippet loginInChat = new Snippet("login in Chat") {
        @Override
        public void execute() {

            QBChat.getInstance().loginWithUser(qbUser, new LoginListener() {
                @Override
                public void onLoginSuccess() {
                    System.out.println("success when login");
                    QBChat.getInstance().createRoom(ROOM_NAME, qbUser, false, true, roomReceivingListener);

                    // Add Chat message listener
                    initChatMessageListener();
                }

                @Override
                public void onLoginError() {
                    System.out.println("error when login");
                }
            });
        }
    };

    Snippet isLoggedIn = new Snippet("Is logged In") {
        @Override
        public void execute() {
            boolean isLoggedIn = QBChat.getInstance().isLoggedIn();
            System.out.println("isLoggedIn: " + isLoggedIn);
        }
    };

    Snippet logoutFromChat = new Snippet("Logout from Chat") {
        @Override
        public void execute() {
            QBChat.getInstance().logout();
        }
    };


    //
    ///////////////////////////////////////////// Presence /////////////////////////////////////////////
    //
    Snippet sendPresence = new Snippet("send presence") {
        @Override
        public void execute() {
            QBChat.getInstance().sendPresence();
        }
    };

    Snippet startAutoSendPresence = new Snippet("start auto send presence") {
        @Override
        public void execute() {
            int intervalInSecondsBetweenSending = 10;
            QBChat.getInstance().startAutoSendPresence(intervalInSecondsBetweenSending);
        }
    };


    //
    ///////////////////////////////////////////// 1-1 Chat /////////////////////////////////////////////
    //

    Snippet sendMessageWithText = new Snippet("send message with text") {
        @Override
        public void execute() {
            QBChat.getInstance().sendMessage(USER_ID, "<NAME>!");
        }
    };

    Snippet sendMessageWithMessage = new Snippet("send message with Message") {
        @Override
        public void execute() {
            Message message = new Message(QBChatUtils.getChatLoginFull(USER_ID), Message.Type.chat);
            message.setBody("Hey QuickBlox!");
            QBChat.getInstance().sendMessage(USER_ID, message);
        }
    };

    private void initChatMessageListener() {
        // Set 1-1 Chat message listener
        chatMessageListener = new ChatMessageListener() {
            @Override
            public void processMessage(Message message) {

                // get message params
                int userId = QBChatUtils.parseQBUser(message.getFrom());
                String messageBody = message.getBody();
                //
                final String messageText = String.format("Received message from user %s:'%s'", userId, messageBody);

                // Show message
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(context, messageText, Toast.LENGTH_SHORT).show();
                    }
                });

                System.out.println("processMessage >>> " + message.toString());
            }

            @Override
            public boolean accept(Message.Type type) {
                switch (type) {
                    case normal:
                    case chat:
                    case groupchat:
                        return true;
                    default:
                        return false;
                }
            }
        };
        //
        QBChat.getInstance().addChatMessageListener(chatMessageListener);
    }



    Snippet createRoom = new Snippet("create Room") {
        @Override
        public void execute() {
            QBChat.getInstance().createRoom(ROOM_NAME, qbUser, false, true, roomReceivingListener);
        }
    };

    Snippet joinRoom = new Snippet("join Room") {
        @Override
        public void execute() {
            QBChat.getInstance().joinRoom(ROOM_NAME, qbUser, roomReceivingListener);
        }
    };


{'fraction_non_alphanumeric': 0.11965496903568269, 'fraction_numerical': 0.0028015334709525215, 'mean_word_length': 1.9393282773564464, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4738973', 'n_tokens_mistral': 3667, 'n_tokens_neox': 3457, 'n_words': 715}