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		
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					menuInviteContacts.setVisible(true);
252				} else {
253					menuContactDetails.setVisible(true);
254					menuMucDetails.setVisible(false);
255					menuInviteContacts.setVisible(false);
256					if (this.getSelectedConversation().getLatestMessage().getEncryption() != Message.ENCRYPTION_NONE) {
257						menuSecure.setIcon(R.drawable.ic_action_secure);
258					}
259				}
260			}
261		}
262		return true;
263	}
264
265	@Override
266	public boolean onOptionsItemSelected(MenuItem item) {
267		switch (item.getItemId()) {
268		case android.R.id.home:
269			spl.openPane();
270			break;
271		case R.id.action_add:
272			startActivity(new Intent(this, ContactsActivity.class));
273			break;
274		case R.id.action_archive:
275			Conversation conv = getSelectedConversation();
276			conv.setStatus(Conversation.STATUS_ARCHIVED);
277			paneShouldBeOpen = true;
278			spl.openPane();
279			xmppConnectionService.archiveConversation(conv);
280			if (conversationList.size() > 0) {
281				selectedConversation = conversationList.get(0);
282			} else {
283				selectedConversation = null;
284			}
285			break;
286		case R.id.action_contact_details:
287			Contact contact = this.getSelectedConversation().getContact();
288			if (contact != null) {
289				Intent intent = new Intent(this,ContactDetailsActivity.class);
290				intent.setAction(ContactDetailsActivity.ACTION_VIEW_CONTACT);
291				intent.putExtra("uuid", contact.getUuid());
292				startActivity(intent);
293			} else {
294				String jid = getSelectedConversation().getContactJid();
295				AlertDialog.Builder builder = new AlertDialog.Builder(this);
296				builder.setTitle(jid);
297				builder.setMessage("The contact is not in your roster. Would you like to add it.");
298				builder.setNegativeButton("Cancel", null);
299				builder.setPositiveButton("Add",addToRoster);
300				builder.create().show();
301			}
302			break;
303		case R.id.action_muc_details:
304			Intent intent = new Intent(this,MucDetailsActivity.class);
305			intent.setAction(MucDetailsActivity.ACTION_VIEW_MUC);
306			intent.putExtra("uuid", getSelectedConversation().getUuid());
307			startActivity(intent);
308			break;
309		case R.id.action_invite:
310			Intent inviteIntent = new Intent(getApplicationContext(),
311					ContactsActivity.class);
312			inviteIntent.setAction("invite");
313			inviteIntent.putExtra("uuid",selectedConversation.getUuid());
314			startActivity(inviteIntent);
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	@Override
397	public void onStart() {
398		super.onStart();
399		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
400		this.useSubject = preferences.getBoolean("use_subject_in_muc", true);
401		if (this.xmppConnectionServiceBound) {
402			this.onBackendConnected();
403		}
404		if (conversationList.size()>=1) {
405			onConvChanged.onConversationListChanged();
406		}
407	}
408	
409	@Override
410	protected void onStop() {
411		if (xmppConnectionServiceBound) {
412        	xmppConnectionService.removeOnConversationListChangedListener();
413		}
414		super.onStop();
415	}
416
417	@Override
418	void onBackendConnected() {
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}