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;
 29
 30import org.openintents.openpgp.util.OpenPgpUtils;
 31
 32import java.util.List;
 33
 34import eu.siacs.conversations.R;
 35import eu.siacs.conversations.crypto.PgpEngine;
 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.CryptoHelper;
 42import eu.siacs.conversations.utils.UIHelper;
 43import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
 44import eu.siacs.conversations.xmpp.XmppConnection;
 45import eu.siacs.conversations.xmpp.jid.InvalidJidException;
 46import eu.siacs.conversations.xmpp.jid.Jid;
 47
 48public class ContactDetailsActivity extends XmppActivity implements OnAccountUpdate, OnRosterUpdate, OnUpdateBlocklist {
 49	public static final String ACTION_VIEW_CONTACT = "view_contact";
 50
 51	private Contact contact;
 52	private DialogInterface.OnClickListener removeFromRoster = new DialogInterface.OnClickListener() {
 53
 54		@Override
 55		public void onClick(DialogInterface dialog, int which) {
 56			xmppConnectionService.deleteContactOnServer(contact);
 57		}
 58	};
 59	private OnCheckedChangeListener mOnSendCheckedChange = new OnCheckedChangeListener() {
 60
 61		@Override
 62		public void onCheckedChanged(CompoundButton buttonView,
 63				boolean isChecked) {
 64			if (isChecked) {
 65				if (contact
 66						.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
 67					xmppConnectionService.sendPresencePacket(contact
 68							.getAccount(),
 69							xmppConnectionService.getPresenceGenerator()
 70							.sendPresenceUpdatesTo(contact));
 71				} else {
 72					contact.setOption(Contact.Options.PREEMPTIVE_GRANT);
 73				}
 74			} else {
 75				contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
 76				xmppConnectionService.sendPresencePacket(contact.getAccount(),
 77						xmppConnectionService.getPresenceGenerator()
 78						.stopPresenceUpdatesTo(contact));
 79			}
 80		}
 81	};
 82	private OnCheckedChangeListener mOnReceiveCheckedChange = new OnCheckedChangeListener() {
 83
 84		@Override
 85		public void onCheckedChanged(CompoundButton buttonView,
 86				boolean isChecked) {
 87			if (isChecked) {
 88				xmppConnectionService.sendPresencePacket(contact.getAccount(),
 89						xmppConnectionService.getPresenceGenerator()
 90						.requestPresenceUpdatesFrom(contact));
 91			} else {
 92				xmppConnectionService.sendPresencePacket(contact.getAccount(),
 93						xmppConnectionService.getPresenceGenerator()
 94						.stopPresenceUpdatesFrom(contact));
 95			}
 96		}
 97	};
 98	private Jid accountJid;
 99	private Jid contactJid;
100	private TextView contactJidTv;
101	private TextView accountJidTv;
102	private TextView lastseen;
103	private CheckBox send;
104	private CheckBox receive;
105	private Button addContactButton;
106	private QuickContactBadge badge;
107	private LinearLayout keys;
108	private LinearLayout tags;
109	private boolean showDynamicTags;
110
111	private DialogInterface.OnClickListener addToPhonebook = new DialogInterface.OnClickListener() {
112
113		@Override
114		public void onClick(DialogInterface dialog, int which) {
115			Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
116			intent.setType(Contacts.CONTENT_ITEM_TYPE);
117			intent.putExtra(Intents.Insert.IM_HANDLE, contact.getJid().toString());
118			intent.putExtra(Intents.Insert.IM_PROTOCOL,
119					CommonDataKinds.Im.PROTOCOL_JABBER);
120			intent.putExtra("finishActivityOnSaveCompleted", true);
121			ContactDetailsActivity.this.startActivityForResult(intent, 0);
122		}
123	};
124
125	private OnClickListener onBadgeClick = new OnClickListener() {
126
127		@Override
128		public void onClick(View v) {
129			AlertDialog.Builder builder = new AlertDialog.Builder(
130					ContactDetailsActivity.this);
131			builder.setTitle(getString(R.string.action_add_phone_book));
132			builder.setMessage(getString(R.string.add_phone_book_text,
133						contact.getJid()));
134			builder.setNegativeButton(getString(R.string.cancel), null);
135			builder.setPositiveButton(getString(R.string.add), addToPhonebook);
136			builder.create().show();
137		}
138	};
139
140	@Override
141	public void onRosterUpdate() {
142		refreshUi();
143	}
144
145	@Override
146	public void onAccountUpdate() {
147		refreshUi();
148	}
149
150	@Override
151	protected void refreshUiReal() {
152		invalidateOptionsMenu();
153		populateView();
154	}
155
156	@Override
157	protected String getShareableUri() {
158		if (contact != null) {
159			return contact.getShareableUri();
160		} else {
161			return "";
162		}
163	}
164
165	@Override
166	protected void onCreate(final Bundle savedInstanceState) {
167		super.onCreate(savedInstanceState);
168		if (getIntent().getAction().equals(ACTION_VIEW_CONTACT)) {
169			try {
170				this.accountJid = Jid.fromString(getIntent().getExtras().getString("account"));
171			} catch (final InvalidJidException ignored) {
172			}
173			try {
174				this.contactJid = Jid.fromString(getIntent().getExtras().getString("contact"));
175			} catch (final InvalidJidException ignored) {
176			}
177		}
178		setContentView(R.layout.activity_contact_details);
179
180		contactJidTv = (TextView) findViewById(R.id.details_contactjid);
181		accountJidTv = (TextView) findViewById(R.id.details_account);
182		lastseen = (TextView) findViewById(R.id.details_lastseen);
183		send = (CheckBox) findViewById(R.id.details_send_presence);
184		receive = (CheckBox) findViewById(R.id.details_receive_presence);
185		badge = (QuickContactBadge) findViewById(R.id.details_contact_badge);
186		addContactButton = (Button) findViewById(R.id.add_contact_button);
187		addContactButton.setOnClickListener(new OnClickListener() {
188			@Override
189			public void onClick(View view) {
190				showAddToRosterDialog(contact);
191			}
192		});
193		keys = (LinearLayout) findViewById(R.id.details_contact_keys);
194		tags = (LinearLayout) findViewById(R.id.tags);
195		if (getActionBar() != null) {
196			getActionBar().setHomeButtonEnabled(true);
197			getActionBar().setDisplayHomeAsUpEnabled(true);
198		}
199
200		final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
201		this.showDynamicTags = preferences.getBoolean("show_dynamic_tags",false);
202	}
203
204	@Override
205	public boolean onOptionsItemSelected(final MenuItem menuItem) {
206		final AlertDialog.Builder builder = new AlertDialog.Builder(this);
207		builder.setNegativeButton(getString(R.string.cancel), null);
208		switch (menuItem.getItemId()) {
209			case android.R.id.home:
210				finish();
211				break;
212			case R.id.action_delete_contact:
213				builder.setTitle(getString(R.string.action_delete_contact))
214					.setMessage(
215							getString(R.string.remove_contact_text,
216								contact.getJid()))
217					.setPositiveButton(getString(R.string.delete),
218							removeFromRoster).create().show();
219				break;
220			case R.id.action_edit_contact:
221				if (contact.getSystemAccount() == null) {
222					quickEdit(contact.getDisplayName(), new OnValueEdited() {
223
224						@Override
225						public void onValueEdited(String value) {
226							contact.setServerName(value);
227							ContactDetailsActivity.this.xmppConnectionService
228								.pushContactToServer(contact);
229							populateView();
230						}
231					});
232				} else {
233					Intent intent = new Intent(Intent.ACTION_EDIT);
234					String[] systemAccount = contact.getSystemAccount().split("#");
235					long id = Long.parseLong(systemAccount[0]);
236					Uri uri = Contacts.getLookupUri(id, systemAccount[1]);
237					intent.setDataAndType(uri, Contacts.CONTENT_ITEM_TYPE);
238					intent.putExtra("finishActivityOnSaveCompleted", true);
239					startActivity(intent);
240				}
241				break;
242			case R.id.action_block:
243				BlockContactDialog.show(this, xmppConnectionService, contact);
244				break;
245			case R.id.action_unblock:
246				BlockContactDialog.show(this, xmppConnectionService, contact);
247				break;
248		}
249		return super.onOptionsItemSelected(menuItem);
250	}
251
252	@Override
253	public boolean onCreateOptionsMenu(final Menu menu) {
254		getMenuInflater().inflate(R.menu.contact_details, menu);
255		MenuItem block = menu.findItem(R.id.action_block);
256		MenuItem unblock = menu.findItem(R.id.action_unblock);
257		MenuItem edit = menu.findItem(R.id.action_edit_contact);
258		MenuItem delete = menu.findItem(R.id.action_delete_contact);
259		final XmppConnection connection = contact.getAccount().getXmppConnection();
260		if (connection != null && connection.getFeatures().blocking()) {
261			if (this.contact.isBlocked()) {
262				menu.findItem(R.id.action_block).setVisible(false);
263			} else {
264				menu.findItem(R.id.action_unblock).setVisible(false);
265			}
266		} else {
267			menu.findItem(R.id.action_unblock).setVisible(false);
268			menu.findItem(R.id.action_block).setVisible(false);
269		}
270		if (!contact.showInRoster()) {
271			edit.setVisible(false);
272			delete.setVisible(false);
273		}
274		return true;
275	}
276
277	private void populateView() {
278		setTitle(contact.getDisplayName());
279		if (contact.showInRoster()) {
280			send.setVisibility(View.VISIBLE);
281			receive.setVisibility(View.VISIBLE);
282			addContactButton.setVisibility(View.GONE);
283			send.setOnCheckedChangeListener(null);
284			receive.setOnCheckedChangeListener(null);
285
286			if (contact.getOption(Contact.Options.FROM)) {
287				send.setText(R.string.send_presence_updates);
288				send.setChecked(true);
289			} else if (contact.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
290				send.setChecked(false);
291				send.setText(R.string.send_presence_updates);
292			} else {
293				send.setText(R.string.preemptively_grant);
294				if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
295					send.setChecked(true);
296				} else {
297					send.setChecked(false);
298				}
299			}
300			if (contact.getOption(Contact.Options.TO)) {
301				receive.setText(R.string.receive_presence_updates);
302				receive.setChecked(true);
303			} else {
304				receive.setText(R.string.ask_for_presence_updates);
305				if (contact.getOption(Contact.Options.ASKING)) {
306					receive.setChecked(true);
307				} else {
308					receive.setChecked(false);
309				}
310			}
311			if (contact.getAccount().isOnlineAndConnected()) {
312				receive.setEnabled(true);
313				send.setEnabled(true);
314			} else {
315				receive.setEnabled(false);
316				send.setEnabled(false);
317			}
318
319			send.setOnCheckedChangeListener(this.mOnSendCheckedChange);
320			receive.setOnCheckedChangeListener(this.mOnReceiveCheckedChange);
321		} else {
322			addContactButton.setVisibility(View.VISIBLE);
323			send.setVisibility(View.GONE);
324			receive.setVisibility(View.GONE);
325		}
326
327		if (contact.isBlocked() && !this.showDynamicTags) {
328			lastseen.setText(R.string.contact_blocked);
329		} else {
330			lastseen.setText(UIHelper.lastseen(getApplicationContext(), contact.lastseen.time));
331		}
332
333		if (contact.getPresences().size() > 1) {
334			contactJidTv.setText(contact.getJid() + " ("
335					+ contact.getPresences().size() + ")");
336		} else {
337			contactJidTv.setText(contact.getJid().toString());
338		}
339		accountJidTv.setText(getString(R.string.using_account, contact
340					.getAccount().getJid().toBareJid()));
341		prepareContactBadge(badge, contact);
342		if (contact.getSystemAccount() == null) {
343			badge.setOnClickListener(onBadgeClick);
344		}
345
346		keys.removeAllViews();
347		boolean hasKeys = false;
348		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
349		for(final String otrFingerprint : contact.getOtrFingerprints()) {
350			hasKeys = true;
351			View view = inflater.inflate(R.layout.contact_key, keys, false);
352			TextView key = (TextView) view.findViewById(R.id.key);
353			TextView keyType = (TextView) view.findViewById(R.id.key_type);
354			ImageButton remove = (ImageButton) view
355				.findViewById(R.id.button_remove);
356			remove.setVisibility(View.VISIBLE);
357			keyType.setText("OTR Fingerprint");
358			key.setText(CryptoHelper.prettifyFingerprint(otrFingerprint));
359			keys.addView(view);
360			remove.setOnClickListener(new OnClickListener() {
361
362				@Override
363				public void onClick(View v) {
364					confirmToDeleteFingerprint(otrFingerprint);
365				}
366			});
367		}
368		if (contact.getPgpKeyId() != 0) {
369			hasKeys = true;
370			View view = inflater.inflate(R.layout.contact_key, keys, false);
371			TextView key = (TextView) view.findViewById(R.id.key);
372			TextView keyType = (TextView) view.findViewById(R.id.key_type);
373			keyType.setText("PGP Key ID");
374			key.setText(OpenPgpUtils.convertKeyIdToHex(contact.getPgpKeyId()));
375			view.setOnClickListener(new OnClickListener() {
376
377				@Override
378				public void onClick(View v) {
379					PgpEngine pgp = ContactDetailsActivity.this.xmppConnectionService
380						.getPgpEngine();
381					if (pgp != null) {
382						PendingIntent intent = pgp.getIntentForKey(contact);
383						if (intent != null) {
384							try {
385								startIntentSenderForResult(
386										intent.getIntentSender(), 0, null, 0,
387										0, 0);
388							} catch (SendIntentException e) {
389
390							}
391						}
392					}
393				}
394			});
395			keys.addView(view);
396		}
397		if (hasKeys) {
398			keys.setVisibility(View.VISIBLE);
399		} else {
400			keys.setVisibility(View.GONE);
401		}
402
403		List<ListItem.Tag> tagList = contact.getTags();
404		if (tagList.size() == 0 || !this.showDynamicTags) {
405			tags.setVisibility(View.GONE);
406		} else {
407			tags.setVisibility(View.VISIBLE);
408			tags.removeAllViewsInLayout();
409			for(final ListItem.Tag tag : tagList) {
410				final TextView tv = (TextView) inflater.inflate(R.layout.list_item_tag,tags,false);
411				tv.setText(tag.getName());
412				tv.setBackgroundColor(tag.getColor());
413				tags.addView(tv);
414			}
415		}
416	}
417
418	private void prepareContactBadge(QuickContactBadge badge, Contact contact) {
419		if (contact.getSystemAccount() != null) {
420			String[] systemAccount = contact.getSystemAccount().split("#");
421			long id = Long.parseLong(systemAccount[0]);
422			badge.assignContactUri(Contacts.getLookupUri(id, systemAccount[1]));
423		}
424		badge.setImageBitmap(avatarService().get(contact, getPixel(72)));
425	}
426
427	protected void confirmToDeleteFingerprint(final String fingerprint) {
428		AlertDialog.Builder builder = new AlertDialog.Builder(this);
429		builder.setTitle(R.string.delete_fingerprint);
430		builder.setMessage(R.string.sure_delete_fingerprint);
431		builder.setNegativeButton(R.string.cancel, null);
432		builder.setPositiveButton(R.string.delete,
433				new android.content.DialogInterface.OnClickListener() {
434
435					@Override
436					public void onClick(DialogInterface dialog, int which) {
437						if (contact.deleteOtrFingerprint(fingerprint)) {
438							populateView();
439							xmppConnectionService.syncRosterToDisk(contact.getAccount());
440						}
441					}
442
443				});
444		builder.create().show();
445	}
446
447	@Override
448	public void onBackendConnected() {
449		if ((accountJid != null) && (contactJid != null)) {
450			Account account = xmppConnectionService
451				.findAccountByJid(accountJid);
452			if (account == null) {
453				return;
454			}
455			this.contact = account.getRoster().getContact(contactJid);
456			populateView();
457		}
458	}
459
460	@Override
461	public void OnUpdateBlocklist(final Status status) {
462		runOnUiThread(new Runnable() {
463
464			@Override
465			public void run() {
466				invalidateOptionsMenu();
467				populateView();
468			}
469		});
470	}
471}