DialogContactDetails.java

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