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.util.Log;
 18import android.view.LayoutInflater;
 19import android.view.Menu;
 20import android.view.MenuItem;
 21import android.view.View;
 22import android.view.View.OnClickListener;
 23import android.widget.Button;
 24import android.widget.CheckBox;
 25import android.widget.CompoundButton;
 26import android.widget.CompoundButton.OnCheckedChangeListener;
 27import android.widget.ImageButton;
 28import android.widget.LinearLayout;
 29import android.widget.QuickContactBadge;
 30import android.widget.TextView;
 31import android.widget.Toast;
 32
 33import org.openintents.openpgp.util.OpenPgpUtils;
 34
 35import java.security.cert.X509Certificate;
 36import java.util.List;
 37
 38import eu.siacs.conversations.Config;
 39import eu.siacs.conversations.R;
 40import eu.siacs.conversations.crypto.PgpEngine;
 41import eu.siacs.conversations.crypto.axolotl.AxolotlService;
 42import eu.siacs.conversations.crypto.axolotl.XmppAxolotlSession;
 43import eu.siacs.conversations.entities.Account;
 44import eu.siacs.conversations.entities.Contact;
 45import eu.siacs.conversations.entities.ListItem;
 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 lastseen;
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.getJid()));
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("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		lastseen = (TextView) findViewById(R.id.details_lastseen);
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.getJid()))
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(), 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 true;
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			if (contact.getOption(Contact.Options.FROM)) {
315				send.setText(R.string.send_presence_updates);
316				send.setChecked(true);
317			} else if (contact.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
318				send.setChecked(false);
319				send.setText(R.string.send_presence_updates);
320			} else {
321				send.setText(R.string.preemptively_grant);
322				if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
323					send.setChecked(true);
324				} else {
325					send.setChecked(false);
326				}
327			}
328			if (contact.getOption(Contact.Options.TO)) {
329				receive.setText(R.string.receive_presence_updates);
330				receive.setChecked(true);
331			} else {
332				receive.setText(R.string.ask_for_presence_updates);
333				if (contact.getOption(Contact.Options.ASKING)) {
334					receive.setChecked(true);
335				} else {
336					receive.setChecked(false);
337				}
338			}
339			if (contact.getAccount().isOnlineAndConnected()) {
340				receive.setEnabled(true);
341				send.setEnabled(true);
342			} else {
343				receive.setEnabled(false);
344				send.setEnabled(false);
345			}
346
347			send.setOnCheckedChangeListener(this.mOnSendCheckedChange);
348			receive.setOnCheckedChangeListener(this.mOnReceiveCheckedChange);
349		} else {
350			addContactButton.setVisibility(View.VISIBLE);
351			send.setVisibility(View.GONE);
352			receive.setVisibility(View.GONE);
353		}
354
355		if (contact.isBlocked() && !this.showDynamicTags) {
356			lastseen.setText(R.string.contact_blocked);
357		} else {
358			lastseen.setText(UIHelper.lastseen(getApplicationContext(), contact.lastseen.time));
359		}
360
361		if (contact.getPresences().size() > 1) {
362			contactJidTv.setText(contact.getJid() + " ("
363					+ contact.getPresences().size() + ")");
364		} else {
365			contactJidTv.setText(contact.getJid().toString());
366		}
367		String account;
368		if (Config.DOMAIN_LOCK != null) {
369			account = contact.getAccount().getJid().getLocalpart();
370		} else {
371			account = contact.getAccount().getJid().toBareJid().toString();
372		}
373		accountJidTv.setText(getString(R.string.using_account, account));
374		badge.setImageBitmap(avatarService().get(contact, getPixel(72)));
375		badge.setOnClickListener(this.onBadgeClick);
376
377		keys.removeAllViews();
378		boolean hasKeys = false;
379		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
380		for(final String otrFingerprint : contact.getOtrFingerprints()) {
381			hasKeys = true;
382			View view = inflater.inflate(R.layout.contact_key, keys, false);
383			TextView key = (TextView) view.findViewById(R.id.key);
384			TextView keyType = (TextView) view.findViewById(R.id.key_type);
385			ImageButton removeButton = (ImageButton) view
386				.findViewById(R.id.button_remove);
387			removeButton.setVisibility(View.VISIBLE);
388			keyType.setText("OTR Fingerprint");
389			key.setText(CryptoHelper.prettifyFingerprint(otrFingerprint));
390			keys.addView(view);
391			removeButton.setOnClickListener(new OnClickListener() {
392
393				@Override
394				public void onClick(View v) {
395					confirmToDeleteFingerprint(otrFingerprint);
396				}
397			});
398		}
399		for (final String fingerprint : contact.getAccount().getAxolotlService().getFingerprintsForContact(contact)) {
400			boolean highlight = fingerprint.equals(messageFingerprint);
401			hasKeys |= addFingerprintRow(keys, contact.getAccount(), fingerprint, highlight, new OnClickListener() {
402				@Override
403				public void onClick(View v) {
404					onOmemoKeyClicked(contact.getAccount(), fingerprint);
405				}
406			});
407		}
408		if (contact.getPgpKeyId() != 0) {
409			hasKeys = true;
410			View view = inflater.inflate(R.layout.contact_key, keys, false);
411			TextView key = (TextView) view.findViewById(R.id.key);
412			TextView keyType = (TextView) view.findViewById(R.id.key_type);
413			keyType.setText("PGP Key ID");
414			key.setText(OpenPgpUtils.convertKeyIdToHex(contact.getPgpKeyId()));
415			view.setOnClickListener(new OnClickListener() {
416
417				@Override
418				public void onClick(View v) {
419					PgpEngine pgp = ContactDetailsActivity.this.xmppConnectionService
420						.getPgpEngine();
421					if (pgp != null) {
422						PendingIntent intent = pgp.getIntentForKey(contact);
423						if (intent != null) {
424							try {
425								startIntentSenderForResult(
426										intent.getIntentSender(), 0, null, 0,
427										0, 0);
428							} catch (SendIntentException e) {
429
430							}
431						}
432					}
433				}
434			});
435			keys.addView(view);
436		}
437		if (hasKeys) {
438			keys.setVisibility(View.VISIBLE);
439		} else {
440			keys.setVisibility(View.GONE);
441		}
442
443		List<ListItem.Tag> tagList = contact.getTags();
444		if (tagList.size() == 0 || !this.showDynamicTags) {
445			tags.setVisibility(View.GONE);
446		} else {
447			tags.setVisibility(View.VISIBLE);
448			tags.removeAllViewsInLayout();
449			for(final ListItem.Tag tag : tagList) {
450				final TextView tv = (TextView) inflater.inflate(R.layout.list_item_tag,tags,false);
451				tv.setText(tag.getName());
452				tv.setBackgroundColor(tag.getColor());
453				tags.addView(tv);
454			}
455		}
456	}
457
458	private void onOmemoKeyClicked(Account account, String fingerprint) {
459		final XmppAxolotlSession.Trust trust = account.getAxolotlService().getFingerprintTrust(fingerprint);
460		if (trust != null && trust == XmppAxolotlSession.Trust.TRUSTED_X509) {
461			X509Certificate x509Certificate = account.getAxolotlService().getFingerprintCertificate(fingerprint);
462			if (x509Certificate != null) {
463				showCertificateInformationDialog(CryptoHelper.extractCertificateInformation(x509Certificate));
464			} else {
465				Toast.makeText(this,R.string.certificate_not_found, Toast.LENGTH_SHORT).show();
466			}
467		}
468	}
469
470	private void showCertificateInformationDialog(Bundle bundle) {
471		View view = getLayoutInflater().inflate(R.layout.certificate_information, null);
472		final String not_available = getString(R.string.certicate_info_not_available);
473		TextView subject_cn = (TextView) view.findViewById(R.id.subject_cn);
474		TextView subject_o = (TextView) view.findViewById(R.id.subject_o);
475		TextView issuer_cn = (TextView) view.findViewById(R.id.issuer_cn);
476		TextView issuer_o = (TextView) view.findViewById(R.id.issuer_o);
477		TextView sha1 = (TextView) view.findViewById(R.id.sha1);
478
479		subject_cn.setText(bundle.getString("subject_cn", not_available));
480		subject_o.setText(bundle.getString("subject_o", not_available));
481		issuer_cn.setText(bundle.getString("issuer_cn", not_available));
482		issuer_o.setText(bundle.getString("issuer_o", not_available));
483		sha1.setText(bundle.getString("sha1", not_available));
484
485		AlertDialog.Builder builder = new AlertDialog.Builder(this);
486		builder.setTitle(R.string.certificate_information);
487		builder.setView(view);
488		builder.setPositiveButton(R.string.ok, null);
489		builder.create().show();
490	}
491
492	protected void confirmToDeleteFingerprint(final String fingerprint) {
493		AlertDialog.Builder builder = new AlertDialog.Builder(this);
494		builder.setTitle(R.string.delete_fingerprint);
495		builder.setMessage(R.string.sure_delete_fingerprint);
496		builder.setNegativeButton(R.string.cancel, null);
497		builder.setPositiveButton(R.string.delete,
498				new android.content.DialogInterface.OnClickListener() {
499
500					@Override
501					public void onClick(DialogInterface dialog, int which) {
502						if (contact.deleteOtrFingerprint(fingerprint)) {
503							populateView();
504							xmppConnectionService.syncRosterToDisk(contact.getAccount());
505						}
506					}
507
508				});
509		builder.create().show();
510	}
511
512	@Override
513	public void onBackendConnected() {
514		if ((accountJid != null) && (contactJid != null)) {
515			Account account = xmppConnectionService
516				.findAccountByJid(accountJid);
517			if (account == null) {
518				return;
519			}
520			this.contact = account.getRoster().getContact(contactJid);
521			populateView();
522		}
523	}
524
525	@Override
526	public void onKeyStatusUpdated(AxolotlService.FetchStatus report) {
527		refreshUi();
528	}
529}