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) {
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	public void onStart() {
398		super.onStart();
399		this.registerListener();
400		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
401		this.useSubject = preferences.getBoolean("use_subject_in_muc", true);
402		if (conversationList.size()>=1) {
403			onConvChanged.onConversationListChanged();
404		}
405	}
406	
407	@Override
408	protected void onStop() {
409		Log.d("gultsch","called on stop in conversation activity");
410		if (xmppConnectionServiceBound) {
411        	xmppConnectionService.removeOnConversationListChangedListener();
412		}
413		super.onStop();
414	}
415
416	@Override
417	void onBackendConnected() {
418		
419		this.registerListener();
420		if (conversationList.size()==0) {
421			updateConversationList();
422		}
423
424		if ((getIntent().getAction()!=null)&&(getIntent().getAction().equals(Intent.ACTION_VIEW) && (!handledViewIntent))) {
425			if (getIntent().getType().equals(
426					ConversationActivity.VIEW_CONVERSATION)) {
427				handledViewIntent = true;
428
429				String convToView = (String) getIntent().getExtras().get(CONVERSATION);
430
431				for(int i = 0; i < conversationList.size(); ++i) {
432					if (conversationList.get(i).getUuid().equals(convToView)) {
433						selectedConversation = conversationList.get(i);
434					}
435				}
436				paneShouldBeOpen = false;
437				String text = getIntent().getExtras().getString(TEXT, null);
438				swapConversationFragment().setText(text);
439			}
440		} else {
441			if (xmppConnectionService.getAccounts().size() == 0) {
442				startActivity(new Intent(this, ManageAccountActivity.class));
443				finish();
444			} else if (conversationList.size() <= 0) {
445				//add no history
446				startActivity(new Intent(this, ContactsActivity.class));
447				finish();
448			} else {
449				spl.openPane();
450				//find currently loaded fragment
451				ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager().findFragmentByTag("conversation");
452				if (selectedFragment!=null) {
453					selectedFragment.onBackendConnected();
454				} else {
455					selectedConversation = conversationList.get(0);
456					swapConversationFragment();
457				}
458				ExceptionHelper.checkForCrash(this,this.xmppConnectionService);
459			}
460		}
461	}
462	public void registerListener() {
463		 if (xmppConnectionServiceBound) {
464			 xmppConnectionService.setOnConversationListChangedListener(this.onConvChanged);
465		 }
466	}
467
468	@Override
469	 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
470		 super.onActivityResult(requestCode, resultCode, data);
471		 if (resultCode == RESULT_OK) {
472			if (requestCode == REQUEST_DECRYPT_PGP) {
473				ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager().findFragmentByTag("conversation");
474				if (selectedFragment!=null) {
475					selectedFragment.hidePgpPassphraseBox();
476				}
477			}
478		 }
479	 }
480
481	public void updateConversationList() {
482		conversationList.clear();
483		conversationList.addAll(xmppConnectionService
484				.getConversations());
485		listView.invalidateViews();
486	}
487}