Skip to content

Commit

Permalink
Release 0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
weiqiangliu committed Nov 9, 2021
1 parent cd3ff87 commit 5294feb
Show file tree
Hide file tree
Showing 16 changed files with 508 additions and 181 deletions.
4 changes: 2 additions & 2 deletions SensorsABTestSDK/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
apply plugin: 'signing'
apply plugin: 'maven-publish'

version = "0.1.0"
version = "0.1.1"
android {
compileSdkVersion 29

Expand Down Expand Up @@ -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:5.2.1'
compileOnly 'com.sensorsdata.analytics.android:SensorsAnalyticsSDK:6.0.0'
}

task sourceJar(type: Jar) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ interface ISensorsABTestApi {
*/
<T> void asyncFetchABTest(String paramName, T defaultValue, int timeoutMillSeconds, OnABTestReceivedData<T> callBack);

/**
* 始终从网络请求试验结果,可自定义属性和超时时间
*
* @param experiment 试验参数
* @param callBack 接口回调
* @param <T> 默认值类型
*/
<T> void asyncFetchABTest(SensorsABTestExperiment<T> experiment, OnABTestReceivedData<T> callBack);

/**
* 如果本地有缓存,则返回缓存数据;否则从网络请求最新的试验数据,默认 30s 超时时间
*
Expand All @@ -70,4 +79,13 @@ interface ISensorsABTestApi {
* @param <T> 默认值类型
*/
<T> void fastFetchABTest(String paramName, T defaultValue, int timeoutMillSeconds, OnABTestReceivedData<T> callBack);

/**
* 如果本地有缓存,则返回缓存数据;否则从网络请求最新的试验数据。可自定义属性和超时时间
*
* @param experiment 试验参数
* @param callBack 接口回调
* @param <T> 默认值类型
*/
<T> void fastFetchABTest(SensorsABTestExperiment<T> experiment, OnABTestReceivedData<T> callBack);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import android.text.TextUtils;
import android.util.Log;

import com.sensorsdata.abtest.core.SensorsABTestHelper;
import com.sensorsdata.abtest.core.SABErrorDispatcher;
import com.sensorsdata.abtest.core.SensorsABTestApiRequestHelper;
import com.sensorsdata.abtest.core.SensorsABTestCacheManager;
import com.sensorsdata.abtest.core.SensorsABTestHelper;
import com.sensorsdata.abtest.entity.SABErrorEnum;
import com.sensorsdata.abtest.util.AppInfoUtils;
import com.sensorsdata.abtest.util.TaskRunner;
Expand All @@ -34,13 +34,14 @@
import com.sensorsdata.analytics.android.sdk.TrackTaskManager;

import java.lang.reflect.Method;
import java.util.Map;

public class SensorsABTest implements ISensorsABTestApi {

private static final String TAG = "SAB.SensorsABTest";
private static SensorsABTest sInstance;
// 默认请求超时时间
private static final int TIMEOUT_REQUEST = 30 * 1000;
protected static final int TIMEOUT_REQUEST = 30 * 1000;
private SensorsABTestConfigOptions mConfigOptions;
private Context mContext;

Expand Down Expand Up @@ -139,57 +140,56 @@ public <T> T fetchCacheABTest(String paramName, T defaultValue) {

@Override
public <T> void asyncFetchABTest(String paramName, T defaultValue, OnABTestReceivedData<T> callBack) {
try {
asyncFetchABTest(paramName, defaultValue, TIMEOUT_REQUEST, callBack);
} catch (Exception e) {
SALog.printStackTrace(e);
}
asyncFetchABTestInner(paramName, defaultValue, null, TIMEOUT_REQUEST, callBack);
}

@Override
public <T> void asyncFetchABTest(final String paramName, final T defaultValue, final int timeoutMillSeconds, final OnABTestReceivedData<T> callBack) {
try {
addTrackEventTask(new Runnable() {
@Override
public void run() {
asyncFetchABTestInner(paramName, defaultValue, timeoutMillSeconds, callBack);
}
});
} catch (Exception e) {
SALog.printStackTrace(e);
}
public <T> void asyncFetchABTest(String paramName, T defaultValue, int timeoutMillSeconds, OnABTestReceivedData<T> callBack) {
asyncFetchABTestInner(paramName, defaultValue, null, timeoutMillSeconds, callBack);
}

private <T> void asyncFetchABTestInner(final String paramName, final T defaultValue, int timeoutMillSeconds, final OnABTestReceivedData<T> callBack) {
try {
if (timeoutMillSeconds > 0) {
SALog.i(TAG, "timeoutMillSeconds minimum value is 1000ms");
timeoutMillSeconds = Math.max(1000, timeoutMillSeconds);
} else {
SALog.i(TAG, "timeoutMillSeconds params is not valid: <= 0 and set default value: " + TIMEOUT_REQUEST);
timeoutMillSeconds = TIMEOUT_REQUEST;
}
SALog.i(TAG, "asyncFetchABTest request param name: " + paramName + ",default value: " + defaultValue + ",timeoutMillSeconds: " + timeoutMillSeconds);
final String distinctId = SensorsDataAPI.sharedInstance().getDistinctId();
final String loginId = SensorsDataAPI.sharedInstance().getLoginId();
final String anonymousId = SensorsDataAPI.sharedInstance().getAnonymousId();
new SensorsABTestApiRequestHelper<T>().requestExperimentByParamName(distinctId, loginId, anonymousId, paramName, defaultValue, timeoutMillSeconds, callBack);
} catch (Exception e) {
SALog.printStackTrace(e);
@Override
public <T> void asyncFetchABTest(SensorsABTestExperiment<T> experiment, OnABTestReceivedData<T> callBack) {
if (experiment == null) {
SALog.i(TAG, "experiment is null, check your param please!");
return;
}
asyncFetchABTestInner(experiment.paramName, experiment.defaultValue, experiment.properties, experiment.timeoutMillSeconds, callBack);
}

@Override
public <T> void fastFetchABTest(String paramName, T defaultValue, OnABTestReceivedData<T> callBack) {
fastFetchABTestInner(paramName, defaultValue, null, TIMEOUT_REQUEST, callBack);
}

@Override
public <T> void fastFetchABTest(String paramName, T defaultValue, int timeoutMillSeconds, OnABTestReceivedData<T> callBack) {
fastFetchABTestInner(paramName, defaultValue, null, timeoutMillSeconds, callBack);
}

@Override
public <T> void fastFetchABTest(SensorsABTestExperiment<T> experiment, OnABTestReceivedData<T> callBack) {
if (experiment == null) {
SALog.i(TAG, "experiment is null, check your param please!");
return;
}
fastFetchABTestInner(experiment.paramName, experiment.defaultValue, experiment.properties, experiment.timeoutMillSeconds, callBack);
}

private <T> void asyncFetchABTestInner(final String paramName, final T defaultValue, final Map<String, Object> properties, final int timeoutMillSeconds, final OnABTestReceivedData<T> callBack) {
try {
fastFetchABTest(paramName, defaultValue, TIMEOUT_REQUEST, callBack);
addTrackEventTask(new Runnable() {
@Override
public void run() {
requestExperimentWithParams(paramName, defaultValue, properties, timeoutMillSeconds, callBack);
}
});
} catch (Exception e) {
SALog.printStackTrace(e);
}
}

@Override
public <T> void fastFetchABTest(final String paramName, final T defaultValue, final int timeoutMillSeconds, final OnABTestReceivedData<T> callBack) {
private <T> void fastFetchABTestInner(final String paramName, final T defaultValue, final Map<String, Object> properties, final int timeoutMillSeconds, final OnABTestReceivedData<T> callBack) {
try {
addTrackEventTask(new Runnable() {
@Override
Expand All @@ -204,7 +204,7 @@ public void run() {
}
});
} else {
asyncFetchABTestInner(paramName, defaultValue, timeoutMillSeconds, callBack);
requestExperimentWithParams(paramName, defaultValue, properties, timeoutMillSeconds, callBack);
}
} catch (Exception e) {
SALog.printStackTrace(e);
Expand All @@ -216,6 +216,25 @@ public void run() {
}
}

private <T> void requestExperimentWithParams(final String paramName, final T defaultValue, Map<String, Object> properties, int timeoutMillSeconds, final OnABTestReceivedData<T> callBack) {
try {
if (timeoutMillSeconds > 0) {
SALog.i(TAG, "timeoutMillSeconds minimum value is 1000ms");
timeoutMillSeconds = Math.max(1000, timeoutMillSeconds);
} else {
SALog.i(TAG, "timeoutMillSeconds params is not valid: <= 0 and set default value: " + TIMEOUT_REQUEST);
timeoutMillSeconds = TIMEOUT_REQUEST;
}
SALog.i(TAG, "asyncFetchABTest request param name: " + paramName + ",default value: " + defaultValue + ",timeoutMillSeconds: " + timeoutMillSeconds);
final String distinctId = SensorsDataAPI.sharedInstance().getDistinctId();
final String loginId = SensorsDataAPI.sharedInstance().getLoginId();
final String anonymousId = SensorsDataAPI.sharedInstance().getAnonymousId();
new SensorsABTestApiRequestHelper<T>().requestExperimentByParamName(distinctId, loginId, anonymousId, paramName, defaultValue, properties, timeoutMillSeconds, callBack);
} catch (Exception e) {
SALog.printStackTrace(e);
}
}

private static void addTrackEventTask(Runnable runnable) {
try {
Object obj = TrackTaskManager.getInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,19 @@ public <T> void asyncFetchABTest(String paramName, T defaultValue, OnABTestRecei
public <T> void asyncFetchABTest(String paramName, T defaultValue, int timeoutMillSeconds, OnABTestReceivedData<T> callBack) {
}

@Override
public <T> void asyncFetchABTest(SensorsABTestExperiment<T> experiment, OnABTestReceivedData<T> callBack) {
}

@Override
public <T> void fastFetchABTest(String paramName, T defaultValue, OnABTestReceivedData<T> callBack) {
}

@Override
public <T> void fastFetchABTest(String paramName, T defaultValue, int timeoutMillSeconds, OnABTestReceivedData<T> callBack) {
}

@Override
public <T> void fastFetchABTest(SensorsABTestExperiment<T> experiment, OnABTestReceivedData<T> callBack) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Created by luweibin on 2021/10/21.
* Copyright 2015-2021 Sensors Data Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.sensorsdata.abtest;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SensorsABTestExperiment<T> {
protected final String paramName;
protected final T defaultValue;
protected Map<String, Object> properties;
protected int timeoutMillSeconds = SensorsABTest.TIMEOUT_REQUEST;

private SensorsABTestExperiment(String paramName, T defaultValue) {
this.paramName = paramName;
this.defaultValue = defaultValue;
}

@Override
public String toString() {
return "SensorsABTestExperiment{" +
"paramName='" + paramName + '\'' +
", defaultValue=" + defaultValue +
", properties=" + properties +
", timeoutMillSeconds=" + timeoutMillSeconds +
'}';
}

public static <T> ExperimentBuilder<T> newBuilder(String paramName, T defaultValue) {
return new ExperimentBuilder<>(paramName, defaultValue);
}

public static class ExperimentBuilder<T> {
private final SensorsABTestExperiment<T> experiment;

private ExperimentBuilder(String paramName, T defaultValue) {
experiment = new SensorsABTestExperiment<>(paramName, defaultValue);
}

public ExperimentBuilder<T> addProperty(String propertyKey, CharSequence propertyValue) {
addObjectProperty(propertyKey, propertyValue);
return this;
}

public ExperimentBuilder<T> addProperty(String propertyKey, boolean propertyValue) {
addObjectProperty(propertyKey, propertyValue);
return this;
}

public ExperimentBuilder<T> addProperty(String propertyKey, Number propertyValue) {
addObjectProperty(propertyKey, propertyValue);
return this;
}

public ExperimentBuilder<T> addProperty(String propertyKey, List<String> propertyValue) {
addObjectProperty(propertyKey, propertyValue);
return this;
}

public ExperimentBuilder<T> addProperty(String propertyKey, Date propertyValue) {
addObjectProperty(propertyKey, propertyValue);
return this;
}

private void addObjectProperty(String propertyKey, Object propertyValue) {
if (experiment.properties == null) {
experiment.properties = new HashMap<>();
}
experiment.properties.put(propertyKey, propertyValue);
}

public ExperimentBuilder<T> setTimeoutMillSeconds(int timeoutMillSeconds) {
experiment.timeoutMillSeconds = timeoutMillSeconds;
return this;
}

public SensorsABTestExperiment<T> create() {
return experiment;
}
}
}
Loading

0 comments on commit 5294feb

Please sign in to comment.