1 package org.jivesoftware.smack; 2 3 import org.jivesoftware.smack.util.DNSUtil; 4 import org.jivesoftware.smack.util.dns.DNSJavaResolver; 5 import org.jivesoftware.smackx.ConfigureProviderManager; 6 import org.jivesoftware.smackx.InitStaticCode; 7 import org.xbill.DNS.ResolverConfig; 8 9 import android.content.BroadcastReceiver; 10 import android.content.Context; 11 import android.content.Intent; 12 import android.content.IntentFilter; 13 14 public class SmackAndroid { 15 private static SmackAndroid sSmackAndroid = null; 16 17 private BroadcastReceiver mConnectivityChangedReceiver; 18 private Context mCtx; 19 20 private SmackAndroid(Context ctx) { 21 mCtx = ctx; 22 DNSUtil.setDNSResolver(DNSJavaResolver.getInstance()); 23 InitStaticCode.initStaticCode(ctx); 24 ConfigureProviderManager.configureProviderManager(); 25 maybeRegisterReceiver(); 26 } 27 28 public static SmackAndroid init(Context ctx) { 29 if (sSmackAndroid == null) { 30 sSmackAndroid = new SmackAndroid(ctx); 31 } else { 32 sSmackAndroid.maybeRegisterReceiver(); 33 } 34 return sSmackAndroid; 35 } 36 37 public void onDestroy() { 38 if (mConnectivityChangedReceiver != null) { 39 mCtx.unregisterReceiver(mConnectivityChangedReceiver); 40 mConnectivityChangedReceiver = null; 41 } 42 } 43 44 private void maybeRegisterReceiver() { 45 if (mConnectivityChangedReceiver == null) { 46 mConnectivityChangedReceiver = new ConnectivtyChangedReceiver(); 47 mCtx.registerReceiver(mConnectivityChangedReceiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE")); 48 } 49 } 50 51 class ConnectivtyChangedReceiver extends BroadcastReceiver { 52 53 @Override 54 public void onReceive(Context context, Intent intent) { 55 ResolverConfig.refresh(); 56 } 57 58 } 59 } 60