2014-01-24 01:04:05 +00:00
|
|
|
package de.gultsch.chat.ui;
|
|
|
|
|
2014-01-28 21:21:08 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Comparator;
|
|
|
|
import java.util.List;
|
2014-01-24 01:04:05 +00:00
|
|
|
|
|
|
|
import de.gultsch.chat.R;
|
2014-01-25 18:33:12 +00:00
|
|
|
import de.gultsch.chat.entities.Account;
|
2014-01-24 22:58:51 +00:00
|
|
|
import de.gultsch.chat.entities.Contact;
|
2014-01-25 18:33:12 +00:00
|
|
|
import de.gultsch.chat.entities.Conversation;
|
2014-02-01 00:25:56 +00:00
|
|
|
import de.gultsch.chat.utils.Beautifier;
|
2014-01-28 18:21:54 +00:00
|
|
|
import de.gultsch.chat.utils.Validator;
|
2014-02-01 00:25:56 +00:00
|
|
|
import android.net.Uri;
|
2014-01-24 01:04:05 +00:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.provider.ContactsContract;
|
|
|
|
import android.text.Editable;
|
|
|
|
import android.text.TextWatcher;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.LayoutInflater;
|
2014-01-25 18:33:12 +00:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
2014-01-24 01:04:05 +00:00
|
|
|
import android.view.View;
|
2014-01-28 21:21:08 +00:00
|
|
|
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;
|
2014-01-24 01:04:05 +00:00
|
|
|
import android.widget.TextView;
|
|
|
|
import android.widget.ImageView;
|
2014-01-28 22:15:30 +00:00
|
|
|
import android.app.Activity;
|
|
|
|
import android.app.AlertDialog;
|
2014-01-24 01:04:05 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.CursorLoader;
|
2014-01-28 22:15:30 +00:00
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.content.DialogInterface.OnClickListener;
|
2014-01-24 01:04:05 +00:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.Loader;
|
|
|
|
import android.content.Loader.OnLoadCompleteListener;
|
|
|
|
import android.database.Cursor;
|
|
|
|
|
2014-01-25 18:33:12 +00:00
|
|
|
public class NewConversationActivity extends XmppActivity {
|
2014-01-24 01:04:05 +00:00
|
|
|
|
2014-01-28 21:21:08 +00:00
|
|
|
protected List<Contact> phoneContacts = new ArrayList<Contact>();
|
|
|
|
protected List<Contact> rosterContacts = new ArrayList<Contact>();
|
|
|
|
protected List<Contact> aggregatedContacts = new ArrayList<Contact>();
|
|
|
|
protected ListView contactsView;
|
|
|
|
protected ArrayAdapter<Contact> 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<Contact>() {
|
|
|
|
|
|
|
|
@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];
|
2014-02-01 00:25:56 +00:00
|
|
|
Contact newContact = new Contact(name, searchString,null);
|
2014-01-28 21:21:08 +00:00
|
|
|
aggregatedContacts.add(newContact);
|
|
|
|
contactsHeader.setText("Create new contact");
|
|
|
|
} else {
|
|
|
|
contactsHeader.setText("Contacts");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
contactsHeader.setText("Contacts");
|
|
|
|
}
|
|
|
|
|
|
|
|
contactsAdapter.notifyDataSetChanged();
|
|
|
|
}
|
2014-01-24 01:04:05 +00:00
|
|
|
|
|
|
|
static final String[] PROJECTION = new String[] {
|
|
|
|
ContactsContract.Data.CONTACT_ID,
|
|
|
|
ContactsContract.Data.DISPLAY_NAME,
|
|
|
|
ContactsContract.Data.PHOTO_THUMBNAIL_URI,
|
|
|
|
ContactsContract.CommonDataKinds.Im.DATA };
|
|
|
|
|
|
|
|
// This is the select criteria
|
|
|
|
static final String SELECTION = "(" + ContactsContract.Data.MIMETYPE
|
|
|
|
+ "=\"" + ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE
|
|
|
|
+ "\") AND (" + ContactsContract.CommonDataKinds.Im.PROTOCOL
|
|
|
|
+ "=\"" + ContactsContract.CommonDataKinds.Im.PROTOCOL_JABBER
|
|
|
|
+ "\")";
|
2014-01-28 21:21:08 +00:00
|
|
|
|
|
|
|
@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() {
|
|
|
|
|
2014-01-24 01:04:05 +00:00
|
|
|
@Override
|
2014-01-28 21:21:08 +00:00
|
|
|
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<Contact>(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);
|
2014-01-24 01:04:05 +00:00
|
|
|
}
|
2014-01-28 21:21:08 +00:00
|
|
|
|
|
|
|
((TextView) view.findViewById(R.id.contact_display_name))
|
|
|
|
.setText(getItem(position).getDisplayName());
|
|
|
|
((TextView) view.findViewById(R.id.contact_jid))
|
|
|
|
.setText(getItem(position).getJid());
|
2014-02-01 00:25:56 +00:00
|
|
|
String profilePhoto = getItem(position).getProfilePhoto();
|
|
|
|
ImageView imageView = (ImageView) view.findViewById(R.id.contact_photo);
|
|
|
|
if (profilePhoto!=null) {
|
|
|
|
imageView.setImageURI(Uri.parse(profilePhoto));
|
|
|
|
} else {
|
|
|
|
imageView.setImageBitmap(Beautifier.getUnknownContactPicture(getItem(position).getDisplayName(),90));
|
|
|
|
}
|
2014-01-28 21:21:08 +00:00
|
|
|
return view;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
contactsView.setAdapter(contactsAdapter);
|
2014-01-28 22:15:30 +00:00
|
|
|
final Activity activity = this;
|
2014-01-28 21:21:08 +00:00
|
|
|
contactsView.setOnItemClickListener(new OnItemClickListener() {
|
|
|
|
|
|
|
|
@Override
|
2014-01-28 22:15:30 +00:00
|
|
|
public void onItemClick(AdapterView<?> arg0, final View view, int pos,
|
2014-01-28 21:21:08 +00:00
|
|
|
long arg3) {
|
2014-01-28 22:15:30 +00:00
|
|
|
final Contact clickedContact = aggregatedContacts.get(pos);
|
2014-01-28 21:21:08 +00:00
|
|
|
Log.d("gultsch",
|
|
|
|
"clicked on " + clickedContact.getDisplayName());
|
2014-01-28 22:15:30 +00:00
|
|
|
|
|
|
|
final List<Account> accounts = xmppConnectionService.getAccounts();
|
|
|
|
if (accounts.size() == 1) {
|
|
|
|
startConversation(clickedContact, accounts.get(0));
|
|
|
|
} else {
|
|
|
|
String[] accountList = new String[accounts.size()];
|
|
|
|
for(int i = 0; i < accounts.size(); ++i) {
|
|
|
|
accountList[i] = accounts.get(i).getJid();
|
|
|
|
}
|
|
|
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
|
|
|
|
builder.setTitle("Choose account");
|
|
|
|
builder.setSingleChoiceItems(accountList,0,new OnClickListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
Account account = accounts.get(which);
|
|
|
|
startConversation(clickedContact, account);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
builder.create().show();
|
|
|
|
}
|
2014-01-24 01:04:05 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-01-28 22:15:30 +00:00
|
|
|
|
|
|
|
public void startConversation(Contact contact, Account account) {
|
|
|
|
Conversation conversation = xmppConnectionService
|
|
|
|
.findOrCreateConversation(account, contact);
|
|
|
|
|
|
|
|
Intent viewConversationIntent = new Intent(this,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);
|
|
|
|
startActivity(viewConversationIntent);
|
|
|
|
}
|
2014-01-28 21:21:08 +00:00
|
|
|
|
2014-01-24 01:04:05 +00:00
|
|
|
@Override
|
2014-01-28 21:21:08 +00:00
|
|
|
public void onStart() {
|
|
|
|
super.onStart();
|
|
|
|
|
2014-01-24 01:04:05 +00:00
|
|
|
CursorLoader mCursorLoader = new CursorLoader(this,
|
|
|
|
ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, null,
|
|
|
|
null);
|
|
|
|
mCursorLoader.registerListener(0, new OnLoadCompleteListener<Cursor>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLoadComplete(Loader<Cursor> arg0, Cursor cursor) {
|
2014-01-28 21:21:08 +00:00
|
|
|
phoneContacts.clear();
|
2014-01-24 01:04:05 +00:00
|
|
|
while (cursor.moveToNext()) {
|
2014-01-28 21:21:08 +00:00
|
|
|
String profilePhoto = cursor.getString(cursor
|
|
|
|
.getColumnIndex(ContactsContract.Data.PHOTO_THUMBNAIL_URI));
|
2014-02-01 00:25:56 +00:00
|
|
|
/*if (profilePhoto == null) {
|
2014-01-24 22:58:51 +00:00
|
|
|
profilePhoto = DEFAULT_PROFILE_PHOTO;
|
2014-02-01 00:25:56 +00:00
|
|
|
}*/
|
2014-01-24 01:04:05 +00:00
|
|
|
Contact contact = new Contact(
|
|
|
|
cursor.getString(cursor
|
|
|
|
.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)),
|
|
|
|
cursor.getString(cursor
|
|
|
|
.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA)),
|
2014-01-28 21:21:08 +00:00
|
|
|
profilePhoto);
|
|
|
|
phoneContacts.add(contact);
|
2014-01-24 01:04:05 +00:00
|
|
|
}
|
2014-01-28 21:21:08 +00:00
|
|
|
updateAggregatedContacts();
|
2014-01-24 01:04:05 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
mCursorLoader.startLoading();
|
|
|
|
|
|
|
|
}
|
2014-01-25 18:33:12 +00:00
|
|
|
|
|
|
|
@Override
|
2014-01-27 19:40:42 +00:00
|
|
|
void onBackendConnected() {
|
2014-01-28 21:21:08 +00:00
|
|
|
if (xmppConnectionService.getConversationCount() == 0) {
|
2014-01-28 18:21:54 +00:00
|
|
|
getActionBar().setDisplayHomeAsUpEnabled(false);
|
|
|
|
getActionBar().setHomeButtonEnabled(false);
|
|
|
|
}
|
2014-01-25 18:33:12 +00:00
|
|
|
}
|
2014-01-28 21:21:08 +00:00
|
|
|
|
2014-01-25 18:33:12 +00:00
|
|
|
@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;
|
|
|
|
}
|
2014-01-28 21:21:08 +00:00
|
|
|
|
2014-01-25 18:33:12 +00:00
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case R.id.action_settings:
|
|
|
|
startActivity(new Intent(this, SettingsActivity.class));
|
|
|
|
break;
|
|
|
|
case R.id.action_accounts:
|
|
|
|
startActivity(new Intent(this, ManageAccountActivity.class));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
2014-01-28 21:21:08 +00:00
|
|
|
|
2014-01-24 01:04:05 +00:00
|
|
|
}
|