diff options
author | Shuyi Chen <shuyichen@google.com> | 2013-05-22 13:10:40 -0700 |
---|---|---|
committer | Shuyi Chen <shuyichen@google.com> | 2013-05-22 13:10:40 -0700 |
commit | 8f4ce9ea0de51fee918bffe19c434612d6bbb2d7 (patch) | |
tree | aa6b731d2b1c404e048839fc3ace150eaef2512d /asmack-master/static-src/custom/org/jivesoftware/smack | |
parent | 406fc12177de3a9f13c197838235c2fa3e0f16f0 (diff) | |
download | smack-8f4ce9ea0de51fee918bffe19c434612d6bbb2d7.tar.gz |
Add asmack build environemnt.
Downloaded from https://github.com/Flowdalic/asmack/archive/master.zip.
Change-Id: I93fb953348f2d2c058113874923777530a24bcc6
Diffstat (limited to 'asmack-master/static-src/custom/org/jivesoftware/smack')
-rw-r--r-- | asmack-master/static-src/custom/org/jivesoftware/smack/AndroidConnectionConfiguration.java | 113 | ||||
-rw-r--r-- | asmack-master/static-src/custom/org/jivesoftware/smack/SmackAndroid.java | 59 |
2 files changed, 172 insertions, 0 deletions
diff --git a/asmack-master/static-src/custom/org/jivesoftware/smack/AndroidConnectionConfiguration.java b/asmack-master/static-src/custom/org/jivesoftware/smack/AndroidConnectionConfiguration.java new file mode 100644 index 0000000..6ec05e0 --- /dev/null +++ b/asmack-master/static-src/custom/org/jivesoftware/smack/AndroidConnectionConfiguration.java @@ -0,0 +1,113 @@ +package org.jivesoftware.smack; + +import java.io.File; + +import android.os.Build; + +import org.jivesoftware.smack.proxy.ProxyInfo; +import org.jivesoftware.smack.util.DNSUtil; +import org.jivesoftware.smack.util.dns.HostAddress; + +import java.util.List; + +/** + * This class wraps DNS SRV lookups for a new ConnectionConfiguration in a + * new thread, since Android API >= 11 (Honeycomb) does not allow network + * activity in the main thread. + * + * @author Florian Schmaus fschmaus@gmail.com + * + */ +public class AndroidConnectionConfiguration extends ConnectionConfiguration { + private static final int DEFAULT_TIMEOUT = 10000; + + /** + * Creates a new ConnectionConfiguration for the specified service name. + * A DNS SRV lookup will be performed to find out the actual host address + * and port to use for the connection. + * + * @param serviceName the name of the service provided by an XMPP server. + */ + public AndroidConnectionConfiguration(String serviceName) throws XMPPException { + super(); + AndroidInit(serviceName, DEFAULT_TIMEOUT); + } + + /** + * + * @param serviceName + * @param timeout + * @throws XMPPException + */ + public AndroidConnectionConfiguration(String serviceName, int timeout) throws XMPPException { + super(); + AndroidInit(serviceName, timeout); + } + + public AndroidConnectionConfiguration(String host, int port, String name) { + super(host, port, name); + AndroidInit(); + } + + private void AndroidInit() { + // API 14 is Ice Cream Sandwich + if (Build.VERSION.SDK_INT >= 14) { + setTruststoreType("AndroidCAStore"); + setTruststorePassword(null); + setTruststorePath(null); + } else { + setTruststoreType("BKS"); + String path = System.getProperty("javax.net.ssl.trustStore"); + if (path == null) + path = System.getProperty("java.home") + File.separator + "etc" + + File.separator + "security" + File.separator + + "cacerts.bks"; + setTruststorePath(path); + } + } + + /** + * + * @param serviceName + * @param timeout + * @throws XMPPException + */ + private void AndroidInit(String serviceName, int timeout) throws XMPPException { + AndroidInit(); + class DnsSrvLookupRunnable implements Runnable { + String serviceName; + List<HostAddress> addresses; + + public DnsSrvLookupRunnable(String serviceName) { + this.serviceName = serviceName; + } + + @Override + public void run() { + addresses = DNSUtil.resolveXMPPDomain(serviceName); + } + + public List<HostAddress> getHostAddresses() { + return addresses; + } + } + + DnsSrvLookupRunnable dnsSrv = new DnsSrvLookupRunnable(serviceName); + Thread t = new Thread(dnsSrv, "dns-srv-lookup"); + t.start(); + try { + t.join(timeout); + } catch (InterruptedException e) { + throw new XMPPException("DNS lookup timeout after " + timeout + "ms", e); + } + + hostAddresses = dnsSrv.getHostAddresses(); + if (hostAddresses == null) { + throw new XMPPException("DNS lookup failure"); + } + + ProxyInfo proxy = ProxyInfo.forDefaultProxy(); + + init(serviceName, proxy); + } +} diff --git a/asmack-master/static-src/custom/org/jivesoftware/smack/SmackAndroid.java b/asmack-master/static-src/custom/org/jivesoftware/smack/SmackAndroid.java new file mode 100644 index 0000000..a18d675 --- /dev/null +++ b/asmack-master/static-src/custom/org/jivesoftware/smack/SmackAndroid.java @@ -0,0 +1,59 @@ +package org.jivesoftware.smack; + +import org.jivesoftware.smack.util.DNSUtil; +import org.jivesoftware.smack.util.dns.DNSJavaResolver; +import org.jivesoftware.smackx.ConfigureProviderManager; +import org.jivesoftware.smackx.InitStaticCode; +import org.xbill.DNS.ResolverConfig; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; + +public class SmackAndroid { + private static SmackAndroid sSmackAndroid = null; + + private BroadcastReceiver mConnectivityChangedReceiver; + private Context mCtx; + + private SmackAndroid(Context ctx) { + mCtx = ctx; + DNSUtil.setDNSResolver(DNSJavaResolver.getInstance()); + InitStaticCode.initStaticCode(ctx); + ConfigureProviderManager.configureProviderManager(); + maybeRegisterReceiver(); + } + + public static SmackAndroid init(Context ctx) { + if (sSmackAndroid == null) { + sSmackAndroid = new SmackAndroid(ctx); + } else { + sSmackAndroid.maybeRegisterReceiver(); + } + return sSmackAndroid; + } + + public void onDestroy() { + if (mConnectivityChangedReceiver != null) { + mCtx.unregisterReceiver(mConnectivityChangedReceiver); + mConnectivityChangedReceiver = null; + } + } + + private void maybeRegisterReceiver() { + if (mConnectivityChangedReceiver == null) { + mConnectivityChangedReceiver = new ConnectivtyChangedReceiver(); + mCtx.registerReceiver(mConnectivityChangedReceiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE")); + } + } + + class ConnectivtyChangedReceiver extends BroadcastReceiver { + + @Override + public void onReceive(Context context, Intent intent) { + ResolverConfig.refresh(); + } + + } +} |