diff --git a/build.gradle b/build.gradle index 9c04f04..c584fa9 100644 --- a/build.gradle +++ b/build.gradle @@ -32,12 +32,14 @@ android { debug { minifyEnabled false buildConfigField("String", "VERSION_NAME", "\"${project.subVersionName}a\"") + buildConfigField("String", "BUGLY_ID", BUGLY_ID) } release { minifyEnabled true buildConfigField("String", "VERSION_NAME", "\"${project.subVersionName}a\"") - proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + buildConfigField("String", "BUGLY_ID", BUGLY_ID) + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } diff --git a/src/main/java/com/tencent/msdk/dns/DnsConfig.java b/src/main/java/com/tencent/msdk/dns/DnsConfig.java index a3e9135..bdac595 100644 --- a/src/main/java/com/tencent/msdk/dns/DnsConfig.java +++ b/src/main/java/com/tencent/msdk/dns/DnsConfig.java @@ -62,13 +62,16 @@ public final class DnsConfig { public String routeIp; + public boolean experimentalBuglyEnable; + private DnsConfig(int logLevel, String appId, String userId, boolean initBuiltInReporters, String dnsId, String dnsKey, String token, int timeoutMills, Set protectedDomains, Set preLookupDomains, boolean enablePersistentCache, Set persistentCacheDomains, Set ipRankItems, String channel, boolean enableReport, boolean blockFirst, int customNetStack, DnsExecutors.ExecutorSupplier executorSupplier, ILookedUpListener lookedUpListener, List logNodes, List reporters, - boolean useExpiredIpEnable, boolean cachedIpEnable, String routeIp) { + boolean useExpiredIpEnable, boolean cachedIpEnable, String routeIp, + Boolean experimentalBuglyEnable) { this.logLevel = logLevel; this.appId = appId; this.userId = userId; @@ -91,6 +94,7 @@ private DnsConfig(int logLevel, String appId, String userId, boolean initBuiltIn this.useExpiredIpEnable = useExpiredIpEnable; this.cachedIpEnable = cachedIpEnable; this.routeIp = routeIp; + this.experimentalBuglyEnable = experimentalBuglyEnable; } boolean needProtect(/* @Nullable */String hostname) { @@ -134,6 +138,7 @@ public String toString() { + ", cachedIpEnable=" + cachedIpEnable + ", enableDomainServer=" + enableDomainServer + ", routeIp=" + routeIp + + ", experimentalBuglyEnable=" + experimentalBuglyEnable + '}'; } @@ -223,6 +228,7 @@ public static final class Builder { private boolean mUseExpiredIpEnable = false; private boolean mCachedIpEnable = false; private String mRouteIp = ""; + private boolean mExperimentalBuglyEnable = false; /** * 设置最低日志等级, 低于设置等级的日志不会输出 @@ -727,6 +733,17 @@ public Builder routeIp(String routeIp) { return this; } + /** + * 实验性参数,仅提供给内部特定团队使用,请勿随意启用。 + * + * @param buglyEnable + * @return + */ + public Builder setExperimentalBuglyEnable(Boolean buglyEnable) { + mExperimentalBuglyEnable = buglyEnable; + return this; + } + /** * 构建DnsConfig实例 * @@ -746,7 +763,7 @@ public DnsConfig build() { mTimeoutMills, mProtectedDomains, mPreLookupDomains, mEnablePersistentCache, mPersistentCacheDomains, mIpRankItems, mChannel, mEnableReport, mBlockFirst, mCustomNetStack, mExecutorSupplier, mLookedUpListener, mLogNodes, mReporters, mUseExpiredIpEnable, mCachedIpEnable, - mRouteIp); + mRouteIp, mExperimentalBuglyEnable); } } } diff --git a/src/main/java/com/tencent/msdk/dns/DnsService.java b/src/main/java/com/tencent/msdk/dns/DnsService.java index beb329a..f9355f7 100644 --- a/src/main/java/com/tencent/msdk/dns/DnsService.java +++ b/src/main/java/com/tencent/msdk/dns/DnsService.java @@ -3,6 +3,7 @@ import android.content.Context; import android.text.TextUtils; +import com.tencent.msdk.dns.base.bugly.SharedBugly; import com.tencent.msdk.dns.base.executor.DnsExecutors; import com.tencent.msdk.dns.base.lifecycle.ActivityLifecycleDetector; import com.tencent.msdk.dns.base.log.DnsLog; @@ -62,7 +63,7 @@ public static Context getAppContext() { * @throws IllegalArgumentException context为null时抛出 */ public static void init(Context context, /* @Nullable */DnsConfig config) { - try { + try { // NOTE: 参数检查不封装为通用方法, 是为了避免不必要的concat执行 if (null == context) { throw new IllegalArgumentException("context".concat(Const.NULL_POINTER_TIPS)); @@ -77,6 +78,8 @@ public static void init(Context context, /* @Nullable */DnsConfig config) { Context appContext = context.getApplicationContext(); sAppContext = appContext; sConfig = config; + // 集成共享式bugly + SharedBugly.init(appContext); // 底层配置获取 DnsExecutors.WORK.execute(new Runnable() { @Override @@ -106,9 +109,9 @@ public void run() { sInited = true; preLookupAndStartAsyncLookup(); - } catch (Exception e) { - DnsLog.w("DnsService.init failed: %s", e); - } + } catch (Exception e) { + DnsLog.w("DnsService.init failed: %s", e); + } } /** diff --git a/src/main/java/com/tencent/msdk/dns/base/bugly/SharedBugly.java b/src/main/java/com/tencent/msdk/dns/base/bugly/SharedBugly.java new file mode 100644 index 0000000..eb403ce --- /dev/null +++ b/src/main/java/com/tencent/msdk/dns/base/bugly/SharedBugly.java @@ -0,0 +1,35 @@ +package com.tencent.msdk.dns.base.bugly; + +import android.content.Context; +import android.content.SharedPreferences; + +import com.tencent.msdk.dns.BuildConfig; +import com.tencent.msdk.dns.DnsService; +import com.tencent.msdk.dns.base.log.DnsLog; + +public class SharedBugly { + private static String appId = BuildConfig.BUGLY_ID; + private static String appVersion = BuildConfig.VERSION_NAME; + + public static void init(Context context) { + try { + if (DnsService.getDnsConfig().experimentalBuglyEnable) { + SharedPreferences sharedPreferences = context.getSharedPreferences("BuglySdkInfos", Context.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedPreferences.edit(); + editor.putString(appId, appVersion); //必填信息 + editor.commit(); + DnsLog.d("shared bugly inited success"); + } else { + SharedPreferences sharedPreferences = context.getSharedPreferences("BuglySdkInfos", + Context.MODE_PRIVATE); + if (sharedPreferences.contains(appId)) { + SharedPreferences.Editor editor = sharedPreferences.edit(); + editor.remove(appId); + editor.apply(); + } + } + } catch (Exception e) { + DnsLog.d("shared bugly inited error " + e); + } + } +} diff --git a/src/main/java/com/tencent/msdk/dns/report/AttaHelper.java b/src/main/java/com/tencent/msdk/dns/report/AttaHelper.java index 2ac8af1..b4e060b 100644 --- a/src/main/java/com/tencent/msdk/dns/report/AttaHelper.java +++ b/src/main/java/com/tencent/msdk/dns/report/AttaHelper.java @@ -4,6 +4,7 @@ import android.telephony.TelephonyManager; import com.tencent.msdk.dns.BuildConfig; +import com.tencent.msdk.dns.DnsService; import com.tencent.msdk.dns.base.log.DnsLog; import java.io.IOException; @@ -25,8 +26,7 @@ public class AttaHelper { private static final String SYSTEMVERSION = getSystemVersion(); private static final String SESSIONID = Session.getSessionId(); - public static Runnable report(final String carrier, - final String networkType, + public static Runnable report(final String networkType, final String dnsId, final String appId, final String encryptType, @@ -50,7 +50,9 @@ public static Runnable report(final String carrier, public void run() { HttpURLConnection connection = null; try { - URL url = new URL(ATTA_URL + // 获取手机卡运营商code + String carrier = getSimOperator(DnsService.getAppContext()); + String path = ATTA_URL + "?attaid=" + ATTA_ID + "&token=" + ATTA_TOKEN + "&carrier=" + carrier @@ -80,8 +82,9 @@ public void run() { + "&count=" + count + "&ldns=" + ldns + "&hdns=" + hdns - + "&_dc=" + Math.random() - ); + + "&_dc=" + Math.random(); + path = path.replace(" ", "_"); + URL url = new URL(path); DnsLog.d("开始Atta上报:" + url); connection = (HttpURLConnection) url.openConnection(); //设置请求方法 @@ -91,6 +94,8 @@ public void run() { //设置读取超时时间(毫秒) connection.setReadTimeout(2000); connection.connect(); + int respCode = connection.getResponseCode(); + DnsLog.d("Atta respCode:" + respCode); } catch (IOException e) { e.printStackTrace(); } finally { diff --git a/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java b/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java index e3bc2ef..8b20f1a 100644 --- a/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java +++ b/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java @@ -7,7 +7,6 @@ import com.tencent.msdk.dns.BackupResolver; import com.tencent.msdk.dns.BuildConfig; import com.tencent.msdk.dns.DnsConfig; -import com.tencent.msdk.dns.DnsService; import com.tencent.msdk.dns.base.compat.CollectionCompat; import com.tencent.msdk.dns.base.lifecycle.ActivityLifecycleCallbacksWrapper; import com.tencent.msdk.dns.base.lifecycle.ActivityLifecycleDetector; @@ -187,8 +186,6 @@ private static void attaReportLookupEvent(String eventName, LookupResult lookupR StatisticsMerge statMerge = (StatisticsMerge) lookupResult.stat; BackupResolver backupInfo = BackupResolver.getInstance(); - // 获取手机卡运营商code - String carrierCode = AttaHelper.getSimOperator(DnsService.getAppContext()); // 获取当前dnsip String dnsIp = backupInfo.getDnsIp(); String reqType = AttaHelper.getReqType(statMerge.curNetStack); @@ -198,7 +195,7 @@ private static void attaReportLookupEvent(String eventName, LookupResult lookupR if (statMerge.restDnsStat.errorCode == 0) { // 请求成功后将ErrorCount置为0 backupInfo.setErrorCount(0); - MAIN.execute(AttaHelper.report(carrierCode, statMerge.netType, sDnsConfig.lookupExtra.bizId, + MAIN.execute(AttaHelper.report(statMerge.netType, sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, eventName, System.currentTimeMillis(), dnsIp, statMerge.restDnsStat.costTimeMills, statMerge.localDnsStat.costTimeMills, statMerge.requestHostname, reqType, sDnsConfig.timeoutMills, statMerge.restDnsStat.ttl, @@ -212,7 +209,7 @@ private static void attaReportLookupEvent(String eventName, LookupResult lookupR || (Const.HTTPS_CHANNEL.equals(sDnsConfig.channel) && (statMerge.restDnsStat.errorCode == 1))) { // 解析失败,仅当达到最大失败次数满足切换IP时候上报 if (backupInfo.getCanReport(backupInfo.getErrorCount() + 1)) { - MAIN.execute(AttaHelper.report(carrierCode, statMerge.netType, sDnsConfig.lookupExtra.bizId, + MAIN.execute(AttaHelper.report(statMerge.netType, sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, eventName, System.currentTimeMillis(), dnsIp, statMerge.restDnsStat.costTimeMills, statMerge.localDnsStat.costTimeMills, statMerge.requestHostname, reqType, sDnsConfig.timeoutMills, @@ -225,7 +222,7 @@ private static void attaReportLookupEvent(String eventName, LookupResult lookupR backupInfo.incrementErrorCount(); DnsLog.d("dnsip连接失败, 当前失败次数:" + backupInfo.getErrorCount()); } else { - MAIN.execute(AttaHelper.report(carrierCode, statMerge.netType, sDnsConfig.lookupExtra.bizId, + MAIN.execute(AttaHelper.report(statMerge.netType, sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, eventName, System.currentTimeMillis(), dnsIp, statMerge.restDnsStat.costTimeMills, statMerge.localDnsStat.costTimeMills, statMerge.requestHostname, reqType, sDnsConfig.timeoutMills, statMerge.restDnsStat.ttl, @@ -249,20 +246,18 @@ private static void attaReportStatisticsEvent() { int errCount = (int) temp[1]; int curCount = (int) temp[2]; int spendAvg = (int) temp[0] / (errCount + curCount); - // 获取手机卡运营商code - String carrierCode = AttaHelper.getSimOperator(DnsService.getAppContext()); // 获取当前dnsip String dnsIp = BackupResolver.getInstance().getDnsIp(); if (errCount > 0) { // 为空的缓存统计项上报,解析结果不上报 - MAIN.execute(AttaHelper.report(carrierCode, "", sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, + MAIN.execute(AttaHelper.report("", sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, ReportConst.LOOKUP_FROM_CACHED_EVENT_NAME, System.currentTimeMillis(), dnsIp, spendAvg, 0, item.getKey(), "", sDnsConfig.timeoutMills, null, 3, 0, true, errCount, null, null)); } if (curCount > 0) { // 有值的缓存统计项上报,解析结果不上报 - MAIN.execute(AttaHelper.report(carrierCode, "", sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, + MAIN.execute(AttaHelper.report( "", sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, ReportConst.LOOKUP_FROM_CACHED_EVENT_NAME, System.currentTimeMillis(), dnsIp, spendAvg, 0, item.getKey(), "", sDnsConfig.timeoutMills, null, 0, 0, true, curCount, null, null));