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