From d507c6eb14432a4e7cf8ef800bf86bea0a69b349 Mon Sep 17 00:00:00 2001 From: weiqiangliu Date: Fri, 27 May 2022 11:03:40 +0800 Subject: [PATCH] Release 0.2.3 --- SensorsABTestSDK/build.gradle | 4 +-- .../abtest/SensorsDataVersionConfig.java | 19 ++++++++++ .../core/SensorsABTestApiRequestHelper.java | 36 +++++++++++++++---- .../abtest/core/SensorsABTestH5Helper.java | 6 +++- .../sensorsdata/abtest/util/AppInfoUtils.java | 6 +++- app/build.gradle | 2 +- 6 files changed, 62 insertions(+), 11 deletions(-) create mode 100644 SensorsABTestSDK/src/main/java/com/sensorsdata/abtest/SensorsDataVersionConfig.java diff --git a/SensorsABTestSDK/build.gradle b/SensorsABTestSDK/build.gradle index 580731e..023fb60 100644 --- a/SensorsABTestSDK/build.gradle +++ b/SensorsABTestSDK/build.gradle @@ -2,7 +2,7 @@ apply plugin: 'com.android.library' apply plugin: 'signing' apply plugin: 'maven-publish' -version = "0.2.2" +version = "0.2.3" android { compileSdkVersion 29 @@ -47,7 +47,7 @@ dependencies { testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' - compileOnly 'com.sensorsdata.analytics.android:SensorsAnalyticsSDK:6.3.3' + compileOnly 'com.sensorsdata.analytics.android:SensorsAnalyticsSDK:6.3.4' // compileOnly files('libs/SensorsAnalyticsSDK-6.2.0.aar') } diff --git a/SensorsABTestSDK/src/main/java/com/sensorsdata/abtest/SensorsDataVersionConfig.java b/SensorsABTestSDK/src/main/java/com/sensorsdata/abtest/SensorsDataVersionConfig.java new file mode 100644 index 0000000..cd70f6c --- /dev/null +++ b/SensorsABTestSDK/src/main/java/com/sensorsdata/abtest/SensorsDataVersionConfig.java @@ -0,0 +1,19 @@ +package com.sensorsdata.abtest; + +public class SensorsDataVersionConfig { + /** + * 当前 SDK 的版本 + */ + public static final String SDK_VERSION = "0.2.3"; + + /** + * 当前 SDK 所要依赖的版本,可以为 JSONArray + */ + public static final String DEPENDENT_SDK_VERSIONS = "[\n" + + " {\n" + + " \"DEPENDENT_MIN_SDK_VERSIONS\":\"6.3.4\",\n" + + " \"SDK_VERSION_PATH\":\"com.sensorsdata.analytics.android.sdk.SensorsDataAPI\",\n" + + " \"ERROR_MESSAGE\":\"当前 SA SDK 版本 %s 过低,请升级至 %s 及其以上版本后进行使用\"\n" + + " }\n" + + "]"; +} diff --git a/SensorsABTestSDK/src/main/java/com/sensorsdata/abtest/core/SensorsABTestApiRequestHelper.java b/SensorsABTestSDK/src/main/java/com/sensorsdata/abtest/core/SensorsABTestApiRequestHelper.java index 867e8f9..dfac1f8 100644 --- a/SensorsABTestSDK/src/main/java/com/sensorsdata/abtest/core/SensorsABTestApiRequestHelper.java +++ b/SensorsABTestSDK/src/main/java/com/sensorsdata/abtest/core/SensorsABTestApiRequestHelper.java @@ -31,6 +31,7 @@ import com.sensorsdata.abtest.entity.RequestingExperimentInfo; import com.sensorsdata.abtest.entity.SABErrorEnum; import com.sensorsdata.abtest.exception.DataInvalidException; +import com.sensorsdata.abtest.util.AppInfoUtils; import com.sensorsdata.abtest.util.CommonUtils; import com.sensorsdata.abtest.util.SensorsDataHelper; import com.sensorsdata.abtest.util.TaskRunner; @@ -56,10 +57,14 @@ public class SensorsABTestApiRequestHelper { private static final String TAG = "SAB.SensorsABTestApiRequestHelper"; private boolean mHasCallback = false; private String mUserIdentifier; + public final static int DEFAULT_TIMEOUT = 30 * 1000; + private int timeoutMillSeconds = DEFAULT_TIMEOUT; + private TimeoutRunnable runnable; public void requestExperimentByParamName(final String distinctId, final String loginId, final String anonymousId, final String customIDs, final String paramName, final T defaultValue, Map properties, final int timeoutMillSeconds, final OnABTestReceivedData callBack, boolean mergeRequest) { + setTimeoutMillSeconds(timeoutMillSeconds); // callback 为 null if (callBack == null) { SALog.i(TAG, "试验 callback 不正确,试验 callback 不能为空!"); @@ -126,8 +131,10 @@ public void requestExperimentByParamName(final String distinctId, final String l } // 启动定时器 - final TimeoutRunnable runnable = new TimeoutRunnable(currentTask); - TaskRunner.getBackHandler().postDelayed(runnable, timeoutMillSeconds); + if (!AppInfoUtils.checkSASDKVersionIsValid(AppInfoUtils.SA_TIMEOUT_VALID_VERSION)) { + runnable = new TimeoutRunnable(currentTask); + TaskRunner.getBackHandler().postDelayed(runnable, timeoutMillSeconds); + } mUserIdentifier = CommonUtils.getCurrentUserIdentifier(); requestExperimentsAndUpdateCache(propertiesString, paramName, new IApiCallback>() { @@ -135,7 +142,9 @@ public void requestExperimentByParamName(final String distinctId, final String l public void onSuccess(Map experimentMap) { RequestExperimentTaskRecorderManager.getInstance().removeTask(currentTask); List taskExperimentInfoList = currentTask.getRequestingExperimentList(); - TaskRunner.getBackHandler().removeCallbacks(runnable); + if (!AppInfoUtils.checkSASDKVersionIsValid(AppInfoUtils.SA_TIMEOUT_VALID_VERSION)) { + TaskRunner.getBackHandler().removeCallbacks(runnable); + } if (mHasCallback) { SALog.i(TAG, "Request success! but all callbacks has been returned with default value!"); return; @@ -188,7 +197,9 @@ public void onSuccess(Map experimentMap) { @Override public void onFailure(int errorCode, String message) { - TaskRunner.getBackHandler().removeCallbacks(runnable); + if (!AppInfoUtils.checkSASDKVersionIsValid(AppInfoUtils.SA_TIMEOUT_VALID_VERSION)) { + TaskRunner.getBackHandler().removeCallbacks(runnable); + } RequestExperimentTaskRecorderManager.getInstance().removeTask(currentTask); if (!mHasCallback) { List taskExperimentInfoList = currentTask.getRequestingExperimentList(); @@ -232,8 +243,16 @@ void requestExperiments(Map properties, String paramName, JSONOb } Map headers = new HashMap<>(); headers.put("project-key", key); - new RequestHelper.Builder(HttpMethod.POST, url) - .header(headers) + RequestHelper.Builder builder = new RequestHelper.Builder(HttpMethod.POST, url); + if (AppInfoUtils.checkSASDKVersionIsValid(AppInfoUtils.SA_TIMEOUT_VALID_VERSION)) { + try { + builder.connectionTimeout(timeoutMillSeconds); + builder.readTimeout(timeoutMillSeconds); + } catch (NoSuchMethodError e) { + SALog.i(TAG, e.getMessage()); + } + } + builder.header(headers) .jsonData(requestBody) .callback(new HttpCallback.StringCallback() { @Override @@ -355,4 +374,9 @@ public void run() { } } } + + public SensorsABTestApiRequestHelper setTimeoutMillSeconds(int timeoutMillSeconds) { + this.timeoutMillSeconds = timeoutMillSeconds; + return this; + } } diff --git a/SensorsABTestSDK/src/main/java/com/sensorsdata/abtest/core/SensorsABTestH5Helper.java b/SensorsABTestSDK/src/main/java/com/sensorsdata/abtest/core/SensorsABTestH5Helper.java index 7818af4..0bac7fa 100644 --- a/SensorsABTestSDK/src/main/java/com/sensorsdata/abtest/core/SensorsABTestH5Helper.java +++ b/SensorsABTestSDK/src/main/java/com/sensorsdata/abtest/core/SensorsABTestH5Helper.java @@ -67,8 +67,12 @@ public void handlerJSMessage() { JSONObject data = jsonObject.optJSONObject("data"); if (data != null) { request.messageId = data.optString("message_id"); + int timeout = data.optInt("timeout"); + if (timeout <= 0) { + timeout = SensorsABTestApiRequestHelper.DEFAULT_TIMEOUT; + } final String distinctId = SensorsDataAPI.sharedInstance().getDistinctId(); - new SensorsABTestApiRequestHelper<>().requestExperiments(null, null, data.optJSONObject("request_body"), new IApiCallback() { + new SensorsABTestApiRequestHelper<>().setTimeoutMillSeconds(timeout).requestExperiments(null, null, data.optJSONObject("request_body"), new IApiCallback() { @Override public void onSuccess(String s) { if (TextUtils.isEmpty(s)) { diff --git a/SensorsABTestSDK/src/main/java/com/sensorsdata/abtest/util/AppInfoUtils.java b/SensorsABTestSDK/src/main/java/com/sensorsdata/abtest/util/AppInfoUtils.java index aeac6ce..3b363e1 100644 --- a/SensorsABTestSDK/src/main/java/com/sensorsdata/abtest/util/AppInfoUtils.java +++ b/SensorsABTestSDK/src/main/java/com/sensorsdata/abtest/util/AppInfoUtils.java @@ -31,6 +31,10 @@ public class AppInfoUtils { private static final String TAG = "AppInfoUtils"; public static final String MIN_SA_SDK_VERSION = "4.3.6"; public static final String MIN_SA_SECRET_SDK_VERSION = "6.2.0"; + /** + * 网络超时可用版本 + */ + public static final String SA_TIMEOUT_VALID_VERSION = "6.3.4"; public static boolean checkSASDKVersionIsValid() { return checkSASDKVersionIsValid(MIN_SA_SDK_VERSION); @@ -40,7 +44,7 @@ public static boolean checkSASecretVersionIsValid() { return checkSASDKVersionIsValid(MIN_SA_SECRET_SDK_VERSION); } - private static boolean checkSASDKVersionIsValid(String saVersion) { + public static boolean checkSASDKVersionIsValid(String saVersion) { SensorsDataAPI sensorsDataAPI = SensorsDataAPI.sharedInstance(); if (sensorsDataAPI instanceof SensorsDataAPIEmptyImplementation) { Log.e(TAG, "神策 Android 埋点 SDK 未集成或未初始化"); diff --git a/app/build.gradle b/app/build.gradle index d260ed4..006679a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -54,6 +54,6 @@ dependencies { androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' implementation project(path: ':SensorsABTestSDK') implementation 'com.tencent.tbs.tbssdk:sdk:43903' - implementation 'com.sensorsdata.analytics.android:SensorsAnalyticsSDK:6.3.3' + implementation 'com.sensorsdata.analytics.android:SensorsAnalyticsSDK:6.3.4' // implementation files('libs/SensorsAnalyticsSDK-6.2.0.aar') } \ No newline at end of file