Skip to content

Commit

Permalink
Release 1.6.17
Browse files Browse the repository at this point in the history
  • Loading branch information
wangzhzh committed Oct 10, 2016
1 parent 031abb0 commit 543f62e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 42 deletions.
2 changes: 1 addition & 1 deletion SensorsAnalyticsSDK/SensorsAnalyticsSDK/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

version = "1.6.16"
version = "1.6.17"

android {
compileSdkVersion 21
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,25 @@
/**
* Created by wangzhuozhou on 16/9/1
*/
public class AppWebViewInterface {
/* package */ class AppWebViewInterface {
private static final String LOGTAG = "SA.AppWebViewInterface";
private Context mContext;
private JSONObject properties;

AppWebViewInterface(Context c) {
mContext = c;
AppWebViewInterface(Context c, JSONObject p) {
this.mContext = c;
this.properties = p;
}

@JavascriptInterface
public String sensorsdata_call_app() {
try {
JSONObject object = new JSONObject();
object.put("type", "Android");
object.put("distinct_id", SensorsDataAPI.sharedInstance(mContext).getDistinctId());
return object.toString();
if (properties == null) {
properties = new JSONObject();
}
properties.put("type", "Android");
properties.put("distinct_id", SensorsDataAPI.sharedInstance(mContext).getDistinctId());
return properties.toString();
} catch (JSONException e) {
Log.i(LOGTAG, e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import android.util.DisplayMetrics;
import android.util.Log;
import android.webkit.WebView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -105,10 +104,9 @@ String getProfileName() {
}


SensorsDataAPI(Context context, Context activityContext, String serverURL, String configureURL,
SensorsDataAPI(Context context, String serverURL, String configureURL,
String vtrackServerURL, DebugMode debugMode) {
mContext = context;
mActivityContext = activityContext;

final String packageName = context.getApplicationContext().getPackageName();

Expand Down Expand Up @@ -295,8 +293,7 @@ public static SensorsDataAPI sharedInstance(Context context, String serverURL, S

SensorsDataAPI instance = sInstanceMap.get(appContext);
if (null == instance && ConfigurationChecker.checkBasicConfiguration(appContext)) {
instance = new SensorsDataAPI(appContext, context, serverURL, configureUrl, null,
debugMode);
instance = new SensorsDataAPI(appContext, serverURL, configureUrl, null, debugMode);
sInstanceMap.put(appContext, instance);
}

Expand All @@ -316,7 +313,7 @@ public static SensorsDataAPI sharedInstance(Context context, String serverURL, S
* @return SensorsDataAPI单例
*/
public static SensorsDataAPI sharedInstance(Context context, String serverURL,
String configureURL, String vtrackServerURL, DebugMode debugMode) {
String configureURL, String vtrackServerURL, DebugMode debugMode) {
if (null == context) {
return null;
}
Expand All @@ -326,7 +323,7 @@ public static SensorsDataAPI sharedInstance(Context context, String serverURL,

SensorsDataAPI instance = sInstanceMap.get(appContext);
if (null == instance && ConfigurationChecker.checkBasicConfiguration(appContext)) {
instance = new SensorsDataAPI(appContext, context, serverURL, configureURL, vtrackServerURL,
instance = new SensorsDataAPI(appContext, serverURL, configureURL, vtrackServerURL,
debugMode);
sInstanceMap.put(appContext, instance);
}
Expand Down Expand Up @@ -434,14 +431,27 @@ public void enableAutoTrack() {
*/
@SuppressLint(value = {"SetJavaScriptEnabled", "addJavascriptInterface"})
public void showUpWebView(WebView webView, boolean isSupportJellyBean) {
showUpWebView(webView, isSupportJellyBean, null);
}

/**
* 向WebView注入本地方法, 将distinctId传递给当前的WebView
*
* @param webView 当前WebView
* @param isSupportJellyBean 是否支持API level 16及以下的版本。
* 因为API level 16及以下的版本, addJavascriptInterface有安全漏洞,请谨慎使用
* @param properties 用户自定义属性
*/
@SuppressLint(value = {"SetJavaScriptEnabled", "addJavascriptInterface"})
public void showUpWebView(WebView webView, boolean isSupportJellyBean, JSONObject properties) {
if (Build.VERSION.SDK_INT < 17 && !isSupportJellyBean) {
Log.i(LOGTAG, "For applications targeted to API level JELLY_BEAN or below, this feature NOT SUPPORTED");
return;
}

if (webView != null) {
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new AppWebViewInterface(mContext), "SensorsData_APP_JS_Bridge");
webView.addJavascriptInterface(new AppWebViewInterface(mContext, properties), "SensorsData_APP_JS_Bridge");
}
}

Expand Down Expand Up @@ -1168,6 +1178,7 @@ public boolean isProfile() {
private class LifecycleCallbacks implements Application.ActivityLifecycleCallbacks {

private boolean resumeFromBackground = false;
private Integer startedActivityCount = 0;

public LifecycleCallbacks() {
}
Expand All @@ -1178,30 +1189,33 @@ public void onActivityCreated(Activity activity, Bundle bundle) {

@Override
public void onActivityStarted(Activity activity) {
if (activity.getClass().getCanonicalName()
.equals(mActivityContext.getClass().getCanonicalName())) {
// XXX: 注意内部执行顺序
boolean firstStart = mFirstStart.get();
if (firstStart) {
mFirstStart.commit(false);
}
synchronized (startedActivityCount) {
if (startedActivityCount == 0) {
// XXX: 注意内部执行顺序
boolean firstStart = mFirstStart.get();
if (firstStart) {
mFirstStart.commit(false);
}

if (mAutoTrack) {
try {
JSONObject properties = new JSONObject();
properties.put("$resume_from_background", resumeFromBackground);
properties.put("$is_first_time", firstStart);
if (mAutoTrack) {
try {
JSONObject properties = new JSONObject();
properties.put("$resume_from_background", resumeFromBackground);
properties.put("$is_first_time", firstStart);

track("$AppStart", properties);
track("$AppStart", properties);

trackTimer("$AppEnd", TimeUnit.SECONDS);
} catch (InvalidDataException | JSONException e) {
Log.w(LOGTAG, e);
trackTimer("$AppEnd", TimeUnit.SECONDS);
} catch (InvalidDataException | JSONException e) {
Log.w(LOGTAG, e);
}
}

// 下次启动时,从后台恢复
resumeFromBackground = true;
}

// 下次启动时,从后台恢复
resumeFromBackground = true;
startedActivityCount = startedActivityCount + 1;
}
}

Expand Down Expand Up @@ -1236,12 +1250,15 @@ public void onActivityPaused(Activity activity) {

@Override
public void onActivityStopped(Activity activity) {
if (mAutoTrack && activity.getClass().getCanonicalName().equals(mActivityContext.getClass()
.getCanonicalName())) {
try {
track("$AppEnd");
} catch (Exception e) {
Log.w(LOGTAG, e);
synchronized (startedActivityCount) {
startedActivityCount = startedActivityCount - 1;

if (startedActivityCount == 0) {
try {
track("$AppEnd");
} catch (Exception e) {
Log.w(LOGTAG, e);
}
}
}

Expand Down Expand Up @@ -1384,7 +1401,7 @@ private static void mergeJSONObject(final JSONObject source, JSONObject dest)
static final int VTRACK_SUPPORTED_MIN_API = 16;

// SDK版本
static final String VERSION = "1.6.16";
static final String VERSION = "1.6.17";

static Boolean ENABLE_LOG = false;

Expand Down Expand Up @@ -1416,7 +1433,6 @@ private static void mergeJSONObject(final JSONObject source, JSONObject dest)
private JSONObject mLastScreenTrackProperties;

private final Context mContext;
private final Context mActivityContext;
private final AnalyticsMessages mMessages;
private final PersistentDistinctId mDistinctId;
private final PersistentSuperProperties mSuperProperties;
Expand Down

0 comments on commit 543f62e

Please sign in to comment.