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 }
163 TextView convName = (TextView) view.findViewById(R.id.conversation_name);
164 convName.setText(conv.getName());
165 TextView convLastMsg = (TextView) view.findViewById(R.id.conversation_lastmsg);
166 convLastMsg.setText(conv.getLatestMessage().getBody());
167
168 if(!conv.isRead()) {
169 convName.setTypeface(null,Typeface.BOLD);
170 convLastMsg.setTypeface(null,Typeface.BOLD);
171 } else {
172 convName.setTypeface(null,Typeface.NORMAL);
173 convLastMsg.setTypeface(null,Typeface.NORMAL);
174 }
175
176 ((TextView) view.findViewById(R.id.conversation_lastupdate))
177 .setText(UIHelper.readableTimeDifference(conv.getLatestMessage().getTimeSent()));
178
179 Uri profilePhoto = conv.getProfilePhotoUri();
180 ImageView imageView = (ImageView) view.findViewById(R.id.conversation_image);
181 if (profilePhoto!=null) {
182 imageView.setImageURI(profilePhoto);
183 } else {
184 imageView.setImageBitmap(UIHelper.getUnknownContactPicture(getItem(position).getName(),200));
185 }
186
187
188 ((ImageView) view.findViewById(R.id.conversation_image))
189 .setImageURI(conv.getProfilePhotoUri());
190 return view;
191 }
192
193 };
194
195 listView.setAdapter(this.listAdapter);
196
197 listView.setOnItemClickListener(new OnItemClickListener() {
198
199 @Override
200 public void onItemClick(AdapterView<?> arg0, View clickedView,
201 int position, long arg3) {
202 paneShouldBeOpen = false;
203 if (selectedConversation != conversationList.get(position)) {
204 selectedConversation = conversationList.get(position);
205 swapConversationFragment(); //.onBackendConnected(conversationList.get(position));
206 } else {
207 spl.closePane();
208 }
209 }
210 });
211 spl = (SlidingPaneLayout) findViewById(R.id.slidingpanelayout);
212 spl.setParallaxDistance(150);
213 spl.setShadowResource(R.drawable.es_slidingpane_shadow);
214 spl.setSliderFadeColor(0);
215 spl.setPanelSlideListener(new PanelSlideListener() {
216
217 @Override
218 public void onPanelOpened(View arg0) {
219 paneShouldBeOpen = true;
220 getActionBar().setDisplayHomeAsUpEnabled(false);
221 getActionBar().setTitle(R.string.app_name);
222 invalidateOptionsMenu();
223 hideKeyboard();
224 }
225
226 @Override
227 public void onPanelClosed(View arg0) {
228 paneShouldBeOpen = false;
229 if (conversationList.size() > 0) {
230 getActionBar().setDisplayHomeAsUpEnabled(true);
231 getActionBar().setTitle(getSelectedConversation().getName());
232 invalidateOptionsMenu();
233 if (!getSelectedConversation().isRead()) {
234 getSelectedConversation().markRead();
235 updateConversationList();
236 }
237 }
238 }
239
240 @Override
241 public void onPanelSlide(View arg0, float arg1) {
242 // TODO Auto-generated method stub
243
244 }
245 });
246 }
247
248 @Override
249 public boolean onCreateOptionsMenu(Menu menu) {
250 getMenuInflater().inflate(R.menu.conversations, menu);
251 MenuItem menuSecure = (MenuItem) menu.findItem(R.id.action_security);
252 MenuItem menuArchive = (MenuItem) menu.findItem(R.id.action_archive);
253 MenuItem menuMucDetails = (MenuItem) menu.findItem(R.id.action_muc_details);
254 MenuItem menuContactDetails = (MenuItem) menu.findItem(R.id.action_contact_details);
255
256 if ((spl.isOpen()&&(spl.isSlideable()))) {
257 menuArchive.setVisible(false);
258 menuMucDetails.setVisible(false);
259 menuContactDetails.setVisible(false);
260 menuSecure.setVisible(false);
261 } else {
262 ((MenuItem) menu.findItem(R.id.action_add)).setVisible(!spl.isSlideable());
263 if (this.getSelectedConversation()!=null) {
264 if (this.getSelectedConversation().getMode() == Conversation.MODE_MULTI) {
265 menuMucDetails.setVisible(true);
266 menuContactDetails.setVisible(false);
267 menuSecure.setVisible(false);
268 menuArchive.setTitle("Leave conference");
269 } else {
270 menuContactDetails.setVisible(true);
271 menuMucDetails.setVisible(false);
272 if (this.getSelectedConversation().getLatestMessage().getEncryption() != Message.ENCRYPTION_NONE) {
273 menuSecure.setIcon(R.drawable.ic_action_secure);
274 }
275 }
276 }
277 }
278 return true;
279 }
280
281 @Override
282 public boolean onOptionsItemSelected(MenuItem item) {
283 switch (item.getItemId()) {
284 case android.R.id.home:
285 spl.openPane();
286 break;
287 case R.id.action_add:
288 startActivity(new Intent(this, NewConversationActivity.class));
289 break;
290 case R.id.action_archive:
291 Conversation conv = getSelectedConversation();
292 conv.setStatus(Conversation.STATUS_ARCHIVED);
293 paneShouldBeOpen = true;
294 spl.openPane();
295 xmppConnectionService.archiveConversation(conv);
296 selectedConversation = conversationList.get(0);
297 break;
298 case R.id.action_contact_details:
299 Contact contact = this.getSelectedConversation().getContact();
300 if (contact != null) {
301 Intent intent = new Intent(this,ContactDetailsActivity.class);
302 intent.setAction(ContactDetailsActivity.ACTION_VIEW_CONTACT);
303 intent.putExtra("uuid", contact.getUuid());
304 startActivity(intent);
305 } else {
306 String jid = getSelectedConversation().getContactJid();
307 AlertDialog.Builder builder = new AlertDialog.Builder(this);
308 builder.setTitle(jid);
309 builder.setMessage("The contact is not in your roster. Would you like to add it.");
310 builder.setNegativeButton("Cancel", null);
311 builder.setPositiveButton("Add",addToRoster);
312 builder.create().show();
313 }
314 break;
315 case R.id.action_muc_details:
316 Intent intent = new Intent(this,MucDetailsActivity.class);
317 intent.setAction(MucDetailsActivity.ACTION_VIEW_MUC);
318 intent.putExtra("uuid", getSelectedConversation().getUuid());
319 startActivity(intent);
320 break;
321 case R.id.action_security:
322 final Conversation selConv = getSelectedConversation();
323 View menuItemView = findViewById(R.id.action_security);
324 PopupMenu popup = new PopupMenu(this, menuItemView);
325 final ConversationFragment fragment = (ConversationFragment) getFragmentManager().findFragmentByTag("conversation");
326 if (fragment!=null) {
327 popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
328
329 @Override
330 public boolean onMenuItemClick(MenuItem item) {
331 switch (item.getItemId()) {
332 case R.id.encryption_choice_none:
333 selConv.nextMessageEncryption = Message.ENCRYPTION_NONE;
334 item.setChecked(true);
335 break;
336 case R.id.encryption_choice_otr:
337 selConv.nextMessageEncryption = Message.ENCRYPTION_OTR;
338 item.setChecked(true);
339 break;
340 case R.id.encryption_choice_pgp:
341 selConv.nextMessageEncryption = Message.ENCRYPTION_PGP;
342 item.setChecked(true);
343 break;
344 default:
345 selConv.nextMessageEncryption = Message.ENCRYPTION_NONE;
346 break;
347 }
348 fragment.updateChatMsgHint();
349 return true;
350 }
351 });
352 popup.inflate(R.menu.encryption_choices);
353 switch (selConv.nextMessageEncryption) {
354 case Message.ENCRYPTION_NONE:
355 popup.getMenu().findItem(R.id.encryption_choice_none).setChecked(true);
356 break;
357 case Message.ENCRYPTION_OTR:
358 popup.getMenu().findItem(R.id.encryption_choice_otr).setChecked(true);
359 break;
360 case Message.ENCRYPTION_PGP:
361 popup.getMenu().findItem(R.id.encryption_choice_pgp).setChecked(true);
362 break;
363 case Message.ENCRYPTION_DECRYPTED:
364 popup.getMenu().findItem(R.id.encryption_choice_pgp).setChecked(true);
365 break;
366 default:
367 popup.getMenu().findItem(R.id.encryption_choice_none).setChecked(true);
368 break;
369 }
370 popup.show();
371 }
372
373 break;
374 default:
375 break;
376 }
377 return super.onOptionsItemSelected(item);
378 }
379
380 protected ConversationFragment swapConversationFragment() {
381 ConversationFragment selectedFragment = new ConversationFragment();
382
383 FragmentTransaction transaction = getFragmentManager()
384 .beginTransaction();
385 transaction.replace(R.id.selected_conversation, selectedFragment,"conversation");
386 transaction.commit();
387 return selectedFragment;
388 }
389
390 @Override
391 public boolean onKeyDown(int keyCode, KeyEvent event) {
392 if (keyCode == KeyEvent.KEYCODE_BACK) {
393 if (!spl.isOpen()) {
394 spl.openPane();
395 return false;
396 }
397 }
398 return super.onKeyDown(keyCode, event);
399 }
400
401 public void onStart() {
402 super.onStart();
403 NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
404 nm.cancelAll();
405 if (conversationList.size()>=1) {
406 onConvChanged.onConversationListChanged();
407 }
408 }
409
410 @Override
411 protected void onStop() {
412 Log.d("gultsch","called on stop in conversation activity");
413 if (xmppConnectionServiceBound) {
414 xmppConnectionService.removeOnConversationListChangedListener();
415 }
416 super.onStop();
417 }
418
419 @Override
420 void onBackendConnected() {
421
422 xmppConnectionService.setOnConversationListChangedListener(this.onConvChanged);
423
424 if (conversationList.size()==0) {
425 conversationList.addAll(xmppConnectionService
426 .getConversations());
427
428 this.updateConversationList();
429 }
430
431 if ((getIntent().getAction()!=null)&&(getIntent().getAction().equals(Intent.ACTION_VIEW) && (!handledViewIntent))) {
432 if (getIntent().getType().equals(
433 ConversationActivity.VIEW_CONVERSATION)) {
434 handledViewIntent = true;
435
436 String convToView = (String) getIntent().getExtras().get(CONVERSATION);
437
438 for(int i = 0; i < conversationList.size(); ++i) {
439 if (conversationList.get(i).getUuid().equals(convToView)) {
440 selectedConversation = conversationList.get(i);
441 }
442 }
443 paneShouldBeOpen = false;
444 swapConversationFragment();
445 }
446 } else {
447 if (xmppConnectionService.getAccounts().size() == 0) {
448 startActivity(new Intent(this, ManageAccountActivity.class));
449 finish();
450 } else if (conversationList.size() <= 0) {
451 //add no history
452 startActivity(new Intent(this, NewConversationActivity.class));
453 finish();
454 } else {
455 spl.openPane();
456 //find currently loaded fragment
457 ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager().findFragmentByTag("conversation");
458 if (selectedFragment!=null) {
459 selectedFragment.onBackendConnected();
460 } else {
461 selectedConversation = conversationList.get(0);
462 swapConversationFragment();
463 }
464 }
465 }
466 }
467 @Override
468 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
469 super.onActivityResult(requestCode, resultCode, data);
470 if (resultCode == RESULT_OK) {
471 if (requestCode == REQUEST_DECRYPT_PGP) {
472 ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager().findFragmentByTag("conversation");
473 if (selectedFragment!=null) {
474 selectedFragment.hidePgpPassphraseBox();
475 }
476 }
477 }
478 }
479}