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.net.Uri;
 15import android.os.Bundle;
 16import android.app.AlertDialog;
 17import android.app.FragmentTransaction;
 18import android.app.NotificationManager;
 19import android.content.Context;
 20import android.content.DialogInterface;
 21import android.content.Intent;
 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.view.inputmethod.InputMethodManager;
 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	
 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 ArrayAdapter<Conversation> listAdapter;
 58	
 59	private OnConversationListChangedListener onConvChanged = new OnConversationListChangedListener() {
 60		
 61		@Override
 62		public void onConversationListChanged() {
 63			conversationList.clear();
 64			conversationList.addAll(xmppConnectionService
 65					.getConversations());
 66			runOnUiThread(new Runnable() {
 67				
 68				@Override
 69				public void run() {	
 70					updateConversationList();
 71					if(paneShouldBeOpen) {
 72						if (conversationList.size() >= 1) {
 73							swapConversationFragment();
 74						} else {
 75							startActivity(new Intent(getApplicationContext(), NewConversationActivity.class));
 76							finish();
 77						}
 78					}
 79					ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager().findFragmentByTag("conversation");
 80					if (selectedFragment!=null) {
 81						selectedFragment.updateMessages();
 82					}
 83				}
 84			});
 85		}
 86	};
 87	
 88	private DialogInterface.OnClickListener addToRoster = new DialogInterface.OnClickListener() {
 89		
 90		@Override
 91		public void onClick(DialogInterface dialog, int which) {
 92			String jid = getSelectedConversation().getContactJid();
 93			Account account = getSelectedConversation().getAccount();
 94			String name = jid.split("@")[0];
 95			Contact contact = new Contact(account, name, jid, null);
 96			xmppConnectionService.createContact(contact);
 97		}
 98	};
 99	
100	public List<Conversation> getConversationList() {
101		return this.conversationList;
102	}
103
104	public Conversation getSelectedConversation() {
105		return this.selectedConversation;
106	}
107	
108	public ListView getConversationListView() {
109		return this.listView;
110	}
111	
112	public SlidingPaneLayout getSlidingPaneLayout() {
113		return this.spl;
114	}
115	
116	public boolean shouldPaneBeOpen() {
117		return paneShouldBeOpen;
118	}
119	
120	public void updateConversationList() {
121		if (conversationList.size() >= 1) {
122			Collections.sort(this.conversationList, new Comparator<Conversation>() {
123				@Override
124				public int compare(Conversation lhs, Conversation rhs) {
125					return (int) (rhs.getLatestMessage().getTimeSent() - lhs.getLatestMessage().getTimeSent());
126				}
127			});
128		}
129		this.listView.invalidateViews();
130	}
131	
132	@Override
133	protected void onCreate(Bundle savedInstanceState) {
134
135		super.onCreate(savedInstanceState);
136
137		setContentView(R.layout.fragment_conversations_overview);
138
139		listView = (ListView) findViewById(R.id.list);
140
141		this.listAdapter = new ArrayAdapter<Conversation>(this,
142				R.layout.conversation_list_row, conversationList) {
143			@Override
144			public View getView(int position, View view, ViewGroup parent) {
145				if (view == null) {
146					LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
147					view = (View) inflater.inflate(
148							R.layout.conversation_list_row, null);
149				}
150				Conversation conv = getItem(position);
151				TextView convName = (TextView) view.findViewById(R.id.conversation_name);
152				convName.setText(conv.getName());
153				TextView convLastMsg = (TextView) view.findViewById(R.id.conversation_lastmsg);
154				convLastMsg.setText(conv.getLatestMessage().getBody());
155				
156				if(!conv.isRead()) {
157					convName.setTypeface(null,Typeface.BOLD);
158					convLastMsg.setTypeface(null,Typeface.BOLD);
159				} else {
160					convName.setTypeface(null,Typeface.NORMAL);
161					convLastMsg.setTypeface(null,Typeface.NORMAL);
162				}
163				
164				((TextView) view.findViewById(R.id.conversation_lastupdate))
165				.setText(UIHelper.readableTimeDifference(getItem(position).getLatestMessage().getTimeSent()));
166				
167				Uri profilePhoto = getItem(position).getProfilePhotoUri();
168				ImageView imageView = (ImageView) view.findViewById(R.id.conversation_image);
169				if (profilePhoto!=null) {
170					imageView.setImageURI(profilePhoto);
171				} else {
172					imageView.setImageBitmap(UIHelper.getUnknownContactPicture(getItem(position).getName(),200));
173				}
174				
175				
176				((ImageView) view.findViewById(R.id.conversation_image))
177						.setImageURI(getItem(position).getProfilePhotoUri());
178				return view;
179			}
180
181		};
182		
183		listView.setAdapter(this.listAdapter);
184
185		listView.setOnItemClickListener(new OnItemClickListener() {
186
187			@Override
188			public void onItemClick(AdapterView<?> arg0, View clickedView,
189					int position, long arg3) {
190				paneShouldBeOpen = false;
191				if (selectedConversation != conversationList.get(position)) {
192					selectedConversation = conversationList.get(position);
193					swapConversationFragment(); //.onBackendConnected(conversationList.get(position));
194				} else {
195					spl.closePane();
196				}
197			}
198		});
199		spl = (SlidingPaneLayout) findViewById(R.id.slidingpanelayout);
200		spl.setParallaxDistance(150);
201		spl.setShadowResource(R.drawable.es_slidingpane_shadow);
202		spl.setSliderFadeColor(0);
203		spl.setPanelSlideListener(new PanelSlideListener() {
204
205			@Override
206			public void onPanelOpened(View arg0) {
207				paneShouldBeOpen = true;
208				getActionBar().setDisplayHomeAsUpEnabled(false);
209				getActionBar().setTitle(R.string.app_name);
210				invalidateOptionsMenu();
211				hideKeyboard();
212			}
213
214			@Override
215			public void onPanelClosed(View arg0) {
216				paneShouldBeOpen = false;
217				if (conversationList.size() > 0) {
218					getActionBar().setDisplayHomeAsUpEnabled(true);
219					getActionBar().setTitle(getSelectedConversation().getName());
220					invalidateOptionsMenu();
221					if (!getSelectedConversation().isRead()) {
222						getSelectedConversation().markRead();
223						updateConversationList();
224					}
225				}
226			}
227
228			@Override
229			public void onPanelSlide(View arg0, float arg1) {
230				// TODO Auto-generated method stub
231
232			}
233		});
234	}
235
236	@Override
237	public boolean onCreateOptionsMenu(Menu menu) {
238		getMenuInflater().inflate(R.menu.conversations, menu);
239		MenuItem menuSecure = (MenuItem) menu.findItem(R.id.action_security);
240		MenuItem menuArchive = (MenuItem) menu.findItem(R.id.action_archive);
241		MenuItem menuMucDetails = (MenuItem) menu.findItem(R.id.action_muc_details);
242		MenuItem menuContactDetails = (MenuItem) menu.findItem(R.id.action_contact_details);
243		
244		if (spl.isOpen()) {
245			menuArchive.setVisible(false);
246			menuMucDetails.setVisible(false);
247			menuContactDetails.setVisible(false);
248			menuSecure.setVisible(false);
249		} else {
250			((MenuItem) menu.findItem(R.id.action_add)).setVisible(false);
251			if (this.getSelectedConversation()!=null) {
252				if (this.getSelectedConversation().getMode() == Conversation.MODE_MULTI) {
253					menuMucDetails.setVisible(true);
254					menuContactDetails.setVisible(false);
255					menuSecure.setVisible(false);
256					menuArchive.setTitle("Leave conference");
257				} else {
258					menuContactDetails.setVisible(true);
259					menuMucDetails.setVisible(false);
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_settings:
276			startActivity(new Intent(this, SettingsActivity.class));
277			break;
278		case R.id.action_accounts:
279			startActivity(new Intent(this, ManageAccountActivity.class));
280			break;
281		case R.id.action_add:
282			startActivity(new Intent(this, NewConversationActivity.class));
283			break;
284		case R.id.action_archive:
285			Conversation conv = getSelectedConversation();
286			conv.setStatus(Conversation.STATUS_ARCHIVED);
287			paneShouldBeOpen = true;
288			spl.openPane();
289			xmppConnectionService.archiveConversation(conv);
290			selectedConversation = conversationList.get(0);
291			break;
292		case R.id.action_contact_details:
293			DialogContactDetails details = new DialogContactDetails();
294			Contact contact = this.getSelectedConversation().getContact();
295			if (contact != null) {
296				contact.setAccount(this.selectedConversation.getAccount());
297				details.setContact(contact);
298				details.show(getFragmentManager(), "details");
299			} else {
300				String jid = getSelectedConversation().getContactJid();
301				AlertDialog.Builder builder = new AlertDialog.Builder(this);
302				builder.setTitle(jid);
303				builder.setMessage("The contact is not in your roster. Would you like to add it.");
304				builder.setNegativeButton("Cancel", null);
305				builder.setPositiveButton("Add",addToRoster);
306				builder.create().show();
307			}
308			break;
309		case R.id.action_muc_details:
310			DialogMucDetails mucDetails = new DialogMucDetails();
311			mucDetails.setConversation(getSelectedConversation());
312			mucDetails.show(getFragmentManager(), "details");
313			break;
314		case R.id.action_security:
315			final Conversation selConv = getSelectedConversation();
316			View menuItemView = findViewById(R.id.action_security);
317			PopupMenu popup = new PopupMenu(this, menuItemView);
318			final ConversationFragment fragment = (ConversationFragment) getFragmentManager().findFragmentByTag("conversation");
319		    if (fragment!=null) {
320				popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
321					
322					@Override
323					public boolean onMenuItemClick(MenuItem item) {
324						switch (item.getItemId()) {
325						case R.id.encryption_choice_none:
326							selConv.nextMessageEncryption = Message.ENCRYPTION_NONE;
327							item.setChecked(true);
328							break;
329						case R.id.encryption_choice_otr:
330							selConv.nextMessageEncryption = Message.ENCRYPTION_OTR;
331							item.setChecked(true);
332							break;
333						case R.id.encryption_choice_pgp:
334							selConv.nextMessageEncryption = Message.ENCRYPTION_PGP;
335							item.setChecked(true);
336							break;
337						default:
338							selConv.nextMessageEncryption = Message.ENCRYPTION_NONE;
339							break;
340						}
341						fragment.updateChatMsgHint();
342						return true;
343					}
344				});
345			    popup.inflate(R.menu.encryption_choices);
346			    switch (selConv.nextMessageEncryption) {
347				case Message.ENCRYPTION_NONE:
348					popup.getMenu().findItem(R.id.encryption_choice_none).setChecked(true);
349					break;
350				case Message.ENCRYPTION_OTR:
351					popup.getMenu().findItem(R.id.encryption_choice_otr).setChecked(true);
352					break;
353				case Message.ENCRYPTION_PGP:
354					popup.getMenu().findItem(R.id.encryption_choice_pgp).setChecked(true);
355					break;
356				case Message.ENCRYPTION_DECRYPTED:
357					popup.getMenu().findItem(R.id.encryption_choice_pgp).setChecked(true);
358					break;
359				default:
360					popup.getMenu().findItem(R.id.encryption_choice_none).setChecked(true);
361					break;
362				}
363			    popup.show();
364		    }
365
366			break;
367		default:
368			break;
369		}
370		return super.onOptionsItemSelected(item);
371	}
372
373	protected ConversationFragment swapConversationFragment() {
374		ConversationFragment selectedFragment = new ConversationFragment();
375		
376		FragmentTransaction transaction = getFragmentManager()
377				.beginTransaction();
378		transaction.replace(R.id.selected_conversation, selectedFragment,"conversation");
379		transaction.commit();
380		return selectedFragment;
381	}
382
383	@Override
384	public boolean onKeyDown(int keyCode, KeyEvent event) {
385		if (keyCode == KeyEvent.KEYCODE_BACK) {
386			if (!spl.isOpen()) {
387				spl.openPane();
388				return false;
389			}
390		}
391		return super.onKeyDown(keyCode, event);
392	}
393	
394	public void onStart() {
395		super.onStart();
396		NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
397		nm.cancelAll();
398		if (conversationList.size()>=1) {
399			onConvChanged.onConversationListChanged();
400		}
401	}
402	
403	@Override
404	protected void onStop() {
405		Log.d("gultsch","called on stop in conversation activity");
406		if (xmppConnectionServiceBound) {
407        	xmppConnectionService.removeOnConversationListChangedListener();
408		}
409		super.onStop();
410	}
411
412	@Override
413	void onBackendConnected() {
414		
415		xmppConnectionService.setOnConversationListChangedListener(this.onConvChanged);
416		
417		if (conversationList.size()==0) {
418			conversationList.clear();
419			conversationList.addAll(xmppConnectionService
420					.getConversations());
421			
422			this.updateConversationList();
423		}
424
425		if ((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				swapConversationFragment();
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, NewConversationActivity.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					Log.d("gultsch","ConversationActivity. found old fragment.");
454					selectedFragment.onBackendConnected();
455				} else {
456					Log.d("gultsch","conversationactivity. no old fragment found. creating new one");
457					selectedConversation = conversationList.get(0);
458					Log.d("gultsch","selected conversation is #"+selectedConversation);
459					swapConversationFragment();
460				}
461			}
462		}
463	}
464	 @Override
465	 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
466		 super.onActivityResult(requestCode, resultCode, data);
467		 if (resultCode == RESULT_OK) {
468			if (requestCode == REQUEST_DECRYPT_PGP) {
469				ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager().findFragmentByTag("conversation");
470				if (selectedFragment!=null) {
471					selectedFragment.hidePgpPassphraseBox();
472				}
473			}
474		 }
475	 }
476}