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