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