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