aboutsummaryrefslogtreecommitdiffstats
path: root/src/de/gultsch/chat/ui/NewConversationActivity.java
blob: e30fcbb862ec0b3a672bdc1216d8ff5b97a3073f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package de.gultsch.chat.ui;

import java.util.LinkedHashMap;
import java.util.Map.Entry;

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;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
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.widget.TextView;
import android.widget.ImageView;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.content.Loader.OnLoadCompleteListener;
import android.database.Cursor;

public class NewConversationActivity extends XmppActivity {

	final protected LinkedHashMap<Contact, View> availablePhoneContacts = new LinkedHashMap<Contact, View>();
	final protected LinkedHashMap<Contact, View> availableJabberContacts = new LinkedHashMap<Contact, View>();
	protected View newContactView;
	protected Contact newContact;

	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
			+ "\")";
	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());
		((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<Contact, View> entry  : availablePhoneContacts.entrySet()) {
					if (entry.getValue() == v) {
						clickedContact = entry.getKey();
						break;
					}
				}
				for(Entry<Contact, View> entry  : availableJabberContacts.entrySet()) {
					if (entry.getValue() == v) {
						clickedContact = entry.getKey();
						break;
					}
				}
				if (newContactView==v) {
					clickedContact = newContact;
				}
				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);
				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);
			}
		});
		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);
		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) {
				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)),
									profilePhoto
									);
					View contactView = getViewForContact(contact);
					availablePhoneContacts.put(contact, getViewForContact(contact));
					((LinearLayout) findViewById(R.id.phone_contacts)).addView(contactView);
				}
				updateAvailableContacts();
			}
		});
		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<Contact, View> contacts, String search) {
		layout.removeAllViews();
		for(Entry<Contact, View> 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) {
			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()) {
		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);
	}
	
}