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.R.id;
10import de.gultsch.chat.entities.Contact;
11import de.gultsch.chat.entities.Conversation;
12import de.gultsch.chat.utils.UIHelper;
13import android.net.Uri;
14import android.os.Bundle;
15import android.app.FragmentTransaction;
16import android.app.NotificationManager;
17import android.content.Context;
18import android.content.Intent;
19import android.support.v4.widget.SlidingPaneLayout;
20import android.support.v4.widget.SlidingPaneLayout.PanelSlideListener;
21import android.util.Log;
22import android.view.KeyEvent;
23import android.view.LayoutInflater;
24import android.view.Menu;
25import android.view.MenuItem;
26import android.view.View;
27import android.view.ViewGroup;
28import android.view.inputmethod.InputMethodManager;
29import android.widget.AdapterView;
30import android.widget.AdapterView.OnItemClickListener;
31import android.widget.ArrayAdapter;
32import android.widget.ListView;
33import android.widget.TextView;
34import android.widget.ImageView;
35
36public class ConversationActivity extends XmppActivity {
37
38 public static final String VIEW_CONVERSATION = "viewConversation";
39 public static final String CONVERSATION = "conversationUuid";
40
41 protected SlidingPaneLayout spl;
42
43 private List<Conversation> conversationList = new ArrayList<Conversation>();
44 private Conversation selectedConversation = null;
45 private ListView listView;
46
47 private boolean paneShouldBeOpen = true;
48 private ArrayAdapter<Conversation> listAdapter;
49
50 private OnConversationListChangedListener onConvChanged = new OnConversationListChangedListener() {
51
52 @Override
53 public void onConversationListChanged() {
54 final Conversation currentConv = getSelectedConversation();
55 conversationList.clear();
56 conversationList.addAll(xmppConnectionService
57 .getConversations());
58 runOnUiThread(new Runnable() {
59
60 @Override
61 public void run() {
62 updateConversationList();
63 /*for(int i = 0; i < conversationList.size(); ++i) {
64 if (currentConv == conversationList.get(i)) {
65 selectedConversation = conversationList.get(i);
66 break;
67 }
68 }*/
69 if(paneShouldBeOpen) {
70 selectedConversation = conversationList.get(0);
71 if (conversationList.size() >= 1) {
72 swapConversationFragment();
73 } else {
74 startActivity(new Intent(getApplicationContext(), NewConversationActivity.class));
75 finish();
76 }
77 }
78 ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager().findFragmentByTag("conversation");
79 if (selectedFragment!=null) {
80 selectedFragment.updateMessages();
81 }
82 }
83 });
84 }
85 };
86
87
88 public List<Conversation> getConversationList() {
89 return this.conversationList;
90 }
91
92 public Conversation getSelectedConversation() {
93 return this.selectedConversation;
94 }
95
96 public ListView getConversationListView() {
97 return this.listView;
98 }
99
100 public SlidingPaneLayout getSlidingPaneLayout() {
101 return this.spl;
102 }
103
104 public boolean shouldPaneBeOpen() {
105 return paneShouldBeOpen;
106 }
107
108 public void updateConversationList() {
109 if (conversationList.size() >= 1) {
110 Collections.sort(this.conversationList, new Comparator<Conversation>() {
111 @Override
112 public int compare(Conversation lhs, Conversation rhs) {
113 return (int) (rhs.getLatestMessageDate() - lhs.getLatestMessageDate());
114 }
115 });
116 }
117 this.listView.invalidateViews();
118 }
119
120 @Override
121 protected void onCreate(Bundle savedInstanceState) {
122
123 super.onCreate(savedInstanceState);
124
125 setContentView(R.layout.fragment_conversations_overview);
126
127 listView = (ListView) findViewById(R.id.list);
128
129 this.listAdapter = new ArrayAdapter<Conversation>(this,
130 R.layout.conversation_list_row, conversationList) {
131 @Override
132 public View getView(int position, View view, ViewGroup parent) {
133 if (view == null) {
134 LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
135 view = (View) inflater.inflate(
136 R.layout.conversation_list_row, null);
137 }
138 ((TextView) view.findViewById(R.id.conversation_name))
139 .setText(getItem(position).getName());
140 ((TextView) view.findViewById(R.id.conversation_lastmsg)).setText(getItem(position).getLatestMessage());
141 ((TextView) view.findViewById(R.id.conversation_lastupdate))
142 .setText(UIHelper.readableTimeDifference(getItem(position).getLatestMessageDate()));
143
144 Uri profilePhoto = getItem(position).getProfilePhotoUri();
145 ImageView imageView = (ImageView) view.findViewById(R.id.conversation_image);
146 if (profilePhoto!=null) {
147 imageView.setImageURI(profilePhoto);
148 } else {
149 imageView.setImageBitmap(UIHelper.getUnknownContactPicture(getItem(position).getName(),200));
150 }
151
152
153 ((ImageView) view.findViewById(R.id.conversation_image))
154 .setImageURI(getItem(position).getProfilePhotoUri());
155 return view;
156 }
157
158 };
159
160 listView.setAdapter(this.listAdapter);
161
162 listView.setOnItemClickListener(new OnItemClickListener() {
163
164 @Override
165 public void onItemClick(AdapterView<?> arg0, View clickedView,
166 int position, long arg3) {
167 paneShouldBeOpen = false;
168 if (selectedConversation != conversationList.get(position)) {
169 selectedConversation = conversationList.get(position);
170 swapConversationFragment(); //.onBackendConnected(conversationList.get(position));
171 } else {
172 spl.closePane();
173 }
174 }
175 });
176 spl = (SlidingPaneLayout) findViewById(id.slidingpanelayout);
177 spl.setParallaxDistance(150);
178 spl.setShadowResource(R.drawable.es_slidingpane_shadow);
179 spl.setSliderFadeColor(0);
180 spl.setPanelSlideListener(new PanelSlideListener() {
181
182 @Override
183 public void onPanelOpened(View arg0) {
184 paneShouldBeOpen = true;
185 getActionBar().setDisplayHomeAsUpEnabled(false);
186 getActionBar().setTitle(R.string.app_name);
187 invalidateOptionsMenu();
188
189 InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
190
191 View focus = getCurrentFocus();
192
193 if (focus != null) {
194
195 inputManager.hideSoftInputFromWindow(
196 focus.getWindowToken(),
197 InputMethodManager.HIDE_NOT_ALWAYS);
198 }
199 }
200
201 @Override
202 public void onPanelClosed(View arg0) {
203 paneShouldBeOpen = false;
204 if (conversationList.size() > 0) {
205 getActionBar().setDisplayHomeAsUpEnabled(true);
206 getActionBar().setTitle(getSelectedConversation().getName());
207 invalidateOptionsMenu();
208 }
209 }
210
211 @Override
212 public void onPanelSlide(View arg0, float arg1) {
213 // TODO Auto-generated method stub
214
215 }
216 });
217 }
218
219 @Override
220 public boolean onCreateOptionsMenu(Menu menu) {
221 getMenuInflater().inflate(R.menu.conversations, menu);
222
223 if (spl.isOpen()) {
224 ((MenuItem) menu.findItem(R.id.action_archive)).setVisible(false);
225 ((MenuItem) menu.findItem(R.id.action_details)).setVisible(false);
226 ((MenuItem) menu.findItem(R.id.action_security)).setVisible(false);
227 } else {
228 ((MenuItem) menu.findItem(R.id.action_add)).setVisible(false);
229 if (this.getSelectedConversation()!=null) {
230 if (this.getSelectedConversation().getMode() == Conversation.MODE_MULTI) {
231 ((MenuItem) menu.findItem(R.id.action_security)).setVisible(false);
232 }
233 }
234 }
235 return true;
236 }
237
238 @Override
239 public boolean onOptionsItemSelected(MenuItem item) {
240 switch (item.getItemId()) {
241 case android.R.id.home:
242 spl.openPane();
243 break;
244 case R.id.action_settings:
245 startActivity(new Intent(this, SettingsActivity.class));
246 break;
247 case R.id.action_accounts:
248 startActivity(new Intent(this, ManageAccountActivity.class));
249 break;
250 case R.id.action_add:
251 startActivity(new Intent(this, NewConversationActivity.class));
252 break;
253 case R.id.action_archive:
254 Conversation conv = getSelectedConversation();
255 conv.setStatus(Conversation.STATUS_ARCHIVED);
256 paneShouldBeOpen = true;
257 spl.openPane();
258 xmppConnectionService.archiveConversation(conv);
259 selectedConversation = conversationList.get(0);
260 break;
261 case R.id.action_details:
262 DialogContactDetails details = new DialogContactDetails();
263 Contact contact = xmppConnectionService.findOrCreateContact(this.getSelectedConversation().getAccount(),this.getSelectedConversation().getContactJid());
264 details.setContact(contact);
265 details.show(getFragmentManager(), "details");
266 break;
267 default:
268 break;
269 }
270 return super.onOptionsItemSelected(item);
271 }
272
273 protected ConversationFragment swapConversationFragment() {
274 ConversationFragment selectedFragment = new ConversationFragment();
275
276 FragmentTransaction transaction = getFragmentManager()
277 .beginTransaction();
278 transaction.replace(R.id.selected_conversation, selectedFragment,"conversation");
279 transaction.commit();
280 return selectedFragment;
281 }
282
283 @Override
284 public boolean onKeyDown(int keyCode, KeyEvent event) {
285 if (keyCode == KeyEvent.KEYCODE_BACK) {
286 if (!spl.isOpen()) {
287 spl.openPane();
288 return false;
289 }
290 }
291 return super.onKeyDown(keyCode, event);
292 }
293
294 public void onStart() {
295 super.onStart();
296 NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
297 nm.cancelAll();
298 if (conversationList.size()>=1) {
299 onConvChanged.onConversationListChanged();
300 }
301 }
302
303 /*@Override
304 protected void onPause() {
305 super.onPause();
306 if (xmppConnectionServiceBound) {
307 xmppConnectionService.removeOnConversationListChangedListener();
308 unbindService(mConnection);
309 xmppConnectionServiceBound = false;
310 }
311 }*/
312
313 @Override
314 protected void onStop() {
315 Log.d("gultsch","called on stop in conversation activity");
316 if (xmppConnectionServiceBound) {
317 xmppConnectionService.removeOnConversationListChangedListener();
318 }
319 super.onStop();
320 }
321
322 @Override
323 void onBackendConnected() {
324
325 xmppConnectionService.setOnConversationListChangedListener(this.onConvChanged);
326
327 if (conversationList.size()==0) {
328 conversationList.clear();
329 conversationList.addAll(xmppConnectionService
330 .getConversations());
331
332 for(Conversation conversation : conversationList) {
333 conversation.setMessages(xmppConnectionService.getMessages(conversation));
334 }
335
336 this.updateConversationList();
337 }
338
339 if ((getIntent().getAction().equals(Intent.ACTION_VIEW) && (!handledViewIntent))) {
340 if (getIntent().getType().equals(
341 ConversationActivity.VIEW_CONVERSATION)) {
342 handledViewIntent = true;
343
344 String convToView = (String) getIntent().getExtras().get(CONVERSATION);
345
346 for(int i = 0; i < conversationList.size(); ++i) {
347 if (conversationList.get(i).getUuid().equals(convToView)) {
348 selectedConversation = conversationList.get(i);
349 }
350 }
351 paneShouldBeOpen = false;
352 swapConversationFragment();
353 }
354 } else {
355 if (xmppConnectionService.getAccounts().size() == 0) {
356 startActivity(new Intent(this, ManageAccountActivity.class));
357 finish();
358 } else if (conversationList.size() <= 0) {
359 //add no history
360 startActivity(new Intent(this, NewConversationActivity.class));
361 finish();
362 } else {
363 spl.openPane();
364 //find currently loaded fragment
365 ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager().findFragmentByTag("conversation");
366 if (selectedFragment!=null) {
367 Log.d("gultsch","ConversationActivity. found old fragment.");
368 selectedFragment.onBackendConnected();
369 } else {
370 Log.d("gultsch","conversationactivity. no old fragment found. creating new one");
371 selectedConversation = conversationList.get(0);
372 Log.d("gultsch","selected conversation is #"+selectedConversation);
373 swapConversationFragment();
374 }
375 }
376 }
377 }
378}