Blame view

app/src/main/java/com/dinhcv/lifelogpedometer/activity/TopTodayFragment.java 22.6 KB
0c1f9bf91   Dinh Chu   update top handle
1
2
3
  package com.dinhcv.lifelogpedometer.activity;
  
  import android.app.DatePickerDialog;
cb2ba72a7   chudinhbka@gmail.com   update handle cou...
4
  import android.app.ProgressDialog;
0c1f9bf91   Dinh Chu   update top handle
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  import android.content.Context;
  import android.graphics.Color;
  import android.hardware.Sensor;
  import android.hardware.SensorEvent;
  import android.hardware.SensorEventListener;
  import android.hardware.SensorManager;
  import android.os.Bundle;
  import android.support.annotation.Nullable;
  import android.view.LayoutInflater;
  import android.view.Menu;
  import android.view.MenuInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.DatePicker;
  import android.widget.ImageView;
  import android.widget.LinearLayout;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import com.dinhcv.lifelogpedometer.R;
  import com.dinhcv.lifelogpedometer.interfaces.LLAPIManagerListener;
790fc3bf2   Dinh Chu   update: fix bug c...
26
  import com.dinhcv.lifelogpedometer.interfaces.StepListener;
0c1f9bf91   Dinh Chu   update top handle
27
28
29
  import com.dinhcv.lifelogpedometer.model.StepModel;
  import com.dinhcv.lifelogpedometer.model.structure.top.StepItemInfo;
  import com.dinhcv.lifelogpedometer.model.structure.top.TopInfo;
5520a4b27   Dinh Chu   update
30
  import com.dinhcv.lifelogpedometer.portal.ApiServices;
0c1f9bf91   Dinh Chu   update top handle
31
32
33
34
  import com.dinhcv.lifelogpedometer.portal.LLAPIManager;
  import com.dinhcv.lifelogpedometer.utils.Const;
  import com.dinhcv.lifelogpedometer.utils.DayAxisValueFormatter;
  import com.dinhcv.lifelogpedometer.utils.Debug;
790fc3bf2   Dinh Chu   update: fix bug c...
35
  import com.dinhcv.lifelogpedometer.utils.SimpleStepDetector;
0c1f9bf91   Dinh Chu   update top handle
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  import com.dinhcv.lifelogpedometer.utils.Utils;
  import com.github.mikephil.charting.components.XAxis;
  import com.github.mikephil.charting.components.YAxis;
  
  import com.github.mikephil.charting.charts.BarChart;
  import com.github.mikephil.charting.data.BarData;
  import com.github.mikephil.charting.data.BarDataSet;
  import com.github.mikephil.charting.data.BarEntry;
  import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
  
  import org.eazegraph.lib.charts.PieChart;
  import org.eazegraph.lib.models.PieModel;
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import java.text.NumberFormat;
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Calendar;
  import java.util.Date;
  import java.util.List;
  import java.util.Locale;
  
  import static com.github.mikephil.charting.utils.ColorTemplate.rgb;
790fc3bf2   Dinh Chu   update: fix bug c...
61
  public class TopTodayFragment extends FragmentBase implements SettingFragmentPresenter, SensorEventListener, StepListener {
0c1f9bf91   Dinh Chu   update top handle
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  
      private TextView stepsView;
      private TextView tvStepGoal;
      private TextView tvDistance;
      private TextView tvTime;
      private TextView tvStepRemain;
      private TextView tvStepRateDone;
      private boolean showSteps = true;
      private PieModel sliceGoal, sliceCurrent;
      private PieChart pg;
      private TextView tvDate;
      private ImageView ivBack;
      private ImageView ivNext;
  
      private LinearLayout llBike;
      private LinearLayout llWalking;
      private LinearLayout llRunning;
      private Const.STEP_TYPE stepType;
  
      private Date mAnaDate;
      private Calendar mCalendar;
  
      private int mAnaDay;
      private int mAnaMonth;
      private int mAnaYear;
  
      public static int STEP_SIZE = 75;
      private StepModel mStepModel;
      private TextView tvSmallStepGoal;
      private TextView tvSmallRemain;
      private Context mContext;
  
      public final static NumberFormat formatter = NumberFormat.getInstance(Locale.getDefault());
      private SensorManager sensorManager;
      private boolean activityRunning;
      private int stepTotal = 0;
cb2ba72a7   chudinhbka@gmail.com   update handle cou...
98
      private int stepGoal = 0;
0c1f9bf91   Dinh Chu   update top handle
99
100
101
102
103
104
105
106
107
108
109
      private int stepCount = 0;
  
      private Date mFromDate;
      private Date mToDate;
  
      private TopInfo mTopInfo = new TopInfo();
      private BarChart mChart;
  
      private List<String> dateList;
      private String[] mParties;
      private Integer[] mStep;
cb2ba72a7   chudinhbka@gmail.com   update handle cou...
110
      private ProgressDialog progress;
790fc3bf2   Dinh Chu   update: fix bug c...
111
112
113
      private boolean isAccelo = false;
      private boolean isBusy = false;
      private SimpleStepDetector simpleStepDetector;
0c1f9bf91   Dinh Chu   update top handle
114
115
116
117
118
119
120
121
122
123
  
      private TopFragment mTopFragment;
      public void setRootFragment(TopFragment frag) {
          this.mTopFragment = frag;
      }
  
      @Override
      public void onCreate(final Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setHasOptionsMenu(true);
790fc3bf2   Dinh Chu   update: fix bug c...
124

0c1f9bf91   Dinh Chu   update top handle
125
126
127
128
129
130
131
132
      }
  
      @Override
      public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
                               final Bundle savedInstanceState) {
          final View v = inflater.inflate(R.layout.fragment_top_today, null);
          mContext = getActivity();
          sensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
790fc3bf2   Dinh Chu   update: fix bug c...
133
134
          simpleStepDetector = new SimpleStepDetector();
          simpleStepDetector.registerListener(this);
0c1f9bf91   Dinh Chu   update top handle
135
136
137
138
139
140
141
142
143
          initView(v);
  
          // slice for the steps taken today
          sliceCurrent = new PieModel("", 0, Color.parseColor("#6FE7F7"));
          pg.addPieSlice(sliceCurrent);
  
          // slice for the "missing" steps until reaching the goal
          sliceGoal = new PieModel("", Const.STEP_GOAL, Color.parseColor("#B7B8B6"));
          pg.addPieSlice(sliceGoal);
5520a4b27   Dinh Chu   update
144

0c1f9bf91   Dinh Chu   update top handle
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
          pg.setDrawValueInPie(false);
          pg.setUsePieRotation(false);
          pg.startAnimation();
          pg.setAutoCenterInSlice(false);
  
          mCalendar = Calendar.getInstance();
          mCalendar.setTime(new Date());
          mAnaDate = mCalendar.getTime();
          mAnaYear = mCalendar.get(Calendar.YEAR);
          mAnaMonth = mCalendar.get(Calendar.MONTH);
          mAnaDay = mCalendar.get(Calendar.DAY_OF_MONTH);
  
          mFromDate = new Date();
  
          tvDate.setText(Utils.dateToStringFormatDayMonthYearJp(mAnaDate));
  
          initData();
  
          handleEvent();
  
          getTopPage(mAnaDate, stepType);
  
          return v;
      }
  
      private void initView(View v){
          stepsView = (TextView) v.findViewById(R.id.steps);
          tvDistance = (TextView) v.findViewById(R.id.tv_distance);
          tvStepGoal = (TextView) v.findViewById(R.id.tv_stepGoal);
          tvStepRemain = (TextView) v.findViewById(R.id.tv_stepRemain);
          tvStepRateDone = (TextView) v.findViewById(R.id.tv_stepRateDone);
          tvSmallStepGoal = (TextView) v.findViewById(R.id.tv_smallStepGoal);
          tvSmallRemain = (TextView) v.findViewById(R.id.tv_smallRemain);
          tvTime = (TextView) v.findViewById(R.id.tv_time);
          tvDate = (TextView) v.findViewById(R.id.tv_date);
          ivBack = (ImageView) v.findViewById(R.id.iv_back);
          ivNext = (ImageView) v.findViewById(R.id.iv_next);
  
          mChart = (BarChart) v.findViewById(R.id.chart);
          pg = (PieChart) v.findViewById(R.id.graph);
  
          llRunning = (LinearLayout) v.findViewById(R.id.ll_running);
          llWalking = (LinearLayout) v.findViewById(R.id.ll_walking);
          llBike = (LinearLayout) v.findViewById(R.id.ll_bike);
      }
  
      private void initData(){
          mCalendar = Calendar.getInstance();
          mCalendar.setTime(new Date());
          mAnaDate = mCalendar.getTime();
          mAnaYear = mCalendar.get(Calendar.YEAR);
          mAnaMonth = mCalendar.get(Calendar.MONTH);
          mAnaDay = mCalendar.get(Calendar.DAY_OF_MONTH);
  
  
          stepType = Const.STEP_TYPE.WALKING;
          updateUiStepType(false, true, false);
  
          tvDate.setText(Utils.dateToStringFormatDayMonthYearJp(mAnaDate));
      }
  
      private void updateUiStepType(boolean b1, boolean b2, boolean b3) {
          llBike.setSelected(b1);
          llWalking.setSelected(b2);
          llRunning.setSelected(b3);
      }
  
      private void stepsDistanceChanged() {
cb2ba72a7   chudinhbka@gmail.com   update handle cou...
213
214
215
          if (progress != null){
              progress.dismiss();
          }
0c1f9bf91   Dinh Chu   update top handle
216
217
218
219
220
221
222
223
224
225
226
          updatePie();
          updateBars();
      }
  
  
      @Override
      public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
          inflater.inflate(R.menu.main, menu);
      }
  
      private void getTopPage(Date date, Const.STEP_TYPE stepType){
cb2ba72a7   chudinhbka@gmail.com   update handle cou...
227
228
229
230
          progress = new ProgressDialog(mContext);
          progress.setMessage(getString(R.string.loading));
          progress.setCancelable(false);
          progress.show();
5520a4b27   Dinh Chu   update
231
          ApiServices.topInfo(date, stepType, new LLAPIManagerListener() {
0c1f9bf91   Dinh Chu   update top handle
232
233
234
235
236
237
238
239
240
241
              @Override
              public void onError(Error error) {
                  Debug.error("Get data history error");
                  hiddenDialog();
                  showDialogNotData();
              }
  
              @Override
              public void onSuccess(String json) {
                  Debug.error("Get data history success");
0c1f9bf91   Dinh Chu   update top handle
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
                  loadDataDone(json);
              }
  
              @Override
              public void onSuccess(JSONObject object) {
                  Debug.error("Get data history success");
                  hiddenDialog();
              }
          });
      }
  
      private void showDialogNotData(){
          showAlerDialog(mContext, getResources().getString(R.string.can_not_get_data));
      }
  
      private void loadDataDone(String jsonString) {
          JSONObject jsonObject = null;
          try {
              jsonObject = new JSONObject(jsonString);
              int status = jsonObject.optInt("status");
              if (status == 1) {
                  JSONObject jsonObject1 = jsonObject.optJSONObject("result");
                  JSONObject targetInf = jsonObject1.getJSONObject("targetInf");
  
                  if (targetInf != null){
                      String target = targetInf.optString("target_step");
                      Debug.normal("Target: "+ target);
                      mTopInfo.setTaget(targetInf.optString("target_step"));
                      mTopInfo.setSteps(targetInf.optString("num_step"));
                      mTopInfo.setStepRemain(targetInf.optString("remaining_step"));
                      mTopInfo.setCompletePercent(targetInf.optString("complete_percent"));
                  }
  
                  mTopInfo.setKcalo(jsonObject1.optString("kcal"));
                  mTopInfo.setDistance(jsonObject1.optString("distance"));
                  mTopInfo.setTime(jsonObject1.optString("time"));
                  JSONArray stepArr = jsonObject1.optJSONArray("dataChart");
  
  
                  if (stepArr != null && stepArr.length() > 0) {
                      List<StepItemInfo> infoLists = new ArrayList<>();
                      for (int i = 0; i < stepArr.length(); i++){
                          StepItemInfo stepItemInfo = new StepItemInfo();
                          JSONObject ob = (JSONObject) stepArr.get(i);
                          stepItemInfo.setHour(ob.optInt("hour"));
                          stepItemInfo.setNumStep(ob.optInt("numStep"));
                          infoLists.add(stepItemInfo);
                      }
  
                      mTopInfo.setStepList(infoLists);
                  }
  
              }
          } catch (JSONException e) {
              e.printStackTrace();
              mTopInfo = new TopInfo();;
          }
  
          stepsDistanceChanged();
      }
  
      private void updatePie() {
          // todayOffset might still be Integer.MIN_VALUE on first start
          int stepsToday = 0;
          int stepTarget = 0;
          if (mTopInfo != null) {
cb2ba72a7   chudinhbka@gmail.com   update handle cou...
308
309
310
311
312
              if (mTopInfo.getSteps().contains(",")){
                  stepsToday = Integer.valueOf(mTopInfo.getSteps().replace(",", ""));
              }else {
                  stepsToday = Integer.valueOf(mTopInfo.getSteps());
              }
0c1f9bf91   Dinh Chu   update top handle
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
              if (mTopInfo.getTaget().contains(",")) {
                  stepTarget = Integer.valueOf(mTopInfo.getTaget().replace(",", ""));
              }else {
                  stepTarget = Integer.valueOf(mTopInfo.getTaget());
              }
          }
          sliceCurrent.setValue(stepsToday);
          if (stepTarget - stepsToday > 0) {
              // goal not reached yet
              if (pg.getData().size() == 1) {
                  pg.addPieSlice(sliceGoal);
              }
              sliceGoal.setValue(stepTarget - stepsToday);
          } else {
              // goal reached
              pg.clearChart();
              pg.addPieSlice(sliceCurrent);
          }
          pg.update();
cb2ba72a7   chudinhbka@gmail.com   update handle cou...
332
333
          stepGoal = stepTarget;
          stepTotal = stepsToday;
0c1f9bf91   Dinh Chu   update top handle
334
          stepsView.setText(formatter.format(stepsToday));
cb2ba72a7   chudinhbka@gmail.com   update handle cou...
335
336
337
338
          int stepRemain = stepTarget - stepsToday;
          tvStepRemain.setText(String.valueOf(stepRemain));
          tvStepRateDone.setText(getResources().getString(R.string.percent_unit, Utils.convert2String2Decimal(stepsToday *100/ stepTarget)));
          tvSmallRemain.setText(getResources().getString(R.string.pie_text_content3a, stepRemain));
0c1f9bf91   Dinh Chu   update top handle
339
340
          tvStepGoal.setText(mTopInfo.getTaget());
          tvSmallStepGoal.setText(mTopInfo.getTaget());
cb2ba72a7   chudinhbka@gmail.com   update handle cou...
341

0c1f9bf91   Dinh Chu   update top handle
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
          tvDistance.setText(mTopInfo.getDistance());
          tvTime.setText(mTopInfo.getTime());
  
          double distanceToday = stepsToday * STEP_SIZE;
          distanceToday /= 100000;
          tvDistance.setText(Utils.convert2String2Decimal(distanceToday));
  
      }
  
      /**
       * Updates the bar graph to show the steps/distance of the last week. Should
       * be called when switching from step count to distance.
       */
      private void updateBars() {
  
          dateList = new ArrayList<>();
          List<String> dateList = new ArrayList<>();
          List<Integer> stepList = new ArrayList<>();
          List<StepItemInfo> stepItemInfos = mTopInfo.getStepList();
          if (stepItemInfos != null && stepItemInfos.size() >0) {
              for (int i = 0; i < stepItemInfos.size(); i++){
                  dateList.add(String.valueOf(stepItemInfos.get(i).getHour()));
                  stepList.add(stepItemInfos.get(i).getNumStep());
              }
          }
  
          mStep = stepList.toArray(new Integer[0]);
          mParties = dateList.toArray(new String[0]);
  
          initGraph();
      }
  
      private void initGraph(){
  
          mChart.setDrawBarShadow(false);
          mChart.setDrawValueAboveBar(true);
  
          // if more than 60 entries are displayed in the chart, no values will be
          // drawn
          mChart.setMaxVisibleValueCount(60);
  
          mChart.getDescription().setEnabled(false);
          // scaling can now only be done on x- and y-axis separately
          mChart.setPinchZoom(false);
  
          mChart.setDrawGridBackground(false);
          // mChart.setDrawYLabels(false);
          List<String> listx = Arrays.asList(mParties);
          DayAxisValueFormatter xValueFormatter = new DayAxisValueFormatter(listx);
  
          XAxis xAxis = mChart.getXAxis();
          xAxis.setLabelRotationAngle(0);
          xAxis.setPosition(XAxis.XAxisPosition.TOP);
          xAxis.setDrawGridLines(true);
          xAxis.setLabelCount(10);
          xAxis.setTextColor(Color.WHITE);
          xAxis.setValueFormatter(xValueFormatter);
  
  
          YAxis leftAxis = mChart.getAxisLeft();
          leftAxis.setDrawLabels(false);
          leftAxis.setDrawGridLines(false);
          leftAxis.setAxisMinimum(0f);
          leftAxis.setAxisMaximum(10000f);
          leftAxis.setDrawZeroLine(false);
  
          leftAxis.setEnabled(false);
          mChart.setDrawValueAboveBar(false);
          mChart.getAxisRight().setEnabled(false);
  
          // set auto scale min max
          mChart.setAutoScaleMinMaxEnabled(true);
          mChart.notifyDataSetChanged();
  
          // Set enimate y
          mChart.animateY(2000);
  
          setData();
  
          mChart.getLegend().setEnabled(false);
      }
  
  
      private void setData() {
  
          ArrayList<String> xVals = new ArrayList<>();
          for (int i = 0; i < mStep.length; i++) {
              xVals.add(mParties[i % mStep.length]);
          }
  
          ArrayList<BarEntry> yVals1 = new ArrayList<>();
          for (int i = 0; i < mStep.length; i++) {
              float val = (float) (mStep[i]*1);
              yVals1.add(new BarEntry(i, val));
          }
  
          BarDataSet set1;
  
          if (mChart.getData() != null && mChart.getData().getDataSetCount() > 0) {
              set1 = (BarDataSet)mChart.getData().getDataSetByIndex(0);
              set1.setValues(yVals1);
              mChart.getData().notifyDataChanged();
              mChart.notifyDataSetChanged();
          } else {
              ArrayList<Integer> colors = new ArrayList<>();
  
  
              int[] MATERIAL_COLORS = {rgb("#40CDEF")};
  
              for (int c : MATERIAL_COLORS)
                  colors.add(c);
  
              set1 = new BarDataSet(yVals1, null);
  
              set1.setColors(colors);
  
              ArrayList<IBarDataSet> dataSets = new ArrayList<>();
              dataSets.add(set1);
  
              BarData data = new BarData(dataSets);
              data.setValueTextSize(10f);
  
              mChart.setData(data);
          }
      }
  
  
  
      private void handleEvent(){
          tvDate.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  handleAnaDatePicker();
              }
          });
  
          ivBack.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  mCalendar = Calendar.getInstance();
                  mCalendar.setTime(mAnaDate);
                  mCalendar.add(Calendar.DAY_OF_MONTH, -1);
                  Date date = mCalendar.getTime();
                  tvDate.setText(Utils.dateToStringFormatDayMonthYearJp(date));
                  mAnaDate = date;
790fc3bf2   Dinh Chu   update: fix bug c...
487
488
  
                  getTopPage(mAnaDate, stepType);
0c1f9bf91   Dinh Chu   update top handle
489
490
491
492
493
494
495
496
497
498
499
500
              }
          });
  
          ivNext.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  mCalendar = Calendar.getInstance();
                  mCalendar.setTime(mAnaDate);
                  mCalendar.add(Calendar.DAY_OF_MONTH, +1);
                  Date date = mCalendar.getTime();
                  tvDate.setText(Utils.dateToStringFormatDayMonthYearJp(date));
                  mAnaDate = date;
790fc3bf2   Dinh Chu   update: fix bug c...
501
502
  
                  getTopPage(mAnaDate, stepType);
0c1f9bf91   Dinh Chu   update top handle
503
504
505
506
507
508
509
              }
          });
  
          llBike.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  stepType = Const.STEP_TYPE.BIKE;
cb2ba72a7   chudinhbka@gmail.com   update handle cou...
510
                  updateUiStepType(true, false, false);
0c1f9bf91   Dinh Chu   update top handle
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
                  getTopPage(mAnaDate, stepType);
              }
          });
  
          llRunning.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  stepType = Const.STEP_TYPE.RUNNING;
                  updateUiStepType(false, false, true);
                  getTopPage(mAnaDate, stepType);
              }
          });
  
          llWalking.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  stepType = Const.STEP_TYPE.WALKING;
cb2ba72a7   chudinhbka@gmail.com   update handle cou...
528
                  updateUiStepType(false, true, false);
0c1f9bf91   Dinh Chu   update top handle
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
                  getTopPage(mAnaDate, stepType);
              }
          });
  
  
      }
  
  
      /**
       * Show date picker dialog
       */
      private void handleAnaDatePicker() {
  
          new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
              @Override
              public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
  
                  mCalendar = Calendar.getInstance();
                  mCalendar.set(year, monthOfYear, dayOfMonth);
                  mAnaYear = year;
                  mAnaMonth = monthOfYear;
                  mAnaDay = dayOfMonth;
                  Date date = mCalendar.getTime();
                  tvDate.setText(Utils.dateToStringFormatDayMonthYearJp(date));
                  mAnaDate = date;
                  getTopPage(mAnaDate, stepType);
              }
          }, mAnaYear, mAnaMonth, mAnaDay).show();
  
      }
  
  
      private void updateUI(){
790fc3bf2   Dinh Chu   update: fix bug c...
562
          creatLogStep();
cb2ba72a7   chudinhbka@gmail.com   update handle cou...
563
564
565
          Debug.normal("Step total================ "+ stepTotal);
          int stepRemain  = stepGoal - stepTotal;
          double percentDone = stepTotal *100.0 / stepGoal;
0c1f9bf91   Dinh Chu   update top handle
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
          stepsView.setText(String.valueOf(stepTotal));
          tvStepRemain.setText(String.valueOf(stepRemain));
          tvStepRateDone.setText(getResources().getString(R.string.percent_unit, Utils.convert2String2Decimal(percentDone)));
          tvSmallRemain.setText(String.valueOf(stepRemain));
      }
  
      @Override
      public void onAttach(Context context) {
          super.onAttach(context);
  
      }
  
      @Override
      public void onSaveData() {
  
      }
  
      @Override
      public void onInvalidate(boolean isInit) {
          initData();
      }
  
      @Override
      public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
          super.onViewStateRestored(savedInstanceState);
  
          initData();
      }
  
  
      @Override
      public void onResume() {
          super.onResume();
          activityRunning = true;
5520a4b27   Dinh Chu   update
600
          Debug.normal("Today: "+ "Onresume ");
790fc3bf2   Dinh Chu   update: fix bug c...
601

0c1f9bf91   Dinh Chu   update top handle
602
          Sensor countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
790fc3bf2   Dinh Chu   update: fix bug c...
603
604
605
          if (countSensor == null) {
              isAccelo = true;
              countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
0c1f9bf91   Dinh Chu   update top handle
606
          }
790fc3bf2   Dinh Chu   update: fix bug c...
607
608
609
610
611
612
613
              if (countSensor != null) {
                  Debug.normal("Today: " + "countSensor not null ");
                  sensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_UI);
              } else {
                  Debug.normal("Today: " + "countSensor is null ");
                  Toast.makeText(mContext, getResources().getString(R.string.sensor_available), Toast.LENGTH_SHORT).show();
              }
0c1f9bf91   Dinh Chu   update top handle
614
615
616
617
      }
  
      @Override
      public void onSensorChanged(SensorEvent event) {
790fc3bf2   Dinh Chu   update: fix bug c...
618
          //Debug.normal("Sensor count step active");
0c1f9bf91   Dinh Chu   update top handle
619
          if (activityRunning){
790fc3bf2   Dinh Chu   update: fix bug c...
620
621
622
623
624
625
626
627
628
629
              if (isAccelo) {
                  simpleStepDetector.updateAccel(
                          event.timestamp, event.values[0], event.values[1], event.values[2]);
              }else {
                  stepCount = (int) event.values[0];
                  Debug.normal("Step change: " + stepCount);
                  stepTotal++;
                  // update UI
                  updateUI();
              }
0c1f9bf91   Dinh Chu   update top handle
630
631
632
633
634
635
636
637
638
639
640
641
642
          }
      }
  
      @Override
      public void onAccuracyChanged(Sensor sensor, int accuracy) {
  
      }
  
      @Override
      public void onPause() {
          super.onPause();
          activityRunning = false;
          Debug.normal("Start create log");
cb2ba72a7   chudinhbka@gmail.com   update handle cou...
643
644
645
          if (sensorManager != null) {
              sensorManager.unregisterListener(this);
          }
0c1f9bf91   Dinh Chu   update top handle
646
          // create log step
790fc3bf2   Dinh Chu   update: fix bug c...
647
          //creatLogStep();
0c1f9bf91   Dinh Chu   update top handle
648
649
650
651
652
653
654
655
      }
  
      @Override
      public void onDestroy() {
          super.onDestroy();
      }
  
      private void creatLogStep(){
5520a4b27   Dinh Chu   update
656
          Debug.normal("Today: "+ "Create log "+ stepCount);
790fc3bf2   Dinh Chu   update: fix bug c...
657
          mToDate = new Date();
0c1f9bf91   Dinh Chu   update top handle
658

5520a4b27   Dinh Chu   update
659
          ApiServices.createLog(stepType, stepCount, mFromDate, mToDate, new LLAPIManagerListener() {
0c1f9bf91   Dinh Chu   update top handle
660
661
              @Override
              public void onError(Error error) {
790fc3bf2   Dinh Chu   update: fix bug c...
662
                  isBusy = false;
0c1f9bf91   Dinh Chu   update top handle
663
664
665
666
667
                  Debug.error("Get data history error");
              }
  
              @Override
              public void onSuccess(String json) {
790fc3bf2   Dinh Chu   update: fix bug c...
668
                  isBusy = false;
0c1f9bf91   Dinh Chu   update top handle
669
670
671
672
673
                  Debug.error("Get data history success");
              }
  
              @Override
              public void onSuccess(JSONObject object) {
790fc3bf2   Dinh Chu   update: fix bug c...
674
                  isBusy = false;
0c1f9bf91   Dinh Chu   update top handle
675
676
677
678
                  Debug.error("Get data history success");
              }
          });
      }
5520a4b27   Dinh Chu   update
679

790fc3bf2   Dinh Chu   update: fix bug c...
680
681
682
683
684
685
686
687
688
689
      @Override
      public void step(long timeNs) {
          if (!isBusy) {
              isBusy = true;
              stepCount = 1;
              stepTotal++;
              // update UI
              updateUI();
          }
      }
0c1f9bf91   Dinh Chu   update top handle
690
  }