DialogContactDetails.java

  1package eu.siacs.conversations.ui;
  2
  3import eu.siacs.conversations.R;
  4import eu.siacs.conversations.entities.Contact;
  5import eu.siacs.conversations.entities.Presences;
  6import eu.siacs.conversations.utils.UIHelper;
  7import android.app.AlertDialog;
  8import android.app.Dialog;
  9import android.app.DialogFragment;
 10import android.content.DialogInterface;
 11import android.content.Intent;
 12import android.os.Bundle;
 13import android.provider.ContactsContract.CommonDataKinds;
 14import android.provider.ContactsContract.Contacts;
 15import android.provider.ContactsContract.Intents;
 16import android.view.LayoutInflater;
 17import android.view.View;
 18import android.view.View.OnClickListener;
 19import android.widget.CheckBox;
 20import android.widget.QuickContactBadge;
 21import android.widget.TextView;
 22
 23public class DialogContactDetails extends DialogFragment {
 24	
 25	private Contact contact = null;
 26	boolean displayingInRoster = false;
 27	
 28	private DialogContactDetails mDetailsDialog = this;
 29	private XmppActivity activity;
 30	
 31	private CheckBox send;
 32	private CheckBox receive;
 33	
 34	private DialogInterface.OnClickListener askRemoveFromRoster = new DialogInterface.OnClickListener() {
 35		
 36		@Override
 37		public void onClick(DialogInterface dialog, int which) {
 38			AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
 39			builder.setTitle("Delete from roster");
 40			builder.setMessage("Do you want to delete "+contact.getJid()+" from your roster. The conversation assoziated with this account will not be removed.");
 41			builder.setNegativeButton("Cancel", null);
 42			builder.setPositiveButton("Delete",removeFromRoster);
 43			builder.create().show();
 44		}
 45	};
 46	
 47	private DialogInterface.OnClickListener removeFromRoster = new DialogInterface.OnClickListener() {
 48		
 49		@Override
 50		public void onClick(DialogInterface dialog, int which) {
 51			activity.xmppConnectionService.deleteContact(contact);
 52			mDetailsDialog.dismiss();
 53		}
 54	};
 55	
 56	private DialogInterface.OnClickListener addToPhonebook = new DialogInterface.OnClickListener() {
 57		
 58		@Override
 59		public void onClick(DialogInterface dialog, int which) {
 60			Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
 61			intent.setType(Contacts.CONTENT_ITEM_TYPE);
 62			intent.putExtra(Intents.Insert.IM_HANDLE,contact.getJid());
 63			intent.putExtra(Intents.Insert.IM_PROTOCOL,CommonDataKinds.Im.PROTOCOL_JABBER);
 64			intent.putExtra("finishActivityOnSaveCompleted", true);
 65			getActivity().startActivityForResult(intent,0);
 66			mDetailsDialog.dismiss();
 67		}
 68	};
 69	
 70	private DialogInterface.OnClickListener updateSubscriptions = new DialogInterface.OnClickListener() {
 71		
 72		@Override
 73		public void onClick(DialogInterface dialog, int which) {
 74			boolean needsUpdating = false;
 75			if (contact.getSubscriptionOption(Contact.Subscription.FROM)) {
 76				if (!send.isChecked()) {
 77					contact.resetSubscriptionOption(Contact.Subscription.FROM);
 78					contact.resetSubscriptionOption(Contact.Subscription.PREEMPTIVE_GRANT);
 79					activity.xmppConnectionService.stopPresenceUpdatesTo(contact);
 80					needsUpdating=true;
 81				}
 82			} else {
 83				if (contact.getSubscriptionOption(Contact.Subscription.PREEMPTIVE_GRANT)) {
 84					if (!send.isChecked()) {
 85						contact.resetSubscriptionOption(Contact.Subscription.PREEMPTIVE_GRANT);
 86						needsUpdating=true;
 87					}
 88				} else {
 89					if (send.isChecked()) {
 90						contact.setSubscriptionOption(Contact.Subscription.PREEMPTIVE_GRANT);
 91						needsUpdating=true;
 92					}
 93				}
 94			}
 95			if (contact.getSubscriptionOption(Contact.Subscription.TO)) {
 96				if (!receive.isChecked()) {
 97					contact.resetSubscriptionOption(Contact.Subscription.TO);
 98					activity.xmppConnectionService.stopPresenceUpdatesFrom(contact);
 99					needsUpdating=true;
100				}
101			} else {
102				if (contact.getSubscriptionOption(Contact.Subscription.ASKING)) {
103					if (!receive.isChecked()) {
104						contact.resetSubscriptionOption(Contact.Subscription.ASKING);
105						activity.xmppConnectionService.stopPresenceUpdatesFrom(contact);
106						needsUpdating=true;
107					}
108				} else {
109					if (receive.isChecked()) {
110						contact.setSubscriptionOption(Contact.Subscription.ASKING);
111						activity.xmppConnectionService.requestPresenceUpdatesFrom(contact);
112						needsUpdating=true;
113					}
114				}
115			}
116			if (needsUpdating) {
117				activity.xmppConnectionService.updateContact(contact);
118			}
119		}
120	};
121
122	public void setContact(Contact contact) {
123		this.contact = contact;
124	}
125	
126	@Override
127	public Dialog onCreateDialog(Bundle savedInstanceState) {
128		this.activity = (XmppActivity) getActivity();
129		AlertDialog.Builder builder = new AlertDialog.Builder(this.activity);
130		LayoutInflater inflater = getActivity().getLayoutInflater();
131		View view = inflater.inflate(R.layout.dialog_contact_details, null);
132		TextView contactJid = (TextView) view.findViewById(R.id.details_contactjid);
133		TextView accountJid = (TextView) view.findViewById(R.id.details_account);
134		TextView status = (TextView) view.findViewById(R.id.details_contactstatus);
135		send = (CheckBox) view.findViewById(R.id.details_send_presence);
136		receive = (CheckBox) view.findViewById(R.id.details_receive_presence);
137		//ImageView contactPhoto = (ImageView) view.findViewById(R.id.details_contact_picture);
138		QuickContactBadge badge = (QuickContactBadge) view.findViewById(R.id.details_contact_badge);
139		
140		if (contact.getSubscriptionOption(Contact.Subscription.FROM)) {
141			send.setChecked(true);
142		} else {
143			send.setText("Preemptively grant subscription request");
144			if (contact.getSubscriptionOption(Contact.Subscription.PREEMPTIVE_GRANT)) {
145				send.setChecked(true);
146			} else {
147				send.setChecked(false);
148			}
149		}
150		if (contact.getSubscriptionOption(Contact.Subscription.TO)) {
151			receive.setChecked(true);
152		} else {
153			receive.setText("Request presence updates");
154			if (contact.getSubscriptionOption(Contact.Subscription.ASKING)) {
155				receive.setChecked(true);
156			} else {
157				receive.setChecked(false);
158			}
159		}
160		
161		switch (contact.getMostAvailableStatus()) {
162		case Presences.CHAT:
163			status.setText("free to chat");
164			status.setTextColor(0xFF83b600);
165			break;
166		case Presences.ONLINE:
167			status.setText("online");
168			status.setTextColor(0xFF83b600);
169			break;
170		case Presences.AWAY:
171			status.setText("away");
172			status.setTextColor(0xFFffa713);
173			break;
174		case Presences.XA:
175			status.setText("extended away");
176			status.setTextColor(0xFFffa713);
177			break;
178		case Presences.DND:
179			status.setText("do not disturb");
180			status.setTextColor(0xFFe92727);
181			break;
182		case Presences.OFFLINE:
183			status.setText("offline");
184			status.setTextColor(0xFFe92727);
185			break;
186		default:
187			status.setText("offline");
188			status.setTextColor(0xFFe92727);
189			break;
190		}
191		contactJid.setText(contact.getJid());
192		accountJid.setText(contact.getAccount().getJid());
193
194		UIHelper.prepareContactBadge(getActivity(), badge, contact);
195		
196		if (contact.getSystemAccount()==null) {
197			badge.setOnClickListener(new OnClickListener() {
198				
199				@Override
200				public void onClick(View v) {
201					AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
202					builder.setTitle("Add to phone book");
203					builder.setMessage("Do you want to add "+contact.getJid()+" to your phones contact list?");
204					builder.setNegativeButton("Cancel", null);
205					builder.setPositiveButton("Add",addToPhonebook);
206					builder.create().show();
207				}
208			});
209		}
210		
211		builder.setView(view);
212		builder.setTitle(contact.getDisplayName());
213		
214		builder.setNeutralButton("Done", this.updateSubscriptions);
215		builder.setPositiveButton("Remove from roster", this.askRemoveFromRoster);
216		return builder.create();
217	}
218}