Panagiotis Triantafyllou

steps fixes

...@@ -38,7 +38,7 @@ public class SplashActivity extends BaseActivity { ...@@ -38,7 +38,7 @@ public class SplashActivity extends BaseActivity {
38 if (!WarplyDBHelper.getInstance(SplashActivity.this).isTableNotEmpty("auth")) { 38 if (!WarplyDBHelper.getInstance(SplashActivity.this).isTableNotEmpty("auth")) {
39 //6012049321, 6012049322, 6012049323, 7000000831 history, 7000000826, 7000000831 shared coupons 39 //6012049321, 6012049322, 6012049323, 7000000831 history, 7000000826, 7000000831 shared coupons
40 //prod 6006552990, prod 6005892749, live 3000184910,prod 7000070282, live 3000136179 40 //prod 6006552990, prod 6005892749, live 3000184910,prod 7000070282, live 3000136179
41 - WarplyManager.getCosmoteUser("7000000831", mLoginReceiver); 41 + WarplyManager.getCosmoteUser("7000000833", mLoginReceiver);
42 // WarplyManager.verifyTicket("", "7000023699", mLoginReceiver); 42 // WarplyManager.verifyTicket("", "7000023699", mLoginReceiver);
43 } else { 43 } else {
44 startNextActivity(); 44 startNextActivity();
......
...@@ -44,7 +44,6 @@ import android.widget.ProgressBar; ...@@ -44,7 +44,6 @@ import android.widget.ProgressBar;
44 import android.widget.RelativeLayout; 44 import android.widget.RelativeLayout;
45 45
46 import androidx.annotation.NonNull; 46 import androidx.annotation.NonNull;
47 -import androidx.lifecycle.ProcessLifecycleOwner;
48 import androidx.work.Constraints; 47 import androidx.work.Constraints;
49 import androidx.work.NetworkType; 48 import androidx.work.NetworkType;
50 import androidx.work.OneTimeWorkRequest; 49 import androidx.work.OneTimeWorkRequest;
...@@ -56,9 +55,6 @@ import org.json.JSONException; ...@@ -56,9 +55,6 @@ import org.json.JSONException;
56 import org.json.JSONObject; 55 import org.json.JSONObject;
57 56
58 import java.security.SecureRandom; 57 import java.security.SecureRandom;
59 -import java.text.SimpleDateFormat;
60 -import java.util.Date;
61 -import java.util.Locale;
62 import java.util.concurrent.ThreadLocalRandom; 58 import java.util.concurrent.ThreadLocalRandom;
63 import java.util.concurrent.TimeUnit; 59 import java.util.concurrent.TimeUnit;
64 60
...@@ -68,13 +64,11 @@ import ly.warp.sdk.io.callbacks.CallbackReceiver; ...@@ -68,13 +64,11 @@ import ly.warp.sdk.io.callbacks.CallbackReceiver;
68 import ly.warp.sdk.io.models.CouponList; 64 import ly.warp.sdk.io.models.CouponList;
69 import ly.warp.sdk.io.models.WarplyWebviewActivityCallbackEventModel; 65 import ly.warp.sdk.io.models.WarplyWebviewActivityCallbackEventModel;
70 import ly.warp.sdk.io.models.WarplyWebviewCallbackEventModel; 66 import ly.warp.sdk.io.models.WarplyWebviewCallbackEventModel;
71 -import ly.warp.sdk.io.request.PacingCalculateRequest;
72 import ly.warp.sdk.services.EventCampaignCouponService; 67 import ly.warp.sdk.services.EventCampaignCouponService;
73 import ly.warp.sdk.services.EventQuestionnaireService; 68 import ly.warp.sdk.services.EventQuestionnaireService;
74 import ly.warp.sdk.services.PushEventsClickedWorkerService; 69 import ly.warp.sdk.services.PushEventsClickedWorkerService;
75 import ly.warp.sdk.utils.WarpJSONParser; 70 import ly.warp.sdk.utils.WarpJSONParser;
76 import ly.warp.sdk.utils.WarpUtils; 71 import ly.warp.sdk.utils.WarpUtils;
77 -import ly.warp.sdk.utils.WarplyManagerHelper;
78 import ly.warp.sdk.utils.WarplyPreferences; 72 import ly.warp.sdk.utils.WarplyPreferences;
79 import ly.warp.sdk.utils.WarplyProperty; 73 import ly.warp.sdk.utils.WarplyProperty;
80 import ly.warp.sdk.utils.managers.WarplyAnalyticsManager; 74 import ly.warp.sdk.utils.managers.WarplyAnalyticsManager;
...@@ -137,13 +131,14 @@ public class WarpViewActivity extends WarpBaseActivity { ...@@ -137,13 +131,14 @@ public class WarpViewActivity extends WarpBaseActivity {
137 super.onStop(); 131 super.onStop();
138 WarplySessionManager.onStopActivity(this); 132 WarplySessionManager.onStopActivity(this);
139 EventBus.getDefault().unregister(this); 133 EventBus.getDefault().unregister(this);
134 + WarplyManager.sendSteps(this);
140 } 135 }
141 136
142 @Override 137 @Override
143 protected void onDestroy() { 138 protected void onDestroy() {
144 super.onDestroy(); 139 super.onDestroy();
145 if (metersHandler != null) metersHandler.removeCallbacksAndMessages(null); 140 if (metersHandler != null) metersHandler.removeCallbacksAndMessages(null);
146 - sendSteps(); 141 +// WarplyManager.sendSteps(this);
147 WarpUtils.setWebviewParams(this, new JSONObject()); 142 WarpUtils.setWebviewParams(this, new JSONObject());
148 setWebviewSupermarket(false); 143 setWebviewSupermarket(false);
149 } 144 }
...@@ -346,26 +341,4 @@ public class WarpViewActivity extends WarpBaseActivity { ...@@ -346,26 +341,4 @@ public class WarpViewActivity extends WarpBaseActivity {
346 public static void setWebviewSupermarket(boolean isLoaded) { 341 public static void setWebviewSupermarket(boolean isLoaded) {
347 mWebviewSupermarket = isLoaded; 342 mWebviewSupermarket = isLoaded;
348 } 343 }
349 -
350 - private void sendSteps() {
351 - String date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.getDefault()).format(new Date());
352 - WarpUtils.log("SEND_STEPS: " + String.valueOf(WarpUtils.getStepsCounter(this)));
353 - if (WarpUtils.getStepsCounter(this) > 0) {
354 - WarplyManager.setPacingDetails(new PacingCalculateRequest().setCounter(WarpUtils.getStepsCounter(this)).setDate(date), new CallbackReceiver<JSONObject>() {
355 - @Override
356 - public void onSuccess(JSONObject result) {
357 - int status = result.optInt("status", 2);
358 - if (status == 1) {
359 - WarplyManagerHelper.mSteps = 0;
360 - WarpUtils.setStepsCounter(WarpViewActivity.this, 0);
361 - }
362 - }
363 -
364 - @Override
365 - public void onFailure(int errorCode) {
366 - WarpUtils.log("Warply Health Service error: " + String.valueOf(errorCode));
367 - }
368 - });
369 - }
370 - }
371 } 344 }
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -20,23 +20,12 @@ import android.os.IBinder; ...@@ -20,23 +20,12 @@ import android.os.IBinder;
20 import androidx.annotation.Nullable; 20 import androidx.annotation.Nullable;
21 import androidx.core.app.NotificationCompat; 21 import androidx.core.app.NotificationCompat;
22 22
23 -import org.greenrobot.eventbus.EventBus;
24 -import org.json.JSONObject;
25 -
26 -import java.text.SimpleDateFormat;
27 -import java.util.Date;
28 -import java.util.Locale;
29 -
30 import ly.warp.sdk.R; 23 import ly.warp.sdk.R;
31 import ly.warp.sdk.Warply; 24 import ly.warp.sdk.Warply;
32 import ly.warp.sdk.io.callbacks.CallbackReceiver; 25 import ly.warp.sdk.io.callbacks.CallbackReceiver;
33 -import ly.warp.sdk.io.models.HealthEventModel;
34 import ly.warp.sdk.io.models.PacingDetails; 26 import ly.warp.sdk.io.models.PacingDetails;
35 -import ly.warp.sdk.io.request.PacingCalculateRequest;
36 -import ly.warp.sdk.io.request.PacingDetailsRequest;
37 import ly.warp.sdk.utils.WarpUtils; 27 import ly.warp.sdk.utils.WarpUtils;
38 import ly.warp.sdk.utils.WarplyManagerHelper; 28 import ly.warp.sdk.utils.WarplyManagerHelper;
39 -import ly.warp.sdk.utils.managers.WarplyEventBusManager;
40 import ly.warp.sdk.utils.managers.WarplyManager; 29 import ly.warp.sdk.utils.managers.WarplyManager;
41 30
42 /** 31 /**
...@@ -88,11 +77,24 @@ public class WarplyHealthService extends Service implements SensorEventListener ...@@ -88,11 +77,24 @@ public class WarplyHealthService extends Service implements SensorEventListener
88 mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 77 mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
89 mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 78 mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
90 Warply.getInitializer(getApplicationContext()).init(); 79 Warply.getInitializer(getApplicationContext()).init();
91 - WarplyManager.getPacingDetails(new PacingDetailsRequest(), mPacingCallback); 80 +
81 + /** 1. We get all daily steps from backend so we
82 + * can send them with event bus to the client app
83 + * and show them on their home card.
84 + * Comment this line if registerStepSensor() below
85 + * is going to be used instead. */
86 +// WarplyManager.getPacingDetails(new PacingDetailsRequest(), mPacingCallback);
87 +
88 + /** 2. We register our sensor to start giving data.
89 + * Comment this line if WarplyManager.getPacingDetails()
90 + * above is going to be used instead. */
91 + registerStepSensor();
92 } 92 }
93 93
94 @Override 94 @Override
95 public int onStartCommand(Intent intent, int flags, int startId) { 95 public int onStartCommand(Intent intent, int flags, int startId) {
96 + /** 3. We create a sticky notification so the
97 + * service can run on background */
96 NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 98 NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
97 PackageManager pm = getPackageManager(); 99 PackageManager pm = getPackageManager();
98 Intent bIntent = pm.getLaunchIntentForPackage(getPackageName()); 100 Intent bIntent = pm.getLaunchIntentForPackage(getPackageName());
...@@ -129,27 +131,12 @@ public class WarplyHealthService extends Service implements SensorEventListener ...@@ -129,27 +131,12 @@ public class WarplyHealthService extends Service implements SensorEventListener
129 } 131 }
130 } 132 }
131 133
132 - /** Old Implementation */ 134 + /** We save all the steps every 15 minutes */
133 -// new Thread(
134 -// () -> {
135 -// while (true) {
136 -// sendSteps();
137 -// try {
138 -// Thread.sleep(900000);
139 -// } catch (InterruptedException e) {
140 -// e.printStackTrace();
141 -// }
142 -// }
143 -// }
144 -// ).start();
145 -
146 - /** New Implementation */
147 - // Use Handler instead of Thread for periodic tasks
148 final Handler handler = new Handler(); 135 final Handler handler = new Handler();
149 Runnable runnable = new Runnable() { 136 Runnable runnable = new Runnable() {
150 @Override 137 @Override
151 public void run() { 138 public void run() {
152 - sendSteps(); 139 + WarplyManager.sendSteps(WarplyHealthService.this);
153 handler.postDelayed(this, 900000); 140 handler.postDelayed(this, 900000);
154 } 141 }
155 }; 142 };
...@@ -168,7 +155,7 @@ public class WarplyHealthService extends Service implements SensorEventListener ...@@ -168,7 +155,7 @@ public class WarplyHealthService extends Service implements SensorEventListener
168 eventMeters = 0.0d; 155 eventMeters = 0.0d;
169 pacingMeters = 0.0d; 156 pacingMeters = 0.0d;
170 sumSteps = 0; 157 sumSteps = 0;
171 - sendSteps(); 158 + WarplyManager.sendSteps(this);
172 } 159 }
173 160
174 @Nullable 161 @Nullable
...@@ -237,20 +224,25 @@ public class WarplyHealthService extends Service implements SensorEventListener ...@@ -237,20 +224,25 @@ public class WarplyHealthService extends Service implements SensorEventListener
237 if (velocityEstimate > STEP_THRESHOLD 224 if (velocityEstimate > STEP_THRESHOLD
238 && oldVelocityEstimate <= STEP_THRESHOLD 225 && oldVelocityEstimate <= STEP_THRESHOLD
239 && (timeNs - lastStepTimeNs > STEP_DELAY_NS)) { 226 && (timeNs - lastStepTimeNs > STEP_DELAY_NS)) {
227 +
228 + /** These 2 lines have moved inside WarpView passMeters */
229 +// WarplyManagerHelper.mSteps++;
230 +// WarpUtils.setStepsCounter(this, WarplyManagerHelper.mSteps);
231 +
240 WarplyManagerHelper.mStepsWebview++; 232 WarplyManagerHelper.mStepsWebview++;
241 - sumSteps++; 233 + WarpUtils.log("WEBVIEW_STEPS: " + String.valueOf(WarplyManagerHelper.mStepsWebview));
242 - WarplyManagerHelper.mSteps++;
243 - WarpUtils.log("COUNT_STEPS: " + String.valueOf(WarplyManagerHelper.mSteps));
244 - WarpUtils.setStepsCounter(this, WarplyManagerHelper.mSteps);
245 WarplyManagerHelper.mMetersWebview = (WarplyManagerHelper.mStepsWebview * 0.762); 234 WarplyManagerHelper.mMetersWebview = (WarplyManagerHelper.mStepsWebview * 0.762);
246 WarpUtils.log("WEBVIEW_METERS: " + String.valueOf(WarplyManagerHelper.mMetersWebview)); 235 WarpUtils.log("WEBVIEW_METERS: " + String.valueOf(WarplyManagerHelper.mMetersWebview));
247 - HealthEventModel healthSteps = new HealthEventModel(); 236 +
248 -// healthSteps.setMeters((mStepsAll * 0.762)); // 16/09/2022 237 + /** 5. We send overall steps to the client card.
249 -// healthSteps.setMeters(WarplyManagerHelper.mMetersWebview); // 20/09/2022 238 + * Needs step 1. */
250 - eventMeters = (sumSteps * 0.762); 239 +// HealthEventModel healthSteps = new HealthEventModel();
251 - WarpUtils.log("WIDGET_METERS: " + String.valueOf(pacingMeters + eventMeters)); 240 +// sumSteps++;
252 - healthSteps.setMeters(pacingMeters + eventMeters); 241 +// healthSteps.setMeters((mStepsAll * 0.762));
253 - EventBus.getDefault().post(new WarplyEventBusManager(healthSteps)); 242 +// healthSteps.setMeters(WarplyManagerHelper.mMetersWebview);
243 +// eventMeters = (sumSteps * 0.762);
244 +// healthSteps.setMeters(pacingMeters + eventMeters);
245 +// EventBus.getDefault().post(new WarplyEventBusManager(healthSteps));
254 246
255 lastStepTimeNs = timeNs; 247 lastStepTimeNs = timeNs;
256 } 248 }
...@@ -294,35 +286,12 @@ public class WarplyHealthService extends Service implements SensorEventListener ...@@ -294,35 +286,12 @@ public class WarplyHealthService extends Service implements SensorEventListener
294 return retval; 286 return retval;
295 } 287 }
296 288
297 - private void sendSteps() {
298 - String date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.getDefault()).format(new Date());
299 -
300 - if (WarpUtils.getStepsCounter(this) > 0) {
301 - WarplyManager.setPacingDetails(new PacingCalculateRequest()
302 - .setCounter(WarpUtils.getStepsCounter(this))
303 - .setDate(date),
304 - new CallbackReceiver<JSONObject>() {
305 - @Override
306 - public void onSuccess(JSONObject result) {
307 - int status = result.optInt("status", 2);
308 - if (status == 1) {
309 - WarplyManagerHelper.mSteps = 0;
310 - WarpUtils.setStepsCounter(WarplyHealthService.this, 0);
311 - }
312 - }
313 -
314 - @Override
315 - public void onFailure(int errorCode) {
316 - WarpUtils.log("Warply Health Service error: " + String.valueOf(errorCode));
317 - }
318 - });
319 - }
320 - }
321 -
322 private CallbackReceiver<PacingDetails> mPacingCallback = new CallbackReceiver<PacingDetails>() { 289 private CallbackReceiver<PacingDetails> mPacingCallback = new CallbackReceiver<PacingDetails>() {
323 @Override 290 @Override
324 public void onSuccess(PacingDetails result) { 291 public void onSuccess(PacingDetails result) {
325 pacingMeters = result.getMeters().getDay().getValue(); 292 pacingMeters = result.getMeters().getDay().getValue();
293 +
294 + /** 2. We register our sensor to start giving data */
326 registerStepSensor(); 295 registerStepSensor();
327 } 296 }
328 297
......
...@@ -654,13 +654,13 @@ public class WarpUtils { ...@@ -654,13 +654,13 @@ public class WarpUtils {
654 return prefs.getString(PREFERENCES_KEY_WEBVIEW_PARAMS, ""); 654 return prefs.getString(PREFERENCES_KEY_WEBVIEW_PARAMS, "");
655 } 655 }
656 656
657 - public static void setStepsCounter(Context context, int counter) { 657 + public static synchronized void setStepsCounter(Context context, int counter) {
658 SharedPreferences.Editor editor = getPreferences(context).edit(); 658 SharedPreferences.Editor editor = getPreferences(context).edit();
659 editor.putInt(PREFERENCES_STEPS_COUNTER, counter); 659 editor.putInt(PREFERENCES_STEPS_COUNTER, counter);
660 editor.apply(); 660 editor.apply();
661 } 661 }
662 662
663 - public static int getStepsCounter(Context context) { 663 + public static synchronized int getStepsCounter(Context context) {
664 SharedPreferences prefs = getPreferences(context); 664 SharedPreferences prefs = getPreferences(context);
665 return prefs.getInt(PREFERENCES_STEPS_COUNTER, 0); 665 return prefs.getInt(PREFERENCES_STEPS_COUNTER, 0);
666 } 666 }
......
...@@ -58,6 +58,7 @@ import java.util.Collections; ...@@ -58,6 +58,7 @@ import java.util.Collections;
58 import java.util.Date; 58 import java.util.Date;
59 import java.util.LinkedHashSet; 59 import java.util.LinkedHashSet;
60 import java.util.List; 60 import java.util.List;
61 +import java.util.Locale;
61 import java.util.Map; 62 import java.util.Map;
62 import java.util.Set; 63 import java.util.Set;
63 import java.util.concurrent.ExecutorService; 64 import java.util.concurrent.ExecutorService;
...@@ -5117,6 +5118,31 @@ public class WarplyManager { ...@@ -5117,6 +5118,31 @@ public class WarplyManager {
5117 }, null); 5118 }, null);
5118 } 5119 }
5119 5120
5121 + public static void sendSteps(Context context) {
5122 + String date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.getDefault()).format(new Date());
5123 +
5124 + if (WarpUtils.getStepsCounter(context) > 0) {
5125 + setPacingDetails(new PacingCalculateRequest()
5126 + .setCounter(WarpUtils.getStepsCounter(context))
5127 + .setDate(date),
5128 + new CallbackReceiver<JSONObject>() {
5129 + @Override
5130 + public void onSuccess(JSONObject result) {
5131 + int status = result.optInt("status", 2);
5132 + if (status == 1) {
5133 + WarplyManagerHelper.mSteps = 0;
5134 + WarpUtils.setStepsCounter(context, 0);
5135 + }
5136 + }
5137 +
5138 + @Override
5139 + public void onFailure(int errorCode) {
5140 + WarpUtils.log("Warply Health Service error: " + String.valueOf(errorCode));
5141 + }
5142 + });
5143 + }
5144 + }
5145 +
5120 private static boolean isMyServiceRunning(Class<?> serviceClass) { 5146 private static boolean isMyServiceRunning(Class<?> serviceClass) {
5121 ActivityManager manager = (ActivityManager) Warply.getWarplyContext().getSystemService(Context.ACTIVITY_SERVICE); 5147 ActivityManager manager = (ActivityManager) Warply.getWarplyContext().getSystemService(Context.ACTIVITY_SERVICE);
5122 for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { 5148 for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
......
...@@ -826,11 +826,16 @@ public class WarpView extends WebView implements DefaultLifecycleObserver { ...@@ -826,11 +826,16 @@ public class WarpView extends WebView implements DefaultLifecycleObserver {
826 WarpView.this.evaluateJavascript(scriptSourceMeters, s -> { 826 WarpView.this.evaluateJavascript(scriptSourceMeters, s -> {
827 827
828 }); 828 });
829 +
830 + /** These 2 lines have moved here from WarplyHealthService */
831 + WarplyManagerHelper.mSteps += WarplyManagerHelper.mStepsWebview;
832 + WarpUtils.setStepsCounter(getContext(), WarplyManagerHelper.mSteps);
833 +
829 WarplyManagerHelper.mStepsWebview = 0; 834 WarplyManagerHelper.mStepsWebview = 0;
830 WarplyManagerHelper.mMetersWebview = 0.0d; 835 WarplyManagerHelper.mMetersWebview = 0.0d;
831 WarpViewActivity.getMetersHandler().postDelayed(this, 300); 836 WarpViewActivity.getMetersHandler().postDelayed(this, 300);
832 } 837 }
833 - }, 3000); //TODO: change it back to 1000 838 + }, 1000);
834 839
835 if (url.contains("about:blank")) { 840 if (url.contains("about:blank")) {
836 WarpView.this.clearHistory(); 841 WarpView.this.clearHistory();
......