2014-01-24 01:04:05 +00:00
|
|
|
package de.gultsch.chat.ui;
|
|
|
|
|
2014-01-27 19:40:42 +00:00
|
|
|
import java.util.ArrayList;
|
2014-02-16 15:32:15 +00:00
|
|
|
import java.util.Hashtable;
|
2014-01-27 19:40:42 +00:00
|
|
|
import java.util.List;
|
2014-02-16 15:32:15 +00:00
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import net.java.otr4j.OtrException;
|
|
|
|
import net.java.otr4j.session.SessionStatus;
|
2014-01-27 19:40:42 +00:00
|
|
|
|
2014-01-24 01:04:05 +00:00
|
|
|
import de.gultsch.chat.R;
|
2014-02-10 02:34:00 +00:00
|
|
|
import de.gultsch.chat.entities.Contact;
|
2014-01-24 22:58:51 +00:00
|
|
|
import de.gultsch.chat.entities.Conversation;
|
2014-01-26 02:27:55 +00:00
|
|
|
import de.gultsch.chat.entities.Message;
|
2014-02-16 15:32:15 +00:00
|
|
|
import de.gultsch.chat.services.XmppConnectionService;
|
2014-02-10 14:24:34 +00:00
|
|
|
import de.gultsch.chat.utils.PhoneHelper;
|
2014-02-03 17:38:47 +00:00
|
|
|
import de.gultsch.chat.utils.UIHelper;
|
2014-02-16 15:32:15 +00:00
|
|
|
import android.app.AlertDialog;
|
2014-01-24 01:04:05 +00:00
|
|
|
import android.app.Fragment;
|
2014-02-16 15:32:15 +00:00
|
|
|
import android.content.DialogInterface;
|
2014-02-10 14:24:34 +00:00
|
|
|
import android.content.SharedPreferences;
|
2014-01-26 02:27:55 +00:00
|
|
|
import android.graphics.Typeface;
|
|
|
|
import android.net.Uri;
|
2014-01-24 01:04:05 +00:00
|
|
|
import android.os.Bundle;
|
2014-02-10 14:24:34 +00:00
|
|
|
import android.preference.PreferenceManager;
|
2014-01-24 01:04:05 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
2014-01-26 02:27:55 +00:00
|
|
|
import android.view.View.OnClickListener;
|
2014-01-24 01:04:05 +00:00
|
|
|
import android.view.ViewGroup;
|
2014-01-26 02:27:55 +00:00
|
|
|
import android.widget.ArrayAdapter;
|
|
|
|
import android.widget.EditText;
|
2014-02-16 15:32:15 +00:00
|
|
|
import android.widget.LinearLayout;
|
2014-01-26 02:27:55 +00:00
|
|
|
import android.widget.ListView;
|
|
|
|
import android.widget.ImageButton;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
2014-01-24 01:04:05 +00:00
|
|
|
|
|
|
|
public class ConversationFragment extends Fragment {
|
2014-02-16 15:32:15 +00:00
|
|
|
|
2014-01-27 19:40:42 +00:00
|
|
|
protected Conversation conversation;
|
|
|
|
protected ListView messagesView;
|
|
|
|
protected LayoutInflater inflater;
|
|
|
|
protected List<Message> messageList = new ArrayList<Message>();
|
2014-02-02 15:33:34 +00:00
|
|
|
protected ArrayAdapter<Message> messageListAdapter;
|
2014-02-10 02:34:00 +00:00
|
|
|
protected Contact contact;
|
2014-02-16 15:32:15 +00:00
|
|
|
|
2014-02-13 22:40:08 +00:00
|
|
|
private EditText chatMsg;
|
2014-02-16 15:32:15 +00:00
|
|
|
|
|
|
|
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;
|
2014-02-16 18:30:22 +00:00
|
|
|
Message message = new Message(conversation, chatMsg.getText()
|
2014-02-16 15:32:15 +00:00
|
|
|
.toString(), conversation.nextMessageEncryption);
|
|
|
|
if (conversation.nextMessageEncryption == Message.ENCRYPTION_OTR) {
|
2014-02-16 18:30:22 +00:00
|
|
|
sendOtrMessage(message);
|
2014-02-16 15:32:15 +00:00
|
|
|
} else {
|
2014-02-16 18:30:22 +00:00
|
|
|
sendPlainTextMessage(message);
|
2014-02-16 15:32:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-27 19:40:42 +00:00
|
|
|
@Override
|
|
|
|
public View onCreateView(final LayoutInflater inflater,
|
|
|
|
ViewGroup container, Bundle savedInstanceState) {
|
2014-01-26 02:27:55 +00:00
|
|
|
|
2014-01-27 19:40:42 +00:00
|
|
|
this.inflater = inflater;
|
2014-01-26 02:27:55 +00:00
|
|
|
|
2014-01-27 19:40:42 +00:00
|
|
|
final View view = inflater.inflate(R.layout.fragment_conversation,
|
|
|
|
container, false);
|
2014-02-13 22:40:08 +00:00
|
|
|
chatMsg = (EditText) view.findViewById(R.id.textinput);
|
2014-02-16 15:32:15 +00:00
|
|
|
ImageButton sendButton = (ImageButton) view
|
|
|
|
.findViewById(R.id.textSendButton);
|
|
|
|
sendButton.setOnClickListener(this.sendMsgListener);
|
2014-01-26 02:27:55 +00:00
|
|
|
|
2014-01-27 19:40:42 +00:00
|
|
|
messagesView = (ListView) view.findViewById(R.id.messages_view);
|
2014-02-16 15:32:15 +00:00
|
|
|
|
2014-02-10 14:24:34 +00:00
|
|
|
SharedPreferences sharedPref = PreferenceManager
|
2014-02-16 15:32:15 +00:00
|
|
|
.getDefaultSharedPreferences(getActivity()
|
|
|
|
.getApplicationContext());
|
|
|
|
boolean showPhoneSelfContactPicture = sharedPref.getBoolean(
|
|
|
|
"show_phone_selfcontact_picture", true);
|
|
|
|
|
2014-02-10 14:24:34 +00:00
|
|
|
final Uri selfiUri;
|
|
|
|
if (showPhoneSelfContactPicture) {
|
2014-02-16 15:32:15 +00:00
|
|
|
selfiUri = PhoneHelper.getSefliUri(getActivity());
|
2014-02-10 14:24:34 +00:00
|
|
|
} else {
|
|
|
|
selfiUri = null;
|
|
|
|
}
|
2014-02-16 15:32:15 +00:00
|
|
|
|
2014-02-02 15:33:34 +00:00
|
|
|
messageListAdapter = new ArrayAdapter<Message>(this.getActivity()
|
2014-02-16 15:32:15 +00:00
|
|
|
.getApplicationContext(), R.layout.message_sent,
|
|
|
|
this.messageList) {
|
2014-01-27 19:40:42 +00:00
|
|
|
|
|
|
|
private static final int SENT = 0;
|
|
|
|
private static final int RECIEVED = 1;
|
2014-02-16 15:32:15 +00:00
|
|
|
|
2014-01-27 19:40:42 +00:00
|
|
|
@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 02:27:55 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public View getView(int position, View view, ViewGroup parent) {
|
|
|
|
Message item = getItem(position);
|
2014-01-27 19:40:42 +00:00
|
|
|
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-16 15:32:15 +00:00
|
|
|
ImageView imageView = (ImageView) view
|
|
|
|
.findViewById(R.id.message_photo);
|
2014-01-27 19:40:42 +00:00
|
|
|
if (type == RECIEVED) {
|
2014-02-16 15:32:15 +00:00
|
|
|
if (item.getConversation().getMode() == Conversation.MODE_SINGLE) {
|
2014-02-05 21:33:39 +00:00
|
|
|
Uri uri = item.getConversation().getProfilePhotoUri();
|
2014-02-16 15:32:15 +00:00
|
|
|
if (uri != null) {
|
2014-02-05 21:33:39 +00:00
|
|
|
imageView.setImageURI(uri);
|
|
|
|
} else {
|
2014-02-16 15:32:15 +00:00
|
|
|
imageView.setImageBitmap(UIHelper
|
|
|
|
.getUnknownContactPicture(item
|
|
|
|
.getConversation().getName(), 200));
|
2014-02-05 21:33:39 +00:00
|
|
|
}
|
2014-02-16 15:32:15 +00:00
|
|
|
} else if (item.getConversation().getMode() == Conversation.MODE_MULTI) {
|
|
|
|
if (item.getCounterpart() != null) {
|
|
|
|
imageView.setImageBitmap(UIHelper
|
|
|
|
.getUnknownContactPicture(
|
|
|
|
item.getCounterpart(), 200));
|
2014-02-05 21:33:39 +00:00
|
|
|
} else {
|
2014-02-16 15:32:15 +00:00
|
|
|
imageView.setImageBitmap(UIHelper
|
|
|
|
.getUnknownContactPicture(item
|
|
|
|
.getConversation().getName(), 200));
|
2014-02-05 21:33:39 +00:00
|
|
|
}
|
2014-02-01 14:07:20 +00:00
|
|
|
}
|
2014-01-27 19:40:42 +00:00
|
|
|
} else {
|
2014-02-16 15:32:15 +00:00
|
|
|
if (selfiUri != null) {
|
2014-02-10 14:24:34 +00:00
|
|
|
imageView.setImageURI(selfiUri);
|
|
|
|
} else {
|
2014-02-16 15:32:15 +00:00
|
|
|
imageView.setImageBitmap(UIHelper
|
|
|
|
.getUnknownContactPicture(conversation
|
|
|
|
.getAccount().getJid(), 200));
|
2014-02-10 14:24:34 +00:00
|
|
|
}
|
2014-01-26 02:27:55 +00:00
|
|
|
}
|
2014-02-16 15:32:15 +00:00
|
|
|
TextView messageBody = (TextView) view
|
|
|
|
.findViewById(R.id.message_body);
|
2014-02-02 16:53:34 +00:00
|
|
|
String body = item.getBody();
|
2014-02-16 15:32:15 +00:00
|
|
|
if (body != null) {
|
2014-02-02 16:53:34 +00:00
|
|
|
messageBody.setText(body.trim());
|
|
|
|
}
|
2014-01-26 02:27:55 +00:00
|
|
|
TextView time = (TextView) view.findViewById(R.id.message_time);
|
|
|
|
if (item.getStatus() == Message.STATUS_UNSEND) {
|
|
|
|
time.setTypeface(null, Typeface.ITALIC);
|
2014-02-11 22:55:03 +00:00
|
|
|
time.setText("sending\u2026");
|
2014-01-26 02:27:55 +00:00
|
|
|
} else {
|
2014-02-16 15:32:15 +00:00
|
|
|
time.setTypeface(null, Typeface.NORMAL);
|
|
|
|
if ((item.getConversation().getMode() == Conversation.MODE_SINGLE)
|
|
|
|
|| (type != RECIEVED)) {
|
2014-02-11 22:55:03 +00:00
|
|
|
time.setText(UIHelper.readableTimeDifference(item
|
|
|
|
.getTimeSent()));
|
2014-02-16 15:32:15 +00:00
|
|
|
} else {
|
|
|
|
time.setText(item.getCounterpart()
|
|
|
|
+ " \u00B7 "
|
|
|
|
+ UIHelper.readableTimeDifference(item
|
|
|
|
.getTimeSent()));
|
2014-02-11 22:55:03 +00:00
|
|
|
}
|
2014-01-26 02:27:55 +00:00
|
|
|
}
|
|
|
|
return view;
|
|
|
|
}
|
2014-02-02 15:33:34 +00:00
|
|
|
};
|
|
|
|
messagesView.setAdapter(messageListAdapter);
|
2014-01-26 02:27:55 +00:00
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
2014-01-24 22:58:51 +00:00
|
|
|
|
2014-01-27 19:40:42 +00:00
|
|
|
@Override
|
|
|
|
public void onStart() {
|
|
|
|
super.onStart();
|
|
|
|
final ConversationActivity activity = (ConversationActivity) getActivity();
|
2014-02-16 15:32:15 +00:00
|
|
|
|
2014-01-27 19:40:42 +00:00
|
|
|
if (activity.xmppConnectionServiceBound) {
|
2014-02-07 01:57:36 +00:00
|
|
|
this.conversation = activity.getSelectedConversation();
|
2014-02-05 21:33:39 +00:00
|
|
|
updateMessages();
|
2014-01-28 18:21:54 +00:00
|
|
|
// rendering complete. now go tell activity to close pane
|
|
|
|
if (!activity.shouldPaneBeOpen()) {
|
|
|
|
activity.getSlidingPaneLayout().closePane();
|
2014-01-28 21:21:08 +00:00
|
|
|
activity.getActionBar().setDisplayHomeAsUpEnabled(true);
|
|
|
|
activity.getActionBar().setTitle(conversation.getName());
|
|
|
|
activity.invalidateOptionsMenu();
|
2014-02-10 21:45:59 +00:00
|
|
|
if (!conversation.isRead()) {
|
|
|
|
conversation.markRead();
|
|
|
|
activity.updateConversationList();
|
|
|
|
}
|
2014-01-28 18:21:54 +00:00
|
|
|
}
|
2014-01-27 19:40:42 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-16 15:32:15 +00:00
|
|
|
|
2014-01-27 19:40:42 +00:00
|
|
|
public void onBackendConnected() {
|
|
|
|
final ConversationActivity activity = (ConversationActivity) getActivity();
|
2014-02-07 01:57:36 +00:00
|
|
|
this.conversation = activity.getSelectedConversation();
|
2014-02-05 21:33:39 +00:00
|
|
|
updateMessages();
|
2014-01-28 18:21:54 +00:00
|
|
|
// rendering complete. now go tell activity to close pane
|
|
|
|
if (!activity.shouldPaneBeOpen()) {
|
|
|
|
activity.getSlidingPaneLayout().closePane();
|
2014-01-28 21:21:08 +00:00
|
|
|
activity.getActionBar().setDisplayHomeAsUpEnabled(true);
|
|
|
|
activity.getActionBar().setTitle(conversation.getName());
|
|
|
|
activity.invalidateOptionsMenu();
|
2014-02-10 21:45:59 +00:00
|
|
|
if (!conversation.isRead()) {
|
|
|
|
conversation.markRead();
|
|
|
|
activity.updateConversationList();
|
|
|
|
}
|
2014-01-28 18:21:54 +00:00
|
|
|
}
|
2014-01-24 22:58:51 +00:00
|
|
|
}
|
2014-02-02 15:33:34 +00:00
|
|
|
|
|
|
|
public void updateMessages() {
|
2014-02-16 15:32:15 +00:00
|
|
|
ConversationActivity activity = (ConversationActivity) getActivity();
|
2014-02-02 15:33:34 +00:00
|
|
|
this.messageList.clear();
|
|
|
|
this.messageList.addAll(this.conversation.getMessages());
|
|
|
|
this.messageListAdapter.notifyDataSetChanged();
|
2014-02-16 15:32:15 +00:00
|
|
|
if (messageList.size() >= 1) {
|
|
|
|
int latestEncryption = this.conversation.getLatestMessage()
|
|
|
|
.getEncryption();
|
|
|
|
conversation.nextMessageEncryption = latestEncryption;
|
|
|
|
makeFingerprintWarning(latestEncryption);
|
2014-02-13 22:40:08 +00:00
|
|
|
}
|
|
|
|
getActivity().invalidateOptionsMenu();
|
2014-02-16 15:32:15 +00:00
|
|
|
updateChatMsgHint();
|
2014-02-05 21:33:39 +00:00
|
|
|
int size = this.messageList.size();
|
|
|
|
if (size >= 1)
|
|
|
|
messagesView.setSelection(size - 1);
|
2014-02-10 22:12:11 +00:00
|
|
|
if (!activity.shouldPaneBeOpen()) {
|
|
|
|
conversation.markRead();
|
|
|
|
activity.updateConversationList();
|
|
|
|
}
|
2014-02-02 15:33:34 +00:00
|
|
|
}
|
2014-02-16 15:32:15 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2014-02-16 18:30:22 +00:00
|
|
|
|
|
|
|
protected void sendPlainTextMessage(Message message) {
|
|
|
|
ConversationActivity activity = (ConversationActivity) getActivity();
|
|
|
|
activity.xmppConnectionService.sendMessage(conversation.getAccount(), message,
|
|
|
|
null);
|
|
|
|
chatMsg.setText("");
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void sendOtrMessage(final Message message) {
|
|
|
|
ConversationActivity activity = (ConversationActivity) getActivity();
|
|
|
|
final XmppConnectionService xmppService = activity.xmppConnectionService;
|
|
|
|
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 {
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(
|
|
|
|
getActivity());
|
|
|
|
builder.setTitle("Choose Presence");
|
|
|
|
final String[] presencesArray = new String[presences.size()];
|
|
|
|
presences.keySet().toArray(presencesArray);
|
|
|
|
builder.setItems(presencesArray, new DialogInterface.OnClickListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
xmppService.sendMessage(conversation.getAccount(), message, presencesArray[which]);
|
|
|
|
chatMsg.setText("");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
builder.create().show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-24 01:04:05 +00:00
|
|
|
}
|