NewConversationActivity.java

  1package de.gultsch.chat.ui;
  2
  3import java.util.ArrayList;
  4import java.util.Collections;
  5import java.util.Comparator;
  6import java.util.List;
  7
  8import de.gultsch.chat.R;
  9import de.gultsch.chat.entities.Account;
 10import de.gultsch.chat.entities.Contact;
 11import de.gultsch.chat.entities.Conversation;
 12import de.gultsch.chat.utils.UIHelper;
 13import de.gultsch.chat.utils.Validator;
 14import android.net.Uri;
 15import android.os.Bundle;
 16import android.provider.ContactsContract;
 17import android.text.Editable;
 18import android.text.TextWatcher;
 19import android.util.Log;
 20import android.view.LayoutInflater;
 21import android.view.Menu;
 22import android.view.MenuItem;
 23import android.view.View;
 24import android.view.ViewGroup;
 25import android.widget.AdapterView;
 26import android.widget.AdapterView.OnItemClickListener;
 27import android.widget.ArrayAdapter;
 28import android.widget.EditText;
 29import android.widget.ListView;
 30import android.widget.ProgressBar;
 31import android.widget.TextView;
 32import android.widget.ImageView;
 33import android.annotation.SuppressLint;
 34import android.app.Activity;
 35import android.app.AlertDialog;
 36import android.content.Context;
 37import android.content.CursorLoader;
 38import android.content.DialogInterface;
 39import android.content.DialogInterface.OnClickListener;
 40import android.content.Intent;
 41import android.content.Loader;
 42import android.content.Loader.OnLoadCompleteListener;
 43import android.database.Cursor;
 44
 45public class NewConversationActivity extends XmppActivity {
 46
 47	protected List<Contact> phoneContacts = new ArrayList<Contact>();
 48	protected List<Contact> rosterContacts = new ArrayList<Contact>();
 49	protected List<Contact> aggregatedContacts = new ArrayList<Contact>();
 50	protected ListView contactsView;
 51	protected ArrayAdapter<Contact> contactsAdapter;
 52
 53	protected EditText search;
 54	protected String searchString = "";
 55	private TextView contactsHeader;
 56	private List<Account> accounts;
 57
 58	protected void updateAggregatedContacts() {
 59
 60		aggregatedContacts.clear();
 61		for (Contact contact : rosterContacts) {
 62			if (contact.match(searchString))
 63				aggregatedContacts.add(contact);
 64		}
 65
 66		Collections.sort(aggregatedContacts, new Comparator<Contact>() {
 67
 68			@SuppressLint("DefaultLocale")
 69			@Override
 70			public int compare(Contact lhs, Contact rhs) {
 71				return lhs.getDisplayName().toLowerCase()
 72						.compareTo(rhs.getDisplayName().toLowerCase());
 73			}
 74		});
 75
 76		if (aggregatedContacts.size() == 0) {
 77
 78			if (Validator.isValidJid(searchString)) {
 79				String name = searchString.split("@")[0];
 80				Contact newContact = new Contact(null, name, searchString, null);
 81				aggregatedContacts.add(newContact);
 82				contactsHeader.setText("Create new contact");
 83			} else {
 84				contactsHeader.setText("Contacts");
 85			}
 86		} else {
 87			contactsHeader.setText("Contacts");
 88		}
 89
 90		contactsAdapter.notifyDataSetChanged();
 91		contactsView.setScrollX(0);
 92	}
 93
 94	@Override
 95	protected void onCreate(Bundle savedInstanceState) {
 96
 97		super.onCreate(savedInstanceState);
 98
 99		setContentView(R.layout.activity_new_conversation);
100
101		contactsHeader = (TextView) findViewById(R.id.contacts_header);
102
103		search = (EditText) findViewById(R.id.new_conversation_search);
104		search.addTextChangedListener(new TextWatcher() {
105
106			@Override
107			public void onTextChanged(CharSequence s, int start, int before,
108					int count) {
109				searchString = search.getText().toString();
110				updateAggregatedContacts();
111			}
112
113			@Override
114			public void afterTextChanged(Editable s) {
115				// TODO Auto-generated method stub
116
117			}
118
119			@Override
120			public void beforeTextChanged(CharSequence s, int start, int count,
121					int after) {
122				// TODO Auto-generated method stub
123
124			}
125		});
126
127		contactsView = (ListView) findViewById(R.id.contactList);
128		contactsAdapter = new ArrayAdapter<Contact>(getApplicationContext(),
129				R.layout.contact, aggregatedContacts) {
130			@Override
131			public View getView(int position, View view, ViewGroup parent) {
132				LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
133				if (view == null) {
134					view = (View) inflater.inflate(R.layout.contact, null);
135				}
136
137				((TextView) view.findViewById(R.id.contact_display_name))
138						.setText(getItem(position).getDisplayName());
139				((TextView) view.findViewById(R.id.contact_jid))
140						.setText(getItem(position).getJid());
141				String profilePhoto = getItem(position).getProfilePhoto();
142				ImageView imageView = (ImageView) view
143						.findViewById(R.id.contact_photo);
144				if (profilePhoto != null) {
145					imageView.setImageURI(Uri.parse(profilePhoto));
146				} else {
147					imageView.setImageBitmap(UIHelper.getUnknownContactPicture(
148							getItem(position).getDisplayName(), 90));
149				}
150				return view;
151			}
152		};
153		contactsView.setAdapter(contactsAdapter);
154		final Activity activity = this;
155		contactsView.setOnItemClickListener(new OnItemClickListener() {
156
157			@Override
158			public void onItemClick(AdapterView<?> arg0, final View view,
159					int pos, long arg3) {
160				final Contact clickedContact = aggregatedContacts.get(pos);
161				Log.d("gultsch",
162						"clicked on " + clickedContact.getDisplayName());
163
164				final List<Account> accounts = xmppConnectionService
165						.getAccounts();
166				if (accounts.size() == 1) {
167					startConversation(clickedContact, accounts.get(0));
168				} else {
169					String[] accountList = new String[accounts.size()];
170					for (int i = 0; i < accounts.size(); ++i) {
171						accountList[i] = accounts.get(i).getJid();
172					}
173
174					AlertDialog.Builder builder = new AlertDialog.Builder(
175							activity);
176					builder.setTitle("Choose account");
177					builder.setItems(accountList, new OnClickListener() {
178
179						@Override
180						public void onClick(DialogInterface dialog, int which) {
181							Account account = accounts.get(which);
182							startConversation(clickedContact, account);
183						}
184					});
185					builder.create().show();
186				}
187			}
188		});
189	}
190
191	public void startConversation(Contact contact, Account account) {
192		Conversation conversation = xmppConnectionService
193				.findOrCreateConversation(account, contact);
194
195		Intent viewConversationIntent = new Intent(this,
196				ConversationActivity.class);
197		viewConversationIntent.setAction(Intent.ACTION_VIEW);
198		viewConversationIntent.putExtra(ConversationActivity.CONVERSATION,
199				conversation.getUuid());
200		viewConversationIntent.setType(ConversationActivity.VIEW_CONVERSATION);
201		viewConversationIntent.setFlags(viewConversationIntent.getFlags()
202				| Intent.FLAG_ACTIVITY_CLEAR_TOP);
203		startActivity(viewConversationIntent);
204	}
205
206	@Override
207	void onBackendConnected() {
208		if (xmppConnectionService.getConversationCount() == 0) {
209			getActionBar().setDisplayHomeAsUpEnabled(false);
210			getActionBar().setHomeButtonEnabled(false);
211		}
212		this.accounts = xmppConnectionService.getAccounts();
213		this.rosterContacts.clear();
214		for(int i = 0; i < accounts.size(); ++i) {
215				if (accounts.get(i).getStatus()==Account.STATUS_ONLINE) {
216				xmppConnectionService.getRoster(accounts.get(i),new OnRosterFetchedListener() {
217		
218					@Override
219					public void onRosterFetched(List<Contact> roster) {
220						rosterContacts.addAll(roster);
221						runOnUiThread(new Runnable() {
222		
223							@Override
224							public void run() {
225								updateAggregatedContacts();
226							}
227						});
228		
229					}
230				});
231				}
232		}
233	}
234
235	@Override
236	public boolean onCreateOptionsMenu(Menu menu) {
237		// Inflate the menu; this adds items to the action bar if it is present.
238		getMenuInflater().inflate(R.menu.newconversation, menu);
239		return true;
240	}
241
242	@Override
243	public boolean onOptionsItemSelected(MenuItem item) {
244		switch (item.getItemId()) {
245		case R.id.action_settings:
246			startActivity(new Intent(this, SettingsActivity.class));
247			break;
248		case R.id.action_accounts:
249			startActivity(new Intent(this, ManageAccountActivity.class));
250			break;
251		case R.id.action_refresh_contacts:
252			refreshContacts();
253			break;
254		default:
255			break;
256		}
257		return super.onOptionsItemSelected(item);
258	}
259
260	private void refreshContacts() {
261		final ProgressBar progress = (ProgressBar) findViewById(R.id.progressBar1);
262		final EditText searchBar = (EditText) findViewById(R.id.new_conversation_search);
263		final TextView contactsHeader = (TextView) findViewById(R.id.contacts_header);
264		final ListView contactList = (ListView) findViewById(R.id.contactList);
265		searchBar.setVisibility(View.GONE);
266		contactsHeader.setVisibility(View.GONE);
267		contactList.setVisibility(View.GONE);
268		progress.setVisibility(View.VISIBLE);
269		this.accounts = xmppConnectionService.getAccounts();
270		this.rosterContacts.clear();
271		for (int i = 0; i < accounts.size(); ++i) {
272			if (accounts.get(i).getStatus()==Account.STATUS_ONLINE) {
273			xmppConnectionService.updateRoster(accounts.get(i),
274					new OnRosterFetchedListener() {
275
276						@Override
277						public void onRosterFetched(final List<Contact> roster) {
278							runOnUiThread(new Runnable() {
279
280								@Override
281								public void run() {
282									rosterContacts.addAll(roster);
283									progress.setVisibility(View.GONE);
284									searchBar.setVisibility(View.VISIBLE);
285									contactList.setVisibility(View.VISIBLE);
286									contactList.setVisibility(View.VISIBLE);
287									updateAggregatedContacts();
288								}
289							});
290						}
291					});
292			}
293		}
294	}
295}