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.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.PgpEngine;
 40import eu.siacs.conversations.crypto.axolotl.AxolotlService;
 41import eu.siacs.conversations.crypto.axolotl.FingerprintStatus;
 42import eu.siacs.conversations.crypto.axolotl.XmppAxolotlSession;
 43import eu.siacs.conversations.entities.Account;
 44import eu.siacs.conversations.entities.Contact;
 45import eu.siacs.conversations.entities.ListItem;
 46import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
 47import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate;
 48import eu.siacs.conversations.utils.CryptoHelper;
 49import eu.siacs.conversations.utils.UIHelper;
 50import eu.siacs.conversations.utils.XmppUri;
 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 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		tags = (FlowLayout) findViewById(R.id.tags);
224		mShowInactiveDevicesButton = (Button) findViewById(R.id.show_inactive_devices);
225		if (getActionBar() != null) {
226			getActionBar().setHomeButtonEnabled(true);
227			getActionBar().setDisplayHomeAsUpEnabled(true);
228		}
229		mShowInactiveDevicesButton.setOnClickListener(new OnClickListener() {
230			@Override
231			public void onClick(View v) {
232				showInactiveOmemo = !showInactiveOmemo;
233				populateView();
234			}
235		});
236	}
237
238	@Override
239	public void onSaveInstanceState(final Bundle savedInstanceState) {
240		savedInstanceState.putBoolean("show_inactive_omemo",showInactiveOmemo);
241		super.onSaveInstanceState(savedInstanceState);
242	}
243
244	@Override
245	public void onStart() {
246		super.onStart();
247		final int theme = findTheme();
248		if (this.mTheme != theme) {
249			recreate();
250		} else {
251			final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
252			this.showDynamicTags = preferences.getBoolean("show_dynamic_tags", false);
253			this.showLastSeen = preferences.getBoolean("last_activity", false);
254		}
255	}
256
257	@Override
258	public boolean onOptionsItemSelected(final MenuItem menuItem) {
259		final AlertDialog.Builder builder = new AlertDialog.Builder(this);
260		builder.setNegativeButton(getString(R.string.cancel), null);
261		switch (menuItem.getItemId()) {
262			case android.R.id.home:
263				finish();
264				break;
265			case R.id.action_share:
266				shareUri();
267				break;
268			case R.id.action_delete_contact:
269				builder.setTitle(getString(R.string.action_delete_contact))
270					.setMessage(
271							getString(R.string.remove_contact_text,
272								contact.getDisplayJid()))
273					.setPositiveButton(getString(R.string.delete),
274							removeFromRoster).create().show();
275				break;
276			case R.id.action_edit_contact:
277				Uri systemAccount = contact.getSystemAccount();
278				if (systemAccount == null) {
279					quickEdit(contact.getDisplayName(), 0, new OnValueEdited() {
280
281						@Override
282						public void onValueEdited(String value) {
283							contact.setServerName(value);
284							ContactDetailsActivity.this.xmppConnectionService
285								.pushContactToServer(contact);
286							populateView();
287						}
288					});
289				} else {
290					Intent intent = new Intent(Intent.ACTION_EDIT);
291					intent.setDataAndType(systemAccount, Contacts.CONTENT_ITEM_TYPE);
292					intent.putExtra("finishActivityOnSaveCompleted", true);
293					startActivity(intent);
294				}
295				break;
296			case R.id.action_block:
297				BlockContactDialog.show(this, xmppConnectionService, contact);
298				break;
299			case R.id.action_unblock:
300				BlockContactDialog.show(this, xmppConnectionService, contact);
301				break;
302		}
303		return super.onOptionsItemSelected(menuItem);
304	}
305
306	@Override
307	public boolean onCreateOptionsMenu(final Menu menu) {
308		getMenuInflater().inflate(R.menu.contact_details, menu);
309		MenuItem block = menu.findItem(R.id.action_block);
310		MenuItem unblock = menu.findItem(R.id.action_unblock);
311		MenuItem edit = menu.findItem(R.id.action_edit_contact);
312		MenuItem delete = menu.findItem(R.id.action_delete_contact);
313		if (contact == null) {
314			return true;
315		}
316		final XmppConnection connection = contact.getAccount().getXmppConnection();
317		if (connection != null && connection.getFeatures().blocking()) {
318			if (this.contact.isBlocked()) {
319				block.setVisible(false);
320			} else {
321				unblock.setVisible(false);
322			}
323		} else {
324			unblock.setVisible(false);
325			block.setVisible(false);
326		}
327		if (!contact.showInRoster()) {
328			edit.setVisible(false);
329			delete.setVisible(false);
330		}
331		return super.onCreateOptionsMenu(menu);
332	}
333
334	private void populateView() {
335		if (contact == null) {
336			return;
337		}
338		invalidateOptionsMenu();
339		setTitle(contact.getDisplayName());
340		if (contact.showInRoster()) {
341			send.setVisibility(View.VISIBLE);
342			receive.setVisibility(View.VISIBLE);
343			addContactButton.setVisibility(View.GONE);
344			send.setOnCheckedChangeListener(null);
345			receive.setOnCheckedChangeListener(null);
346
347			List<String> statusMessages = contact.getPresences().getStatusMessages();
348			if (statusMessages.size() == 0) {
349				statusMessage.setVisibility(View.GONE);
350			} else {
351				StringBuilder builder = new StringBuilder();
352				statusMessage.setVisibility(View.VISIBLE);
353				int s = statusMessages.size();
354				for(int i = 0; i < s; ++i) {
355					if (s > 1) {
356						builder.append("");
357					}
358					builder.append(statusMessages.get(i));
359					if (i < s - 1) {
360						builder.append("\n");
361					}
362				}
363				statusMessage.setText(builder);
364			}
365
366			if (contact.getOption(Contact.Options.FROM)) {
367				send.setText(R.string.send_presence_updates);
368				send.setChecked(true);
369			} else if (contact.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
370				send.setChecked(false);
371				send.setText(R.string.send_presence_updates);
372			} else {
373				send.setText(R.string.preemptively_grant);
374				if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
375					send.setChecked(true);
376				} else {
377					send.setChecked(false);
378				}
379			}
380			if (contact.getOption(Contact.Options.TO)) {
381				receive.setText(R.string.receive_presence_updates);
382				receive.setChecked(true);
383			} else {
384				receive.setText(R.string.ask_for_presence_updates);
385				if (contact.getOption(Contact.Options.ASKING)) {
386					receive.setChecked(true);
387				} else {
388					receive.setChecked(false);
389				}
390			}
391			if (contact.getAccount().isOnlineAndConnected()) {
392				receive.setEnabled(true);
393				send.setEnabled(true);
394			} else {
395				receive.setEnabled(false);
396				send.setEnabled(false);
397			}
398			send.setOnCheckedChangeListener(this.mOnSendCheckedChange);
399			receive.setOnCheckedChangeListener(this.mOnReceiveCheckedChange);
400		} else {
401			addContactButton.setVisibility(View.VISIBLE);
402			send.setVisibility(View.GONE);
403			receive.setVisibility(View.GONE);
404			statusMessage.setVisibility(View.GONE);
405		}
406
407		if (contact.isBlocked() && !this.showDynamicTags) {
408			lastseen.setVisibility(View.VISIBLE);
409			lastseen.setText(R.string.contact_blocked);
410		} else {
411			if (showLastSeen && contact.getLastseen() > 0) {
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.equals(messageFingerprint)) {
449					keyType.setText(R.string.otr_fingerprint_selected_message);
450					keyType.setTextColor(getResources().getColor(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		if (Config.supportOmemo()) {
465			boolean skippedInactive = false;
466			boolean showsInactive = false;
467			for (final XmppAxolotlSession session : contact.getAccount().getAxolotlService().findSessionsForContact(contact)) {
468				final FingerprintStatus trust = session.getTrust();
469				if (!trust.isActive()) {
470					if (showInactiveOmemo) {
471						showsInactive = true;
472					} else {
473						skippedInactive = true;
474						continue;
475					}
476				}
477				if (!trust.isCompromised()) {
478					boolean highlight = session.getFingerprint().equals(messageFingerprint);
479					hasKeys = true;
480					addFingerprintRow(keys, session, highlight);
481				}
482			}
483			if (showsInactive || skippedInactive) {
484				mShowInactiveDevicesButton.setText(showsInactive ? R.string.hide_inactive_devices : R.string.show_inactive_devices);
485				mShowInactiveDevicesButton.setVisibility(View.VISIBLE);
486			} else {
487				mShowInactiveDevicesButton.setVisibility(View.GONE);
488			}
489		} else {
490			mShowInactiveDevicesButton.setVisibility(View.GONE);
491		}
492		if (Config.supportOpenPgp() && contact.getPgpKeyId() != 0) {
493			hasKeys = true;
494			View view = inflater.inflate(R.layout.contact_key, keys, false);
495			TextView key = (TextView) view.findViewById(R.id.key);
496			TextView keyType = (TextView) view.findViewById(R.id.key_type);
497			keyType.setText(R.string.openpgp_key_id);
498			if ("pgp".equals(messageFingerprint)) {
499				keyType.setTextColor(getResources().getColor(R.color.accent));
500			}
501			key.setText(OpenPgpUtils.convertKeyIdToHex(contact.getPgpKeyId()));
502			view.setOnClickListener(new OnClickListener() {
503
504				@Override
505				public void onClick(View v) {
506					PgpEngine pgp = ContactDetailsActivity.this.xmppConnectionService
507						.getPgpEngine();
508					if (pgp != null) {
509						PendingIntent intent = pgp.getIntentForKey(contact);
510						if (intent != null) {
511							try {
512								startIntentSenderForResult(
513										intent.getIntentSender(), 0, null, 0,
514										0, 0);
515							} catch (SendIntentException e) {
516
517							}
518						}
519					}
520				}
521			});
522			keys.addView(view);
523		}
524		if (hasKeys) {
525			keys.setVisibility(View.VISIBLE);
526		} else {
527			keys.setVisibility(View.GONE);
528		}
529
530		List<ListItem.Tag> tagList = contact.getTags(this);
531		if (tagList.size() == 0 || !this.showDynamicTags) {
532			tags.setVisibility(View.GONE);
533		} else {
534			tags.setVisibility(View.VISIBLE);
535			tags.removeAllViewsInLayout();
536			for(final ListItem.Tag tag : tagList) {
537				final TextView tv = (TextView) inflater.inflate(R.layout.list_item_tag,tags,false);
538				tv.setText(tag.getName());
539				tv.setBackgroundColor(tag.getColor());
540				tags.addView(tv);
541			}
542		}
543	}
544
545	protected void confirmToDeleteFingerprint(final String fingerprint) {
546		AlertDialog.Builder builder = new AlertDialog.Builder(this);
547		builder.setTitle(R.string.delete_fingerprint);
548		builder.setMessage(R.string.sure_delete_fingerprint);
549		builder.setNegativeButton(R.string.cancel, null);
550		builder.setPositiveButton(R.string.delete,
551				new android.content.DialogInterface.OnClickListener() {
552
553					@Override
554					public void onClick(DialogInterface dialog, int which) {
555						if (contact.deleteOtrFingerprint(fingerprint)) {
556							populateView();
557							xmppConnectionService.syncRosterToDisk(contact.getAccount());
558						}
559					}
560
561				});
562		builder.create().show();
563	}
564
565	public void onBackendConnected() {
566		if (accountJid != null && contactJid != null) {
567			Account account = xmppConnectionService.findAccountByJid(accountJid);
568			if (account == null) {
569				return;
570			}
571			this.contact = account.getRoster().getContact(contactJid);
572			if (mPendingFingerprintVerificationUri != null) {
573				processFingerprintVerification(mPendingFingerprintVerificationUri);
574				mPendingFingerprintVerificationUri = null;
575			}
576			populateView();
577		}
578	}
579
580	@Override
581	public void onKeyStatusUpdated(AxolotlService.FetchStatus report) {
582		refreshUi();
583	}
584
585	@Override
586	protected void processFingerprintVerification(XmppUri uri) {
587		if (contact != null && contact.getJid().toBareJid().equals(uri.getJid()) && uri.hasFingerprints()) {
588			if (xmppConnectionService.verifyFingerprints(contact,uri.getFingerprints())) {
589				Toast.makeText(this,R.string.verified_fingerprints,Toast.LENGTH_SHORT).show();
590			}
591		} else {
592			Toast.makeText(this,R.string.invalid_barcode,Toast.LENGTH_SHORT).show();
593		}
594	}
595}