Commit 54e5fcd562171a4e4441eb6eb480c2f5a558835f

Authored by chudinhbka@gmail.com
1 parent c25bb19fce
Exists in master and in 1 other branch development

handle top api

Showing 8 changed files with 291 additions and 266 deletions Side-by-side Diff

app/src/main/java/com/dinhcv/lifelogpedometer/activity/DateFragment.java
... ... @@ -4,6 +4,10 @@
4 4 import android.content.Context;
5 5 import android.content.Intent;
6 6 import android.content.SharedPreferences;
  7 +import android.hardware.Sensor;
  8 +import android.hardware.SensorEvent;
  9 +import android.hardware.SensorEventListener;
  10 +import android.hardware.SensorManager;
7 11 import android.os.Bundle;
8 12 import android.support.annotation.Nullable;
9 13 import android.support.v4.app.Fragment;
... ... @@ -14,6 +18,7 @@
14 18 import android.widget.ImageView;
15 19 import android.widget.LinearLayout;
16 20 import android.widget.TextView;
  21 +import android.widget.Toast;
17 22  
18 23 import com.dinhcv.lifelogpedometer.R;
19 24 import com.dinhcv.lifelogpedometer.adapter.NoticeAdapter;
20 25  
... ... @@ -280,14 +285,13 @@
280 285 mTagetInfo = new TagetInfo();;
281 286 }
282 287  
283   - updateUI();
  288 + loadUI();
284 289 }
285 290  
286 291  
287   - private void updateUI(){
288   -
289   - tvStep.setText(mTagetInfo.getSteps());
290   - tvRemain.setText(mTagetInfo.getStepRemain());
  292 + private void loadUI(){
  293 + tvStep.setText(String.valueOf(mTagetInfo.getSteps()));
  294 + tvRemain.setText(String.valueOf(mTagetInfo.getStepRemain()));
291 295 tvRateDone.setText(getResources().getString(R.string.percent_unit, mTagetInfo.getCompletePercent()));
292 296  
293 297 List<NoticeInfo> infoLists = mTagetInfo.getNoticeList();
... ... @@ -334,5 +338,6 @@
334 338 super.onActivityResult(requestCode, resultCode, data);
335 339  
336 340 }
  341 +
337 342 }
app/src/main/java/com/dinhcv/lifelogpedometer/activity/MainActivity.java
1   -package com.dinhcv.lifelogpedometer.activity;
2   -
3   -import android.hardware.Sensor;
4   -import android.hardware.SensorEvent;
5   -import android.hardware.SensorEventListener;
6   -import android.hardware.SensorManager;
7   -import android.support.v7.app.AppCompatActivity;
8   -import android.os.Bundle;
9   -import android.view.View;
10   -import android.widget.Button;
11   -import android.widget.TextView;
12   -
13   -import com.dinhcv.lifelogpedometer.R;
14   -
15   -public class MainActivity extends AppCompatActivity implements SensorEventListener, StepListener {
16   - private TextView tvSteps;
17   - private Button btnStart;
18   - private Button btnStop;
19   - private StepDetector simpleStepDetector;
20   - private SensorManager sensorManager;
21   - private Sensor accel;
22   - private static final String TEXT_NUM_STEPS = "Number of Steps: ";
23   - private int numSteps;
24   -
25   - @Override
26   - protected void onCreate(Bundle savedInstanceState) {
27   - super.onCreate(savedInstanceState);
28   - setContentView(R.layout.activity_main);
29   -
30   -
31   - // Get an instance of the SensorManager
32   - sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
33   - accel = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
34   - simpleStepDetector = new StepDetector();
35   - simpleStepDetector.registerListener(this);
36   -
37   - tvSteps = (TextView) findViewById(R.id.tv_steps);
38   - btnStart = (Button) findViewById(R.id.btn_start);
39   - btnStop = (Button) findViewById(R.id.btn_stop);
40   -
41   -
42   -
43   - btnStart.setOnClickListener(new View.OnClickListener() {
44   -
45   - @Override
46   - public void onClick(View arg0) {
47   -
48   - numSteps = 0;
49   - sensorManager.registerListener(MainActivity.this, accel, SensorManager.SENSOR_DELAY_FASTEST);
50   -
51   - }
52   - });
53   -
54   -
55   - btnStop.setOnClickListener(new View.OnClickListener() {
56   -
57   - @Override
58   - public void onClick(View arg0) {
59   -
60   - sensorManager.unregisterListener(MainActivity.this);
61   -
62   - }
63   - });
64   - }
65   -
66   -
67   -
68   - @Override
69   - public void onAccuracyChanged(Sensor sensor, int accuracy) {
70   - }
71   -
72   - @Override
73   - public void onSensorChanged(SensorEvent event) {
74   - if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
75   - simpleStepDetector.updateAccel(
76   - event.timestamp, event.values[0], event.values[1], event.values[2]);
77   - }
78   - }
79   -
80   - @Override
81   - public void step(long timeNs) {
82   - numSteps++;
83   - tvSteps.setText(TEXT_NUM_STEPS + numSteps);
84   - }
85   -
86   -}
app/src/main/java/com/dinhcv/lifelogpedometer/activity/SensorFilter.java
1   -package com.dinhcv.lifelogpedometer.activity;
2   -
3   -public class SensorFilter {
4   -
5   - private SensorFilter() {
6   - }
7   -
8   - public static float sum(float[] array) {
9   - float retval = 0;
10   - for (int i = 0; i < array.length; i++) {
11   - retval += array[i];
12   - }
13   - return retval;
14   - }
15   -
16   - public static float[] cross(float[] arrayA, float[] arrayB) {
17   - float[] retArray = new float[3];
18   - retArray[0] = arrayA[1] * arrayB[2] - arrayA[2] * arrayB[1];
19   - retArray[1] = arrayA[2] * arrayB[0] - arrayA[0] * arrayB[2];
20   - retArray[2] = arrayA[0] * arrayB[1] - arrayA[1] * arrayB[0];
21   - return retArray;
22   - }
23   -
24   - public static float norm(float[] array) {
25   - float retval = 0;
26   - for (int i = 0; i < array.length; i++) {
27   - retval += array[i] * array[i];
28   - }
29   - return (float) Math.sqrt(retval);
30   - }
31   -
32   -
33   - public static float dot(float[] a, float[] b) {
34   - float retval = a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
35   - return retval;
36   - }
37   -
38   - public static float[] normalize(float[] a) {
39   - float[] retval = new float[a.length];
40   - float norm = norm(a);
41   - for (int i = 0; i < a.length; i++) {
42   - retval[i] = a[i] / norm;
43   - }
44   - return retval;
45   - }
46   -
47   -}
app/src/main/java/com/dinhcv/lifelogpedometer/activity/StepDetector.java
1   -package com.dinhcv.lifelogpedometer.activity;
2   -
3   -public class StepDetector {
4   -
5   - private static final int ACCEL_RING_SIZE = 50;
6   - private static final int VEL_RING_SIZE = 10;
7   -
8   - // change this threshold according to your sensitivity preferences
9   - private static final float STEP_THRESHOLD = 50f;
10   -
11   - private static final int STEP_DELAY_NS = 250000000;
12   -
13   - private int accelRingCounter = 0;
14   - private float[] accelRingX = new float[ACCEL_RING_SIZE];
15   - private float[] accelRingY = new float[ACCEL_RING_SIZE];
16   - private float[] accelRingZ = new float[ACCEL_RING_SIZE];
17   - private int velRingCounter = 0;
18   - private float[] velRing = new float[VEL_RING_SIZE];
19   - private long lastStepTimeNs = 0;
20   - private float oldVelocityEstimate = 0;
21   -
22   - private StepListener listener;
23   -
24   - public void registerListener(StepListener listener) {
25   - this.listener = listener;
26   - }
27   -
28   -
29   - public void updateAccel(long timeNs, float x, float y, float z) {
30   - float[] currentAccel = new float[3];
31   - currentAccel[0] = x;
32   - currentAccel[1] = y;
33   - currentAccel[2] = z;
34   -
35   - // First step is to update our guess of where the global z vector is.
36   - accelRingCounter++;
37   - accelRingX[accelRingCounter % ACCEL_RING_SIZE] = currentAccel[0];
38   - accelRingY[accelRingCounter % ACCEL_RING_SIZE] = currentAccel[1];
39   - accelRingZ[accelRingCounter % ACCEL_RING_SIZE] = currentAccel[2];
40   -
41   - float[] worldZ = new float[3];
42   - worldZ[0] = SensorFilter.sum(accelRingX) / Math.min(accelRingCounter, ACCEL_RING_SIZE);
43   - worldZ[1] = SensorFilter.sum(accelRingY) / Math.min(accelRingCounter, ACCEL_RING_SIZE);
44   - worldZ[2] = SensorFilter.sum(accelRingZ) / Math.min(accelRingCounter, ACCEL_RING_SIZE);
45   -
46   - float normalization_factor = SensorFilter.norm(worldZ);
47   -
48   - worldZ[0] = worldZ[0] / normalization_factor;
49   - worldZ[1] = worldZ[1] / normalization_factor;
50   - worldZ[2] = worldZ[2] / normalization_factor;
51   -
52   - float currentZ = SensorFilter.dot(worldZ, currentAccel) - normalization_factor;
53   - velRingCounter++;
54   - velRing[velRingCounter % VEL_RING_SIZE] = currentZ;
55   -
56   - float velocityEstimate = SensorFilter.sum(velRing);
57   -
58   - if (velocityEstimate > STEP_THRESHOLD && oldVelocityEstimate <= STEP_THRESHOLD
59   - && (timeNs - lastStepTimeNs > STEP_DELAY_NS)) {
60   - listener.step(timeNs);
61   - lastStepTimeNs = timeNs;
62   - }
63   - oldVelocityEstimate = velocityEstimate;
64   - }
65   -}
app/src/main/java/com/dinhcv/lifelogpedometer/activity/StepListener.java
1   -package com.dinhcv.lifelogpedometer.activity;
2   -
3   -public interface StepListener {
4   -
5   - public void step(long timeNs);
6   -
7   -}
app/src/main/java/com/dinhcv/lifelogpedometer/activity/TopFragment.java
... ... @@ -3,6 +3,10 @@
3 3 import android.app.DatePickerDialog;
4 4 import android.content.Context;
5 5 import android.graphics.Color;
  6 +import android.hardware.Sensor;
  7 +import android.hardware.SensorEvent;
  8 +import android.hardware.SensorEventListener;
  9 +import android.hardware.SensorManager;
6 10 import android.os.Bundle;
7 11 import android.support.annotation.Nullable;
8 12 import android.util.Pair;
9 13  
10 14  
11 15  
12 16  
... ... @@ -13,13 +17,18 @@
13 17 import android.view.ViewGroup;
14 18 import android.widget.DatePicker;
15 19 import android.widget.ImageView;
  20 +import android.widget.LinearLayout;
16 21 import android.widget.TextView;
  22 +import android.widget.Toast;
17 23  
18 24 import com.dinhcv.lifelogpedometer.R;
19 25 import com.dinhcv.lifelogpedometer.feature.Database;
  26 +import com.dinhcv.lifelogpedometer.interfaces.LLAPIManagerListener;
20 27 import com.dinhcv.lifelogpedometer.model.StepModel;
  28 +import com.dinhcv.lifelogpedometer.portal.LLAPIManager;
21 29 import com.dinhcv.lifelogpedometer.utils.Const;
22 30 import com.dinhcv.lifelogpedometer.utils.DayAxisValueFormatter;
  31 +import com.dinhcv.lifelogpedometer.utils.Debug;
23 32 import com.dinhcv.lifelogpedometer.utils.Utils;
24 33 import com.github.mikephil.charting.components.XAxis;
25 34 import com.github.mikephil.charting.components.YAxis;
... ... @@ -32,6 +41,8 @@
32 41  
33 42 import org.eazegraph.lib.charts.PieChart;
34 43 import org.eazegraph.lib.models.PieModel;
  44 +import org.json.JSONArray;
  45 +import org.json.JSONObject;
35 46  
36 47 import java.text.NumberFormat;
37 48 import java.util.ArrayList;
38 49  
... ... @@ -43,9 +54,10 @@
43 54 import static com.github.mikephil.charting.utils.ColorTemplate.rgb;
44 55  
45 56  
46   -public class TopFragment extends FragmentBase implements SettingFragmentPresenter {
  57 +public class TopFragment extends FragmentBase implements SettingFragmentPresenter, SensorEventListener {
47 58  
48 59 private TextView stepsView;
  60 + private TextView tvStepGoal;
49 61 private TextView tvDistance;
50 62 private TextView tvStepRemain;
51 63 private TextView tvStepRateDone;
... ... @@ -57,6 +69,10 @@
57 69 private ImageView ivNext;
58 70 private ImageView ivPlay;
59 71  
  72 + private LinearLayout llBike;
  73 + private LinearLayout llWalking;
  74 + private LinearLayout llRunning;
  75 + private Const.STEP_TYPE stepType;
60 76  
61 77 private Date mAnaDate;
62 78 private Calendar mCalendar;
63 79  
64 80  
65 81  
... ... @@ -65,12 +81,24 @@
65 81 private int mAnaMonth;
66 82 private int mAnaYear;
67 83  
68   - private int todayOffset, total_start, since_boot;
69   - public final static NumberFormat formatter = NumberFormat.getInstance(Locale.getDefault());
70 84 public static int STEP_SIZE = 75;
71 85 private StepModel mStepModel;
  86 + private TextView tvSmallStepGoal;
72 87 private TextView tvSmallRemain;
  88 + private Context mContext;
73 89  
  90 + private SensorManager sensorManager;
  91 + private boolean activityRunning;
  92 + private int stepTotal = 0;
  93 + private int stepRemain = 0;
  94 + private int stepCount = 0;
  95 +
  96 + private BarChart mChart;
  97 +
  98 + private List<String> dateList;
  99 + private String[] mParties;
  100 + private Integer[] mStep;
  101 +
74 102 @Override
75 103 public void onCreate(final Bundle savedInstanceState) {
76 104 super.onCreate(savedInstanceState);
77 105  
... ... @@ -80,19 +108,11 @@
80 108 @Override
81 109 public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
82 110 final Bundle savedInstanceState) {
83   - final View v = inflater.inflate(com.dinhcv.lifelogpedometer.R.layout.fragment_top, null);
84   - stepsView = (TextView) v.findViewById(R.id.steps);
85   - tvDistance = (TextView) v.findViewById(R.id.tv_distance);
86   - tvStepRemain = (TextView) v.findViewById(R.id.tv_stepRemain);
87   - tvStepRateDone = (TextView) v.findViewById(R.id.tv_stepRateDone);
88   - tvSmallRemain = (TextView) v.findViewById(R.id.tv_smallRemain);
89   - tvDate = (TextView) v.findViewById(R.id.tv_date);
90   - ivBack = (ImageView) v.findViewById(R.id.iv_back);
91   - ivNext = (ImageView) v.findViewById(R.id.iv_next);
92   - ivPlay = (ImageView) v.findViewById(R.id.iv_play);
  111 + final View v = inflater.inflate(R.layout.fragment_top, null);
  112 + mContext = getActivity();
  113 + sensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
93 114  
94   - mChart = (BarChart) v.findViewById(R.id.chart);
95   - pg = (PieChart) v.findViewById(R.id.graph);
  115 + initView(v);
96 116  
97 117 // slice for the steps taken today
98 118 sliceCurrent = new PieModel("", 0, Color.parseColor("#6FE7F7"));
... ... @@ -120,6 +140,23 @@
120 140 return v;
121 141 }
122 142  
  143 + private void initView(View v){
  144 + stepsView = (TextView) v.findViewById(R.id.steps);
  145 + tvDistance = (TextView) v.findViewById(R.id.tv_distance);
  146 + tvStepGoal = (TextView) v.findViewById(R.id.tv_stepGoal);
  147 + tvStepRemain = (TextView) v.findViewById(R.id.tv_stepRemain);
  148 + tvStepRateDone = (TextView) v.findViewById(R.id.tv_stepRateDone);
  149 + tvSmallStepGoal = (TextView) v.findViewById(R.id.tv_smallStepGoal);
  150 + tvSmallRemain = (TextView) v.findViewById(R.id.tv_smallRemain);
  151 + tvDate = (TextView) v.findViewById(R.id.tv_date);
  152 + ivBack = (ImageView) v.findViewById(R.id.iv_back);
  153 + ivNext = (ImageView) v.findViewById(R.id.iv_next);
  154 + ivPlay = (ImageView) v.findViewById(R.id.iv_play);
  155 +
  156 + mChart = (BarChart) v.findViewById(R.id.chart);
  157 + pg = (PieChart) v.findViewById(R.id.graph);
  158 + }
  159 +
123 160 private void stepsDistanceChanged() {
124 161 updatePie();
125 162 updateBars();
126 163  
... ... @@ -187,12 +224,7 @@
187 224 initGraph();
188 225 }
189 226  
190   - private BarChart mChart;
191 227  
192   - private List<String> dateList;
193   - private String[] mParties;
194   - private Integer[] mStep;
195   -
196 228 private void initGraph(){
197 229  
198 230 mChart.setDrawBarShadow(false);
199 231  
200 232  
201 233  
... ... @@ -321,10 +353,46 @@
321 353 }
322 354 });
323 355  
  356 + llWalking.setOnClickListener(new View.OnClickListener() {
  357 + @Override
  358 + public void onClick(View view) {
  359 + stepType = Const.STEP_TYPE.WALKING;
  360 + updateUiStepType(false, true, false);
  361 + // add data
  362 + getTopData(mAnaDate, stepType);
  363 + }
  364 + });
324 365  
  366 + llRunning.setOnClickListener(new View.OnClickListener() {
  367 + @Override
  368 + public void onClick(View view) {
  369 + stepType = Const.STEP_TYPE.RUNNING;
  370 + updateUiStepType(false, false, true);
  371 + // add
  372 + getTopData(mAnaDate, stepType);
  373 + }
  374 + });
  375 +
  376 + llBike.setOnClickListener(new View.OnClickListener() {
  377 + @Override
  378 + public void onClick(View view) {
  379 + stepType = Const.STEP_TYPE.BIKE;
  380 + updateUiStepType(true, false, false);
  381 + // add data
  382 + getTopData(mAnaDate, stepType);
  383 + }
  384 + });
  385 +
  386 +
325 387 }
326 388  
  389 + private void updateUiStepType(boolean b1, boolean b2, boolean b3) {
  390 + llBike.setSelected(b1);
  391 + llWalking.setSelected(b2);
  392 + llRunning.setSelected(b3);
  393 + }
327 394  
  395 +
328 396 /**
329 397 * Show date picker dialog
330 398 */
331 399  
332 400  
... ... @@ -347,8 +415,101 @@
347 415  
348 416 }
349 417  
  418 + private void getTopData(Date date, Const.STEP_TYPE stepType){
350 419  
  420 + showDialog(mContext);
  421 + LLAPIManager.homePage(date, stepType, new LLAPIManagerListener() {
  422 + @Override
  423 + public void onError(Error error) {
  424 + Debug.error("Get data history error");
  425 + hiddenDialog();
  426 + showDialogNotData();
  427 + }
351 428  
  429 + @Override
  430 + public void onSuccess(String json) {
  431 + Debug.error("Get data history success");
  432 + hiddenDialog();
  433 + loadDataDone(json);
  434 + }
  435 +
  436 + @Override
  437 + public void onSuccess(JSONObject object) {
  438 + Debug.error("Get data history success");
  439 + hiddenDialog();
  440 + }
  441 + });
  442 + }
  443 +
  444