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