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;
|
|
|
|
import java.util.List;
|
|
|
|
|
2014-01-24 01:04:05 +00:00
|
|
|
import de.gultsch.chat.R;
|
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-03 17:38:47 +00:00
|
|
|
import de.gultsch.chat.utils.UIHelper;
|
2014-01-24 01:04:05 +00:00
|
|
|
import android.app.Fragment;
|
2014-01-26 02:27:55 +00:00
|
|
|
import android.database.Cursor;
|
|
|
|
import android.graphics.Typeface;
|
|
|
|
import android.net.Uri;
|
2014-01-24 01:04:05 +00:00
|
|
|
import android.os.Bundle;
|
2014-01-26 02:27:55 +00:00
|
|
|
import android.provider.ContactsContract.Profile;
|
|
|
|
import android.util.Log;
|
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;
|
|
|
|
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-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-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-01-26 02:27:55 +00:00
|
|
|
((ImageButton) view.findViewById(R.id.textSendButton))
|
|
|
|
.setOnClickListener(new OnClickListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2014-01-27 19:40:42 +00:00
|
|
|
ConversationActivity activity = (ConversationActivity) getActivity();
|
|
|
|
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);
|
2014-02-02 15:05:15 +00:00
|
|
|
activity.xmppConnectionService.sendMessage(conversation.getAccount(),message);
|
2014-01-26 02:27:55 +00:00
|
|
|
chatMsg.setText("");
|
|
|
|
|
2014-02-08 23:47:11 +00:00
|
|
|
if (conversation.getMode()==Conversation.MODE_SINGLE) {
|
2014-02-09 00:00:23 +00:00
|
|
|
conversation.getMessages().add(message);
|
2014-02-08 23:47:11 +00:00
|
|
|
messageList.add(message);
|
|
|
|
}
|
2014-01-27 19:40:42 +00:00
|
|
|
|
|
|
|
activity.updateConversationList();
|
2014-01-26 02:27:55 +00:00
|
|
|
|
2014-01-27 19:40:42 +00:00
|
|
|
messagesView.setSelection(messageList.size() - 1);
|
2014-01-26 02:27:55 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-01-27 19:40:42 +00:00
|
|
|
messagesView = (ListView) view.findViewById(R.id.messages_view);
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
2014-02-02 15:33:34 +00:00
|
|
|
messageListAdapter = new ArrayAdapter<Message>(this.getActivity()
|
2014-01-27 19:40:42 +00:00
|
|
|
.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 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-01 14:07:20 +00:00
|
|
|
ImageView imageView = (ImageView) view.findViewById(R.id.message_photo);
|
2014-01-27 19:40:42 +00:00
|
|
|
if (type == RECIEVED) {
|
2014-02-05 21:33:39 +00:00
|
|
|
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 14:07:20 +00:00
|
|
|
}
|
2014-01-27 19:40:42 +00:00
|
|
|
} else {
|
2014-02-01 14:07:20 +00:00
|
|
|
imageView.setImageURI(profilePicture);
|
2014-01-26 02:27:55 +00:00
|
|
|
}
|
2014-02-02 16:53:34 +00:00
|
|
|
TextView messageBody = (TextView) view.findViewById(R.id.message_body);
|
|
|
|
String body = item.getBody();
|
|
|
|
if (body!=null) {
|
|
|
|
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);
|
|
|
|
} else {
|
2014-02-03 17:38:47 +00:00
|
|
|
time.setText(UIHelper.readableTimeDifference(item
|
2014-01-27 19:40:42 +00:00
|
|
|
.getTimeSent()));
|
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();
|
|
|
|
|
|
|
|
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-01-28 18:21:54 +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-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() {
|
|
|
|
this.messageList.clear();
|
|
|
|
this.messageList.addAll(this.conversation.getMessages());
|
|
|
|
this.messageListAdapter.notifyDataSetChanged();
|
2014-02-05 21:33:39 +00:00
|
|
|
int size = this.messageList.size();
|
|
|
|
if (size >= 1)
|
|
|
|
messagesView.setSelection(size - 1);
|
2014-02-02 15:33:34 +00:00
|
|
|
}
|
2014-01-24 01:04:05 +00:00
|
|
|
}
|