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