Skip to content

Commit

Permalink
Merge pull request #29 from lemondark/release/v4.9.0
Browse files Browse the repository at this point in the history
Release/v4.9.0
  • Loading branch information
ilovetochangetheworld authored Mar 18, 2024
2 parents a8d41f6 + 61dc596 commit 7e9d2de
Show file tree
Hide file tree
Showing 12 changed files with 311 additions and 203 deletions.
4 changes: 0 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,10 @@ configurations {

dependencies {
implementation 'androidx.annotation:annotation:1.1.0'
// implementation files('libs/beacon-android-release-3.1.2-cover.jar')
testImplementation 'junit:junit:4.12'
androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'androidx.room:room-runtime:2.2.0'
annotationProcessor 'androidx.room:room-compiler:2.2.0'

doclava 'com.google.doclava:doclava:1.0.6'
}

Expand Down
91 changes: 45 additions & 46 deletions src/main/java/com/tencent/msdk/dns/DnsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.tencent.msdk.dns.core.LookupParameters;
import com.tencent.msdk.dns.core.LookupResult;
import com.tencent.msdk.dns.core.cache.Cache;
import com.tencent.msdk.dns.core.cache.database.LookupCacheDatabase;
import com.tencent.msdk.dns.core.rest.share.LookupExtra;
import com.tencent.msdk.dns.core.stat.StatisticsMerge;
import com.tencent.msdk.dns.report.CacheStatisticsReport;
Expand Down Expand Up @@ -63,50 +62,53 @@ public static Context getAppContext() {
* @throws IllegalArgumentException context为null时抛出
*/
public static void init(Context context, /* @Nullable */DnsConfig config) {
// NOTE: 参数检查不封装为通用方法, 是为了避免不必要的concat执行
if (null == context) {
throw new IllegalArgumentException("context".concat(Const.NULL_POINTER_TIPS));
}
if (null == config) {
config = new DnsConfig.Builder().build();
}
// NOTE: 在开始打日志之前设置日志开关
DnsLog.setLogLevel(config.logLevel);
addLogNodes(config.logNodes);
DnsLog.v("DnsService.init(%s, %s) called, ver:%s", context, config, BuildConfig.VERSION_NAME);
Context appContext = context.getApplicationContext();
sAppContext = appContext;
sConfig = config;
// 底层配置获取
DnsExecutors.WORK.execute(new Runnable() {
@Override
public void run() {
ConfigFromServer.init(sConfig.lookupExtra, sConfig.channel);
try {
// NOTE: 参数检查不封装为通用方法, 是为了避免不必要的concat执行
if (null == context) {
throw new IllegalArgumentException("context".concat(Const.NULL_POINTER_TIPS));
}
});
// 初始化容灾服务
BackupResolver.getInstance().init(sConfig);
// 初始化SpendHelper配置为正常上报做准备
SpendReportResolver.getInstance().init();
NetworkChangeManager.install(appContext);
ActivityLifecycleDetector.install(appContext);
// Room 本地数据读取
DnsExecutors.WORK.execute(new Runnable() {
@Override
public void run() {
Cache.getInstance().readFromDb();
if (null == config) {
config = new DnsConfig.Builder().build();
}
});
ReportHelper.init(config);
DnsExecutors.sExecutorSupplier = sConfig.executorSupplier;
setLookedUpListener(config.lookedUpListener);

// NOTE: addReporters需保证在ReportManager init之后调用
addReporters(config.reporters);

sInited = true;

preLookupAndStartAsyncLookup();
// NOTE: 在开始打日志之前设置日志开关
DnsLog.setLogLevel(config.logLevel);
addLogNodes(config.logNodes);
DnsLog.v("DnsService.init(%s, %s) called, ver:%s", context, config, BuildConfig.VERSION_NAME);
Context appContext = context.getApplicationContext();
sAppContext = appContext;
sConfig = config;
// 底层配置获取
DnsExecutors.WORK.execute(new Runnable() {
@Override
public void run() {
ConfigFromServer.init(sConfig.lookupExtra, sConfig.channel);
}
});
// 初始化容灾服务
BackupResolver.getInstance().init(sConfig);
// 初始化SpendHelper配置为正常上报做准备
SpendReportResolver.getInstance().init();
NetworkChangeManager.install(appContext);
ActivityLifecycleDetector.install(appContext);
// Room 本地数据读取
DnsExecutors.WORK.execute(new Runnable() {
@Override
public void run() {
Cache.getInstance().readFromDb();
}
});
ReportHelper.init(config);
DnsExecutors.sExecutorSupplier = sConfig.executorSupplier;
setLookedUpListener(config.lookedUpListener);

// NOTE: addReporters需保证在ReportManager init之后调用
addReporters(config.reporters);

sInited = true;
preLookupAndStartAsyncLookup();
} catch (Exception e) {
DnsLog.w("DnsService.init failed: %s", e);
}
}

/**
Expand Down Expand Up @@ -163,9 +165,6 @@ public static synchronized void setCachedIpEnable(boolean mCachedIpEnable) {
throw new IllegalStateException("DnsService".concat(Const.NOT_INIT_TIPS));
}
sConfig.cachedIpEnable = mCachedIpEnable;
if (mCachedIpEnable) {
LookupCacheDatabase.creat(sAppContext);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@

public final class NetworkChangeManager {

private static INetworkChangeObservable sNetworkChangeObservable = null;
private static volatile INetworkChangeObservable sNetworkChangeObservable;

public static void install(Context context) {
if (null == context) {
DnsLog.w("Install network change manager failed: context can not be null");
return;
}
synchronized (NetworkChangeManager.class) {
if (null == sNetworkChangeObservable) {
sNetworkChangeObservable = new NetworkChangeObservableFactory().create(context);
if (null == sNetworkChangeObservable) {
synchronized (NetworkChangeManager.class) {
if (null == sNetworkChangeObservable) {
sNetworkChangeObservable = new NetworkChangeObservableFactory().create(context);

}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import android.content.IntentFilter;
import android.net.ConnectivityManager;

import com.tencent.msdk.dns.base.log.DnsLog;

final class NetworkChangeObservableImpl extends AbsNetworkChangeObservable {

NetworkChangeObservableImpl(Context context) {
Expand All @@ -14,13 +16,17 @@ final class NetworkChangeObservableImpl extends AbsNetworkChangeObservable {
if (null == context) {
return;
}
final Context appContext = context.getApplicationContext();
appContext.registerReceiver(new BroadcastReceiver() {
try {
final Context appContext = context.getApplicationContext();
appContext.registerReceiver(new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
mayChangeNetwork(appContext);
}
}, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
@Override
public void onReceive(Context context, Intent intent) {
mayChangeNetwork(appContext);
}
}, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
} catch (Exception e) {
DnsLog.w("network register failed " + e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,60 +25,64 @@ final class NetworkChangeObservableV21Impl extends AbsNetworkChangeObservable {
if (null == connectivityManager) {
return;
}
NetworkRequest allNetworkRequest =
new NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.addTransportType(NetworkCapabilities.TRANSPORT_BLUETOOTH)
.addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET)
.build();
connectivityManager.registerNetworkCallback(allNetworkRequest,
new ConnectivityManager.NetworkCallback() {
try {
NetworkRequest allNetworkRequest =
new NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.addTransportType(NetworkCapabilities.TRANSPORT_BLUETOOTH)
.addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET)
.build();
connectivityManager.registerNetworkCallback(allNetworkRequest,
new ConnectivityManager.NetworkCallback() {

// NOTE:存在一次切网多次调用的情况
// NOTE: 当前不好确认只在回调onAvailable和onLost时驱动切网逻辑是否能覆盖所有切网情况, 日志记录所有回调时机方便后续定位问题
// NOTE:存在一次切网多次调用的情况
// NOTE: 当前不好确认只在回调onAvailable和onLost时驱动切网逻辑是否能覆盖所有切网情况, 日志记录所有回调时机方便后续定位问题

@Override
public void onAvailable(Network network) {
super.onAvailable(network);
DnsLog.d("Network onAvailable(%s)", network);
@Override
public void onAvailable(Network network) {
super.onAvailable(network);
DnsLog.d("Network onAvailable(%s)", network);

mayChangeNetwork(appContext);
}
mayChangeNetwork(appContext);
}

@Override
public void onLosing(Network network, int maxMsToLive) {
super.onLosing(network, maxMsToLive);
DnsLog.d("Network onLosing(%s)", network);
}
@Override
public void onLosing(Network network, int maxMsToLive) {
super.onLosing(network, maxMsToLive);
DnsLog.d("Network onLosing(%s)", network);
}

@Override
public void onLost(Network network) {
super.onLost(network);
DnsLog.d("Network onLost(%s)", network);
@Override
public void onLost(Network network) {
super.onLost(network);
DnsLog.d("Network onLost(%s)", network);

mayChangeNetwork(appContext);
}
mayChangeNetwork(appContext);
}

@Override
public void onUnavailable() {
super.onUnavailable();
DnsLog.d("Network onUnavailable");
}
@Override
public void onUnavailable() {
super.onUnavailable();
DnsLog.d("Network onUnavailable");
}

@Override
public void onCapabilitiesChanged(Network network,
NetworkCapabilities networkCapabilities) {
super.onCapabilitiesChanged(network, networkCapabilities);
DnsLog.d("Network onCapabilitiesChanged(%s)", network);
}
@Override
public void onCapabilitiesChanged(Network network,
NetworkCapabilities networkCapabilities) {
super.onCapabilitiesChanged(network, networkCapabilities);
DnsLog.d("Network onCapabilitiesChanged(%s)", network);
}

@Override
public void onLinkPropertiesChanged(Network network,
LinkProperties linkProperties) {
super.onLinkPropertiesChanged(network, linkProperties);
DnsLog.d("Network onLinkPropertiesChanged(%s)", network);
}
});
@Override
public void onLinkPropertiesChanged(Network network,
LinkProperties linkProperties) {
super.onLinkPropertiesChanged(network, linkProperties);
DnsLog.d("Network onLinkPropertiesChanged(%s)", network);
}
});
} catch (Exception e) {
DnsLog.w("network register failed " + e);
}
}
}
21 changes: 10 additions & 11 deletions src/main/java/com/tencent/msdk/dns/core/cache/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
import com.tencent.msdk.dns.core.Const;
import com.tencent.msdk.dns.core.ICache;
import com.tencent.msdk.dns.core.LookupResult;
import com.tencent.msdk.dns.core.cache.database.CacheDbHelper;
import com.tencent.msdk.dns.core.cache.database.LookupCache;
import com.tencent.msdk.dns.core.cache.database.LookupCacheDao;
import com.tencent.msdk.dns.core.cache.database.LookupCacheDatabase;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -20,8 +19,7 @@ public final class Cache implements ICache {

private final Map<String, LookupResult> mHostnameIpsMap = new ConcurrentHashMap<>();

private final LookupCacheDao lookupCacheDao =
LookupCacheDatabase.getInstance(DnsService.getAppContext()).lookupCacheDao();
private final CacheDbHelper cacheDbHelper = new CacheDbHelper(DnsService.getContext());

private boolean getCachedIpEnable() {
return DnsService.getDnsConfig().cachedIpEnable;
Expand All @@ -40,17 +38,18 @@ public static Cache getInstance() {

public void readFromDb() {
if (getCachedIpEnable()) {
List<LookupCache> allCache = lookupCacheDao.getAll();
ArrayList<LookupCache> expired = new ArrayList<>();
List<LookupCache> allCache = cacheDbHelper.getAll();
ArrayList<String> expiredList = new ArrayList<>();
for (LookupCache lookupCache : allCache) {
mHostnameIpsMap.put(lookupCache.hostname, lookupCache.lookupResult);

if (lookupCache.isExpired()) {
expired.add(lookupCache);
expiredList.add(lookupCache.hostname);
}
}
String [] expiredHosts = expiredList.toArray(new String[expiredList.size()]);
// 内存读取后,清空本地已过期的缓存
lookupCacheDao.deleteLookupCaches(expired);
cacheDbHelper.delete(expiredHosts);
}
}

Expand Down Expand Up @@ -90,7 +89,7 @@ public void add(String hostname, LookupResult lookupResult) {
DnsLog.d("Cache %s for %s", lookupResult, hostname);
mHostnameIpsMap.put(hostname, lookupResult);
if (getCachedIpEnable()) {
lookupCacheDao.insertLookupCache(new LookupCache(hostname, lookupResult));
cacheDbHelper.insert(new LookupCache(hostname, lookupResult));
}
}

Expand All @@ -103,7 +102,7 @@ public void delete(String hostname) {
mHostnameIpsMap.remove(hostname);

if (getCachedIpEnable()) {
lookupCacheDao.delete(hostname);
cacheDbHelper.delete(hostname);
}

}
Expand All @@ -113,7 +112,7 @@ public void clear() {
mHostnameIpsMap.clear();

if (getCachedIpEnable()) {
lookupCacheDao.clear();
cacheDbHelper.clear();
}
}
}
Loading

0 comments on commit 7e9d2de

Please sign in to comment.