Search in sources :

Example 1 with ChatList

use of com.romainpiel.model.ChatList in project meatspace-android by RomainPiel.

the class ChatService method onCreate.

@Override
public void onCreate() {
    super.onCreate();
    cacheManager = new CacheManager(this, false);
    apiManager = ApiManager.get();
    busManager = BusManager.get();
    handler = new Handler();
    ioState = IOState.IDLE;
    chatList = new ChatList();
    mutedUsers = new HashSet<String>();
    BackgroundExecutor.execute(new Runnable() {

        @Override
        public void run() {
            final HashSet<String> cachedMutedUsers = (HashSet<String>) cacheManager.readFile(Constants.CACHE_MUTED_USERS, null);
            if (cachedMutedUsers != null) {
                handler.post(new Runnable() {

                    @Override
                    public void run() {
                        mutedUsers = cachedMutedUsers;
                        if (chatList.get() != null && !chatList.get().isEmpty()) {
                            syncChatList(chatList);
                            post();
                        }
                    }
                });
            }
        }
    }, API_GET_CACHED_MUTED_REQ_ID, null);
    busManager.getChatBus().register(this);
    busManager.getUiBus().register(this);
    closeChatReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            ioState = IOState.DISCONNECTED;
            post();
            context.stopService(new Intent(context, ChatService.class));
        }
    };
    registerReceiver(closeChatReceiver, new IntentFilter(Constants.FILTER_CHAT_CLOSE));
}
Also used : Context(android.content.Context) IntentFilter(android.content.IntentFilter) ChatList(com.romainpiel.model.ChatList) CacheManager(com.romainpiel.lib.utils.CacheManager) Handler(android.os.Handler) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) BroadcastReceiver(android.content.BroadcastReceiver) HashSet(java.util.HashSet)

Example 2 with ChatList

use of com.romainpiel.model.ChatList in project meatspace-android by RomainPiel.

the class ChatService method onEvent.

/**
 * Socket event callback
 *
 * @param dataString  raw data of the message
 * @param acknowledge socket channel details
 */
@Override
public void onEvent(final String dataString, Acknowledge acknowledge) {
    final Gson jsonParser = apiManager.getJsonParser();
    BackgroundExecutor.execute(new Runnable() {

        @Override
        public void run() {
            try {
                final SocketChatEvent event = jsonParser.fromJson(dataString, SocketChatEvent.class);
                final String name = event.getName();
                final List<Chat> chats = event.getChats();
                if (!ApiManager.EVENT_MESSAGE.equals(name) || chats == null)
                    return;
                handler.post(new Runnable() {

                    @Override
                    public void run() {
                        ChatList newChats = new ChatList(chats);
                        syncChatList(newChats);
                        saveAndPost(newChats);
                        int newMissedMessageCount = 0;
                        for (Chat chat : chats) {
                            // don't count if it's from me
                            if (!chat.getValue().isFromMe()) {
                                newMissedMessageCount++;
                            }
                        }
                        if (appInBackground && newMissedMessageCount > 0) {
                            missedMessageCount += newMissedMessageCount;
                            showForeground();
                        }
                    }
                });
            } catch (Exception e) {
                Debug.out(e);
            }
        }
    });
}
Also used : ChatList(com.romainpiel.model.ChatList) Chat(com.romainpiel.model.Chat) Gson(com.google.gson.Gson) ChatList(com.romainpiel.model.ChatList) List(java.util.List) SocketChatEvent(com.romainpiel.model.SocketChatEvent)

Aggregations

ChatList (com.romainpiel.model.ChatList)2 PendingIntent (android.app.PendingIntent)1 BroadcastReceiver (android.content.BroadcastReceiver)1 Context (android.content.Context)1 Intent (android.content.Intent)1 IntentFilter (android.content.IntentFilter)1 Handler (android.os.Handler)1 Gson (com.google.gson.Gson)1 CacheManager (com.romainpiel.lib.utils.CacheManager)1 Chat (com.romainpiel.model.Chat)1 SocketChatEvent (com.romainpiel.model.SocketChatEvent)1 HashSet (java.util.HashSet)1 List (java.util.List)1