Panagiotis Triantafyllou

crash fixes

......@@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
ext {
PUBLISH_GROUP_ID = 'ly.warp'
PUBLISH_VERSION = '4.5.4.6rc35'
PUBLISH_VERSION = '4.5.4.6rc36'
PUBLISH_ARTIFACT_ID = 'warply-android-sdk'
}
......
......@@ -432,7 +432,7 @@ public enum Warply {
requestsInQueueCount = WarplyDBHelper.getInstance(context).addRequest(
microappName, jObj.toString(), force);
}
tryWakingSendingTaskInternal(requestsInQueueCount);
tryWakingSendingTaskInternal(context, requestsInQueueCount);
}
public static void postMicroappPush(String microappName, JSONObject jObj, boolean force) {
......@@ -1157,6 +1157,109 @@ public enum Warply {
}
}
private void tryWakingSendingTaskInternal(Context context, long requestsInQueue) {
if (!WarpUtils.isRegisteredWarply(mContext)) {
WarpUtils
.log("************* WARPLY Registration ********************");
WarpUtils
.log("[WARP TRace] Not registered yet, not waking post task!");
WarpUtils
.log("******************************************************");
return;
}
// if (WarplyDBHelper.getInstance(mContext.get()).isForceRequestsExist()
// || (requestsInQueue >= MINIMUM_REQUESTS_FOR_SENDING)) {
if (WarplyDBHelper.getInstance(mContext).isForceRequestsExist()
|| (requestsInQueue > 0)) {
WarpUtils.log("Waking post task!");
wakeSendingTask(context);
} else {
WarpUtils.log("Criteria not matched, not waking post task!");
}
}
private void wakeSendingTask(Context context) {
if (!acquirePostLockInternal())
return;
ArrayList<Long> ids = new ArrayList<>();
JSONArray jArray = new JSONArray();
Cursor c = WarplyDBHelper.getInstance(mContext).getAllRequests();
while (c.moveToNext()) {
JSONObject jObj = new JSONObject();
try {
jObj.putOpt(
c.getString(c
.getColumnIndex(WarplyDBHelper.KEY_REQUESTS_MICROAPP)),
new JSONObject(c.getString(c
.getColumnIndex(WarplyDBHelper.KEY_REQUESTS_ENTITY))));
jArray.put(jObj);
} catch (JSONException e) {
if (WarpConstants.DEBUG) {
WarpUtils
.warn("[WARP Trace] Exception thrown when creating the JSON from DB with id: "
+ c.getLong(c
.getColumnIndex(WarplyDBHelper.KEY_REQUESTS_ID)),
e);
}
} finally {
ids.add(c.getLong(c.getColumnIndex(WarplyDBHelper.KEY_REQUESTS_ID)));
}
}
c.close();
PostHook ph = new PostHook(getWarplyContext(), ids);
postToServerInternal(context, null, jArray, ph, null);
getFromServerInternal(null, null, null, null);/** get context **/
}
private void postToServerInternal(Context context, String warplyPath, JSONArray data,
CallbackReceiver<JSONObject> listener, Object tag) {
String url = buildWarplyRequestUrl(context, warplyPath);
JSONArray tempAnalytics = new JSONArray();
JSONArray tempDeviceInfo = new JSONArray();
JSONArray tempOther = new JSONArray();
if (data.length() > 0) {
for (int i = 0; i < data.length(); i++) {
JSONObject microappItem = data.optJSONObject(i);
if (microappItem != null) {
if (microappItem.has("inapp_analytics")) {
tempAnalytics.put(microappItem);
} else if (microappItem.has("device_info") || microappItem.has("application_data")) {
tempDeviceInfo.put(microappItem);
} else {
tempOther.put(microappItem);
}
}
}
}
if (tempAnalytics != null && tempAnalytics.length() > 0) {
url = WarplyProperty.getBaseUrl(mContext) + WarpConstants.WARPLY_ASYNC + WarpConstants.WARPLY_ANALYTICS + WarplyProperty.getAppUuid(mContext) + "/";
}
if (tempDeviceInfo != null && tempDeviceInfo.length() > 0) {
url = WarplyProperty.getBaseUrl(mContext) + WarpConstants.WARPLY_ASYNC + WarpConstants.WARPLY_DEVICE_INFO + WarplyProperty.getAppUuid(mContext) + "/";
}
if (tempOther != null && tempOther.length() > 0) {
url = buildWarplyRequestUrl(warplyPath);
}
requestToServerInternal(Method.POST, url, data, listener, tag);
}
private String buildWarplyRequestUrl(Context context, String warplyPath) {
StringBuilder sb = new StringBuilder(WarplyProperty.getBaseUrl(context) + WarpConstants.BASE_URL_MOBILE);
sb.append(WarplyProperty.getAppUuid(mContext)).append("/")
.append(WarpConstants.PATH_CONTEXT).append("/");
if (warplyPath != null)
sb.append("?path=").append(warplyPath);
return sb.toString();
}
public static void tryWakingSendingPushTask(boolean force) {
INSTANCE.isInitializedOrThrow();
INSTANCE.tryWakingSendingPushTaskInternal(WarplyDBHelper.getInstance(
......
......@@ -97,7 +97,7 @@ public class GiftsForYouActivity extends AppCompatActivity implements View.OnCli
@Override
public void onResume() {
super.onResume();
WarplyAnalyticsManager.logTrackersEvent(this, "screen", "GiftsForYouScreen");
WarplyAnalyticsManager.logTrackersEvent(GiftsForYouActivity.this, "screen", "GiftsForYouScreen");
mTimer = 0;
mSecondsHandler.post(new Runnable() {
@Override
......
......@@ -16,6 +16,7 @@
package ly.warp.sdk.io.volley.toolbox;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
......@@ -34,6 +35,8 @@ import ly.warp.sdk.io.volley.Cache;
import ly.warp.sdk.io.volley.VolleyLog;
import android.os.SystemClock;
import com.google.android.gms.common.util.IOUtils;
/**
* Cache implementation that caches files directly onto the hard disk in the specified
* directory. The default disk usage size is 5MB, but is configurable.
......@@ -114,6 +117,7 @@ public class DiskBasedCache implements Cache {
cis = new CountingInputStream(new FileInputStream(file));
CacheHeader.readHeader(cis); // eat header
byte[] data = streamToBytes(cis, (int) Math.abs((int)(file.length() - cis.bytesRead)));
// byte[] data = readAllBytes(cis);
return entry.toCacheEntry(data);
} catch (IOException e) {
VolleyLog.d("%s: %s", file.getAbsolutePath(), e.toString());
......@@ -312,7 +316,12 @@ public class DiskBasedCache implements Cache {
* Reads the contents of an InputStream into a byte[].
* */
private static byte[] streamToBytes(InputStream in, int length) throws IOException {
byte[] bytes = new byte[length];
byte[] bytes = new byte[1];
try {
bytes = IOUtils.toByteArray(in);
} catch (OutOfMemoryError e) {
e.printStackTrace();
}
int count;
int pos = 0;
while (pos < length && ((count = in.read(bytes, pos, length - pos)) != -1)) {
......@@ -324,6 +333,32 @@ public class DiskBasedCache implements Cache {
return bytes;
}
public static byte[] readAllBytes(InputStream inputStream) throws IOException {
final int bufLen = 4 * 0x400; // 4KB
byte[] buf = new byte[bufLen];
int readLen;
IOException exception = null;
try {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
while ((readLen = inputStream.read(buf, 0, bufLen)) != -1)
outputStream.write(buf, 0, readLen);
return outputStream.toByteArray();
}
} catch (IOException e) {
exception = e;
throw e;
} finally {
if (exception == null) inputStream.close();
else try {
inputStream.close();
} catch (IOException e) {
exception.addSuppressed(e);
}
}
}
/**
* Handles holding onto the cache headers for an entry.
*/
......@@ -522,7 +557,7 @@ public class DiskBasedCache implements Cache {
static String readString(InputStream is) throws IOException {
int n = (int) readLong(is);
byte[] b = streamToBytes(is, n);
byte[] b = readAllBytes(is);
return new String(b, "UTF-8");
}
......
......@@ -56,14 +56,18 @@ public class WarplyProperty {
// ===========================================================
private static String getWarplyProperty(Context context, String propertyKey) {
if(context == null) {
return "";
}
try {
Properties properties = new Properties();
properties.load(context.getResources().getAssets().open(PROPERTIES_FILE));
return properties.getProperty(propertyKey);
} catch (IOException e) {
WarpUtils.log(e.getMessage());
return "";
}
return "";
// return "";
}
@Nullable
......