ContactDetailsActivity.java

  1package eu.siacs.conversations.ui;
  2
  3import java.util.Iterator;
  4
  5import org.openintents.openpgp.util.OpenPgpUtils;
  6
  7import android.app.AlertDialog;
  8import android.app.PendingIntent;
  9import android.content.Context;
 10import android.content.DialogInterface;
 11import android.content.Intent;
 12import android.content.IntentSender.SendIntentException;
 13import android.net.Uri;
 14import android.os.Bundle;
 15import android.provider.ContactsContract.CommonDataKinds;
 16import android.provider.ContactsContract.Contacts;
 17import android.provider.ContactsContract.Intents;
 18import android.view.LayoutInflater;
 19import android.view.Menu;
 20import android.view.MenuItem;
 21import android.view.View;
 22import android.view.View.OnClickListener;
 23import android.widget.CheckBox;
 24import android.widget.EditText;
 25import android.widget.LinearLayout;
 26import android.widget.QuickContactBadge;
 27import android.widget.TextView;
 28import android.widget.Toast;
 29import eu.siacs.conversations.R;
 30import eu.siacs.conversations.crypto.PgpEngine;
 31import eu.siacs.conversations.entities.Account;
 32import eu.siacs.conversations.entities.Contact;
 33import eu.siacs.conversations.entities.Presences;
 34import eu.siacs.conversations.utils.UIHelper;
 35
 36public class ContactDetailsActivity extends XmppActivity {
 37	public static final String ACTION_VIEW_CONTACT = "view_contact";
 38
 39	protected ContactDetailsActivity activity = this;
 40
 41	private Contact contact;
 42	
 43	private String accountJid;
 44	private String contactJid;
 45	
 46	private EditText name;
 47	private TextView contactJidTv;
 48	private TextView accountJidTv;
 49	private TextView status;
 50	private TextView askAgain;
 51	private CheckBox send;
 52	private CheckBox receive;
 53	private QuickContactBadge badge;
 54
 55	private DialogInterface.OnClickListener removeFromRoster = new DialogInterface.OnClickListener() {
 56
 57		@Override
 58		public void onClick(DialogInterface dialog, int which) {
 59			activity.xmppConnectionService.deleteContactOnServer(contact);
 60			activity.finish();
 61		}
 62	};
 63
 64	private DialogInterface.OnClickListener editContactNameListener = new DialogInterface.OnClickListener() {
 65
 66		@Override
 67		public void onClick(DialogInterface dialog, int which) {
 68			contact.setServerName(name.getText().toString());
 69			activity.xmppConnectionService.pushContactToServer(contact);
 70			populateView();
 71		}
 72	};
 73
 74	private DialogInterface.OnClickListener addToPhonebook = new DialogInterface.OnClickListener() {
 75
 76		@Override
 77		public void onClick(DialogInterface dialog, int which) {
 78			Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
 79			intent.setType(Contacts.CONTENT_ITEM_TYPE);
 80			intent.putExtra(Intents.Insert.IM_HANDLE, contact.getJid());
 81			intent.putExtra(Intents.Insert.IM_PROTOCOL,
 82					CommonDataKinds.Im.PROTOCOL_JABBER);
 83			intent.putExtra("finishActivityOnSaveCompleted", true);
 84			activity.startActivityForResult(intent, 0);
 85		}
 86	};
 87	private OnClickListener onBadgeClick = new OnClickListener() {
 88
 89		@Override
 90		public void onClick(View v) {
 91			AlertDialog.Builder builder = new AlertDialog.Builder(activity);
 92			builder.setTitle(getString(R.string.action_add_phone_book));
 93			builder.setMessage(getString(R.string.add_phone_book_text, contact.getJid()));
 94			builder.setNegativeButton(getString(R.string.cancel), null);
 95			builder.setPositiveButton(getString(R.string.add), addToPhonebook);
 96			builder.create().show();
 97		}
 98	};
 99
100	private LinearLayout keys;
101
102	@Override
103	protected void onCreate(Bundle savedInstanceState) {
104		super.onCreate(savedInstanceState);
105		if (getIntent().getAction().equals(ACTION_VIEW_CONTACT)) {
106			this.accountJid = getIntent().getExtras().getString("account");
107			this.contactJid = getIntent().getExtras().getString("contact");
108		}
109		setContentView(R.layout.activity_contact_details);
110
111		contactJidTv = (TextView) findViewById(R.id.details_contactjid);
112		accountJidTv = (TextView) findViewById(R.id.details_account);
113		status = (TextView) findViewById(R.id.details_contactstatus);
114		send = (CheckBox) findViewById(R.id.details_send_presence);
115		receive = (CheckBox) findViewById(R.id.details_receive_presence);
116		askAgain = (TextView) findViewById(R.id.ask_again);
117		badge = (QuickContactBadge) findViewById(R.id.details_contact_badge);
118		keys = (LinearLayout) findViewById(R.id.details_contact_keys);
119		getActionBar().setHomeButtonEnabled(true);
120		getActionBar().setDisplayHomeAsUpEnabled(true);
121
122	}
123
124	@Override
125	public boolean onOptionsItemSelected(MenuItem menuItem) {
126		AlertDialog.Builder builder = new AlertDialog.Builder(this);
127		builder.setNegativeButton(getString(R.string.cancel), null);
128		switch (menuItem.getItemId()) {
129		case android.R.id.home:
130			finish();
131			break;
132		case R.id.action_delete_contact:
133			builder.setTitle(getString(R.string.action_delete_contact))
134					.setMessage(
135							getString(R.string.remove_contact_text,
136									contact.getJid()))
137					.setPositiveButton(getString(R.string.delete), removeFromRoster).create()
138					.show();
139			break;
140		case R.id.action_edit_contact:
141			if (contact.getSystemAccount() == null) {
142
143				View view = (View) getLayoutInflater().inflate(
144						R.layout.edit_contact_name, null);
145				name = (EditText) view.findViewById(R.id.editText1);
146				name.setText(contact.getDisplayName());
147				builder.setView(view).setTitle(contact.getJid())
148						.setPositiveButton(getString(R.string.edit), editContactNameListener)
149						.create().show();
150
151			} else {
152				Intent intent = new Intent(Intent.ACTION_EDIT);
153				String[] systemAccount = contact.getSystemAccount().split("#");
154				long id = Long.parseLong(systemAccount[0]);
155				Uri uri = Contacts.getLookupUri(id, systemAccount[1]);
156				intent.setDataAndType(uri, Contacts.CONTENT_ITEM_TYPE);
157				intent.putExtra("finishActivityOnSaveCompleted", true);
158				startActivity(intent);
159			}
160			break;
161		}
162		return super.onOptionsItemSelected(menuItem);
163	}
164
165	@Override
166	public boolean onCreateOptionsMenu(Menu menu) {
167		getMenuInflater().inflate(R.menu.contact_details, menu);
168		return true;
169	}
170
171	private void populateView() {
172		setTitle(contact.getDisplayName());
173		if (contact.getOption(Contact.Options.FROM)) {
174			send.setChecked(true);
175		} else {
176			send.setText(R.string.preemptively_grant);
177			if (contact
178					.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
179				send.setChecked(true);
180			} else {
181				send.setChecked(false);
182			}
183		}
184		if (contact.getOption(Contact.Options.TO)) {
185			receive.setChecked(true);
186		} else {
187			receive.setText(R.string.ask_for_presence_updates);
188			askAgain.setVisibility(View.VISIBLE);
189			askAgain.setOnClickListener(new OnClickListener() {
190				
191				@Override
192				public void onClick(View v) {
193					Toast.makeText(getApplicationContext(), getString(R.string.asked_for_presence_updates),
194                            Toast.LENGTH_SHORT).show();
195					xmppConnectionService.requestPresenceUpdatesFrom(contact);
196					
197				}
198			});
199			if (contact.getOption(Contact.Options.ASKING)) {
200				receive.setChecked(true);
201			} else {
202				receive.setChecked(false);
203			}
204		}
205
206		switch (contact.getMostAvailableStatus()) {
207		case Presences.CHAT:
208			status.setText(R.string.contact_status_free_to_chat);
209			status.setTextColor(0xFF83b600);
210			break;
211		case Presences.ONLINE:
212			status.setText(R.string.contact_status_online);
213			status.setTextColor(0xFF83b600);
214			break;
215		case Presences.AWAY:
216			status.setText(R.string.contact_status_away);
217			status.setTextColor(0xFFffa713);
218			break;
219		case Presences.XA:
220			status.setText(R.string.contact_status_extended_away);
221			status.setTextColor(0xFFffa713);
222			break;
223		case Presences.DND:
224			status.setText(R.string.contact_status_do_not_disturb);
225			status.setTextColor(0xFFe92727);
226			break;
227		case Presences.OFFLINE:
228			status.setText(R.string.contact_status_offline);
229			status.setTextColor(0xFFe92727);
230			break;
231		default:
232			status.setText(R.string.contact_status_offline);
233			status.setTextColor(0xFFe92727);
234			break;
235		}
236		if (contact.getPresences().size() > 1) {
237			contactJidTv.setText(contact.getJid()+" ("+contact.getPresences().size()+")");
238		} else {
239			contactJidTv.setText(contact.getJid());
240		}
241		accountJidTv.setText(contact.getAccount().getJid());
242
243		UIHelper.prepareContactBadge(this, badge, contact, getApplicationContext());
244
245		if (contact.getSystemAccount() == null) {
246			badge.setOnClickListener(onBadgeClick);
247		}
248
249		keys.removeAllViews();
250		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
251		for (Iterator<String> iterator = contact.getOtrFingerprints()
252				.iterator(); iterator.hasNext();) {
253			String otrFingerprint = iterator.next();
254			View view = (View) inflater.inflate(R.layout.contact_key, null);
255			TextView key = (TextView) view.findViewById(R.id.key);
256			TextView keyType = (TextView) view.findViewById(R.id.key_type);
257			keyType.setText("OTR Fingerprint");
258			key.setText(otrFingerprint);
259			keys.addView(view);
260		}
261		if (contact.getPgpKeyId() != 0) {
262			View view = (View) inflater.inflate(R.layout.contact_key, null);
263			TextView key = (TextView) view.findViewById(R.id.key);
264			TextView keyType = (TextView) view.findViewById(R.id.key_type);
265			keyType.setText("PGP Key ID");
266			key.setText(OpenPgpUtils.convertKeyIdToHex(contact.getPgpKeyId()));
267			view.setOnClickListener(new OnClickListener() {
268				
269				@Override
270				public void onClick(View v) {
271					PgpEngine pgp = activity.xmppConnectionService.getPgpEngine();
272					if (pgp!=null) {
273						PendingIntent intent = pgp.getIntentForKey(contact);
274						if (intent!=null) {
275							try {
276								startIntentSenderForResult(intent.getIntentSender(), 0, null, 0, 0, 0);
277							} catch (SendIntentException e) {
278								
279							}
280						}
281					}
282				}
283			});
284			keys.addView(view);
285		}
286	}
287
288	@Override
289	public void onBackendConnected() {
290		if ((accountJid != null)&&(contactJid != null)) {
291			Account account = xmppConnectionService.findAccountByJid(accountJid);
292			if (account==null) {
293				return;
294			}
295			this.contact = account.getRoster().getContact(contactJid);
296			populateView();
297		}
298	}
299
300	@Override
301	protected void onStop() {
302		super.onStop();
303		boolean updated = false;
304		boolean online = contact.getAccount().getStatus() == Account.STATUS_ONLINE;
305		if (contact.getOption(Contact.Options.FROM)) {
306			if (!send.isChecked()) {
307				if (online) {
308					contact.resetOption(Contact.Options.FROM);
309					contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
310					activity.xmppConnectionService.stopPresenceUpdatesTo(contact);
311				}
312				updated = true;
313			}
314		} else {
315			if (contact
316					.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
317				if (!send.isChecked()) {
318					if (online) {
319						contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
320					}
321					updated = true;
322				}
323			} else {
324				if (send.isChecked()) {
325					if (online) {
326						contact.setOption(Contact.Options.PREEMPTIVE_GRANT);
327					}
328					updated = true;
329				}
330			}
331		}
332		if (contact.getOption(Contact.Options.TO)) {
333			if (!receive.isChecked()) {
334				if (online) {
335					contact.resetOption(Contact.Options.TO);
336					activity.xmppConnectionService.stopPresenceUpdatesFrom(contact);
337				}
338				updated = true;
339			}
340		} else {
341			if (contact.getOption(Contact.Options.ASKING)) {
342				if (!receive.isChecked()) {
343					if (online) {
344						contact.resetOption(Contact.Options.ASKING);
345						activity.xmppConnectionService
346							.stopPresenceUpdatesFrom(contact);
347					}
348					updated = true;
349				}
350			} else {
351				if (receive.isChecked()) {
352					if (online) {
353						contact.setOption(Contact.Options.ASKING);
354						activity.xmppConnectionService
355							.requestPresenceUpdatesFrom(contact);
356					}
357					updated = true;
358				}
359			}
360		}
361		if (updated) {
362			if (online) {
363				Toast.makeText(getApplicationContext(), getString(R.string.subscription_updated), Toast.LENGTH_SHORT).show();
364			} else {
365				Toast.makeText(getApplicationContext(), getString(R.string.subscription_not_updated_offline), Toast.LENGTH_SHORT).show();
366			}
367		}
368	}
369
370}