src/com/beem/project/beem/BeemService.java
changeset 774 1b88edb34d96
parent 773 489fde4ab7d4
parent 762 56d44e5c7081
child 793 4fb9df09ffdf
--- a/src/com/beem/project/beem/BeemService.java	Mon Jun 14 22:45:56 2010 +0200
+++ b/src/com/beem/project/beem/BeemService.java	Mon Jun 14 22:47:22 2010 +0200
@@ -66,6 +66,7 @@
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
 import android.net.ConnectivityManager;
+import android.net.Uri;
 import android.os.IBinder;
 import android.os.RemoteException;
 import android.preference.PreferenceManager;
@@ -79,7 +80,9 @@
 import com.beem.project.beem.utils.Status;
 
 /**
- * This class is for the Beem service. The connection to the xmpp server will be made asynchronously when the service
+ * This class is for the Beem service.
+ * It must contains every global informations needed to maintain the background service.
+ * The connection to the xmpp server will be made asynchronously when the service
  * will start.
  * @author darisk
  */
@@ -121,13 +124,13 @@
      * Initialize the connection.
      */
     private void initConnectionConfig() {
-	mUseProxy = mSettings.getBoolean("settings_key_proxy_use", false);
+	mUseProxy = mSettings.getBoolean(BeemApplication.PROXY_USE_KEY, false);
 	if (mUseProxy) {
-	    String stype = mSettings.getString("settings_key_proxy_type", "HTTP");
-	    String phost = mSettings.getString("settings_key_proxy_server", "");
-	    String puser = mSettings.getString("settings_key_proxy_username", "");
-	    String ppass = mSettings.getString("settings_key_proxy_password", "");
-	    int pport = Integer.parseInt(mSettings.getString("settings_key_proxy_port", "1080"));
+	    String stype = mSettings.getString(BeemApplication.PROXY_TYPE_KEY, "HTTP");
+	    String phost = mSettings.getString(BeemApplication.PROXY_SERVER_KEY, "");
+	    String puser = mSettings.getString(BeemApplication.PROXY_USERNAME_KEY, "");
+	    String ppass = mSettings.getString(BeemApplication.PROXY_PASSWORD_KEY, "");
+	    int pport = Integer.parseInt(mSettings.getString(BeemApplication.PROXY_PORT_KEY, "1080"));
 	    ProxyInfo.ProxyType type = ProxyType.valueOf(stype);
 	    mProxyInfo = new ProxyInfo(type, phost, pport, puser, ppass);
 	} else {
@@ -158,21 +161,16 @@
 	return mBind;
     }
 
-    /* (non-Javadoc)
-     * @see android.app.Service#onUnbind(android.content.Intent)
-     */
     @Override
     public boolean onUnbind(Intent intent) {
 	Log.d(TAG, "ONUNBIND()");
 	if (!mConnection.getAdaptee().isConnected()) {
-	    Log.d(TAG, "DESTROYED");
 	    this.stopSelf();
 	}
 	return true;
     }
 
 
-
     /**
      * {@inheritDoc}
      */
@@ -222,7 +220,6 @@
     @Override
     public void onDestroy() {
 	super.onDestroy();
-	Log.d("Service", "onDestroy");
 	resetStatus();
 	mNotificationManager.cancelAll();
 	unregisterReceiver(mReceiver);
@@ -249,11 +246,16 @@
     }
 
     /**
-     * Show a notification.
+     * Show a notification using the preference of the user.
      * @param id the id of the notification.
      * @param notif the notification to show
      */
     public void sendNotification(int id, Notification notif) {
+	if (mSettings.getBoolean(BeemApplication.NOTIFICATION_VIBRATE_KEY, true))
+	    notif.defaults |= Notification.DEFAULT_VIBRATE;
+	notif.defaults |= Notification.DEFAULT_LIGHTS;
+	String ringtoneStr = mSettings.getString(BeemApplication.NOTIFICATION_SOUND_KEY, "");
+	notif.sound = Uri.parse(ringtoneStr);
 	mNotificationManager.notify(id, notif);
     }
 
@@ -298,6 +300,15 @@
     }
 
     /**
+     * Get the notification manager system service.
+     *
+     * @return the notification manager service.
+     */
+    public NotificationManager getNotificationManager() {
+	return mNotificationManager;
+    }
+
+    /**
      * A sort of patch from this thread: http://www.igniterealtime.org/community/thread/31118. Avoid ClassCastException
      * by bypassing the classloading shit of Smack.
      * @param pm The ProviderManager.
@@ -399,9 +410,6 @@
 	public BeemServicePreferenceListener() {
 	}
 
-	/**
-	 * {@inheritDoc}
-	 */
 	@Override
 	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
 	    if ("settings_away_chk".equals(key)) {
@@ -426,24 +434,23 @@
 	private int mOldMode;
 
 	/**
-	 * ctor.
+	 * Constructor.
 	 */
 	public BeemServiceBroadcastReceiver() {
 	}
 
-	/**
-	 * {@inheritDoc}
-	 */
 	@Override
 	public void onReceive(final Context context, final Intent intent) {
 	    String intentAction = intent.getAction();
 	    if (intentAction.equals(Intent.ACTION_SCREEN_OFF)) {
 		mOldMode = mConnection.getPreviousMode();
 		mOldStatus = mConnection.getPreviousStatus();
-		mConnection.changeStatus(Status.CONTACT_STATUS_AWAY,
-		    mSettings.getString("settings_away_message", "Away"));
+		if (mConnection.isAuthentificated())
+		    mConnection.changeStatus(Status.CONTACT_STATUS_AWAY,
+			    mSettings.getString("settings_away_message", "Away"));
 	    } else if (intentAction.equals(Intent.ACTION_SCREEN_ON)) {
-		mConnection.changeStatus(mOldMode, mOldStatus);
+		if (mConnection.isAuthentificated())
+		    mConnection.changeStatus(mOldMode, mOldStatus);
 	    }
 	}
     }