src/com/beem/project/beem/BeemService.java
changeset 212 bbc0b169cdf0
parent 193 54e1e3f0bd4b
child 216 5d168a9b6b82
--- a/src/com/beem/project/beem/BeemService.java	Fri May 22 19:17:50 2009 +0200
+++ b/src/com/beem/project/beem/BeemService.java	Tue May 26 19:56:38 2009 +0200
@@ -10,6 +10,7 @@
 import org.jivesoftware.smack.packet.Presence;
 import org.jivesoftware.smack.proxy.ProxyInfo;
 import org.jivesoftware.smack.proxy.ProxyInfo.ProxyType;
+
 import android.app.Notification;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
@@ -27,8 +28,9 @@
 import com.beem.project.beem.ui.Subscription;
 
 /**
- * This class is for the Beem service. The connection to the xmpp server will be made asynchronously when the service
- * will start.
+ * This class is for the Beem service. The connection to the xmpp server will be made asynchronously
+ * when the service will start.
+ * 
  * @author darisk
  */
 public class BeemService extends Service {
@@ -36,20 +38,20 @@
     /**
      * The id to use for status notification.
      */
-    public static final int NOTIFICATION_STATUS_ID = 100;
+    public static final int         NOTIFICATION_STATUS_ID = 100;
 
-    private NotificationManager mNotificationManager;
-    private XmppConnectionAdapter mConnection;
-    private SharedPreferences mSettings;
-    private String mLogin;
-    private String mPassword;
-    private String mHost;
-    private String mService;
-    private int mPort;
+    private NotificationManager     mNotificationManager;
+    private XmppConnectionAdapter   mConnection;
+    private SharedPreferences       mSettings;
+    private String                  mLogin;
+    private String                  mPassword;
+    private String                  mHost;
+    private String                  mService;
+    private int                     mPort;
     private ConnectionConfiguration mConnectionConfiguration;
-    private ProxyInfo mProxyInfo;
-    private boolean mUseProxy;
-    private IXmppFacade.Stub mBind;
+    private ProxyInfo               mProxyInfo;
+    private boolean                 mUseProxy;
+    private IXmppFacade.Stub        mBind;
 
     /**
      * Constructor.
@@ -58,67 +60,11 @@
     }
 
     /**
-     * {@inheritDoc}
-     */
-    @Override
-    public IBinder onBind(Intent intent) {
-	return mBind;
-	// to forbid a client to bind
-	// return null;
-    }
-
-    /**
-     * {@inheritDoc}
+     * Close the connection to the xmpp server.
      */
-    @Override
-    public void onCreate() {
-
-	super.onCreate();
-	mSettings = getSharedPreferences(getString(R.string.PreferenceFileName), MODE_PRIVATE);
-	mLogin = mSettings.getString(getString(R.string.PreferenceLoginKey), "");
-	mPassword = mSettings.getString(getString(R.string.PreferencePasswordKey), "");
-	mHost = mSettings.getString(getString(R.string.PreferenceHostKey), "");
-	mPort = mSettings.getInt(getString(R.string.PreferencePortKey), 5222);
-	if (mHost.equals("talk.google.com"))
-	    mService = "gmail.com";
-	else
-	    mService = null;
-	initConnectionConfig();
-	mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
-	mConnection = new XmppConnectionAdapter(mConnectionConfiguration, mLogin, mPassword, this);
-	initRosterRequestListener();
-	mBind = new XmppFacade(mConnection, this);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void onStart(Intent intent, int startId) {
-	try {
-	    mConnection.connectAsync();
-	} catch (RemoteException e) {
-	    // TODO Auto-generated catch block
-	    e.printStackTrace();
-	}
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void onDestroy() {
-	closeConnection();
-	mNotificationManager.cancel(NOTIFICATION_STATUS_ID);
-    }
-
-    /**
-     * Show a notification.
-     * @param id the id of the notification.
-     * @param notif the notification to show
-     */
-    public void sendNotification(int id, Notification notif) {
-	mNotificationManager.notify(id, notif);
+    private void closeConnection() {
+	if (mConnection != null)
+	    mConnection.disconnect();
     }
 
     /**
@@ -126,14 +72,14 @@
      */
     private void initConnectionConfig() {
 	// TODO mettre a false par defaut et remplacer les valeurs par defaut
-	mUseProxy = mSettings.getBoolean(getString(R.string.PreferenceUseProxy), false);
+	mUseProxy = mSettings.getBoolean(getString(R.string.settings_key_proxy_use), false);
 	if (mUseProxy) {
 	    String stype = mSettings.getString(getString(R.string.PreferenceProxyType),
-		getString(R.string.PreferenceProxyTypeHttp));
-	    String phost = mSettings.getString(getString(R.string.PreferenceProxyHost), "");
-	    String puser = mSettings.getString(getString(R.string.PreferenceProxyUser), "");
-	    String ppass = mSettings.getString(getString(R.string.PreferenceProxyPassword), "");
-	    int pport = mSettings.getInt(getString(R.string.PreferenceProxyPort), 1080);
+		    getString(R.string.PreferenceProxyTypeHttp));
+	    String phost = mSettings.getString(getString(R.string.settings_key_proxy_server), "");
+	    String puser = mSettings.getString(getString(R.string.settings_key_proxy_username), "");
+	    String ppass = mSettings.getString(getString(R.string.settings_key_proxy_password), "");
+	    int pport = Integer.parseInt(mSettings.getString(getString(R.string.settings_key_proxy_port), "1080"));
 	    ProxyInfo.ProxyType type = ProxyType.valueOf(stype);
 	    mProxyInfo = new ProxyInfo(type, phost, pport, puser, ppass);
 	    if (mService != null)
@@ -154,10 +100,78 @@
 	Roster.setDefaultSubscriptionMode(SubscriptionMode.manual);
 	final XMPPConnection con = mConnection.getAdaptee();
 	try {
-	    // l'ajout d'un packet listener ne peut etre effectuer que lorsqu'on est connecte au serveur
+	    // l'ajout d'un packet listener ne peut etre effectuer que lorsqu'on est connecte au
+	    // serveur
 	    mConnection.addConnectionListener(new IBeemConnectionListener.Stub() {
 
 		@Override
+		public void connectionClosed() throws RemoteException {
+		    // TODO Auto-generated method stub
+
+		}
+
+		@Override
+		public void connectionClosedOnError() throws RemoteException {
+		    // TODO Auto-generated method stub
+
+		}
+
+		@Override
+		public void connectionFailed(String errorMsg) throws RemoteException {
+		    // TODO Auto-generated method stub
+
+		}
+
+		@Override
+		public void onConnect() throws RemoteException {
+
+		    PacketFilter filter = new PacketFilter() {
+
+			@Override
+			public boolean accept(Packet packet) {
+			    if (packet instanceof Presence) {
+				Presence pres = (Presence) packet;
+				if (pres.getType() == Presence.Type.subscribe)
+				    return true;
+			    }
+			    return false;
+			}
+		    };
+		    con.addPacketListener(new PacketListener() {
+
+			@Override
+			public void processPacket(Packet packet) {
+			    String from = packet.getFrom();
+			    Notification notif = new Notification(com.beem.project.beem.R.drawable.signal,
+				    "Demande d'ajout", System.currentTimeMillis());
+			    notif.defaults = Notification.DEFAULT_ALL;
+			    notif.flags = Notification.FLAG_AUTO_CANCEL;
+			    Intent intent = new Intent(BeemService.this, Subscription.class);
+			    intent.putExtra("from", from);
+			    notif
+				    .setLatestEventInfo(BeemService.this, from, "demande d'ajout de " + from,
+				            PendingIntent.getActivity(BeemService.this, 0, intent,
+				                    PendingIntent.FLAG_ONE_SHOT));
+			    int id = packet.hashCode();
+			    sendNotification(id, notif);
+			}
+		    }, filter);
+
+		}
+
+		@Override
+		public void reconnectingIn(int seconds) throws RemoteException {
+		    // TODO Auto-generated method stub
+
+		}
+
+		@Override
+		public void reconnectionFailed() throws RemoteException {
+		    // TODO Auto-generated method stub
+
+		}
+
+		@Override
 		public void reconnectionSuccessful() throws RemoteException {
 		    // TODO Auto-generated method stub
 		    PacketFilter filter = new PacketFilter() {
@@ -181,71 +195,6 @@
 			}
 		    }, filter);
 		}
-
-		@Override
-		public void reconnectionFailed() throws RemoteException {
-		    // TODO Auto-generated method stub
-
-		}
-
-		@Override
-		public void reconnectingIn(int seconds) throws RemoteException {
-		    // TODO Auto-generated method stub
-
-		}
-
-		@Override
-		public void onConnect() throws RemoteException {
-
-		    PacketFilter filter = new PacketFilter() {
-
-			@Override
-			public boolean accept(Packet packet) {
-			    if (packet instanceof Presence) {
-				Presence pres = (Presence) packet;
-				if (pres.getType() == Presence.Type.subscribe)
-				    return true;
-			    }
-			    return false;
-			}
-		    };
-		    con.addPacketListener(new PacketListener() {
-
-			@Override
-			public void processPacket(Packet packet) {
-			    String from = packet.getFrom();
-			    Notification notif = new Notification(com.beem.project.beem.R.drawable.signal,
-				"Demande d'ajout", System.currentTimeMillis());
-			    notif.defaults = Notification.DEFAULT_ALL;
-			    notif.flags = Notification.FLAG_AUTO_CANCEL;
-			    Intent intent = new Intent(BeemService.this, Subscription.class);
-			    intent.putExtra("from", from);
-			    notif.setLatestEventInfo(BeemService.this, from, "demande d'ajout de " + from,
-				PendingIntent.getActivity(BeemService.this, 0, intent, PendingIntent.FLAG_ONE_SHOT));
-			    int id = packet.hashCode();
-			    sendNotification(id, notif);
-			}
-		    }, filter);
-
-		}
-
-		@Override
-		public void connectionClosedOnError() throws RemoteException {
-		    // TODO Auto-generated method stub
-
-		}
-
-		@Override
-		public void connectionClosed() throws RemoteException {
-		    // TODO Auto-generated method stub
-
-		}
-
-		@Override
-		public void connectionFailed(String errorMsg) throws RemoteException {
-		    // TODO Auto-generated method stub
-
-		}
 	    });
 	} catch (RemoteException e) {
 	    // TODO Auto-generated catch block
@@ -254,11 +203,75 @@
     }
 
     /**
-     * Close the connection to the xmpp server.
+     * {@inheritDoc}
+     */
+    @Override
+    public IBinder onBind(Intent intent) {
+	return mBind;
+	// to forbid a client to bind
+	// return null;
+    }
+
+    /**
+     * {@inheritDoc}
      */
-    private void closeConnection() {
-	if (mConnection != null)
-	    mConnection.disconnect();
+    @Override
+    public void onCreate() {
+
+	super.onCreate();
+	mSettings = getSharedPreferences(getString(R.string.settings_filename), MODE_PRIVATE);
+	mLogin = mSettings.getString(getString(R.string.settings_key_account_username), "");
+	mPassword = mSettings.getString(getString(R.string.settings_key_account_password), "");
+	mHost = mSettings.getString(getString(R.string.settings_key_xmpp_server), "");
+	mPort = Integer.parseInt(mSettings.getString(getString(R.string.settings_key_xmpp_port), "5222"));
+
+	Log.i("BEEMSERVICE", mLogin);
+	Log.i("BEEMSERVICE", mPassword);
+	Log.i("BEEMSERVICE", mHost);
+	Log.i("BEEMSERVICE", "" + mPort + "");
+	if (mHost.equals("talk.google.com"))
+	    mService = "gmail.com";
+	else
+	    mService = null;
+	initConnectionConfig();
+	mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
+	mConnection = new XmppConnectionAdapter(mConnectionConfiguration, mLogin, mPassword, this);
+	initRosterRequestListener();
+	mBind = new XmppFacade(mConnection, this);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void onDestroy() {
+	closeConnection();
+	mNotificationManager.cancel(NOTIFICATION_STATUS_ID);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void onStart(Intent intent, int startId) {
+	try {
+	    mConnection.connectAsync();
+	} catch (RemoteException e) {
+	    // TODO Auto-generated catch block
+	    e.printStackTrace();
+	}
+    }
+
+    /**
+     * Show a notification.
+     * 
+     * @param id
+     *            the id of the notification.
+     * @param notif
+     *            the notification to show
+     */
+    public void sendNotification(int id, Notification notif) {
+	mNotificationManager.notify(id, notif);
     }
 
 }