Skip to content

Commit 6459993

Browse files
author
jordanqin
committed
update qcloud sdk to 1.5.56
1 parent 050c9b7 commit 6459993

File tree

11 files changed

+233
-11
lines changed

11 files changed

+233
-11
lines changed

QCloudFoundation/foundation/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ android {
77
minSdkVersion 15
88
targetSdkVersion 28
99

10-
versionCode 10555
11-
versionName "1.5.55"
10+
versionCode 10556
11+
versionName "1.5.56"
1212

1313
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
1414

QCloudFoundation/foundation/src/main/java/com/tencent/qcloud/core/http/StreamingRequestBody.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ static StreamingRequestBody uri(Uri uri, ContentResolver contentResolver, String
143143
}
144144

145145
boolean isLargeData() {
146-
return file != null || stream != null;
146+
return file != null || uri != null || stream != null;
147147
}
148148

149149
@Override

QCloudFoundation/foundation/src/main/java/com/tencent/qcloud/core/http/interceptor/TrafficControlInterceptor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.tencent.qcloud.core.http.HttpTask;
3030
import com.tencent.qcloud.core.http.HttpUtil;
3131
import com.tencent.qcloud.core.logger.QCloudLogger;
32+
import com.tencent.qcloud.core.task.TaskExecutors;
3233
import com.tencent.qcloud.core.task.TaskManager;
3334

3435
import java.io.IOException;
@@ -41,6 +42,9 @@
4142
import okhttp3.Response;
4243

4344
public class TrafficControlInterceptor implements Interceptor {
45+
private TrafficStrategy uploadTrafficStrategy = new ModerateTrafficStrategy("UploadStrategy-", TaskExecutors.UPLOAD_THREAD_COUNT);
46+
private TrafficStrategy downloadTrafficStrategy = new AggressiveTrafficStrategy("DownloadStrategy-", TaskExecutors.DOWNLOAD_THREAD_COUNT);
47+
4448
private static class ResizableSemaphore extends Semaphore {
4549

4650
ResizableSemaphore(int permit, boolean fair) {
@@ -168,8 +172,6 @@ private TrafficStrategy getSuitableStrategy(HttpTask task) {
168172
if (!task.isEnableTraffic()) {
169173
return null;
170174
}
171-
TrafficStrategy uploadTrafficStrategy = new ModerateTrafficStrategy("UploadStrategy-", task.getUploadMaxThreadCount());
172-
TrafficStrategy downloadTrafficStrategy = new AggressiveTrafficStrategy("DownloadStrategy-", task.getDownloadMaxThreadCount());
173175
if(task.isDownloadTask()){
174176
return downloadTrafficStrategy;
175177
} else if(task.isUploadTask()){

QCloudFoundation/foundation/src/main/java/com/tencent/qcloud/core/task/TaskExecutors.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@
3232
import java.util.concurrent.atomic.AtomicInteger;
3333

3434
public class TaskExecutors {
35-
public static final int DEFAULT_UPLOAD_THREAD_COUNT = 2;
36-
public static final int DEFAULT_DOWNLOAD_THREAD_COUNT = 5;
35+
// 上传线程数
36+
public static int UPLOAD_THREAD_COUNT = 4;
37+
// 下载线程数
38+
public static int DOWNLOAD_THREAD_COUNT = 6;
3739

3840
public static final ThreadPoolExecutor COMMAND_EXECUTOR;
3941

@@ -44,10 +46,13 @@ public class TaskExecutors {
4446
public static final UIThreadExecutor UI_THREAD_EXECUTOR;
4547

4648
public static void initExecutor(int uploadMaxThreadCount, int downloadMaxThreadCount){
47-
UPLOAD_EXECUTOR = new ThreadPoolExecutor(Math.min(DEFAULT_UPLOAD_THREAD_COUNT, uploadMaxThreadCount), uploadMaxThreadCount, 5L,
49+
UPLOAD_THREAD_COUNT = uploadMaxThreadCount;
50+
DOWNLOAD_THREAD_COUNT = downloadMaxThreadCount;
51+
52+
UPLOAD_EXECUTOR = new ThreadPoolExecutor(UPLOAD_THREAD_COUNT, UPLOAD_THREAD_COUNT, 5L,
4853
TimeUnit.SECONDS, new PriorityBlockingQueue<Runnable>(),
4954
new TaskThreadFactory("Upload-", 3));
50-
DOWNLOAD_EXECUTOR = new ThreadPoolExecutor(Math.min(DEFAULT_DOWNLOAD_THREAD_COUNT, downloadMaxThreadCount), downloadMaxThreadCount, 5L,
55+
DOWNLOAD_EXECUTOR = new ThreadPoolExecutor(DOWNLOAD_THREAD_COUNT, DOWNLOAD_THREAD_COUNT, 5L,
5156
TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(Integer.MAX_VALUE),
5257
new TaskThreadFactory("Download-", 3));
5358
UPLOAD_EXECUTOR.allowCoreThreadTimeOut(true);
@@ -58,10 +63,10 @@ public static void initExecutor(int uploadMaxThreadCount, int downloadMaxThreadC
5863
COMMAND_EXECUTOR = new ThreadPoolExecutor(5, 5, 5L,
5964
TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(Integer.MAX_VALUE),
6065
new TaskThreadFactory("Command-", 8));
61-
UPLOAD_EXECUTOR = new ThreadPoolExecutor(DEFAULT_UPLOAD_THREAD_COUNT, DEFAULT_UPLOAD_THREAD_COUNT, 5L,
66+
UPLOAD_EXECUTOR = new ThreadPoolExecutor(UPLOAD_THREAD_COUNT, UPLOAD_THREAD_COUNT, 5L,
6267
TimeUnit.SECONDS, new PriorityBlockingQueue<Runnable>(),
6368
new TaskThreadFactory("Upload-", 3));
64-
DOWNLOAD_EXECUTOR = new ThreadPoolExecutor(DEFAULT_DOWNLOAD_THREAD_COUNT, DEFAULT_DOWNLOAD_THREAD_COUNT, 5L,
69+
DOWNLOAD_EXECUTOR = new ThreadPoolExecutor(DOWNLOAD_THREAD_COUNT, DOWNLOAD_THREAD_COUNT, 5L,
6570
TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(Integer.MAX_VALUE),
6671
new TaskThreadFactory("Download-", 3));
6772
UI_THREAD_EXECUTOR = new UIThreadExecutor();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-keep class net.jpountz.lz4.** { *; }
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.tencent.qcloud.track;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumented test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
23+
assertEquals("com.tencent.qcloud.track.test", appContext.getPackageName());
24+
}
25+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.tencent.qcloud.track">
4+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
5+
</manifest>
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
* Copyright (c) 2010-2020 Tencent Cloud. All rights reserved.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
package com.tencent.qcloud.track;
24+
25+
import android.content.Context;
26+
import android.util.Log;
27+
28+
import com.tencentcloudapi.cls.android.CLSAdapter;
29+
import com.tencentcloudapi.cls.android.CLSConfig;
30+
import com.tencentcloudapi.cls.android.producer.AsyncProducerClient;
31+
import com.tencentcloudapi.cls.android.producer.AsyncProducerConfig;
32+
import com.tencentcloudapi.cls.android.producer.common.LogItem;
33+
import com.tencentcloudapi.cls.android.producer.errors.ProducerException;
34+
import com.tencentcloudapi.cls.android.producer.util.NetworkUtils;
35+
import com.tencentcloudapi.cls.plugin.network_diagnosis.CLSNetDiagnosisPlugin;
36+
37+
import java.util.ArrayList;
38+
import java.util.List;
39+
import java.util.Map;
40+
41+
/**
42+
* 数据追踪服务
43+
*/
44+
public class QCloudTrackService {
45+
private static final String TAG = "TrackService";
46+
47+
private static final String NET_DIAGNOSIS_TOPIC_ID = "net_diagnosis";
48+
49+
private static boolean debug = false;
50+
private static boolean isCloseTrack = false;
51+
private static AsyncProducerClient clsClient;
52+
53+
/**
54+
* 主题前缀
55+
*/
56+
private static String topicIdPrefix = "qcloud_track";
57+
58+
/**
59+
* 初始化
60+
* @param context context
61+
* @param endpoint 接入点
62+
* @param secretId API密钥 secretId
63+
* @param secretKey API密钥 secretKey
64+
* @param secretToken API密钥 secretToken
65+
* @param debug 是否是调试模式 会输出日志
66+
* @param isCloseTrack 是否关闭数据上报
67+
*/
68+
public static void init(Context context,
69+
String topicIdPrefix,
70+
String endpoint, String secretId, String secretKey, String secretToken,
71+
boolean debug, boolean isCloseTrack) {
72+
QCloudTrackService.topicIdPrefix = topicIdPrefix;
73+
QCloudTrackService.debug = debug;
74+
QCloudTrackService.isCloseTrack = isCloseTrack;
75+
// NetworkUtils.getLocalMachineIP() 获取本地网卡ip,如果不指定,默认填充服务端接收到的网络出口ip
76+
AsyncProducerConfig config = new AsyncProducerConfig(context.getApplicationContext(), endpoint, secretId, secretKey, secretToken, NetworkUtils.getLocalMachineIP());
77+
// 构建一个客户端实例
78+
QCloudTrackService.clsClient = new AsyncProducerClient(config);
79+
80+
// 添加网络探测插件
81+
CLSAdapter adapter = CLSAdapter.getInstance();
82+
adapter.addPlugin(new CLSNetDiagnosisPlugin());
83+
CLSConfig clsConfig = new CLSConfig(context.getApplicationContext());
84+
clsConfig.endpoint = endpoint;
85+
clsConfig.accessKeyId = secretId;
86+
clsConfig.accessKeySecret = secretKey;
87+
clsConfig.securityToken = secretToken;
88+
clsConfig.topicId = topicIdPrefix + "_" + NET_DIAGNOSIS_TOPIC_ID;
89+
// 发布时,建议关闭,即配置为config.debuggable = false。
90+
clsConfig.debuggable = debug;
91+
adapter.init(clsConfig);
92+
}
93+
94+
public static void track(String eventCode, Map<String, String> params) {
95+
if (QCloudTrackService.isCloseTrack || !isIncludeTrackService()) return;
96+
97+
if (clsClient == null) {
98+
throw new IllegalArgumentException("Please call the init method of TrackService first");
99+
}
100+
101+
for (int i = 0; i < 10000; ++i) {
102+
List<LogItem> logItems = new ArrayList<>();
103+
LogItem logItem = new LogItem();
104+
for (String key : params.keySet()){
105+
logItem.PushBack(key, params.get(key));
106+
}
107+
logItems.add(logItem);
108+
try {
109+
clsClient.putLogs(QCloudTrackService.topicIdPrefix + "_" + eventCode, logItems,
110+
result -> {
111+
if (debug) {
112+
StringBuilder mapAsString = new StringBuilder("{");
113+
for (String key : params.keySet()) {
114+
mapAsString.append(key).append("=").append(params.get(key)).append(", ");
115+
}
116+
mapAsString.delete(mapAsString.length() - 2, mapAsString.length()).append("}");
117+
Log.i(TAG, String.format("eventCode: %s, params: %s => result: %s", QCloudTrackService.topicIdPrefix + "_" + eventCode, mapAsString, result.toString()));
118+
}
119+
}
120+
);
121+
} catch (InterruptedException | ProducerException e) {
122+
e.printStackTrace();
123+
}
124+
}
125+
}
126+
127+
// TODO: 2023/6/21 添加适合sdk用的网络探测方法
128+
129+
/**
130+
* 设置是否关闭数据上报
131+
* @param isCloseTrack 是否关闭数据上报
132+
*/
133+
public static void setIsCloseTrack(boolean isCloseTrack) {
134+
QCloudTrackService.isCloseTrack = isCloseTrack;
135+
}
136+
137+
private static boolean isIncludeTrackService() {
138+
try {
139+
Class.forName("com.tencent.qcloud.track.QCloudTrackService");
140+
return true;
141+
} catch (ClassNotFoundException e) {
142+
return false;
143+
}
144+
}
145+
}

0 commit comments

Comments
 (0)