Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #21 from bytedance/init_time_optimize
Browse files Browse the repository at this point in the history
initializeSdk optimize + minSdkVersion 17 + custom flush interval
  • Loading branch information
akhiljames committed Oct 19, 2021
2 parents 80a5d45 + ad8ccfd commit 2ff01a0
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 71 deletions.
14 changes: 4 additions & 10 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ buildscript {
repositories {
mavenCentral()
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.2"
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.+'
classpath 'com.android.tools.build:gradle:4.2.2'
}
}

Expand All @@ -23,20 +21,16 @@ allprojects {
repositories {
mavenCentral()
google()
maven {
url "https://dl.bintray.com/bytedancer/bytedance"
}
maven { url 'https://jitpack.io' }
jcenter()
}
}

ext {
versionName = VERSION_NAME

minSdkVersion = 16
targetSdkVersion = 30
compileSdkVersion = 30
minSdkVersion = 17
targetSdkVersion = 31
compileSdkVersion = 31

sourceCompatibilityVersion = JavaVersion.VERSION_1_8
targetCompatibilityVersion = JavaVersion.VERSION_1_8
Expand Down
25 changes: 16 additions & 9 deletions business-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

apply plugin: 'com.android.library'
apply plugin: 'maven-publish'
//group = "com.tiktok"

android {
compileSdkVersion rootProject.ext.compileSdkVersion
Expand All @@ -21,11 +20,19 @@ android {
}

buildTypes {
debug{
buildConfigField("String","VERSION_NAME","\"${defaultConfig.versionName}\"")
}
release {
buildConfigField("String","VERSION_NAME","\"${defaultConfig.versionName}\"")
minifyEnabled false
}
}

buildFeatures {
buildConfig = true
}

compileOptions {
sourceCompatibility rootProject.ext.sourceCompatibilityVersion
targetCompatibility rootProject.ext.targetCompatibilityVersion
Expand All @@ -41,20 +48,20 @@ android {
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])

implementation 'androidx.annotation:annotation:1.1.0'
implementation 'androidx.lifecycle:lifecycle-process:2.2.0'
implementation 'androidx.lifecycle:lifecycle-common-java8:2.2.0'
implementation 'androidx.annotation:annotation:1.2.0'
implementation 'androidx.lifecycle:lifecycle-process:2.3.1'
implementation 'androidx.lifecycle:lifecycle-common-java8:2.3.1'
implementation 'commons-codec:commons-codec:1.11'

testImplementation 'junit:junit:4.13'
testImplementation 'junit:junit:4.13.2'

def powerMockVersion = '2.0.2'
def powerMockVersion = '2.0.9'
testImplementation "org.powermock:powermock-api-mockito2:$powerMockVersion"
testImplementation "org.powermock:powermock-module-junit4:$powerMockVersion"
testImplementation 'org.json:json:20200518'
testImplementation 'org.json:json:20210307'

androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

afterEvaluate {
Expand Down
20 changes: 16 additions & 4 deletions business-core/src/main/java/com/tiktok/TikTokBusinessSdk.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class TikTokBusinessSdk {
/**
* api available version
*/
private static String apiAvailableVersion = "v1.1";
private static String apiAvailableVersion = "v1.2";
/**
* api treckEvent sent Domain
*/
Expand Down Expand Up @@ -111,7 +111,7 @@ public static synchronized void initializeSdk(TTConfig ttConfig) {

TTUserInfo.reset(TikTokBusinessSdk.getApplicationContext(), false);
// the appEventLogger instance will be the main interface to track events
appEventLogger = new TTAppEventLogger(ttConfig.autoEvent, ttConfig.disabledEvents);
appEventLogger = new TTAppEventLogger(ttConfig.autoEvent, ttConfig.disabledEvents, ttConfig.flushTime);
}

/**
Expand Down Expand Up @@ -485,9 +485,12 @@ public static synchronized void logout() {
public static class TTConfig {
/* application context */
private final Application application;
/* api_id for api calls */
/* api_id for api calls, App ID in EM */
private String appId;
/* tt_app_id for api calls, TikTok App ID from EM */
private Long ttAppId;
/* flush time interval in seconds, default 15, 0 -> disabled */
private int flushTime = 15;
/* Access-Token for api calls */
private String accessToken;
/* to enable logs */
Expand All @@ -499,7 +502,7 @@ public static class TTConfig {
/* auto init flag check in manifest */
private boolean autoStart = true;
/* disable custom auto events */
private List<TTConst.AutoEvents> disabledEvents;
private final List<TTConst.AutoEvents> disabledEvents;

/**
* Read configs from <meta-data>
Expand Down Expand Up @@ -634,6 +637,15 @@ public TTConfig disableAdvertiserIDCollection() {
this.advertiserIDCollectionEnable = false;
return this;
}

/**
* to set a custom flush time interval in seconds, defaults to 15
*/
public TTConfig setFlushTimeInterval(int seconds) {
if (seconds < 0) throw new RuntimeException("Invalid Flush interval");
this.flushTime = seconds;
return this;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public class TTAppEventLogger {
static final String TAG = TTAppEventLogger.class.getName();

// every TIME_BUFFER seconds, a flush task will be pushed to the execution queue
private static final int TIME_BUFFER = 15;
private static int TIME_BUFFER;
int counter;
// once THRESHOLD events got accumulated in the memory, a flush task will be pushed to the execution queue
static final int THRESHOLD = 100;
public static final String NETWORK_IS_TURNED_OFF = "SDK can't send tracking events to server, it will be cached locally, and will be sent in batches only after startTracking";
Expand Down Expand Up @@ -72,21 +73,20 @@ public static List<TTAppEvent> getSuccessfulEvents() {
return TTRequest.getSuccessfullySentRequests();
}

public TTAppEventLogger(boolean lifecycleTrackEnable, List<TTConst.AutoEvents> disabledEvents) {
public TTAppEventLogger(boolean lifecycleTrackEnable, List<TTConst.AutoEvents> disabledEvents, int flushTime) {
logger = new TTLogger(TAG, TikTokBusinessSdk.getLogLevel());
this.lifecycleTrackEnable = lifecycleTrackEnable;
this.disabledEvents = disabledEvents;

TIME_BUFFER = flushTime;
counter = flushTime;
lifecycle = ProcessLifecycleOwner.get().getLifecycle();

/** ActivityLifecycleCallbacks & LifecycleObserver */
TTActivityLifecycleCallbacksListener activityLifecycleCallbacks = new TTActivityLifecycleCallbacksListener(this);
this.lifecycle.addObserver(activityLifecycleCallbacks);

/** advertiser id fetch */
autoEventsManager = new TTAutoEventsManager(this);

SystemInfoUtil.initUserAgent();
addToQ(SystemInfoUtil::initUserAgent);
addToQ(TTAppEventsQueue::clearAll);
if (TikTokBusinessSdk.getAccessToken() != null) {
fetchGlobalConfig(0);
Expand All @@ -95,7 +95,6 @@ public TTAppEventLogger(boolean lifecycleTrackEnable, List<TTConst.AutoEvents> d
}
}


/**
* persist events to the disk
*/
Expand All @@ -122,14 +121,16 @@ public void trackPurchase(List<TTPurchaseInfo> purchaseInfos) {
});
}

int counter = 15;

void startScheduler() {
doStartScheduler(TIME_BUFFER, false);
if (TIME_BUFFER != 0) {
doStartScheduler(TIME_BUFFER, false);
}
}

void restartScheduler() {
doStartScheduler(TIME_BUFFER, true);
if (TIME_BUFFER != 0) {
doStartScheduler(TIME_BUFFER, true);
}
}

/**
Expand Down Expand Up @@ -298,8 +299,8 @@ public enum FlushReason {
TIMER, // triggered every 15 seconds
START_UP, // when app is started, flush all the accumulated events
FORCE_FLUSH, // when developer calls flush from app
IDENTIFY,// when calling identify
LOGOUT,//when logging out
IDENTIFY, // when calling identify
LOGOUT, //when logging out
}

private void addToQ(Runnable task) {
Expand Down Expand Up @@ -359,9 +360,9 @@ public void fetchGlobalConfig(int delaySeconds) {
}

JSONObject businessSdkConfig = (JSONObject) requestResult.get("business_sdk_config");
Boolean enableSDK = (Boolean) businessSdkConfig.get("enable_sdk");
String availableVersion = (String) businessSdkConfig.get("available_version");
String trackEventDomain = (String) businessSdkConfig.get("domain");
Boolean enableSDK = businessSdkConfig.getBoolean("enable_sdk");
String availableVersion = businessSdkConfig.getString("available_version");
String trackEventDomain = businessSdkConfig.getString("domain");

TikTokBusinessSdk.setSdkGlobalSwitch(enableSDK);
logger.debug("enable_sdk=" + enableSDK);
Expand All @@ -377,7 +378,7 @@ public void fetchGlobalConfig(int delaySeconds) {
} catch (JSONException e) {
e.printStackTrace();
logger.warn("Errors happened during initGlobalConfig because the structure of api result is not correct");
} catch (Exception e){
} catch (Exception e) {
logger.warn("Errors occurred during initGlobalConfig because of " + e.getMessage());
e.printStackTrace();
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class TTAutoEventsManager {
}

private final TTAppEventLogger appEventLogger;
private TTKeyValueStore store;
private final TTKeyValueStore store;

public TTAutoEventsManager(TTAppEventLogger appEventLogger) {
this.appEventLogger = appEventLogger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public static synchronized List<TTAppEvent> reportAppEvent(JSONObject basePayloa
} else {
try {
JSONObject resultJson = new JSONObject(result);
int code = (Integer) resultJson.getInt("code");
int code = resultJson.getInt("code");

if (code == TTConst.ApiErrorCodes.API_ERROR.code) {
failedEventsToBeDiscarded.addAll(currentBatch);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

import com.tiktok.appevents.TTCrashHandler;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -162,8 +160,6 @@ public static String doPost(String url, Map<String, String> headerParamMap, Stri
// http code is different from the code returned by api
if (responseCode == HttpURLConnection.HTTP_OK) {
result = streamToString(connection.getInputStream());
JSONObject obj = new JSONObject(result);
System.out.println(obj);
}
} catch (Exception e) {
TTCrashHandler.handleCrash(TAG, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.webkit.WebView;
import android.webkit.WebSettings;

import android.webkit.WebView;
import androidx.annotation.RequiresApi;
import com.tiktok.BuildConfig;
import com.tiktok.TikTokBusinessSdk;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;

public class SystemInfoUtil {
Expand Down Expand Up @@ -91,9 +95,10 @@ public static String getLocale() {
private static String userAgent = "";

// invoke it in the mainThread
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public static void initUserAgent() {
try {
userAgent = new WebView(TikTokBusinessSdk.getApplicationContext()).getSettings().getUserAgentString();
userAgent = WebSettings.getDefaultUserAgent(TikTokBusinessSdk.getApplicationContext());
} catch (Exception e) {
userAgent = System.getProperty("http.agent");
}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ android.enableJetifier=true

GROUP=com.tiktok

VERSION_NAME=1.2.4
VERSION_NAME=1.2.6

mavenGroupId = com.tiktok
mavenArtifactId = tiktok-business-android-sdk
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-all.zip
33 changes: 15 additions & 18 deletions samples/TestApp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ android {

defaultConfig {
applicationId "com.example"
minSdkVersion 16
minSdkVersion 17
targetSdkVersion 30
versionCode 1
versionName "0.0.1-alpha"
Expand Down Expand Up @@ -42,24 +42,21 @@ android {
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation project(':business-core')
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.navigation:navigation-fragment:2.3.1'
implementation 'androidx.navigation:navigation-ui:2.3.1'
implementation 'androidx.lifecycle:lifecycle-process:2.2.0'
implementation 'androidx.lifecycle:lifecycle-common-java8:2.2.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.navigation:navigation-fragment:2.3.5'
implementation 'androidx.navigation:navigation-ui:2.3.5'
implementation 'androidx.lifecycle:lifecycle-process:2.3.1'
implementation 'androidx.lifecycle:lifecycle-common-java8:2.3.1'

def billing_version = "3.0.1"
implementation "com.android.billingclient:billing:$billing_version"
implementation 'com.android.billingclient:billing:3.0.3'

def room_version = "2.2.5"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
implementation 'androidx.room:room-runtime:2.3.0'
annotationProcessor 'androidx.room:room-compiler:2.3.0'

testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

}
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
1 change: 1 addition & 0 deletions samples/TestApp/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

<activity
android:name=".App"
android:exported="true"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Loading

0 comments on commit 2ff01a0

Please sign in to comment.