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		invalidateOptionsMenu();
317		setTitle(contact.getDisplayName());
318		if (contact.showInRoster()) {
319			send.setVisibility(View.VISIBLE);
320			receive.setVisibility(View.VISIBLE);
321			addContactButton.setVisibility(View.GONE);
322			send.setOnCheckedChangeListener(null);
323			receive.setOnCheckedChangeListener(null);
324
325			List<String> statusMessages = contact.getPresences().getStatusMessages();
326			if (statusMessages.size() == 0) {
327				statusMessage.setVisibility(View.GONE);
328			} else {
329				StringBuilder builder = new StringBuilder();
330				statusMessage.setVisibility(View.VISIBLE);
331				int s = statusMessages.size();
332				for(int i = 0; i < s; ++i) {
333					if (s > 1) {
334						builder.append("");
335					}
336					builder.append(statusMessages.get(i));
337					if (i < s - 1) {
338						builder.append("\n");
339					}
340				}
341				statusMessage.setText(builder);
342			}
343
344			if (contact.getOption(Contact.Options.FROM)) {
345				send.setText(R.string.send_presence_updates);
346				send.setChecked(true);
347			} else if (contact.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
348				send.setChecked(false);
349				send.setText(R.string.send_presence_updates);
350			} else {
351				send.setText(R.string.preemptively_grant);
352				if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
353					send.setChecked(true);
354				} else {
355					send.setChecked(false);
356				}
357			}
358			if (contact.getOption(Contact.Options.TO)) {
359				receive.setText(R.string.receive_presence_updates);
360				receive.setChecked(true);
361			} else {
362				receive.setText(R.string.ask_for_presence_updates);
363				if (contact.getOption(Contact.Options.ASKING)) {
364					receive.setChecked(true);
365				} else {
366					receive.setChecked(false);
367				}
368			}
369			if (contact.getAccount().isOnlineAndConnected()) {
370				receive.setEnabled(true);
371				send.setEnabled(true);
372			} else {
373				receive.setEnabled(false);
374				send.setEnabled(false);
375			}
376			send.setOnCheckedChangeListener(this.mOnSendCheckedChange);
377			receive.setOnCheckedChangeListener(this.mOnReceiveCheckedChange);
378		} else {
379			addContactButton.setVisibility(View.VISIBLE);
380			send.setVisibility(View.GONE);
381			receive.setVisibility(View.GONE);
382			statusMessage.setVisibility(View.GONE);
383		}
384
385		if (contact.isBlocked() && !this.showDynamicTags) {
386			lastseen.setVisibility(View.VISIBLE);
387			lastseen.setText(R.string.contact_blocked);
388		} else {
389			if (showLastSeen && contact.getLastseen() > 0) {
390				lastseen.setVisibility(View.VISIBLE);
391				lastseen.setText(UIHelper.lastseen(getApplicationContext(), contact.isActive(), contact.getLastseen()));
392			} else {
393				lastseen.setVisibility(View.GONE);
394			}
395		}
396
397		if (contact.getPresences().size() > 1) {
398			contactJidTv.setText(contact.getDisplayJid() + " ("
399					+ contact.getPresences().size() + ")");
400		} else {
401			contactJidTv.setText(contact.getDisplayJid());
402		}
403		String account;
404		if (Config.DOMAIN_LOCK != null) {
405			account = contact.getAccount().getJid().getLocalpart();
406		} else {
407			account = contact.getAccount().getJid().toBareJid().toString();
408		}
409		accountJidTv.setText(getString(R.string.using_account, account));
410		badge.setImageBitmap(avatarService().get(contact, getPixel(72)));
411		badge.setOnClickListener(this.onBadgeClick);
412
413		keys.removeAllViews();
414		boolean hasKeys = false;
415		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
416		if (Config.supportOtr()) {
417			for (final String otrFingerprint : contact.getOtrFingerprints()) {
418				hasKeys = true;
419				View view = inflater.inflate(R.layout.contact_key, keys, false);
420				TextView key = (TextView) view.findViewById(R.id.key);
421				TextView keyType = (TextView) view.findViewById(R.id.key_type);
422				ImageButton removeButton = (ImageButton) view
423						.findViewById(R.id.button_remove);
424				removeButton.setVisibility(View.VISIBLE);
425				key.setText(CryptoHelper.prettifyFingerprint(otrFingerprint));
426				if (otrFingerprint != null && otrFingerprint.equals(messageFingerprint)) {
427					keyType.setText(R.string.otr_fingerprint_selected_message);
428					keyType.setTextColor(getResources().getColor(R.color.accent));
429				} else {
430					keyType.setText(R.string.otr_fingerprint);
431				}
432				keys.addView(view);
433				removeButton.setOnClickListener(new OnClickListener() {
434
435					@Override
436					public void onClick(View v) {
437						confirmToDeleteFingerprint(otrFingerprint);
438					}
439				});
440			}
441		}
442		if (Config.supportOmemo()) {
443			for (final String fingerprint : contact.getAccount().getAxolotlService().getFingerprintsForContact(contact)) {
444				boolean highlight = fingerprint.equals(messageFingerprint);
445				hasKeys |= addFingerprintRow(keys, contact.getAccount(), fingerprint, highlight, new OnClickListener() {
446					@Override
447					public void onClick(View v) {
448						onOmemoKeyClicked(contact.getAccount(), fingerprint);
449					}
450				});
451			}
452		}
453		if (Config.supportOpenPgp() && contact.getPgpKeyId() != 0) {
454			hasKeys = true;
455			View view = inflater.inflate(R.layout.contact_key, keys, false);
456			TextView key = (TextView) view.findViewById(R.id.key);
457			TextView keyType = (TextView) view.findViewById(R.id.key_type);
458			keyType.setText(R.string.openpgp_key_id);
459			if ("pgp".equals(messageFingerprint)) {
460				keyType.setTextColor(getResources().getColor(R.color.accent));
461			}
462			key.setText(OpenPgpUtils.convertKeyIdToHex(contact.getPgpKeyId()));
463			view.setOnClickListener(new OnClickListener() {
464
465				@Override
466				public void onClick(View v) {
467					PgpEngine pgp = ContactDetailsActivity.this.xmppConnectionService
468						.getPgpEngine();
469					if (pgp != null) {
470						PendingIntent intent = pgp.getIntentForKey(contact);
471						if (intent != null) {
472							try {
473								startIntentSenderForResult(
474										intent.getIntentSender(), 0, null, 0,
475										0, 0);
476							} catch (SendIntentException e) {
477
478							}
479						}
480					}
481				}
482			});
483			keys.addView(view);
484		}
485		if (hasKeys) {
486			keys.setVisibility(View.VISIBLE);
487		} else {
488			keys.setVisibility(View.GONE);
489		}
490
491		List<ListItem.Tag> tagList = contact.getTags(this);
492		if (tagList.size() == 0 || !this.showDynamicTags) {
493			tags.setVisibility(View.GONE);
494		} else {
495			tags.setVisibility(View.VISIBLE);
496			tags.removeAllViewsInLayout();
497			for(final ListItem.Tag tag : tagList) {
498				final TextView tv = (TextView) inflater.inflate(R.layout.list_item_tag,tags,false);
499				tv.setText(tag.getName());
500				tv.setBackgroundColor(tag.getColor());
501				tags.addView(tv);
502			}
503		}
504	}
505
506	private void onOmemoKeyClicked(Account account, String fingerprint) {
507		final XmppAxolotlSession.Trust trust = account.getAxolotlService().getFingerprintTrust(fingerprint);
508		if (Config.X509_VERIFICATION && trust != null && trust == XmppAxolotlSession.Trust.TRUSTED_X509) {
509			X509Certificate x509Certificate = account.getAxolotlService().getFingerprintCertificate(fingerprint);
510			if (x509Certificate != null) {
511				showCertificateInformationDialog(CryptoHelper.extractCertificateInformation(x509Certificate));
512			} else {
513				Toast.makeText(this,R.string.certificate_not_found, Toast.LENGTH_SHORT).show();
514			}
515		}
516	}
517
518	private void showCertificateInformationDialog(Bundle bundle) {
519		View view = getLayoutInflater().inflate(R.layout.certificate_information, null);
520		final String not_available = getString(R.string.certicate_info_not_available);
521		TextView subject_cn = (TextView) view.findViewById(R.id.subject_cn);
522		TextView subject_o = (TextView) view.findViewById(R.id.subject_o);
523		TextView issuer_cn = (TextView) view.findViewById(R.id.issuer_cn);
524		TextView issuer_o = (TextView) view.findViewById(R.id.issuer_o);
525		TextView sha1 = (TextView) view.findViewById(R.id.sha1);
526
527		subject_cn.setText(bundle.getString("subject_cn", not_available));
528		subject_o.setText(bundle.getString("subject_o", not_available));
529		issuer_cn.setText(bundle.getString("issuer_cn", not_available));
530		issuer_o.setText(bundle.getString("issuer_o", not_available));
531		sha1.setText(bundle.getString("sha1", not_available));
532
533		AlertDialog.Builder builder = new AlertDialog.Builder(this);
534		builder.setTitle(R.string.certificate_information);
535		builder.setView(view);
536		builder.setPositiveButton(R.string.ok, null);
537		builder.create().show();
538	}
539
540	protected void confirmToDeleteFingerprint(final String fingerprint) {
541		AlertDialog.Builder builder = new AlertDialog.Builder(this);
542		builder.setTitle(R.string.delete_fingerprint);
543		builder.setMessage(R.string.sure_delete_fingerprint);
544		builder.setNegativeButton(R.string.cancel, null);
545		builder.setPositiveButton(R.string.delete,
546				new android.content.DialogInterface.OnClickListener() {
547
548					@Override
549					public void onClick(DialogInterface dialog, int which) {
550						if (contact.deleteOtrFingerprint(fingerprint)) {
551							populateView();
552							xmppConnectionService.syncRosterToDisk(contact.getAccount());
553						}
554					}
555
556				});
557		builder.create().show();
558	}
559
560	@Override
561	public void onBackendConnected() {
562		if ((accountJid != null) && (contactJid != null)) {
563			Account account = xmppConnectionService
564				.findAccountByJid(accountJid);
565			if (account == null) {
566				return;
567			}
568			this.contact = account.getRoster().getContact(contactJid);
569			populateView();
570		}
571	}
572
573	@Override
574	public void onKeyStatusUpdated(AxolotlService.FetchStatus report) {
575		refreshUi();
576	}
577}