aboutsummaryrefslogtreecommitdiffstats
path: root/src/de/gultsch/chat/ui/ConversationFragment.java
blob: fca5202a74a8b78a0177cdcee6fab545205619e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package de.gultsch.chat.ui;

import de.gultsch.chat.R;
import de.gultsch.chat.entities.Conversation;
import de.gultsch.chat.entities.Message;
import de.gultsch.chat.utils.Beautifier;
import android.app.Fragment;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.Profile;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
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;

public class ConversationFragment extends Fragment {

	Conversation conversation;

	public void setConversation(Conversation conv) {
		this.conversation = conv;
	}

	@Override
	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;
	}
}