Multi Account on the beem service OK but doesnt connect
author"Vincent Veronis"
Sun, 20 Mar 2011 00:22:37 +0100
changeset 873 cdf001e43dce
parent 872 11a7b9c1c500
child 874 3fdcebc8db6e
Multi Account on the beem service OK but doesnt connect
AndroidManifest.xml
src/com/beem/project/beem/BeemService.java
src/com/beem/project/beem/account/Authenticator.java
src/com/beem/project/beem/service/XmppConnectionAdapter.java
src/com/beem/project/beem/ui/wizard/AccountConfigure.java
--- a/AndroidManifest.xml	Sat Mar 19 20:04:33 2011 +0100
+++ b/AndroidManifest.xml	Sun Mar 20 00:22:37 2011 +0100
@@ -105,6 +105,7 @@
 	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 	<uses-permission android:name="com.beem.project.beem.BEEM_SERVICE" />
 	<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
+	<uses-permission android:name="android.permission.GET_ACCOUNTS" />
 	<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" />
 	<supports-screens android:largeScreens="true"
 		android:normalScreens="true" android:smallScreens="true"
--- a/src/com/beem/project/beem/BeemService.java	Sat Mar 19 20:04:33 2011 +0100
+++ b/src/com/beem/project/beem/BeemService.java	Sun Mar 20 00:22:37 2011 +0100
@@ -44,17 +44,13 @@
 package com.beem.project.beem;
 
 import java.util.ArrayList;
-import java.util.List;
+import java.util.HashMap;
+import java.util.Map;
 
-import org.jivesoftware.smack.ConnectionConfiguration;
 import org.jivesoftware.smack.Roster;
 import org.jivesoftware.smack.XMPPConnection;
-import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
 import org.jivesoftware.smack.Roster.SubscriptionMode;
 import org.jivesoftware.smack.provider.ProviderManager;
-import org.jivesoftware.smack.proxy.ProxyInfo;
-import org.jivesoftware.smack.proxy.ProxyInfo.ProxyType;
-import org.jivesoftware.smack.util.StringUtils;
 import org.jivesoftware.smackx.packet.ChatStateExtension;
 import org.jivesoftware.smackx.provider.DelayInfoProvider;
 import org.jivesoftware.smackx.provider.DiscoverInfoProvider;
@@ -64,6 +60,8 @@
 import org.jivesoftware.smackx.pubsub.provider.ItemsProvider;
 import org.jivesoftware.smackx.pubsub.provider.PubSubProvider;
 
+import android.accounts.Account;
+import android.accounts.AccountManager;
 import android.app.Notification;
 import android.app.NotificationManager;
 import android.app.Service;
@@ -77,7 +75,6 @@
 import android.net.Uri;
 import android.os.IBinder;
 import android.os.RemoteException;
-import android.preference.PreferenceManager;
 import android.util.Log;
 
 import com.beem.project.beem.service.XmppConnectionAdapter;
@@ -101,27 +98,20 @@
     public static final int NOTIFICATION_STATUS_ID = 100;
 
     private static final String TAG = "BeemService";
-    private static final int DEFAULT_XMPP_PORT = 5222;
-    //private static final String COMMAND_NAMESPACE = "http://jabber.org/protocol/commands";
 
     private NotificationManager mNotificationManager;
-    private List<XmppConnectionAdapter> mConnection = new ArrayList<XmppConnectionAdapter>();
-    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 ArrayList<XmppFacade> mBind = new ArrayList<XmppFacade>();
+    //TODO: MAP instead of LIST
+    private Map<String, XmppConnectionAdapter> mConnection = new HashMap<String, XmppConnectionAdapter>();
+    private Map<String, BeemConnection> mBeemConnection = new HashMap<String, BeemConnection>();
+
+    //TODO: MAp maybe instead of LIST
+    private Map<String, XmppFacade> mBind = new HashMap<String, XmppFacade>();
+
+    private boolean mOnOffReceiverIsRegistered;
 
     private BeemBroadcastReceiver mReceiver = new BeemBroadcastReceiver();
+    private BeemServicePreferenceListener mPreferenceListener = new BeemServicePreferenceListener();
     private BeemServiceBroadcastReceiver mOnOffReceiver = new BeemServiceBroadcastReceiver();
-    private BeemServicePreferenceListener mPreferenceListener = new BeemServicePreferenceListener();
-
-    private boolean mOnOffReceiverIsRegistered;
 
     /**
      * Constructor.
@@ -130,39 +120,6 @@
     }
 
     /**
-     * Initialize the connection.
-     */
-    private void initConnectionConfig() {
-	mUseProxy = mSettings.getBoolean(BeemApplication.PROXY_USE_KEY, false);
-	if (mUseProxy) {
-	    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 {
-	    mProxyInfo = ProxyInfo.forNoProxy();
-	}
-	if (mSettings.getBoolean("settings_key_specific_server", false))
-	    mConnectionConfiguration = new ConnectionConfiguration(mHost, mPort, mService, mProxyInfo);
-	else
-	    mConnectionConfiguration = new ConnectionConfiguration(mService, mProxyInfo);
-
-	if (mSettings.getBoolean("settings_key_xmpp_tls_use", false)
-	    || mSettings.getBoolean("settings_key_gmail", false)) {
-	    mConnectionConfiguration.setSecurityMode(SecurityMode.required);
-	}
-	if (mSettings.getBoolean(BeemApplication.SMACK_DEBUG_KEY, false))
-	    mConnectionConfiguration.setDebuggerEnabled(true);
-	mConnectionConfiguration.setSendPresence(true);
-	// maybe not the universal path, but it works on most devices (Samsung Galaxy, Google Nexus One)
-	mConnectionConfiguration.setTruststoreType("BKS");
-	mConnectionConfiguration.setTruststorePath("/system/etc/security/cacerts.bks");
-    }
-
-    /**
      * {@inheritDoc}
      */
     @Override
@@ -176,7 +133,7 @@
     public boolean onUnbind(Intent intent) {
 	Log.d(TAG, "ONUNBIND()");
 	boolean isConnected = true;
-	for (XmppConnectionAdapter connection : mConnection) {
+	for (XmppConnectionAdapter connection : mConnection.values()) {
 	    if (!connection.getAdaptee().isConnected())
 		isConnected = false;
 	}
@@ -192,44 +149,29 @@
     @Override
     public void onCreate() {
 	super.onCreate();
+	AccountManager am = AccountManager.get(BeemService.this);
+	Account allAccount[] = am.getAccountsByType("com.beem.project.com");
+	for (Account account : allAccount) {
+	    BeemConnection beemco = new BeemConnection(BeemService.this
+		.getSharedPreferences(account.name, MODE_PRIVATE), mPreferenceListener);
+	    if (beemco.getSettings().getBoolean("settings_away_chk", false)) {
+		mOnOffReceiverIsRegistered = true;
+		registerReceiver(mOnOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
+		registerReceiver(mOnOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_ON));
+	    }
+	    mBeemConnection.put(account.name, beemco);
+	    XmppConnectionAdapter beemcoAdapter = new XmppConnectionAdapter(beemco.getConnectionConfiguration(), beemco
+		.getJid(), beemco.getPassword(), this);
+	    mConnection.put(account.name, beemcoAdapter);
+	    mBind.put(account.name, new XmppFacade(beemcoAdapter));
+	    Log.e(TAG, "Account configuration : " + account.toString() + " DONE");
+	}
 	registerReceiver(mReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
-	mSettings = PreferenceManager.getDefaultSharedPreferences(this);
-	mSettings.registerOnSharedPreferenceChangeListener(mPreferenceListener);
-	if (mSettings.getBoolean("settings_away_chk", false)) {
-	    mOnOffReceiverIsRegistered = true;
-	    registerReceiver(mOnOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
-	    registerReceiver(mOnOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_ON));
-	}
-	String tmpJid = mSettings.getString(BeemApplication.ACCOUNT_USERNAME_KEY, "");
-	mLogin = StringUtils.parseName(tmpJid);
-	mPassword = mSettings.getString(BeemApplication.ACCOUNT_PASSWORD_KEY, "");
-	mPort = DEFAULT_XMPP_PORT;
-	mService = StringUtils.parseServer(tmpJid);
-	mHost = mService;
 
-	if (mSettings.getBoolean("settings_key_specific_server", false)) {
-	    mHost = mSettings.getString("settings_key_xmpp_server", "");
-	    if ("".equals(mHost))
-		mHost = mService;
-	    String tmpPort = mSettings.getString("settings_key_xmpp_port", "5222");
-	    mPort = ("".equals(tmpPort)) ? DEFAULT_XMPP_PORT : Integer.parseInt(tmpPort);
-	}
-	if (mSettings.getBoolean(BeemApplication.FULL_JID_LOGIN_KEY, false) || "gmail.com".equals(mService)
-	    || "googlemail.com".equals(mService)) {
-	    mLogin = tmpJid;
-	}
-
-	initConnectionConfig();
 	configure(ProviderManager.getInstance());
-
 	mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
-	//TODO: Faire la liste des accounts et ajouter leur connection
-	mConnection.add(new XmppConnectionAdapter(mConnectionConfiguration, mLogin, mPassword, this));
-
 	Roster.setDefaultSubscriptionMode(SubscriptionMode.manual);
 
-	for (XmppConnectionAdapter connection : mConnection)
-	    mBind.add(new XmppFacade(connection));
 	Log.d(TAG, "ONCREATE");
     }
 
@@ -241,10 +183,10 @@
 	super.onDestroy();
 	mNotificationManager.cancelAll();
 	unregisterReceiver(mReceiver);
-	mSettings.unregisterOnSharedPreferenceChangeListener(mPreferenceListener);
+
 	if (mOnOffReceiverIsRegistered)
 	    unregisterReceiver(mOnOffReceiver);
-	for (XmppConnectionAdapter connection : mConnection) {
+	for (XmppConnectionAdapter connection : mConnection.values()) {
 	    if (connection.isAuthentificated() && BeemConnectivity.isConnected(this))
 		connection.disconnect();
 	}
@@ -259,7 +201,7 @@
 	super.onStart(intent, startId);
 	Log.d(TAG, "onStart");
 	try {
-	    for (XmppConnectionAdapter connection : mConnection)
+	    for (XmppConnectionAdapter connection : mConnection.values())
 		connection.connectAsync();
 	} catch (RemoteException e) {
 	    e.printStackTrace();
@@ -272,12 +214,12 @@
      * @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);
+//	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);
     }
 
     /**
@@ -292,9 +234,12 @@
      * Reset the status to online after a disconnect.
      */
     public void resetStatus() {
-	Editor edit = mSettings.edit();
-	edit.putInt(BeemApplication.STATUS_KEY, 1);
-	edit.commit();
+	for (BeemConnection beemco : mBeemConnection.values()) {
+	    Editor edit = beemco.getSettings().edit();
+	    edit.putInt(BeemApplication.STATUS_KEY, 1);
+	    edit.commit();
+	}
+
     }
 
     /**
@@ -317,8 +262,8 @@
      * Get the preference of the service.
      * @return the preference
      */
-    public SharedPreferences getServicePreference() {
-	return mSettings;
+    public SharedPreferences getServicePreference(String accountName) {
+	return mBeemConnection.get(accountName).getSettings();
     }
 
     /**
@@ -422,7 +367,7 @@
     /**
      * Listen on preference changes.
      */
-    private class BeemServicePreferenceListener implements SharedPreferences.OnSharedPreferenceChangeListener {
+    public class BeemServicePreferenceListener implements SharedPreferences.OnSharedPreferenceChangeListener {
 
 	/**
 	 * ctor.
@@ -463,15 +408,16 @@
 	public void onReceive(final Context context, final Intent intent) {
 	    String intentAction = intent.getAction();
 	    if (intentAction.equals(Intent.ACTION_SCREEN_OFF)) {
-		for (XmppConnectionAdapter connection : mConnection) {
+		for (Map.Entry<String, XmppConnectionAdapter> item : mConnection.entrySet()) {
+		    XmppConnectionAdapter connection = item.getValue();
 		    mOldMode = connection.getPreviousMode();
 		    mOldStatus = connection.getPreviousStatus();
 		    if (connection.isAuthentificated())
-			connection.changeStatus(Status.CONTACT_STATUS_AWAY, mSettings.getString(
-			    "settings_away_message", "Away"));
+			connection.changeStatus(Status.CONTACT_STATUS_AWAY, mBeemConnection.get(item.getKey())
+			    .getSettings().getString("settings_away_message", "Away"));
 		}
 	    } else if (intentAction.equals(Intent.ACTION_SCREEN_ON)) {
-		for (XmppConnectionAdapter connection : mConnection) {
+		for (XmppConnectionAdapter connection : mConnection.values()) {
 		    if (connection.isAuthentificated())
 			connection.changeStatus(mOldMode, mOldStatus);
 		}
--- a/src/com/beem/project/beem/account/Authenticator.java	Sat Mar 19 20:04:33 2011 +0100
+++ b/src/com/beem/project/beem/account/Authenticator.java	Sun Mar 20 00:22:37 2011 +0100
@@ -19,7 +19,7 @@
 	super(context);
 	mContext = context;
     }
-
+    
     @Override
     public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType,
 	String[] requiredFeatures, Bundle options) throws NetworkErrorException {
--- a/src/com/beem/project/beem/service/XmppConnectionAdapter.java	Sat Mar 19 20:04:33 2011 +0100
+++ b/src/com/beem/project/beem/service/XmppConnectionAdapter.java	Sun Mar 20 00:22:37 2011 +0100
@@ -53,6 +53,7 @@
 import org.jivesoftware.smack.filter.PacketFilter;
 import org.jivesoftware.smack.packet.Packet;
 import org.jivesoftware.smack.packet.Presence;
+import org.jivesoftware.smack.util.StringUtils;
 import org.jivesoftware.smackx.ChatStateManager;
 import org.jivesoftware.smackx.ServiceDiscoveryManager;
 import org.jivesoftware.smackx.packet.DiscoverInfo;
@@ -154,14 +155,15 @@
 	final BeemService service) {
 	mAdaptee = con;
 	PrivacyListManager.getInstanceFor(mAdaptee);
-	mLogin = login;
+	mLogin = StringUtils.parseName(login);
+
 	mPassword = password;
 	mService = service;
 	Context ctx = mService.getApplicationContext();
 	if (ctx instanceof BeemApplication) {
 	    mApplication = (BeemApplication) ctx;
 	}
-	mPref = mService.getServicePreference();
+	mPref = mService.getServicePreference(login); // login is the jid
 	try {
 	    mPreviousPriority = Integer.parseInt(mPref.getString("settings_key_priority", "0"));
 	} catch (NumberFormatException ex) {
--- a/src/com/beem/project/beem/ui/wizard/AccountConfigure.java	Sat Mar 19 20:04:33 2011 +0100
+++ b/src/com/beem/project/beem/ui/wizard/AccountConfigure.java	Sun Mar 20 00:22:37 2011 +0100
@@ -148,12 +148,13 @@
      * Store the account in the settings.
      */
     private void configureAccount() {
-	SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
+	final String jid = mAccountJID.getText().toString();
+	SharedPreferences settings = getSharedPreferences(jid, MODE_PRIVATE); //PreferenceManager.getDefaultSharedPreferences(this);
 	SharedPreferences.Editor edit = settings.edit();
-	edit.putString(BeemApplication.ACCOUNT_USERNAME_KEY, mAccountJID.getText().toString());
+	edit.putString(BeemApplication.ACCOUNT_USERNAME_KEY, jid);
 	edit.putString(BeemApplication.ACCOUNT_PASSWORD_KEY, mAccountPassword.getText().toString());
 	edit.commit();
-	Account account = new Account(mAccountJID.getText().toString(), "com.beem.project.com");
+	Account account = new Account(jid, "com.beem.project.com");
 	boolean accountCreated = mAccountManager.addAccountExplicitly(account, mAccountPassword.getText().toString(),
 	    null);
 	Bundle extras = getIntent().getExtras();