aboutsummaryrefslogtreecommitdiffstats
path: root/src/de/gultsch/chat/ui/ConversationFragment.java
blob: 1f125ab07a05038914101d15afa1667c5669424b (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package de.gultsch.chat.ui;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Set;

import net.java.otr4j.OtrException;
import net.java.otr4j.session.SessionStatus;

import de.gultsch.chat.R;
import de.gultsch.chat.entities.Contact;
import de.gultsch.chat.entities.Conversation;
import de.gultsch.chat.entities.Message;
import de.gultsch.chat.services.XmppConnectionService;
import de.gultsch.chat.utils.PhoneHelper;
import de.gultsch.chat.utils.UIHelper;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
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.LinearLayout;
import android.widget.ListView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

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;
	protected Contact contact;

	private EditText chatMsg;

	private OnClickListener sendMsgListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			ConversationActivity activity = (ConversationActivity) getActivity();
			final XmppConnectionService xmppService = activity.xmppConnectionService;
			if (chatMsg.getText().length() < 1)
				return;
			final Message message = new Message(conversation, chatMsg.getText()
					.toString(), conversation.nextMessageEncryption);
			if (conversation.nextMessageEncryption == Message.ENCRYPTION_OTR) {
				if (conversation.hasValidOtrSession()) {
					activity.xmppConnectionService.sendMessage(
							conversation.getAccount(), message, null);
					chatMsg.setText("");
				} else {
					Hashtable<String, Integer> presences = conversation
							.getContact().getPresences();
					if (presences.size() == 0) {
						AlertDialog.Builder builder = new AlertDialog.Builder(
								getActivity());
						builder.setTitle("Contact is offline");
						builder.setIconAttribute(android.R.attr.alertDialogIcon);
						builder.setMessage("Sending OTR encrypted messages to an offline contact is impossible.");
						builder.setPositiveButton("Send plain text",
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										conversation.nextMessageEncryption = Message.ENCRYPTION_NONE;
										message.setEncryption(Message.ENCRYPTION_NONE);
										xmppService.sendMessage(
												conversation.getAccount(),
												message, null);
										chatMsg.setText("");
									}
								});
						builder.setNegativeButton("Cancel", null);
						builder.create().show();
					} else if (presences.size() == 1) {
						xmppService.sendMessage(conversation.getAccount(),
								message,
								(String) presences.keySet().toArray()[0]);
						chatMsg.setText("");
					}
				}
			} else {
				xmppService.sendMessage(conversation.getAccount(), message,
						null);
				chatMsg.setText("");
			}
		}
	};

	public void updateChatMsgHint() {
		if (conversation.getMode() == Conversation.MODE_MULTI) {
			chatMsg.setHint("Send message to conference");
		} else {
			switch (conversation.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;
			}
		}
	}

	@Override
	public View onCreateView(final LayoutInflater inflater,
			ViewGroup container, Bundle savedInstanceState) {

		this.inflater = inflater;

		final View view = inflater.inflate(R.layout.fragment_conversation,
				container, false);
		chatMsg = (EditText) view.findViewById(R.id.textinput);
		ImageButton sendButton = (ImageButton) view
				.findViewById(R.id.textSendButton);
		sendButton.setOnClickListener(this.sendMsgListener);

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

			@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;
					}
				}
				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));
						}
					}
				} else {
					if (selfiUri != null) {
						imageView.setImageURI(selfiUri);
					} else {
						imageView.setImageBitmap(UIHelper
								.getUnknownContactPicture(conversation
										.getAccount().getJid(), 200));
					}
				}
				TextView messageBody = (TextView) view
						.findViewById(R.id.message_body);
				String body = item.getBody();
				if (body != null) {
					messageBody.setText(body.trim());
				}
				TextView time = (TextView) view.findViewById(R.id.message_time);
				if (item.getStatus() == Message.STATUS_UNSEND) {
					time.setTypeface(null, Typeface.ITALIC);
					time.setText("sending\u2026");
				} else {
					time.setTypeface(null, Typeface.NORMAL);
					if ((item.getConversation().getMode() == Conversation.MODE_SINGLE)
							|| (type != RECIEVED)) {
						time.setText(UIHelper.readableTimeDifference(item
								.getTimeSent()));
					} else {
						time.setText(item.getCounterpart()
								+ " \u00B7 "
								+ UIHelper.readableTimeDifference(item
										.getTimeSent()));
					}
				}
				return view;
			}
		};
		messagesView.setAdapter(messageListAdapter);

		return view;
	}

	@Override
	public void onStart() {
		super.onStart();
		final ConversationActivity activity = (ConversationActivity) getActivity();

		if (activity.xmppConnectionServiceBound) {
			this.conversation = activity.getSelectedConversation();
			updateMessages();
			// 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();
				if (!conversation.isRead()) {
					conversation.markRead();
					activity.updateConversationList();
				}
			}
		}
	}

	public void onBackendConnected() {
		final ConversationActivity activity = (ConversationActivity) getActivity();
		this.conversation = activity.getSelectedConversation();
		updateMessages();
		// 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();
			if (!conversation.isRead()) {
				conversation.markRead();
				activity.updateConversationList();
			}
		}
	}

	public void updateMessages() {
		ConversationActivity activity = (ConversationActivity) getActivity();
		this.messageList.clear();
		this.messageList.addAll(this.conversation.getMessages());
		this.messageListAdapter.notifyDataSetChanged();
		if (messageList.size() >= 1) {
			int latestEncryption = this.conversation.getLatestMessage()
					.getEncryption();
			conversation.nextMessageEncryption = latestEncryption;
			makeFingerprintWarning(latestEncryption);
		}
		getActivity().invalidateOptionsMenu();
		updateChatMsgHint();
		int size = this.messageList.size();
		if (size >= 1)
			messagesView.setSelection(size - 1);
		if (!activity.shouldPaneBeOpen()) {
			conversation.markRead();
			activity.updateConversationList();
		}
	}

	protected void makeFingerprintWarning(int latestEncryption) {
		final LinearLayout fingerprintWarning = (LinearLayout) getView()
				.findViewById(R.id.new_fingerprint);
		Set<String> knownFingerprints = conversation.getContact()
				.getOtrFingerprints();
		if ((latestEncryption == Message.ENCRYPTION_OTR)
				&& (conversation.hasValidOtrSession()
						&& (conversation.getOtrSession().getSessionStatus() == SessionStatus.ENCRYPTED) && (!knownFingerprints
							.contains(conversation.getOtrFingerprint())))) {
			fingerprintWarning.setVisibility(View.VISIBLE);
			TextView fingerprint = (TextView) getView().findViewById(
					R.id.otr_fingerprint);
			fingerprint.setText(conversation.getOtrFingerprint());
			fingerprintWarning.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					AlertDialog dialog = UIHelper.getVerifyFingerprintDialog((ConversationActivity) getActivity(),conversation,fingerprintWarning);
					dialog.show();
				}
			});
		} else {
			fingerprintWarning.setVisibility(View.GONE);
		}
	}
}