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.UIHelper;
 42import eu.siacs.conversations.utils.XmppUri;
 43import eu.siacs.conversations.xml.Namespace;
 44import eu.siacs.conversations.xmpp.OnKeyStatusUpdated;
 45import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
 46import eu.siacs.conversations.xmpp.XmppConnection;
 47import eu.siacs.conversations.xmpp.jid.InvalidJidException;
 48import eu.siacs.conversations.xmpp.jid.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,
134						contact.getDisplayJid()));
135				builder.setNegativeButton(getString(R.string.cancel), null);
136				builder.setPositiveButton(getString(R.string.add), addToPhonebook);
137				builder.create().show();
138			} else {
139				Intent intent = new Intent(Intent.ACTION_VIEW);
140				intent.setData(systemAccount);
141				startActivity(intent);
142			}
143		}
144	};
145
146	@Override
147	public void onRosterUpdate() {
148		refreshUi();
149	}
150
151	@Override
152	public void onAccountUpdate() {
153		refreshUi();
154	}
155
156	@Override
157	public void OnUpdateBlocklist(final Status status) {
158		refreshUi();
159	}
160
161	@Override
162	protected void refreshUiReal() {
163		invalidateOptionsMenu();
164		populateView();
165	}
166
167	@Override
168	protected String getShareableUri(boolean http) {
169		final String prefix = http ? "https://conversations.im/i/" : "xmpp:";
170		if (contact != null) {
171			return prefix+contact.getJid().toBareJid().toString();
172		} else {
173			return "";
174		}
175	}
176
177	@Override
178	protected void onCreate(final Bundle savedInstanceState) {
179		super.onCreate(savedInstanceState);
180		showInactiveOmemo = savedInstanceState != null && savedInstanceState.getBoolean("show_inactive_omemo",false);
181		if (getIntent().getAction().equals(ACTION_VIEW_CONTACT)) {
182			try {
183				this.accountJid = Jid.fromString(getIntent().getExtras().getString(EXTRA_ACCOUNT));
184			} catch (final InvalidJidException ignored) {
185			}
186			try {
187				this.contactJid = Jid.fromString(getIntent().getExtras().getString("contact"));
188			} catch (final InvalidJidException ignored) {
189			}
190		}
191		this.messageFingerprint = getIntent().getStringExtra("fingerprint");
192		this.binding = DataBindingUtil.setContentView(this, R.layout.activity_contact_details);
193
194		if (getSupportActionBar() != null) {
195			getSupportActionBar().setHomeButtonEnabled(true);
196			getSupportActionBar().setDisplayHomeAsUpEnabled(true);
197		}
198		binding.showInactiveDevices.setOnClickListener(v -> {
199			showInactiveOmemo = !showInactiveOmemo;
200			populateView();
201		});
202	}
203
204	@Override
205	public void onSaveInstanceState(final Bundle savedInstanceState) {
206		savedInstanceState.putBoolean("show_inactive_omemo",showInactiveOmemo);
207		super.onSaveInstanceState(savedInstanceState);
208	}
209
210	@Override
211	public void onStart() {
212		super.onStart();
213		final int theme = findTheme();
214		if (this.mTheme != theme) {
215			recreate();
216		} else {
217			final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
218			this.showDynamicTags = preferences.getBoolean(SettingsActivity.SHOW_DYNAMIC_TAGS, false);
219			this.showLastSeen = preferences.getBoolean("last_activity", false);
220		}
221	}
222
223	@Override
224	public boolean onOptionsItemSelected(final MenuItem menuItem) {
225		final AlertDialog.Builder builder = new AlertDialog.Builder(this);
226		builder.setNegativeButton(getString(R.string.cancel), null);
227		switch (menuItem.getItemId()) {
228			case android.R.id.home:
229				finish();
230				break;
231			case R.id.action_share_http:
232				shareLink(true);
233				break;
234			case R.id.action_share_uri:
235				shareLink(false);
236				break;
237			case R.id.action_delete_contact:
238				builder.setTitle(getString(R.string.action_delete_contact))
239					.setMessage(
240							getString(R.string.remove_contact_text,
241								contact.getDisplayJid()))
242					.setPositiveButton(getString(R.string.delete),
243							removeFromRoster).create().show();
244				break;
245			case R.id.action_edit_contact:
246				Uri systemAccount = contact.getSystemAccount();
247				if (systemAccount == null) {
248					quickEdit(contact.getDisplayName(), 0, new OnValueEdited() {
249
250						@Override
251						public String onValueEdited(String value) {
252							contact.setServerName(value);
253							ContactDetailsActivity.this.xmppConnectionService.pushContactToServer(contact);
254							populateView();
255							return null;
256						}
257					});
258				} else {
259					Intent intent = new Intent(Intent.ACTION_EDIT);
260					intent.setDataAndType(systemAccount, Contacts.CONTENT_ITEM_TYPE);
261					intent.putExtra("finishActivityOnSaveCompleted", true);
262					startActivity(intent);
263				}
264				break;
265			case R.id.action_block:
266				BlockContactDialog.show(this, contact);
267				break;
268			case R.id.action_unblock:
269				BlockContactDialog.show(this, contact);
270				break;
271		}
272		return super.onOptionsItemSelected(menuItem);
273	}
274
275	@Override
276	public boolean onCreateOptionsMenu(final Menu menu) {
277		getMenuInflater().inflate(R.menu.contact_details, menu);
278		MenuItem block = menu.findItem(R.id.action_block);
279		MenuItem unblock = menu.findItem(R.id.action_unblock);
280		MenuItem edit = menu.findItem(R.id.action_edit_contact);
281		MenuItem delete = menu.findItem(R.id.action_delete_contact);
282		if (contact == null) {
283			return true;
284		}
285		final XmppConnection connection = contact.getAccount().getXmppConnection();
286		if (connection != null && connection.getFeatures().blocking()) {
287			if (this.contact.isBlocked()) {
288				block.setVisible(false);
289			} else {
290				unblock.setVisible(false);
291			}
292		} else {
293			unblock.setVisible(false);
294			block.setVisible(false);
295		}
296		if (!contact.showInRoster()) {
297			edit.setVisible(false);
298			delete.setVisible(false);
299		}
300		return super.onCreateOptionsMenu(menu);
301	}
302
303	private void populateView() {
304		if (contact == null) {
305			return;
306		}
307		invalidateOptionsMenu();
308		setTitle(contact.getDisplayName());
309		if (contact.showInRoster()) {
310			binding.detailsSendPresence.setVisibility(View.VISIBLE);
311			binding.detailsReceivePresence.setVisibility(View.VISIBLE);
312			binding.addContactButton.setVisibility(View.GONE);
313			binding.detailsSendPresence.setOnCheckedChangeListener(null);
314			binding.detailsReceivePresence.setOnCheckedChangeListener(null);
315
316			List<String> statusMessages = contact.getPresences().getStatusMessages();
317			if (statusMessages.size() == 0) {
318				binding.statusMessage.setVisibility(View.GONE);
319			} else {
320				StringBuilder builder = new StringBuilder();
321				binding.statusMessage.setVisibility(View.VISIBLE);
322				int s = statusMessages.size();
323				for(int i = 0; i < s; ++i) {
324					if (s > 1) {
325						builder.append("");
326					}
327					builder.append(statusMessages.get(i));
328					if (i < s - 1) {
329						builder.append("\n");
330					}
331				}
332				binding.statusMessage.setText(builder);
333			}
334
335			if (contact.getOption(Contact.Options.FROM)) {
336				binding.detailsSendPresence.setText(R.string.send_presence_updates);
337				binding.detailsSendPresence.setChecked(true);
338			} else if (contact.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
339				binding.detailsSendPresence.setChecked(false);
340				binding.detailsSendPresence.setText(R.string.send_presence_updates);
341			} else {
342				binding.detailsSendPresence.setText(R.string.preemptively_grant);
343				if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
344					binding.detailsSendPresence.setChecked(true);
345				} else {
346					binding.detailsSendPresence.setChecked(false);
347				}
348			}
349			if (contact.getOption(Contact.Options.TO)) {
350				binding.detailsReceivePresence.setText(R.string.receive_presence_updates);
351				binding.detailsReceivePresence.setChecked(true);
352			} else {
353				binding.detailsReceivePresence.setText(R.string.ask_for_presence_updates);
354				if (contact.getOption(Contact.Options.ASKING)) {
355					binding.detailsReceivePresence.setChecked(true);
356				} else {
357					binding.detailsReceivePresence.setChecked(false);
358				}
359			}
360			if (contact.getAccount().isOnlineAndConnected()) {
361				binding.detailsReceivePresence.setEnabled(true);
362				binding.detailsSendPresence.setEnabled(true);
363			} else {
364				binding.detailsReceivePresence.setEnabled(false);
365				binding.detailsSendPresence.setEnabled(false);
366			}
367			binding.detailsSendPresence.setOnCheckedChangeListener(this.mOnSendCheckedChange);
368			binding.detailsReceivePresence.setOnCheckedChangeListener(this.mOnReceiveCheckedChange);
369		} else {
370			binding.addContactButton.setVisibility(View.VISIBLE);
371			binding.detailsSendPresence.setVisibility(View.GONE);
372			binding.detailsReceivePresence.setVisibility(View.GONE);
373			binding.statusMessage.setVisibility(View.GONE);
374		}
375
376		if (contact.isBlocked() && !this.showDynamicTags) {
377			binding.detailsLastseen.setVisibility(View.VISIBLE);
378			binding.detailsLastseen.setText(R.string.contact_blocked);
379		} else {
380			if (showLastSeen
381					&& contact.getLastseen() > 0
382					&& contact.getPresences().allOrNonSupport(Namespace.IDLE)) {
383				binding.detailsLastseen.setVisibility(View.VISIBLE);
384				binding.detailsLastseen.setText(UIHelper.lastseen(getApplicationContext(), contact.isActive(), contact.getLastseen()));
385			} else {
386				binding.detailsLastseen.setVisibility(View.GONE);
387			}
388		}
389
390		if (contact.getPresences().size() > 1) {
391			binding.detailsContactjid.setText(contact.getDisplayJid() + " ("
392					+ contact.getPresences().size() + ")");
393		} else {
394			binding.detailsContactjid.setText(contact.getDisplayJid());
395		}
396		String account;
397		if (Config.DOMAIN_LOCK != null) {
398			account = contact.getAccount().getJid().getLocalpart();
399		} else {
400			account = contact.getAccount().getJid().toBareJid().toString();
401		}
402		binding.detailsAccount.setText(getString(R.string.using_account, account));
403		binding.detailsContactBadge.setImageBitmap(avatarService().get(contact, getPixel(72)));
404		binding.detailsContactBadge.setOnClickListener(this.onBadgeClick);
405
406		binding.detailsContactKeys.removeAllViews();
407		boolean hasKeys = false;
408		final LayoutInflater inflater = getLayoutInflater();
409		final AxolotlService axolotlService = contact.getAccount().getAxolotlService();
410		if (Config.supportOmemo() && axolotlService != null) {
411			boolean skippedInactive = false;
412			boolean showsInactive = false;
413			for (final XmppAxolotlSession session : axolotlService.findSessionsForContact(contact)) {
414				final FingerprintStatus trust = session.getTrust();
415				hasKeys |= !trust.isCompromised();
416				if (!trust.isActive()) {
417					if (showInactiveOmemo) {
418						showsInactive = true;
419					} else {
420						skippedInactive = true;
421						continue;
422					}
423				}
424				if (!trust.isCompromised()) {
425					boolean highlight = session.getFingerprint().equals(messageFingerprint);
426					addFingerprintRow(binding.detailsContactKeys, session, highlight);
427				}
428			}
429			if (showsInactive || skippedInactive) {
430				binding.showInactiveDevices.setText(showsInactive ? R.string.hide_inactive_devices : R.string.show_inactive_devices);
431				binding.showInactiveDevices.setVisibility(View.VISIBLE);
432			} else {
433				binding.showInactiveDevices.setVisibility(View.GONE);
434			}
435		} else {
436			binding.showInactiveDevices.setVisibility(View.GONE);
437		}
438		if (Config.supportOpenPgp() && contact.getPgpKeyId() != 0) {
439			hasKeys = true;
440			View view = inflater.inflate(R.layout.contact_key, binding.detailsContactKeys, false);
441			TextView key = (TextView) view.findViewById(R.id.key);
442			TextView keyType = (TextView) view.findViewById(R.id.key_type);
443			keyType.setText(R.string.openpgp_key_id);
444			if ("pgp".equals(messageFingerprint)) {
445				keyType.setTextColor(ContextCompat.getColor(this, R.color.accent));
446			}
447			key.setText(OpenPgpUtils.convertKeyIdToHex(contact.getPgpKeyId()));
448			final OnClickListener openKey = v -> launchOpenKeyChain(contact.getPgpKeyId());
449			view.setOnClickListener(openKey);
450			key.setOnClickListener(openKey);
451			keyType.setOnClickListener(openKey);
452			binding.detailsContactKeys.addView(view);
453		}
454		binding.keysWrapper.setVisibility(hasKeys ? View.VISIBLE : View.GONE);
455
456		List<ListItem.Tag> tagList = contact.getTags(this);
457		if (tagList.size() == 0 || !this.showDynamicTags) {
458			binding.tags.setVisibility(View.GONE);
459		} else {
460			binding.tags.setVisibility(View.VISIBLE);
461			binding.tags.removeAllViewsInLayout();
462			for(final ListItem.Tag tag : tagList) {
463				final TextView tv = (TextView) inflater.inflate(R.layout.list_item_tag,binding.tags,false);
464				tv.setText(tag.getName());
465				tv.setBackgroundColor(tag.getColor());
466				binding.tags.addView(tv);
467			}
468		}
469	}
470
471	public void onBackendConnected() {
472		if (accountJid != null && contactJid != null) {
473			Account account = xmppConnectionService.findAccountByJid(accountJid);
474			if (account == null) {
475				return;
476			}
477			this.contact = account.getRoster().getContact(contactJid);
478			if (mPendingFingerprintVerificationUri != null) {
479				processFingerprintVerification(mPendingFingerprintVerificationUri);
480				mPendingFingerprintVerificationUri = null;
481			}
482			populateView();
483		}
484	}
485
486	@Override
487	public void onKeyStatusUpdated(AxolotlService.FetchStatus report) {
488		refreshUi();
489	}
490
491	@Override
492	protected void processFingerprintVerification(XmppUri uri) {
493		if (contact != null && contact.getJid().toBareJid().equals(uri.getJid()) && uri.hasFingerprints()) {
494			if (xmppConnectionService.verifyFingerprints(contact,uri.getFingerprints())) {
495				Toast.makeText(this,R.string.verified_fingerprints,Toast.LENGTH_SHORT).show();
496			}
497		} else {
498			Toast.makeText(this,R.string.invalid_barcode,Toast.LENGTH_SHORT).show();
499		}
500	}
501}