Send an unsubscribed presence when refusing subscription
authorDa Risk <darisk972@gmail.com>
Sun, 02 Jan 2011 19:25:34 +0100
changeset 848 0d604b75d5ae
parent 847 176d2f9d2ebf
child 849 5fed871bb54f
Send an unsubscribed presence when refusing subscription fix #324
src/com/beem/project/beem/service/Contact.java
src/com/beem/project/beem/service/XmppConnectionAdapter.java
src/com/beem/project/beem/ui/Subscription.java
--- a/src/com/beem/project/beem/service/Contact.java	Sun Jan 02 17:53:38 2011 +0100
+++ b/src/com/beem/project/beem/service/Contact.java	Sun Jan 02 19:25:34 2011 +0100
@@ -105,6 +105,28 @@
     }
 
     /**
+     * Make an xmpp uri for a spcific jid.
+     *
+     * @param jid the jid to represent as an uri
+     * @return an uri representing this jid.
+     */
+    public static Uri makeXmppUri(String jid) {
+	StringBuilder build = new StringBuilder("xmpp:");
+	String name = StringUtils.parseName(jid);
+	build.append(name);
+	if (!"".equals(name))
+	    build.append('@');
+	build.append(StringUtils.parseServer(jid));
+	String resource = StringUtils.parseResource(jid);
+	if (!"".equals(resource)) {
+	    build.append('/');
+	    build.append(resource);
+	}
+	Uri u = Uri.parse(build.toString());
+	return u;
+    }
+
+    /**
      * Constructor.
      * @param jid JID of the contact
      */
@@ -387,14 +409,7 @@
      * @return the URI
      */
     public Uri toUri() {
-	StringBuilder build = new StringBuilder("xmpp:");
-	String name = StringUtils.parseName(mJID);
-	build.append(name);
-	if (!"".equals(name))
-	    build.append('@');
-	build.append(StringUtils.parseServer(mJID));
-	Uri u = Uri.parse(build.toString());
-	return u;
+	return makeXmppUri(mJID);
     }
 
     /**
--- a/src/com/beem/project/beem/service/XmppConnectionAdapter.java	Sun Jan 02 17:53:38 2011 +0100
+++ b/src/com/beem/project/beem/service/XmppConnectionAdapter.java	Sun Jan 02 19:25:34 2011 +0100
@@ -694,7 +694,7 @@
 		R.string.AcceptContactRequest, from), System.currentTimeMillis());
 	    notification.flags = Notification.FLAG_AUTO_CANCEL;
 	    Intent intent = new Intent(mService, Subscription.class);
-	    intent.putExtra("from", from);
+	    intent.setData(Contact.makeXmppUri(from));
 	    notification.setLatestEventInfo(mService, from, mService
 		.getString(R.string.AcceptContactRequestFrom, from), PendingIntent.getActivity(mService, 0,
 		    intent, PendingIntent.FLAG_ONE_SHOT));
--- a/src/com/beem/project/beem/ui/Subscription.java	Sun Jan 02 17:53:38 2011 +0100
+++ b/src/com/beem/project/beem/ui/Subscription.java	Sun Jan 02 19:25:34 2011 +0100
@@ -54,6 +54,7 @@
 import android.os.Bundle;
 import android.os.IBinder;
 import android.os.RemoteException;
+import android.util.Log;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.TextView;
@@ -63,6 +64,7 @@
 import com.beem.project.beem.R;
 import com.beem.project.beem.service.PresenceAdapter;
 import com.beem.project.beem.service.aidl.IXmppFacade;
+import com.beem.project.beem.service.Contact;
 import com.beem.project.beem.utils.BeemBroadcastReceiver;
 
 /**
@@ -72,6 +74,7 @@
 public class Subscription extends Activity {
 
     private static final Intent SERVICE_INTENT = new Intent();
+    private static final String TAG = Subscription.class.getSimpleName();
     private IXmppFacade mService;
     private String mContact;
     private ServiceConnection mServConn = new BeemServiceConnection();
@@ -97,7 +100,8 @@
 	setContentView(R.layout.subscription);
 	findViewById(R.id.SubscriptionAccept).setOnClickListener(mClickListener);
 	findViewById(R.id.SubscriptionRefuse).setOnClickListener(mClickListener);
-	mContact = getIntent().getStringExtra("from");
+	Contact c = new Contact(getIntent().getData());
+	mContact = c.getJID();
 	TextView tv = (TextView) findViewById(R.id.SubscriptionText);
 	String str = String.format(getString(R.string.SubscriptText), mContact);
 	tv.setText(str);
@@ -131,6 +135,15 @@
 	this.unregisterReceiver(mReceiver);
     }
 
+    private void sendPresence(Presence p) {
+	PresenceAdapter preAdapt = new PresenceAdapter(p);
+	try {
+	    mService.sendPresencePacket(preAdapt);
+	} catch (RemoteException e) {
+	    Log.e(TAG, "Error while sending subscription response", e);
+	}
+    }
+
     /**
      * Event simple click on buttons.
      */
@@ -144,29 +157,25 @@
 
 	@Override
 	public void onClick(View v) {
+	    Presence presence = null;
 	    switch (v.getId()) {
 		case R.id.SubscriptionAccept:
-		    Presence presence = new Presence(Type.subscribed);
-		    presence.setTo(mContact);
-		    PresenceAdapter preAdapt = new PresenceAdapter(presence);
-		    try {
-			mService.sendPresencePacket(preAdapt);
-			Toast.makeText(Subscription.this, getString(R.string.SubscriptAccept), Toast.LENGTH_SHORT)
+		    presence = new Presence(Type.subscribed);
+		    Toast.makeText(Subscription.this, getString(R.string.SubscriptAccept), Toast.LENGTH_SHORT)
 			    .show();
-			finish();
-		    } catch (RemoteException e) {
-			Toast.makeText(Subscription.this, getString(R.string.SubscriptError), Toast.LENGTH_SHORT)
-			    .show();
-			e.printStackTrace();
-		    }
 		    break;
 		case R.id.SubscriptionRefuse:
+		    presence = new Presence(Type.unsubscribed);
 		    Toast.makeText(Subscription.this, getString(R.string.SubscriptRefused), Toast.LENGTH_SHORT).show();
-		    finish();
 		    break;
 		default:
 		    Toast.makeText(Subscription.this, getString(R.string.SubscriptError), Toast.LENGTH_SHORT).show();
 	    }
+	    if (presence != null) {
+		presence.setTo(mContact);
+		sendPresence(presence);
+	    }
+	    finish();
 	}
     };