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

112 lines
3.6 KiB
Java
Raw Normal View History

2014-01-23 20:04:05 -05:00
package de.gultsch.chat.ui;
import de.gultsch.chat.R;
import de.gultsch.chat.entities.Conversation;
2014-01-25 21:27:55 -05:00
import de.gultsch.chat.entities.Message;
import de.gultsch.chat.utils.Beautifier;
2014-01-23 20:04:05 -05:00
import android.app.Fragment;
2014-01-25 21:27:55 -05:00
import android.content.Context;
import android.database.Cursor;
import android.graphics.Typeface;
import android.net.Uri;
2014-01-23 20:04:05 -05:00
import android.os.Bundle;
2014-01-25 21:27:55 -05:00
import android.provider.ContactsContract.Profile;
import android.util.Log;
2014-01-23 20:04:05 -05:00
import android.view.LayoutInflater;
import android.view.View;
2014-01-25 21:27:55 -05:00
import android.view.View.OnClickListener;
2014-01-23 20:04:05 -05:00
import android.view.ViewGroup;
2014-01-25 21:27:55 -05:00
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
2014-01-23 20:04:05 -05:00
public class ConversationFragment extends Fragment {
2014-01-25 21:27:55 -05:00
Conversation conversation;
2014-01-25 21:27:55 -05:00
public void setConversation(Conversation conv) {
this.conversation = conv;
}
2014-01-25 21:27:55 -05:00
2014-01-23 20:04:05 -05:00
@Override
2014-01-25 21:27:55 -05:00
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String[] mProjection = new String[]
{
Profile._ID,
Profile.PHOTO_THUMBNAIL_URI
};
Cursor mProfileCursor = getActivity().getContentResolver().query(
Profile.CONTENT_URI,
mProjection ,
null,
null,
null);
mProfileCursor.moveToFirst();
final Uri profilePicture = Uri.parse(mProfileCursor.getString(1));
Log.d("gultsch","found user profile pic "+profilePicture.toString());
final View view = inflater.inflate(R.layout.fragment_conversation, container,
false);
((ImageButton) view.findViewById(R.id.textSendButton))
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
EditText chatMsg = (EditText) view.findViewById(R.id.textinput);
if (chatMsg.getText().length() < 1) return;
Message message = new Message(conversation,chatMsg.getText().toString(),
Message.ENCRYPTION_NONE);
XmppActivity activity = (XmppActivity) getActivity();
activity.xmppConnectionService.sendMessage(message);
conversation.getMessages().add(message);
chatMsg.setText("");
ListView messagesView = (ListView) view.findViewById(R.id.messages_view);
ArrayAdapter<Message> adapter = (ArrayAdapter<Message>) messagesView.getAdapter();
adapter.notifyDataSetChanged();
messagesView.setSelection(conversation.getMessages().size() -1);
}
});
ListView messagesView = (ListView) view
.findViewById(R.id.messages_view);
messagesView.setAdapter(new ArrayAdapter<Message>(this.getActivity()
.getApplicationContext(), R.layout.message_sent,
this.conversation.getMessages()) {
@Override
public View getView(int position, View view, ViewGroup parent) {
Message item = getItem(position);
if ((item.getStatus() != Message.STATUS_RECIEVED)
|| (item.getStatus() == Message.STATUS_SEND)) {
view = (View) inflater.inflate(R.layout.message_sent, null);
((ImageView) view.findViewById(R.id.message_photo)).setImageURI(profilePicture);
}
((TextView) view.findViewById(R.id.message_body)).setText(item.getBody());
TextView time = (TextView) view.findViewById(R.id.message_time);
if (item.getStatus() == Message.STATUS_UNSEND) {
time.setTypeface(null, Typeface.ITALIC);
} else {
time.setText(Beautifier.readableTimeDifference(item.getTimeSent()));
}
return view;
}
});
return view;
}
public Conversation getConversation() {
return conversation;
}
2014-01-23 20:04:05 -05:00
}