# HG changeset patch # User "Vincent Veronis" # Date 1300576957 -3600 # Node ID cdf001e43dce5b6005ff782bef8c4d1a0e6c7198 # Parent 11a7b9c1c500bfd3b67a98e8adfb6988b3a53dcf Multi Account on the beem service OK but doesnt connect diff -r 11a7b9c1c500 -r cdf001e43dce AndroidManifest.xml --- 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 @@ + mConnection = new ArrayList(); - 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 mBind = new ArrayList(); + //TODO: MAP instead of LIST + private Map mConnection = new HashMap(); + private Map mBeemConnection = new HashMap(); + + //TODO: MAp maybe instead of LIST + private Map mBind = new HashMap(); + + 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 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); } diff -r 11a7b9c1c500 -r cdf001e43dce src/com/beem/project/beem/account/Authenticator.java --- 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 { diff -r 11a7b9c1c500 -r cdf001e43dce src/com/beem/project/beem/service/XmppConnectionAdapter.java --- 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) { diff -r 11a7b9c1c500 -r cdf001e43dce src/com/beem/project/beem/ui/wizard/AccountConfigure.java --- 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();