private boolean importAndroidStoredMessagedIntoLibLinphoneStorage() { Log.w("Importing previous messages into new database..."); try { ChatStorage db = LinphoneActivity.instance().getChatStorage(); List<String> conversations = db.getChatList(); for (int j = conversations.size() - 1; j >= 0; j--) { String correspondent = conversations.get(j); LinphoneChatRoom room = LinphoneManager.getLc().getOrCreateChatRoom(correspondent); for (ChatMessage message : db.getMessages(correspondent)) { LinphoneChatMessage msg = room.createLinphoneChatMessage( message.getMessage(), message.getUrl(), message.getStatus(), Long.parseLong(message.getTimestamp()), true, message.isIncoming()); if (message.getImage() != null) { String path = saveImageAsFile(message.getId(), message.getImage()); if (path != null) msg.setExternalBodyUrl(path); } msg.store(); } db.removeDiscussion(correspondent); } return true; } catch (Exception e) { e.printStackTrace(); } return false; }
private static Notification buildNotification( Context context, boolean playSound, ChatMessage chatMsg) { Resources res = context.getResources(); String message; String title = res.getString(R.string.app_name); String tapToOpen = res.getString(R.string.click_to_open); boolean hasNewMessages = msNotifications.size() > 0; // Build a different message depending on wether we have notifications if (chatMsg != null) { message = chatMsg.getUser() + ": " + chatMsg.getMessage(); } else if (!hasNewMessages) { message = tapToOpen; } else { message = String.format( res.getString(R.string.you_have_pending_notifications), msNotifications.size()) + "\n"; for (int i = msNotifications.size() - 1; i >= 0; --i) { ChatMessage msg = msNotifications.get(i); message = message + "<" + msg.getUser() + "> " + msg.getMessage() + "\n"; } } // Make it Android 4 stylish NotificationCompat.InboxStyle bigTextStyle = new NotificationCompat.InboxStyle(); bigTextStyle.setBigContentTitle(title); if (chatMsg != null) { String msg = "<b>" + chatMsg.getUser() + "</b> " + chatMsg.getMessage(); bigTextStyle.addLine(Html.fromHtml(msg)); } else if (!hasNewMessages) { bigTextStyle.addLine(tapToOpen); } else { bigTextStyle.addLine( String.format( res.getString(R.string.you_have_pending_notifications), msNotifications.size())); for (int i = msNotifications.size() - 1; i >= 0; --i) { ChatMessage msg = msNotifications.get(i); bigTextStyle.addLine(Html.fromHtml("<b>" + msg.getUser() + "</b> " + msg.getMessage())); } } boolean useLights = hasNewMessages || chatMsg != null; int icon = (hasNewMessages || chatMsg != null) ? R.drawable.ic_new_messages : R.drawable.ic_launcher; return buildNotification(context, playSound, useLights, icon, message, bigTextStyle); }
// what will run forever public void run() { // to loop until LOGOUT boolean keepGoing = true; while (keepGoing) { // read a String (which is an object) try { cm = (ChatMessage) sInput.readObject(); } catch (IOException e) { display(username + " Exception reading Streams: " + e); break; } catch (ClassNotFoundException e2) { break; } // the messaage part of the ChatMessage String message = cm.getMessage(); // Switch on the type of message receive switch (cm.getType()) { case ChatMessage.MESSAGE: broadcast(username + ": " + message); break; case ChatMessage.LOGOUT: display(username + " disconnected with a LOGOUT message."); keepGoing = false; break; case ChatMessage.WHOISIN: writeMsg("List of the users connected at " + sdf.format(new Date()) + "\n"); // scan al the users connected for (int i = 0; i < al.size(); ++i) { ClientThread ct = al.get(i); writeMsg((i + 1) + ") " + ct.username + " since " + ct.date); } break; case ChatMessage.TO: broadcastOnlyOne(username + ": " + message, cm); break; case ChatMessage.SENDFILE: sendFile(username + ": " + message, cm); break; case ChatMessage.PROCEED: proceedSendFile(); break; case ChatMessage.SENDMEDIAFILE: String fileList = getFileList(); writeMsg(new ChatMessage(ChatMessage.SENDMEDIAFILE, fileList)); break; case ChatMessage.ESTABLISHCONNECTIONANDPLAY: sendMediaFile(username + ": " + message, cm); break; } } // remove myself from the arrayList containing the list of the // connected Clients remove(id); close(); }
@Override public void update(ChatMessage message) throws IOException { System.out.println("Inside update() " + number + " received message " + message.getMessage()); System.out.println("sending message..."); // ChatClient client = (ChatClient) message.sender; // char chars[] = (message.getSender().getSenderName() + ": " + // message.getMessage()).toCharArray(); char chars[] = getMessageForSend(message); // TODO convert message to function // char chars[] = message.getMessage().toCharArray(); for (char c : chars) { // outputStream.write((int)c); // System.out.println("Sending " + c); outputStream.write((int) c); } outputStream.flush(); System.out.println("Sent: " + message.getMessage()); }
/* * Write a String to the Client output stream */ private boolean writeMsg(ChatMessage cm) { // if Client is still connected send the message to it if (!socket.isConnected()) { close(); return false; } // write the message to the stream try { boolean sendChatMessage = cm.getType() == ChatMessage.ESTABLISHCONNECTION; boolean sendMediaFileMessage = cm.getType() == ChatMessage.ESTABLISHCONNECTIONANDPLAY; if (sendChatMessage || sendMediaFileMessage) sOutput.writeObject(cm); else sOutput.writeObject(cm.getMessage()); } // if an error occurs, do not abort just inform the user catch (IOException e) { display("Error sending message to " + username); display(e.toString()); } return true; }
// what will run forever public void run() { // to loop until LOGOUT boolean keepGoing = true; while (keepGoing) { // read a String (which is an object) try { cm = (ChatMessage) sInput.readObject(); } catch (IOException e) { display(username + " Exception reading Streams: " + e); break; } catch (ClassNotFoundException e2) { break; } // the messaage part of the ChatMessage String message = cm.getMessage(); // Switch on the type of message receive switch (cm.getType()) { case ChatMessage.MESSAGE: broadcast(username + ": " + message); break; case ChatMessage.LOGOUT: display(username + " LOGOUT dari chat."); keepGoing = false; break; case ChatMessage.WHOISIN: writeMsg("Daftar Client yang terkoneksi pada " + sdf.format(new Date()) + "\n"); // scan al the users connected for (int i = 0; i < al.size(); ++i) { ClientThread ct = al.get(i); writeMsg((i + 1) + ") " + ct.username + " sejak " + ct.date); } break; } } // remove myself from the arrayList containing the list of the // connected Clients remove(id); close(); }
public View getView(int position, View convertView, ViewGroup parent) { View view = null; if (convertView != null) { view = convertView; } else { view = mInflater.inflate(R.layout.chatlist_cell, parent, false); } String contact; boolean isDraft = false; if (position >= mDrafts.size()) { contact = mConversations.get(position - mDrafts.size()); } else { contact = mDrafts.get(position); isDraft = true; } view.setTag(contact); int unreadMessagesCount = LinphoneActivity.instance().getChatStorage().getUnreadMessageCount(contact); LinphoneAddress address; try { address = LinphoneCoreFactory.instance().createLinphoneAddress(contact); } catch (LinphoneCoreException e) { Log.e("Chat view cannot parse address", e); return view; } LinphoneUtils.findUriPictureOfContactAndSetDisplayName( address, view.getContext().getContentResolver()); String message = ""; if (useNativeAPI) { LinphoneChatRoom chatRoom = LinphoneManager.getLc().getOrCreateChatRoom(contact); LinphoneChatMessage[] history = chatRoom.getHistory(20); if (history != null && history.length > 0) { for (int i = history.length - 1; i >= 0; i--) { LinphoneChatMessage msg = history[i]; if (msg.getText() != null && msg.getText().length() > 0) { message = msg.getText(); break; } } } } else { List<ChatMessage> messages = LinphoneActivity.instance().getChatMessages(contact); if (messages != null && messages.size() > 0) { int iterator = messages.size() - 1; ChatMessage lastMessage = null; while (iterator >= 0) { lastMessage = messages.get(iterator); if (lastMessage.getMessage() == null) { iterator--; } else { iterator = -1; } } message = (lastMessage == null || lastMessage.getMessage() == null) ? "" : lastMessage.getMessage(); } } TextView lastMessageView = (TextView) view.findViewById(R.id.lastMessage); lastMessageView.setText(message); TextView sipUri = (TextView) view.findViewById(R.id.sipUri); sipUri.setSelected(true); // For animation if (getResources().getBoolean(R.bool.only_display_username_if_unknown) && address.getDisplayName() != null && LinphoneUtils.isSipAddress(address.getDisplayName())) { address.setDisplayName(LinphoneUtils.getUsernameFromAddress(address.getDisplayName())); } else if (getResources().getBoolean(R.bool.only_display_username_if_unknown) && LinphoneUtils.isSipAddress(contact)) { contact = LinphoneUtils.getUsernameFromAddress(contact); } sipUri.setText(address.getDisplayName() == null ? contact : address.getDisplayName()); if (isDraft) { view.findViewById(R.id.draft).setVisibility(View.VISIBLE); } ImageView delete = (ImageView) view.findViewById(R.id.delete); TextView unreadMessages = (TextView) view.findViewById(R.id.unreadMessages); if (unreadMessagesCount > 0) { unreadMessages.setVisibility(View.VISIBLE); unreadMessages.setText(String.valueOf(unreadMessagesCount)); } else { unreadMessages.setVisibility(View.GONE); } if (isEditMode) { delete.setVisibility(View.VISIBLE); } else { delete.setVisibility(View.INVISIBLE); } return view; }
/** * Composes messages for send (using message body, sender name etc.) * * @param message * @return */ private char[] getMessageForSend(ChatMessage message) { char chars[] = (message.getSender().getSenderName() + ": " + message.getMessage()).toCharArray(); return chars; }
public RSCPacket getPacket() { List<Bubble> bubblesNeedingDisplayed = playerToUpdate.getBubblesNeedingDisplayed(); List<ChatMessage> chatMessagesNeedingDisplayed = playerToUpdate.getChatMessagesNeedingDisplayed(); List<Player> playersNeedingHitsUpdate = playerToUpdate.getPlayersRequiringHitsUpdate(); List<Projectile> projectilesNeedingDisplayed = playerToUpdate.getProjectilesNeedingDisplayed(); List<Player> playersNeedingAppearanceUpdate = playerToUpdate.getPlayersRequiringAppearanceUpdate(); int updateSize = bubblesNeedingDisplayed.size() + chatMessagesNeedingDisplayed.size() + playersNeedingHitsUpdate.size() + projectilesNeedingDisplayed.size() + playersNeedingAppearanceUpdate.size(); if (updateSize > 0) { RSCPacketBuilder updates = new RSCPacketBuilder(); updates.setID(53); updates.addShort(updateSize); for (Bubble b : bubblesNeedingDisplayed) { // 0 - Draws item over players head updates.addShort(b.getOwner().getIndex()); updates.addByte((byte) 0); updates.addShort(b.getID()); } for (ChatMessage cm : chatMessagesNeedingDisplayed) { // 1/6 - Player talking updates.addShort(cm.getSender().getIndex()); updates.addByte((byte) (cm.getRecipient() == null ? 1 : 6)); System.out.println(cm.getRecipient() == null); updates.addByte((byte) cm.getLength()); updates.addBytes(cm.getMessage()); } for (Player p : playersNeedingHitsUpdate) { // 2 - Hitpoints update for players, draws health bar etc too updates.addShort(p.getIndex()); updates.addByte((byte) 2); updates.addByte((byte) p.getLastDamage()); updates.addByte((byte) p.getCurStat(3)); updates.addByte((byte) p.getMaxStat(3)); } for (Projectile p : projectilesNeedingDisplayed) { // 3/4 - Draws a projectile Entity victim = p.getVictim(); if (victim instanceof Npc) { updates.addShort(p.getCaster().getIndex()); updates.addByte((byte) 3); updates.addShort(p.getType()); updates.addShort(((Npc) victim).getIndex()); } else if (victim instanceof Player) { updates.addShort(p.getCaster().getIndex()); updates.addByte((byte) 4); updates.addShort(p.getType()); updates.addShort(((Player) victim).getIndex()); } } for (Player p : playersNeedingAppearanceUpdate) { // 5 - Updates players appearance, clothes, skull, // combat etc. PlayerAppearance appearance = p.getPlayerAppearance(); updates.addShort(p.getIndex()); updates.addByte((byte) 5); updates.addShort(p.getAppearanceID()); updates.addLong(p.getUsernameHash()); updates.addLong(p.getClanNameHash()); updates.addByte((byte) p.getWornItems().length); for (int i : p.getWornItems()) { updates.addByte((byte) i); } updates.addByte(appearance.getHairColour()); updates.addByte(appearance.getTopColour()); updates.addByte(appearance.getTrouserColour()); updates.addByte(appearance.getSkinColour()); updates.addByte((byte) p.getCombatLevel()); updates.addByte((byte) (p.isSkulled() ? 1 : 0)); updates.addByte( (byte) (p.isAdmin() ? 3 : (p.isMod() ? 2 : (p.isPMod() ? 1 : (p.isEvent() ? 4 : (p.isDeveloper() ? 5 : 0)))))); updates.addLong(DataConversions.usernameToHash((p.flag == null ? "--" : p.flag))); } return updates.toPacket(); } return null; }