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