Skip to content

Commit

Permalink
Merge pull request #31 from lemondark/release/v4.9.1
Browse files Browse the repository at this point in the history
release: v4.9.1
  • Loading branch information
ilovetochangetheworld authored May 7, 2024
2 parents 7e9d2de + 404dbfd commit 59569f2
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 22 deletions.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}

Expand Down
21 changes: 19 additions & 2 deletions src/main/java/com/tencent/msdk/dns/DnsConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<WildcardDomain> protectedDomains,
Set<String> preLookupDomains, boolean enablePersistentCache, Set<String> persistentCacheDomains,
Set<IpRankItem> ipRankItems, String channel, boolean enableReport, boolean blockFirst,
int customNetStack, DnsExecutors.ExecutorSupplier executorSupplier,
ILookedUpListener lookedUpListener, List<ILogNode> logNodes, List<IReporter> reporters,
boolean useExpiredIpEnable, boolean cachedIpEnable, String routeIp) {
boolean useExpiredIpEnable, boolean cachedIpEnable, String routeIp,
Boolean experimentalBuglyEnable) {
this.logLevel = logLevel;
this.appId = appId;
this.userId = userId;
Expand All @@ -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) {
Expand Down Expand Up @@ -134,6 +138,7 @@ public String toString() {
+ ", cachedIpEnable=" + cachedIpEnable
+ ", enableDomainServer=" + enableDomainServer
+ ", routeIp=" + routeIp
+ ", experimentalBuglyEnable=" + experimentalBuglyEnable
+ '}';
}

Expand Down Expand Up @@ -223,6 +228,7 @@ public static final class Builder {
private boolean mUseExpiredIpEnable = false;
private boolean mCachedIpEnable = false;
private String mRouteIp = "";
private boolean mExperimentalBuglyEnable = false;

/**
* 设置最低日志等级, 低于设置等级的日志不会输出
Expand Down Expand Up @@ -727,6 +733,17 @@ public Builder routeIp(String routeIp) {
return this;
}

/**
* 实验性参数,仅提供给内部特定团队使用,请勿随意启用。
*
* @param buglyEnable
* @return
*/
public Builder setExperimentalBuglyEnable(Boolean buglyEnable) {
mExperimentalBuglyEnable = buglyEnable;
return this;
}

/**
* 构建DnsConfig实例
*
Expand All @@ -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);
}
}
}
11 changes: 7 additions & 4 deletions src/main/java/com/tencent/msdk/dns/DnsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
}

/**
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/com/tencent/msdk/dns/base/bugly/SharedBugly.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
15 changes: 10 additions & 5 deletions src/main/java/com/tencent/msdk/dns/report/AttaHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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();
//设置请求方法
Expand All @@ -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 {
Expand Down
15 changes: 5 additions & 10 deletions src/main/java/com/tencent/msdk/dns/report/ReportHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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));
Expand Down

0 comments on commit 59569f2

Please sign in to comment.