Blame view
app/src/main/java/com/dinhcv/lifelogpedometer/activity/TopFragment.java
19.3 KB
7f095a929
|
1 2 3 4 5 |
package com.dinhcv.lifelogpedometer.activity; import android.app.DatePickerDialog; import android.content.Context; import android.graphics.Color; |
54e5fcd56
|
6 7 8 9 |
import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; |
7f095a929
|
10 11 12 13 14 15 16 17 18 19 |
import android.os.Bundle; import android.support.annotation.Nullable; import android.util.Pair; 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; |
54e5fcd56
|
20 |
import android.widget.LinearLayout; |
7f095a929
|
21 |
import android.widget.TextView; |
54e5fcd56
|
22 |
import android.widget.Toast; |
7f095a929
|
23 24 25 |
import com.dinhcv.lifelogpedometer.R; import com.dinhcv.lifelogpedometer.feature.Database; |
54e5fcd56
|
26 |
import com.dinhcv.lifelogpedometer.interfaces.LLAPIManagerListener; |
7f095a929
|
27 |
import com.dinhcv.lifelogpedometer.model.StepModel; |
86ee629d1
|
28 29 |
import com.dinhcv.lifelogpedometer.model.structure.top.StepHourInfo; import com.dinhcv.lifelogpedometer.model.structure.top.TagetTopInfo; |
54e5fcd56
|
30 |
import com.dinhcv.lifelogpedometer.portal.LLAPIManager; |
7f095a929
|
31 32 |
import com.dinhcv.lifelogpedometer.utils.Const; import com.dinhcv.lifelogpedometer.utils.DayAxisValueFormatter; |
54e5fcd56
|
33 |
import com.dinhcv.lifelogpedometer.utils.Debug; |
7f095a929
|
34 35 36 37 38 39 40 41 42 43 44 45 |
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; |
54e5fcd56
|
46 |
import org.json.JSONArray; |
86ee629d1
|
47 |
import org.json.JSONException; |
54e5fcd56
|
48 |
import org.json.JSONObject; |
7f095a929
|
49 50 51 52 53 54 55 56 57 |
import java.text.NumberFormat; import java.util.ArrayList; 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; |
54e5fcd56
|
58 |
public class TopFragment extends FragmentBase implements SettingFragmentPresenter, SensorEventListener { |
7f095a929
|
59 60 |
private TextView stepsView; |
54e5fcd56
|
61 |
private TextView tvStepGoal; |
7f095a929
|
62 63 64 65 66 67 68 69 70 71 |
private TextView tvDistance; 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 ImageView ivPlay; |
54e5fcd56
|
72 73 74 75 |
private LinearLayout llBike; private LinearLayout llWalking; private LinearLayout llRunning; private Const.STEP_TYPE stepType; |
7f095a929
|
76 77 78 79 80 81 82 |
private Date mAnaDate; private Calendar mCalendar; private int mAnaDay; private int mAnaMonth; private int mAnaYear; |
7f095a929
|
83 84 |
public static int STEP_SIZE = 75; private StepModel mStepModel; |
54e5fcd56
|
85 |
private TextView tvSmallStepGoal; |
7f095a929
|
86 |
private TextView tvSmallRemain; |
54e5fcd56
|
87 88 89 90 91 92 93 |
private Context mContext; private SensorManager sensorManager; private boolean activityRunning; private int stepTotal = 0; private int stepRemain = 0; private int stepCount = 0; |
86ee629d1
|
94 95 96 97 |
private Date mFromDate; private Date mToDate; private TagetTopInfo mTagetTopInfo = new TagetTopInfo(); |
54e5fcd56
|
98 99 100 101 102 |
private BarChart mChart; private List<String> dateList; private String[] mParties; private Integer[] mStep; |
7f095a929
|
103 104 105 106 107 108 109 110 111 112 |
@Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); } @Override public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) { |
54e5fcd56
|
113 114 115 |
final View v = inflater.inflate(R.layout.fragment_top, null); mContext = getActivity(); sensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE); |
7f095a929
|
116 |
|
54e5fcd56
|
117 |
initView(v); |
7f095a929
|
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
// 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); 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); tvDate.setText(Utils.dateToStringFormatDayMonthYearJp(mAnaDate)); handleEvent(); return v; } |
54e5fcd56
|
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
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); tvDate = (TextView) v.findViewById(R.id.tv_date); ivBack = (ImageView) v.findViewById(R.id.iv_back); ivNext = (ImageView) v.findViewById(R.id.iv_next); ivPlay = (ImageView) v.findViewById(R.id.iv_play); mChart = (BarChart) v.findViewById(R.id.chart); pg = (PieChart) v.findViewById(R.id.graph); } |
7f095a929
|
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 213 214 215 216 217 218 219 220 221 222 223 224 225 |
private void stepsDistanceChanged() { updatePie(); updateBars(); } @Override public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) { inflater.inflate(R.menu.main, menu); } private void updatePie() { // todayOffset might still be Integer.MIN_VALUE on first start int steps_today = Math.max(todayOffset + since_boot, 0); sliceCurrent.setValue(steps_today); if (Const.STEP_GOAL - steps_today > 0) { // goal not reached yet if (pg.getData().size() == 1) { pg.addPieSlice(sliceGoal); } sliceGoal.setValue(Const.STEP_GOAL - steps_today); } else { // goal reached pg.clearChart(); pg.addPieSlice(sliceCurrent); } pg.update(); stepsView.setText(formatter.format(steps_today)); int remain = Const.STEP_GOAL - steps_today; tvStepRemain.setText(String.valueOf(remain)); tvStepRateDone.setText(getResources().getString(R.string.percent_unit, Utils.convert2String2Decimal(steps_today *100/ Const.STEP_GOAL))); tvSmallRemain.setText(getResources().getString(R.string.pie_text_content3a, remain)); double distance_today = steps_today * STEP_SIZE; distance_today /= 100000; tvDistance.setText(Utils.convert2String2Decimal(distance_today)); } /** * 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<Integer> stepList = new ArrayList<>(); Database db = Database.getInstance(getActivity()); List<Pair<Long, Integer>> last = db.getLastEntries(8, mAnaDate); db.close(); for (int i = last.size() - 1; i > 0; i--) { Pair<Long, Integer> current = last.get(i); int step = current.second; Date date = new Date(current.first); stepList.add(step); dateList.add(String.valueOf(Utils.getDay(date))); } mStep = stepList.toArray(new Integer[0]); mParties = dateList.toArray(new String[0]); initGraph(); } |
7f095a929
|
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
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); DayAxisValueFormatter xValueFormatter = new DayAxisValueFormatter(dateList); 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; } }); 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; } }); |
54e5fcd56
|
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 |
llWalking.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { stepType = Const.STEP_TYPE.WALKING; updateUiStepType(false, true, false); // add data getTopData(mAnaDate, stepType); } }); llRunning.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { stepType = Const.STEP_TYPE.RUNNING; updateUiStepType(false, false, true); // add getTopData(mAnaDate, stepType); } }); llBike.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { stepType = Const.STEP_TYPE.BIKE; updateUiStepType(true, false, false); // add data getTopData(mAnaDate, stepType); } }); } |
7f095a929
|
386 |
|
54e5fcd56
|
387 388 389 390 |
private void updateUiStepType(boolean b1, boolean b2, boolean b3) { llBike.setSelected(b1); llWalking.setSelected(b2); llRunning.setSelected(b3); |
7f095a929
|
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
} /** * 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; } }, mAnaYear, mAnaMonth, mAnaDay).show(); } |
54e5fcd56
|
415 416 417 |
private void getTopData(Date date, Const.STEP_TYPE stepType){ showDialog(mContext); |
86ee629d1
|
418 |
LLAPIManager.topInfo(date, stepType, new LLAPIManagerListener() { |
54e5fcd56
|
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 |
@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"); hiddenDialog(); 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"); JSONArray listNotice = jsonObject1.getJSONArray("listNotice"); if (targetInf != null){ String target = targetInf.optString("target_step"); Debug.normal("Target: "+ target); |
86ee629d1
|
458 459 460 461 |
mTagetTopInfo.setTaget(targetInf.optString("target_step")); mTagetTopInfo.setSteps(targetInf.optString("num_step")); mTagetTopInfo.setStepRemain(targetInf.optString("remaining_step")); mTagetTopInfo.setCompletePercent(targetInf.optString("complete_percent")); |
54e5fcd56
|
462 463 464 |
} if (listNotice != null && listNotice.length() > 0) { |
86ee629d1
|
465 |
List<StepHourInfo> infoLists = new ArrayList<>(); |
54e5fcd56
|
466 |
for (int i = 0; i < listNotice.length(); i++){ |
86ee629d1
|
467 |
StepHourInfo noticeInfo = new StepHourInfo(); |
54e5fcd56
|
468 469 470 471 472 |
JSONObject ob = (JSONObject) listNotice.get(i); noticeInfo.setId(ob.optInt("id")); noticeInfo.setContent(ob.optString("notice_content")); infoLists.add(noticeInfo); } |
86ee629d1
|
473 |
mTagetTopInfo.setStepHourList(infoLists); |
54e5fcd56
|
474 |
} |
7f095a929
|
475 |
|
54e5fcd56
|
476 477 478 |
} } catch (JSONException e) { e.printStackTrace(); |
86ee629d1
|
479 |
mTagetTopInfo = new TagetTopInfo();; |
54e5fcd56
|
480 481 482 483 484 485 486 487 488 489 |
} loadUI(); } private void loadUI(){ tvStep.setText(String.valueOf(mTagetInfo.getSteps())); tvRemain.setText(String.valueOf(mTagetInfo.getStepRemain())); tvRateDone.setText(getResources().getString(R.string.percent_unit, mTagetInfo.getCompletePercent())); |
86ee629d1
|
490 |
List<StepHourInfo> infoLists = mTagetTopInfo.getStepHourList(); |
54e5fcd56
|
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 |
if (infoLists != null && infoLists.size() >0){ mNoticeAdapter = new NoticeAdapter(mContext, infoLists); lvNotice.setAdapter(mNoticeAdapter); lvNotice.setExpanded(true); } } private void updateUI(){ stepTotal = stepTotal + stepCount; stepRemain = stepRemain - stepCount; double percentDone = stepTotal *100.0 / (stepTotal + stepRemain); tvStep.setText(String.valueOf(stepTotal)); tvRemain.setText(mTagetInfo.getStepRemain()); tvRateDone.setText(getResources().getString(R.string.percent_unit, Utils.convert2String2Decimal(percentDone))); } |
7f095a929
|
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 |
@Override public void onAttach(Context context) { super.onAttach(context); } private void initData() { } @Override public void onSaveData() { } @Override public void onInvalidate(boolean isInit) { initData(); } @Override public void onViewStateRestored(@Nullable Bundle savedInstanceState) { super.onViewStateRestored(savedInstanceState); initData(); } |
54e5fcd56
|
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 562 563 564 565 566 567 568 569 |
@Override public void onResume() { super.onResume(); activityRunning = true; Sensor countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER); if (countSensor != null) { sensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_UI); } else { Toast.makeText(mContext, getResources().getString(R.string.sensor_available), Toast.LENGTH_SHORT).show(); } } @Override public void onSensorChanged(SensorEvent event) { if (activityRunning){ stepCount = (int) event.values[0]; // update UI updateUI(); } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } @Override public void onPause() { super.onPause(); activityRunning = false; } @Override public void onDestroy() { super.onDestroy(); |
86ee629d1
|
570 571 |
// create log step creatLogStep(); |
54e5fcd56
|
572 |
} |
86ee629d1
|
573 |
private void creatLogStep(){ |
54e5fcd56
|
574 |
|
86ee629d1
|
575 |
LLAPIManager.createLog(stepType, stepCount, mFromDate, mToDate, new LLAPIManagerListener() { |
54e5fcd56
|
576 577 578 579 580 581 582 583 |
@Override public void onError(Error error) { Debug.error("Get data history error"); } @Override public void onSuccess(String json) { Debug.error("Get data history success"); |
54e5fcd56
|
584 585 586 587 588 589 590 591 |
} @Override public void onSuccess(JSONObject object) { Debug.error("Get data history success"); } }); } |
7f095a929
|
592 |
} |