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