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 contact.getShareableUri();
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_delete_contact:
245				builder.setTitle(getString(R.string.action_delete_contact))
246					.setMessage(
247							getString(R.string.remove_contact_text,
248								contact.getDisplayJid()))
249					.setPositiveButton(getString(R.string.delete),
250							removeFromRoster).create().show();
251				break;
252			case R.id.action_edit_contact:
253				if (contact.getSystemAccount() == null) {
254					quickEdit(contact.getDisplayName(), 0, new OnValueEdited() {
255
256						@Override
257						public void onValueEdited(String value) {
258							contact.setServerName(value);
259							ContactDetailsActivity.this.xmppConnectionService
260								.pushContactToServer(contact);
261							populateView();
262						}
263					});
264				} else {
265					Intent intent = new Intent(Intent.ACTION_EDIT);
266					String[] systemAccount = contact.getSystemAccount().split("#");
267					long id = Long.parseLong(systemAccount[0]);
268					Uri uri = Contacts.getLookupUri(id, systemAccount[1]);
269					intent.setDataAndType(uri, Contacts.CONTENT_ITEM_TYPE);
270					intent.putExtra("finishActivityOnSaveCompleted", true);
271					startActivity(intent);
272				}
273				break;
274			case R.id.action_block:
275				BlockContactDialog.show(this, xmppConnectionService, contact);
276				break;
277			case R.id.action_unblock:
278				BlockContactDialog.show(this, xmppConnectionService, contact);
279				break;
280		}
281		return super.onOptionsItemSelected(menuItem);
282	}
283
284	@Override
285	public boolean onCreateOptionsMenu(final Menu menu) {
286		getMenuInflater().inflate(R.menu.contact_details, menu);
287		MenuItem block = menu.findItem(R.id.action_block);
288		MenuItem unblock = menu.findItem(R.id.action_unblock);
289		MenuItem edit = menu.findItem(R.id.action_edit_contact);
290		MenuItem delete = menu.findItem(R.id.action_delete_contact);
291		if (contact == null) {
292			return true;
293		}
294		final XmppConnection connection = contact.getAccount().getXmppConnection();
295		if (connection != null && connection.getFeatures().blocking()) {
296			if (this.contact.isBlocked()) {
297				block.setVisible(false);
298			} else {
299				unblock.setVisible(false);
300			}
301		} else {
302			unblock.setVisible(false);
303			block.setVisible(false);
304		}
305		if (!contact.showInRoster()) {
306			edit.setVisible(false);
307			delete.setVisible(false);
308		}
309		return super.onCreateOptionsMenu(menu);
310	}
311
312	private void populateView() {
313		invalidateOptionsMenu();
314		setTitle(contact.getDisplayName());
315		if (contact.showInRoster()) {
316			send.setVisibility(View.VISIBLE);
317			receive.setVisibility(View.VISIBLE);
318			addContactButton.setVisibility(View.GONE);
319			send.setOnCheckedChangeListener(null);
320			receive.setOnCheckedChangeListener(null);
321
322			List<String> statusMessages = contact.getPresences().getStatusMessages();
323			if (statusMessages.size() == 0) {
324				statusMessage.setVisibility(View.GONE);
325			} else {
326				StringBuilder builder = new StringBuilder();
327				statusMessage.setVisibility(View.VISIBLE);
328				int s = statusMessages.size();
329				for(int i = 0; i < s; ++i) {
330					if (s > 1) {
331						builder.append("");
332					}
333					builder.append(statusMessages.get(i));
334					if (i < s - 1) {
335						builder.append("\n");
336					}
337				}
338				statusMessage.setText(builder);
339			}
340
341			if (contact.getOption(Contact.Options.FROM)) {
342				send.setText(R.string.send_presence_updates);
343				send.setChecked(true);
344			} else if (contact.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
345				send.setChecked(false);
346				send.setText(R.string.send_presence_updates);
347			} else {
348				send.setText(R.string.preemptively_grant);
349				if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
350					send.setChecked(true);
351				} else {
352					send.setChecked(false);
353				}
354			}
355			if (contact.getOption(Contact.Options.TO)) {
356				receive.setText(R.string.receive_presence_updates);
357				receive.setChecked(true);
358			} else {
359				receive.setText(R.string.ask_for_presence_updates);
360				if (contact.getOption(Contact.Options.ASKING)) {
361					receive.setChecked(true);
362				} else {
363					receive.setChecked(false);
364				}
365			}
366			if (contact.getAccount().isOnlineAndConnected()) {
367				receive.setEnabled(true);
368				send.setEnabled(true);
369			} else {
370				receive.setEnabled(false);
371				send.setEnabled(false);
372			}
373			send.setOnCheckedChangeListener(this.mOnSendCheckedChange);
374			receive.setOnCheckedChangeListener(this.mOnReceiveCheckedChange);
375		} else {
376			addContactButton.setVisibility(View.VISIBLE);
377			send.setVisibility(View.GONE);
378			receive.setVisibility(View.GONE);
379			statusMessage.setVisibility(View.GONE);
380		}
381
382		if (contact.isBlocked() && !this.showDynamicTags) {
383			lastseen.setVisibility(View.VISIBLE);
384			lastseen.setText(R.string.contact_blocked);
385		} else {
386			if (showLastSeen && contact.getLastseen() > 0) {
387				lastseen.setVisibility(View.VISIBLE);
388				lastseen.setText(UIHelper.lastseen(getApplicationContext(), contact.isActive(), contact.getLastseen()));
389			} else {
390				lastseen.setVisibility(View.GONE);
391			}
392		}
393
394		if (contact.getPresences().size() > 1) {
395			contactJidTv.setText(contact.getDisplayJid() + " ("
396					+ contact.getPresences().size() + ")");
397		} else {
398			contactJidTv.setText(contact.getDisplayJid());
399		}
400		String account;
401		if (Config.DOMAIN_LOCK != null) {
402			account = contact.getAccount().getJid().getLocalpart();
403		} else {
404			account = contact.getAccount().getJid().toBareJid().toString();
405		}
406		accountJidTv.setText(getString(R.string.using_account, account));
407		badge.setImageBitmap(avatarService().get(contact, getPixel(72)));
408		badge.setOnClickListener(this.onBadgeClick);
409
410		keys.removeAllViews();
411		boolean hasKeys = false;
412		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
413		if (Config.supportOtr()) {
414			for (final String otrFingerprint : contact.getOtrFingerprints()) {
415				hasKeys = true;
416				View view = inflater.inflate(R.layout.contact_key, keys, false);
417				TextView key = (TextView) view.findViewById(R.id.key);
418				TextView keyType = (TextView) view.findViewById(R.id.key_type);
419				ImageButton removeButton = (ImageButton) view
420						.findViewById(R.id.button_remove);
421				removeButton.setVisibility(View.VISIBLE);
422				keyType.setText("OTR Fingerprint");
423				key.setText(CryptoHelper.prettifyFingerprint(otrFingerprint));
424				keys.addView(view);
425				removeButton.setOnClickListener(new OnClickListener() {
426
427					@Override
428					public void onClick(View v) {
429						confirmToDeleteFingerprint(otrFingerprint);
430					}
431				});
432			}
433		}
434		if (Config.supportOmemo()) {
435			for (final String fingerprint : contact.getAccount().getAxolotlService().getFingerprintsForContact(contact)) {
436				boolean highlight = fingerprint.equals(messageFingerprint);
437				hasKeys |= addFingerprintRow(keys, contact.getAccount(), fingerprint, highlight, new OnClickListener() {
438					@Override
439					public void onClick(View v) {
440						onOmemoKeyClicked(contact.getAccount(), fingerprint);
441					}
442				});
443			}
444		}
445		if (Config.supportOpenPgp() && contact.getPgpKeyId() != 0) {
446			hasKeys = true;
447			View view = inflater.inflate(R.layout.contact_key, keys, false);
448			TextView key = (TextView) view.findViewById(R.id.key);
449			TextView keyType = (TextView) view.findViewById(R.id.key_type);
450			keyType.setText("PGP Key ID");
451			key.setText(OpenPgpUtils.convertKeyIdToHex(contact.getPgpKeyId()));
452			view.setOnClickListener(new OnClickListener() {
453
454				@Override
455				public void onClick(View v) {
456					PgpEngine pgp = ContactDetailsActivity.this.xmppConnectionService
457						.getPgpEngine();
458					if (pgp != null) {
459						PendingIntent intent = pgp.getIntentForKey(contact);
460						if (intent != null) {
461							try {
462								startIntentSenderForResult(
463										intent.getIntentSender(), 0, null, 0,
464										0, 0);
465							} catch (SendIntentException e) {
466
467							}
468						}
469					}
470				}
471			});
472			keys.addView(view);
473		}
474		if (hasKeys) {
475			keys.setVisibility(View.VISIBLE);
476		} else {
477			keys.setVisibility(View.GONE);
478		}
479
480		List<ListItem.Tag> tagList = contact.getTags(this);
481		if (tagList.size() == 0 || !this.showDynamicTags) {
482			tags.setVisibility(View.GONE);
483		} else {
484			tags.setVisibility(View.VISIBLE);
485			tags.removeAllViewsInLayout();
486			for(final ListItem.Tag tag : tagList) {
487				final TextView tv = (TextView) inflater.inflate(R.layout.list_item_tag,tags,false);
488				tv.setText(tag.getName());
489				tv.setBackgroundColor(tag.getColor());
490				tags.addView(tv);
491			}
492		}
493	}
494
495	private void onOmemoKeyClicked(Account account, String fingerprint) {
496		final XmppAxolotlSession.Trust trust = account.getAxolotlService().getFingerprintTrust(fingerprint);
497		if (Config.X509_VERIFICATION && trust != null && trust == XmppAxolotlSession.Trust.TRUSTED_X509) {
498			X509Certificate x509Certificate = account.getAxolotlService().getFingerprintCertificate(fingerprint);
499			if (x509Certificate != null) {
500				showCertificateInformationDialog(CryptoHelper.extractCertificateInformation(x509Certificate));
501			} else {
502				Toast.makeText(this,R.string.certificate_not_found, Toast.LENGTH_SHORT).show();
503			}
504		}
505	}
506
507	private void showCertificateInformationDialog(Bundle bundle) {
508		View view = getLayoutInflater().inflate(R.layout.certificate_information, null);
509		final String not_available = getString(R.string.certicate_info_not_available);
510		TextView subject_cn = (TextView) view.findViewById(R.id.subject_cn);
511		TextView subject_o = (TextView) view.findViewById(R.id.subject_o);
512		TextView issuer_cn = (TextView) view.findViewById(R.id.issuer_cn);
513		TextView issuer_o = (TextView) view.findViewById(R.id.issuer_o);
514		TextView sha1 = (TextView) view.findViewById(R.id.sha1);
515
516		subject_cn.setText(bundle.getString("subject_cn", not_available));
517		subject_o.setText(bundle.getString("subject_o", not_available));
518		issuer_cn.setText(bundle.getString("issuer_cn", not_available));
519		issuer_o.setText(bundle.getString("issuer_o", not_available));
520		sha1.setText(bundle.getString("sha1", not_available));
521
522		AlertDialog.Builder builder = new AlertDialog.Builder(this);
523		builder.setTitle(R.string.certificate_information);
524		builder.setView(view);
525		builder.setPositiveButton(R.string.ok, null);
526		builder.create().show();
527	}
528
529	protected void confirmToDeleteFingerprint(final String fingerprint) {
530		AlertDialog.Builder builder = new AlertDialog.Builder(this);
531		builder.setTitle(R.string.delete_fingerprint);
532		builder.setMessage(R.string.sure_delete_fingerprint);
533		builder.setNegativeButton(R.string.cancel, null);
534		builder.setPositiveButton(R.string.delete,
535				new android.content.DialogInterface.OnClickListener() {
536
537					@Override
538					public void onClick(DialogInterface dialog, int which) {
539						if (contact.deleteOtrFingerprint(fingerprint)) {
540							populateView();
541							xmppConnectionService.syncRosterToDisk(contact.getAccount());
542						}
543					}
544
545				});
546		builder.create().show();
547	}
548
549	@Override
550	public void onBackendConnected() {
551		if ((accountJid != null) && (contactJid != null)) {
552			Account account = xmppConnectionService
553				.findAccountByJid(accountJid);
554			if (account == null) {
555				return;
556			}
557			this.contact = account.getRoster().getContact(contactJid);
558			populateView();
559		}
560	}
561
562	@Override
563	public void onKeyStatusUpdated(AxolotlService.FetchStatus report) {
564		refreshUi();
565	}
566}