Skip to content

Commit

Permalink
Release 1.6.38
Browse files Browse the repository at this point in the history
  • Loading branch information
wangzhzh committed Mar 22, 2017
1 parent 76ab0f0 commit bdb999d
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 63 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.37"
version = "1.6.38"

android {
compileSdkVersion 21
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import android.text.TextUtils;
import android.util.Log;
import android.webkit.WebView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
Expand Down Expand Up @@ -88,19 +89,17 @@ public void enqueueEventMessage(final String type, final JSONObject eventJson) {
final Message m = Message.obtain();
m.what = FLUSH_QUEUE;

if (SensorsDataUtils.isNetworkAvailable(mContext)) {
if (SensorsDataAPI.sharedInstance(mContext).isDebugMode() || ret ==
DbAdapter.DB_OUT_OF_MEMORY_ERROR) {
if (SensorsDataAPI.sharedInstance(mContext).isDebugMode() || ret ==
DbAdapter.DB_OUT_OF_MEMORY_ERROR) {
mWorker.runMessage(m);
} else {
// track_signup 立即发送
if (type.equals("track_signup") || ret > SensorsDataAPI.sharedInstance(mContext)
.getFlushBulkSize()) {
mWorker.runMessage(m);
} else {
// track_signup 立即发送
if (type.equals("track_signup") || ret > SensorsDataAPI.sharedInstance(mContext)
.getFlushBulkSize()) {
mWorker.runMessage(m);
} else {
final int interval = SensorsDataAPI.sharedInstance(mContext).getFlushInterval();
mWorker.runMessageOnce(m, interval);
}
final int interval = SensorsDataAPI.sharedInstance(mContext).getFlushInterval();
mWorker.runMessageOnce(m, interval);
}
}
}
Expand All @@ -118,10 +117,16 @@ public void checkConfigure(final DecideMessages check) {
}

public void flush() {
final Message m = Message.obtain();
m.what = FLUSH_QUEUE;
try {
if (SensorsDataUtils.isNetworkAvailable(mContext)) {
final Message m = Message.obtain();
m.what = FLUSH_QUEUE;

mWorker.runMessage(m);
mWorker.runMessage(m);
}
} catch (Exception e) {
e.printStackTrace();
}
}

private byte[] slurp(final InputStream inputStream)
Expand All @@ -141,26 +146,30 @@ private byte[] slurp(final InputStream inputStream)

public void sendData() {
int count = 100;
Toast toast = null;
while (count > 0) {
boolean deleteEvents = true;
InputStream in = null;
OutputStream out = null;
BufferedOutputStream bout = null;
HttpURLConnection connection = null;
try {
String[] eventsData;
synchronized (mDbAdapter) {
if (SensorsDataAPI.sharedInstance(mContext).isDebugMode()) {
eventsData = mDbAdapter.generateDataString(DbAdapter.Table.EVENTS, 1);
} else {
eventsData = mDbAdapter.generateDataString(DbAdapter.Table.EVENTS, 50);
}
}
if (eventsData == null) {
return;
String[] eventsData;
synchronized (mDbAdapter) {
if (SensorsDataAPI.sharedInstance(mContext).isDebugMode()) {
eventsData = mDbAdapter.generateDataString(DbAdapter.Table.EVENTS, 1);
} else {
eventsData = mDbAdapter.generateDataString(DbAdapter.Table.EVENTS, 50);
}
}
if (eventsData == null) {
return;
}

final String lastId = eventsData[0];
final String rawMessage = eventsData[1];
final String lastId = eventsData[0];
final String rawMessage = eventsData[1];
String errorMessage = null;

try {

String data;
try {
Expand Down Expand Up @@ -216,7 +225,6 @@ public void sendData() {

String response = new String(responseBody, "UTF-8");


if (SensorsDataAPI.sharedInstance(mContext).isDebugMode()) {
if (responseCode == 200) {
Log.i(LOGTAG, String.format("valid message: %s", rawMessage));
Expand All @@ -234,34 +242,44 @@ public void sendData() {
}
} catch (IOException e) {
throw new ConnectErrorException(e);
} finally {
count = mDbAdapter.cleanupEvents(lastId, DbAdapter.Table.EVENTS);
if (SensorsDataAPI.ENABLE_LOG) {
Log.i(LOGTAG, String.format("Events flushed. [left = %d]", count));
}
}

} catch (ConnectErrorException e) {
Log.w(LOGTAG, "Connection error: " + e.getMessage());
deleteEvents = false;
errorMessage = "Connection error: " + e.getMessage();
} catch (InvalidDataException e) {
if (SensorsDataAPI.sharedInstance(mContext).isDebugMode()) {
throw new DebugModeException(e.getMessage());
} else {
Log.i(LOGTAG, "Invalid data: " + e.getMessage());
}
deleteEvents = true;
errorMessage = "Invalid data: " + e.getMessage();
} catch (ResponseErrorException e) {
if (SensorsDataAPI.sharedInstance(mContext).isDebugMode()) {
throw new DebugModeException(e.getMessage());
} else {
Log.i(LOGTAG, "ResponseErrorException: " + e.getMessage());
}
deleteEvents = true;
errorMessage = "ResponseErrorException: " + e.getMessage();
} catch (Exception e) {
if (SensorsDataAPI.sharedInstance(mContext).isDebugMode()) {
throw new DebugModeException(e.getMessage());
} else {
Log.i(LOGTAG, "Exception: " + e.getMessage());
}
deleteEvents = false;
errorMessage = "Exception: " + e.getMessage();
} finally {
boolean isDebugMode = SensorsDataAPI.sharedInstance(mContext).isDebugMode();
if (!TextUtils.isEmpty(errorMessage)) {
if (isDebugMode || SensorsDataAPI.ENABLE_LOG) {
Log.i(LOGTAG, errorMessage);
if (isDebugMode) {
try {
if (toast != null) {
toast.cancel();
}
toast = Toast.makeText(mContext, errorMessage, Toast.LENGTH_LONG);
toast.show();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

if (deleteEvents) {
count = mDbAdapter.cleanupEvents(lastId, DbAdapter.Table.EVENTS);
if (isDebugMode || SensorsDataAPI.ENABLE_LOG) {
Log.i(LOGTAG, String.format("Events flushed. [left = %d]", count));
}
}
if (null != bout)
try {
bout.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,16 @@ boolean isDebugWriteData() {
e);
}

mMessages = AnalyticsMessages.getInstance(mContext, packageName);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
final Application app = (Application) context.getApplicationContext();
app.registerActivityLifecycleCallbacks(new LifecycleCallbacks());
}

Log.i(LOGTAG, String.format("Initialized the instance of Sensors Analytics SDK with server"
+ " url '%s', configure url '%s' flush interval %d ms", mServerUrl,
mConfigureUrl, mFlushInterval));
+ " url '%s', configure url '%s' flush interval %d ms, debugMode: %s", mServerUrl,
mConfigureUrl, mFlushInterval, debugMode));

final Map<String, Object> deviceInfo = new HashMap<String, Object>();

Expand Down Expand Up @@ -257,8 +259,6 @@ public void onPrefsLoaded(SharedPreferences preferences) {
mFirstDay.commit(mIsFirstDayDateFormat.format(System.currentTimeMillis()));
}

mMessages = AnalyticsMessages.getInstance(mContext, packageName);

mVTrack.startUpdates();
if (mEnableVTrack) {
mMessages.checkConfigure(new DecideMessages(mVTrack));
Expand Down Expand Up @@ -1089,9 +1089,9 @@ private void showDebugModeWarning() {
}
String info = null;
if (mDebugMode == DebugMode.DEBUG_ONLY) {
info = "现在您打开了神策SDK的'DEBUG_ONLY'模式,此模式下只校验数据但不导入数据,数据出错时会以 App Crash 的方式提示开发者,请上线前一定关闭。";
info = "现在您打开了神策SDK的'DEBUG_ONLY'模式,此模式下只校验数据但不导入数据,数据出错时会以 Toast 的方式提示开发者,请上线前一定使用 DEBUG_OFF 模式。";
} else if (mDebugMode == DebugMode.DEBUG_AND_TRACK) {
info = "现在您打开了神策SDK的'DEBUG_AND_TRACK'模式,此模式下校验数据并且导入数据,数据出错时会以 App Crash 的方式提示开发者,请上线前一定关闭。";
info = "现在您打开了神策SDK的'DEBUG_AND_TRACK'模式,此模式下校验数据并且导入数据,数据出错时会以 Toast 的方式提示开发者,请上线前一定使用 DEBUG_OFF 模式。";
}

Toast.makeText(mContext, info, Toast.LENGTH_LONG).show();
Expand Down Expand Up @@ -1484,9 +1484,7 @@ public void onActivityStopped(Activity activity) {
}
}
try {
if (SensorsDataUtils.isNetworkAvailable(mContext)) {
mMessages.flush();
}
mMessages.flush();
} catch (Exception e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -1695,7 +1693,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.37";
static final String VERSION = "1.6.38";

static Boolean ENABLE_LOG = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ public static String getUserAgent(Context context) {
String userAgent = preferences.getString(SHARED_PREF_USER_AGENT_KEY, null);
if (TextUtils.isEmpty(userAgent)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
userAgent = WebSettings.getDefaultUserAgent(context);
try {
Class webSettingsClass = Class.forName("android.webkit.WebSettings");
Method getDefaultUserAgentMethod = webSettingsClass.getMethod("getDefaultUserAgent");
if (getDefaultUserAgentMethod != null) {
userAgent = WebSettings.getDefaultUserAgent(context);
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
try {
final Class<?> webSettingsClassicClass = Class.forName("android.webkit.WebSettingsClassic");
Expand All @@ -62,11 +70,14 @@ public static String getUserAgent(Context context) {
userAgent = (String) method.invoke(constructor.newInstance(context, null));
} catch (final Exception e) {
e.printStackTrace();
userAgent = System.getProperty("http.agent");
}
}
}

if (TextUtils.isEmpty(userAgent)) {
userAgent = System.getProperty("http.agent");
}

if (!TextUtils.isEmpty(userAgent)) {
final SharedPreferences.Editor editor = preferences.edit();
editor.putString(SHARED_PREF_USER_AGENT_KEY, userAgent);
Expand All @@ -75,6 +86,7 @@ public static String getUserAgent(Context context) {

return userAgent;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
Expand Down

0 comments on commit bdb999d

Please sign in to comment.