Skip to content

Commit

Permalink
Merge pull request #10 from wingify/aman-development
Browse files Browse the repository at this point in the history
Aman development
  • Loading branch information
aman400 authored Dec 22, 2017
2 parents ba1e5bb + 68efa98 commit e1382a1
Show file tree
Hide file tree
Showing 19 changed files with 1,637 additions and 182 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Add dependencies to app/build.gradle file

dependencies {
...
compile 'com.vwo:mobile:2.0.0@aar'
compile 'com.vwo:mobile:2.0.1@aar'
debugCompile ('io.socket:socket.io-client:1.0.0') {
// excluding org.json which is provided by Android
exclude group: 'org.json', module: 'json'
Expand Down
8 changes: 6 additions & 2 deletions demo/src/main/res/menu/activity_main_drawer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">

<item android:title="@string/choose_campaign">
<item
android:id="@+id/choose_campaign_menu"
android:title="@string/choose_campaign">
<menu>
<group android:checkableBehavior="single">
<item
Expand All @@ -16,7 +18,9 @@
</menu>
</item>

<item android:title="@string/settings">
<item
android:id="@+id/settings_menu"
android:title="@string/settings">
<menu>
<item
android:id="@+id/action_clear_data"
Expand Down
15 changes: 8 additions & 7 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ android {
compileSdkVersion 27
buildToolsVersion '27.0.2'
defaultConfig {
versionCode 13
versionCode 14
minSdkVersion 14
targetSdkVersion 27
versionName "2.0.0"
versionName "2.0.1"
}
signingConfigs {
config {
Expand Down Expand Up @@ -63,9 +63,12 @@ dependencies {
// Required -- JUnit 4 framework
testImplementation 'junit:junit:4.12'
// Optional -- Mockito framework
testImplementation 'org.mockito:mockito-core:2.12.0'
testImplementation 'org.powermock:powermock-api-easymock:1.7.3'
testImplementation 'org.robolectric:robolectric:3.5.1'
testImplementation 'org.mockito:mockito-core:2.13.0'
testImplementation 'org.powermock:powermock-api-mockito2:2.0.0-beta.5'
testImplementation 'org.powermock:powermock-module-junit4:2.0.0-beta.5'
testImplementation "org.powermock:powermock-module-junit4-rule:2.0.0-beta.5"
testImplementation "org.powermock:powermock-classloading-xstream:2.0.0-beta.5"
testImplementation 'org.robolectric:robolectric:3.6'
testImplementation 'com.squareup.okhttp3:mockwebserver:3.9.1'
implementation('io.socket:socket.io-client:1.0.0') {
exclude group: 'org.json', module: 'json'
Expand All @@ -83,5 +86,3 @@ ext {
}

apply from: 'android-release-jar.gradle'


Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface MessageQueue<T> {
T peek();

/**
* Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions.
* Inserts the specified element asynchronously into this queue if it is possible to do so immediately without violating capacity restrictions.
* @param t {@link T} is the element to be inserted
*/
void add(T t);
Expand Down
59 changes: 35 additions & 24 deletions library/src/main/java/com/vwo/mobile/data/VWOMessageQueue.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package com.vwo.mobile.data;

import android.content.Context;
import android.os.Parcelable;
import android.support.annotation.Nullable;

import com.vwo.mobile.data.io.QueueFile;
import com.vwo.mobile.models.Entry;
import com.vwo.mobile.utils.Parceler;
import com.vwo.mobile.utils.VWOLog;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Locale;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* Created by aman on 18/09/17.
Expand All @@ -25,12 +24,37 @@ public class VWOMessageQueue implements MessageQueue<Entry> {

private final QueueFile queueFile;
private String filename;
private Queue<Entry> waitingQueue;
private ExecutorService executorService;
private Thread thread;

private VWOMessageQueue(Context context, String fileName) throws IOException {
File file = new File(IOUtils.getCacheDirectory(context), fileName);
try {
queueFile = new QueueFile(file);
this.filename = fileName;
this.waitingQueue = new ConcurrentLinkedQueue<>();
executorService = Executors.newSingleThreadExecutor();

thread = new Thread(new Runnable() {
@Override
public void run() {
while (!waitingQueue.isEmpty()) {
Entry entry = waitingQueue.poll();
VWOLog.i(VWOLog.STORAGE_LOGS, String.format(Locale.ENGLISH, "Adding to queue %s\n%s", filename, entry.toString()), true);
byte[] data = Parceler.marshall(entry);
try {
synchronized (queueFile) {
queueFile.add(data);
}
} catch (IOException exception) {
VWOLog.e(VWOLog.STORAGE_LOGS, String.format(Locale.ENGLISH, "File %s corrupted. Clearing last entry...", filename), true, false);
remove();
VWOLog.e(VWOLog.STORAGE_LOGS, "Unable to create Object", exception, true, true);
}
}
}
});
} catch (IOException exception) {
VWOLog.e(VWOLog.STORAGE_LOGS, "Failed to initialize queue: " + fileName, exception,
false, true);
Expand All @@ -49,25 +73,8 @@ public static VWOMessageQueue getInstance(Context context, String fileName) thro
*/
@Override
public void add(final Entry entry) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
VWOLog.i(VWOLog.STORAGE_LOGS, String.format(Locale.ENGLISH, "Adding to queue %s\n%s", filename, entry.toString()), true);
byte[] data = Parceler.marshall(entry);
try {
synchronized (queueFile) {
queueFile.add(data);
}
} catch (IOException exception) {
VWOLog.e(VWOLog.STORAGE_LOGS, String.format(Locale.ENGLISH, "File %s corrupted. Clearing last entry...", filename), true, false);
remove();
VWOLog.e(VWOLog.STORAGE_LOGS, "Unable to create Object", exception, true, true);
}
}
});

thread.start();

waitingQueue.add(entry);
startInsertionThread();
}

@Override
Expand Down Expand Up @@ -151,4 +158,8 @@ public Entry poll() {
remove();
return entry;
}

private void startInsertionThread() {
executorService.execute(thread);
}
}
4 changes: 1 addition & 3 deletions library/src/main/java/com/vwo/mobile/models/Campaign.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,8 @@ public Campaign(JSONObject campaignData) {
mSegments.add(new DefaultSegment());
mSegmentType = SEGMENT_DEFAULT;
}


} catch (JSONException e) {
e.printStackTrace();
VWOLog.e(VWOLog.DOWNLOAD_DATA_LOGS, e.getMessage(), true, true);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ public boolean evaluate(VWO vwo, JSONArray data, String key) {
DAY_OF_WEEK_EQUAL_TO(AppConstants.DAY_OF_WEEK, AppConstants.EQUAL_TO, new EvaluateSegment() {
@Override
public boolean evaluate(VWO vwo, JSONArray data) {

Calendar c = GregorianCalendar.getInstance();
Calendar c = VWOUtils.getCalendar();
int dayOfWeek = c.get(GregorianCalendar.DAY_OF_WEEK) - 1;
for (int i = 0; i < data.length(); i++) {
try {
Expand All @@ -138,7 +137,7 @@ public boolean evaluate(VWO vwo, JSONArray data, String key) {
@Override
public boolean evaluate(VWO vwo, JSONArray data) {

Calendar c = GregorianCalendar.getInstance();
Calendar c = VWOUtils.getCalendar();
int dayOfWeek = c.get(GregorianCalendar.DAY_OF_WEEK) - 1;
for (int i = 0; i < data.length(); i++) {
try {
Expand All @@ -162,7 +161,7 @@ public boolean evaluate(VWO vwo, JSONArray data, String key) {
HOUR_OF_DAY_EQUAL_TO(AppConstants.HOUR_OF_DAY, AppConstants.EQUAL_TO, new EvaluateSegment() {
@Override
public boolean evaluate(VWO vwo, JSONArray data) {
Calendar c = GregorianCalendar.getInstance();
Calendar c = VWOUtils.getCalendar();
int hourOfTheDay = c.get(GregorianCalendar.HOUR_OF_DAY);
for (int i = 0; i < data.length(); i++) {
try {
Expand All @@ -186,7 +185,7 @@ public boolean evaluate(VWO vwo, JSONArray data, String key) {
HOUR_OF_DAY_NOT_EQUAL_TO(AppConstants.HOUR_OF_DAY, AppConstants.NOT_EQUAL_TO, new EvaluateSegment() {
@Override
public boolean evaluate(VWO vwo, JSONArray data) {
Calendar c = GregorianCalendar.getInstance();
Calendar c = VWOUtils.getCalendar();
int hourOfTheDay = c.get(GregorianCalendar.HOUR_OF_DAY);
for (int i = 0; i < data.length(); i++) {
try {
Expand Down Expand Up @@ -294,15 +293,15 @@ public boolean evaluate(VWO vwo, JSONArray data) {
for (int i = 0; i < data.length(); i++) {
try {
int version = Integer.parseInt(data.getString(i));
if (version == appVersion) {
if (version != appVersion) {
return true;
}
} catch (JSONException | NumberFormatException exception) {
VWOLog.e(VWOLog.SEGMENTATION_LOGS, "Unable to parse app version", exception,
false, false);
}
}
return true;
return false;
}

@Override
Expand All @@ -319,7 +318,7 @@ public boolean evaluate(VWO vwo, JSONArray data) {
try {
Pattern pattern = Pattern.compile(data.getString(i));
Matcher matcher = pattern.matcher(appVersion);
if (matcher.find()) {
if (matcher.matches()) {
return true;
}
} catch (JSONException exception) {
Expand Down Expand Up @@ -461,7 +460,7 @@ public boolean evaluate(VWO vwo, JSONArray data, String key) {
try {
Pattern pattern = Pattern.compile(data.getString(i));
Matcher matcher = pattern.matcher(customVariable);
if (matcher.find()) {
if (matcher.matches()) {
return true;
}
} catch (JSONException exception) {
Expand Down
6 changes: 6 additions & 0 deletions library/src/main/java/com/vwo/mobile/utils/VWOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import com.vwo.mobile.VWO;
import com.vwo.mobile.constants.AppConstants;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -203,6 +205,10 @@ public static boolean checkForPermission(Context context, String permission) {
return true;
}

public static Calendar getCalendar() {
return GregorianCalendar.getInstance();
}

public static boolean checkIfClassExists(String className) {
try {
Class.forName(className);
Expand Down
Loading

0 comments on commit e1382a1

Please sign in to comment.