1package eu.siacs.conversations.ui;
2
3import java.util.ArrayList;
4import java.util.Collections;
5import java.util.Comparator;
6import java.util.List;
7
8import eu.siacs.conversations.R;
9import eu.siacs.conversations.entities.Account;
10import eu.siacs.conversations.entities.Contact;
11import eu.siacs.conversations.entities.Conversation;
12import eu.siacs.conversations.entities.Message;
13import eu.siacs.conversations.utils.UIHelper;
14import android.net.Uri;
15import android.os.Bundle;
16import android.app.AlertDialog;
17import android.app.FragmentTransaction;
18import android.app.NotificationManager;
19import android.content.Context;
20import android.content.DialogInterface;
21import android.content.Intent;
22import android.graphics.Color;
23import android.graphics.Typeface;
24import android.support.v4.widget.SlidingPaneLayout;
25import android.support.v4.widget.SlidingPaneLayout.PanelSlideListener;
26import android.util.Log;
27import android.view.KeyEvent;
28import android.view.LayoutInflater;
29import android.view.Menu;
30import android.view.MenuItem;
31import android.view.View;
32import android.view.ViewGroup;
33import android.widget.AdapterView;
34import android.widget.AdapterView.OnItemClickListener;
35import android.widget.ArrayAdapter;
36import android.widget.ListView;
37import android.widget.PopupMenu;
38import android.widget.PopupMenu.OnMenuItemClickListener;
39import android.widget.TextView;
40import android.widget.ImageView;
41
42public class ConversationActivity extends XmppActivity {
43
44 public static final String VIEW_CONVERSATION = "viewConversation";
45 public static final String CONVERSATION = "conversationUuid";
46
47 public static final int REQUEST_SEND_MESSAGE = 0x75441;
48 public static final int REQUEST_DECRYPT_PGP = 0x76783;
49
50 protected SlidingPaneLayout spl;
51
52 private List<Conversation> conversationList = new ArrayList<Conversation>();
53 private Conversation selectedConversation = null;
54 private ListView listView;
55
56 private boolean paneShouldBeOpen = true;
57 private ArrayAdapter<Conversation> listAdapter;
58
59 private OnConversationListChangedListener onConvChanged = new OnConversationListChangedListener() {
60
61 @Override
62 public void onConversationListChanged() {
63 conversationList.clear();
64 conversationList.addAll(xmppConnectionService
65 .getConversations());
66 runOnUiThread(new Runnable() {
67
68 @Override
69 public void run() {
70 updateConversationList();
71 if(paneShouldBeOpen) {
72 if (conversationList.size() >= 1) {
73 swapConversationFragment();
74 } else {
75 startActivity(new Intent(getApplicationContext(), NewConversationActivity.class));
76 finish();
77 }
78 }
79 ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager().findFragmentByTag("conversation");
80 if (selectedFragment!=null) {
81 selectedFragment.updateMessages();
82 }
83 }
84 });
85 }
86 };
87
88 private DialogInterface.OnClickListener addToRoster = new DialogInterface.OnClickListener() {
89
90 @Override
91 public void onClick(DialogInterface dialog, int which) {
92 String jid = getSelectedConversation().getContactJid();
93 Account account = getSelectedConversation().getAccount();
94 String name = jid.split("@")[0];
95 Contact contact = new Contact(account, name, jid, null);
96 xmppConnectionService.createContact(contact);
97 }
98 };
99
100 public List<Conversation> getConversationList() {
101 return this.conversationList;
102 }
103
104 public Conversation getSelectedConversation() {
105 return this.selectedConversation;
106 }
107
108 public ListView getConversationListView() {
109 return this.listView;
110 }
111
112 public SlidingPaneLayout getSlidingPaneLayout() {
113 return this.spl;
114 }
115
116 public boolean shouldPaneBeOpen() {
117 return paneShouldBeOpen;
118 }
119
120 public void updateConversationList() {
121 if (conversationList.size() >= 1) {
122 Collections.sort(this.conversationList, new Comparator<Conversation>() {
123 @Override
124 public int compare(Conversation lhs, Conversation rhs) {
125 return (int) (rhs.getLatestMessage().getTimeSent() - lhs.getLatestMessage().getTimeSent());
126 }
127 });
128 }
129 this.listView.invalidateViews();
130 }
131
132 @Override
133 protected void onCreate(Bundle savedInstanceState) {
134
135 super.onCreate(savedInstanceState);
136
137 setContentView(R.layout.fragment_conversations_overview);
138
139 listView = (ListView) findViewById(R.id.list);
140
141 this.listAdapter = new ArrayAdapter<Conversation>(this,
142 R.layout.conversation_list_row, conversationList) {
143 @Override
144 public View getView(int position, View view, ViewGroup parent) {
145 if (view == null) {
146 LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
147 view = (View) inflater.inflate(
148 R.layout.conversation_list_row, null);
149 }
150 Conversation conv;
151 if (conversationList.size() > position) {
152 conv = getItem(position);
153 } else {
154 return view;
155 }
156 if (!spl.isSlideable()) {
157 if (conv==getSelectedConversation()) {
158 view.setBackgroundColor(0xffdddddd);
159 } else {
160 view.setBackgroundColor(Color.TRANSPARENT);
161 }
162 } else {
163 view.setBackgroundColor(Color.TRANSPARENT);
164 }
165 TextView convName = (TextView) view.findViewById(R.id.conversation_name);
166 convName.setText(conv.getName());
167 TextView convLastMsg = (TextView) view.findViewById(R.id.conversation_lastmsg);
168 convLastMsg.setText(conv.getLatestMessage().getBody());
169
170 if(!conv.isRead()) {
171 convName.setTypeface(null,Typeface.BOLD);
172 convLastMsg.setTypeface(null,Typeface.BOLD);
173 } else {
174 convName.setTypeface(null,Typeface.NORMAL);
175 convLastMsg.setTypeface(null,Typeface.NORMAL);
176 }
177
178 ((TextView) view.findViewById(R.id.conversation_lastupdate))
179 .setText(UIHelper.readableTimeDifference(conv.getLatestMessage().getTimeSent()));
180
181 Uri profilePhoto = conv.getProfilePhotoUri();
182 ImageView imageView = (ImageView) view.findViewById(R.id.conversation_image);
183 if (profilePhoto!=null) {
184 imageView.setImageURI(profilePhoto);
185 } else {
186 imageView.setImageBitmap(UIHelper.getUnknownContactPicture(getItem(position).getName(),200));
187 }
188
189
190 ((ImageView) view.findViewById(R.id.conversation_image))
191 .setImageURI(conv.getProfilePhotoUri());
192 return view;
193 }
194
195 };
196
197 listView.setAdapter(this.listAdapter);
198
199 listView.setOnItemClickListener(new OnItemClickListener() {
200
201 @Override
202 public void onItemClick(AdapterView<?> arg0, View clickedView,
203 int position, long arg3) {
204 paneShouldBeOpen = false;
205 if (selectedConversation != conversationList.get(position)) {
206 selectedConversation = conversationList.get(position);
207 swapConversationFragment(); //.onBackendConnected(conversationList.get(position));
208 } else {
209 spl.closePane();
210 }
211 }
212 });
213 spl = (SlidingPaneLayout) findViewById(R.id.slidingpanelayout);
214 spl.setParallaxDistance(150);
215 spl.setShadowResource(R.drawable.es_slidingpane_shadow);
216 spl.setSliderFadeColor(0);
217 spl.setPanelSlideListener(new PanelSlideListener() {
218
219 @Override
220 public void onPanelOpened(View arg0) {
221 paneShouldBeOpen = true;
222 getActionBar().setDisplayHomeAsUpEnabled(false);
223 getActionBar().setTitle(R.string.app_name);
224 invalidateOptionsMenu();
225 hideKeyboard();
226 }
227
228 @Override
229 public void onPanelClosed(View arg0) {
230 paneShouldBeOpen = false;
231 if (conversationList.size() > 0) {
232 getActionBar().setDisplayHomeAsUpEnabled(true);
233 getActionBar().setTitle(getSelectedConversation().getName());
234 invalidateOptionsMenu();
235 if (!getSelectedConversation().isRead()) {
236 getSelectedConversation().markRead();
237 UIHelper.updateNotification(getApplicationContext(), getConversationList(), false);
238 updateConversationList();
239 }
240 }
241 }
242
243 @Override
244 public void onPanelSlide(View arg0, float arg1) {
245 // TODO Auto-generated method stub
246
247 }
248 });
249 }
250
251 @Override
252 public boolean onCreateOptionsMenu(Menu menu) {
253 getMenuInflater().inflate(R.menu.conversations, menu);
254 MenuItem menuSecure = (MenuItem) menu.findItem(R.id.action_security);
255 MenuItem menuArchive = (MenuItem) menu.findItem(R.id.action_archive);
256 MenuItem menuMucDetails = (MenuItem) menu.findItem(R.id.action_muc_details);
257 MenuItem menuContactDetails = (MenuItem) menu.findItem(R.id.action_contact_details);
258
259 if ((spl.isOpen()&&(spl.isSlideable()))) {
260 menuArchive.setVisible(false);
261 menuMucDetails.setVisible(false);
262 menuContactDetails.setVisible(false);
263 menuSecure.setVisible(false);
264 } else {
265 ((MenuItem) menu.findItem(R.id.action_add)).setVisible(!spl.isSlideable());
266 if (this.getSelectedConversation()!=null) {
267 if (this.getSelectedConversation().getMode() == Conversation.MODE_MULTI) {
268 menuMucDetails.setVisible(true);
269 menuContactDetails.setVisible(false);
270 menuSecure.setVisible(false);
271 menuArchive.setTitle("Leave conference");
272 } else {
273 menuContactDetails.setVisible(true);
274 menuMucDetails.setVisible(false);
275 if (this.getSelectedConversation().getLatestMessage().getEncryption() != Message.ENCRYPTION_NONE) {
276 menuSecure.setIcon(R.drawable.ic_action_secure);
277 }
278 }
279 }
280 }
281 return true;
282 }
283
284 @Override
285 public boolean onOptionsItemSelected(MenuItem item) {
286 switch (item.getItemId()) {
287 case android.R.id.home:
288 spl.openPane();
289 break;
290 case R.id.action_add:
291 startActivity(new Intent(this, NewConversationActivity.class));
292 break;
293 case R.id.action_archive:
294 Conversation conv = getSelectedConversation();
295 conv.setStatus(Conversation.STATUS_ARCHIVED);
296 paneShouldBeOpen = true;
297 spl.openPane();
298 xmppConnectionService.archiveConversation(conv);
299 if (conversationList.size() > 0) {
300 selectedConversation = conversationList.get(0);
301 } else {
302 selectedConversation = null;
303 }
304 break;
305 case R.id.action_contact_details:
306 Contact contact = this.getSelectedConversation().getContact();
307 if (contact != null) {
308 Intent intent = new Intent(this,ContactDetailsActivity.class);
309 intent.setAction(ContactDetailsActivity.ACTION_VIEW_CONTACT);
310 intent.putExtra("uuid", contact.getUuid());
311 startActivity(intent);
312 } else {
313 String jid = getSelectedConversation().getContactJid();
314 AlertDialog.Builder builder = new AlertDialog.Builder(this);
315 builder.setTitle(jid);
316 builder.setMessage("The contact is not in your roster. Would you like to add it.");
317 builder.setNegativeButton("Cancel", null);
318 builder.setPositiveButton("Add",addToRoster);
319 builder.create().show();
320 }
321 break;
322 case R.id.action_muc_details:
323 Intent intent = new Intent(this,MucDetailsActivity.class);
324 intent.setAction(MucDetailsActivity.ACTION_VIEW_MUC);
325 intent.putExtra("uuid", getSelectedConversation().getUuid());
326 startActivity(intent);
327 break;
328 case R.id.action_security:
329 final Conversation selConv = getSelectedConversation();
330 View menuItemView = findViewById(R.id.action_security);
331 PopupMenu popup = new PopupMenu(this, menuItemView);
332 final ConversationFragment fragment = (ConversationFragment) getFragmentManager().findFragmentByTag("conversation");
333 if (fragment!=null) {
334 popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
335
336 @Override
337 public boolean onMenuItemClick(MenuItem item) {
338 switch (item.getItemId()) {
339 case R.id.encryption_choice_none:
340 selConv.nextMessageEncryption = Message.ENCRYPTION_NONE;
341 item.setChecked(true);
342 break;
343 case R.id.encryption_choice_otr:
344 selConv.nextMessageEncryption = Message.ENCRYPTION_OTR;
345 item.setChecked(true);
346 break;
347 case R.id.encryption_choice_pgp:
348 selConv.nextMessageEncryption = Message.ENCRYPTION_PGP;
349 item.setChecked(true);
350 break;
351 default:
352 selConv.nextMessageEncryption = Message.ENCRYPTION_NONE;
353 break;
354 }
355 fragment.updateChatMsgHint();
356 return true;
357 }
358 });
359 popup.inflate(R.menu.encryption_choices);
360 switch (selConv.nextMessageEncryption) {
361 case Message.ENCRYPTION_NONE:
362 popup.getMenu().findItem(R.id.encryption_choice_none).setChecked(true);
363 break;
364 case Message.ENCRYPTION_OTR:
365 popup.getMenu().findItem(R.id.encryption_choice_otr).setChecked(true);
366 break;
367 case Message.ENCRYPTION_PGP:
368 popup.getMenu().findItem(R.id.encryption_choice_pgp).setChecked(true);
369 break;
370 case Message.ENCRYPTION_DECRYPTED:
371 popup.getMenu().findItem(R.id.encryption_choice_pgp).setChecked(true);
372 break;
373 default:
374 popup.getMenu().findItem(R.id.encryption_choice_none).setChecked(true);
375 break;
376 }
377 popup.show();
378 }
379
380 break;
381 default:
382 break;
383 }
384 return super.onOptionsItemSelected(item);
385 }
386
387 protected ConversationFragment swapConversationFragment() {
388 ConversationFragment selectedFragment = new ConversationFragment();
389
390 FragmentTransaction transaction = getFragmentManager()
391 .beginTransaction();
392 transaction.replace(R.id.selected_conversation, selectedFragment,"conversation");
393 transaction.commit();
394 return selectedFragment;
395 }
396
397 @Override
398 public boolean onKeyDown(int keyCode, KeyEvent event) {
399 if (keyCode == KeyEvent.KEYCODE_BACK) {
400 if (!spl.isOpen()) {
401 spl.openPane();
402 return false;
403 }
404 }
405 return super.onKeyDown(keyCode, event);
406 }
407
408 public void onStart() {
409 super.onStart();
410 if (xmppConnectionServiceBound) {
411 xmppConnectionService.setOnConversationListChangedListener(this.onConvChanged);
412 }
413 if (conversationList.size()>=1) {
414 onConvChanged.onConversationListChanged();
415 }
416 }
417
418 @Override
419 protected void onStop() {
420 Log.d("gultsch","called on stop in conversation activity");
421 if (xmppConnectionServiceBound) {
422 xmppConnectionService.removeOnConversationListChangedListener();
423 }
424 super.onStop();
425 }
426
427 @Override
428 void onBackendConnected() {
429
430 xmppConnectionService.setOnConversationListChangedListener(this.onConvChanged);
431
432 if (conversationList.size()==0) {
433 conversationList.addAll(xmppConnectionService
434 .getConversations());
435
436 this.updateConversationList();
437 }
438
439 if ((getIntent().getAction()!=null)&&(getIntent().getAction().equals(Intent.ACTION_VIEW) && (!handledViewIntent))) {
440 if (getIntent().getType().equals(
441 ConversationActivity.VIEW_CONVERSATION)) {
442 handledViewIntent = true;
443
444 String convToView = (String) getIntent().getExtras().get(CONVERSATION);
445
446 for(int i = 0; i < conversationList.size(); ++i) {
447 if (conversationList.get(i).getUuid().equals(convToView)) {
448 selectedConversation = conversationList.get(i);
449 }
450 }
451 paneShouldBeOpen = false;
452 swapConversationFragment();
453 }
454 } else {
455 if (xmppConnectionService.getAccounts().size() == 0) {
456 startActivity(new Intent(this, ManageAccountActivity.class));
457 finish();
458 } else if (conversationList.size() <= 0) {
459 //add no history
460 startActivity(new Intent(this, NewConversationActivity.class));
461 finish();
462 } else {
463 spl.openPane();
464 //find currently loaded fragment
465 ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager().findFragmentByTag("conversation");
466 if (selectedFragment!=null) {
467 selectedFragment.onBackendConnected();
468 } else {
469 selectedConversation = conversationList.get(0);
470 swapConversationFragment();
471 }
472 }
473 }
474 }
475 @Override
476 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
477 super.onActivityResult(requestCode, resultCode, data);
478 if (resultCode == RESULT_OK) {
479 if (requestCode == REQUEST_DECRYPT_PGP) {
480 ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager().findFragmentByTag("conversation");
481 if (selectedFragment!=null) {
482 selectedFragment.hidePgpPassphraseBox();
483 }
484 }
485 }
486 }
487}