ContactDetailsActivity.java

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