diff --git a/app/src/main/java/neu/madcourse/walkwithme/MainActivity.java b/app/src/main/java/neu/madcourse/walkwithme/MainActivity.java index 7e45611..199ec37 100644 --- a/app/src/main/java/neu/madcourse/walkwithme/MainActivity.java +++ b/app/src/main/java/neu/madcourse/walkwithme/MainActivity.java @@ -160,6 +160,9 @@ protected void onStop() { editor.commit(); Log.v(TAG, "On Stop, Starting CheckRecentRun service..."); startService(new Intent(this, CheckAppRunService.class)); + Intent startIntent = new Intent(this, StepService3.class); + startIntent.setAction(Constants.START_FOREGROUND); + startService(startIntent); } diff --git a/app/src/main/java/neu/madcourse/walkwithme/Test/StepService3.java b/app/src/main/java/neu/madcourse/walkwithme/Test/StepService3.java index 14b97c6..f588d6e 100644 --- a/app/src/main/java/neu/madcourse/walkwithme/Test/StepService3.java +++ b/app/src/main/java/neu/madcourse/walkwithme/Test/StepService3.java @@ -52,6 +52,7 @@ public class StepService3 extends Service implements SensorEventListener { private Sensor stepCounter; int[] historyData = new int[6]; + private String timestamp = ""; //Variables used in calculations @@ -120,9 +121,9 @@ public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG,"starting service"); break; - case Constants.RESET_COUNT : - resetCount(); - break; +// case Constants.RESET_COUNT : +// resetCount(); +// break; case Constants.STOP_SAVE_COUNT : stopForegroundService(true); @@ -180,17 +181,22 @@ public void onSensorChanged(SensorEvent event) { } - final String timestamp = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime()); + final String now = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime()); // step_ref.child("Total Steps").setValue(totalStep); // step_ref.child("Step Count").child(timestamp).child("steps").setValue(step); + if(now.equals(timestamp)) { - step_ref.child("Total Steps").setValue(totalStep); - step_ref.child("Step Count").child(timestamp).child("steps").setValue(step); + step_ref.child("Total Steps").setValue(totalStep); + step_ref.child("Step Count").child(timestamp).child("steps").setValue(step); + }else{ + resetSteps(); + } // step_ref.child("Step Count").child(timestamp).child("steps").setValue(step); // step_ref.child("Total Steps").setValue(totalStep); - historyData[FIVE_DAY] = step; + historyData[FIVE_DAY] = step; + } } @@ -217,13 +223,6 @@ public void stopForegroundService(boolean update){ updateSteps(); } - public void resetCount(){ - step = 0; - //distance = 0; - //startTime = SystemClock.uptimeMillis(); - //updatedTime = elapsedTime; - } - //Runnable that calculates the elapsed time since the user presses the "start" button private Runnable timerRunnable = new Runnable() { @Override @@ -318,7 +317,7 @@ public int[] getDays(){ private void updateSteps(){ try { - final String timestamp = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime()); + timestamp = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime()); Steps steps = new Steps(step, timestamp); step_ref.child("Step Count").child(timestamp).setValue(steps); step_ref.child("Total Steps").setValue(totalStep); @@ -326,6 +325,15 @@ private void updateSteps(){ } } + + private void resetSteps(){ + //Daily reset step to 0 + Log.d(TAG, "resetSteps: "); + timestamp = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime()); + step = 0; + Steps steps = new Steps(step, timestamp); + step_ref.child("Step Count").child(timestamp).setValue(steps); + } // private Notification getNotification(String title, String body, int id){ // @@ -366,7 +374,7 @@ private void updateSteps(){ private void accessData(){ - final String timestamp = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime()); + timestamp = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime()); Log.d(TAG,"access firebase data"); try{ step_ref.addValueEventListener(new ValueEventListener() { @@ -394,9 +402,9 @@ public void onDataChange(@NonNull DataSnapshot dataSnapshot) { Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -i); Date todate1 = cal.getTime(); - String timestamp = new SimpleDateFormat("yyyy-MM-dd").format(todate1); - if (dataSnapshot.child("Step Count").child(timestamp).exists()) { - Steps steps = dataSnapshot.child("Step Count").child(timestamp).getValue(Steps.class); + String tmptimestamp = new SimpleDateFormat("yyyy-MM-dd").format(todate1); + if (dataSnapshot.child("Step Count").child(tmptimestamp).exists()) { + Steps steps = dataSnapshot.child("Step Count").child(tmptimestamp).getValue(Steps.class); step = (int) steps.getSteps(); tmp[FIVE_DAY - i] = step; } diff --git a/app/src/main/java/neu/madcourse/walkwithme/Test/StepsFragment2.java b/app/src/main/java/neu/madcourse/walkwithme/Test/StepsFragment2.java index 15f352a..c43c707 100644 --- a/app/src/main/java/neu/madcourse/walkwithme/Test/StepsFragment2.java +++ b/app/src/main/java/neu/madcourse/walkwithme/Test/StepsFragment2.java @@ -170,14 +170,14 @@ public void onClick(View v) { } }); - Button resetButton = (Button) view.findViewById(R.id.resetButton); - resetButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - if(isBound && service.isActive()) - service.resetCount(); - } - }); +// Button resetButton = (Button) view.findViewById(R.id.resetButton); +// resetButton.setOnClickListener(new View.OnClickListener() { +// @Override +// public void onClick(View v) { +// if(isBound && service.isActive()) +// service.resetCount(); +// } +// }); //drawChart();