Display unread messages on chat list view

This commit is contained in:
Sylvain Berfini 2012-09-24 12:10:36 +02:00
parent e03a2ee2e8
commit f02038997f
5 changed files with 53 additions and 30 deletions

View file

@ -19,16 +19,18 @@
android:visibility="invisible"
android:src="@drawable/list_delete" />
<ImageView
android:contentDescription="@string/content_description_detail"
android:id="@+id/detail"
android:paddingRight="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
<TextView
android:id="@+id/unreadMessages"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginRight="5dp"
android:layout_toLeftOf="@id/delete"
android:layout_centerVertical="true"
android:background="@drawable/chat_icon_over"
android:adjustViewBounds="true"
android:src="@drawable/list_detail" />
android:gravity="center"
android:textSize="18dp"
android:visibility="gone" />
<TextView
android:id="@+id/draft"
@ -38,7 +40,7 @@
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/text_selected"
android:layout_toLeftOf="@id/detail"
android:layout_toLeftOf="@id/unreadMessages"
android:text="@string/draft"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />

View file

@ -328,7 +328,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
saveImage(item.getGroupId());
break;
case MENU_COPY_TEXT:
copyText(item.getGroupId());
copyTextMessageToClipboard(item.getGroupId());
break;
case MENU_PICTURE_SMALL:
uploadAndSendImage(fileToUploadPath, imageToUpload, ImageSize.SMALL);
@ -431,7 +431,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
}, 100);
}
private void copyText(int id) {
private void copyTextMessageToClipboard(int id) {
String msg = LinphoneActivity.instance().getChatStorage().getTextMessageForId(id);
if (msg != null) {
Compatibility.copyTextToClipboard(getActivity(), msg);

View file

@ -23,6 +23,7 @@ import org.linphone.core.LinphoneAddress;
import org.linphone.core.LinphoneCoreFactory;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
@ -44,6 +45,8 @@ import android.widget.TextView;
* @author Sylvain Berfini
*/
public class ChatListFragment extends Fragment implements OnClickListener, OnItemClickListener {
private Handler mHandler = new Handler();
private LayoutInflater mInflater;
private List<String> mConversations, mDrafts;
private ListView chatList;
@ -92,18 +95,28 @@ public class ChatListFragment extends Fragment implements OnClickListener, OnIte
}
}
public void refresh() {
mHandler.post(new Runnable() {
@Override
public void run() {
mConversations = LinphoneActivity.instance().getChatList();
mDrafts = LinphoneActivity.instance().getDraftChatList();
mConversations.removeAll(mDrafts);
hideAndDisplayMessageIfNoChat();
}
});
}
@Override
public void onResume() {
super.onResume();
if (LinphoneActivity.isInstanciated()) {
LinphoneActivity.instance().selectMenu(FragmentsAvailable.CHATLIST);
LinphoneActivity.instance().updateChatListFragment(this);
}
mConversations = LinphoneActivity.instance().getChatList();
mDrafts = LinphoneActivity.instance().getDraftChatList();
mConversations.removeAll(mDrafts);
hideAndDisplayMessageIfNoChat();
refresh();
}
@Override
@ -216,7 +229,7 @@ public class ChatListFragment extends Fragment implements OnClickListener, OnIte
isDraft = true;
}
view.setTag(contact);
final String fContact = contact;
int unreadMessagesCount = LinphoneActivity.instance().getChatStorage().getUnreadMessageCount(contact);
LinphoneAddress address = LinphoneCoreFactory.instance().createLinphoneAddress(contact);
LinphoneUtils.findUriPictureOfContactAndSetDisplayName(address, view.getContext().getContentResolver());
@ -234,24 +247,20 @@ public class ChatListFragment extends Fragment implements OnClickListener, OnIte
view.findViewById(R.id.draft).setVisibility(View.VISIBLE);
}
ImageView delete, detail;
delete = (ImageView) view.findViewById(R.id.delete);
detail = (ImageView) view.findViewById(R.id.detail);
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);
detail.setVisibility(View.INVISIBLE);
} else {
delete.setVisibility(View.INVISIBLE);
detail.setVisibility(View.VISIBLE);
detail.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (LinphoneActivity.isInstanciated()) {
LinphoneActivity.instance().displayChat(fContact);
}
}
});
}
return view;

View file

@ -235,6 +235,10 @@ public class ChatStorage {
return db.query(TABLE_NAME, null, "read LIKE " + NOT_READ, null, null, null, null).getCount();
}
public int getUnreadMessageCount(String contact) {
return db.query(TABLE_NAME, null, "remoteContact LIKE \"" + contact + "\" AND read LIKE " + NOT_READ, null, null, null, null).getCount();
}
public byte[] getRawImageFromMessage(int id) {
String[] columns = { "image" };
Cursor c = db.query(TABLE_NAME, columns, "id LIKE " + id + "", null, null, null, null);

View file

@ -96,7 +96,7 @@ public class LinphoneActivity extends FragmentActivity implements OnClickListene
private LinearLayout menu, mark;
private RelativeLayout contacts, history, settings, chat, aboutChat, aboutSettings;
private FragmentsAvailable currentFragment, nextFragment;
private Fragment dialerFragment, messageListenerFragment;
private Fragment dialerFragment, messageListenerFragment, messageListFragment;
private SavedState dialerSavedState;
private ChatStorage chatStorage;
private boolean preferLinphoneContacts = false, isAnimationDisabled = false;
@ -268,6 +268,7 @@ public class LinphoneActivity extends FragmentActivity implements OnClickListene
break;
case CHATLIST:
newFragment = new ChatListFragment();
messageListFragment = new Fragment();
break;
}
@ -587,6 +588,10 @@ public class LinphoneActivity extends FragmentActivity implements OnClickListene
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}
public void updateChatListFragment(ChatListFragment fragment) {
messageListFragment = fragment;
}
public void hideMenu(boolean hide) {
menu.setVisibility(hide ? View.GONE : View.VISIBLE);
mark.setVisibility(hide ? View.GONE : View.VISIBLE);
@ -685,6 +690,9 @@ public class LinphoneActivity extends FragmentActivity implements OnClickListene
getChatStorage().markMessageAsRead(id);
} else if (LinphoneService.isReady()) {
displayMissedChats(getChatStorage().getUnreadMessageCount());
if (messageListFragment != null && messageListFragment.isVisible()) {
((ChatListFragment) messageListFragment).refresh();
}
}
LinphoneUtils.findUriPictureOfContactAndSetDisplayName(from, getContentResolver());
LinphoneService.instance().displayMessageNotification(from.asStringUriOnly(), from.getDisplayName(), notificationText);