aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/siacs/conversations/ui/ContactDetailsActivity.java
blob: 2ec6459b292276a733bca108ad2d5ce7b65d9aa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package eu.siacs.conversations.ui;

import java.util.Iterator;

import org.openintents.openpgp.util.OpenPgpUtils;

import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Intents;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.QuickContactBadge;
import android.widget.TextView;
import eu.siacs.conversations.R;
import eu.siacs.conversations.crypto.PgpEngine;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Presences;
import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate;
import eu.siacs.conversations.utils.UIHelper;

public class ContactDetailsActivity extends XmppActivity {
	public static final String ACTION_VIEW_CONTACT = "view_contact";

	protected ContactDetailsActivity activity = this;

	private Contact contact;

	private String accountJid;
	private String contactJid;

	private TextView contactJidTv;
	private TextView accountJidTv;
	private TextView status;
	private TextView lastseen;
	private CheckBox send;
	private CheckBox receive;
	private QuickContactBadge badge;

	private DialogInterface.OnClickListener removeFromRoster = new DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			activity.xmppConnectionService.deleteContactOnServer(contact);
			activity.finish();
		}
	};

	private DialogInterface.OnClickListener addToPhonebook = new DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
			intent.setType(Contacts.CONTENT_ITEM_TYPE);
			intent.putExtra(Intents.Insert.IM_HANDLE, contact.getJid());
			intent.putExtra(Intents.Insert.IM_PROTOCOL,
					CommonDataKinds.Im.PROTOCOL_JABBER);
			intent.putExtra("finishActivityOnSaveCompleted", true);
			activity.startActivityForResult(intent, 0);
		}
	};
	private OnClickListener onBadgeClick = new OnClickListener() {

		@Override
		public void onClick(View v) {
			AlertDialog.Builder builder = new AlertDialog.Builder(activity);
			builder.setTitle(getString(R.string.action_add_phone_book));
			builder.setMessage(getString(R.string.add_phone_book_text,
					contact.getJid()));
			builder.setNegativeButton(getString(R.string.cancel), null);
			builder.setPositiveButton(getString(R.string.add), addToPhonebook);
			builder.create().show();
		}
	};

	private LinearLayout keys;

	private OnRosterUpdate rosterUpdate = new OnRosterUpdate() {

		@Override
		public void onRosterUpdate() {
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					populateView();
				}
			});
		}
	};

	private OnCheckedChangeListener mOnSendCheckedChange = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			if (isChecked) {
				if (contact.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
					xmppConnectionService.sendPresencePacket(contact
							.getAccount(),
							xmppConnectionService.getPresenceGenerator()
									.sendPresenceUpdatesTo(contact));
				} else {
					contact.setOption(Contact.Options.PREEMPTIVE_GRANT);
				}
			} else {
				contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
				xmppConnectionService.sendPresencePacket(contact.getAccount(),
						xmppConnectionService.getPresenceGenerator()
								.stopPresenceUpdatesTo(contact));
			}
		}
	};

	private OnCheckedChangeListener mOnReceiveCheckedChange = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			if (isChecked) {
				xmppConnectionService.sendPresencePacket(contact.getAccount(),
						xmppConnectionService.getPresenceGenerator()
								.requestPresenceUpdatesFrom(contact));
			} else {
				xmppConnectionService.sendPresencePacket(contact.getAccount(),
						xmppConnectionService.getPresenceGenerator()
								.stopPresenceUpdatesFrom(contact));
			}
		}
	};

	private OnAccountUpdate accountUpdate = new OnAccountUpdate() {
		
		@Override
		public void onAccountUpdate() {
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					populateView();
				}
			});
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (getIntent().getAction().equals(ACTION_VIEW_CONTACT)) {
			this.accountJid = getIntent().getExtras().getString("account");
			this.contactJid = getIntent().getExtras().getString("contact");
		}
		setContentView(R.layout.activity_contact_details);

		contactJidTv = (TextView) findViewById(R.id.details_contactjid);
		accountJidTv = (TextView) findViewById(R.id.details_account);
		status = (TextView) findViewById(R.id.details_contactstatus);
		lastseen = (TextView) findViewById(R.id.details_lastseen);
		send = (CheckBox) findViewById(R.id.details_send_presence);
		send.setOnCheckedChangeListener(this.mOnSendCheckedChange);
		receive = (CheckBox) findViewById(R.id.details_receive_presence);
		receive.setOnCheckedChangeListener(this.mOnReceiveCheckedChange);
		badge = (QuickContactBadge) findViewById(R.id.details_contact_badge);
		keys = (LinearLayout) findViewById(R.id.details_contact_keys);
		getActionBar().setHomeButtonEnabled(true);
		getActionBar().setDisplayHomeAsUpEnabled(true);

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem menuItem) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setNegativeButton(getString(R.string.cancel), null);
		switch (menuItem.getItemId()) {
		case android.R.id.home:
			finish();
			break;
		case R.id.action_delete_contact:
			builder.setTitle(getString(R.string.action_delete_contact))
					.setMessage(
							getString(R.string.remove_contact_text,
									contact.getJid()))
					.setPositiveButton(getString(R.string.delete),
							removeFromRoster).create().show();
			break;
		case R.id.action_edit_contact:
			if (contact.getSystemAccount() == null) {
				quickEdit(contact.getDisplayName(), new OnValueEdited() {

					@Override
					public void onValueEdited(String value) {
						contact.setServerName(value);
						activity.xmppConnectionService
								.pushContactToServer(contact);
						populateView();
					}
				});
			} else {
				Intent intent = new Intent(Intent.ACTION_EDIT);
				String[] systemAccount = contact.getSystemAccount().split("#");
				long id = Long.parseLong(systemAccount[0]);
				Uri uri = Contacts.getLookupUri(id, systemAccount[1]);
				intent.setDataAndType(uri, Contacts.CONTENT_ITEM_TYPE);
				intent.putExtra("finishActivityOnSaveCompleted", true);
				startActivity(intent);
			}
			break;
		}
		return super.onOptionsItemSelected(menuItem);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.contact_details, menu);
		return true;
	}

	private void populateView() {
		setTitle(contact.getDisplayName());
		if (contact.getOption(Contact.Options.FROM)) {
			send.setText(R.string.send_presence_updates);
			send.setChecked(true);
		} else if (contact
				.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
			send.setChecked(false);
			send.setText(R.string.send_presence_updates);
		} else {
			send.setText(R.string.preemptively_grant);
			if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
				send.setChecked(true);
			} else {
				send.setChecked(false);
			}
		}
		if (contact.getOption(Contact.Options.TO)) {
			receive.setText(R.string.receive_presence_updates);
			receive.setChecked(true);
		} else {
			receive.setText(R.string.ask_for_presence_updates);
			if (contact.getOption(Contact.Options.ASKING)) {
				receive.setChecked(true);
			} else {
				receive.setChecked(false);
			}
		}
		if (contact.getAccount().getStatus() == Account.STATUS_ONLINE) {
			receive.setEnabled(true);
			send.setEnabled(true);
		} else {
			receive.setEnabled(false);
			send.setEnabled(false);
		}

		lastseen.setText(UIHelper.lastseen(getApplicationContext(),
				contact.lastseen.time));

		switch (contact.getMostAvailableStatus()) {
		case Presences.CHAT:
			status.setText(R.string.contact_status_free_to_chat);
			status.setTextColor(0xFF83b600);
			break;
		case Presences.ONLINE:
			status.setText(R.string.contact_status_online);
			status.setTextColor(0xFF83b600);
			break;
		case Presences.AWAY:
			status.setText(R.string.contact_status_away);
			status.setTextColor(0xFFffa713);
			break;
		case Presences.XA:
			status.setText(R.string.contact_status_extended_away);
			status.setTextColor(0xFFffa713);
			break;
		case Presences.DND:
			status.setText(R.string.contact_status_do_not_disturb);
			status.setTextColor(0xFFe92727);
			break;
		case Presences.OFFLINE:
			status.setText(R.string.contact_status_offline);
			status.setTextColor(0xFFe92727);
			break;
		default:
			status.setText(R.string.contact_status_offline);
			status.setTextColor(0xFFe92727);
			break;
		}
		if (contact.getPresences().size() > 1) {
			contactJidTv.setText(contact.getJid() + " ("
					+ contact.getPresences().size() + ")");
		} else {
			contactJidTv.setText(contact.getJid());
		}
		accountJidTv.setText(contact.getAccount().getJid());

		UIHelper.prepareContactBadge(this, badge, contact,
				getApplicationContext());

		if (contact.getSystemAccount() == null) {
			badge.setOnClickListener(onBadgeClick);
		}

		keys.removeAllViews();
		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		for (Iterator<String> iterator = contact.getOtrFingerprints()
				.iterator(); iterator.hasNext();) {
			String otrFingerprint = iterator.next();
			View view = (View) inflater.inflate(R.layout.contact_key, null);
			TextView key = (TextView) view.findViewById(R.id.key);
			TextView keyType = (TextView) view.findViewById(R.id.key_type);
			keyType.setText("OTR Fingerprint");
			key.setText(otrFingerprint);
			keys.addView(view);
		}
		if (contact.getPgpKeyId() != 0) {
			View view = (View) inflater.inflate(R.layout.contact_key, null);
			TextView key = (TextView) view.findViewById(R.id.key);
			TextView keyType = (TextView) view.findViewById(R.id.key_type);
			keyType.setText("PGP Key ID");
			key.setText(OpenPgpUtils.convertKeyIdToHex(contact.getPgpKeyId()));
			view.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					PgpEngine pgp = activity.xmppConnectionService
							.getPgpEngine();
					if (pgp != null) {
						PendingIntent intent = pgp.getIntentForKey(contact);
						if (intent != null) {
							try {
								startIntentSenderForResult(
										intent.getIntentSender(), 0, null, 0,
										0, 0);
							} catch (SendIntentException e) {

							}
						}
					}
				}
			});
			keys.addView(view);
		}
	}

	@Override
	public void onBackendConnected() {
		xmppConnectionService.setOnRosterUpdateListener(this.rosterUpdate);
		xmppConnectionService.setOnAccountListChangedListener(this.accountUpdate );
		if ((accountJid != null) && (contactJid != null)) {
			Account account = xmppConnectionService
					.findAccountByJid(accountJid);
			if (account == null) {
				return;
			}
			this.contact = account.getRoster().getContact(contactJid);
			populateView();
		}
	}

	@Override
	protected void onStop() {
		super.onStop();
		xmppConnectionService.removeOnRosterUpdateListener();
		xmppConnectionService.removeOnAccountListChangedListener();
	}

}