ConversationActivity.java

  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(), NewConversationActivity.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		
253		if ((spl.isOpen()&&(spl.isSlideable()))) {
254			menuArchive.setVisible(false);
255			menuMucDetails.setVisible(false);
256			menuContactDetails.setVisible(false);
257			menuSecure.setVisible(false);
258		} else {
259			((MenuItem) menu.findItem(R.id.action_add)).setVisible(!spl.isSlideable());
260			if (this.getSelectedConversation()!=null) {
261				if (this.getSelectedConversation().getMode() == Conversation.MODE_MULTI) {
262					menuMucDetails.setVisible(true);
263					menuContactDetails.setVisible(false);
264					menuSecure.setVisible(false);
265					menuArchive.setTitle("Leave conference");
266				} else {
267					menuContactDetails.setVisible(true);
268					menuMucDetails.setVisible(false);
269					if (this.getSelectedConversation().getLatestMessage().getEncryption() != Message.ENCRYPTION_NONE) {
270						menuSecure.setIcon(R.drawable.ic_action_secure);
271					}
272				}
273			}
274		}
275		return true;
276	}
277
278	@Override
279	public boolean onOptionsItemSelected(MenuItem item) {
280		switch (item.getItemId()) {
281		case android.R.id.home:
282			spl.openPane();
283			break;
284		case R.id.action_add:
285			startActivity(new Intent(this, NewConversationActivity.class));
286			break;
287		case R.id.action_archive:
288			Conversation conv = getSelectedConversation();
289			conv.setStatus(Conversation.STATUS_ARCHIVED);
290			paneShouldBeOpen = true;
291			spl.openPane();
292			xmppConnectionService.archiveConversation(conv);
293			if (conversationList.size() > 0) {
294				selectedConversation = conversationList.get(0);
295			} else {
296				selectedConversation = null;
297			}
298			break;
299		case R.id.action_contact_details:
300			Contact contact = this.getSelectedConversation().getContact();
301			if (contact != null) {
302				Intent intent = new Intent(this,ContactDetailsActivity.class);
303				intent.setAction(ContactDetailsActivity.ACTION_VIEW_CONTACT);
304				intent.putExtra("uuid", contact.getUuid());
305				startActivity(intent);
306			} else {
307				String jid = getSelectedConversation().getContactJid();
308				AlertDialog.Builder builder = new AlertDialog.Builder(this);
309				builder.setTitle(jid);
310				builder.setMessage("The contact is not in your roster. Would you like to add it.");
311				builder.setNegativeButton("Cancel", null);
312				builder.setPositiveButton("Add",addToRoster);
313				builder.create().show();
314			}
315			break;
316		case R.id.action_muc_details:
317			Intent intent = new Intent(this,MucDetailsActivity.class);
318			intent.setAction(MucDetailsActivity.ACTION_VIEW_MUC);
319			intent.putExtra("uuid", getSelectedConversation().getUuid());
320			startActivity(intent);
321			break;
322		case R.id.action_security:
323			final Conversation selConv = getSelectedConversation();
324			View menuItemView = findViewById(R.id.action_security);
325			PopupMenu popup = new PopupMenu(this, menuItemView);
326			final ConversationFragment fragment = (ConversationFragment) getFragmentManager().findFragmentByTag("conversation");
327		    if (fragment!=null) {
328				popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
329					
330					@Override
331					public boolean onMenuItemClick(MenuItem item) {
332						switch (item.getItemId()) {
333						case R.id.encryption_choice_none:
334							selConv.nextMessageEncryption = Message.ENCRYPTION_NONE;
335							item.setChecked(true);
336							break;
337						case R.id.encryption_choice_otr:
338							selConv.nextMessageEncryption = Message.ENCRYPTION_OTR;
339							item.setChecked(true);
340							break;
341						case R.id.encryption_choice_pgp:
342							selConv.nextMessageEncryption = Message.ENCRYPTION_PGP;
343							item.setChecked(true);
344							break;
345						default:
346							selConv.nextMessageEncryption = Message.ENCRYPTION_NONE;
347							break;
348						}
349						fragment.updateChatMsgHint();
350						return true;
351					}
352				});
353			    popup.inflate(R.menu.encryption_choices);
354			    switch (selConv.nextMessageEncryption) {
355				case Message.ENCRYPTION_NONE:
356					popup.getMenu().findItem(R.id.encryption_choice_none).setChecked(true);
357					break;
358				case Message.ENCRYPTION_OTR:
359					popup.getMenu().findItem(R.id.encryption_choice_otr).setChecked(true);
360					break;
361				case Message.ENCRYPTION_PGP:
362					popup.getMenu().findItem(R.id.encryption_choice_pgp).setChecked(true);
363					break;
364				case Message.ENCRYPTION_DECRYPTED:
365					popup.getMenu().findItem(R.id.encryption_choice_pgp).setChecked(true);
366					break;
367				default:
368					popup.getMenu().findItem(R.id.encryption_choice_none).setChecked(true);
369					break;
370				}
371			    popup.show();
372		    }
373
374			break;
375		default:
376			break;
377		}
378		return super.onOptionsItemSelected(item);
379	}
380
381	protected ConversationFragment swapConversationFragment() {
382		ConversationFragment selectedFragment = new ConversationFragment();
383		
384		FragmentTransaction transaction = getFragmentManager()
385				.beginTransaction();
386		transaction.replace(R.id.selected_conversation, selectedFragment,"conversation");
387		transaction.commit();
388		return selectedFragment;
389	}
390
391	@Override
392	public boolean onKeyDown(int keyCode, KeyEvent event) {
393		if (keyCode == KeyEvent.KEYCODE_BACK) {
394			if (!spl.isOpen()) {
395				spl.openPane();
396				return false;
397			}
398		}
399		return super.onKeyDown(keyCode, event);
400	}
401	
402	public void onStart() {
403		super.onStart();
404		this.registerListener();
405		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
406		this.useSubject = preferences.getBoolean("use_subject_in_muc", true);
407		if (conversationList.size()>=1) {
408			onConvChanged.onConversationListChanged();
409		}
410	}
411	
412	@Override
413	protected void onStop() {
414		Log.d("gultsch","called on stop in conversation activity");
415		if (xmppConnectionServiceBound) {
416        	xmppConnectionService.removeOnConversationListChangedListener();
417		}
418		super.onStop();
419	}
420
421	@Override
422	void onBackendConnected() {
423		
424		this.registerListener();
425		if (conversationList.size()==0) {
426			conversationList.addAll(xmppConnectionService
427					.getConversations());
428			
429			this.updateConversationList();
430		}
431
432		if ((getIntent().getAction()!=null)&&(getIntent().getAction().equals(Intent.ACTION_VIEW) && (!handledViewIntent))) {
433			if (getIntent().getType().equals(
434					ConversationActivity.VIEW_CONVERSATION)) {
435				handledViewIntent = true;
436
437				String convToView = (String) getIntent().getExtras().get(CONVERSATION);
438
439				for(int i = 0; i < conversationList.size(); ++i) {
440					if (conversationList.get(i).getUuid().equals(convToView)) {
441						selectedConversation = conversationList.get(i);
442					}
443				}
444				paneShouldBeOpen = false;
445				String text = getIntent().getExtras().getString(TEXT, null);
446				swapConversationFragment().setText(text);
447			}
448		} else {
449			if (xmppConnectionService.getAccounts().size() == 0) {
450				startActivity(new Intent(this, ManageAccountActivity.class));
451				finish();
452			} else if (conversationList.size() <= 0) {
453				//add no history
454				startActivity(new Intent(this, NewConversationActivity.class));
455				finish();
456			} else {
457				spl.openPane();
458				//find currently loaded fragment
459				ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager().findFragmentByTag("conversation");
460				if (selectedFragment!=null) {
461					selectedFragment.onBackendConnected();
462				} else {
463					selectedConversation = conversationList.get(0);
464					swapConversationFragment();
465				}
466			}
467		}
468	}
469	public void registerListener() {
470		 if (xmppConnectionServiceBound) {
471			 xmppConnectionService.setOnConversationListChangedListener(this.onConvChanged);
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}