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