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