Compare View

switch
from
...
to
 
Commits (2)

Changes

Showing 12 changed files Side-by-side Diff

app/src/main/java/com/dinhcv/lifelogpedometer/activity/DateFragment.java
... ... @@ -4,6 +4,10 @@ import android.app.DatePickerDialog;
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 @@ import android.widget.DatePicker;
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;
... ... @@ -280,14 +285,13 @@ public class DateFragment extends FragmentBase implements SettingFragmentPresent
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,4 +338,5 @@ public class DateFragment extends FragmentBase implements SettingFragmentPresent
334 338 super.onActivityResult(requestCode, resultCode, data);
335 339  
336 340 }
  341 +
337 342 }
app/src/main/java/com/dinhcv/lifelogpedometer/activity/TopFragment.java
... ... @@ -3,6 +3,10 @@ package com.dinhcv.lifelogpedometer.activity;
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;
... ... @@ -13,13 +17,20 @@ import android.view.View;
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.model.structure.top.StepHourInfo;
  29 +import com.dinhcv.lifelogpedometer.model.structure.top.TagetTopInfo;
  30 +import com.dinhcv.lifelogpedometer.portal.LLAPIManager;
21 31 import com.dinhcv.lifelogpedometer.utils.Const;
22 32 import com.dinhcv.lifelogpedometer.utils.DayAxisValueFormatter;
  33 +import com.dinhcv.lifelogpedometer.utils.Debug;
23 34 import com.dinhcv.lifelogpedometer.utils.Utils;
24 35 import com.github.mikephil.charting.components.XAxis;
25 36 import com.github.mikephil.charting.components.YAxis;
... ... @@ -32,6 +43,9 @@ import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
32 43  
33 44 import org.eazegraph.lib.charts.PieChart;
34 45 import org.eazegraph.lib.models.PieModel;
  46 +import org.json.JSONArray;
  47 +import org.json.JSONException;
  48 +import org.json.JSONObject;
35 49  
36 50 import java.text.NumberFormat;
37 51 import java.util.ArrayList;
... ... @@ -43,9 +57,10 @@ import java.util.Locale;
43 57 import static com.github.mikephil.charting.utils.ColorTemplate.rgb;
44 58  
45 59  
46   -public class TopFragment extends FragmentBase implements SettingFragmentPresenter {
  60 +public class TopFragment extends FragmentBase implements SettingFragmentPresenter, SensorEventListener {
47 61  
48 62 private TextView stepsView;
  63 + private TextView tvStepGoal;
49 64 private TextView tvDistance;
50 65 private TextView tvStepRemain;
51 66 private TextView tvStepRateDone;
... ... @@ -57,6 +72,10 @@ public class TopFragment extends FragmentBase implements SettingFragmentPresente
57 72 private ImageView ivNext;
58 73 private ImageView ivPlay;
59 74  
  75 + private LinearLayout llBike;
  76 + private LinearLayout llWalking;
  77 + private LinearLayout llRunning;
  78 + private Const.STEP_TYPE stepType;
60 79  
61 80 private Date mAnaDate;
62 81 private Calendar mCalendar;
... ... @@ -65,11 +84,27 @@ public class TopFragment extends FragmentBase implements SettingFragmentPresente
65 84 private int mAnaMonth;
66 85 private int mAnaYear;
67 86  
68   - private int todayOffset, total_start, since_boot;
69   - public final static NumberFormat formatter = NumberFormat.getInstance(Locale.getDefault());
70 87 public static int STEP_SIZE = 75;
71 88 private StepModel mStepModel;
  89 + private TextView tvSmallStepGoal;
72 90 private TextView tvSmallRemain;
  91 + private Context mContext;
  92 +
  93 + private SensorManager sensorManager;
  94 + private boolean activityRunning;
  95 + private int stepTotal = 0;
  96 + private int stepRemain = 0;
  97 + private int stepCount = 0;
  98 +
  99 + private Date mFromDate;
  100 + private Date mToDate;
  101 +
  102 + private TagetTopInfo mTagetTopInfo = new TagetTopInfo();
  103 + private BarChart mChart;
  104 +
  105 + private List<String> dateList;
  106 + private String[] mParties;
  107 + private Integer[] mStep;
73 108  
74 109 @Override
75 110 public void onCreate(final Bundle savedInstanceState) {
... ... @@ -80,19 +115,11 @@ public class TopFragment extends FragmentBase implements SettingFragmentPresente
80 115 @Override
81 116 public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
82 117 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);
  118 + final View v = inflater.inflate(R.layout.fragment_top, null);
  119 + mContext = getActivity();
  120 + sensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
93 121  
94   - mChart = (BarChart) v.findViewById(R.id.chart);
95   - pg = (PieChart) v.findViewById(R.id.graph);
  122 + initView(v);
96 123  
97 124 // slice for the steps taken today
98 125 sliceCurrent = new PieModel("", 0, Color.parseColor("#6FE7F7"));
... ... @@ -120,6 +147,23 @@ public class TopFragment extends FragmentBase implements SettingFragmentPresente
120 147 return v;
121 148 }
122 149  
  150 + private void initView(View v){
  151 + stepsView = (TextView) v.findViewById(R.id.steps);
  152 + tvDistance = (TextView) v.findViewById(R.id.tv_distance);
  153 + tvStepGoal = (TextView) v.findViewById(R.id.tv_stepGoal);
  154 + tvStepRemain = (TextView) v.findViewById(R.id.tv_stepRemain);
  155 + tvStepRateDone = (TextView) v.findViewById(R.id.tv_stepRateDone);
  156 + tvSmallStepGoal = (TextView) v.findViewById(R.id.tv_smallStepGoal);
  157 + tvSmallRemain = (TextView) v.findViewById(R.id.tv_smallRemain);
  158 + tvDate = (TextView) v.findViewById(R.id.tv_date);
  159 + ivBack = (ImageView) v.findViewById(R.id.iv_back);
  160 + ivNext = (ImageView) v.findViewById(R.id.iv_next);
  161 + ivPlay = (ImageView) v.findViewById(R.id.iv_play);
  162 +
  163 + mChart = (BarChart) v.findViewById(R.id.chart);
  164 + pg = (PieChart) v.findViewById(R.id.graph);
  165 + }
  166 +
123 167 private void stepsDistanceChanged() {
124 168 updatePie();
125 169 updateBars();
... ... @@ -187,11 +231,6 @@ public class TopFragment extends FragmentBase implements SettingFragmentPresente
187 231 initGraph();
188 232 }
189 233  
190   - private BarChart mChart;
191   -
192   - private List<String> dateList;
193   - private String[] mParties;
194   - private Integer[] mStep;
195 234  
196 235 private void initGraph(){
197 236  
... ... @@ -321,9 +360,45 @@ public class TopFragment extends FragmentBase implements SettingFragmentPresente
321 360 }
322 361 });
323 362  
  363 + llWalking.setOnClickListener(new View.OnClickListener() {
  364 + @Override
  365 + public void onClick(View view) {
  366 + stepType = Const.STEP_TYPE.WALKING;
  367 + updateUiStepType(false, true, false);
  368 + // add data
  369 + getTopData(mAnaDate, stepType);
  370 + }
  371 + });
  372 +
  373 + llRunning.setOnClickListener(new View.OnClickListener() {
  374 + @Override
  375 + public void onClick(View view) {
  376 + stepType = Const.STEP_TYPE.RUNNING;
  377 + updateUiStepType(false, false, true);
  378 + // add
  379 + getTopData(mAnaDate, stepType);
  380 + }
  381 + });
  382 +
  383 + llBike.setOnClickListener(new View.OnClickListener() {
  384 + @Override
  385 + public void onClick(View view) {
  386 + stepType = Const.STEP_TYPE.BIKE;
  387 + updateUiStepType(true, false, false);
  388 + // add data
  389 + getTopData(mAnaDate, stepType);
  390 + }
  391 + });
  392 +
324 393  
325 394 }
326 395  
  396 + private void updateUiStepType(boolean b1, boolean b2, boolean b3) {
  397 + llBike.setSelected(b1);
  398 + llWalking.setSelected(b2);
  399 + llRunning.setSelected(b3);
  400 + }
  401 +
327 402  
328 403 /**
329 404 * Show date picker dialog
... ... @@ -347,7 +422,100 @@ public class TopFragment extends FragmentBase implements SettingFragmentPresente
347 422  
348 423 }
349 424  
  425 + private void getTopData(Date date, Const.STEP_TYPE stepType){
350 426  
  427 + showDialog(mContext);
  428 + LLAPIManager.topInfo(date, stepType, new LLAPIManagerListener() {
  429 + @Override
  430 + public void onError(Error error) {
  431 + Debug.error("Get data history error");
  432 + hiddenDialog();
  433 + showDialogNotData();
  434 + }
  435 +
  436 + @Override
  437 + public void onSuccess(String json) {
  438 + Debug.error("Get data history success");
  439 + hiddenDialog();
  440 + loadDataDone(json);
  441 + }
  442 +
  443 + @Override
  444 + public void onSuccess(JSONObject object) {
  445 + Debug.error("Get data history success");
  446 + hiddenDialog();
  447 + }
  448 + });
  449 + }
  450 +
  451 + private void showDialogNotData(){
  452 + showAlerDialog(mContext, getResources().getString(R.string.can_not_get_data));
  453 + }
  454 +
  455 + private void loadDataDone(String jsonString) {
  456 + JSONObject jsonObject = null;
  457 + try {
  458 + jsonObject = new JSONObject(jsonString);
  459 + int status = jsonObject.optInt("status");
  460 + if (status == 1) {
  461 + JSONObject jsonObject1 = jsonObject.optJSONObject("result");
  462 + JSONObject targetInf = jsonObject1.getJSONObject("targetInf");
  463 + JSONArray listNotice = jsonObject1.getJSONArray("listNotice");
  464 +
  465 + if (targetInf != null){
  466 + String target = targetInf.optString("target_step");
  467 + Debug.normal("Target: "+ target);
  468 + mTagetTopInfo.setTaget(targetInf.optString("target_step"));
  469 + mTagetTopInfo.setSteps(targetInf.optString("num_step"));
  470 + mTagetTopInfo.setStepRemain(targetInf.optString("remaining_step"));
  471 + mTagetTopInfo.setCompletePercent(targetInf.optString("complete_percent"));
  472 + }
  473 +
  474 + if (listNotice != null && listNotice.length() > 0) {
  475 + List<StepHourInfo> infoLists = new ArrayList<>();
  476 + for (int i = 0; i < listNotice.length(); i++){
  477 + StepHourInfo noticeInfo = new StepHourInfo();
  478 + JSONObject ob = (JSONObject) listNotice.get(i);
  479 + noticeInfo.setId(ob.optInt("id"));
  480 + noticeInfo.setContent(ob.optString("notice_content"));
  481 + infoLists.add(noticeInfo);
  482 + }
  483 +
  484 + mTagetTopInfo.setStepHourList(infoLists);
  485 + }
  486 +
  487 + }
  488 + } catch (JSONException e) {
  489 + e.printStackTrace();
  490 + mTagetTopInfo = new TagetTopInfo();;
  491 + }
  492 +
  493 + loadUI();
  494 + }
  495 +
  496 +
  497 + private void loadUI(){
  498 + tvStep.setText(String.valueOf(mTagetInfo.getSteps()));
  499 + tvRemain.setText(String.valueOf(mTagetInfo.getStepRemain()));
  500 + tvRateDone.setText(getResources().getString(R.string.percent_unit, mTagetInfo.getCompletePercent()));
  501 +
  502 + List<StepHourInfo> infoLists = mTagetTopInfo.getStepHourList();
  503 + if (infoLists != null && infoLists.size() >0){
  504 + mNoticeAdapter = new NoticeAdapter(mContext, infoLists);
  505 + lvNotice.setAdapter(mNoticeAdapter);
  506 + lvNotice.setExpanded(true);
  507 + }
  508 +
  509 + }
  510 +
  511 + private void updateUI(){
  512 + stepTotal = stepTotal + stepCount;
  513 + stepRemain = stepRemain - stepCount;
  514 + double percentDone = stepTotal *100.0 / (stepTotal + stepRemain);
  515 + tvStep.setText(String.valueOf(stepTotal));
  516 + tvRemain.setText(mTagetInfo.getStepRemain());
  517 + tvRateDone.setText(getResources().getString(R.string.percent_unit, Utils.convert2String2Decimal(percentDone)));
  518 + }
351 519  
352 520 @Override
353 521 public void onAttach(Context context) {
... ... @@ -376,4 +544,64 @@ public class TopFragment extends FragmentBase implements SettingFragmentPresente
376 544 initData();
377 545 }
378 546  
  547 +
  548 + @Override
  549 + public void onResume() {
  550 + super.onResume();
  551 + activityRunning = true;
  552 + Sensor countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
  553 + if (countSensor != null) {
  554 + sensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_UI);
  555 + } else {
  556 + Toast.makeText(mContext, getResources().getString(R.string.sensor_available), Toast.LENGTH_SHORT).show();
  557 + }
  558 + }
  559 +
  560 + @Override
  561 + public void onSensorChanged(SensorEvent event) {
  562 + if (activityRunning){
  563 + stepCount = (int) event.values[0];
  564 + // update UI
  565 + updateUI();
  566 + }
  567 + }
  568 +
  569 + @Override
  570 + public void onAccuracyChanged(Sensor sensor, int accuracy) {
  571 +
  572 + }
  573 +
  574 + @Override
  575 + public void onPause() {
  576 + super.onPause();
  577 + activityRunning = false;
  578 + }
  579 +
  580 + @Override
  581 + public void onDestroy() {
  582 + super.onDestroy();
  583 + // create log step
  584 + creatLogStep();
  585 + }
  586 +
  587 + private void creatLogStep(){
  588 +
  589 + LLAPIManager.createLog(stepType, stepCount, mFromDate, mToDate, new LLAPIManagerListener() {
  590 + @Override
  591 + public void onError(Error error) {
  592 + Debug.error("Get data history error");
  593 + }
  594 +
  595 + @Override
  596 + public void onSuccess(String json) {
  597 + Debug.error("Get data history success");
  598 + }
  599 +
  600 + @Override
  601 + public void onSuccess(JSONObject object) {
  602 + Debug.error("Get data history success");
  603 + }
  604 + });
  605 + }
  606 +
379 607 }
app/src/main/java/com/dinhcv/lifelogpedometer/model/structure/top/StepHourInfo.java
... ... @@ -0,0 +1,23 @@
  1 +package com.dinhcv.lifelogpedometer.model.structure.top;
  2 +
  3 +public class StepHourInfo {
  4 + private int hour;
  5 + private int step;
  6 +
  7 + public int getHour() {
  8 + return hour;
  9 + }
  10 +
  11 + public void setHour(int hour) {
  12 + this.hour = hour;
  13 + }
  14 +
  15 + public int getStep() {
  16 + return step;
  17 + }
  18 +
  19 + public void setStep(int step) {
  20 + this.step = step;
  21 + }
  22 +
  23 +}
app/src/main/java/com/dinhcv/lifelogpedometer/model/structure/top/TagetTopInfo.java
... ... @@ -0,0 +1,83 @@
  1 +package com.dinhcv.lifelogpedometer.model.structure.top;
  2 +
  3 +import com.dinhcv.lifelogpedometer.model.structure.home.NoticeInfo;
  4 +
  5 +import java.util.List;
  6 +
  7 +public class TagetTopInfo {
  8 + private String taget;
  9 + private String steps;
  10 + private String stepRemain;
  11 + private String completePercent;
  12 + private String calo;
  13 + private String distance;
  14 + private String time;
  15 +
  16 + private List<StepHourInfo> stepHourList;
  17 +
  18 +
  19 + public String getTaget() {
  20 + return taget;
  21 + }
  22 +
  23 + public void setTaget(String taget) {
  24 + this.taget = taget;
  25 + }
  26 +
  27 + public String getSteps() {
  28 + return steps;
  29 + }
  30 +
  31 + public void setSteps(String steps) {
  32 + this.steps = steps;
  33 + }
  34 +
  35 + public String getStepRemain() {
  36 + return stepRemain;
  37 + }
  38 +
  39 + public void setStepRemain(String stepRemain) {
  40 + this.stepRemain = stepRemain;
  41 + }
  42 +
  43 + public String getCompletePercent() {
  44 + return completePercent;
  45 + }
  46 +
  47 + public void setCompletePercent(String completePercent) {
  48 + this.completePercent = completePercent;
  49 + }
  50 +
  51 + public String getCalo() {
  52 + return calo;
  53 + }
  54 +
  55 + public void setCalo(String calo) {
  56 + this.calo = calo;
  57 + }
  58 +
  59 + public String getDistance() {
  60 + return distance;
  61 + }
  62 +
  63 + public void setDistance(String distance) {
  64 + this.distance = distance;
  65 + }
  66 +
  67 + public String getTime() {
  68 + return time;
  69 + }
  70 +
  71 + public void setTime(String taget) {
  72 + this.time = time;
  73 + }
  74 +
  75 + public void setStepHourList(List<StepHourInfo> stepHourList){
  76 + this.stepHourList = stepHourList;
  77 + }
  78 +
  79 + public List<StepHourInfo> getStepHourList(){
  80 + return stepHourList;
  81 + }
  82 +
  83 +}
app/src/main/java/com/dinhcv/lifelogpedometer/portal/LLAPIManager.java
... ... @@ -61,6 +61,11 @@ public class LLAPIManager {
61 61 public static String URL_FORGET_PASS_CONFIRM_INFO = "forgetPass/confirm";
62 62 // Refesh Token
63 63 public static String URL_REFESH_TOKEN_INFO = "refreshToken";
  64 + // Top info
  65 + public static String URL_TOP_INFO = "api/top";
  66 + // Refesh Token
  67 + public static String URL_CREATE_LOG_INFO = "api/createLog";
  68 +
64 69  
65 70  
66 71 private static int deviationValue = 1;
... ... @@ -756,6 +761,171 @@ public class LLAPIManager {
756 761 }.execute();
757 762 }
758 763  
  764 + public static void topInfo(final Date date, final Const.STEP_TYPE stepType, final LLAPIManagerListener action) {
  765 + new AsyncTask<Void, Void, String>() {
  766 +
  767 +
  768 + @Override
  769 + protected void onPreExecute() {
  770 + super.onPreExecute();
  771 + }
  772 +
  773 + @Override
  774 + protected String doInBackground(Void... voids) {
  775 +
  776 + String dateStr = Utils.dateToStringFormatDayMonthYear(date);
  777 + Debug.normal("Token: "+ APIResponse.getInstance().getToken());
  778 + HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_TOP_INFO + "/"+ stepType.value +"/"+ dateStr);
  779 + Debug.normal("URL: "+baseUrl.toString());
  780 + final Request.Builder request = new Request.Builder()
  781 + .url(baseUrl)
  782 + .header("User-Agent", Utils.getCustomUA())
  783 + .header("Content-Type", "application/json")
  784 + .addHeader("token", APIResponse.getInstance().getToken())
  785 + .get();
  786 +
  787 + OkHttpClient httpClient = new OkHttpClient.Builder()
  788 + .readTimeout(30, TimeUnit.SECONDS)
  789 + .writeTimeout(30, TimeUnit.SECONDS)
  790 + .connectTimeout(30, TimeUnit.SECONDS)
  791 + .build();
  792 +
  793 + String jsonString = null;
  794 + try {
  795 + Response response = httpClient.newCall(request.build()).execute();
  796 + if (response == null) {
  797 + action.onError(null);
  798 + Debug.error("Response is null");
  799 + return null;
  800 + }
  801 + jsonString = getResponseData(response.body());
  802 +
  803 + } catch (IOException e) {
  804 + Debug.normal("Error %s", e.getMessage());
  805 + }
  806 + return jsonString;
  807 + }
  808 +
  809 + @Override
  810 + protected void onCancelled() {
  811 + super.onCancelled();
  812 + action.onError(null);
  813 + }
  814 +
  815 + @Override
  816 + protected void onPostExecute(String jsonString) {
  817 + super.onPostExecute(jsonString);
  818 + if ((jsonString == null) || (jsonString.isEmpty())) {
  819 + action.onError(null);
  820 + return;
  821 + }
  822 + Debug.normal("String body: "+jsonString);
  823 +
  824 + try {
  825 + JSONObject jsonObject = new JSONObject(jsonString);
  826 + int status = jsonObject.optInt("status");
  827 + if (status == 1) {
  828 + action.onSuccess(jsonString);
  829 + } else {
  830 + action.onError(new Error(jsonObject.getString("message")));
  831 + }
  832 + } catch (JSONException e) {
  833 + action.onError(null);
  834 + }
  835 + }
  836 + }.execute();
  837 + }
  838 +
  839 + public static void createLog(final Const.STEP_TYPE stepType, final int stepCount, final Date startTime,
  840 + final Date endTime, final LLAPIManagerListener action) {
  841 + new AsyncTask<Void, Void, String>() {
  842 +
  843 +
  844 + @Override
  845 + protected void onPreExecute() {
  846