Modify the preferences to deal with account.
authorDa Risk <da_risk@beem-project.com>
Tue, 25 Sep 2012 21:17:40 +0200
changeset 992 3808cae96552
parent 991 9a579769bb05
child 993 8e9ab434ef7d
Modify the preferences to deal with account.
res/values-fr/strings.xml
res/values/strings.xml
res/xml/preferences.xml
src/com/beem/project/beem/BeemService.java
src/com/beem/project/beem/ui/Settings.java
--- a/res/values-fr/strings.xml	Mon Sep 24 23:41:26 2012 +0200
+++ b/res/values-fr/strings.xml	Tue Sep 25 21:17:40 2012 +0200
@@ -217,7 +217,7 @@
 
 	<!-- EditSettings Activity Categories -->
 	<string name="general_preferences">Préférences de l\'application</string>
-	<string name="user_preferences">Utilisateur (obligatoire)</string>
+	<string name="user_preferences">Compte (obligatoire)</string>
 	<string name="user_preferences_advanced">Utilisateur avancés (facultatif)</string>
 	<string name="network_preferences">Options réseau</string>
 	<string name="proxy_proxy_settings">Paramètres du proxy</string>
--- a/res/values/strings.xml	Mon Sep 24 23:41:26 2012 +0200
+++ b/res/values/strings.xml	Tue Sep 25 21:17:40 2012 +0200
@@ -209,7 +209,7 @@
 
 	<!-- EditSettings Activity Categories -->
 	<string name="general_preferences">General preferences</string>
-	<string name="user_preferences">User settings (required)</string>
+	<string name="user_preferences">Account (required)</string>
 	<string name="user_preferences_advanced">User settings advanced (optional)</string>
 	<string name="network_preferences">Network options</string>
 	<string name="proxy_proxy_settings">Proxy settings</string>
--- a/res/xml/preferences.xml	Mon Sep 24 23:41:26 2012 +0200
+++ b/res/xml/preferences.xml	Tue Sep 25 21:17:40 2012 +0200
@@ -81,13 +81,9 @@
 		</PreferenceScreen>
 	</PreferenceCategory>
 	<PreferenceCategory android:title="@string/user_preferences">
-		<EditTextPreference android:singleLine="true"
-			android:summary="@string/SettingsText" android:title="@string/settings_account_username"
-			android:key="account_username" android:hint="@string/login_username_info_default"
-			android:inputType="textEmailAddress" />
-		<EditTextPreference android:name="password"
-			android:singleLine="true" android:password="true" android:summary="@string/SettingsPassword"
-			android:title="@string/settings_account_password" android:key="account_password" />
+	    <Preference android:key="account_username" android:title="@string/settings_account_username">
+	        <intent android:targetPackage="com.beem.project.beem" android:targetClass="com.beem.project.beem.ui.wizard.Account" />
+	    </Preference>
 	</PreferenceCategory>
 	<PreferenceCategory android:title="@string/user_preferences_advanced">
 		<EditTextPreference android:key="connection_resource"
--- a/src/com/beem/project/beem/BeemService.java	Mon Sep 24 23:41:26 2012 +0200
+++ b/src/com/beem/project/beem/BeemService.java	Tue Sep 25 21:17:40 2012 +0200
@@ -47,6 +47,8 @@
 
 import javax.net.ssl.SSLContext;
 
+import android.accounts.Account;
+import android.accounts.AccountManager;
 import android.app.Notification;
 import android.app.NotificationManager;
 import android.app.Service;
@@ -67,11 +69,13 @@
 import com.beem.project.beem.service.XmppConnectionAdapter;
 import com.beem.project.beem.service.XmppFacade;
 import com.beem.project.beem.service.aidl.IXmppFacade;
+import com.beem.project.beem.service.auth.AccountAuthenticator;
 import com.beem.project.beem.service.auth.PreferenceAuthenticator;
 import com.beem.project.beem.smack.avatar.AvatarMetadataProvider;
 import com.beem.project.beem.smack.avatar.AvatarProvider;
 import com.beem.project.beem.smack.caps.CapsProvider;
 import com.beem.project.beem.smack.ping.PingExtension;
+import com.beem.project.beem.smack.sasl.SASLGoogleOAuth2Mechanism;
 import com.beem.project.beem.utils.BeemBroadcastReceiver;
 import com.beem.project.beem.utils.BeemConnectivity;
 import com.beem.project.beem.utils.Status;
@@ -82,6 +86,7 @@
 import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
 import org.jivesoftware.smack.Roster;
 import org.jivesoftware.smack.Roster.SubscriptionMode;
+import org.jivesoftware.smack.SASLAuthentication;
 import org.jivesoftware.smack.XMPPConnection;
 import org.jivesoftware.smack.provider.ProviderManager;
 import org.jivesoftware.smack.proxy.ProxyInfo;
@@ -156,10 +161,28 @@
 	} else {
 	    mProxyInfo = ProxyInfo.forNoProxy();
 	}
+	boolean useSystemAccount = mSettings.getBoolean(BeemApplication.USE_SYSTEM_ACCOUNT_KEY, false);
 	if (mSettings.getBoolean("settings_key_specific_server", false))
 	    mConnectionConfiguration = new ConnectionConfiguration(mHost, mPort, mService, mProxyInfo);
-	else
+	if (useSystemAccount) {
+	    String accountType = mSettings.getString(BeemApplication.ACCOUNT_SYSTEM_TYPE_KEY, "");
+	    String accountName = mSettings.getString(BeemApplication.ACCOUNT_USERNAME_KEY, "");
+	    Account account = getAccount(accountName, accountType);
+	    if (account == null) {
+		mSettings.edit().putString(BeemApplication.ACCOUNT_USERNAME_KEY, "");
+		mConnectionConfiguration = new ConnectionConfiguration(mService, mProxyInfo);
+		mConnectionConfiguration.setCallbackHandler(new PreferenceAuthenticator(this));
+	    } else if ("com.google".equals(accountType)) {
+		mConnectionConfiguration = new ConnectionConfiguration("talk.google.com", 5222, mProxyInfo);
+		mConnectionConfiguration.setServiceName(StringUtils.parseServer(accountName));
+		mConnectionConfiguration.setCallbackHandler(new AccountAuthenticator(this, account));
+		mConnectionConfiguration.setSecurityMode(SecurityMode.required);
+	    } else
+		mConnectionConfiguration.setCallbackHandler(new AccountAuthenticator(this, account));
+	} else {
 	    mConnectionConfiguration = new ConnectionConfiguration(mService, mProxyInfo);
+		mConnectionConfiguration.setCallbackHandler(new PreferenceAuthenticator(this));
+	}
 
 	if (mSettings.getBoolean("settings_key_xmpp_tls_use", false)
 	    || mSettings.getBoolean("settings_key_gmail", false)) {
@@ -173,7 +196,7 @@
 	mConnectionConfiguration.setTruststorePath("/system/etc/security/cacerts.bks");
 	if (sslContext != null)
 	    mConnectionConfiguration.setCustomSSLContext(sslContext);
-	mConnectionConfiguration.setCallbackHandler(new PreferenceAuthenticator(this));
+    }
     }
 
     /**
@@ -211,6 +234,7 @@
 	}
 	String tmpJid = mSettings.getString(BeemApplication.ACCOUNT_USERNAME_KEY, "").trim();
 	mLogin = StringUtils.parseName(tmpJid);
+	boolean useSystemAccount = mSettings.getBoolean(BeemApplication.USE_SYSTEM_ACCOUNT_KEY, false);
 	mPort = DEFAULT_XMPP_PORT;
 	mService = StringUtils.parseServer(tmpJid);
 	mHost = mService;
@@ -225,7 +249,8 @@
 		mPort = Integer.parseInt(tmpPort);
 	}
 	if (mSettings.getBoolean(BeemApplication.FULL_JID_LOGIN_KEY, false)
-		|| "gmail.com".equals(mService) || "googlemail.com".equals(mService))  {
+		|| "gmail.com".equals(mService) || "googlemail.com".equals(mService)
+		|| useSystemAccount)  {
 	    mLogin = tmpJid;
 	}
 
@@ -372,6 +397,24 @@
     }
 
     /**
+     * Get the specified Android account.
+     *
+     * @param accountName the account name
+     * @param accountType the account type
+     *
+     * @return the account or null if it does not exist
+     */
+    private Account getAccount(String accountName, String accountType) {
+	AccountManager am = AccountManager.get(this);
+	for (Account a : am.getAccountsByType(accountType)) {
+	    if (a.name.equals(accountName)) {
+		return a;
+	    }
+	}
+	return null;
+    }
+
+    /**
      * Install the MemorizingTrustManager in the ConnectionConfiguration of Smack.
      */
     private void initMemorizingTrustManager() {
@@ -507,7 +550,7 @@
 	    new AdHocCommandDataProvider.SessionExpiredError());
 	 */
 
-	// register additionnals sasl mechanisms
+	/* register additionnals sasl mechanisms */
 	SASLAuthentication.registerSASLMechanism(SASLGoogleOAuth2Mechanism.MECHANISM_NAME,
 	    SASLGoogleOAuth2Mechanism.class);
 	SASLAuthentication.supportSASLMechanism(SASLGoogleOAuth2Mechanism.MECHANISM_NAME);
--- a/src/com/beem/project/beem/ui/Settings.java	Mon Sep 24 23:41:26 2012 +0200
+++ b/src/com/beem/project/beem/ui/Settings.java	Tue Sep 25 21:17:40 2012 +0200
@@ -46,11 +46,13 @@
 import android.content.ComponentName;
 import android.content.Intent;
 import android.os.Bundle;
+import android.preference.Preference;
 import android.preference.PreferenceActivity;
 import android.view.Menu;
 import android.view.MenuInflater;
 import android.view.MenuItem;
 
+import com.beem.project.beem.BeemApplication;
 import com.beem.project.beem.R;
 import com.beem.project.beem.ui.wizard.Account;
 
@@ -77,6 +79,15 @@
 	addPreferencesFromResource(R.xml.preferences);
     }
 
+    @Override
+    protected void onStart() {
+    	super.onStart();
+    	String accountName = getPreferenceManager().getDefaultSharedPreferences(this)
+	    .getString(BeemApplication.ACCOUNT_USERNAME_KEY, "");
+    	Preference account = findPreference(BeemApplication.ACCOUNT_USERNAME_KEY);
+    	account.setSummary(accountName);
+    }
+
     /**
      * {@inheritDoc}
      */