ContactDetailsActivity.java

  1package eu.siacs.conversations.ui;
  2
  3import android.app.AlertDialog;
  4import android.app.PendingIntent;
  5import android.content.Context;
  6import android.content.DialogInterface;
  7import android.content.Intent;
  8import android.content.IntentSender.SendIntentException;
  9import android.content.SharedPreferences;
 10import android.net.Uri;
 11import android.os.Bundle;
 12import android.preference.PreferenceManager;
 13import android.provider.ContactsContract;
 14import android.provider.ContactsContract.CommonDataKinds;
 15import android.provider.ContactsContract.Contacts;
 16import android.provider.ContactsContract.Intents;
 17import android.view.LayoutInflater;
 18import android.view.Menu;
 19import android.view.MenuItem;
 20import android.view.View;
 21import android.view.View.OnClickListener;
 22import android.widget.Button;
 23import android.widget.CheckBox;
 24import android.widget.CompoundButton;
 25import android.widget.CompoundButton.OnCheckedChangeListener;
 26import android.widget.ImageButton;
 27import android.widget.LinearLayout;
 28import android.widget.QuickContactBadge;
 29import android.widget.TextView;
 30import android.widget.Toast;
 31
 32import org.openintents.openpgp.util.OpenPgpUtils;
 33
 34import java.security.cert.X509Certificate;
 35import java.util.List;
 36
 37import eu.siacs.conversations.Config;
 38import eu.siacs.conversations.R;
 39import eu.siacs.conversations.crypto.PgpEngine;
 40import eu.siacs.conversations.crypto.axolotl.AxolotlService;
 41import eu.siacs.conversations.crypto.axolotl.XmppAxolotlSession;
 42import eu.siacs.conversations.entities.Account;
 43import eu.siacs.conversations.entities.Contact;
 44import eu.siacs.conversations.entities.ListItem;
 45import eu.siacs.conversations.entities.Presence;
 46import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
 47import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate;
 48import eu.siacs.conversations.utils.CryptoHelper;
 49import eu.siacs.conversations.utils.UIHelper;
 50import eu.siacs.conversations.xmpp.OnKeyStatusUpdated;
 51import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
 52import eu.siacs.conversations.xmpp.XmppConnection;
 53import eu.siacs.conversations.xmpp.jid.InvalidJidException;
 54import eu.siacs.conversations.xmpp.jid.Jid;
 55
 56public class ContactDetailsActivity extends XmppActivity implements OnAccountUpdate, OnRosterUpdate, OnUpdateBlocklist, OnKeyStatusUpdated {
 57	public static final String ACTION_VIEW_CONTACT = "view_contact";
 58
 59	private Contact contact;
 60	private DialogInterface.OnClickListener removeFromRoster = new DialogInterface.OnClickListener() {
 61
 62		@Override
 63		public void onClick(DialogInterface dialog, int which) {
 64			xmppConnectionService.deleteContactOnServer(contact);
 65		}
 66	};
 67	private OnCheckedChangeListener mOnSendCheckedChange = new OnCheckedChangeListener() {
 68
 69		@Override
 70		public void onCheckedChanged(CompoundButton buttonView,
 71				boolean isChecked) {
 72			if (isChecked) {
 73				if (contact
 74						.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
 75					xmppConnectionService.sendPresencePacket(contact
 76							.getAccount(),
 77							xmppConnectionService.getPresenceGenerator()
 78							.sendPresenceUpdatesTo(contact));
 79				} else {
 80					contact.setOption(Contact.Options.PREEMPTIVE_GRANT);
 81				}
 82			} else {
 83				contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
 84				xmppConnectionService.sendPresencePacket(contact.getAccount(),
 85						xmppConnectionService.getPresenceGenerator()
 86						.stopPresenceUpdatesTo(contact));
 87			}
 88		}
 89	};
 90	private OnCheckedChangeListener mOnReceiveCheckedChange = new OnCheckedChangeListener() {
 91
 92		@Override
 93		public void onCheckedChanged(CompoundButton buttonView,
 94				boolean isChecked) {
 95			if (isChecked) {
 96				xmppConnectionService.sendPresencePacket(contact.getAccount(),
 97						xmppConnectionService.getPresenceGenerator()
 98						.requestPresenceUpdatesFrom(contact));
 99			} else {
100				xmppConnectionService.sendPresencePacket(contact.getAccount(),
101						xmppConnectionService.getPresenceGenerator()
102						.stopPresenceUpdatesFrom(contact));
103			}
104		}
105	};
106	private Jid accountJid;
107	private TextView lastseen;
108	private Jid contactJid;
109	private TextView contactJidTv;
110	private TextView accountJidTv;
111	private TextView statusMessage;
112	private CheckBox send;
113	private CheckBox receive;
114	private Button addContactButton;
115	private QuickContactBadge badge;
116	private LinearLayout keys;
117	private LinearLayout tags;
118	private boolean showDynamicTags = false;
119	private boolean showLastSeen = false;
120	private String messageFingerprint;
121
122	private DialogInterface.OnClickListener addToPhonebook = new DialogInterface.OnClickListener() {
123
124		@Override
125		public void onClick(DialogInterface dialog, int which) {
126			Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
127			intent.setType(Contacts.CONTENT_ITEM_TYPE);
128			intent.putExtra(Intents.Insert.IM_HANDLE, contact.getJid().toString());
129			intent.putExtra(Intents.Insert.IM_PROTOCOL,
130					CommonDataKinds.Im.PROTOCOL_JABBER);
131			intent.putExtra("finishActivityOnSaveCompleted", true);
132			ContactDetailsActivity.this.startActivityForResult(intent, 0);
133		}
134	};
135
136	private OnClickListener onBadgeClick = new OnClickListener() {
137
138		@Override
139		public void onClick(View v) {
140			if (contact.getSystemAccount() == null) {
141				AlertDialog.Builder builder = new AlertDialog.Builder(
142						ContactDetailsActivity.this);
143				builder.setTitle(getString(R.string.action_add_phone_book));
144				builder.setMessage(getString(R.string.add_phone_book_text,
145						contact.getDisplayJid()));
146				builder.setNegativeButton(getString(R.string.cancel), null);
147				builder.setPositiveButton(getString(R.string.add), addToPhonebook);
148				builder.create().show();
149			} else {
150					String[] systemAccount = contact.getSystemAccount().split("#");
151					long id = Long.parseLong(systemAccount[0]);
152					Uri uri = ContactsContract.Contacts.getLookupUri(id, systemAccount[1]);
153					Intent intent = new Intent(Intent.ACTION_VIEW);
154					intent.setData(uri);
155					startActivity(intent);
156			}
157		}
158	};
159
160	@Override
161	public void onRosterUpdate() {
162		refreshUi();
163	}
164
165	@Override
166	public void onAccountUpdate() {
167		refreshUi();
168	}
169
170	@Override
171	public void OnUpdateBlocklist(final Status status) {
172		refreshUi();
173	}
174
175	@Override
176	protected void refreshUiReal() {
177		invalidateOptionsMenu();
178		populateView();
179	}
180
181	@Override
182	protected String getShareableUri() {
183		if (contact != null) {
184			return "xmpp:"+contact.getJid().toBareJid().toString();
185		} else {
186			return "";
187		}
188	}
189
190	@Override
191	protected void onCreate(final Bundle savedInstanceState) {
192		super.onCreate(savedInstanceState);
193		if (getIntent().getAction().equals(ACTION_VIEW_CONTACT)) {
194			try {
195				this.accountJid = Jid.fromString(getIntent().getExtras().getString(EXTRA_ACCOUNT));
196			} catch (final InvalidJidException ignored) {
197			}
198			try {
199				this.contactJid = Jid.fromString(getIntent().getExtras().getString("contact"));
200			} catch (final InvalidJidException ignored) {
201			}
202		}
203		this.messageFingerprint = getIntent().getStringExtra("fingerprint");
204		setContentView(R.layout.activity_contact_details);
205
206		contactJidTv = (TextView) findViewById(R.id.details_contactjid);
207		accountJidTv = (TextView) findViewById(R.id.details_account);
208		lastseen = (TextView) findViewById(R.id.details_lastseen);
209		statusMessage = (TextView) findViewById(R.id.status_message);
210		send = (CheckBox) findViewById(R.id.details_send_presence);
211		receive = (CheckBox) findViewById(R.id.details_receive_presence);
212		badge = (QuickContactBadge) findViewById(R.id.details_contact_badge);
213		addContactButton = (Button) findViewById(R.id.add_contact_button);
214		addContactButton.setOnClickListener(new OnClickListener() {
215			@Override
216			public void onClick(View view) {
217				showAddToRosterDialog(contact);
218			}
219		});
220		keys = (LinearLayout) findViewById(R.id.details_contact_keys);
221		tags = (LinearLayout) findViewById(R.id.tags);
222		if (getActionBar() != null) {
223			getActionBar().setHomeButtonEnabled(true);
224			getActionBar().setDisplayHomeAsUpEnabled(true);
225		}
226	}
227
228	@Override
229	public void onStart() {
230		super.onStart();
231		final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
232		this.showDynamicTags = preferences.getBoolean("show_dynamic_tags",false);
233		this.showLastSeen = preferences.getBoolean("last_activity", false);
234	}
235
236	@Override
237	public boolean onOptionsItemSelected(final MenuItem menuItem) {
238		final AlertDialog.Builder builder = new AlertDialog.Builder(this);
239		builder.setNegativeButton(getString(R.string.cancel), null);
240		switch (menuItem.getItemId()) {
241			case android.R.id.home:
242				finish();
243				break;
244			case R.id.action_share:
245				shareUri();
246				break;
247			case R.id.action_delete_contact:
248				builder.setTitle(getString(R.string.action_delete_contact))
249					.setMessage(
250							getString(R.string.remove_contact_text,
251								contact.getDisplayJid()))
252					.setPositiveButton(getString(R.string.delete),
253							removeFromRoster).create().show();
254				break;
255			case R.id.action_edit_contact:
256				if (contact.getSystemAccount() == null) {
257					quickEdit(contact.getDisplayName(), 0, new OnValueEdited() {
258
259						@Override
260						public void onValueEdited(String value) {
261							contact.setServerName(value);
262							ContactDetailsActivity.this.xmppConnectionService
263								.pushContactToServer(contact);
264							populateView();
265						}
266					});
267				} else {
268					Intent intent = new Intent(Intent.ACTION_EDIT);
269					String[] systemAccount = contact.getSystemAccount().split("#");
270					long id = Long.parseLong(systemAccount[0]);
271					Uri uri = Contacts.getLookupUri(id, systemAccount[1]);
272					intent.setDataAndType(uri, Contacts.CONTENT_ITEM_TYPE);
273					intent.putExtra("finishActivityOnSaveCompleted", true);
274					startActivity(intent);
275				}
276				break;
277			case R.id.action_block:
278				BlockContactDialog.show(this, xmppConnectionService, contact);
279				break;
280			case R.id.action_unblock:
281				BlockContactDialog.show(this, xmppConnectionService, contact);
282				break;
283		}
284		return super.onOptionsItemSelected(menuItem);
285	}
286
287	@Override
288	public boolean onCreateOptionsMenu(final Menu menu) {
289		getMenuInflater().inflate(R.menu.contact_details, menu);
290		MenuItem block = menu.findItem(R.id.action_block);
291		MenuItem unblock = menu.findItem(R.id.action_unblock);
292		MenuItem edit = menu.findItem(R.id.action_edit_contact);
293		MenuItem delete = menu.findItem(R.id.action_delete_contact);
294		if (contact == null) {
295			return true;
296		}
297		final XmppConnection connection = contact.getAccount().getXmppConnection();
298		if (connection != null && connection.getFeatures().blocking()) {
299			if (this.contact.isBlocked()) {
300				block.setVisible(false);
301			} else {
302				unblock.setVisible(false);
303			}
304		} else {
305			unblock.setVisible(false);
306			block.setVisible(false);
307		}
308		if (!contact.showInRoster()) {
309			edit.setVisible(false);
310			delete.setVisible(false);
311		}
312		return super.onCreateOptionsMenu(menu);
313	}
314
315	private void populateView() {
316		if (contact == null) {
317			return;
318		}
319		invalidateOptionsMenu();
320		setTitle(contact.getDisplayName());
321		if (contact.showInRoster()) {
322			send.setVisibility(View.VISIBLE);
323			receive.setVisibility(View.VISIBLE);
324			addContactButton.setVisibility(View.GONE);
325			send.setOnCheckedChangeListener(null);
326			receive.setOnCheckedChangeListener(null);
327
328			List<String> statusMessages = contact.getPresences().getStatusMessages();
329			if (statusMessages.size() == 0) {
330				statusMessage.setVisibility(View.GONE);
331			} else {
332				StringBuilder builder = new StringBuilder();
333				statusMessage.setVisibility(View.VISIBLE);
334				int s = statusMessages.size();
335				for(int i = 0; i < s; ++i) {
336					if (s > 1) {
337						builder.append("");
338					}
339					builder.append(statusMessages.get(i));
340					if (i < s - 1) {
341						builder.append("\n");
342					}
343				}
344				statusMessage.setText(builder);
345			}
346
347			if (contact.getOption(Contact.Options.FROM)) {
348				send.setText(R.string.send_presence_updates);
349				send.setChecked(true);
350			} else if (contact.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
351				send.setChecked(false);
352				send.setText(R.string.send_presence_updates);
353			} else {
354				send.setText(R.string.preemptively_grant);
355				if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
356					send.setChecked(true);
357				} else {
358					send.setChecked(false);
359				}
360			}
361			if (contact.getOption(Contact.Options.TO)) {
362				receive.setText(R.string.receive_presence_updates);
363				receive.setChecked(true);
364			} else {
365				receive.setText(R.string.ask_for_presence_updates);
366				if (contact.getOption(Contact.Options.ASKING)) {
367					receive.setChecked(true);
368				} else {
369					receive.setChecked(false);
370				}
371			}
372			if (contact.getAccount().isOnlineAndConnected()) {
373				receive.setEnabled(true);
374				send.setEnabled(true);
375			} else {
376				receive.setEnabled(false);
377				send.setEnabled(false);
378			}
379			send.setOnCheckedChangeListener(this.mOnSendCheckedChange);
380			receive.setOnCheckedChangeListener(this.mOnReceiveCheckedChange);
381		} else {
382			addContactButton.setVisibility(View.VISIBLE);
383			send.setVisibility(View.GONE);
384			receive.setVisibility(View.GONE);
385			statusMessage.setVisibility(View.GONE);
386		}
387
388		if (contact.isBlocked() && !this.showDynamicTags) {
389			lastseen.setVisibility(View.VISIBLE);
390			lastseen.setText(R.string.contact_blocked);
391		} else {
392			if (showLastSeen && contact.getLastseen() > 0) {
393				lastseen.setVisibility(View.VISIBLE);
394				lastseen.setText(UIHelper.lastseen(getApplicationContext(), contact.isActive(), contact.getLastseen()));
395			} else {
396				lastseen.setVisibility(View.GONE);
397			}
398		}
399
400		if (contact.getPresences().size() > 1) {
401			contactJidTv.setText(contact.getDisplayJid() + " ("
402					+ contact.getPresences().size() + ")");
403		} else {
404			contactJidTv.setText(contact.getDisplayJid());
405		}
406		String account;
407		if (Config.DOMAIN_LOCK != null) {
408			account = contact.getAccount().getJid().getLocalpart();
409		} else {
410			account = contact.getAccount().getJid().toBareJid().toString();
411		}
412		accountJidTv.setText(getString(R.string.using_account, account));
413		badge.setImageBitmap(avatarService().get(contact, getPixel(72)));
414		badge.setOnClickListener(this.onBadgeClick);
415
416		keys.removeAllViews();
417		boolean hasKeys = false;
418		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
419		if (Config.supportOtr()) {
420			for (final String otrFingerprint : contact.getOtrFingerprints()) {
421				hasKeys = true;
422				View view = inflater.inflate(R.layout.contact_key, keys, false);
423				TextView key = (TextView) view.findViewById(R.id.key);
424				TextView keyType = (TextView) view.findViewById(R.id.key_type);
425				ImageButton removeButton = (ImageButton) view
426						.findViewById(R.id.button_remove);
427				removeButton.setVisibility(View.VISIBLE);
428				key.setText(CryptoHelper.prettifyFingerprint(otrFingerprint));
429				if (otrFingerprint != null && otrFingerprint.equals(messageFingerprint)) {
430					keyType.setText(R.string.otr_fingerprint_selected_message);
431					keyType.setTextColor(getResources().getColor(R.color.accent));
432				} else {
433					keyType.setText(R.string.otr_fingerprint);
434				}
435				keys.addView(view);
436				removeButton.setOnClickListener(new OnClickListener() {
437
438					@Override
439					public void onClick(View v) {
440						confirmToDeleteFingerprint(otrFingerprint);
441					}
442				});
443			}
444		}
445		if (Config.supportOmemo()) {
446			for (final String fingerprint : contact.getAccount().getAxolotlService().getFingerprintsForContact(contact)) {
447				boolean highlight = fingerprint.equals(messageFingerprint);
448				hasKeys |= addFingerprintRow(keys, contact.getAccount(), fingerprint, highlight, new OnClickListener() {
449					@Override
450					public void onClick(View v) {
451						onOmemoKeyClicked(contact.getAccount(), fingerprint);
452					}
453				});
454			}
455		}
456		if (Config.supportOpenPgp() && contact.getPgpKeyId() != 0) {
457			hasKeys = true;
458			View view = inflater.inflate(R.layout.contact_key, keys, false);
459			TextView key = (TextView) view.findViewById(R.id.key);
460			TextView keyType = (TextView) view.findViewById(R.id.key_type);
461			keyType.setText(R.string.openpgp_key_id);
462			if ("pgp".equals(messageFingerprint)) {
463				keyType.setTextColor(getResources().getColor(R.color.accent));
464			}
465			key.setText(OpenPgpUtils.convertKeyIdToHex(contact.getPgpKeyId()));
466			view.setOnClickListener(new OnClickListener() {
467
468				@Override
469				public void onClick(View v) {
470					PgpEngine pgp = ContactDetailsActivity.this.xmppConnectionService
471						.getPgpEngine();
472					if (pgp != null) {
473						PendingIntent intent = pgp.getIntentForKey(contact);
474						if (intent != null) {
475							try {
476								startIntentSenderForResult(
477										intent.getIntentSender(), 0, null, 0,
478										0, 0);
479							} catch (SendIntentException e) {
480
481							}
482						}
483					}
484				}
485			});
486			keys.addView(view);
487		}
488		if (hasKeys) {
489			keys.setVisibility(View.VISIBLE);
490		} else {
491			keys.setVisibility(View.GONE);
492		}
493
494		List<ListItem.Tag> tagList = contact.getTags(this);
495		if (tagList.size() == 0 || !this.showDynamicTags) {
496			tags.setVisibility(View.GONE);
497		} else {
498			tags.setVisibility(View.VISIBLE);
499			tags.removeAllViewsInLayout();
500			for(final ListItem.Tag tag : tagList) {
501				final TextView tv = (TextView) inflater.inflate(R.layout.list_item_tag,tags,false);
502				tv.setText(tag.getName());
503				tv.setBackgroundColor(tag.getColor());
504				tags.addView(tv);
505			}
506		}
507	}
508
509	private void onOmemoKeyClicked(Account account, String fingerprint) {
510		final XmppAxolotlSession.Trust trust = account.getAxolotlService().getFingerprintTrust(fingerprint);
511		if (Config.X509_VERIFICATION && trust != null && trust == XmppAxolotlSession.Trust.TRUSTED_X509) {
512			X509Certificate x509Certificate = account.getAxolotlService().getFingerprintCertificate(fingerprint);
513			if (x509Certificate != null) {
514				showCertificateInformationDialog(CryptoHelper.extractCertificateInformation(x509Certificate));
515			} else {
516				Toast.makeText(this,R.string.certificate_not_found, Toast.LENGTH_SHORT).show();
517			}
518		}
519	}
520
521	private void showCertificateInformationDialog(Bundle bundle) {
522		View view = getLayoutInflater().inflate(R.layout.certificate_information, null);
523		final String not_available = getString(R.string.certicate_info_not_available);
524		TextView subject_cn = (TextView) view.findViewById(R.id.subject_cn);
525		TextView subject_o = (TextView) view.findViewById(R.id.subject_o);
526		TextView issuer_cn = (TextView) view.findViewById(R.id.issuer_cn);
527		TextView issuer_o = (TextView) view.findViewById(R.id.issuer_o);
528		TextView sha1 = (TextView) view.findViewById(R.id.sha1);
529
530		subject_cn.setText(bundle.getString("subject_cn", not_available));
531		subject_o.setText(bundle.getString("subject_o", not_available));
532		issuer_cn.setText(bundle.getString("issuer_cn", not_available));
533		issuer_o.setText(bundle.getString("issuer_o", not_available));
534		sha1.setText(bundle.getString("sha1", not_available));
535
536		AlertDialog.Builder builder = new AlertDialog.Builder(this);
537		builder.setTitle(R.string.certificate_information);
538		builder.setView(view);
539		builder.setPositiveButton(R.string.ok, null);
540		builder.create().show();
541	}
542
543	protected void confirmToDeleteFingerprint(final String fingerprint) {
544		AlertDialog.Builder builder = new AlertDialog.Builder(this);
545		builder.setTitle(R.string.delete_fingerprint);
546		builder.setMessage(R.string.sure_delete_fingerprint);
547		builder.setNegativeButton(R.string.cancel, null);
548		builder.setPositiveButton(R.string.delete,
549				new android.content.DialogInterface.OnClickListener() {
550
551					@Override
552					public void onClick(DialogInterface dialog, int which) {
553						if (contact.deleteOtrFingerprint(fingerprint)) {
554							populateView();
555							xmppConnectionService.syncRosterToDisk(contact.getAccount());
556						}
557					}
558
559				});
560		builder.create().show();
561	}
562
563	@Override
564	public void onBackendConnected() {
565		if ((accountJid != null) && (contactJid != null)) {
566			Account account = xmppConnectionService
567				.findAccountByJid(accountJid);
568			if (account == null) {
569				return;
570			}
571			this.contact = account.getRoster().getContact(contactJid);
572			populateView();
573		}
574	}
575
576	@Override
577	public void onKeyStatusUpdated(AxolotlService.FetchStatus report) {
578		refreshUi();
579	}
580}