Utilisation d'une application qui gere les informations globales.
authorDa Risk <darisk972@gmail.com>
Tue, 31 Mar 2009 21:29:48 +0200
changeset 45 ae035477b6aa
parent 44 ff7b74becc50
child 46 d5a36d820645
Utilisation d'une application qui gere les informations globales. Quelques trucs a revoir quand meme.
src/com/beem/project/beem/BeemApplication.java
src/com/beem/project/beem/ui/Beem.java
src/com/beem/project/beem/ui/ContactList.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/com/beem/project/beem/BeemApplication.java	Tue Mar 31 21:29:48 2009 +0200
@@ -0,0 +1,98 @@
+/**
+ * 
+ */
+package com.beem.project.beem;
+
+import com.beem.project.beem.service.aidl.IXMPPFacade;
+
+import android.app.Activity;
+import android.app.Application;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.ServiceConnection;
+import android.content.res.Configuration;
+import android.content.res.Resources;
+import android.os.IBinder;
+
+/**
+ * @author darisk
+ */
+public class BeemApplication extends Application {
+
+    private IXMPPFacade mFacade;
+  
+    private Context mApplicationContext;
+    private Resources mPrivateResources;
+    private static BeemApplication mBeemApp;
+    
+    public static BeemApplication getApplication(Activity activity) {
+	if (mBeemApp == null) {
+	    mBeemApp = new BeemApplication();
+	    mBeemApp.mApplicationContext = activity.getApplication();
+	    mBeemApp.mPrivateResources = activity.getResources();
+	    mBeemApp.onCreate();
+	}
+	return mBeemApp;
+    }
+    
+    @Override
+    public void onConfigurationChanged(Configuration newConfig) {
+	// TODO Auto-generated method stub
+	super.onConfigurationChanged(newConfig);
+    }
+
+    @Override
+    public void onCreate() {
+	// TODO Auto-generated method stub
+	super.onCreate();
+	mFacade = null;
+    }
+
+    @Override
+    public void onLowMemory() {
+	// TODO Auto-generated method stub
+	super.onLowMemory();
+    }
+
+    @Override
+    public void onTerminate() {
+	// TODO Auto-generated method stub
+	super.onTerminate();
+    }
+
+    public synchronized void startBeemService() {
+	if (mFacade == null) {
+	    // Intent intent = new Intent(this, BeemService.class);
+	    Intent intent = new Intent();
+	    intent.setComponent(new ComponentName("com.beem.project.beem", "com.beem.project.beem.BeemService"));
+	    mApplicationContext.startService(intent);
+	    mApplicationContext.bindService(intent, mServConn, BIND_AUTO_CREATE);
+	}
+    }
+
+    private ServiceConnection mServConn = new ServiceConnection() {
+
+	@Override
+	public void onServiceDisconnected(ComponentName name) {
+	    mFacade = null;
+
+	}
+
+	@Override
+	public void onServiceConnected(ComponentName name, IBinder service) {
+	    mFacade = IXMPPFacade.Stub.asInterface(service);
+	}
+    };
+
+    public synchronized void stopBeemService() {
+	Intent intent = new Intent(this, BeemService.class);
+	mApplicationContext.unbindService(mServConn);
+	mApplicationContext.stopService(intent);
+    }
+
+    public IXMPPFacade getXmppFacade() {
+	return mFacade;
+    }
+
+}
--- a/src/com/beem/project/beem/ui/Beem.java	Sat Mar 28 03:36:14 2009 -0400
+++ b/src/com/beem/project/beem/ui/Beem.java	Tue Mar 31 21:29:48 2009 +0200
@@ -12,6 +12,7 @@
 import android.view.View.OnClickListener;
 import android.widget.Button;
 
+import com.beem.project.beem.BeemApplication;
 import com.beem.project.beem.R;
 
 /**
@@ -23,6 +24,7 @@
     private BeemDialogSettings mDialog;
     private Button mButton;
     private Handler mHandler;
+    private BeemApplication mBeemApplication;
 
     /**
      * Default constructor.
@@ -40,6 +42,7 @@
     @Override
     public final void onCreate(Bundle savedInstanceState) {
 	super.onCreate(savedInstanceState);
+	mBeemApplication = BeemApplication.getApplication(this);
 	setContentView(R.layout.beem);
 	mSettings = getSharedPreferences(
 		getString(R.string.PreferenceFileName), MODE_PRIVATE);
@@ -52,7 +55,7 @@
 		if (mButton.getText() == getString(R.string.BeemCreateAccount))
 		    mDialog.show();
 		else
-		    startActivity(new Intent(Beem.this, ContactList.class));
+		    startActivity(new Intent(Beem.this, ContactList.class));	    
 	    }
 	});
 	showJID();
--- a/src/com/beem/project/beem/ui/ContactList.java	Sat Mar 28 03:36:14 2009 -0400
+++ b/src/com/beem/project/beem/ui/ContactList.java	Tue Mar 31 21:29:48 2009 +0200
@@ -16,6 +16,7 @@
 import android.widget.ExpandableListAdapter;
 import android.widget.SimpleExpandableListAdapter;
 
+import com.beem.project.beem.BeemApplication;
 import com.beem.project.beem.BeemService;
 import com.beem.project.beem.R;
 import com.beem.project.beem.service.Contact;
@@ -30,11 +31,17 @@
     
     private IXMPPFacade mService = null;
 
+    private BeemApplication mBeemApplication;
+    
     @Override
     public void onCreate(Bundle saveBundle) {
         super.onCreate(saveBundle);
-        bindService(new Intent(this, BeemService.class), mConnection,
-                BIND_AUTO_CREATE | BIND_DEBUG_UNBIND);
+        mBeemApplication = BeemApplication.getApplication(this);
+        mBeemApplication.startBeemService();
+        mService = mBeemApplication.getXmppFacade();
+        /* bindService(new Intent(this, BeemService.class), mConnection,
+                BIND_AUTO_CREATE | BIND_DEBUG_UNBIND); */
+        showContactList();
     }
 
     private void showContactList() {