45 |
45 |
46 import java.security.GeneralSecurityException; |
46 import java.security.GeneralSecurityException; |
47 |
47 |
48 import javax.net.ssl.SSLContext; |
48 import javax.net.ssl.SSLContext; |
49 |
49 |
|
50 import android.accounts.Account; |
|
51 import android.accounts.AccountManager; |
50 import android.app.Notification; |
52 import android.app.Notification; |
51 import android.app.NotificationManager; |
53 import android.app.NotificationManager; |
52 import android.app.Service; |
54 import android.app.Service; |
53 import android.content.BroadcastReceiver; |
55 import android.content.BroadcastReceiver; |
54 import android.content.Context; |
56 import android.content.Context; |
65 import android.util.Log; |
67 import android.util.Log; |
66 |
68 |
67 import com.beem.project.beem.service.XmppConnectionAdapter; |
69 import com.beem.project.beem.service.XmppConnectionAdapter; |
68 import com.beem.project.beem.service.XmppFacade; |
70 import com.beem.project.beem.service.XmppFacade; |
69 import com.beem.project.beem.service.aidl.IXmppFacade; |
71 import com.beem.project.beem.service.aidl.IXmppFacade; |
|
72 import com.beem.project.beem.service.auth.AccountAuthenticator; |
70 import com.beem.project.beem.service.auth.PreferenceAuthenticator; |
73 import com.beem.project.beem.service.auth.PreferenceAuthenticator; |
71 import com.beem.project.beem.smack.avatar.AvatarMetadataProvider; |
74 import com.beem.project.beem.smack.avatar.AvatarMetadataProvider; |
72 import com.beem.project.beem.smack.avatar.AvatarProvider; |
75 import com.beem.project.beem.smack.avatar.AvatarProvider; |
73 import com.beem.project.beem.smack.caps.CapsProvider; |
76 import com.beem.project.beem.smack.caps.CapsProvider; |
74 import com.beem.project.beem.smack.ping.PingExtension; |
77 import com.beem.project.beem.smack.ping.PingExtension; |
|
78 import com.beem.project.beem.smack.sasl.SASLGoogleOAuth2Mechanism; |
75 import com.beem.project.beem.utils.BeemBroadcastReceiver; |
79 import com.beem.project.beem.utils.BeemBroadcastReceiver; |
76 import com.beem.project.beem.utils.BeemConnectivity; |
80 import com.beem.project.beem.utils.BeemConnectivity; |
77 import com.beem.project.beem.utils.Status; |
81 import com.beem.project.beem.utils.Status; |
78 |
82 |
79 import de.duenndns.ssl.MemorizingTrustManager; |
83 import de.duenndns.ssl.MemorizingTrustManager; |
80 |
84 |
81 import org.jivesoftware.smack.ConnectionConfiguration; |
85 import org.jivesoftware.smack.ConnectionConfiguration; |
82 import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode; |
86 import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode; |
83 import org.jivesoftware.smack.Roster; |
87 import org.jivesoftware.smack.Roster; |
84 import org.jivesoftware.smack.Roster.SubscriptionMode; |
88 import org.jivesoftware.smack.Roster.SubscriptionMode; |
|
89 import org.jivesoftware.smack.SASLAuthentication; |
85 import org.jivesoftware.smack.XMPPConnection; |
90 import org.jivesoftware.smack.XMPPConnection; |
86 import org.jivesoftware.smack.provider.ProviderManager; |
91 import org.jivesoftware.smack.provider.ProviderManager; |
87 import org.jivesoftware.smack.proxy.ProxyInfo; |
92 import org.jivesoftware.smack.proxy.ProxyInfo; |
88 import org.jivesoftware.smack.proxy.ProxyInfo.ProxyType; |
93 import org.jivesoftware.smack.proxy.ProxyInfo.ProxyType; |
89 import org.jivesoftware.smack.util.StringUtils; |
94 import org.jivesoftware.smack.util.StringUtils; |
154 ProxyInfo.ProxyType type = ProxyType.valueOf(stype); |
159 ProxyInfo.ProxyType type = ProxyType.valueOf(stype); |
155 mProxyInfo = new ProxyInfo(type, phost, pport, puser, ppass); |
160 mProxyInfo = new ProxyInfo(type, phost, pport, puser, ppass); |
156 } else { |
161 } else { |
157 mProxyInfo = ProxyInfo.forNoProxy(); |
162 mProxyInfo = ProxyInfo.forNoProxy(); |
158 } |
163 } |
|
164 boolean useSystemAccount = mSettings.getBoolean(BeemApplication.USE_SYSTEM_ACCOUNT_KEY, false); |
159 if (mSettings.getBoolean("settings_key_specific_server", false)) |
165 if (mSettings.getBoolean("settings_key_specific_server", false)) |
160 mConnectionConfiguration = new ConnectionConfiguration(mHost, mPort, mService, mProxyInfo); |
166 mConnectionConfiguration = new ConnectionConfiguration(mHost, mPort, mService, mProxyInfo); |
161 else |
167 if (useSystemAccount) { |
|
168 String accountType = mSettings.getString(BeemApplication.ACCOUNT_SYSTEM_TYPE_KEY, ""); |
|
169 String accountName = mSettings.getString(BeemApplication.ACCOUNT_USERNAME_KEY, ""); |
|
170 Account account = getAccount(accountName, accountType); |
|
171 if (account == null) { |
|
172 mSettings.edit().putString(BeemApplication.ACCOUNT_USERNAME_KEY, ""); |
|
173 mConnectionConfiguration = new ConnectionConfiguration(mService, mProxyInfo); |
|
174 mConnectionConfiguration.setCallbackHandler(new PreferenceAuthenticator(this)); |
|
175 } else if ("com.google".equals(accountType)) { |
|
176 mConnectionConfiguration = new ConnectionConfiguration("talk.google.com", 5222, mProxyInfo); |
|
177 mConnectionConfiguration.setServiceName(StringUtils.parseServer(accountName)); |
|
178 mConnectionConfiguration.setCallbackHandler(new AccountAuthenticator(this, account)); |
|
179 mConnectionConfiguration.setSecurityMode(SecurityMode.required); |
|
180 } else |
|
181 mConnectionConfiguration.setCallbackHandler(new AccountAuthenticator(this, account)); |
|
182 } else { |
162 mConnectionConfiguration = new ConnectionConfiguration(mService, mProxyInfo); |
183 mConnectionConfiguration = new ConnectionConfiguration(mService, mProxyInfo); |
|
184 mConnectionConfiguration.setCallbackHandler(new PreferenceAuthenticator(this)); |
|
185 } |
163 |
186 |
164 if (mSettings.getBoolean("settings_key_xmpp_tls_use", false) |
187 if (mSettings.getBoolean("settings_key_xmpp_tls_use", false) |
165 || mSettings.getBoolean("settings_key_gmail", false)) { |
188 || mSettings.getBoolean("settings_key_gmail", false)) { |
166 mConnectionConfiguration.setSecurityMode(SecurityMode.required); |
189 mConnectionConfiguration.setSecurityMode(SecurityMode.required); |
167 } |
190 } |
171 // maybe not the universal path, but it works on most devices (Samsung Galaxy, Google Nexus One) |
194 // maybe not the universal path, but it works on most devices (Samsung Galaxy, Google Nexus One) |
172 mConnectionConfiguration.setTruststoreType("BKS"); |
195 mConnectionConfiguration.setTruststoreType("BKS"); |
173 mConnectionConfiguration.setTruststorePath("/system/etc/security/cacerts.bks"); |
196 mConnectionConfiguration.setTruststorePath("/system/etc/security/cacerts.bks"); |
174 if (sslContext != null) |
197 if (sslContext != null) |
175 mConnectionConfiguration.setCustomSSLContext(sslContext); |
198 mConnectionConfiguration.setCustomSSLContext(sslContext); |
176 mConnectionConfiguration.setCallbackHandler(new PreferenceAuthenticator(this)); |
199 } |
177 } |
200 } |
178 |
201 |
179 /** |
202 /** |
180 * {@inheritDoc} |
203 * {@inheritDoc} |
181 */ |
204 */ |
209 registerReceiver(mOnOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF)); |
232 registerReceiver(mOnOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF)); |
210 registerReceiver(mOnOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_ON)); |
233 registerReceiver(mOnOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_ON)); |
211 } |
234 } |
212 String tmpJid = mSettings.getString(BeemApplication.ACCOUNT_USERNAME_KEY, "").trim(); |
235 String tmpJid = mSettings.getString(BeemApplication.ACCOUNT_USERNAME_KEY, "").trim(); |
213 mLogin = StringUtils.parseName(tmpJid); |
236 mLogin = StringUtils.parseName(tmpJid); |
|
237 boolean useSystemAccount = mSettings.getBoolean(BeemApplication.USE_SYSTEM_ACCOUNT_KEY, false); |
214 mPort = DEFAULT_XMPP_PORT; |
238 mPort = DEFAULT_XMPP_PORT; |
215 mService = StringUtils.parseServer(tmpJid); |
239 mService = StringUtils.parseServer(tmpJid); |
216 mHost = mService; |
240 mHost = mService; |
217 initMemorizingTrustManager(); |
241 initMemorizingTrustManager(); |
218 |
242 |
223 String tmpPort = mSettings.getString("settings_key_xmpp_port", "5222"); |
247 String tmpPort = mSettings.getString("settings_key_xmpp_port", "5222"); |
224 if (!"".equals(tmpPort)) |
248 if (!"".equals(tmpPort)) |
225 mPort = Integer.parseInt(tmpPort); |
249 mPort = Integer.parseInt(tmpPort); |
226 } |
250 } |
227 if (mSettings.getBoolean(BeemApplication.FULL_JID_LOGIN_KEY, false) |
251 if (mSettings.getBoolean(BeemApplication.FULL_JID_LOGIN_KEY, false) |
228 || "gmail.com".equals(mService) || "googlemail.com".equals(mService)) { |
252 || "gmail.com".equals(mService) || "googlemail.com".equals(mService) |
|
253 || useSystemAccount) { |
229 mLogin = tmpJid; |
254 mLogin = tmpJid; |
230 } |
255 } |
231 |
256 |
232 configure(ProviderManager.getInstance()); |
257 configure(ProviderManager.getInstance()); |
233 |
258 |
367 try { |
392 try { |
368 mConnection.connectAsync(); |
393 mConnection.connectAsync(); |
369 } catch (RemoteException e) { |
394 } catch (RemoteException e) { |
370 Log.w(TAG, "unable to connect", e); |
395 Log.w(TAG, "unable to connect", e); |
371 } |
396 } |
|
397 } |
|
398 |
|
399 /** |
|
400 * Get the specified Android account. |
|
401 * |
|
402 * @param accountName the account name |
|
403 * @param accountType the account type |
|
404 * |
|
405 * @return the account or null if it does not exist |
|
406 */ |
|
407 private Account getAccount(String accountName, String accountType) { |
|
408 AccountManager am = AccountManager.get(this); |
|
409 for (Account a : am.getAccountsByType(accountType)) { |
|
410 if (a.name.equals(accountName)) { |
|
411 return a; |
|
412 } |
|
413 } |
|
414 return null; |
372 } |
415 } |
373 |
416 |
374 /** |
417 /** |
375 * Install the MemorizingTrustManager in the ConnectionConfiguration of Smack. |
418 * Install the MemorizingTrustManager in the ConnectionConfiguration of Smack. |
376 */ |
419 */ |
505 new AdHocCommandDataProvider.BadSessionIDError()); |
548 new AdHocCommandDataProvider.BadSessionIDError()); |
506 pm.addExtensionProvider("session-expired", COMMAND_NAMESPACE, |
549 pm.addExtensionProvider("session-expired", COMMAND_NAMESPACE, |
507 new AdHocCommandDataProvider.SessionExpiredError()); |
550 new AdHocCommandDataProvider.SessionExpiredError()); |
508 */ |
551 */ |
509 |
552 |
510 // register additionnals sasl mechanisms |
553 /* register additionnals sasl mechanisms */ |
511 SASLAuthentication.registerSASLMechanism(SASLGoogleOAuth2Mechanism.MECHANISM_NAME, |
554 SASLAuthentication.registerSASLMechanism(SASLGoogleOAuth2Mechanism.MECHANISM_NAME, |
512 SASLGoogleOAuth2Mechanism.class); |
555 SASLGoogleOAuth2Mechanism.class); |
513 SASLAuthentication.supportSASLMechanism(SASLGoogleOAuth2Mechanism.MECHANISM_NAME); |
556 SASLAuthentication.supportSASLMechanism(SASLGoogleOAuth2Mechanism.MECHANISM_NAME); |
514 } |
557 } |
515 |
558 |