ContactDetailsActivity.java

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