Panagiotis Triantafyllou

steps fixes

......@@ -38,7 +38,7 @@ public class SplashActivity extends BaseActivity {
if (!WarplyDBHelper.getInstance(SplashActivity.this).isTableNotEmpty("auth")) {
//6012049321, 6012049322, 6012049323, 7000000831 history, 7000000826, 7000000831 shared coupons
//prod 6006552990, prod 6005892749, live 3000184910,prod 7000070282, live 3000136179
WarplyManager.getCosmoteUser("7000000831", mLoginReceiver);
WarplyManager.getCosmoteUser("7000000833", mLoginReceiver);
// WarplyManager.verifyTicket("", "7000023699", mLoginReceiver);
} else {
startNextActivity();
......
......@@ -44,7 +44,6 @@ import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import androidx.annotation.NonNull;
import androidx.lifecycle.ProcessLifecycleOwner;
import androidx.work.Constraints;
import androidx.work.NetworkType;
import androidx.work.OneTimeWorkRequest;
......@@ -56,9 +55,6 @@ import org.json.JSONException;
import org.json.JSONObject;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
......@@ -68,13 +64,11 @@ import ly.warp.sdk.io.callbacks.CallbackReceiver;
import ly.warp.sdk.io.models.CouponList;
import ly.warp.sdk.io.models.WarplyWebviewActivityCallbackEventModel;
import ly.warp.sdk.io.models.WarplyWebviewCallbackEventModel;
import ly.warp.sdk.io.request.PacingCalculateRequest;
import ly.warp.sdk.services.EventCampaignCouponService;
import ly.warp.sdk.services.EventQuestionnaireService;
import ly.warp.sdk.services.PushEventsClickedWorkerService;
import ly.warp.sdk.utils.WarpJSONParser;
import ly.warp.sdk.utils.WarpUtils;
import ly.warp.sdk.utils.WarplyManagerHelper;
import ly.warp.sdk.utils.WarplyPreferences;
import ly.warp.sdk.utils.WarplyProperty;
import ly.warp.sdk.utils.managers.WarplyAnalyticsManager;
......@@ -137,13 +131,14 @@ public class WarpViewActivity extends WarpBaseActivity {
super.onStop();
WarplySessionManager.onStopActivity(this);
EventBus.getDefault().unregister(this);
WarplyManager.sendSteps(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (metersHandler != null) metersHandler.removeCallbacksAndMessages(null);
sendSteps();
// WarplyManager.sendSteps(this);
WarpUtils.setWebviewParams(this, new JSONObject());
setWebviewSupermarket(false);
}
......@@ -346,26 +341,4 @@ public class WarpViewActivity extends WarpBaseActivity {
public static void setWebviewSupermarket(boolean isLoaded) {
mWebviewSupermarket = isLoaded;
}
private void sendSteps() {
String date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.getDefault()).format(new Date());
WarpUtils.log("SEND_STEPS: " + String.valueOf(WarpUtils.getStepsCounter(this)));
if (WarpUtils.getStepsCounter(this) > 0) {
WarplyManager.setPacingDetails(new PacingCalculateRequest().setCounter(WarpUtils.getStepsCounter(this)).setDate(date), new CallbackReceiver<JSONObject>() {
@Override
public void onSuccess(JSONObject result) {
int status = result.optInt("status", 2);
if (status == 1) {
WarplyManagerHelper.mSteps = 0;
WarpUtils.setStepsCounter(WarpViewActivity.this, 0);
}
}
@Override
public void onFailure(int errorCode) {
WarpUtils.log("Warply Health Service error: " + String.valueOf(errorCode));
}
});
}
}
}
\ No newline at end of file
......
......@@ -20,23 +20,12 @@ import android.os.IBinder;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import ly.warp.sdk.R;
import ly.warp.sdk.Warply;
import ly.warp.sdk.io.callbacks.CallbackReceiver;
import ly.warp.sdk.io.models.HealthEventModel;
import ly.warp.sdk.io.models.PacingDetails;
import ly.warp.sdk.io.request.PacingCalculateRequest;
import ly.warp.sdk.io.request.PacingDetailsRequest;
import ly.warp.sdk.utils.WarpUtils;
import ly.warp.sdk.utils.WarplyManagerHelper;
import ly.warp.sdk.utils.managers.WarplyEventBusManager;
import ly.warp.sdk.utils.managers.WarplyManager;
/**
......@@ -88,11 +77,24 @@ public class WarplyHealthService extends Service implements SensorEventListener
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
Warply.getInitializer(getApplicationContext()).init();
WarplyManager.getPacingDetails(new PacingDetailsRequest(), mPacingCallback);
/** 1. We get all daily steps from backend so we
* can send them with event bus to the client app
* and show them on their home card.
* Comment this line if registerStepSensor() below
* is going to be used instead. */
// WarplyManager.getPacingDetails(new PacingDetailsRequest(), mPacingCallback);
/** 2. We register our sensor to start giving data.
* Comment this line if WarplyManager.getPacingDetails()
* above is going to be used instead. */
registerStepSensor();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
/** 3. We create a sticky notification so the
* service can run on background */
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
PackageManager pm = getPackageManager();
Intent bIntent = pm.getLaunchIntentForPackage(getPackageName());
......@@ -129,27 +131,12 @@ public class WarplyHealthService extends Service implements SensorEventListener
}
}
/** Old Implementation */
// new Thread(
// () -> {
// while (true) {
// sendSteps();
// try {
// Thread.sleep(900000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// }
// ).start();
/** New Implementation */
// Use Handler instead of Thread for periodic tasks
/** We save all the steps every 15 minutes */
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
sendSteps();
WarplyManager.sendSteps(WarplyHealthService.this);
handler.postDelayed(this, 900000);
}
};
......@@ -168,7 +155,7 @@ public class WarplyHealthService extends Service implements SensorEventListener
eventMeters = 0.0d;
pacingMeters = 0.0d;
sumSteps = 0;
sendSteps();
WarplyManager.sendSteps(this);
}
@Nullable
......@@ -237,20 +224,25 @@ public class WarplyHealthService extends Service implements SensorEventListener
if (velocityEstimate > STEP_THRESHOLD
&& oldVelocityEstimate <= STEP_THRESHOLD
&& (timeNs - lastStepTimeNs > STEP_DELAY_NS)) {
/** These 2 lines have moved inside WarpView passMeters */
// WarplyManagerHelper.mSteps++;
// WarpUtils.setStepsCounter(this, WarplyManagerHelper.mSteps);
WarplyManagerHelper.mStepsWebview++;
sumSteps++;
WarplyManagerHelper.mSteps++;
WarpUtils.log("COUNT_STEPS: " + String.valueOf(WarplyManagerHelper.mSteps));
WarpUtils.setStepsCounter(this, WarplyManagerHelper.mSteps);
WarpUtils.log("WEBVIEW_STEPS: " + String.valueOf(WarplyManagerHelper.mStepsWebview));
WarplyManagerHelper.mMetersWebview = (WarplyManagerHelper.mStepsWebview * 0.762);
WarpUtils.log("WEBVIEW_METERS: " + String.valueOf(WarplyManagerHelper.mMetersWebview));
HealthEventModel healthSteps = new HealthEventModel();
// healthSteps.setMeters((mStepsAll * 0.762)); // 16/09/2022
// healthSteps.setMeters(WarplyManagerHelper.mMetersWebview); // 20/09/2022
eventMeters = (sumSteps * 0.762);
WarpUtils.log("WIDGET_METERS: " + String.valueOf(pacingMeters + eventMeters));
healthSteps.setMeters(pacingMeters + eventMeters);
EventBus.getDefault().post(new WarplyEventBusManager(healthSteps));
/** 5. We send overall steps to the client card.
* Needs step 1. */
// HealthEventModel healthSteps = new HealthEventModel();
// sumSteps++;
// healthSteps.setMeters((mStepsAll * 0.762));
// healthSteps.setMeters(WarplyManagerHelper.mMetersWebview);
// eventMeters = (sumSteps * 0.762);
// healthSteps.setMeters(pacingMeters + eventMeters);
// EventBus.getDefault().post(new WarplyEventBusManager(healthSteps));
lastStepTimeNs = timeNs;
}
......@@ -294,35 +286,12 @@ public class WarplyHealthService extends Service implements SensorEventListener
return retval;
}
private void sendSteps() {
String date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.getDefault()).format(new Date());
if (WarpUtils.getStepsCounter(this) > 0) {
WarplyManager.setPacingDetails(new PacingCalculateRequest()
.setCounter(WarpUtils.getStepsCounter(this))
.setDate(date),
new CallbackReceiver<JSONObject>() {
@Override
public void onSuccess(JSONObject result) {
int status = result.optInt("status", 2);
if (status == 1) {
WarplyManagerHelper.mSteps = 0;
WarpUtils.setStepsCounter(WarplyHealthService.this, 0);
}
}
@Override
public void onFailure(int errorCode) {
WarpUtils.log("Warply Health Service error: " + String.valueOf(errorCode));
}
});
}
}
private CallbackReceiver<PacingDetails> mPacingCallback = new CallbackReceiver<PacingDetails>() {
@Override
public void onSuccess(PacingDetails result) {
pacingMeters = result.getMeters().getDay().getValue();
/** 2. We register our sensor to start giving data */
registerStepSensor();
}
......
......@@ -654,13 +654,13 @@ public class WarpUtils {
return prefs.getString(PREFERENCES_KEY_WEBVIEW_PARAMS, "");
}
public static void setStepsCounter(Context context, int counter) {
public static synchronized void setStepsCounter(Context context, int counter) {
SharedPreferences.Editor editor = getPreferences(context).edit();
editor.putInt(PREFERENCES_STEPS_COUNTER, counter);
editor.apply();
}
public static int getStepsCounter(Context context) {
public static synchronized int getStepsCounter(Context context) {
SharedPreferences prefs = getPreferences(context);
return prefs.getInt(PREFERENCES_STEPS_COUNTER, 0);
}
......
......@@ -58,6 +58,7 @@ import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
......@@ -5117,6 +5118,31 @@ public class WarplyManager {
}, null);
}
public static void sendSteps(Context context) {
String date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.getDefault()).format(new Date());
if (WarpUtils.getStepsCounter(context) > 0) {
setPacingDetails(new PacingCalculateRequest()
.setCounter(WarpUtils.getStepsCounter(context))
.setDate(date),
new CallbackReceiver<JSONObject>() {
@Override
public void onSuccess(JSONObject result) {
int status = result.optInt("status", 2);
if (status == 1) {
WarplyManagerHelper.mSteps = 0;
WarpUtils.setStepsCounter(context, 0);
}
}
@Override
public void onFailure(int errorCode) {
WarpUtils.log("Warply Health Service error: " + String.valueOf(errorCode));
}
});
}
}
private static boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) Warply.getWarplyContext().getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
......
......@@ -826,11 +826,16 @@ public class WarpView extends WebView implements DefaultLifecycleObserver {
WarpView.this.evaluateJavascript(scriptSourceMeters, s -> {
});
/** These 2 lines have moved here from WarplyHealthService */
WarplyManagerHelper.mSteps += WarplyManagerHelper.mStepsWebview;
WarpUtils.setStepsCounter(getContext(), WarplyManagerHelper.mSteps);
WarplyManagerHelper.mStepsWebview = 0;
WarplyManagerHelper.mMetersWebview = 0.0d;
WarpViewActivity.getMetersHandler().postDelayed(this, 300);
}
}, 3000); //TODO: change it back to 1000
}, 1000);
if (url.contains("about:blank")) {
WarpView.this.clearHistory();
......