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.Iterator;
32import java.util.List;
33
34import eu.siacs.conversations.R;
35import eu.siacs.conversations.crypto.PgpEngine;
36import eu.siacs.conversations.entities.Account;
37import eu.siacs.conversations.entities.Contact;
38import eu.siacs.conversations.entities.ListItem;
39import eu.siacs.conversations.entities.Presences;
40import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
41import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate;
42import eu.siacs.conversations.utils.UIHelper;
43import eu.siacs.conversations.xmpp.jid.InvalidJidException;
44import eu.siacs.conversations.xmpp.jid.Jid;
45
46public class ContactDetailsActivity extends XmppActivity implements OnAccountUpdate, OnRosterUpdate {
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(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 getActionBar().setHomeButtonEnabled(true);
194 getActionBar().setDisplayHomeAsUpEnabled(true);
195
196 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
197 this.showDynamicTags = preferences.getBoolean("show_dynamic_tags",false);
198 }
199
200 @Override
201 public boolean onOptionsItemSelected(MenuItem menuItem) {
202 AlertDialog.Builder builder = new AlertDialog.Builder(this);
203 builder.setNegativeButton(getString(R.string.cancel), null);
204 switch (menuItem.getItemId()) {
205 case android.R.id.home:
206 finish();
207 break;
208 case R.id.action_delete_contact:
209 builder.setTitle(getString(R.string.action_delete_contact))
210 .setMessage(
211 getString(R.string.remove_contact_text,
212 contact.getJid()))
213 .setPositiveButton(getString(R.string.delete),
214 removeFromRoster).create().show();
215 break;
216 case R.id.action_edit_contact:
217 if (contact.getSystemAccount() == null) {
218 quickEdit(contact.getDisplayName(), new OnValueEdited() {
219
220 @Override
221 public void onValueEdited(String value) {
222 contact.setServerName(value);
223 ContactDetailsActivity.this.xmppConnectionService
224 .pushContactToServer(contact);
225 populateView();
226 }
227 });
228 } else {
229 Intent intent = new Intent(Intent.ACTION_EDIT);
230 String[] systemAccount = contact.getSystemAccount().split("#");
231 long id = Long.parseLong(systemAccount[0]);
232 Uri uri = Contacts.getLookupUri(id, systemAccount[1]);
233 intent.setDataAndType(uri, Contacts.CONTENT_ITEM_TYPE);
234 intent.putExtra("finishActivityOnSaveCompleted", true);
235 startActivity(intent);
236 }
237 break;
238 }
239 return super.onOptionsItemSelected(menuItem);
240 }
241
242 @Override
243 public boolean onCreateOptionsMenu(Menu menu) {
244 getMenuInflater().inflate(R.menu.contact_details, menu);
245 return true;
246 }
247
248 private void populateView() {
249 send.setOnCheckedChangeListener(null);
250 receive.setOnCheckedChangeListener(null);
251 setTitle(contact.getDisplayName());
252 if (contact.getOption(Contact.Options.FROM)) {
253 send.setText(R.string.send_presence_updates);
254 send.setChecked(true);
255 } else if (contact
256 .getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
257 send.setChecked(false);
258 send.setText(R.string.send_presence_updates);
259 } else {
260 send.setText(R.string.preemptively_grant);
261 if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
262 send.setChecked(true);
263 } else {
264 send.setChecked(false);
265 }
266 }
267 if (contact.getOption(Contact.Options.TO)) {
268 receive.setText(R.string.receive_presence_updates);
269 receive.setChecked(true);
270 } else {
271 receive.setText(R.string.ask_for_presence_updates);
272 if (contact.getOption(Contact.Options.ASKING)) {
273 receive.setChecked(true);
274 } else {
275 receive.setChecked(false);
276 }
277 }
278 if (contact.getAccount().getStatus() == Account.State.ONLINE) {
279 receive.setEnabled(true);
280 send.setEnabled(true);
281 } else {
282 receive.setEnabled(false);
283 send.setEnabled(false);
284 }
285
286 send.setOnCheckedChangeListener(this.mOnSendCheckedChange);
287 receive.setOnCheckedChangeListener(this.mOnReceiveCheckedChange);
288
289 lastseen.setText(UIHelper.lastseen(getApplicationContext(),
290 contact.lastseen.time));
291
292 if (contact.getPresences().size() > 1) {
293 contactJidTv.setText(contact.getJid() + " ("
294 + contact.getPresences().size() + ")");
295 } else {
296 contactJidTv.setText(contact.getJid().toString());
297 }
298 accountJidTv.setText(getString(R.string.using_account, contact
299 .getAccount().getJid().toBareJid()));
300 prepareContactBadge(badge, contact);
301 if (contact.getSystemAccount() == null) {
302 badge.setOnClickListener(onBadgeClick);
303 }
304
305 keys.removeAllViews();
306 boolean hasKeys = false;
307 LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
308 for(final String otrFingerprint : contact.getOtrFingerprints()) {
309 hasKeys = true;
310 View view = inflater.inflate(R.layout.contact_key, keys, false);
311 TextView key = (TextView) view.findViewById(R.id.key);
312 TextView keyType = (TextView) view.findViewById(R.id.key_type);
313 ImageButton remove = (ImageButton) view
314 .findViewById(R.id.button_remove);
315 remove.setVisibility(View.VISIBLE);
316 keyType.setText("OTR Fingerprint");
317 key.setText(otrFingerprint);
318 keys.addView(view);
319 remove.setOnClickListener(new OnClickListener() {
320
321 @Override
322 public void onClick(View v) {
323 confirmToDeleteFingerprint(otrFingerprint);
324 }
325 });
326 }
327 if (contact.getPgpKeyId() != 0) {
328 hasKeys = true;
329 View view = inflater.inflate(R.layout.contact_key, keys, false);
330 TextView key = (TextView) view.findViewById(R.id.key);
331 TextView keyType = (TextView) view.findViewById(R.id.key_type);
332 keyType.setText("PGP Key ID");
333 key.setText(OpenPgpUtils.convertKeyIdToHex(contact.getPgpKeyId()));
334 view.setOnClickListener(new OnClickListener() {
335
336 @Override
337 public void onClick(View v) {
338 PgpEngine pgp = ContactDetailsActivity.this.xmppConnectionService
339 .getPgpEngine();
340 if (pgp != null) {
341 PendingIntent intent = pgp.getIntentForKey(contact);
342 if (intent != null) {
343 try {
344 startIntentSenderForResult(
345 intent.getIntentSender(), 0, null, 0,
346 0, 0);
347 } catch (SendIntentException e) {
348
349 }
350 }
351 }
352 }
353 });
354 keys.addView(view);
355 }
356 if (hasKeys) {
357 keys.setVisibility(View.VISIBLE);
358 } else {
359 keys.setVisibility(View.GONE);
360 }
361
362 List<ListItem.Tag> tagList = contact.getTags();
363 if (tagList.size() == 0 || !this.showDynamicTags) {
364 tags.setVisibility(View.GONE);
365 } else {
366 tags.setVisibility(View.VISIBLE);
367 tags.removeAllViewsInLayout();
368 for(ListItem.Tag tag : tagList) {
369 TextView tv = (TextView) inflater.inflate(R.layout.list_item_tag,tags,false);
370 tv.setText(tag.getName());
371 tv.setBackgroundColor(tag.getColor());
372 tags.addView(tv);
373 }
374 }
375 }
376
377 private void prepareContactBadge(QuickContactBadge badge, Contact contact) {
378 if (contact.getSystemAccount() != null) {
379 String[] systemAccount = contact.getSystemAccount().split("#");
380 long id = Long.parseLong(systemAccount[0]);
381 badge.assignContactUri(Contacts.getLookupUri(id, systemAccount[1]));
382 }
383 badge.setImageBitmap(avatarService().get(contact, getPixel(72)));
384 }
385
386 protected void confirmToDeleteFingerprint(final String fingerprint) {
387 AlertDialog.Builder builder = new AlertDialog.Builder(this);
388 builder.setTitle(R.string.delete_fingerprint);
389 builder.setMessage(R.string.sure_delete_fingerprint);
390 builder.setNegativeButton(R.string.cancel, null);
391 builder.setPositiveButton(R.string.delete,
392 new android.content.DialogInterface.OnClickListener() {
393
394 @Override
395 public void onClick(DialogInterface dialog, int which) {
396 if (contact.deleteOtrFingerprint(fingerprint)) {
397 populateView();
398 xmppConnectionService.syncRosterToDisk(contact
399 .getAccount());
400 }
401 }
402
403 });
404 builder.create().show();
405 }
406
407 @Override
408 public void onBackendConnected() {
409 if ((accountJid != null) && (contactJid != null)) {
410 Account account = xmppConnectionService
411 .findAccountByJid(accountJid);
412 if (account == null) {
413 return;
414 }
415 this.contact = account.getRoster().getContact(contactJid);
416 populateView();
417 }
418 }
419}