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