private void sendImageMessage(String url, Bitmap bitmap) { LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull(); boolean isNetworkReachable = lc == null ? false : lc.isNetworkReachable(); if (chatRoom != null && url != null && url.length() > 0 && isNetworkReachable) { LinphoneChatMessage chatMessage = chatRoom.createLinphoneChatMessage(""); chatMessage.setExternalBodyUrl(url); chatRoom.sendMessage(chatMessage, this); int newId = -1; if (LinphoneActivity.isInstanciated()) { newId = LinphoneActivity.instance().onMessageSent(sipUri, bitmap, url); } newId = chatMessage.getStorageId(); latestImageMessages.put(newId, url); if (useLinphoneMessageStorage) url = saveImage(bitmap, newId, chatMessage); adapter.refreshHistory(); adapter.notifyDataSetChanged(); scrollToEnd(); } else if (!isNetworkReachable && LinphoneActivity.isInstanciated()) { LinphoneActivity.instance() .displayCustomToast(getString(R.string.error_network_unreachable), Toast.LENGTH_LONG); } }
private void sendTextMessage(String messageToSend) { LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull(); boolean isNetworkReachable = lc == null ? false : lc.isNetworkReachable(); if (chatRoom != null && messageToSend != null && messageToSend.length() > 0 && isNetworkReachable) { LinphoneChatMessage chatMessage = chatRoom.createLinphoneChatMessage(messageToSend); chatRoom.sendMessage(chatMessage, this); if (LinphoneActivity.isInstanciated()) { LinphoneActivity.instance().onMessageSent(sipUri, messageToSend); } adapter.refreshHistory(); adapter.notifyDataSetChanged(); Log.i("Sent message current status: " + chatMessage.getStatus()); scrollToEnd(); } else if (!isNetworkReachable && LinphoneActivity.isInstanciated()) { LinphoneActivity.instance() .displayCustomToast(getString(R.string.error_network_unreachable), Toast.LENGTH_LONG); } }
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; }
@SmallTest @MediumTest @LargeTest public void testEReceiveTextMessage() { goToChat(); solo.clickOnText(iContext.getString(org.linphone.test.R.string.account_test_calls_login)); LinphoneChatRoom chatRoom = LinphoneTestManager.getLc() .getOrCreateChatRoom( "sip:" + iContext.getString(R.string.account_linphone_login) + "@" + iContext.getString(R.string.account_linphone_domain)); LinphoneChatMessage msg = chatRoom.createLinphoneChatMessage(iContext.getString(R.string.chat_test_text_received)); chatRoom.sendMessage( msg, new LinphoneChatMessage.StateListener() { @Override public void onLinphoneChatMessageStateChanged(LinphoneChatMessage msg, State state) { Log.e("Chat message state = " + state.toString()); } }); solo.sleep(1000); Assert.assertTrue(solo.searchText(iContext.getString(R.string.chat_test_text_received))); }
@Override public void isComposingReceived(LinphoneCore lc, LinphoneChatRoom room) { if (chatRoom != null && room != null && chatRoom .getPeerAddress() .asStringUriOnly() .equals(room.getPeerAddress().asStringUriOnly())) { remoteComposing.setVisibility(chatRoom.isRemoteComposing() ? View.VISIBLE : View.GONE); } }
@SuppressLint("UseSparseArrays") @Override public void onResume() { message.addTextChangedListener(textWatcher); addVirtualKeyboardVisiblityListener(); LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull(); if (lc != null) { lc.addListener(mListener); } if (LinphoneActivity.isInstanciated()) { if (getResources().getBoolean(R.bool.show_statusbar_only_on_dialer)) { LinphoneActivity.instance().hideStatusBar(); } LinphoneActivity.instance().updateChatFragment(this); } String draft = getArguments().getString("messageDraft"); message.setText(draft); remoteComposing.setVisibility(chatRoom.isRemoteComposing() ? View.VISIBLE : View.GONE); dispayMessageList(); super.onResume(); }
public void changeDisplayedChat(String newSipUri, String displayName, String pictureUri) { this.sipUri = newSipUri; this.displayName = displayName; this.pictureUri = pictureUri; if (!message.getText().toString().equals("") && LinphoneActivity.isInstanciated()) { ChatStorage chatStorage = LinphoneActivity.instance().getChatStorage(); if (chatStorage.getDraft(sipUri) == null) { chatStorage.saveDraft(sipUri, message.getText().toString()); } else { chatStorage.updateDraft(sipUri, message.getText().toString()); } } else if (LinphoneActivity.isInstanciated()) { LinphoneActivity.instance().getChatStorage().deleteDraft(sipUri); } if (LinphoneActivity.isInstanciated()) { String draft = LinphoneActivity.instance().getChatStorage().getDraft(sipUri); if (draft == null) draft = ""; message.setText(draft); } LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull(); if (lc != null) { chatRoom = lc.getOrCreateChatRoom(sipUri); // Only works if using liblinphone storage chatRoom.markAsRead(); } displayChatHeader(displayName, pictureUri); dispayMessageList(); }
private LinphoneChatMessage getMessageForId(int id) { for (LinphoneChatMessage message : chatRoom.getHistory()) { if (message.getStorageId() == id) { return message; } } return null; }
private String saveImage(Bitmap bm, int id, LinphoneChatMessage message) { try { String path = Environment.getExternalStorageDirectory().toString(); if (!path.endsWith("/")) path += "/"; path += "Pictures/"; File directory = new File(path); directory.mkdirs(); String filename = getString(R.string.picture_name_format).replace("%s", String.valueOf(id)); File file = new File(path, filename); OutputStream fOut = null; fOut = new FileOutputStream(file); bm.compress(Bitmap.CompressFormat.JPEG, 100, fOut); fOut.flush(); fOut.close(); if (useLinphoneMessageStorage) { // Update url path in liblinphone database if (message == null) { LinphoneChatMessage[] history = chatRoom.getHistory(); for (LinphoneChatMessage msg : history) { if (msg.getStorageId() == id) { message = msg; break; } } } message.setExternalBodyUrl(path + filename); chatRoom.updateUrl(message); } MediaStore.Images.Media.insertImage( getActivity().getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName()); return file.getAbsolutePath(); } catch (Exception e) { e.printStackTrace(); } return null; }
private void resendMessage(int id) { LinphoneChatMessage message = getMessageForId(id); if (message == null) return; chatRoom.deleteMessage(getMessageForId(id)); invalidate(); if (message.getText() != null && message.getText().length() > 0) { sendTextMessage(message.getText()); } else { sendImageMessage(message.getAppData()); } }
private void sendTextMessage(String messageToSend) { LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull(); boolean isNetworkReachable = lc == null ? false : lc.isNetworkReachable(); if (chatRoom != null && messageToSend != null && messageToSend.length() > 0 && isNetworkReachable) { LinphoneChatMessage message = chatRoom.createLinphoneChatMessage(messageToSend); message.setListener(this); chatRoom.sendChatMessage(message); if (LinphoneActivity.isInstanciated()) { LinphoneActivity.instance().onMessageSent(sipUri, messageToSend); } invalidate(); Log.i("Sent message current status: " + message.getStatus()); } else if (!isNetworkReachable && LinphoneActivity.isInstanciated()) { LinphoneActivity.instance() .displayCustomToast(getString(R.string.error_network_unreachable), Toast.LENGTH_LONG); } }
@Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case MENU_DELETE_MESSAGE: if (chatRoom != null) { LinphoneChatMessage message = getMessageForId(item.getGroupId()); if (message != null) { chatRoom.deleteMessage(message); invalidate(); } } break; case MENU_COPY_TEXT: copyTextMessageToClipboard(item.getGroupId()); break; case MENU_RESEND_MESSAGE: resendMessage(item.getGroupId()); break; } return true; }
@Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreate(savedInstanceState); instance = this; View view = inflater.inflate(R.layout.chat, container, false); // Retain the fragment across configuration changes setRetainInstance(true); // Retrieve parameter from intent sipUri = getArguments().getString("SipUri"); displayName = getArguments().getString("DisplayName"); pictureUri = getArguments().getString("PictureUri"); // Initialize UI contactName = (TextView) view.findViewById(R.id.contactName); contactPicture = (AvatarWithShadow) view.findViewById(R.id.contactPicture); messagesList = (ListView) view.findViewById(R.id.chatMessageList); textLayout = (RelativeLayout) view.findViewById(R.id.messageLayout); progressBar = (ProgressBar) view.findViewById(R.id.progressbar); topBar = (LinearLayout) view.findViewById(R.id.topbar); sendMessage = (TextView) view.findViewById(R.id.sendMessage); sendMessage.setOnClickListener(this); remoteComposing = (TextView) view.findViewById(R.id.remoteComposing); remoteComposing.setVisibility(View.GONE); uploadLayout = (RelativeLayout) view.findViewById(R.id.uploadLayout); uploadLayout.setVisibility(View.GONE); displayChatHeader(displayName, pictureUri); // Manage multiline message = (EditText) view.findViewById(R.id.message); if (!getResources().getBoolean(R.bool.allow_chat_multiline)) { message.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE); message.setMaxLines(1); } sendImage = (TextView) view.findViewById(R.id.sendPicture); if (!getResources().getBoolean(R.bool.disable_chat_send_file)) { sendImage.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { pickImage(); } }); } else { sendImage.setEnabled(false); } back = (TextView) view.findViewById(R.id.back); if (back != null) { back.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { getActivity().finish(); } }); } cancelUpload = (ImageView) view.findViewById(R.id.cancelUpload); cancelUpload.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { if (currentMessageInFileTransferUploadState != null) { uploadLayout.setVisibility(View.GONE); textLayout.setVisibility(View.VISIBLE); progressBar.setProgress(0); currentMessageInFileTransferUploadState.cancelFileTransfer(); currentMessageInFileTransferUploadState = null; } } }); LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull(); if (lc != null) { chatRoom = lc.getOrCreateChatRoom(sipUri); // Only works if using liblinphone storage chatRoom.markAsRead(); } mListener = new LinphoneCoreListenerBase() { @Override public void messageReceived( LinphoneCore lc, LinphoneChatRoom cr, LinphoneChatMessage message) { LinphoneAddress from = cr.getPeerAddress(); if (from.asStringUriOnly().equals(sipUri)) { invalidate(); } } @Override public void isComposingReceived(LinphoneCore lc, LinphoneChatRoom room) { if (chatRoom != null && room != null && chatRoom .getPeerAddress() .asStringUriOnly() .equals(room.getPeerAddress().asStringUriOnly())) { remoteComposing.setVisibility( chatRoom.isRemoteComposing() ? View.VISIBLE : View.GONE); } } }; textWatcher = new TextWatcher() { public void afterTextChanged(Editable arg0) {} public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {} public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { if (message.getText().toString().equals("")) { sendMessage.setEnabled(false); } else { if (chatRoom != null) chatRoom.compose(); sendMessage.setEnabled(true); } } }; // Force hide keyboard getActivity() .getWindow() .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); return view; }
private void invalidate() { adapter.refreshHistory(); adapter.notifyDataSetChanged(); chatRoom.markAsRead(); }
public void dispayMessageList() { adapter = new ChatMessageAdapter(getActivity(), chatRoom.getHistory()); messagesList.setAdapter(adapter); adapter.notifyDataSetChanged(); }
@Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { sipUri = getArguments().getString("SipUri"); String displayName = getArguments().getString("DisplayName"); String pictureUri = getArguments().getString("PictureUri"); view = inflater.inflate(R.layout.chat, container, false); useLinphoneMessageStorage = getResources().getBoolean(R.bool.use_linphone_chat_storage); contactName = (TextView) view.findViewById(R.id.contactName); contactPicture = (AvatarWithShadow) view.findViewById(R.id.contactPicture); sendMessage = (TextView) view.findViewById(R.id.sendMessage); sendMessage.setOnClickListener(this); remoteComposing = (TextView) view.findViewById(R.id.remoteComposing); remoteComposing.setVisibility(View.GONE); messagesList = (ListView) view.findViewById(R.id.chatMessageList); message = (EditText) view.findViewById(R.id.message); if (!getActivity().getResources().getBoolean(R.bool.allow_chat_multiline)) { message.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE); message.setMaxLines(1); } uploadLayout = (RelativeLayout) view.findViewById(R.id.uploadLayout); uploadLayout.setVisibility(View.GONE); textLayout = (RelativeLayout) view.findViewById(R.id.messageLayout); progressBar = (ProgressBar) view.findViewById(R.id.progressbar); sendImage = (TextView) view.findViewById(R.id.sendPicture); if (!getResources().getBoolean(R.bool.disable_chat_send_file)) { registerForContextMenu(sendImage); sendImage.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { pickImage(); } }); } else { sendImage.setEnabled(false); } cancelUpload = (ImageView) view.findViewById(R.id.cancelUpload); cancelUpload.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { uploadThread.interrupt(); uploadLayout.setVisibility(View.GONE); textLayout.setVisibility(View.VISIBLE); progressBar.setProgress(0); } }); LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull(); if (lc != null) { chatRoom = lc.getOrCreateChatRoom(sipUri); // Only works if using liblinphone storage chatRoom.markAsRead(); } displayChatHeader(displayName, pictureUri); uploadServerUri = LinphonePreferences.instance().getSharingPictureServerUrl(); textWatcher = new TextWatcher() { public void afterTextChanged(Editable arg0) {} public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {} public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { if (message.getText().toString().equals("")) { sendMessage.setEnabled(false); } else { if (chatRoom != null) chatRoom.compose(); sendMessage.setEnabled(true); } } }; // Force hide keyboard if (LinphoneActivity.isInstanciated()) { InputMethodManager imm = (InputMethodManager) LinphoneActivity.instance().getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(), 0); } // Workaround for SGS3 issue if (savedInstanceState != null) { fileToUploadPath = savedInstanceState.getString("fileToUploadPath"); imageToUpload = savedInstanceState.getParcelable("imageToUpload"); } if (fileToUploadPath != null || imageToUpload != null) { sendImage.post( new Runnable() { @Override public void run() { sendImage.showContextMenu(); } }); } return view; }
// Added by me public void createGroupChat(String groupName, String[] groupMembers, int groupSize) { // Intent intent = new Intent(this, ChatActivity.class); // intent.putExtra("GroupName", groupName); // intent.putExtra("GroupMembers", groupMembers); // intent.putExtra("GroupSize", groupSize); // intent.putExtra("ChatType", 1); LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull(); if (lc != null) { LinphoneChatRoom chatRoom = null; chatRoom = lc.getOrCreateGroupChatRoom(groupName, groupMembers, groupSize, 0, 0); if (chatRoom != null) { LinphoneAddress la = chatRoom.getPeerAddress(); String sipUri = la.asStringUriOnly(); String displayName = la.getDisplayName(); String addrStr = la.asString(); // intent.putExtra("SipUri", sipUri); // intent.putExtra("DisplayName", displayName); LinphoneProxyConfig pc = lc.getDefaultProxyConfig(); String identity = pc.getIdentity(); String id = ""; try { la = LinphoneCoreFactory.instance().createLinphoneAddress(identity); id = la.getUserName(); } catch (LinphoneCoreException e) { Log.e("Cannot display chat", e); // return; } String message = id + " created group " + groupName; LinphoneChatMessage msg = chatRoom.createLinphoneGroupChatMessage(message); chatRoom.sendGroupChatMessage(msg); // chatRoom.sendGroupMessage(message); onMessageSent(sipUri, message); goToChatList(); /*String toDisplay = "Display Name: " + displayName; toDisplay += "\nSIP URI: " + sipUri; toDisplay += "\nGroup Name: " + chatRoom.getGroupName(); toDisplay += "\nGroup Address: " + addrStr; displayCustomToast(toDisplay, Toast.LENGTH_SHORT);*/ } // LinphoneProxyConfig pc = lc.getDefaultProxyConfig(); // String identity = pc.getIdentity(); // displayCustomToast("I am: " + identity, Toast.LENGTH_SHORT); } /*startOrientationSensor(); startActivityForResult(intent, CHAT_ACTIVITY); LinphoneService.instance().resetMessageNotifCount(); LinphoneService.instance().removeMessageNotification(); displayMissedChats(getChatStorage().getUnreadMessageCount());*/ }
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; }
public void dispayMessageList() { adapter = new ChatMessageAdapter(this.getActivity(), chatRoom.getHistory()); messagesList.setAdapter(adapter); }
private void scrollToEnd() { messagesList.smoothScrollToPosition(messagesList.getCount()); chatRoom.markAsRead(); }