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		runOnUiThread(new Runnable() {
143
144			@Override
145			public void run() {
146				invalidateOptionsMenu();
147				populateView();
148			}
149		});
150	}
151
152	@Override
153	public void onAccountUpdate() {
154		runOnUiThread(new Runnable() {
155
156			@Override
157			public void run() {
158				invalidateOptionsMenu();
159				populateView();
160			}
161		});
162	}
163
164	@Override
165	protected String getShareableUri() {
166		if (contact != null) {
167			return contact.getShareableUri();
168		} else {
169			return "";
170		}
171	}
172
173	@Override
174	protected void onCreate(final Bundle savedInstanceState) {
175		super.onCreate(savedInstanceState);
176		if (getIntent().getAction().equals(ACTION_VIEW_CONTACT)) {
177			try {
178				this.accountJid = Jid.fromString(getIntent().getExtras().getString("account"));
179			} catch (final InvalidJidException ignored) {
180			}
181			try {
182				this.contactJid = Jid.fromString(getIntent().getExtras().getString("contact"));
183			} catch (final InvalidJidException ignored) {
184			}
185		}
186		setContentView(R.layout.activity_contact_details);
187
188		contactJidTv = (TextView) findViewById(R.id.details_contactjid);
189		accountJidTv = (TextView) findViewById(R.id.details_account);
190		lastseen = (TextView) findViewById(R.id.details_lastseen);
191		send = (CheckBox) findViewById(R.id.details_send_presence);
192		receive = (CheckBox) findViewById(R.id.details_receive_presence);
193		badge = (QuickContactBadge) findViewById(R.id.details_contact_badge);
194		addContactButton = (Button) findViewById(R.id.add_contact_button);
195		addContactButton.setOnClickListener(new OnClickListener() {
196			@Override
197			public void onClick(View view) {
198				showAddToRosterDialog(contact);
199			}
200		});
201		keys = (LinearLayout) findViewById(R.id.details_contact_keys);
202		tags = (LinearLayout) findViewById(R.id.tags);
203		if (getActionBar() != null) {
204			getActionBar().setHomeButtonEnabled(true);
205			getActionBar().setDisplayHomeAsUpEnabled(true);
206		}
207
208		final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
209		this.showDynamicTags = preferences.getBoolean("show_dynamic_tags",false);
210	}
211
212	@Override
213	public boolean onOptionsItemSelected(final MenuItem menuItem) {
214		final AlertDialog.Builder builder = new AlertDialog.Builder(this);
215		builder.setNegativeButton(getString(R.string.cancel), null);
216		switch (menuItem.getItemId()) {
217			case android.R.id.home:
218				finish();
219				break;
220			case R.id.action_delete_contact:
221				builder.setTitle(getString(R.string.action_delete_contact))
222					.setMessage(
223							getString(R.string.remove_contact_text,
224								contact.getJid()))
225					.setPositiveButton(getString(R.string.delete),
226							removeFromRoster).create().show();
227				break;
228			case R.id.action_edit_contact:
229				if (contact.getSystemAccount() == null) {
230					quickEdit(contact.getDisplayName(), new OnValueEdited() {
231
232						@Override
233						public void onValueEdited(String value) {
234							contact.setServerName(value);
235							ContactDetailsActivity.this.xmppConnectionService
236								.pushContactToServer(contact);
237							populateView();
238						}
239					});
240				} else {
241					Intent intent = new Intent(Intent.ACTION_EDIT);
242					String[] systemAccount = contact.getSystemAccount().split("#");
243					long id = Long.parseLong(systemAccount[0]);
244					Uri uri = Contacts.getLookupUri(id, systemAccount[1]);
245					intent.setDataAndType(uri, Contacts.CONTENT_ITEM_TYPE);
246					intent.putExtra("finishActivityOnSaveCompleted", true);
247					startActivity(intent);
248				}
249				break;
250			case R.id.action_block:
251				BlockContactDialog.show(this, xmppConnectionService, contact);
252				break;
253			case R.id.action_unblock:
254				BlockContactDialog.show(this, xmppConnectionService, contact);
255				break;
256		}
257		return super.onOptionsItemSelected(menuItem);
258	}
259
260	@Override
261	public boolean onCreateOptionsMenu(final Menu menu) {
262		getMenuInflater().inflate(R.menu.contact_details, menu);
263		MenuItem block = menu.findItem(R.id.action_block);
264		MenuItem unblock = menu.findItem(R.id.action_unblock);
265		MenuItem edit = menu.findItem(R.id.action_edit_contact);
266		MenuItem delete = menu.findItem(R.id.action_delete_contact);
267		final XmppConnection connection = contact.getAccount().getXmppConnection();
268		if (connection != null && connection.getFeatures().blocking()) {
269			if (this.contact.isBlocked()) {
270				menu.findItem(R.id.action_block).setVisible(false);
271			} else {
272				menu.findItem(R.id.action_unblock).setVisible(false);
273			}
274		} else {
275			menu.findItem(R.id.action_unblock).setVisible(false);
276			menu.findItem(R.id.action_block).setVisible(false);
277		}
278		if (!contact.showInRoster()) {
279			edit.setVisible(false);
280			delete.setVisible(false);
281		}
282		return true;
283	}
284
285	private void populateView() {
286		setTitle(contact.getDisplayName());
287		if (contact.showInRoster()) {
288			send.setVisibility(View.VISIBLE);
289			receive.setVisibility(View.VISIBLE);
290			addContactButton.setVisibility(View.GONE);
291			send.setOnCheckedChangeListener(null);
292			receive.setOnCheckedChangeListener(null);
293
294			if (contact.getOption(Contact.Options.FROM)) {
295				send.setText(R.string.send_presence_updates);
296				send.setChecked(true);
297			} else if (contact.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
298				send.setChecked(false);
299				send.setText(R.string.send_presence_updates);
300			} else {
301				send.setText(R.string.preemptively_grant);
302				if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
303					send.setChecked(true);
304				} else {
305					send.setChecked(false);
306				}
307			}
308			if (contact.getOption(Contact.Options.TO)) {
309				receive.setText(R.string.receive_presence_updates);
310				receive.setChecked(true);
311			} else {
312				receive.setText(R.string.ask_for_presence_updates);
313				if (contact.getOption(Contact.Options.ASKING)) {
314					receive.setChecked(true);
315				} else {
316					receive.setChecked(false);
317				}
318			}
319			if (contact.getAccount().isOnlineAndConnected()) {
320				receive.setEnabled(true);
321				send.setEnabled(true);
322			} else {
323				receive.setEnabled(false);
324				send.setEnabled(false);
325			}
326
327			send.setOnCheckedChangeListener(this.mOnSendCheckedChange);
328			receive.setOnCheckedChangeListener(this.mOnReceiveCheckedChange);
329		} else {
330			addContactButton.setVisibility(View.VISIBLE);
331			send.setVisibility(View.GONE);
332			receive.setVisibility(View.GONE);
333		}
334
335		if (contact.isBlocked() && !this.showDynamicTags) {
336			lastseen.setText(R.string.contact_blocked);
337		} else {
338			lastseen.setText(UIHelper.lastseen(getApplicationContext(), contact.lastseen.time));
339		}
340
341		if (contact.getPresences().size() > 1) {
342			contactJidTv.setText(contact.getJid() + " ("
343					+ contact.getPresences().size() + ")");
344		} else {
345			contactJidTv.setText(contact.getJid().toString());
346		}
347		accountJidTv.setText(getString(R.string.using_account, contact
348					.getAccount().getJid().toBareJid()));
349		prepareContactBadge(badge, contact);
350		if (contact.getSystemAccount() == null) {
351			badge.setOnClickListener(onBadgeClick);
352		}
353
354		keys.removeAllViews();
355		boolean hasKeys = false;
356		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
357		for(final String otrFingerprint : contact.getOtrFingerprints()) {
358			hasKeys = true;
359			View view = inflater.inflate(R.layout.contact_key, keys, false);
360			TextView key = (TextView) view.findViewById(R.id.key);
361			TextView keyType = (TextView) view.findViewById(R.id.key_type);
362			ImageButton remove = (ImageButton) view
363				.findViewById(R.id.button_remove);
364			remove.setVisibility(View.VISIBLE);
365			keyType.setText("OTR Fingerprint");
366			key.setText(CryptoHelper.prettifyFingerprint(otrFingerprint));
367			keys.addView(view);
368			remove.setOnClickListener(new OnClickListener() {
369
370				@Override
371				public void onClick(View v) {
372					confirmToDeleteFingerprint(otrFingerprint);
373				}
374			});
375		}
376		if (contact.getPgpKeyId() != 0) {
377			hasKeys = true;
378			View view = inflater.inflate(R.layout.contact_key, keys, false);
379			TextView key = (TextView) view.findViewById(R.id.key);
380			TextView keyType = (TextView) view.findViewById(R.id.key_type);
381			keyType.setText("PGP Key ID");
382			key.setText(OpenPgpUtils.convertKeyIdToHex(contact.getPgpKeyId()));
383			view.setOnClickListener(new OnClickListener() {
384
385				@Override
386				public void onClick(View v) {
387					PgpEngine pgp = ContactDetailsActivity.this.xmppConnectionService
388						.getPgpEngine();
389					if (pgp != null) {
390						PendingIntent intent = pgp.getIntentForKey(contact);
391						if (intent != null) {
392							try {
393								startIntentSenderForResult(
394										intent.getIntentSender(), 0, null, 0,
395										0, 0);
396							} catch (SendIntentException e) {
397
398							}
399						}
400					}
401				}
402			});
403			keys.addView(view);
404		}
405		if (hasKeys) {
406			keys.setVisibility(View.VISIBLE);
407		} else {
408			keys.setVisibility(View.GONE);
409		}
410
411		List<ListItem.Tag> tagList = contact.getTags();
412		if (tagList.size() == 0 || !this.showDynamicTags) {
413			tags.setVisibility(View.GONE);
414		} else {
415			tags.setVisibility(View.VISIBLE);
416			tags.removeAllViewsInLayout();
417			for(final ListItem.Tag tag : tagList) {
418				final TextView tv = (TextView) inflater.inflate(R.layout.list_item_tag,tags,false);
419				tv.setText(tag.getName());
420				tv.setBackgroundColor(tag.getColor());
421				tags.addView(tv);
422			}
423		}
424	}
425
426	private void prepareContactBadge(QuickContactBadge badge, Contact contact) {
427		if (contact.getSystemAccount() != null) {
428			String[] systemAccount = contact.getSystemAccount().split("#");
429			long id = Long.parseLong(systemAccount[0]);
430			badge.assignContactUri(Contacts.getLookupUri(id, systemAccount[1]));
431		}
432		badge.setImageBitmap(avatarService().get(contact, getPixel(72)));
433	}
434
435	protected void confirmToDeleteFingerprint(final String fingerprint) {
436		AlertDialog.Builder builder = new AlertDialog.Builder(this);
437		builder.setTitle(R.string.delete_fingerprint);
438		builder.setMessage(R.string.sure_delete_fingerprint);
439		builder.setNegativeButton(R.string.cancel, null);
440		builder.setPositiveButton(R.string.delete,
441				new android.content.DialogInterface.OnClickListener() {
442
443					@Override
444					public void onClick(DialogInterface dialog, int which) {
445						if (contact.deleteOtrFingerprint(fingerprint)) {
446							populateView();
447							xmppConnectionService.syncRosterToDisk(contact.getAccount());
448						}
449					}
450
451				});
452		builder.create().show();
453	}
454
455	@Override
456	public void onBackendConnected() {
457		if ((accountJid != null) && (contactJid != null)) {
458			Account account = xmppConnectionService
459				.findAccountByJid(accountJid);
460			if (account == null) {
461				return;
462			}
463			this.contact = account.getRoster().getContact(contactJid);
464			populateView();
465		}
466	}
467
468	@Override
469	public void OnUpdateBlocklist(final Status status) {
470		runOnUiThread(new Runnable() {
471
472			@Override
473			public void run() {
474				invalidateOptionsMenu();
475				populateView();
476			}
477		});
478	}
479}