Panagiotis Triantafyllou

crash fixes

...@@ -2,7 +2,7 @@ apply plugin: 'com.android.library' ...@@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
2 2
3 ext { 3 ext {
4 PUBLISH_GROUP_ID = 'ly.warp' 4 PUBLISH_GROUP_ID = 'ly.warp'
5 - PUBLISH_VERSION = '4.5.4.6rc35' 5 + PUBLISH_VERSION = '4.5.4.6rc36'
6 PUBLISH_ARTIFACT_ID = 'warply-android-sdk' 6 PUBLISH_ARTIFACT_ID = 'warply-android-sdk'
7 } 7 }
8 8
......
...@@ -432,7 +432,7 @@ public enum Warply { ...@@ -432,7 +432,7 @@ public enum Warply {
432 requestsInQueueCount = WarplyDBHelper.getInstance(context).addRequest( 432 requestsInQueueCount = WarplyDBHelper.getInstance(context).addRequest(
433 microappName, jObj.toString(), force); 433 microappName, jObj.toString(), force);
434 } 434 }
435 - tryWakingSendingTaskInternal(requestsInQueueCount); 435 + tryWakingSendingTaskInternal(context, requestsInQueueCount);
436 } 436 }
437 437
438 public static void postMicroappPush(String microappName, JSONObject jObj, boolean force) { 438 public static void postMicroappPush(String microappName, JSONObject jObj, boolean force) {
...@@ -1157,6 +1157,109 @@ public enum Warply { ...@@ -1157,6 +1157,109 @@ public enum Warply {
1157 } 1157 }
1158 } 1158 }
1159 1159
1160 + private void tryWakingSendingTaskInternal(Context context, long requestsInQueue) {
1161 + if (!WarpUtils.isRegisteredWarply(mContext)) {
1162 + WarpUtils
1163 + .log("************* WARPLY Registration ********************");
1164 + WarpUtils
1165 + .log("[WARP TRace] Not registered yet, not waking post task!");
1166 + WarpUtils
1167 + .log("******************************************************");
1168 + return;
1169 + }
1170 +
1171 +// if (WarplyDBHelper.getInstance(mContext.get()).isForceRequestsExist()
1172 +// || (requestsInQueue >= MINIMUM_REQUESTS_FOR_SENDING)) {
1173 +
1174 + if (WarplyDBHelper.getInstance(mContext).isForceRequestsExist()
1175 + || (requestsInQueue > 0)) {
1176 +
1177 + WarpUtils.log("Waking post task!");
1178 + wakeSendingTask(context);
1179 + } else {
1180 + WarpUtils.log("Criteria not matched, not waking post task!");
1181 + }
1182 + }
1183 +
1184 + private void wakeSendingTask(Context context) {
1185 +
1186 + if (!acquirePostLockInternal())
1187 + return;
1188 +
1189 + ArrayList<Long> ids = new ArrayList<>();
1190 + JSONArray jArray = new JSONArray();
1191 + Cursor c = WarplyDBHelper.getInstance(mContext).getAllRequests();
1192 + while (c.moveToNext()) {
1193 + JSONObject jObj = new JSONObject();
1194 + try {
1195 + jObj.putOpt(
1196 + c.getString(c
1197 + .getColumnIndex(WarplyDBHelper.KEY_REQUESTS_MICROAPP)),
1198 + new JSONObject(c.getString(c
1199 + .getColumnIndex(WarplyDBHelper.KEY_REQUESTS_ENTITY))));
1200 + jArray.put(jObj);
1201 + } catch (JSONException e) {
1202 + if (WarpConstants.DEBUG) {
1203 + WarpUtils
1204 + .warn("[WARP Trace] Exception thrown when creating the JSON from DB with id: "
1205 + + c.getLong(c
1206 + .getColumnIndex(WarplyDBHelper.KEY_REQUESTS_ID)),
1207 + e);
1208 + }
1209 + } finally {
1210 + ids.add(c.getLong(c.getColumnIndex(WarplyDBHelper.KEY_REQUESTS_ID)));
1211 + }
1212 + }
1213 + c.close();
1214 + PostHook ph = new PostHook(getWarplyContext(), ids);
1215 + postToServerInternal(context, null, jArray, ph, null);
1216 + getFromServerInternal(null, null, null, null);/** get context **/
1217 + }
1218 +
1219 + private void postToServerInternal(Context context, String warplyPath, JSONArray data,
1220 + CallbackReceiver<JSONObject> listener, Object tag) {
1221 + String url = buildWarplyRequestUrl(context, warplyPath);
1222 +
1223 + JSONArray tempAnalytics = new JSONArray();
1224 + JSONArray tempDeviceInfo = new JSONArray();
1225 + JSONArray tempOther = new JSONArray();
1226 + if (data.length() > 0) {
1227 + for (int i = 0; i < data.length(); i++) {
1228 + JSONObject microappItem = data.optJSONObject(i);
1229 + if (microappItem != null) {
1230 + if (microappItem.has("inapp_analytics")) {
1231 + tempAnalytics.put(microappItem);
1232 + } else if (microappItem.has("device_info") || microappItem.has("application_data")) {
1233 + tempDeviceInfo.put(microappItem);
1234 + } else {
1235 + tempOther.put(microappItem);
1236 + }
1237 + }
1238 + }
1239 + }
1240 +
1241 + if (tempAnalytics != null && tempAnalytics.length() > 0) {
1242 + url = WarplyProperty.getBaseUrl(mContext) + WarpConstants.WARPLY_ASYNC + WarpConstants.WARPLY_ANALYTICS + WarplyProperty.getAppUuid(mContext) + "/";
1243 + }
1244 + if (tempDeviceInfo != null && tempDeviceInfo.length() > 0) {
1245 + url = WarplyProperty.getBaseUrl(mContext) + WarpConstants.WARPLY_ASYNC + WarpConstants.WARPLY_DEVICE_INFO + WarplyProperty.getAppUuid(mContext) + "/";
1246 + }
1247 + if (tempOther != null && tempOther.length() > 0) {
1248 + url = buildWarplyRequestUrl(warplyPath);
1249 + }
1250 +
1251 + requestToServerInternal(Method.POST, url, data, listener, tag);
1252 + }
1253 +
1254 + private String buildWarplyRequestUrl(Context context, String warplyPath) {
1255 + StringBuilder sb = new StringBuilder(WarplyProperty.getBaseUrl(context) + WarpConstants.BASE_URL_MOBILE);
1256 + sb.append(WarplyProperty.getAppUuid(mContext)).append("/")
1257 + .append(WarpConstants.PATH_CONTEXT).append("/");
1258 + if (warplyPath != null)
1259 + sb.append("?path=").append(warplyPath);
1260 + return sb.toString();
1261 + }
1262 +
1160 public static void tryWakingSendingPushTask(boolean force) { 1263 public static void tryWakingSendingPushTask(boolean force) {
1161 INSTANCE.isInitializedOrThrow(); 1264 INSTANCE.isInitializedOrThrow();
1162 INSTANCE.tryWakingSendingPushTaskInternal(WarplyDBHelper.getInstance( 1265 INSTANCE.tryWakingSendingPushTaskInternal(WarplyDBHelper.getInstance(
......
...@@ -97,7 +97,7 @@ public class GiftsForYouActivity extends AppCompatActivity implements View.OnCli ...@@ -97,7 +97,7 @@ public class GiftsForYouActivity extends AppCompatActivity implements View.OnCli
97 @Override 97 @Override
98 public void onResume() { 98 public void onResume() {
99 super.onResume(); 99 super.onResume();
100 - WarplyAnalyticsManager.logTrackersEvent(this, "screen", "GiftsForYouScreen"); 100 + WarplyAnalyticsManager.logTrackersEvent(GiftsForYouActivity.this, "screen", "GiftsForYouScreen");
101 mTimer = 0; 101 mTimer = 0;
102 mSecondsHandler.post(new Runnable() { 102 mSecondsHandler.post(new Runnable() {
103 @Override 103 @Override
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
16 16
17 package ly.warp.sdk.io.volley.toolbox; 17 package ly.warp.sdk.io.volley.toolbox;
18 18
19 +import java.io.ByteArrayOutputStream;
19 import java.io.EOFException; 20 import java.io.EOFException;
20 import java.io.File; 21 import java.io.File;
21 import java.io.FileInputStream; 22 import java.io.FileInputStream;
...@@ -34,6 +35,8 @@ import ly.warp.sdk.io.volley.Cache; ...@@ -34,6 +35,8 @@ import ly.warp.sdk.io.volley.Cache;
34 import ly.warp.sdk.io.volley.VolleyLog; 35 import ly.warp.sdk.io.volley.VolleyLog;
35 import android.os.SystemClock; 36 import android.os.SystemClock;
36 37
38 +import com.google.android.gms.common.util.IOUtils;
39 +
37 /** 40 /**
38 * Cache implementation that caches files directly onto the hard disk in the specified 41 * Cache implementation that caches files directly onto the hard disk in the specified
39 * directory. The default disk usage size is 5MB, but is configurable. 42 * directory. The default disk usage size is 5MB, but is configurable.
...@@ -114,6 +117,7 @@ public class DiskBasedCache implements Cache { ...@@ -114,6 +117,7 @@ public class DiskBasedCache implements Cache {
114 cis = new CountingInputStream(new FileInputStream(file)); 117 cis = new CountingInputStream(new FileInputStream(file));
115 CacheHeader.readHeader(cis); // eat header 118 CacheHeader.readHeader(cis); // eat header
116 byte[] data = streamToBytes(cis, (int) Math.abs((int)(file.length() - cis.bytesRead))); 119 byte[] data = streamToBytes(cis, (int) Math.abs((int)(file.length() - cis.bytesRead)));
120 +// byte[] data = readAllBytes(cis);
117 return entry.toCacheEntry(data); 121 return entry.toCacheEntry(data);
118 } catch (IOException e) { 122 } catch (IOException e) {
119 VolleyLog.d("%s: %s", file.getAbsolutePath(), e.toString()); 123 VolleyLog.d("%s: %s", file.getAbsolutePath(), e.toString());
...@@ -312,7 +316,12 @@ public class DiskBasedCache implements Cache { ...@@ -312,7 +316,12 @@ public class DiskBasedCache implements Cache {
312 * Reads the contents of an InputStream into a byte[]. 316 * Reads the contents of an InputStream into a byte[].
313 * */ 317 * */
314 private static byte[] streamToBytes(InputStream in, int length) throws IOException { 318 private static byte[] streamToBytes(InputStream in, int length) throws IOException {
315 - byte[] bytes = new byte[length]; 319 + byte[] bytes = new byte[1];
320 + try {
321 + bytes = IOUtils.toByteArray(in);
322 + } catch (OutOfMemoryError e) {
323 + e.printStackTrace();
324 + }
316 int count; 325 int count;
317 int pos = 0; 326 int pos = 0;
318 while (pos < length && ((count = in.read(bytes, pos, length - pos)) != -1)) { 327 while (pos < length && ((count = in.read(bytes, pos, length - pos)) != -1)) {
...@@ -324,6 +333,32 @@ public class DiskBasedCache implements Cache { ...@@ -324,6 +333,32 @@ public class DiskBasedCache implements Cache {
324 return bytes; 333 return bytes;
325 } 334 }
326 335
336 + public static byte[] readAllBytes(InputStream inputStream) throws IOException {
337 + final int bufLen = 4 * 0x400; // 4KB
338 + byte[] buf = new byte[bufLen];
339 + int readLen;
340 + IOException exception = null;
341 +
342 + try {
343 + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
344 + while ((readLen = inputStream.read(buf, 0, bufLen)) != -1)
345 + outputStream.write(buf, 0, readLen);
346 +
347 + return outputStream.toByteArray();
348 + }
349 + } catch (IOException e) {
350 + exception = e;
351 + throw e;
352 + } finally {
353 + if (exception == null) inputStream.close();
354 + else try {
355 + inputStream.close();
356 + } catch (IOException e) {
357 + exception.addSuppressed(e);
358 + }
359 + }
360 + }
361 +
327 /** 362 /**
328 * Handles holding onto the cache headers for an entry. 363 * Handles holding onto the cache headers for an entry.
329 */ 364 */
...@@ -522,7 +557,7 @@ public class DiskBasedCache implements Cache { ...@@ -522,7 +557,7 @@ public class DiskBasedCache implements Cache {
522 557
523 static String readString(InputStream is) throws IOException { 558 static String readString(InputStream is) throws IOException {
524 int n = (int) readLong(is); 559 int n = (int) readLong(is);
525 - byte[] b = streamToBytes(is, n); 560 + byte[] b = readAllBytes(is);
526 return new String(b, "UTF-8"); 561 return new String(b, "UTF-8");
527 } 562 }
528 563
......
...@@ -56,14 +56,18 @@ public class WarplyProperty { ...@@ -56,14 +56,18 @@ public class WarplyProperty {
56 // =========================================================== 56 // ===========================================================
57 57
58 private static String getWarplyProperty(Context context, String propertyKey) { 58 private static String getWarplyProperty(Context context, String propertyKey) {
59 + if(context == null) {
60 + return "";
61 + }
59 try { 62 try {
60 Properties properties = new Properties(); 63 Properties properties = new Properties();
61 properties.load(context.getResources().getAssets().open(PROPERTIES_FILE)); 64 properties.load(context.getResources().getAssets().open(PROPERTIES_FILE));
62 return properties.getProperty(propertyKey); 65 return properties.getProperty(propertyKey);
63 } catch (IOException e) { 66 } catch (IOException e) {
64 WarpUtils.log(e.getMessage()); 67 WarpUtils.log(e.getMessage());
68 + return "";
65 } 69 }
66 - return ""; 70 +// return "";
67 } 71 }
68 72
69 @Nullable 73 @Nullable
......