Conversations/src/de/gultsch/chat/ui/ConversationFragment.java

233 lines
7.4 KiB
Java
Raw Normal View History

2014-01-24 02:04:05 +01:00
package de.gultsch.chat.ui;
import java.util.ArrayList;
import java.util.List;
2014-01-24 02:04:05 +01:00
import de.gultsch.chat.R;
2014-02-10 03:34:00 +01:00
import de.gultsch.chat.entities.Contact;
import de.gultsch.chat.entities.Conversation;
2014-01-26 03:27:55 +01:00
import de.gultsch.chat.entities.Message;
import de.gultsch.chat.utils.PhoneHelper;
2014-02-03 18:38:47 +01:00
import de.gultsch.chat.utils.UIHelper;
2014-01-24 02:04:05 +01:00
import android.app.Fragment;
import android.content.SharedPreferences;
2014-01-26 03:27:55 +01:00
import android.graphics.Typeface;
import android.net.Uri;
2014-01-24 02:04:05 +01:00
import android.os.Bundle;
import android.preference.PreferenceManager;
2014-01-24 02:04:05 +01:00
import android.view.LayoutInflater;
import android.view.View;
2014-01-26 03:27:55 +01:00
import android.view.View.OnClickListener;
2014-01-24 02:04:05 +01:00
import android.view.ViewGroup;
2014-01-26 03:27:55 +01:00
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
2014-01-24 02:04:05 +01:00
public class ConversationFragment extends Fragment {
protected Conversation conversation;
protected ListView messagesView;
protected LayoutInflater inflater;
protected List<Message> messageList = new ArrayList<Message>();
protected ArrayAdapter<Message> messageListAdapter;
2014-02-10 03:34:00 +01:00
protected Contact contact;
2014-02-13 23:40:08 +01:00
private EditText chatMsg;
private int nextMessageEncryption = Message.ENCRYPTION_NONE;
@Override
public View onCreateView(final LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
2014-01-26 03:27:55 +01:00
this.inflater = inflater;
2014-01-26 03:27:55 +01:00
final View view = inflater.inflate(R.layout.fragment_conversation,
container, false);
2014-02-13 23:40:08 +01:00
chatMsg = (EditText) view.findViewById(R.id.textinput);
2014-01-26 03:27:55 +01:00
((ImageButton) view.findViewById(R.id.textSendButton))
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ConversationActivity activity = (ConversationActivity) getActivity();
if (chatMsg.getText().length() < 1)
return;
Message message = new Message(conversation, chatMsg
2014-02-13 23:40:08 +01:00
.getText().toString(), nextMessageEncryption);
activity.xmppConnectionService.sendMessage(conversation.getAccount(),message);
2014-01-26 03:27:55 +01:00
chatMsg.setText("");
}
});
messagesView = (ListView) view.findViewById(R.id.messages_view);
SharedPreferences sharedPref = PreferenceManager
.getDefaultSharedPreferences(getActivity().getApplicationContext());
boolean showPhoneSelfContactPicture = sharedPref.getBoolean("show_phone_selfcontact_picture",true);
final Uri selfiUri;
if (showPhoneSelfContactPicture) {
selfiUri = PhoneHelper.getSefliUri(getActivity());
} else {
selfiUri = null;
}
messageListAdapter = new ArrayAdapter<Message>(this.getActivity()
.getApplicationContext(), R.layout.message_sent, this.messageList) {
private static final int SENT = 0;
private static final int RECIEVED = 1;
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
if (getItem(position).getStatus() == Message.STATUS_RECIEVED) {
return RECIEVED;
} else {
return SENT;
}
}
2014-01-26 03:27:55 +01:00
@Override
public View getView(int position, View view, ViewGroup parent) {
Message item = getItem(position);
int type = getItemViewType(position);
if (view == null) {
switch (type) {
case SENT:
view = (View) inflater.inflate(R.layout.message_sent,
null);
break;
case RECIEVED:
view = (View) inflater.inflate(
R.layout.message_recieved, null);
break;
}
}
2014-02-01 15:07:20 +01:00
ImageView imageView = (ImageView) view.findViewById(R.id.message_photo);
if (type == RECIEVED) {
if(item.getConversation().getMode()==Conversation.MODE_SINGLE) {
Uri uri = item.getConversation().getProfilePhotoUri();
if (uri!=null) {
imageView.setImageURI(uri);
} else {
imageView.setImageBitmap(UIHelper.getUnknownContactPicture(item.getConversation().getName(), 200));
}
} else if (item.getConversation().getMode()==Conversation.MODE_MULTI) {
if (item.getCounterpart()!=null) {
imageView.setImageBitmap(UIHelper.getUnknownContactPicture(item.getCounterpart(), 200));
} else {
imageView.setImageBitmap(UIHelper.getUnknownContactPicture(item.getConversation().getName(), 200));
}
2014-02-01 15:07:20 +01:00
}
} else {
if (selfiUri!=null) {
imageView.setImageURI(selfiUri);
} else {
imageView.setImageBitmap(UIHelper.getUnknownContactPicture(conversation.getAccount().getJid(),200));
}
2014-01-26 03:27:55 +01:00
}
2014-02-02 17:53:34 +01:00
TextView messageBody = (TextView) view.findViewById(R.id.message_body);
String body = item.getBody();
if (body!=null) {
messageBody.setText(body.trim());
}
2014-01-26 03:27:55 +01:00
TextView time = (TextView) view.findViewById(R.id.message_time);
if (item.getStatus() == Message.STATUS_UNSEND) {
time.setTypeface(null, Typeface.ITALIC);
2014-02-11 23:55:03 +01:00
time.setText("sending\u2026");
2014-01-26 03:27:55 +01:00
} else {
2014-02-11 23:55:03 +01:00
time.setTypeface(null,Typeface.NORMAL);
if ((item.getConversation().getMode()==Conversation.MODE_SINGLE)||(type != RECIEVED)) {
time.setText(UIHelper.readableTimeDifference(item
.getTimeSent()));
2014-02-11 23:55:03 +01:00
} else {
time.setText(item.getCounterpart()+" \u00B7 "+UIHelper.readableTimeDifference(item
.getTimeSent()));
}
2014-01-26 03:27:55 +01:00
}
return view;
}
};
messagesView.setAdapter(messageListAdapter);
2014-01-26 03:27:55 +01:00
return view;
}
@Override
public void onStart() {
super.onStart();
final ConversationActivity activity = (ConversationActivity) getActivity();
if (activity.xmppConnectionServiceBound) {
2014-02-07 02:57:36 +01:00
this.conversation = activity.getSelectedConversation();
updateMessages();
2014-01-28 19:21:54 +01:00
// rendering complete. now go tell activity to close pane
if (!activity.shouldPaneBeOpen()) {
activity.getSlidingPaneLayout().closePane();
activity.getActionBar().setDisplayHomeAsUpEnabled(true);
activity.getActionBar().setTitle(conversation.getName());
activity.invalidateOptionsMenu();
2014-02-10 22:45:59 +01:00
if (!conversation.isRead()) {
conversation.markRead();
activity.updateConversationList();
}
2014-01-28 19:21:54 +01:00
}
}
}
public void onBackendConnected() {
final ConversationActivity activity = (ConversationActivity) getActivity();
2014-02-07 02:57:36 +01:00
this.conversation = activity.getSelectedConversation();
updateMessages();
2014-01-28 19:21:54 +01:00
// rendering complete. now go tell activity to close pane
if (!activity.shouldPaneBeOpen()) {
activity.getSlidingPaneLayout().closePane();
activity.getActionBar().setDisplayHomeAsUpEnabled(true);
activity.getActionBar().setTitle(conversation.getName());
activity.invalidateOptionsMenu();
2014-02-10 22:45:59 +01:00
if (!conversation.isRead()) {
conversation.markRead();
activity.updateConversationList();
}
2014-01-28 19:21:54 +01:00
}
}
public void updateMessages() {
this.messageList.clear();
this.messageList.addAll(this.conversation.getMessages());
this.messageListAdapter.notifyDataSetChanged();
2014-02-13 23:40:08 +01:00
if (messageList.size()>=1) {
nextMessageEncryption = this.conversation.getLatestMessage().getEncryption();
}
getActivity().invalidateOptionsMenu();
switch (nextMessageEncryption) {
case Message.ENCRYPTION_NONE:
chatMsg.setHint("Send plain text message");
break;
case Message.ENCRYPTION_OTR:
chatMsg.setHint("Send OTR encrypted message");
break;
case Message.ENCRYPTION_PGP:
chatMsg.setHint("Send openPGP encryted messeage");
default:
break;
}
int size = this.messageList.size();
if (size >= 1)
messagesView.setSelection(size - 1);
ConversationActivity activity = (ConversationActivity) getActivity();
if (!activity.shouldPaneBeOpen()) {
conversation.markRead();
activity.updateConversationList();
}
}
2014-01-24 02:04:05 +01:00
}