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 Jid contactJid;
108	private TextView contactJidTv;
109	private TextView accountJidTv;
110	private TextView statusMessage;
111	private CheckBox send;
112	private CheckBox receive;
113	private Button addContactButton;
114	private QuickContactBadge badge;
115	private LinearLayout keys;
116	private LinearLayout tags;
117	private boolean showDynamicTags;
118	private String messageFingerprint;
119
120	private DialogInterface.OnClickListener addToPhonebook = new DialogInterface.OnClickListener() {
121
122		@Override
123		public void onClick(DialogInterface dialog, int which) {
124			Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
125			intent.setType(Contacts.CONTENT_ITEM_TYPE);
126			intent.putExtra(Intents.Insert.IM_HANDLE, contact.getJid().toString());
127			intent.putExtra(Intents.Insert.IM_PROTOCOL,
128					CommonDataKinds.Im.PROTOCOL_JABBER);
129			intent.putExtra("finishActivityOnSaveCompleted", true);
130			ContactDetailsActivity.this.startActivityForResult(intent, 0);
131		}
132	};
133
134	private OnClickListener onBadgeClick = new OnClickListener() {
135
136		@Override
137		public void onClick(View v) {
138			if (contact.getSystemAccount() == null) {
139				AlertDialog.Builder builder = new AlertDialog.Builder(
140						ContactDetailsActivity.this);
141				builder.setTitle(getString(R.string.action_add_phone_book));
142				builder.setMessage(getString(R.string.add_phone_book_text,
143						contact.getDisplayJid()));
144				builder.setNegativeButton(getString(R.string.cancel), null);
145				builder.setPositiveButton(getString(R.string.add), addToPhonebook);
146				builder.create().show();
147			} else {
148					String[] systemAccount = contact.getSystemAccount().split("#");
149					long id = Long.parseLong(systemAccount[0]);
150					Uri uri = ContactsContract.Contacts.getLookupUri(id, systemAccount[1]);
151					Intent intent = new Intent(Intent.ACTION_VIEW);
152					intent.setData(uri);
153					startActivity(intent);
154			}
155		}
156	};
157
158	@Override
159	public void onRosterUpdate() {
160		refreshUi();
161	}
162
163	@Override
164	public void onAccountUpdate() {
165		refreshUi();
166	}
167
168	@Override
169	public void OnUpdateBlocklist(final Status status) {
170		refreshUi();
171	}
172
173	@Override
174	protected void refreshUiReal() {
175		invalidateOptionsMenu();
176		populateView();
177	}
178
179	@Override
180	protected String getShareableUri() {
181		if (contact != null) {
182			return contact.getShareableUri();
183		} else {
184			return "";
185		}
186	}
187
188	@Override
189	protected void onCreate(final Bundle savedInstanceState) {
190		super.onCreate(savedInstanceState);
191		if (getIntent().getAction().equals(ACTION_VIEW_CONTACT)) {
192			try {
193				this.accountJid = Jid.fromString(getIntent().getExtras().getString(EXTRA_ACCOUNT));
194			} catch (final InvalidJidException ignored) {
195			}
196			try {
197				this.contactJid = Jid.fromString(getIntent().getExtras().getString("contact"));
198			} catch (final InvalidJidException ignored) {
199			}
200		}
201		this.messageFingerprint = getIntent().getStringExtra("fingerprint");
202		setContentView(R.layout.activity_contact_details);
203
204		contactJidTv = (TextView) findViewById(R.id.details_contactjid);
205		accountJidTv = (TextView) findViewById(R.id.details_account);
206		statusMessage = (TextView) findViewById(R.id.status_message);
207		send = (CheckBox) findViewById(R.id.details_send_presence);
208		receive = (CheckBox) findViewById(R.id.details_receive_presence);
209		badge = (QuickContactBadge) findViewById(R.id.details_contact_badge);
210		addContactButton = (Button) findViewById(R.id.add_contact_button);
211		addContactButton.setOnClickListener(new OnClickListener() {
212			@Override
213			public void onClick(View view) {
214				showAddToRosterDialog(contact);
215			}
216		});
217		keys = (LinearLayout) findViewById(R.id.details_contact_keys);
218		tags = (LinearLayout) findViewById(R.id.tags);
219		if (getActionBar() != null) {
220			getActionBar().setHomeButtonEnabled(true);
221			getActionBar().setDisplayHomeAsUpEnabled(true);
222		}
223
224		final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
225		this.showDynamicTags = preferences.getBoolean("show_dynamic_tags",false);
226	}
227
228	@Override
229	public boolean onOptionsItemSelected(final MenuItem menuItem) {
230		final AlertDialog.Builder builder = new AlertDialog.Builder(this);
231		builder.setNegativeButton(getString(R.string.cancel), null);
232		switch (menuItem.getItemId()) {
233			case android.R.id.home:
234				finish();
235				break;
236			case R.id.action_delete_contact:
237				builder.setTitle(getString(R.string.action_delete_contact))
238					.setMessage(
239							getString(R.string.remove_contact_text,
240								contact.getDisplayJid()))
241					.setPositiveButton(getString(R.string.delete),
242							removeFromRoster).create().show();
243				break;
244			case R.id.action_edit_contact:
245				if (contact.getSystemAccount() == null) {
246					quickEdit(contact.getDisplayName(), 0, new OnValueEdited() {
247
248						@Override
249						public void onValueEdited(String value) {
250							contact.setServerName(value);
251							ContactDetailsActivity.this.xmppConnectionService
252								.pushContactToServer(contact);
253							populateView();
254						}
255					});
256				} else {
257					Intent intent = new Intent(Intent.ACTION_EDIT);
258					String[] systemAccount = contact.getSystemAccount().split("#");
259					long id = Long.parseLong(systemAccount[0]);
260					Uri uri = Contacts.getLookupUri(id, systemAccount[1]);
261					intent.setDataAndType(uri, Contacts.CONTENT_ITEM_TYPE);
262					intent.putExtra("finishActivityOnSaveCompleted", true);
263					startActivity(intent);
264				}
265				break;
266			case R.id.action_block:
267				BlockContactDialog.show(this, xmppConnectionService, contact);
268				break;
269			case R.id.action_unblock:
270				BlockContactDialog.show(this, xmppConnectionService, contact);
271				break;
272		}
273		return super.onOptionsItemSelected(menuItem);
274	}
275
276	@Override
277	public boolean onCreateOptionsMenu(final Menu menu) {
278		getMenuInflater().inflate(R.menu.contact_details, menu);
279		MenuItem block = menu.findItem(R.id.action_block);
280		MenuItem unblock = menu.findItem(R.id.action_unblock);
281		MenuItem edit = menu.findItem(R.id.action_edit_contact);
282		MenuItem delete = menu.findItem(R.id.action_delete_contact);
283		if (contact == null) {
284			return true;
285		}
286		final XmppConnection connection = contact.getAccount().getXmppConnection();
287		if (connection != null && connection.getFeatures().blocking()) {
288			if (this.contact.isBlocked()) {
289				block.setVisible(false);
290			} else {
291				unblock.setVisible(false);
292			}
293		} else {
294			unblock.setVisible(false);
295			block.setVisible(false);
296		}
297		if (!contact.showInRoster()) {
298			edit.setVisible(false);
299			delete.setVisible(false);
300		}
301		return super.onCreateOptionsMenu(menu);
302	}
303
304	private void populateView() {
305		invalidateOptionsMenu();
306		setTitle(contact.getDisplayName());
307		if (contact.showInRoster()) {
308			send.setVisibility(View.VISIBLE);
309			receive.setVisibility(View.VISIBLE);
310			addContactButton.setVisibility(View.GONE);
311			send.setOnCheckedChangeListener(null);
312			receive.setOnCheckedChangeListener(null);
313
314			List<String> statusMessages = contact.getPresences().getStatusMessages();
315			if (statusMessages.size() == 0) {
316				statusMessage.setVisibility(View.GONE);
317			} else {
318				StringBuilder builder = new StringBuilder();
319				statusMessage.setVisibility(View.VISIBLE);
320				int s = statusMessages.size();
321				for(int i = 0; i < s; ++i) {
322					if (s > 1) {
323						builder.append("");
324					}
325					builder.append(statusMessages.get(i));
326					if (i < s - 1) {
327						builder.append("\n");
328					}
329				}
330				statusMessage.setText(builder);
331			}
332
333			if (contact.getOption(Contact.Options.FROM)) {
334				send.setText(R.string.send_presence_updates);
335				send.setChecked(true);
336			} else if (contact.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
337				send.setChecked(false);
338				send.setText(R.string.send_presence_updates);
339			} else {
340				send.setText(R.string.preemptively_grant);
341				if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
342					send.setChecked(true);
343				} else {
344					send.setChecked(false);
345				}
346			}
347			if (contact.getOption(Contact.Options.TO)) {
348				receive.setText(R.string.receive_presence_updates);
349				receive.setChecked(true);
350			} else {
351				receive.setText(R.string.ask_for_presence_updates);
352				if (contact.getOption(Contact.Options.ASKING)) {
353					receive.setChecked(true);
354				} else {
355					receive.setChecked(false);
356				}
357			}
358			if (contact.getAccount().isOnlineAndConnected()) {
359				receive.setEnabled(true);
360				send.setEnabled(true);
361			} else {
362				receive.setEnabled(false);
363				send.setEnabled(false);
364			}
365			send.setOnCheckedChangeListener(this.mOnSendCheckedChange);
366			receive.setOnCheckedChangeListener(this.mOnReceiveCheckedChange);
367		} else {
368			addContactButton.setVisibility(View.VISIBLE);
369			send.setVisibility(View.GONE);
370			receive.setVisibility(View.GONE);
371			statusMessage.setVisibility(View.GONE);
372		}
373
374		if (contact.getPresences().size() > 1) {
375			contactJidTv.setText(contact.getDisplayJid() + " ("
376					+ contact.getPresences().size() + ")");
377		} else {
378			contactJidTv.setText(contact.getDisplayJid());
379		}
380		String account;
381		if (Config.DOMAIN_LOCK != null) {
382			account = contact.getAccount().getJid().getLocalpart();
383		} else {
384			account = contact.getAccount().getJid().toBareJid().toString();
385		}
386		accountJidTv.setText(getString(R.string.using_account, account));
387		badge.setImageBitmap(avatarService().get(contact, getPixel(72)));
388		badge.setOnClickListener(this.onBadgeClick);
389
390		keys.removeAllViews();
391		boolean hasKeys = false;
392		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
393		if (Config.supportOtr()) {
394			for (final String otrFingerprint : contact.getOtrFingerprints()) {
395				hasKeys = true;
396				View view = inflater.inflate(R.layout.contact_key, keys, false);
397				TextView key = (TextView) view.findViewById(R.id.key);
398				TextView keyType = (TextView) view.findViewById(R.id.key_type);
399				ImageButton removeButton = (ImageButton) view
400						.findViewById(R.id.button_remove);
401				removeButton.setVisibility(View.VISIBLE);
402				keyType.setText("OTR Fingerprint");
403				key.setText(CryptoHelper.prettifyFingerprint(otrFingerprint));
404				keys.addView(view);
405				removeButton.setOnClickListener(new OnClickListener() {
406
407					@Override
408					public void onClick(View v) {
409						confirmToDeleteFingerprint(otrFingerprint);
410					}
411				});
412			}
413		}
414		if (Config.supportOmemo()) {
415			for (final String fingerprint : contact.getAccount().getAxolotlService().getFingerprintsForContact(contact)) {
416				boolean highlight = fingerprint.equals(messageFingerprint);
417				hasKeys |= addFingerprintRow(keys, contact.getAccount(), fingerprint, highlight, new OnClickListener() {
418					@Override
419					public void onClick(View v) {
420						onOmemoKeyClicked(contact.getAccount(), fingerprint);
421					}
422				});
423			}
424		}
425		if (Config.supportOpenPgp() && contact.getPgpKeyId() != 0) {
426			hasKeys = true;
427			View view = inflater.inflate(R.layout.contact_key, keys, false);
428			TextView key = (TextView) view.findViewById(R.id.key);
429			TextView keyType = (TextView) view.findViewById(R.id.key_type);
430			keyType.setText("PGP Key ID");
431			key.setText(OpenPgpUtils.convertKeyIdToHex(contact.getPgpKeyId()));
432			view.setOnClickListener(new OnClickListener() {
433
434				@Override
435				public void onClick(View v) {
436					PgpEngine pgp = ContactDetailsActivity.this.xmppConnectionService
437						.getPgpEngine();
438					if (pgp != null) {
439						PendingIntent intent = pgp.getIntentForKey(contact);
440						if (intent != null) {
441							try {
442								startIntentSenderForResult(
443										intent.getIntentSender(), 0, null, 0,
444										0, 0);
445							} catch (SendIntentException e) {
446
447							}
448						}
449					}
450				}
451			});
452			keys.addView(view);
453		}
454		if (hasKeys) {
455			keys.setVisibility(View.VISIBLE);
456		} else {
457			keys.setVisibility(View.GONE);
458		}
459
460		List<ListItem.Tag> tagList = contact.getTags(this);
461		if (tagList.size() == 0 || !this.showDynamicTags) {
462			tags.setVisibility(View.GONE);
463		} else {
464			tags.setVisibility(View.VISIBLE);
465			tags.removeAllViewsInLayout();
466			for(final ListItem.Tag tag : tagList) {
467				final TextView tv = (TextView) inflater.inflate(R.layout.list_item_tag,tags,false);
468				tv.setText(tag.getName());
469				tv.setBackgroundColor(tag.getColor());
470				tags.addView(tv);
471			}
472		}
473	}
474
475	private void onOmemoKeyClicked(Account account, String fingerprint) {
476		final XmppAxolotlSession.Trust trust = account.getAxolotlService().getFingerprintTrust(fingerprint);
477		if (Config.X509_VERIFICATION && trust != null && trust == XmppAxolotlSession.Trust.TRUSTED_X509) {
478			X509Certificate x509Certificate = account.getAxolotlService().getFingerprintCertificate(fingerprint);
479			if (x509Certificate != null) {
480				showCertificateInformationDialog(CryptoHelper.extractCertificateInformation(x509Certificate));
481			} else {
482				Toast.makeText(this,R.string.certificate_not_found, Toast.LENGTH_SHORT).show();
483			}
484		}
485	}
486
487	private void showCertificateInformationDialog(Bundle bundle) {
488		View view = getLayoutInflater().inflate(R.layout.certificate_information, null);
489		final String not_available = getString(R.string.certicate_info_not_available);
490		TextView subject_cn = (TextView) view.findViewById(R.id.subject_cn);
491		TextView subject_o = (TextView) view.findViewById(R.id.subject_o);
492		TextView issuer_cn = (TextView) view.findViewById(R.id.issuer_cn);
493		TextView issuer_o = (TextView) view.findViewById(R.id.issuer_o);
494		TextView sha1 = (TextView) view.findViewById(R.id.sha1);
495
496		subject_cn.setText(bundle.getString("subject_cn", not_available));
497		subject_o.setText(bundle.getString("subject_o", not_available));
498		issuer_cn.setText(bundle.getString("issuer_cn", not_available));
499		issuer_o.setText(bundle.getString("issuer_o", not_available));
500		sha1.setText(bundle.getString("sha1", not_available));
501
502		AlertDialog.Builder builder = new AlertDialog.Builder(this);
503		builder.setTitle(R.string.certificate_information);
504		builder.setView(view);
505		builder.setPositiveButton(R.string.ok, null);
506		builder.create().show();
507	}
508
509	protected void confirmToDeleteFingerprint(final String fingerprint) {
510		AlertDialog.Builder builder = new AlertDialog.Builder(this);
511		builder.setTitle(R.string.delete_fingerprint);
512		builder.setMessage(R.string.sure_delete_fingerprint);
513		builder.setNegativeButton(R.string.cancel, null);
514		builder.setPositiveButton(R.string.delete,
515				new android.content.DialogInterface.OnClickListener() {
516
517					@Override
518					public void onClick(DialogInterface dialog, int which) {
519						if (contact.deleteOtrFingerprint(fingerprint)) {
520							populateView();
521							xmppConnectionService.syncRosterToDisk(contact.getAccount());
522						}
523					}
524
525				});
526		builder.create().show();
527	}
528
529	@Override
530	public void onBackendConnected() {
531		if ((accountJid != null) && (contactJid != null)) {
532			Account account = xmppConnectionService
533				.findAccountByJid(accountJid);
534			if (account == null) {
535				return;
536			}
537			this.contact = account.getRoster().getContact(contactJid);
538			populateView();
539		}
540	}
541
542	@Override
543	public void onKeyStatusUpdated(AxolotlService.FetchStatus report) {
544		refreshUi();
545	}
546}