conversation archiveable. new conversation will find or restart old conversations

This commit is contained in:
Daniel Gultsch 2014-01-27 20:40:42 +01:00
parent 898b0ca8c4
commit 4f902d8210
14 changed files with 403 additions and 117 deletions

View File

@ -76,7 +76,8 @@ public final class R {
public static final int conversation_list_row=0x7f030002; public static final int conversation_list_row=0x7f030002;
public static final int fragment_conversation=0x7f030003; public static final int fragment_conversation=0x7f030003;
public static final int fragment_conversations_overview=0x7f030004; public static final int fragment_conversations_overview=0x7f030004;
public static final int message_sent=0x7f030005; public static final int message_recieved=0x7f030005;
public static final int message_sent=0x7f030006;
} }
public static final class menu { public static final class menu {
public static final int conversations=0x7f090000; public static final int conversations=0x7f090000;

View File

@ -22,7 +22,6 @@
android:id="@+id/conversation_name" android:id="@+id/conversation_name"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Mein Contact"
android:textColor="#636363" android:textColor="#636363"
android:textSize="20sp" android:textSize="20sp"
android:typeface="sans" /> android:typeface="sans" />
@ -34,7 +33,8 @@
android:layout_below="@id/conversation_name" android:layout_below="@id/conversation_name"
android:textColor="#636363" android:textColor="#636363"
android:textSize="14sp" android:textSize="14sp"
android:text="This is my last message" android:singleLine="true"
android:scrollHorizontally="false"
android:paddingTop="3dp"/> android:paddingTop="3dp"/>
<TextView <TextView
@ -44,7 +44,6 @@
android:layout_alignBaseline="@+id/conversation_name" android:layout_alignBaseline="@+id/conversation_name"
android:layout_alignParentRight="true" android:layout_alignParentRight="true"
android:gravity="right" android:gravity="right"
android:text="2 min"
android:textColor="#636363" android:textColor="#636363"
android:textSize="12sp" /> android:textSize="12sp" />

View File

@ -49,7 +49,9 @@
android:background="#e5e5e5" android:background="#e5e5e5"
tools:listitem="@layout/message_sent" tools:listitem="@layout/message_sent"
android:divider="@null" android:divider="@null"
android:dividerHeight="0dp"> android:dividerHeight="0dp"
android:transcriptMode="alwaysScroll"
android:listSelector="@android:color/transparent">
</ListView> </ListView>
</RelativeLayout> </RelativeLayout>

View File

@ -0,0 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/message_border"
android:layout_toRightOf="@+id/message_photo"
android:layout_alignParentBottom="true"
android:minHeight="48dp"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#ededed"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hi, how are you?"
android:textSize="16sp"
android:id="@+id/message_body"
android:textColor="#333333"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="1dp"
android:text="@string/sending"
android:textColor="#8e8e8e"
android:textSize="12sp"
android:id="@+id/message_time"/>
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/message_photo"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginRight="-1.5dp"
android:padding="0dp"
android:src="@drawable/ic_profile"
android:scaleType="fitXY"/>
</RelativeLayout>

View File

@ -10,12 +10,11 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="@drawable/message_border" android:background="@drawable/message_border"
android:layout_toLeftOf="@+id/message_photo" android:layout_toLeftOf="@+id/message_photo"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true" android:layout_alignParentBottom="true"
android:minHeight="48dp" android:minHeight="48dp"
> >
<LinearLayout <LinearLayout
android:layout_width="fill_parent" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" android:orientation="vertical"
android:background="#ededed" android:background="#ededed"

View File

@ -53,9 +53,32 @@ public class Conversation extends AbstractEntity {
public List<Message> getMessages() { public List<Message> getMessages() {
if (messages == null) this.messages = new ArrayList<Message>(); //prevent null pointer if (messages == null) this.messages = new ArrayList<Message>(); //prevent null pointer
//populate with Conversation (this)
for(Message msg : messages) {
msg.setConversation(this);
}
return messages; return messages;
} }
public String getLatestMessage() {
if ((this.messages == null)||(this.messages.size()==0)) {
return null;
} else {
return this.messages.get(this.messages.size() - 1).getBody();
}
}
public long getLatestMessageDate() {
if ((this.messages == null)||(this.messages.size()==0)) {
return this.getCreated();
} else {
return this.messages.get(this.messages.size() - 1).getTimeSent();
}
}
public void setMessages(List<Message> msgs) { public void setMessages(List<Message> msgs) {
this.messages = msgs; this.messages = msgs;
} }
@ -112,4 +135,8 @@ public class Conversation extends AbstractEntity {
cursor.getLong(cursor.getColumnIndex(CREATED)), cursor.getLong(cursor.getColumnIndex(CREATED)),
cursor.getInt(cursor.getColumnIndex(STATUS))); cursor.getInt(cursor.getColumnIndex(STATUS)));
} }
public void setStatus(int status) {
this.status = status;
}
} }

View File

@ -35,8 +35,9 @@ public class Message extends AbstractEntity {
public Message(Conversation conversation, String body, int encryption) { public Message(Conversation conversation, String body, int encryption) {
this(java.util.UUID.randomUUID().toString(), conversation.getUuid(), this(java.util.UUID.randomUUID().toString(), conversation.getUuid(),
conversation.getContactJid(), body, 0, encryption, conversation.getContactJid(), body, System.currentTimeMillis(), encryption,
Message.STATUS_UNSEND); Message.STATUS_UNSEND);
this.conversation = conversation;
} }
public Message(String uuid, String conversationUUid, String counterpart, public Message(String uuid, String conversationUUid, String counterpart,
@ -67,6 +68,10 @@ public class Message extends AbstractEntity {
return conversationUuid; return conversationUuid;
} }
public Conversation getConversation() {
return this.conversation;
}
public String getCounterpart() { public String getCounterpart() {
return counterpart; return counterpart;
} }
@ -97,4 +102,8 @@ public class Message extends AbstractEntity {
cursor.getInt(cursor.getColumnIndex(STATUS))); cursor.getInt(cursor.getColumnIndex(STATUS)));
} }
public void setConversation(Conversation conv) {
this.conversation = conv;
}
} }

View File

@ -3,6 +3,8 @@ package de.gultsch.chat.persistance;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import de.gultsch.chat.entities.Account;
import de.gultsch.chat.entities.Contact;
import de.gultsch.chat.entities.Conversation; import de.gultsch.chat.entities.Conversation;
import de.gultsch.chat.entities.Message; import de.gultsch.chat.entities.Message;
import android.content.Context; import android.content.Context;
@ -24,12 +26,16 @@ public class DatabaseBackend extends SQLiteOpenHelper {
@Override @Override
public void onCreate(SQLiteDatabase db) { public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + Conversation.TABLENAME + " (" db.execSQL("create table " + Conversation.TABLENAME + " ("
+ Conversation.UUID + " TEXT, " + Conversation.NAME + " TEXT, " + Conversation.UUID + " TEXT PRIMARY KEY, " + Conversation.NAME
+ Conversation.PHOTO_URI + " TEXT, " + Conversation.ACCOUNT + " TEXT, " + Conversation.PHOTO_URI + " TEXT, "
+ " TEXT, " + Conversation.CONTACT + " TEXT, " + Conversation.ACCOUNT + " TEXT, " + Conversation.CONTACT
+ Conversation.CREATED + " NUMBER, " + Conversation.STATUS + " TEXT, " + Conversation.CREATED + " NUMBER, "
+ Conversation.STATUS + " NUMBER)");
db.execSQL("create table " + Message.TABLENAME + "( " + Message.UUID
+ " TEXT PRIMARY KEY, " + Message.CONVERSATION + " TEXT, " + Message.TIME_SENT +" NUMBER, "
+ Message.COUNTERPART + " TEXT, " + Message.BODY + " TEXT, "
+ Message.ENCRYPTION + " NUMBER, " + Message.STATUS
+ " NUMBER)"); + " NUMBER)");
db.execSQL("create table "+Message.TABLENAME+ "()");
} }
@Override @Override
@ -45,15 +51,20 @@ public class DatabaseBackend extends SQLiteOpenHelper {
return instance; return instance;
} }
public void addConversation(Conversation conversation) { public void createConversation(Conversation conversation) {
SQLiteDatabase db = this.getWritableDatabase(); SQLiteDatabase db = this.getWritableDatabase();
db.insert("conversations", null, conversation.getContentValues()); db.insert(Conversation.TABLENAME, null, conversation.getContentValues());
}
public void createMessage(Message message) {
SQLiteDatabase db = this.getWritableDatabase();
db.insert(Message.TABLENAME, null, message.getContentValues());
} }
public int getConversationCount() { public int getConversationCount() {
SQLiteDatabase db = this.getReadableDatabase(); SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery( Cursor cursor = db.rawQuery(
"select count(uuid) as count from conversations", null); "select count(uuid) as count from "+Conversation.TABLENAME+" where "+Conversation.STATUS+"="+Conversation.STATUS_AVAILABLE, null);
cursor.moveToFirst(); cursor.moveToFirst();
return cursor.getInt(0); return cursor.getInt(0);
} }
@ -62,14 +73,43 @@ public class DatabaseBackend extends SQLiteOpenHelper {
List<Conversation> list = new ArrayList<Conversation>(); List<Conversation> list = new ArrayList<Conversation>();
SQLiteDatabase db = this.getReadableDatabase(); SQLiteDatabase db = this.getReadableDatabase();
String[] selectionArgs = { "" + status }; String[] selectionArgs = { "" + status };
Cursor cursor = db Cursor cursor = db.rawQuery("select * from " + Conversation.TABLENAME
.rawQuery( + " where " + Conversation.STATUS + " = ? order by "
"select * from conversations where status = ? order by created desc", + Conversation.CREATED + " desc", selectionArgs);
selectionArgs);
while (cursor.moveToNext()) { while (cursor.moveToNext()) {
list.add(Conversation.fromCursor(cursor)); list.add(Conversation.fromCursor(cursor));
} }
return list; return list;
} }
public List<Message> getMessages(Conversation conversation, int limit) {
List<Message> list = new ArrayList<Message>();
SQLiteDatabase db = this.getReadableDatabase();
String[] selectionArgs = { conversation.getUuid() };
Cursor cursor = db.query(Message.TABLENAME, null,Message.CONVERSATION +"=?", selectionArgs, null, null,
Message.TIME_SENT+" DESC",String.valueOf(limit));
if (cursor.getCount() > 0 ) {
cursor.moveToLast();
do {
list.add(Message.fromCursor(cursor));
} while (cursor.moveToPrevious());
}
return list;
}
public Conversation findConversation(Account account, Contact contact) {
SQLiteDatabase db = this.getReadableDatabase();
String[] selectionArgs = { account.getUuid() , contact.getJid() };
Cursor cursor = db.query(Conversation.TABLENAME, null,Conversation.ACCOUNT+"=? AND "+Conversation.CONTACT+"=?", selectionArgs, null, null, null);
if (cursor.getCount() == 0) return null;
cursor.moveToFirst();
return Conversation.fromCursor(cursor);
}
public void updateConversation(Conversation conversation) {
SQLiteDatabase db = this.getReadableDatabase();
String[] args = {conversation.getUuid()};
db.update(Conversation.TABLENAME, conversation.getContentValues(),Conversation.UUID+"=?",args);
}
} }

View File

@ -2,6 +2,8 @@ package de.gultsch.chat.services;
import java.util.List; import java.util.List;
import de.gultsch.chat.entities.Account;
import de.gultsch.chat.entities.Contact;
import de.gultsch.chat.entities.Conversation; import de.gultsch.chat.entities.Conversation;
import de.gultsch.chat.entities.Message; import de.gultsch.chat.entities.Message;
import de.gultsch.chat.persistance.DatabaseBackend; import de.gultsch.chat.persistance.DatabaseBackend;
@ -35,15 +37,39 @@ public class XmppConnectionService extends Service {
} }
public void sendMessage(Message message) { public void sendMessage(Message message) {
Log.d(LOGTAG,"sending message"); databaseBackend.createMessage(message);
} }
public void addConversation(Conversation conversation) { public void addConversation(Conversation conversation) {
databaseBackend.addConversation(conversation); databaseBackend.createConversation(conversation);
} }
public List<Conversation> getConversations(int status) { public List<Conversation> getConversations(int status) {
return databaseBackend.getConversations(status); return databaseBackend.getConversations(status);
} }
public List<Message> getMessages(Conversation conversation) {
return databaseBackend.getMessages(conversation, 100);
}
public Conversation findOrCreateConversation(Account account, Contact contact) {
Conversation conversation = databaseBackend.findConversation(account, contact);
if (conversation!=null) {
Log.d("gultsch","found one. unarchive it");
conversation.setStatus(Conversation.STATUS_AVAILABLE);
this.databaseBackend.updateConversation(conversation);
} else {
conversation = new Conversation(contact.getDisplayName(), contact.getProfilePhoto(), account, contact.getJid());
this.databaseBackend.createConversation(conversation);
}
return conversation;
}
public void updateConversation(Conversation conversation) {
this.databaseBackend.updateConversation(conversation);
}
public int getConversationCount() {
return this.databaseBackend.getConversationCount();
}
} }

View File

@ -1,14 +1,13 @@
package de.gultsch.chat.ui; package de.gultsch.chat.ui;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List; import java.util.List;
import de.gultsch.chat.R; import de.gultsch.chat.R;
import de.gultsch.chat.R.id; import de.gultsch.chat.R.id;
import de.gultsch.chat.entities.Account;
import de.gultsch.chat.entities.Contact;
import de.gultsch.chat.entities.Conversation; import de.gultsch.chat.entities.Conversation;
import de.gultsch.chat.persistance.DatabaseBackend;
import de.gultsch.chat.utils.Beautifier; import de.gultsch.chat.utils.Beautifier;
import android.os.Bundle; import android.os.Bundle;
import android.app.FragmentTransaction; import android.app.FragmentTransaction;
@ -34,12 +33,55 @@ import android.widget.ImageView;
public class ConversationActivity extends XmppActivity { public class ConversationActivity extends XmppActivity {
public static final String VIEW_CONVERSATION = "viewConversation"; public static final String VIEW_CONVERSATION = "viewConversation";
private static final String LOGTAG = "secureconversation";
protected static final String CONVERSATION = "conversationUuid"; protected static final String CONVERSATION = "conversationUuid";
protected SlidingPaneLayout spl; protected SlidingPaneLayout spl;
final List<Conversation> conversationList = new ArrayList<Conversation>(); private List<Conversation> conversationList = new ArrayList<Conversation>();
private int selectedConversation = 0;
private ListView listView;
private boolean paneShouldBeOpen = true;
public List<Conversation> getConversationList() {
return this.conversationList;
}
public int getSelectedConversation() {
return this.selectedConversation;
}
public ListView getConversationListView() {
return this.listView;
}
public SlidingPaneLayout getSlidingPaneLayout() {
return this.spl;
}
public boolean shouldPaneBeOpen() {
return paneShouldBeOpen;
}
public void updateConversationList() {
if (conversationList.size() >= 1) {
Conversation currentConv = conversationList.get(selectedConversation);
Collections.sort(this.conversationList, new Comparator<Conversation>() {
@Override
public int compare(Conversation lhs, Conversation rhs) {
return (int) (rhs.getLatestMessageDate() - lhs.getLatestMessageDate());
}
});
for(int i = 0; i < conversationList.size(); ++i) {
if (currentConv == conversationList.get(i)) {
selectedConversation = i;
break;
}
}
}
this.listView.invalidateViews();
}
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -48,7 +90,7 @@ public class ConversationActivity extends XmppActivity {
setContentView(R.layout.fragment_conversations_overview); setContentView(R.layout.fragment_conversations_overview);
final ListView listView = (ListView) findViewById(R.id.list); listView = (ListView) findViewById(R.id.list);
listView.setAdapter(new ArrayAdapter<Conversation>(this, listView.setAdapter(new ArrayAdapter<Conversation>(this,
R.layout.conversation_list_row, conversationList) { R.layout.conversation_list_row, conversationList) {
@ -61,8 +103,9 @@ public class ConversationActivity extends XmppActivity {
} }
((TextView) view.findViewById(R.id.conversation_name)) ((TextView) view.findViewById(R.id.conversation_name))
.setText(getItem(position).getName()); .setText(getItem(position).getName());
((TextView) view.findViewById(R.id.conversation_lastmsg)).setText(getItem(position).getLatestMessage());
((TextView) view.findViewById(R.id.conversation_lastupdate)) ((TextView) view.findViewById(R.id.conversation_lastupdate))
.setText(Beautifier.readableTimeDifference(getItem(position).getCreated())); .setText(Beautifier.readableTimeDifference(getItem(position).getLatestMessageDate()));
((ImageView) view.findViewById(R.id.conversation_image)) ((ImageView) view.findViewById(R.id.conversation_image))
.setImageURI(getItem(position).getProfilePhotoUri()); .setImageURI(getItem(position).getProfilePhotoUri());
return view; return view;
@ -75,12 +118,14 @@ public class ConversationActivity extends XmppActivity {
@Override @Override
public void onItemClick(AdapterView<?> arg0, View clickedView, public void onItemClick(AdapterView<?> arg0, View clickedView,
int position, long arg3) { int position, long arg3) {
Log.d(LOGTAG, "List view was klicked on position " + position); paneShouldBeOpen = false;
swapConversationFragment(conversationList.get(position)); if (selectedConversation != position) {
getActionBar().setTitle( selectedConversation = position;
conversationList.get(position).getName()); swapConversationFragment(); //.onBackendConnected(conversationList.get(position));
} else {
spl.closePane(); spl.closePane();
} }
}
}); });
spl = (SlidingPaneLayout) findViewById(id.slidingpanelayout); spl = (SlidingPaneLayout) findViewById(id.slidingpanelayout);
spl.setParallaxDistance(150); spl.setParallaxDistance(150);
@ -91,6 +136,7 @@ public class ConversationActivity extends XmppActivity {
@Override @Override
public void onPanelOpened(View arg0) { public void onPanelOpened(View arg0) {
paneShouldBeOpen = true;
getActionBar().setDisplayHomeAsUpEnabled(false); getActionBar().setDisplayHomeAsUpEnabled(false);
getActionBar().setTitle(R.string.app_name); getActionBar().setTitle(R.string.app_name);
invalidateOptionsMenu(); invalidateOptionsMenu();
@ -105,21 +151,13 @@ public class ConversationActivity extends XmppActivity {
focus.getWindowToken(), focus.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS); InputMethodManager.HIDE_NOT_ALWAYS);
} }
listView.requestFocus();
} }
@Override @Override
public void onPanelClosed(View arg0) { public void onPanelClosed(View arg0) {
if (conversationList.size() > 0) { if (conversationList.size() > 0) {
getActionBar().setDisplayHomeAsUpEnabled(true); getActionBar().setDisplayHomeAsUpEnabled(true);
ConversationFragment convFrag = (ConversationFragment) getFragmentManager() getActionBar().setTitle(conversationList.get(selectedConversation).getName());
.findFragmentById(R.id.selected_conversation);
if (convFrag == null) {
Log.d(LOGTAG, "conversation fragment was not found.");
return; // just do nothing. at least dont crash
}
getActionBar().setTitle(
convFrag.getConversation().getName());
invalidateOptionsMenu(); invalidateOptionsMenu();
} }
} }
@ -134,7 +172,6 @@ public class ConversationActivity extends XmppActivity {
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.conversations, menu); getMenuInflater().inflate(R.menu.conversations, menu);
if (spl.isOpen()) { if (spl.isOpen()) {
@ -161,8 +198,23 @@ public class ConversationActivity extends XmppActivity {
break; break;
case R.id.action_add: case R.id.action_add:
startActivity(new Intent(this, NewConversationActivity.class)); startActivity(new Intent(this, NewConversationActivity.class));
break;
case R.id.action_archive: case R.id.action_archive:
Conversation conv = getConversationList().get(selectedConversation);
conv.setStatus(Conversation.STATUS_ARCHIVED);
xmppConnectionService.updateConversation(conv);
conversationList.remove(selectedConversation);
selectedConversation = 0;
if (conversationList.size() >= 1) {
paneShouldBeOpen = true;
swapConversationFragment();
((ArrayAdapter) listView.getAdapter()).notifyDataSetChanged();
spl.openPane();
} else {
startActivity(new Intent(this, NewConversationActivity.class));
finish();
}
//goto new
break; break;
default: default:
break; break;
@ -170,14 +222,14 @@ public class ConversationActivity extends XmppActivity {
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
protected void swapConversationFragment(Conversation conv) { protected ConversationFragment swapConversationFragment() {
Log.d(LOGTAG, "swap conversation fragment to " + conv.getName());
ConversationFragment selectedFragment = new ConversationFragment(); ConversationFragment selectedFragment = new ConversationFragment();
selectedFragment.setConversation(conv);
FragmentTransaction transaction = getFragmentManager() FragmentTransaction transaction = getFragmentManager()
.beginTransaction(); .beginTransaction();
transaction.replace(R.id.selected_conversation, selectedFragment); transaction.replace(R.id.selected_conversation, selectedFragment,"conversation");
transaction.commit(); transaction.commit();
return selectedFragment;
} }
@Override @Override
@ -202,25 +254,31 @@ public class ConversationActivity extends XmppActivity {
} }
@Override @Override
void servConnected() { void onBackendConnected() {
conversationList.clear(); conversationList.clear();
conversationList.addAll(xmppConnectionService conversationList.addAll(xmppConnectionService
.getConversations(Conversation.STATUS_AVAILABLE)); .getConversations(Conversation.STATUS_AVAILABLE));
//spl.openPane(); for(Conversation conversation : conversationList) {
conversation.setMessages(xmppConnectionService.getMessages(conversation));
}
this.updateConversationList();
if ((getIntent().getAction().equals(Intent.ACTION_VIEW) && (!handledViewIntent))) { if ((getIntent().getAction().equals(Intent.ACTION_VIEW) && (!handledViewIntent))) {
if (getIntent().getType().equals( if (getIntent().getType().equals(
ConversationActivity.VIEW_CONVERSATION)) { ConversationActivity.VIEW_CONVERSATION)) {
handledViewIntent = true; handledViewIntent = true;
swapConversationFragment(conversationList.get(0)); String convToView = (String) getIntent().getExtras().get(CONVERSATION);
spl.closePane();
// why do i even need this
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setTitle(conversationList.get(0).getName());
for(int i = 0; i < conversationList.size(); ++i) {
if (conversationList.get(i).getUuid().equals(convToView)) {
selectedConversation = i;
}
}
paneShouldBeOpen = false;
swapConversationFragment();
} }
} else { } else {
if (conversationList.size() <= 0) { if (conversationList.size() <= 0) {
@ -228,7 +286,16 @@ public class ConversationActivity extends XmppActivity {
startActivity(new Intent(this, NewConversationActivity.class)); startActivity(new Intent(this, NewConversationActivity.class));
finish(); finish();
} else { } else {
swapConversationFragment(conversationList.get(0)); //find currently loaded fragment
ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager().findFragmentByTag("conversation");
if (selectedFragment!=null) {
Log.d("gultsch","ConversationActivity. found old fragment.");
selectedFragment.onBackendConnected();
} else {
Log.d("gultsch","conversationactivity. no old fragment found. creating new one");
Log.d("gultsch","selected conversation is #"+selectedConversation);
swapConversationFragment();
}
} }
} }
} }

View File

@ -1,11 +1,13 @@
package de.gultsch.chat.ui; package de.gultsch.chat.ui;
import java.util.ArrayList;
import java.util.List;
import de.gultsch.chat.R; import de.gultsch.chat.R;
import de.gultsch.chat.entities.Conversation; import de.gultsch.chat.entities.Conversation;
import de.gultsch.chat.entities.Message; import de.gultsch.chat.entities.Message;
import de.gultsch.chat.utils.Beautifier; import de.gultsch.chat.utils.Beautifier;
import android.app.Fragment; import android.app.Fragment;
import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.net.Uri; import android.net.Uri;
@ -18,7 +20,6 @@ import android.view.View.OnClickListener;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView; import android.widget.ListView;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.ImageView; import android.widget.ImageView;
@ -26,77 +27,111 @@ import android.widget.TextView;
public class ConversationFragment extends Fragment { public class ConversationFragment extends Fragment {
Conversation conversation; protected Conversation conversation;
protected ListView messagesView;
public void setConversation(Conversation conv) { protected LayoutInflater inflater;
this.conversation = conv; protected List<Message> messageList = new ArrayList<Message>();
}
@Override @Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, public View onCreateView(final LayoutInflater inflater,
Bundle savedInstanceState) { ViewGroup container, Bundle savedInstanceState) {
String[] mProjection = new String[] this.inflater = inflater;
{
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, final View view = inflater.inflate(R.layout.fragment_conversation,
false); container, false);
((ImageButton) view.findViewById(R.id.textSendButton)) ((ImageButton) view.findViewById(R.id.textSendButton))
.setOnClickListener(new OnClickListener() { .setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
EditText chatMsg = (EditText) view.findViewById(R.id.textinput); ConversationActivity activity = (ConversationActivity) getActivity();
if (chatMsg.getText().length() < 1) return; EditText chatMsg = (EditText) view
Message message = new Message(conversation,chatMsg.getText().toString(), .findViewById(R.id.textinput);
Message.ENCRYPTION_NONE); if (chatMsg.getText().length() < 1)
XmppActivity activity = (XmppActivity) getActivity(); return;
Message message = new Message(conversation, chatMsg
.getText().toString(), Message.ENCRYPTION_NONE);
activity.xmppConnectionService.sendMessage(message); activity.xmppConnectionService.sendMessage(message);
conversation.getMessages().add(message); conversation.getMessages().add(message);
chatMsg.setText(""); chatMsg.setText("");
ListView messagesView = (ListView) view.findViewById(R.id.messages_view); messageList.add(message);
ArrayAdapter<Message> adapter = (ArrayAdapter<Message>) messagesView.getAdapter();
adapter.notifyDataSetChanged();
messagesView.setSelection(conversation.getMessages().size() -1); activity.updateConversationList();
messagesView.setSelection(messageList.size() - 1);
} }
}); });
ListView messagesView = (ListView) view messagesView = (ListView) view.findViewById(R.id.messages_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));
messagesView.setAdapter(new ArrayAdapter<Message>(this.getActivity() messagesView.setAdapter(new ArrayAdapter<Message>(this.getActivity()
.getApplicationContext(), R.layout.message_sent, .getApplicationContext(), R.layout.message_sent, this.messageList) {
this.conversation.getMessages()) {
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 @Override
public View getView(int position, View view, ViewGroup parent) { public View getView(int position, View view, ViewGroup parent) {
Message item = getItem(position); Message item = getItem(position);
if ((item.getStatus() != Message.STATUS_RECIEVED) int type = getItemViewType(position);
|| (item.getStatus() == Message.STATUS_SEND)) { if (view == null) {
view = (View) inflater.inflate(R.layout.message_sent, null); switch (type) {
((ImageView) view.findViewById(R.id.message_photo)).setImageURI(profilePicture); case SENT:
view = (View) inflater.inflate(R.layout.message_sent,
null);
Log.d("gultsch", "inflated new message_sent view");
break;
case RECIEVED:
view = (View) inflater.inflate(
R.layout.message_recieved, null);
Log.d("gultsch", "inflated new message_recieved view");
break;
} }
((TextView) view.findViewById(R.id.message_body)).setText(item.getBody()); } else {
Log.d("gultsch", "recylecd a view");
}
if (type == RECIEVED) {
((ImageView) view.findViewById(R.id.message_photo))
.setImageURI(item.getConversation()
.getProfilePhotoUri());
} else {
((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); TextView time = (TextView) view.findViewById(R.id.message_time);
if (item.getStatus() == Message.STATUS_UNSEND) { if (item.getStatus() == Message.STATUS_UNSEND) {
time.setTypeface(null, Typeface.ITALIC); time.setTypeface(null, Typeface.ITALIC);
} else { } else {
time.setText(Beautifier.readableTimeDifference(item.getTimeSent())); time.setText(Beautifier.readableTimeDifference(item
.getTimeSent()));
} }
return view; return view;
} }
@ -105,7 +140,37 @@ public class ConversationFragment extends Fragment {
return view; return view;
} }
public Conversation getConversation() { @Override
return conversation; public void onStart() {
super.onStart();
Log.d("gultsch","conversationfragment onStart");
final ConversationActivity activity = (ConversationActivity) getActivity();
// TODO check if bond and get data back
if (activity.xmppConnectionServiceBound) {
this.conversation = activity.getConversationList().get(activity.getSelectedConversation());
this.messageList.clear();
this.messageList.addAll(this.conversation.getMessages());
}
// rendering complete. now go tell activity to close pane
if (!activity.shouldPaneBeOpen()) {
activity.getSlidingPaneLayout().closePane();
}
int size = this.messageList.size();
if (size >= 1)
messagesView.setSelection(size - 1);
}
public void onBackendConnected() {
final ConversationActivity activity = (ConversationActivity) getActivity();
this.conversation = activity.getConversationList().get(activity.getSelectedConversation());
this.messageList.clear();
this.messageList.addAll(this.conversation.getMessages());
} }
} }

View File

@ -85,9 +85,7 @@ public class NewConversationActivity extends XmppActivity {
Account account = new Account(); Account account = new Account();
Conversation conversation = new Conversation(clickedContact.getDisplayName(), clickedContact.getProfilePhoto(), account, clickedContact.getJid()); Conversation conversation = xmppConnectionService.findOrCreateConversation(account, clickedContact);
xmppConnectionService.addConversation(conversation);
Intent viewConversationIntent = new Intent(v.getContext(),ConversationActivity.class); Intent viewConversationIntent = new Intent(v.getContext(),ConversationActivity.class);
viewConversationIntent.setAction(Intent.ACTION_VIEW); viewConversationIntent.setAction(Intent.ACTION_VIEW);
@ -218,9 +216,10 @@ public class NewConversationActivity extends XmppActivity {
} }
@Override @Override
void servConnected() { void onBackendConnected() {
// TODO Auto-generated method stub
getActionBar().setDisplayHomeAsUpEnabled(false);
getActionBar().setHomeButtonEnabled(false);
} }
@Override @Override

View File

@ -20,7 +20,7 @@ public abstract class XmppActivity extends Activity {
XmppConnectionBinder binder = (XmppConnectionBinder) service; XmppConnectionBinder binder = (XmppConnectionBinder) service;
xmppConnectionService = binder.getService(); xmppConnectionService = binder.getService();
xmppConnectionServiceBound = true; xmppConnectionServiceBound = true;
servConnected(); onBackendConnected();
} }
@Override @Override
@ -47,5 +47,5 @@ public abstract class XmppActivity extends Activity {
} }
} }
abstract void servConnected(); abstract void onBackendConnected();
} }