From efba73ffd012cede18b6d50fe32b3077bb866741 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 28 Jan 2014 22:21:08 +0100 Subject: [PATCH] refactored newconversationactivty to use array adapter --- gen/de/gultsch/chat/R.java | 86 +++--- res/layout/activity_new_conversation.xml | 82 ++--- res/layout/contact.xml | 13 +- .../gultsch/chat/ui/ConversationActivity.java | 2 +- .../gultsch/chat/ui/ConversationFragment.java | 6 + .../chat/ui/NewConversationActivity.java | 288 +++++++++--------- 6 files changed, 219 insertions(+), 258 deletions(-) diff --git a/gen/de/gultsch/chat/R.java b/gen/de/gultsch/chat/R.java index 68890a5c3..b7dde8f47 100644 --- a/gen/de/gultsch/chat/R.java +++ b/gen/de/gultsch/chat/R.java @@ -38,56 +38,50 @@ public final class R { public static final int section_header=0x7f02000a; } public static final class id { - public static final int account_confirm_password_desc=0x7f0a0017; + public static final int account_confirm_password_desc=0x7f0a0011; public static final int account_jid=0x7f0a0000; - public static final int account_list=0x7f0a0027; - public static final int account_password=0x7f0a0015; - public static final int account_password_confirm2=0x7f0a0018; - public static final int account_server=0x7f0a001d; - public static final int account_show_advanced=0x7f0a0019; + public static final int account_list=0x7f0a0021; + public static final int account_password=0x7f0a000f; + public static final int account_password_confirm2=0x7f0a0012; + public static final int account_server=0x7f0a0017; + public static final int account_show_advanced=0x7f0a0013; public static final int account_status=0x7f0a0002; - public static final int account_username=0x7f0a0013; - public static final int action_accounts=0x7f0a002f; - public static final int action_add=0x7f0a002b; - public static final int action_add_account=0x7f0a0031; - public static final int action_archive=0x7f0a002e; - public static final int action_details=0x7f0a002d; - public static final int action_security=0x7f0a002c; - public static final int action_settings=0x7f0a0030; - public static final int advanced_options=0x7f0a001a; - public static final int contact_display_name=0x7f0a000c; - public static final int contact_divider=0x7f0a000e; - public static final int contact_jid=0x7f0a000d; - public static final int contact_photo=0x7f0a000b; - public static final int conversation_image=0x7f0a000f; - public static final int conversation_lastmsg=0x7f0a0011; - public static final int conversation_lastupdate=0x7f0a0012; - public static final int conversation_name=0x7f0a0010; - public static final int create_new_contact=0x7f0a000a; - public static final int editText3=0x7f0a001f; - public static final int edit_account_register_new=0x7f0a0016; - public static final int jabber_contacts=0x7f0a0008; - public static final int jabber_contacts_header=0x7f0a0007; - public static final int list=0x7f0a0025; - public static final int message_body=0x7f0a0029; - public static final int message_photo=0x7f0a0028; - public static final int message_time=0x7f0a002a; - public static final int messages_view=0x7f0a0023; - public static final int new_contact_header=0x7f0a0009; + public static final int account_username=0x7f0a000d; + public static final int action_accounts=0x7f0a0029; + public static final int action_add=0x7f0a0025; + public static final int action_add_account=0x7f0a002b; + public static final int action_archive=0x7f0a0028; + public static final int action_details=0x7f0a0027; + public static final int action_security=0x7f0a0026; + public static final int action_settings=0x7f0a002a; + public static final int advanced_options=0x7f0a0014; + public static final int contactList=0x7f0a0005; + public static final int contact_display_name=0x7f0a0007; + public static final int contact_jid=0x7f0a0008; + public static final int contact_photo=0x7f0a0006; + public static final int contacts_header=0x7f0a0004; + public static final int conversation_image=0x7f0a0009; + public static final int conversation_lastmsg=0x7f0a000b; + public static final int conversation_lastupdate=0x7f0a000c; + public static final int conversation_name=0x7f0a000a; + public static final int editText3=0x7f0a0019; + public static final int edit_account_register_new=0x7f0a0010; + public static final int list=0x7f0a001f; + public static final int message_body=0x7f0a0023; + public static final int message_photo=0x7f0a0022; + public static final int message_time=0x7f0a0024; + public static final int messages_view=0x7f0a001d; public static final int new_conversation_search=0x7f0a0003; - public static final int phone_contacts=0x7f0a0006; - public static final int phone_contacts_header=0x7f0a0005; - public static final int scrollView1=0x7f0a0004; - public static final int selected_conversation=0x7f0a0026; - public static final int server_input_layout=0x7f0a001b; - public static final int slidingpanelayout=0x7f0a0024; - public static final int textSendButton=0x7f0a0022; - public static final int textView1=0x7f0a0014; + public static final int selected_conversation=0x7f0a0020; + public static final int server_input_layout=0x7f0a0015; + public static final int slidingpanelayout=0x7f0a001e; + public static final int textSendButton=0x7f0a001c; + public static final int textView1=0x7f0a000e; public static final int textView2=0x7f0a0001; - public static final int textView3=0x7f0a001c; - public static final int textView4=0x7f0a001e; - public static final int textinput=0x7f0a0021; - public static final int textsend=0x7f0a0020; + public static final int textView3=0x7f0a0016; + public static final int textView4=0x7f0a0018; + public static final int textinput=0x7f0a001b; + public static final int textsend=0x7f0a001a; } public static final class layout { public static final int account_row=0x7f030000; diff --git a/res/layout/activity_new_conversation.xml b/res/layout/activity_new_conversation.xml index 688367616..c993ce491 100644 --- a/res/layout/activity_new_conversation.xml +++ b/res/layout/activity_new_conversation.xml @@ -2,7 +2,7 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - android:background="#f9f9f9"> + android:background="#e5e5e5" > + android:inputType="textEmailAddress" + android:paddingBottom="12dp" + android:paddingLeft="8dp" + android:paddingRight="8dp" + android:paddingTop="12dp" /> + + - + android:layout_below="@+id/contacts_header" + tools:listitem="@layout/contact" > - + - - - - - - - - - - - - - - \ No newline at end of file diff --git a/res/layout/contact.xml b/res/layout/contact.xml index 1b11f0fc5..ec985fcf4 100644 --- a/res/layout/contact.xml +++ b/res/layout/contact.xml @@ -1,12 +1,8 @@ - - - - - \ No newline at end of file + \ No newline at end of file diff --git a/src/de/gultsch/chat/ui/ConversationActivity.java b/src/de/gultsch/chat/ui/ConversationActivity.java index 959e39a9c..90fc1d40c 100644 --- a/src/de/gultsch/chat/ui/ConversationActivity.java +++ b/src/de/gultsch/chat/ui/ConversationActivity.java @@ -129,7 +129,6 @@ public class ConversationActivity extends XmppActivity { }); spl = (SlidingPaneLayout) findViewById(id.slidingpanelayout); spl.setParallaxDistance(150); - spl.openPane(); spl.setShadowResource(R.drawable.es_slidingpane_shadow); spl.setSliderFadeColor(0); spl.setPanelSlideListener(new PanelSlideListener() { @@ -286,6 +285,7 @@ public class ConversationActivity extends XmppActivity { startActivity(new Intent(this, NewConversationActivity.class)); finish(); } else { + spl.openPane(); //find currently loaded fragment ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager().findFragmentByTag("conversation"); if (selectedFragment!=null) { diff --git a/src/de/gultsch/chat/ui/ConversationFragment.java b/src/de/gultsch/chat/ui/ConversationFragment.java index 1ae04ce9f..c3b73ac51 100644 --- a/src/de/gultsch/chat/ui/ConversationFragment.java +++ b/src/de/gultsch/chat/ui/ConversationFragment.java @@ -157,6 +157,9 @@ public class ConversationFragment extends Fragment { // 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(); } int size = this.messageList.size(); @@ -173,6 +176,9 @@ public class ConversationFragment extends Fragment { // 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(); } } } diff --git a/src/de/gultsch/chat/ui/NewConversationActivity.java b/src/de/gultsch/chat/ui/NewConversationActivity.java index e30fcbb86..2725178de 100644 --- a/src/de/gultsch/chat/ui/NewConversationActivity.java +++ b/src/de/gultsch/chat/ui/NewConversationActivity.java @@ -1,13 +1,14 @@ package de.gultsch.chat.ui; -import java.util.LinkedHashMap; -import java.util.Map.Entry; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; import de.gultsch.chat.R; 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 de.gultsch.chat.utils.Validator; import android.os.Bundle; import android.provider.ContactsContract; @@ -18,8 +19,12 @@ import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; -import android.view.View.OnClickListener; -import android.widget.LinearLayout; +import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.AdapterView.OnItemClickListener; +import android.widget.ArrayAdapter; +import android.widget.EditText; +import android.widget.ListView; import android.widget.TextView; import android.widget.ImageView; import android.content.Context; @@ -31,10 +36,53 @@ import android.database.Cursor; public class NewConversationActivity extends XmppActivity { - final protected LinkedHashMap availablePhoneContacts = new LinkedHashMap(); - final protected LinkedHashMap availableJabberContacts = new LinkedHashMap(); - protected View newContactView; - protected Contact newContact; + protected List phoneContacts = new ArrayList(); + protected List rosterContacts = new ArrayList(); + protected List aggregatedContacts = new ArrayList(); + protected ListView contactsView; + protected ArrayAdapter contactsAdapter; + + protected EditText search; + protected String searchString = ""; + private TextView contactsHeader; + + protected void updateAggregatedContacts() { + + aggregatedContacts.clear(); + for (Contact contact : phoneContacts) { + if (contact.match(searchString)) + aggregatedContacts.add(contact); + } + for (Contact contact : rosterContacts) { + if (contact.match(searchString)) + aggregatedContacts.add(contact); + } + + Collections.sort(aggregatedContacts, new Comparator() { + + @Override + public int compare(Contact lhs, Contact rhs) { + return lhs.getDisplayName().compareTo(rhs.getDisplayName()); + } + }); + + if (aggregatedContacts.size() == 0) { + + if (Validator.isValidJid(searchString)) { + String name = searchString.split("@")[0]; + Contact newContact = new Contact(name, searchString, + DEFAULT_PROFILE_PHOTO); + aggregatedContacts.add(newContact); + contactsHeader.setText("Create new contact"); + } else { + contactsHeader.setText("Contacts"); + } + } else { + contactsHeader.setText("Contacts"); + } + + contactsAdapter.notifyDataSetChanged(); + } static final String[] PROJECTION = new String[] { ContactsContract.Data.CONTACT_ID, @@ -48,63 +96,95 @@ public class NewConversationActivity extends XmppActivity { + "\") 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 static final String DEFAULT_PROFILE_PHOTO = "android.resource://de.gultsch.chat/" + + R.drawable.ic_profile; + + @Override + protected void onCreate(Bundle savedInstanceState) { + + super.onCreate(savedInstanceState); + + setContentView(R.layout.activity_new_conversation); + + contactsHeader = (TextView) findViewById(R.id.contacts_header); + + search = (EditText) findViewById(R.id.new_conversation_search); + search.addTextChangedListener(new TextWatcher() { - 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()); - ((ImageView) view.findViewById(R.id.contact_photo)).setImageURI(contact.getProfilePhoto()); - view.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - Contact clickedContact = null; - for(Entry entry : availablePhoneContacts.entrySet()) { - if (entry.getValue() == v) { - clickedContact = entry.getKey(); - break; - } + public void onTextChanged(CharSequence s, int start, int before, + int count) { + searchString = search.getText().toString(); + updateAggregatedContacts(); + } + + @Override + public void afterTextChanged(Editable s) { + // TODO Auto-generated method stub + + } + + @Override + public void beforeTextChanged(CharSequence s, int start, int count, + int after) { + // TODO Auto-generated method stub + + } + }); + + contactsView = (ListView) findViewById(R.id.contactList); + contactsAdapter = new ArrayAdapter(getApplicationContext(), + R.layout.contact, aggregatedContacts) { + @Override + public View getView(int position, View view, ViewGroup parent) { + LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); + if (view == null) { + view = (View) inflater.inflate(R.layout.contact, null); } - for(Entry entry : availableJabberContacts.entrySet()) { - if (entry.getValue() == v) { - clickedContact = entry.getKey(); - break; - } - } - if (newContactView==v) { - clickedContact = newContact; - } - Log.d("gultsch","clicked on "+clickedContact.getDisplayName()); - - + + ((TextView) view.findViewById(R.id.contact_display_name)) + .setText(getItem(position).getDisplayName()); + ((TextView) view.findViewById(R.id.contact_jid)) + .setText(getItem(position).getJid()); + ((ImageView) view.findViewById(R.id.contact_photo)) + .setImageURI(getItem(position).getProfilePhoto()); + return view; + } + }; + contactsView.setAdapter(contactsAdapter); + contactsView.setOnItemClickListener(new OnItemClickListener() { + + @Override + public void onItemClick(AdapterView arg0, View view, int pos, + long arg3) { + Contact clickedContact = aggregatedContacts.get(pos); + Log.d("gultsch", + "clicked on " + clickedContact.getDisplayName()); + Account account = new Account(); - - Conversation conversation = xmppConnectionService.findOrCreateConversation(account, clickedContact); - - Intent viewConversationIntent = new Intent(v.getContext(),ConversationActivity.class); + + Conversation conversation = xmppConnectionService + .findOrCreateConversation(account, clickedContact); + + Intent viewConversationIntent = new Intent(view.getContext(), + ConversationActivity.class); viewConversationIntent.setAction(Intent.ACTION_VIEW); - viewConversationIntent.putExtra(ConversationActivity.CONVERSATION, conversation.getUuid()); - viewConversationIntent.setType(ConversationActivity.VIEW_CONVERSATION); - viewConversationIntent.setFlags(viewConversationIntent.getFlags() | Intent.FLAG_ACTIVITY_CLEAR_TOP); + viewConversationIntent.putExtra( + ConversationActivity.CONVERSATION, + conversation.getUuid()); + viewConversationIntent + .setType(ConversationActivity.VIEW_CONVERSATION); + viewConversationIntent.setFlags(viewConversationIntent + .getFlags() | Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(viewConversationIntent); } }); - return view; } - + @Override - 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); + public void onStart() { + super.onStart(); + CursorLoader mCursorLoader = new CursorLoader(this, ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, null, null); @@ -112,8 +192,10 @@ public class NewConversationActivity extends XmppActivity { @Override public void onLoadComplete(Loader arg0, Cursor cursor) { + phoneContacts.clear(); while (cursor.moveToNext()) { - String profilePhoto = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.PHOTO_THUMBNAIL_URI)); + String profilePhoto = cursor.getString(cursor + .getColumnIndex(ContactsContract.Data.PHOTO_THUMBNAIL_URI)); if (profilePhoto == null) { profilePhoto = DEFAULT_PROFILE_PHOTO; } @@ -122,109 +204,31 @@ public class NewConversationActivity extends XmppActivity { .getColumnIndex(ContactsContract.Data.DISPLAY_NAME)), cursor.getString(cursor .getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA)), - profilePhoto - ); - View contactView = getViewForContact(contact); - availablePhoneContacts.put(contact, getViewForContact(contact)); - ((LinearLayout) findViewById(R.id.phone_contacts)).addView(contactView); + profilePhoto); + phoneContacts.add(contact); } - updateAvailableContacts(); + updateAggregatedContacts(); } }); mCursorLoader.startLoading(); - ((TextView) findViewById(R.id.new_conversation_search)).addTextChangedListener(new TextWatcher() { - - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - updateAvailableContacts(); - } - - @Override - public void afterTextChanged(Editable s) { - // TODO Auto-generated method stub - - } - - @Override - public void beforeTextChanged(CharSequence s, int start, int count, - int after) { - // TODO Auto-generated method stub - - } - }); - - } - - protected void updateAvailableContacts() { - String search = ((TextView) findViewById(R.id.new_conversation_search)).getText().toString(); - - LinearLayout phoneContacts = (LinearLayout) findViewById(R.id.phone_contacts); - filterAvailableContacts(phoneContacts,this.availablePhoneContacts,search); - - if (phoneContacts.getChildCount() == 0) { - findViewById(R.id.phone_contacts_header).setVisibility(View.GONE); - } else { - findViewById(R.id.phone_contacts_header).setVisibility(View.VISIBLE); - } - - LinearLayout jabberContacts = (LinearLayout) findViewById(R.id.jabber_contacts); - filterAvailableContacts(jabberContacts,this.availableJabberContacts,search); - if (jabberContacts.getChildCount() == 0) { - findViewById(R.id.jabber_contacts_header).setVisibility(View.GONE); - } else { - findViewById(R.id.jabber_contacts_header).setVisibility(View.VISIBLE); - } - - LinearLayout createNewContact = (LinearLayout) findViewById(R.id.create_new_contact); - if (Validator.isValidJid(search)) { - createNewContact.removeAllViews(); - String name = search.split("@")[0]; - newContact = new Contact(name,search,DEFAULT_PROFILE_PHOTO); - newContactView = getViewForContact(newContact); - newContactView.findViewById(R.id.contact_divider).setVisibility(View.GONE); - createNewContact.addView(newContactView); - createNewContact.setVisibility(View.VISIBLE); - ((TextView) findViewById(R.id.new_contact_header)).setVisibility(View.VISIBLE); - } else { - createNewContact.setVisibility(View.GONE); - ((TextView) findViewById(R.id.new_contact_header)).setVisibility(View.GONE); - } - } - - private void filterAvailableContacts( - LinearLayout layout, LinkedHashMap contacts, String search) { - layout.removeAllViews(); - for(Entry entry : contacts.entrySet()) { - - if (entry.getKey().match(search)) { - entry.getValue().setVisibility(View.VISIBLE); - entry.getValue().findViewById(R.id.contact_divider).setVisibility(View.VISIBLE); - layout.addView(entry.getValue()); - } - } - int contactsCount = layout.getChildCount(); - if (contactsCount>=1) { - View lastContact = layout.getChildAt(contactsCount - 1); - lastContact.findViewById(R.id.contact_divider).setVisibility(View.GONE); - } } @Override void onBackendConnected() { - if (xmppConnectionService.getConversationCount()==0) { + if (xmppConnectionService.getConversationCount() == 0) { getActionBar().setDisplayHomeAsUpEnabled(false); getActionBar().setHomeButtonEnabled(false); } } - + @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.newconversation, menu); return true; } - + @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { @@ -239,5 +243,5 @@ public class NewConversationActivity extends XmppActivity { } return super.onOptionsItemSelected(item); } - + }