ContactDetailsActivity.java

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