ConversationActivity.java

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