diff options
author | Daniel Gultsch <daniel.gultsch@rwth-aachen.de> | 2014-01-24 23:58:51 +0100 |
---|---|---|
committer | Daniel Gultsch <daniel.gultsch@rwth-aachen.de> | 2014-01-24 23:58:51 +0100 |
commit | df9dcb1060d44a719e2892446b62cde884a880cf (patch) | |
tree | ed3bf01c8f249162018b33460f81b52ac2f618fb /src/de/gultsch/chat/ui | |
parent | b273390e26e1d28f6df01fb421d8554cb7588a58 (diff) |
basic persistance layer. refactor some parts of the conversation activity
Diffstat (limited to '')
-rw-r--r-- | src/de/gultsch/chat/ui/ConversationActivity.java | 104 | ||||
-rw-r--r-- | src/de/gultsch/chat/ui/ConversationFragment.java | 11 | ||||
-rw-r--r-- | src/de/gultsch/chat/ui/NewConversationActivity.java | 22 |
3 files changed, 91 insertions, 46 deletions
diff --git a/src/de/gultsch/chat/ui/ConversationActivity.java b/src/de/gultsch/chat/ui/ConversationActivity.java index 0abfb3e6..f26d8982 100644 --- a/src/de/gultsch/chat/ui/ConversationActivity.java +++ b/src/de/gultsch/chat/ui/ConversationActivity.java @@ -1,15 +1,20 @@ package de.gultsch.chat.ui; import java.util.HashMap; +import java.util.List; -import de.gultsch.chat.Contact; -import de.gultsch.chat.Conversation; import de.gultsch.chat.ConversationCursor; import de.gultsch.chat.ConversationList; import de.gultsch.chat.R; 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.persistance.DatabaseBackend; +import android.net.Uri; import android.os.Bundle; import android.app.Activity; +import android.app.FragmentManager; import android.app.FragmentTransaction; import android.content.Context; import android.content.Intent; @@ -17,14 +22,18 @@ import android.support.v4.widget.SlidingPaneLayout; import android.support.v4.widget.SlidingPaneLayout.PanelSlideListener; import android.util.Log; import android.view.KeyEvent; +import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; +import android.view.ViewGroup; import android.view.inputmethod.InputMethodManager; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; +import android.widget.ArrayAdapter; import android.widget.ListView; -import android.widget.SimpleCursorAdapter; +import android.widget.TextView; +import android.widget.ImageView; public class ConversationActivity extends Activity { @@ -34,34 +43,51 @@ public class ConversationActivity extends Activity { protected SlidingPaneLayout spl; protected HashMap<Conversation, ConversationFragment> conversationFragments = new HashMap<Conversation, ConversationFragment>(); + private DatabaseBackend dbb; @Override protected void onCreate(Bundle savedInstanceState) { + + dbb = DatabaseBackend.getInstance(this); + super.onCreate(savedInstanceState); - setContentView(R.layout.fragment_conversations_overview); + final List<Conversation> conversationList = dbb.getConversations(Conversation.STATUS_AVAILABLE); - final ConversationList conversationList = new ConversationList(); + if (getIntent().getAction().equals(Intent.ACTION_MAIN)) { + if (conversationList.size() < 0) { + Log.d("gultsch", + "no conversations detected. redirect to new conversation activity"); + startActivity(new Intent(this, NewConversationActivity.class)); + finish(); + } + } - String[] fromColumns = { ConversationCursor.NAME, - ConversationCursor.LAST_MSG }; - int[] toViews = { R.id.conversation_name, R.id.conversation_lastmsg }; + setContentView(R.layout.fragment_conversations_overview); - final SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, - R.layout.conversation_list_row, conversationList.getCursor(), - fromColumns, toViews, 0); final ListView listView = (ListView) findViewById(R.id.list); - listView.setAdapter(adapter); - + + listView.setAdapter(new ArrayAdapter<Conversation>(this, R.layout.conversation_list_row, conversationList) { + @Override + public View getView (int position, View view, ViewGroup parent) { + if (view == null) { + LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); + view = (View) inflater.inflate(R.layout.conversation_list_row,null); + ((TextView) view.findViewById(R.id.conversation_name)).setText(getItem(position).getName()); + ((ImageView) view.findViewById(R.id.conversation_image)).setImageURI(getItem(position).getProfilePhotoUri()); + } + return view; + } + + }); + listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View clickedView, int position, long arg3) { - conversationList.setSelectedConversationPosition(position); - swapConversationFragment(conversationList); - getActionBar().setTitle( - conversationList.getSelectedConversation().getName()); + swapConversationFragment(conversationList.get(position)); + getActionBar().setTitle(conversationList.get(position).getName()); spl.closePane(); } }); @@ -95,9 +121,8 @@ public class ConversationActivity extends Activity { public void onPanelClosed(View arg0) { if (conversationList.size() > 0) { getActionBar().setDisplayHomeAsUpEnabled(true); - getActionBar().setTitle( - conversationList.getSelectedConversation() - .getName()); + ConversationFragment convFrag = (ConversationFragment) getFragmentManager().findFragmentById(R.id.selected_conversation); + getActionBar().setTitle(convFrag.getConversation().getName()); invalidateOptionsMenu(); } } @@ -116,25 +141,26 @@ public class ConversationActivity extends Activity { ConversationActivity.CONVERSATION_CONTACT); Log.d("gultsch", "start conversation with " + contact.getDisplayName()); - int pos = conversationList - .addAndReturnPosition(new Conversation(contact - .getDisplayName())); - conversationList.setSelectedConversationPosition(pos); - swapConversationFragment(conversationList); + + // start new conversation + Conversation conversation = new Conversation( + contact.getDisplayName(), contact.getProfilePhoto(), + new Account(), contact.getJid()); + + //@TODO don't write to database here; always go through service + dbb.addConversation(conversation); + conversationList.add(0, conversation); + swapConversationFragment(conversationList.get(0)); spl.closePane(); // why do i even need this getActionBar().setDisplayHomeAsUpEnabled(true); - getActionBar().setTitle( - conversationList.getSelectedConversation().getName()); + getActionBar().setTitle(conversationList.get(0).getName()); } } else { // normal startup if (conversationList.size() >= 1) { - conversationList.setSelectedConversationPosition(0); - swapConversationFragment(conversationList); - } else { - startActivity(new Intent(this, NewConversationActivity.class)); + swapConversationFragment(conversationList.get(0)); } } } @@ -168,24 +194,22 @@ public class ConversationActivity extends Activity { break; case R.id.action_add: startActivity(new Intent(this, NewConversationActivity.class)); + case R.id.action_archive: + default: break; } return super.onOptionsItemSelected(item); } - protected void swapConversationFragment( - final ConversationList conversationList) { + protected void swapConversationFragment(Conversation conv) { ConversationFragment selectedFragment; - if (conversationFragments.containsKey(conversationList - .getSelectedConversation())) { - selectedFragment = conversationFragments.get(conversationList - .getSelectedConversation()); + if (conversationFragments.containsKey(conv)) { + selectedFragment = conversationFragments.get(conv); } else { selectedFragment = new ConversationFragment(); - conversationFragments.put( - conversationList.getSelectedConversation(), - selectedFragment); + selectedFragment.setConversation(conv); + conversationFragments.put(conv,selectedFragment); } FragmentTransaction transaction = getFragmentManager() .beginTransaction(); diff --git a/src/de/gultsch/chat/ui/ConversationFragment.java b/src/de/gultsch/chat/ui/ConversationFragment.java index 5b8acd98..70ceb07c 100644 --- a/src/de/gultsch/chat/ui/ConversationFragment.java +++ b/src/de/gultsch/chat/ui/ConversationFragment.java @@ -1,6 +1,7 @@ package de.gultsch.chat.ui; import de.gultsch.chat.R; +import de.gultsch.chat.entities.Conversation; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; @@ -9,8 +10,18 @@ import android.view.ViewGroup; public class ConversationFragment extends Fragment { + Conversation conversation; + + public void setConversation(Conversation conv) { + this.conversation = conv; + } + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_conversation, container, false); } + + public Conversation getConversation() { + return conversation; + } } diff --git a/src/de/gultsch/chat/ui/NewConversationActivity.java b/src/de/gultsch/chat/ui/NewConversationActivity.java index 47bdd0ba..9dc385a4 100644 --- a/src/de/gultsch/chat/ui/NewConversationActivity.java +++ b/src/de/gultsch/chat/ui/NewConversationActivity.java @@ -5,8 +5,9 @@ import java.util.Map.Entry; import java.util.regex.Matcher; import java.util.regex.Pattern; -import de.gultsch.chat.Contact; import de.gultsch.chat.R; +import de.gultsch.chat.entities.Contact; +import de.gultsch.chat.persistance.DatabaseBackend; import android.os.Bundle; import android.provider.ContactsContract; import android.text.Editable; @@ -48,15 +49,14 @@ public class NewConversationActivity extends Activity { + "\") AND (" + ContactsContract.CommonDataKinds.Im.PROTOCOL + "=\"" + ContactsContract.CommonDataKinds.Im.PROTOCOL_JABBER + "\")"; + protected static final String DEFAULT_PROFILE_PHOTO = "android.resource://de.gultsch.chat/" + R.drawable.ic_profile; protected View getViewForContact(Contact contact) { LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = (View) inflater.inflate(R.layout.contact,null); ((TextView) view.findViewById(R.id.contact_display_name)).setText(contact.getDisplayName()); ((TextView) view.findViewById(R.id.contact_jid)).setText(contact.getJid()); - if (contact.getProfilePhoto() != null) { ((ImageView) view.findViewById(R.id.contact_photo)).setImageURI(contact.getProfilePhoto()); - } view.setOnClickListener(new OnClickListener() { @Override @@ -93,6 +93,12 @@ public class NewConversationActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + + if (DatabaseBackend.getInstance(this).getConversationCount() < 1) { + getActionBar().setDisplayHomeAsUpEnabled(false); + getActionBar().setHomeButtonEnabled(false); + } + setContentView(R.layout.activity_new_conversation); CursorLoader mCursorLoader = new CursorLoader(this, ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, null, @@ -102,13 +108,17 @@ public class NewConversationActivity extends Activity { @Override public void onLoadComplete(Loader<Cursor> arg0, Cursor cursor) { while (cursor.moveToNext()) { + String profilePhoto = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.PHOTO_THUMBNAIL_URI)); + if (profilePhoto == null) { + profilePhoto = DEFAULT_PROFILE_PHOTO; + } Contact contact = new Contact( cursor.getString(cursor .getColumnIndex(ContactsContract.Data.DISPLAY_NAME)), cursor.getString(cursor .getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA)), - cursor.getString(cursor - .getColumnIndex(ContactsContract.Data.PHOTO_THUMBNAIL_URI))); + profilePhoto + ); View contactView = getViewForContact(contact); availablePhoneContacts.put(contact, getViewForContact(contact)); ((LinearLayout) findViewById(R.id.phone_contacts)).addView(contactView); @@ -166,7 +176,7 @@ public class NewConversationActivity extends Activity { if (matcher.find()) { createNewContact.removeAllViews(); String name = search.split("@")[0]; - newContact = new Contact(name,search,null); + newContact = new Contact(name,search,DEFAULT_PROFILE_PHOTO); newContactView = getViewForContact(newContact); newContactView.findViewById(R.id.contact_divider).setVisibility(View.GONE); createNewContact.addView(newContactView); |