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