ContactDetailsActivity.java

  1package eu.siacs.conversations.ui;
  2
  3import android.support.v7.app.AlertDialog;
  4import android.content.Context;
  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.support.v7.widget.CardView;
 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.util.List;
 36
 37import eu.siacs.conversations.Config;
 38import eu.siacs.conversations.R;
 39import eu.siacs.conversations.crypto.axolotl.AxolotlService;
 40import eu.siacs.conversations.crypto.axolotl.FingerprintStatus;
 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.services.XmppConnectionService.OnAccountUpdate;
 46import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate;
 47import eu.siacs.conversations.utils.CryptoHelper;
 48import eu.siacs.conversations.utils.UIHelper;
 49import eu.siacs.conversations.utils.XmppUri;
 50import eu.siacs.conversations.xml.Namespace;
 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 OmemoActivity 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 Button mShowInactiveDevicesButton;
117	private QuickContactBadge badge;
118	private LinearLayout keys;
119	private CardView keysWrapper;
120	private FlowLayout tags;
121	private boolean showDynamicTags = false;
122	private boolean showLastSeen = false;
123	private boolean showInactiveOmemo = false;
124	private String messageFingerprint;
125
126	private DialogInterface.OnClickListener addToPhonebook = new DialogInterface.OnClickListener() {
127
128		@Override
129		public void onClick(DialogInterface dialog, int which) {
130			Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
131			intent.setType(Contacts.CONTENT_ITEM_TYPE);
132			intent.putExtra(Intents.Insert.IM_HANDLE, contact.getJid().toString());
133			intent.putExtra(Intents.Insert.IM_PROTOCOL,
134					CommonDataKinds.Im.PROTOCOL_JABBER);
135			intent.putExtra("finishActivityOnSaveCompleted", true);
136			ContactDetailsActivity.this.startActivityForResult(intent, 0);
137		}
138	};
139
140	private OnClickListener onBadgeClick = new OnClickListener() {
141
142		@Override
143		public void onClick(View v) {
144			Uri systemAccount = contact.getSystemAccount();
145			if (systemAccount == null) {
146				AlertDialog.Builder builder = new AlertDialog.Builder(
147						ContactDetailsActivity.this);
148				builder.setTitle(getString(R.string.action_add_phone_book));
149				builder.setMessage(getString(R.string.add_phone_book_text,
150						contact.getDisplayJid()));
151				builder.setNegativeButton(getString(R.string.cancel), null);
152				builder.setPositiveButton(getString(R.string.add), addToPhonebook);
153				builder.create().show();
154			} else {
155				Intent intent = new Intent(Intent.ACTION_VIEW);
156				intent.setData(systemAccount);
157				startActivity(intent);
158			}
159		}
160	};
161
162	@Override
163	public void onRosterUpdate() {
164		refreshUi();
165	}
166
167	@Override
168	public void onAccountUpdate() {
169		refreshUi();
170	}
171
172	@Override
173	public void OnUpdateBlocklist(final Status status) {
174		refreshUi();
175	}
176
177	@Override
178	protected void refreshUiReal() {
179		invalidateOptionsMenu();
180		populateView();
181	}
182
183	@Override
184	protected String getShareableUri(boolean http) {
185		final String prefix = http ? "https://conversations.im/i/" : "xmpp:";
186		if (contact != null) {
187			return prefix+contact.getJid().toBareJid().toString();
188		} else {
189			return "";
190		}
191	}
192
193	@Override
194	protected void onCreate(final Bundle savedInstanceState) {
195		super.onCreate(savedInstanceState);
196		showInactiveOmemo = savedInstanceState != null && savedInstanceState.getBoolean("show_inactive_omemo",false);
197		if (getIntent().getAction().equals(ACTION_VIEW_CONTACT)) {
198			try {
199				this.accountJid = Jid.fromString(getIntent().getExtras().getString(EXTRA_ACCOUNT));
200			} catch (final InvalidJidException ignored) {
201			}
202			try {
203				this.contactJid = Jid.fromString(getIntent().getExtras().getString("contact"));
204			} catch (final InvalidJidException ignored) {
205			}
206		}
207		this.messageFingerprint = getIntent().getStringExtra("fingerprint");
208		setContentView(R.layout.activity_contact_details);
209
210		contactJidTv = findViewById(R.id.details_contactjid);
211		accountJidTv = findViewById(R.id.details_account);
212		lastseen = findViewById(R.id.details_lastseen);
213		statusMessage = findViewById(R.id.status_message);
214		send = findViewById(R.id.details_send_presence);
215		receive = findViewById(R.id.details_receive_presence);
216		badge = findViewById(R.id.details_contact_badge);
217		addContactButton = findViewById(R.id.add_contact_button);
218		addContactButton.setOnClickListener(view -> showAddToRosterDialog(contact));
219		keys = findViewById(R.id.details_contact_keys);
220		keysWrapper = findViewById(R.id.keys_wrapper);
221		tags = findViewById(R.id.tags);
222		mShowInactiveDevicesButton = findViewById(R.id.show_inactive_devices);
223		if (getSupportActionBar() != null) {
224			getSupportActionBar().setHomeButtonEnabled(true);
225			getSupportActionBar().setDisplayHomeAsUpEnabled(true);
226		}
227		mShowInactiveDevicesButton.setOnClickListener(v -> {
228			showInactiveOmemo = !showInactiveOmemo;
229			populateView();
230		});
231	}
232
233	@Override
234	public void onSaveInstanceState(final Bundle savedInstanceState) {
235		savedInstanceState.putBoolean("show_inactive_omemo",showInactiveOmemo);
236		super.onSaveInstanceState(savedInstanceState);
237	}
238
239	@Override
240	public void onStart() {
241		super.onStart();
242		final int theme = findTheme();
243		if (this.mTheme != theme) {
244			recreate();
245		} else {
246			final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
247			this.showDynamicTags = preferences.getBoolean(SettingsActivity.SHOW_DYNAMIC_TAGS, false);
248			this.showLastSeen = preferences.getBoolean("last_activity", false);
249		}
250	}
251
252	@Override
253	public boolean onOptionsItemSelected(final MenuItem menuItem) {
254		final AlertDialog.Builder builder = new AlertDialog.Builder(this);
255		builder.setNegativeButton(getString(R.string.cancel), null);
256		switch (menuItem.getItemId()) {
257			case android.R.id.home:
258				finish();
259				break;
260			case R.id.action_share_http:
261				shareLink(true);
262				break;
263			case R.id.action_share_uri:
264				shareLink(false);
265				break;
266			case R.id.action_delete_contact:
267				builder.setTitle(getString(R.string.action_delete_contact))
268					.setMessage(
269							getString(R.string.remove_contact_text,
270								contact.getDisplayJid()))
271					.setPositiveButton(getString(R.string.delete),
272							removeFromRoster).create().show();
273				break;
274			case R.id.action_edit_contact:
275				Uri systemAccount = contact.getSystemAccount();
276				if (systemAccount == null) {
277					quickEdit(contact.getDisplayName(), 0, new OnValueEdited() {
278
279						@Override
280						public String onValueEdited(String value) {
281							contact.setServerName(value);
282							ContactDetailsActivity.this.xmppConnectionService.pushContactToServer(contact);
283							populateView();
284							return null;
285						}
286					});
287				} else {
288					Intent intent = new Intent(Intent.ACTION_EDIT);
289					intent.setDataAndType(systemAccount, Contacts.CONTENT_ITEM_TYPE);
290					intent.putExtra("finishActivityOnSaveCompleted", true);
291					startActivity(intent);
292				}
293				break;
294			case R.id.action_block:
295				BlockContactDialog.show(this, contact);
296				break;
297			case R.id.action_unblock:
298				BlockContactDialog.show(this, contact);
299				break;
300		}
301		return super.onOptionsItemSelected(menuItem);
302	}
303
304	@Override
305	public boolean onCreateOptionsMenu(final Menu menu) {
306		getMenuInflater().inflate(R.menu.contact_details, menu);
307		MenuItem block = menu.findItem(R.id.action_block);
308		MenuItem unblock = menu.findItem(R.id.action_unblock);
309		MenuItem edit = menu.findItem(R.id.action_edit_contact);
310		MenuItem delete = menu.findItem(R.id.action_delete_contact);
311		if (contact == null) {
312			return true;
313		}
314		final XmppConnection connection = contact.getAccount().getXmppConnection();
315		if (connection != null && connection.getFeatures().blocking()) {
316			if (this.contact.isBlocked()) {
317				block.setVisible(false);
318			} else {
319				unblock.setVisible(false);
320			}
321		} else {
322			unblock.setVisible(false);
323			block.setVisible(false);
324		}
325		if (!contact.showInRoster()) {
326			edit.setVisible(false);
327			delete.setVisible(false);
328		}
329		return super.onCreateOptionsMenu(menu);
330	}
331
332	private void populateView() {
333		if (contact == null) {
334			return;
335		}
336		invalidateOptionsMenu();
337		setTitle(contact.getDisplayName());
338		if (contact.showInRoster()) {
339			send.setVisibility(View.VISIBLE);
340			receive.setVisibility(View.VISIBLE);
341			addContactButton.setVisibility(View.GONE);
342			send.setOnCheckedChangeListener(null);
343			receive.setOnCheckedChangeListener(null);
344
345			List<String> statusMessages = contact.getPresences().getStatusMessages();
346			if (statusMessages.size() == 0) {
347				statusMessage.setVisibility(View.GONE);
348			} else {
349				StringBuilder builder = new StringBuilder();
350				statusMessage.setVisibility(View.VISIBLE);
351				int s = statusMessages.size();
352				for(int i = 0; i < s; ++i) {
353					if (s > 1) {
354						builder.append("");
355					}
356					builder.append(statusMessages.get(i));
357					if (i < s - 1) {
358						builder.append("\n");
359					}
360				}
361				statusMessage.setText(builder);
362			}
363
364			if (contact.getOption(Contact.Options.FROM)) {
365				send.setText(R.string.send_presence_updates);
366				send.setChecked(true);
367			} else if (contact.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
368				send.setChecked(false);
369				send.setText(R.string.send_presence_updates);
370			} else {
371				send.setText(R.string.preemptively_grant);
372				if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
373					send.setChecked(true);
374				} else {
375					send.setChecked(false);
376				}
377			}
378			if (contact.getOption(Contact.Options.TO)) {
379				receive.setText(R.string.receive_presence_updates);
380				receive.setChecked(true);
381			} else {
382				receive.setText(R.string.ask_for_presence_updates);
383				if (contact.getOption(Contact.Options.ASKING)) {
384					receive.setChecked(true);
385				} else {
386					receive.setChecked(false);
387				}
388			}
389			if (contact.getAccount().isOnlineAndConnected()) {
390				receive.setEnabled(true);
391				send.setEnabled(true);
392			} else {
393				receive.setEnabled(false);
394				send.setEnabled(false);
395			}
396			send.setOnCheckedChangeListener(this.mOnSendCheckedChange);
397			receive.setOnCheckedChangeListener(this.mOnReceiveCheckedChange);
398		} else {
399			addContactButton.setVisibility(View.VISIBLE);
400			send.setVisibility(View.GONE);
401			receive.setVisibility(View.GONE);
402			statusMessage.setVisibility(View.GONE);
403		}
404
405		if (contact.isBlocked() && !this.showDynamicTags) {
406			lastseen.setVisibility(View.VISIBLE);
407			lastseen.setText(R.string.contact_blocked);
408		} else {
409			if (showLastSeen
410					&& contact.getLastseen() > 0
411					&& contact.getPresences().allOrNonSupport(Namespace.IDLE)) {
412				lastseen.setVisibility(View.VISIBLE);
413				lastseen.setText(UIHelper.lastseen(getApplicationContext(), contact.isActive(), contact.getLastseen()));
414			} else {
415				lastseen.setVisibility(View.GONE);
416			}
417		}
418
419		if (contact.getPresences().size() > 1) {
420			contactJidTv.setText(contact.getDisplayJid() + " ("
421					+ contact.getPresences().size() + ")");
422		} else {
423			contactJidTv.setText(contact.getDisplayJid());
424		}
425		String account;
426		if (Config.DOMAIN_LOCK != null) {
427			account = contact.getAccount().getJid().getLocalpart();
428		} else {
429			account = contact.getAccount().getJid().toBareJid().toString();
430		}
431		accountJidTv.setText(getString(R.string.using_account, account));
432		badge.setImageBitmap(avatarService().get(contact, getPixel(72)));
433		badge.setOnClickListener(this.onBadgeClick);
434
435		keys.removeAllViews();
436		boolean hasKeys = false;
437		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
438		if (Config.supportOtr()) {
439			for (final String otrFingerprint : contact.getOtrFingerprints()) {
440				hasKeys = true;
441				View view = inflater.inflate(R.layout.contact_key, keys, false);
442				TextView key = (TextView) view.findViewById(R.id.key);
443				TextView keyType = (TextView) view.findViewById(R.id.key_type);
444				ImageButton removeButton = (ImageButton) view
445						.findViewById(R.id.button_remove);
446				removeButton.setVisibility(View.VISIBLE);
447				key.setText(CryptoHelper.prettifyFingerprint(otrFingerprint));
448				if (otrFingerprint != null && otrFingerprint.equalsIgnoreCase(messageFingerprint)) {
449					keyType.setText(R.string.otr_fingerprint_selected_message);
450					keyType.setTextColor(ContextCompat.getColor(this, R.color.accent));
451				} else {
452					keyType.setText(R.string.otr_fingerprint);
453				}
454				keys.addView(view);
455				removeButton.setOnClickListener(new OnClickListener() {
456
457					@Override
458					public void onClick(View v) {
459						confirmToDeleteFingerprint(otrFingerprint);
460					}
461				});
462			}
463		}
464		final AxolotlService axolotlService = contact.getAccount().getAxolotlService();
465		if (Config.supportOmemo() && axolotlService != null) {
466			boolean skippedInactive = false;
467			boolean showsInactive = false;
468			for (final XmppAxolotlSession session : axolotlService.findSessionsForContact(contact)) {
469				final FingerprintStatus trust = session.getTrust();
470				hasKeys |= !trust.isCompromised();
471				if (!trust.isActive()) {
472					if (showInactiveOmemo) {
473						showsInactive = true;
474					} else {
475						skippedInactive = true;
476						continue;
477					}
478				}
479				if (!trust.isCompromised()) {
480					boolean highlight = session.getFingerprint().equals(messageFingerprint);
481					addFingerprintRow(keys, session, highlight);
482				}
483			}
484			if (showsInactive || skippedInactive) {
485				mShowInactiveDevicesButton.setText(showsInactive ? R.string.hide_inactive_devices : R.string.show_inactive_devices);
486				mShowInactiveDevicesButton.setVisibility(View.VISIBLE);
487			} else {
488				mShowInactiveDevicesButton.setVisibility(View.GONE);
489			}
490		} else {
491			mShowInactiveDevicesButton.setVisibility(View.GONE);
492		}
493		if (Config.supportOpenPgp() && contact.getPgpKeyId() != 0) {
494			hasKeys = true;
495			View view = inflater.inflate(R.layout.contact_key, keys, false);
496			TextView key = (TextView) view.findViewById(R.id.key);
497			TextView keyType = (TextView) view.findViewById(R.id.key_type);
498			keyType.setText(R.string.openpgp_key_id);
499			if ("pgp".equals(messageFingerprint)) {
500				keyType.setTextColor(ContextCompat.getColor(this, R.color.accent));
501			}
502			key.setText(OpenPgpUtils.convertKeyIdToHex(contact.getPgpKeyId()));
503			final OnClickListener openKey = new OnClickListener() {
504
505				@Override
506				public void onClick(View v) {
507					launchOpenKeyChain(contact.getPgpKeyId());
508				}
509			};
510			view.setOnClickListener(openKey);
511			key.setOnClickListener(openKey);
512			keyType.setOnClickListener(openKey);
513			keys.addView(view);
514		}
515		keysWrapper.setVisibility(hasKeys ? View.VISIBLE : View.GONE);
516
517		List<ListItem.Tag> tagList = contact.getTags(this);
518		if (tagList.size() == 0 || !this.showDynamicTags) {
519			tags.setVisibility(View.GONE);
520		} else {
521			tags.setVisibility(View.VISIBLE);
522			tags.removeAllViewsInLayout();
523			for(final ListItem.Tag tag : tagList) {
524				final TextView tv = (TextView) inflater.inflate(R.layout.list_item_tag,tags,false);
525				tv.setText(tag.getName());
526				tv.setBackgroundColor(tag.getColor());
527				tags.addView(tv);
528			}
529		}
530	}
531
532	protected void confirmToDeleteFingerprint(final String fingerprint) {
533		AlertDialog.Builder builder = new AlertDialog.Builder(this);
534		builder.setTitle(R.string.delete_fingerprint);
535		builder.setMessage(R.string.sure_delete_fingerprint);
536		builder.setNegativeButton(R.string.cancel, null);
537		builder.setPositiveButton(R.string.delete,
538				new android.content.DialogInterface.OnClickListener() {
539
540					@Override
541					public void onClick(DialogInterface dialog, int which) {
542						if (contact.deleteOtrFingerprint(fingerprint)) {
543							populateView();
544							xmppConnectionService.syncRosterToDisk(contact.getAccount());
545						}
546					}
547
548				});
549		builder.create().show();
550	}
551
552	public void onBackendConnected() {
553		if (accountJid != null && contactJid != null) {
554			Account account = xmppConnectionService.findAccountByJid(accountJid);
555			if (account == null) {
556				return;
557			}
558			this.contact = account.getRoster().getContact(contactJid);
559			if (mPendingFingerprintVerificationUri != null) {
560				processFingerprintVerification(mPendingFingerprintVerificationUri);
561				mPendingFingerprintVerificationUri = null;
562			}
563			populateView();
564		}
565	}
566
567	@Override
568	public void onKeyStatusUpdated(AxolotlService.FetchStatus report) {
569		refreshUi();
570	}
571
572	@Override
573	protected void processFingerprintVerification(XmppUri uri) {
574		if (contact != null && contact.getJid().toBareJid().equals(uri.getJid()) && uri.hasFingerprints()) {
575			if (xmppConnectionService.verifyFingerprints(contact,uri.getFingerprints())) {
576				Toast.makeText(this,R.string.verified_fingerprints,Toast.LENGTH_SHORT).show();
577			}
578		} else {
579			Toast.makeText(this,R.string.invalid_barcode,Toast.LENGTH_SHORT).show();
580		}
581	}
582}