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 contactJid.setText(contact.getJid());
231 accountJid.setText(contact.getAccount().getJid());
232
233 UIHelper.prepareContactBadge(this, badge, contact, getApplicationContext());
234
235 if (contact.getSystemAccount() == null) {
236 badge.setOnClickListener(onBadgeClick);
237 }
238
239 keys.removeAllViews();
240 LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
241 for (Iterator<String> iterator = contact.getOtrFingerprints()
242 .iterator(); iterator.hasNext();) {
243 String otrFingerprint = iterator.next();
244 View view = (View) inflater.inflate(R.layout.contact_key, null);
245 TextView key = (TextView) view.findViewById(R.id.key);
246 TextView keyType = (TextView) view.findViewById(R.id.key_type);
247 keyType.setText("OTR Fingerprint");
248 key.setText(otrFingerprint);
249 keys.addView(view);
250 }
251 Log.d("gultsch", "pgp key id " + contact.getPgpKeyId());
252 if (contact.getPgpKeyId() != 0) {
253 View view = (View) inflater.inflate(R.layout.contact_key, null);
254 TextView key = (TextView) view.findViewById(R.id.key);
255 TextView keyType = (TextView) view.findViewById(R.id.key_type);
256 keyType.setText("PGP Key ID");
257 BigInteger bi = new BigInteger("" + contact.getPgpKeyId());
258 StringBuilder builder = new StringBuilder(bi.toString(16)
259 .toUpperCase(Locale.US));
260 builder.insert(8, " ");
261 key.setText(builder.toString());
262 keys.addView(view);
263 }
264 }
265
266 @Override
267 public void onBackendConnected() {
268 if (uuid != null) {
269 this.contact = xmppConnectionService.findContact(uuid);
270 if (this.contact != null) {
271 populateView();
272 }
273 }
274 }
275
276 @Override
277 protected void onStop() {
278 super.onStop();
279 boolean needsUpdating = false;
280 if (contact.getSubscriptionOption(Contact.Subscription.FROM)) {
281 if (!send.isChecked()) {
282 contact.resetSubscriptionOption(Contact.Subscription.FROM);
283 contact.resetSubscriptionOption(Contact.Subscription.PREEMPTIVE_GRANT);
284 activity.xmppConnectionService.stopPresenceUpdatesTo(contact);
285 needsUpdating = true;
286 }
287 } else {
288 if (contact
289 .getSubscriptionOption(Contact.Subscription.PREEMPTIVE_GRANT)) {
290 if (!send.isChecked()) {
291 contact.resetSubscriptionOption(Contact.Subscription.PREEMPTIVE_GRANT);
292 needsUpdating = true;
293 }
294 } else {
295 if (send.isChecked()) {
296 contact.setSubscriptionOption(Contact.Subscription.PREEMPTIVE_GRANT);
297 needsUpdating = true;
298 }
299 }
300 }
301 if (contact.getSubscriptionOption(Contact.Subscription.TO)) {
302 if (!receive.isChecked()) {
303 contact.resetSubscriptionOption(Contact.Subscription.TO);
304 activity.xmppConnectionService.stopPresenceUpdatesFrom(contact);
305 needsUpdating = true;
306 }
307 } else {
308 if (contact.getSubscriptionOption(Contact.Subscription.ASKING)) {
309 if (!receive.isChecked()) {
310 contact.resetSubscriptionOption(Contact.Subscription.ASKING);
311 activity.xmppConnectionService
312 .stopPresenceUpdatesFrom(contact);
313 needsUpdating = true;
314 }
315 } else {
316 if (receive.isChecked()) {
317 contact.setSubscriptionOption(Contact.Subscription.ASKING);
318 activity.xmppConnectionService
319 .requestPresenceUpdatesFrom(contact);
320 needsUpdating = true;
321 }
322 }
323 }
324 if (needsUpdating) {
325 Toast.makeText(getApplicationContext(), "Subscription updated", Toast.LENGTH_SHORT).show();
326 activity.xmppConnectionService.updateContact(contact);
327 }
328 }
329
330}