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