Commit 86ee629d1c1927c72d9bdeeaf23808f0684c3e9c
1 parent
54e5fcd562
Exists in
master
and in
1 other branch
update top api
Showing 5 changed files with 302 additions and 16 deletions Inline Diff
- app/src/main/java/com/dinhcv/lifelogpedometer/activity/TopFragment.java
- app/src/main/java/com/dinhcv/lifelogpedometer/model/structure/top/StepHourInfo.java
- app/src/main/java/com/dinhcv/lifelogpedometer/model/structure/top/TagetTopInfo.java
- app/src/main/java/com/dinhcv/lifelogpedometer/portal/LLAPIManager.java
- app/src/main/java/com/dinhcv/lifelogpedometer/utils/Utils.java
app/src/main/java/com/dinhcv/lifelogpedometer/activity/TopFragment.java
| 1 | package com.dinhcv.lifelogpedometer.activity; | 1 | package com.dinhcv.lifelogpedometer.activity; |
| 2 | 2 | ||
| 3 | import android.app.DatePickerDialog; | 3 | import android.app.DatePickerDialog; |
| 4 | import android.content.Context; | 4 | import android.content.Context; |
| 5 | import android.graphics.Color; | 5 | import android.graphics.Color; |
| 6 | import android.hardware.Sensor; | 6 | import android.hardware.Sensor; |
| 7 | import android.hardware.SensorEvent; | 7 | import android.hardware.SensorEvent; |
| 8 | import android.hardware.SensorEventListener; | 8 | import android.hardware.SensorEventListener; |
| 9 | import android.hardware.SensorManager; | 9 | import android.hardware.SensorManager; |
| 10 | import android.os.Bundle; | 10 | import android.os.Bundle; |
| 11 | import android.support.annotation.Nullable; | 11 | import android.support.annotation.Nullable; |
| 12 | import android.util.Pair; | 12 | import android.util.Pair; |
| 13 | import android.view.LayoutInflater; | 13 | import android.view.LayoutInflater; |
| 14 | import android.view.Menu; | 14 | import android.view.Menu; |
| 15 | import android.view.MenuInflater; | 15 | import android.view.MenuInflater; |
| 16 | import android.view.View; | 16 | import android.view.View; |
| 17 | import android.view.ViewGroup; | 17 | import android.view.ViewGroup; |
| 18 | import android.widget.DatePicker; | 18 | import android.widget.DatePicker; |
| 19 | import android.widget.ImageView; | 19 | import android.widget.ImageView; |
| 20 | import android.widget.LinearLayout; | 20 | import android.widget.LinearLayout; |
| 21 | import android.widget.TextView; | 21 | import android.widget.TextView; |
| 22 | import android.widget.Toast; | 22 | import android.widget.Toast; |
| 23 | 23 | ||
| 24 | import com.dinhcv.lifelogpedometer.R; | 24 | import com.dinhcv.lifelogpedometer.R; |
| 25 | import com.dinhcv.lifelogpedometer.feature.Database; | 25 | import com.dinhcv.lifelogpedometer.feature.Database; |
| 26 | import com.dinhcv.lifelogpedometer.interfaces.LLAPIManagerListener; | 26 | import com.dinhcv.lifelogpedometer.interfaces.LLAPIManagerListener; |
| 27 | import com.dinhcv.lifelogpedometer.model.StepModel; | 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; | ||
| 28 | import com.dinhcv.lifelogpedometer.portal.LLAPIManager; | 30 | import com.dinhcv.lifelogpedometer.portal.LLAPIManager; |
| 29 | import com.dinhcv.lifelogpedometer.utils.Const; | 31 | import com.dinhcv.lifelogpedometer.utils.Const; |
| 30 | import com.dinhcv.lifelogpedometer.utils.DayAxisValueFormatter; | 32 | import com.dinhcv.lifelogpedometer.utils.DayAxisValueFormatter; |
| 31 | import com.dinhcv.lifelogpedometer.utils.Debug; | 33 | import com.dinhcv.lifelogpedometer.utils.Debug; |
| 32 | import com.dinhcv.lifelogpedometer.utils.Utils; | 34 | import com.dinhcv.lifelogpedometer.utils.Utils; |
| 33 | import com.github.mikephil.charting.components.XAxis; | 35 | import com.github.mikephil.charting.components.XAxis; |
| 34 | import com.github.mikephil.charting.components.YAxis; | 36 | import com.github.mikephil.charting.components.YAxis; |
| 35 | 37 | ||
| 36 | import com.github.mikephil.charting.charts.BarChart; | 38 | import com.github.mikephil.charting.charts.BarChart; |
| 37 | import com.github.mikephil.charting.data.BarData; | 39 | import com.github.mikephil.charting.data.BarData; |
| 38 | import com.github.mikephil.charting.data.BarDataSet; | 40 | import com.github.mikephil.charting.data.BarDataSet; |
| 39 | import com.github.mikephil.charting.data.BarEntry; | 41 | import com.github.mikephil.charting.data.BarEntry; |
| 40 | import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; | 42 | import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; |
| 41 | 43 | ||
| 42 | import org.eazegraph.lib.charts.PieChart; | 44 | import org.eazegraph.lib.charts.PieChart; |
| 43 | import org.eazegraph.lib.models.PieModel; | 45 | import org.eazegraph.lib.models.PieModel; |
| 44 | import org.json.JSONArray; | 46 | import org.json.JSONArray; |
| 47 | import org.json.JSONException; | ||
| 45 | import org.json.JSONObject; | 48 | import org.json.JSONObject; |
| 46 | 49 | ||
| 47 | import java.text.NumberFormat; | 50 | import java.text.NumberFormat; |
| 48 | import java.util.ArrayList; | 51 | import java.util.ArrayList; |
| 49 | import java.util.Calendar; | 52 | import java.util.Calendar; |
| 50 | import java.util.Date; | 53 | import java.util.Date; |
| 51 | import java.util.List; | 54 | import java.util.List; |
| 52 | import java.util.Locale; | 55 | import java.util.Locale; |
| 53 | 56 | ||
| 54 | import static com.github.mikephil.charting.utils.ColorTemplate.rgb; | 57 | import static com.github.mikephil.charting.utils.ColorTemplate.rgb; |
| 55 | 58 | ||
| 56 | 59 | ||
| 57 | public class TopFragment extends FragmentBase implements SettingFragmentPresenter, SensorEventListener { | 60 | public class TopFragment extends FragmentBase implements SettingFragmentPresenter, SensorEventListener { |
| 58 | 61 | ||
| 59 | private TextView stepsView; | 62 | private TextView stepsView; |
| 60 | private TextView tvStepGoal; | 63 | private TextView tvStepGoal; |
| 61 | private TextView tvDistance; | 64 | private TextView tvDistance; |
| 62 | private TextView tvStepRemain; | 65 | private TextView tvStepRemain; |
| 63 | private TextView tvStepRateDone; | 66 | private TextView tvStepRateDone; |
| 64 | private boolean showSteps = true; | 67 | private boolean showSteps = true; |
| 65 | private PieModel sliceGoal, sliceCurrent; | 68 | private PieModel sliceGoal, sliceCurrent; |
| 66 | private PieChart pg; | 69 | private PieChart pg; |
| 67 | private TextView tvDate; | 70 | private TextView tvDate; |
| 68 | private ImageView ivBack; | 71 | private ImageView ivBack; |
| 69 | private ImageView ivNext; | 72 | private ImageView ivNext; |
| 70 | private ImageView ivPlay; | 73 | private ImageView ivPlay; |
| 71 | 74 | ||
| 72 | private LinearLayout llBike; | 75 | private LinearLayout llBike; |
| 73 | private LinearLayout llWalking; | 76 | private LinearLayout llWalking; |
| 74 | private LinearLayout llRunning; | 77 | private LinearLayout llRunning; |
| 75 | private Const.STEP_TYPE stepType; | 78 | private Const.STEP_TYPE stepType; |
| 76 | 79 | ||
| 77 | private Date mAnaDate; | 80 | private Date mAnaDate; |
| 78 | private Calendar mCalendar; | 81 | private Calendar mCalendar; |
| 79 | 82 | ||
| 80 | private int mAnaDay; | 83 | private int mAnaDay; |
| 81 | private int mAnaMonth; | 84 | private int mAnaMonth; |
| 82 | private int mAnaYear; | 85 | private int mAnaYear; |
| 83 | 86 | ||
| 84 | public static int STEP_SIZE = 75; | 87 | public static int STEP_SIZE = 75; |
| 85 | private StepModel mStepModel; | 88 | private StepModel mStepModel; |
| 86 | private TextView tvSmallStepGoal; | 89 | private TextView tvSmallStepGoal; |
| 87 | private TextView tvSmallRemain; | 90 | private TextView tvSmallRemain; |
| 88 | private Context mContext; | 91 | private Context mContext; |
| 89 | 92 | ||
| 90 | private SensorManager sensorManager; | 93 | private SensorManager sensorManager; |
| 91 | private boolean activityRunning; | 94 | private boolean activityRunning; |
| 92 | private int stepTotal = 0; | 95 | private int stepTotal = 0; |
| 93 | private int stepRemain = 0; | 96 | private int stepRemain = 0; |
| 94 | private int stepCount = 0; | 97 | private int stepCount = 0; |
| 95 | 98 | ||
| 99 | private Date mFromDate; | ||
| 100 | private Date mToDate; | ||
| 101 | |||
| 102 | private TagetTopInfo mTagetTopInfo = new TagetTopInfo(); | ||
| 96 | private BarChart mChart; | 103 | private BarChart mChart; |
| 97 | 104 | ||
| 98 | private List<String> dateList; | 105 | private List<String> dateList; |
| 99 | private String[] mParties; | 106 | private String[] mParties; |
| 100 | private Integer[] mStep; | 107 | private Integer[] mStep; |
| 101 | 108 | ||
| 102 | @Override | 109 | @Override |
| 103 | public void onCreate(final Bundle savedInstanceState) { | 110 | public void onCreate(final Bundle savedInstanceState) { |
| 104 | super.onCreate(savedInstanceState); | 111 | super.onCreate(savedInstanceState); |
| 105 | setHasOptionsMenu(true); | 112 | setHasOptionsMenu(true); |
| 106 | } | 113 | } |
| 107 | 114 | ||
| 108 | @Override | 115 | @Override |
| 109 | public View onCreateView(final LayoutInflater inflater, final ViewGroup container, | 116 | public View onCreateView(final LayoutInflater inflater, final ViewGroup container, |
| 110 | final Bundle savedInstanceState) { | 117 | final Bundle savedInstanceState) { |
| 111 | final View v = inflater.inflate(R.layout.fragment_top, null); | 118 | final View v = inflater.inflate(R.layout.fragment_top, null); |
| 112 | mContext = getActivity(); | 119 | mContext = getActivity(); |
| 113 | sensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE); | 120 | sensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE); |
| 114 | 121 | ||
| 115 | initView(v); | 122 | initView(v); |
| 116 | 123 | ||
| 117 | // slice for the steps taken today | 124 | // slice for the steps taken today |
| 118 | sliceCurrent = new PieModel("", 0, Color.parseColor("#6FE7F7")); | 125 | sliceCurrent = new PieModel("", 0, Color.parseColor("#6FE7F7")); |
| 119 | pg.addPieSlice(sliceCurrent); | 126 | pg.addPieSlice(sliceCurrent); |
| 120 | 127 | ||
| 121 | // slice for the "missing" steps until reaching the goal | 128 | // slice for the "missing" steps until reaching the goal |
| 122 | sliceGoal = new PieModel("", Const.STEP_GOAL, Color.parseColor("#B7B8B6")); | 129 | sliceGoal = new PieModel("", Const.STEP_GOAL, Color.parseColor("#B7B8B6")); |
| 123 | pg.addPieSlice(sliceGoal); | 130 | pg.addPieSlice(sliceGoal); |
| 124 | pg.setDrawValueInPie(false); | 131 | pg.setDrawValueInPie(false); |
| 125 | pg.setUsePieRotation(false); | 132 | pg.setUsePieRotation(false); |
| 126 | pg.startAnimation(); | 133 | pg.startAnimation(); |
| 127 | pg.setAutoCenterInSlice(false); | 134 | pg.setAutoCenterInSlice(false); |
| 128 | 135 | ||
| 129 | mCalendar = Calendar.getInstance(); | 136 | mCalendar = Calendar.getInstance(); |
| 130 | mCalendar.setTime(new Date()); | 137 | mCalendar.setTime(new Date()); |
| 131 | mAnaDate = mCalendar.getTime(); | 138 | mAnaDate = mCalendar.getTime(); |
| 132 | mAnaYear = mCalendar.get(Calendar.YEAR); | 139 | mAnaYear = mCalendar.get(Calendar.YEAR); |
| 133 | mAnaMonth = mCalendar.get(Calendar.MONTH); | 140 | mAnaMonth = mCalendar.get(Calendar.MONTH); |
| 134 | mAnaDay = mCalendar.get(Calendar.DAY_OF_MONTH); | 141 | mAnaDay = mCalendar.get(Calendar.DAY_OF_MONTH); |
| 135 | 142 | ||
| 136 | tvDate.setText(Utils.dateToStringFormatDayMonthYearJp(mAnaDate)); | 143 | tvDate.setText(Utils.dateToStringFormatDayMonthYearJp(mAnaDate)); |
| 137 | 144 | ||
| 138 | handleEvent(); | 145 | handleEvent(); |
| 139 | 146 | ||
| 140 | return v; | 147 | return v; |
| 141 | } | 148 | } |
| 142 | 149 | ||
| 143 | private void initView(View v){ | 150 | private void initView(View v){ |
| 144 | stepsView = (TextView) v.findViewById(R.id.steps); | 151 | stepsView = (TextView) v.findViewById(R.id.steps); |
| 145 | tvDistance = (TextView) v.findViewById(R.id.tv_distance); | 152 | tvDistance = (TextView) v.findViewById(R.id.tv_distance); |
| 146 | tvStepGoal = (TextView) v.findViewById(R.id.tv_stepGoal); | 153 | tvStepGoal = (TextView) v.findViewById(R.id.tv_stepGoal); |
| 147 | tvStepRemain = (TextView) v.findViewById(R.id.tv_stepRemain); | 154 | tvStepRemain = (TextView) v.findViewById(R.id.tv_stepRemain); |
| 148 | tvStepRateDone = (TextView) v.findViewById(R.id.tv_stepRateDone); | 155 | tvStepRateDone = (TextView) v.findViewById(R.id.tv_stepRateDone); |
| 149 | tvSmallStepGoal = (TextView) v.findViewById(R.id.tv_smallStepGoal); | 156 | tvSmallStepGoal = (TextView) v.findViewById(R.id.tv_smallStepGoal); |
| 150 | tvSmallRemain = (TextView) v.findViewById(R.id.tv_smallRemain); | 157 | tvSmallRemain = (TextView) v.findViewById(R.id.tv_smallRemain); |
| 151 | tvDate = (TextView) v.findViewById(R.id.tv_date); | 158 | tvDate = (TextView) v.findViewById(R.id.tv_date); |
| 152 | ivBack = (ImageView) v.findViewById(R.id.iv_back); | 159 | ivBack = (ImageView) v.findViewById(R.id.iv_back); |
| 153 | ivNext = (ImageView) v.findViewById(R.id.iv_next); | 160 | ivNext = (ImageView) v.findViewById(R.id.iv_next); |
| 154 | ivPlay = (ImageView) v.findViewById(R.id.iv_play); | 161 | ivPlay = (ImageView) v.findViewById(R.id.iv_play); |
| 155 | 162 | ||
| 156 | mChart = (BarChart) v.findViewById(R.id.chart); | 163 | mChart = (BarChart) v.findViewById(R.id.chart); |
| 157 | pg = (PieChart) v.findViewById(R.id.graph); | 164 | pg = (PieChart) v.findViewById(R.id.graph); |
| 158 | } | 165 | } |
| 159 | 166 | ||
| 160 | private void stepsDistanceChanged() { | 167 | private void stepsDistanceChanged() { |
| 161 | updatePie(); | 168 | updatePie(); |
| 162 | updateBars(); | 169 | updateBars(); |
| 163 | } | 170 | } |
| 164 | 171 | ||
| 165 | 172 | ||
| 166 | @Override | 173 | @Override |
| 167 | public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) { | 174 | public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) { |
| 168 | inflater.inflate(R.menu.main, menu); | 175 | inflater.inflate(R.menu.main, menu); |
| 169 | } | 176 | } |
| 170 | 177 | ||
| 171 | 178 | ||
| 172 | private void updatePie() { | 179 | private void updatePie() { |
| 173 | // todayOffset might still be Integer.MIN_VALUE on first start | 180 | // todayOffset might still be Integer.MIN_VALUE on first start |
| 174 | int steps_today = Math.max(todayOffset + since_boot, 0); | 181 | int steps_today = Math.max(todayOffset + since_boot, 0); |
| 175 | sliceCurrent.setValue(steps_today); | 182 | sliceCurrent.setValue(steps_today); |
| 176 | if (Const.STEP_GOAL - steps_today > 0) { | 183 | if (Const.STEP_GOAL - steps_today > 0) { |
| 177 | // goal not reached yet | 184 | // goal not reached yet |
| 178 | if (pg.getData().size() == 1) { | 185 | if (pg.getData().size() == 1) { |
| 179 | pg.addPieSlice(sliceGoal); | 186 | pg.addPieSlice(sliceGoal); |
| 180 | } | 187 | } |
| 181 | sliceGoal.setValue(Const.STEP_GOAL - steps_today); | 188 | sliceGoal.setValue(Const.STEP_GOAL - steps_today); |
| 182 | } else { | 189 | } else { |
| 183 | // goal reached | 190 | // goal reached |
| 184 | pg.clearChart(); | 191 | pg.clearChart(); |
| 185 | pg.addPieSlice(sliceCurrent); | 192 | pg.addPieSlice(sliceCurrent); |
| 186 | } | 193 | } |
| 187 | pg.update(); | 194 | pg.update(); |
| 188 | 195 | ||
| 189 | stepsView.setText(formatter.format(steps_today)); | 196 | stepsView.setText(formatter.format(steps_today)); |
| 190 | int remain = Const.STEP_GOAL - steps_today; | 197 | int remain = Const.STEP_GOAL - steps_today; |
| 191 | tvStepRemain.setText(String.valueOf(remain)); | 198 | tvStepRemain.setText(String.valueOf(remain)); |
| 192 | tvStepRateDone.setText(getResources().getString(R.string.percent_unit, Utils.convert2String2Decimal(steps_today *100/ Const.STEP_GOAL))); | 199 | tvStepRateDone.setText(getResources().getString(R.string.percent_unit, Utils.convert2String2Decimal(steps_today *100/ Const.STEP_GOAL))); |
| 193 | tvSmallRemain.setText(getResources().getString(R.string.pie_text_content3a, remain)); | 200 | tvSmallRemain.setText(getResources().getString(R.string.pie_text_content3a, remain)); |
| 194 | 201 | ||
| 195 | double distance_today = steps_today * STEP_SIZE; | 202 | double distance_today = steps_today * STEP_SIZE; |
| 196 | distance_today /= 100000; | 203 | distance_today /= 100000; |
| 197 | tvDistance.setText(Utils.convert2String2Decimal(distance_today)); | 204 | tvDistance.setText(Utils.convert2String2Decimal(distance_today)); |
| 198 | 205 | ||
| 199 | } | 206 | } |
| 200 | 207 | ||
| 201 | /** | 208 | /** |
| 202 | * Updates the bar graph to show the steps/distance of the last week. Should | 209 | * Updates the bar graph to show the steps/distance of the last week. Should |
| 203 | * be called when switching from step count to distance. | 210 | * be called when switching from step count to distance. |
| 204 | */ | 211 | */ |
| 205 | private void updateBars() { | 212 | private void updateBars() { |
| 206 | 213 | ||
| 207 | dateList = new ArrayList<>(); | 214 | dateList = new ArrayList<>(); |
| 208 | List<Integer> stepList = new ArrayList<>(); | 215 | List<Integer> stepList = new ArrayList<>(); |
| 209 | 216 | ||
| 210 | Database db = Database.getInstance(getActivity()); | 217 | Database db = Database.getInstance(getActivity()); |
| 211 | List<Pair<Long, Integer>> last = db.getLastEntries(8, mAnaDate); | 218 | List<Pair<Long, Integer>> last = db.getLastEntries(8, mAnaDate); |
| 212 | db.close(); | 219 | db.close(); |
| 213 | for (int i = last.size() - 1; i > 0; i--) { | 220 | for (int i = last.size() - 1; i > 0; i--) { |
| 214 | Pair<Long, Integer> current = last.get(i); | 221 | Pair<Long, Integer> current = last.get(i); |
| 215 | int step = current.second; | 222 | int step = current.second; |
| 216 | Date date = new Date(current.first); | 223 | Date date = new Date(current.first); |
| 217 | stepList.add(step); | 224 | stepList.add(step); |
| 218 | dateList.add(String.valueOf(Utils.getDay(date))); | 225 | dateList.add(String.valueOf(Utils.getDay(date))); |
| 219 | } | 226 | } |
| 220 | 227 | ||
| 221 | mStep = stepList.toArray(new Integer[0]); | 228 | mStep = stepList.toArray(new Integer[0]); |
| 222 | mParties = dateList.toArray(new String[0]); | 229 | mParties = dateList.toArray(new String[0]); |
| 223 | 230 | ||
| 224 | initGraph(); | 231 | initGraph(); |
| 225 | } | 232 | } |
| 226 | 233 | ||
| 227 | 234 | ||
| 228 | private void initGraph(){ | 235 | private void initGraph(){ |
| 229 | 236 | ||
| 230 | mChart.setDrawBarShadow(false); | 237 | mChart.setDrawBarShadow(false); |
| 231 | mChart.setDrawValueAboveBar(true); | 238 | mChart.setDrawValueAboveBar(true); |
| 232 | 239 | ||
| 233 | // if more than 60 entries are displayed in the chart, no values will be | 240 | // if more than 60 entries are displayed in the chart, no values will be |
| 234 | // drawn | 241 | // drawn |
| 235 | mChart.setMaxVisibleValueCount(60); | 242 | mChart.setMaxVisibleValueCount(60); |
| 236 | 243 | ||
| 237 | mChart.getDescription().setEnabled(false); | 244 | mChart.getDescription().setEnabled(false); |
| 238 | // scaling can now only be done on x- and y-axis separately | 245 | // scaling can now only be done on x- and y-axis separately |
| 239 | mChart.setPinchZoom(false); | 246 | mChart.setPinchZoom(false); |
| 240 | 247 | ||
| 241 | mChart.setDrawGridBackground(false); | 248 | mChart.setDrawGridBackground(false); |
| 242 | // mChart.setDrawYLabels(false); | 249 | // mChart.setDrawYLabels(false); |
| 243 | 250 | ||
| 244 | DayAxisValueFormatter xValueFormatter = new DayAxisValueFormatter(dateList); | 251 | DayAxisValueFormatter xValueFormatter = new DayAxisValueFormatter(dateList); |
| 245 | 252 | ||
| 246 | XAxis xAxis = mChart.getXAxis(); | 253 | XAxis xAxis = mChart.getXAxis(); |
| 247 | xAxis.setLabelRotationAngle(0); | 254 | xAxis.setLabelRotationAngle(0); |
| 248 | xAxis.setPosition(XAxis.XAxisPosition.TOP); | 255 | xAxis.setPosition(XAxis.XAxisPosition.TOP); |
| 249 | xAxis.setDrawGridLines(true); | 256 | xAxis.setDrawGridLines(true); |
| 250 | xAxis.setLabelCount(10); | 257 | xAxis.setLabelCount(10); |
| 251 | xAxis.setTextColor(Color.WHITE); | 258 | xAxis.setTextColor(Color.WHITE); |
| 252 | xAxis.setValueFormatter(xValueFormatter); | 259 | xAxis.setValueFormatter(xValueFormatter); |
| 253 | 260 | ||
| 254 | 261 | ||
| 255 | YAxis leftAxis = mChart.getAxisLeft(); | 262 | YAxis leftAxis = mChart.getAxisLeft(); |
| 256 | leftAxis.setDrawLabels(false); | 263 | leftAxis.setDrawLabels(false); |
| 257 | leftAxis.setDrawGridLines(false); | 264 | leftAxis.setDrawGridLines(false); |
| 258 | leftAxis.setAxisMinimum(0f); | 265 | leftAxis.setAxisMinimum(0f); |
| 259 | leftAxis.setAxisMaximum(10000f); | 266 | leftAxis.setAxisMaximum(10000f); |
| 260 | leftAxis.setDrawZeroLine(false); | 267 | leftAxis.setDrawZeroLine(false); |
| 261 | 268 | ||
| 262 | leftAxis.setEnabled(false); | 269 | leftAxis.setEnabled(false); |
| 263 | mChart.setDrawValueAboveBar(false); | 270 | mChart.setDrawValueAboveBar(false); |
| 264 | mChart.getAxisRight().setEnabled(false); | 271 | mChart.getAxisRight().setEnabled(false); |
| 265 | 272 | ||
| 266 | // set auto scale min max | 273 | // set auto scale min max |
| 267 | mChart.setAutoScaleMinMaxEnabled(true); | 274 | mChart.setAutoScaleMinMaxEnabled(true); |
| 268 | mChart.notifyDataSetChanged(); | 275 | mChart.notifyDataSetChanged(); |
| 269 | 276 | ||
| 270 | // Set enimate y | 277 | // Set enimate y |
| 271 | mChart.animateY(2000); | 278 | mChart.animateY(2000); |
| 272 | 279 | ||
| 273 | setData(); | 280 | setData(); |
| 274 | 281 | ||
| 275 | mChart.getLegend().setEnabled(false); | 282 | mChart.getLegend().setEnabled(false); |
| 276 | } | 283 | } |
| 277 | 284 | ||
| 278 | 285 | ||
| 279 | private void setData() { | 286 | private void setData() { |
| 280 | 287 | ||
| 281 | ArrayList<String> xVals = new ArrayList<>(); | 288 | ArrayList<String> xVals = new ArrayList<>(); |
| 282 | for (int i = 0; i < mStep.length; i++) { | 289 | for (int i = 0; i < mStep.length; i++) { |
| 283 | xVals.add(mParties[i % mStep.length]); | 290 | xVals.add(mParties[i % mStep.length]); |
| 284 | } | 291 | } |
| 285 | 292 | ||
| 286 | ArrayList<BarEntry> yVals1 = new ArrayList<>(); | 293 | ArrayList<BarEntry> yVals1 = new ArrayList<>(); |
| 287 | for (int i = 0; i < mStep.length; i++) { | 294 | for (int i = 0; i < mStep.length; i++) { |
| 288 | float val = (float) (mStep[i]*1); | 295 | float val = (float) (mStep[i]*1); |
| 289 | yVals1.add(new BarEntry(i, val)); | 296 | yVals1.add(new BarEntry(i, val)); |
| 290 | } | 297 | } |
| 291 | 298 | ||
| 292 | BarDataSet set1; | 299 | BarDataSet set1; |
| 293 | 300 | ||
| 294 | if (mChart.getData() != null && mChart.getData().getDataSetCount() > 0) { | 301 | if (mChart.getData() != null && mChart.getData().getDataSetCount() > 0) { |
| 295 | set1 = (BarDataSet)mChart.getData().getDataSetByIndex(0); | 302 | set1 = (BarDataSet)mChart.getData().getDataSetByIndex(0); |
| 296 | set1.setValues(yVals1); | 303 | set1.setValues(yVals1); |
| 297 | mChart.getData().notifyDataChanged(); | 304 | mChart.getData().notifyDataChanged(); |
| 298 | mChart.notifyDataSetChanged(); | 305 | mChart.notifyDataSetChanged(); |
| 299 | } else { | 306 | } else { |
| 300 | ArrayList<Integer> colors = new ArrayList<>(); | 307 | ArrayList<Integer> colors = new ArrayList<>(); |
| 301 | 308 | ||
| 302 | 309 | ||
| 303 | int[] MATERIAL_COLORS = {rgb("#40CDEF")}; | 310 | int[] MATERIAL_COLORS = {rgb("#40CDEF")}; |
| 304 | 311 | ||
| 305 | for (int c : MATERIAL_COLORS) | 312 | for (int c : MATERIAL_COLORS) |
| 306 | colors.add(c); | 313 | colors.add(c); |
| 307 | 314 | ||
| 308 | set1 = new BarDataSet(yVals1, null); | 315 | set1 = new BarDataSet(yVals1, null); |
| 309 | 316 | ||
| 310 | set1.setColors(colors); | 317 | set1.setColors(colors); |
| 311 | 318 | ||
| 312 | ArrayList<IBarDataSet> dataSets = new ArrayList<>(); | 319 | ArrayList<IBarDataSet> dataSets = new ArrayList<>(); |
| 313 | dataSets.add(set1); | 320 | dataSets.add(set1); |
| 314 | 321 | ||
| 315 | BarData data = new BarData(dataSets); | 322 | BarData data = new BarData(dataSets); |
| 316 | data.setValueTextSize(10f); | 323 | data.setValueTextSize(10f); |
| 317 | 324 | ||
| 318 | mChart.setData(data); | 325 | mChart.setData(data); |
| 319 | } | 326 | } |
| 320 | } | 327 | } |
| 321 | 328 | ||
| 322 | 329 | ||
| 323 | 330 | ||
| 324 | private void handleEvent(){ | 331 | private void handleEvent(){ |
| 325 | tvDate.setOnClickListener(new View.OnClickListener() { | 332 | tvDate.setOnClickListener(new View.OnClickListener() { |
| 326 | @Override | 333 | @Override |
| 327 | public void onClick(View view) { | 334 | public void onClick(View view) { |
| 328 | handleAnaDatePicker(); | 335 | handleAnaDatePicker(); |
| 329 | } | 336 | } |
| 330 | }); | 337 | }); |
| 331 | 338 | ||
| 332 | ivBack.setOnClickListener(new View.OnClickListener() { | 339 | ivBack.setOnClickListener(new View.OnClickListener() { |
| 333 | @Override | 340 | @Override |
| 334 | public void onClick(View view) { | 341 | public void onClick(View view) { |
| 335 | mCalendar = Calendar.getInstance(); | 342 | mCalendar = Calendar.getInstance(); |
| 336 | mCalendar.setTime(mAnaDate); | 343 | mCalendar.setTime(mAnaDate); |
| 337 | mCalendar.add(Calendar.DAY_OF_MONTH, -1); | 344 | mCalendar.add(Calendar.DAY_OF_MONTH, -1); |
| 338 | Date date = mCalendar.getTime(); | 345 | Date date = mCalendar.getTime(); |
| 339 | tvDate.setText(Utils.dateToStringFormatDayMonthYearJp(date)); | 346 | tvDate.setText(Utils.dateToStringFormatDayMonthYearJp(date)); |
| 340 | mAnaDate = date; | 347 | mAnaDate = date; |
| 341 | } | 348 | } |
| 342 | }); | 349 | }); |
| 343 | 350 | ||
| 344 | ivNext.setOnClickListener(new View.OnClickListener() { | 351 | ivNext.setOnClickListener(new View.OnClickListener() { |
| 345 | @Override | 352 | @Override |
| 346 | public void onClick(View view) { | 353 | public void onClick(View view) { |
| 347 | mCalendar = Calendar.getInstance(); | 354 | mCalendar = Calendar.getInstance(); |
| 348 | mCalendar.setTime(mAnaDate); | 355 | mCalendar.setTime(mAnaDate); |
| 349 | mCalendar.add(Calendar.DAY_OF_MONTH, +1); | 356 | mCalendar.add(Calendar.DAY_OF_MONTH, +1); |
| 350 | Date date = mCalendar.getTime(); | 357 | Date date = mCalendar.getTime(); |
| 351 | tvDate.setText(Utils.dateToStringFormatDayMonthYearJp(date)); | 358 | tvDate.setText(Utils.dateToStringFormatDayMonthYearJp(date)); |
| 352 | mAnaDate = date; | 359 | mAnaDate = date; |
| 353 | } | 360 | } |
| 354 | }); | 361 | }); |
| 355 | 362 | ||
| 356 | llWalking.setOnClickListener(new View.OnClickListener() { | 363 | llWalking.setOnClickListener(new View.OnClickListener() { |
| 357 | @Override | 364 | @Override |
| 358 | public void onClick(View view) { | 365 | public void onClick(View view) { |
| 359 | stepType = Const.STEP_TYPE.WALKING; | 366 | stepType = Const.STEP_TYPE.WALKING; |
| 360 | updateUiStepType(false, true, false); | 367 | updateUiStepType(false, true, false); |
| 361 | // add data | 368 | // add data |
| 362 | getTopData(mAnaDate, stepType); | 369 | getTopData(mAnaDate, stepType); |
| 363 | } | 370 | } |
| 364 | }); | 371 | }); |
| 365 | 372 | ||
| 366 | llRunning.setOnClickListener(new View.OnClickListener() { | 373 | llRunning.setOnClickListener(new View.OnClickListener() { |
| 367 | @Override | 374 | @Override |
| 368 | public void onClick(View view) { | 375 | public void onClick(View view) { |
| 369 | stepType = Const.STEP_TYPE.RUNNING; | 376 | stepType = Const.STEP_TYPE.RUNNING; |
| 370 | updateUiStepType(false, false, true); | 377 | updateUiStepType(false, false, true); |
| 371 | // add | 378 | // add |
| 372 | getTopData(mAnaDate, stepType); | 379 | getTopData(mAnaDate, stepType); |
| 373 | } | 380 | } |
| 374 | }); | 381 | }); |
| 375 | 382 | ||
| 376 | llBike.setOnClickListener(new View.OnClickListener() { | 383 | llBike.setOnClickListener(new View.OnClickListener() { |
| 377 | @Override | 384 | @Override |
| 378 | public void onClick(View view) { | 385 | public void onClick(View view) { |
| 379 | stepType = Const.STEP_TYPE.BIKE; | 386 | stepType = Const.STEP_TYPE.BIKE; |
| 380 | updateUiStepType(true, false, false); | 387 | updateUiStepType(true, false, false); |
| 381 | // add data | 388 | // add data |
| 382 | getTopData(mAnaDate, stepType); | 389 | getTopData(mAnaDate, stepType); |
| 383 | } | 390 | } |
| 384 | }); | 391 | }); |
| 385 | 392 | ||
| 386 | 393 | ||
| 387 | } | 394 | } |
| 388 | 395 | ||
| 389 | private void updateUiStepType(boolean b1, boolean b2, boolean b3) { | 396 | private void updateUiStepType(boolean b1, boolean b2, boolean b3) { |
| 390 | llBike.setSelected(b1); | 397 | llBike.setSelected(b1); |
| 391 | llWalking.setSelected(b2); | 398 | llWalking.setSelected(b2); |
| 392 | llRunning.setSelected(b3); | 399 | llRunning.setSelected(b3); |
| 393 | } | 400 | } |
| 394 | 401 | ||
| 395 | 402 | ||
| 396 | /** | 403 | /** |
| 397 | * Show date picker dialog | 404 | * Show date picker dialog |
| 398 | */ | 405 | */ |
| 399 | private void handleAnaDatePicker() { | 406 | private void handleAnaDatePicker() { |
| 400 | 407 | ||
| 401 | new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() { | 408 | new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() { |
| 402 | @Override | 409 | @Override |
| 403 | public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { | 410 | public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { |
| 404 | 411 | ||
| 405 | mCalendar = Calendar.getInstance(); | 412 | mCalendar = Calendar.getInstance(); |
| 406 | mCalendar.set(year, monthOfYear, dayOfMonth); | 413 | mCalendar.set(year, monthOfYear, dayOfMonth); |
| 407 | mAnaYear = year; | 414 | mAnaYear = year; |
| 408 | mAnaMonth = monthOfYear; | 415 | mAnaMonth = monthOfYear; |
| 409 | mAnaDay = dayOfMonth; | 416 | mAnaDay = dayOfMonth; |
| 410 | Date date = mCalendar.getTime(); | 417 | Date date = mCalendar.getTime(); |
| 411 | tvDate.setText(Utils.dateToStringFormatDayMonthYearJp(date)); | 418 | tvDate.setText(Utils.dateToStringFormatDayMonthYearJp(date)); |
| 412 | mAnaDate = date; | 419 | mAnaDate = date; |
| 413 | } | 420 | } |
| 414 | }, mAnaYear, mAnaMonth, mAnaDay).show(); | 421 | }, mAnaYear, mAnaMonth, mAnaDay).show(); |
| 415 | 422 | ||
| 416 | } | 423 | } |
| 417 | 424 | ||
| 418 | private void getTopData(Date date, Const.STEP_TYPE stepType){ | 425 | private void getTopData(Date date, Const.STEP_TYPE stepType){ |
| 419 | 426 | ||
| 420 | showDialog(mContext); | 427 | showDialog(mContext); |
| 421 | LLAPIManager.homePage(date, stepType, new LLAPIManagerListener() { | 428 | LLAPIManager.topInfo(date, stepType, new LLAPIManagerListener() { |
| 422 | @Override | 429 | @Override |
| 423 | public void onError(Error error) { | 430 | public void onError(Error error) { |
| 424 | Debug.error("Get data history error"); | 431 | Debug.error("Get data history error"); |
| 425 | hiddenDialog(); | 432 | hiddenDialog(); |
| 426 | showDialogNotData(); | 433 | showDialogNotData(); |
| 427 | } | 434 | } |
| 428 | 435 | ||
| 429 | @Override | 436 | @Override |
| 430 | public void onSuccess(String json) { | 437 | public void onSuccess(String json) { |
| 431 | Debug.error("Get data history success"); | 438 | Debug.error("Get data history success"); |
| 432 | hiddenDialog(); | 439 | hiddenDialog(); |
| 433 | loadDataDone(json); | 440 | loadDataDone(json); |
| 434 | } | 441 | } |
| 435 | 442 | ||
| 436 | @Override | 443 | @Override |
| 437 | public void onSuccess(JSONObject object) { | 444 | public void onSuccess(JSONObject object) { |
| 438 | Debug.error("Get data history success"); | 445 | Debug.error("Get data history success"); |
| 439 | hiddenDialog(); | 446 | hiddenDialog(); |
| 440 | } | 447 | } |
| 441 | }); | 448 | }); |
| 442 | } | 449 | } |
| 443 | 450 | ||
| 444 | private void showDialogNotData(){ | 451 | private void showDialogNotData(){ |
| 445 | showAlerDialog(mContext, getResources().getString(R.string.can_not_get_data)); | 452 | showAlerDialog(mContext, getResources().getString(R.string.can_not_get_data)); |
| 446 | } | 453 | } |
| 447 | 454 | ||
| 448 | private void loadDataDone(String jsonString) { | 455 | private void loadDataDone(String jsonString) { |
| 449 | JSONObject jsonObject = null; | 456 | JSONObject jsonObject = null; |
| 450 | try { | 457 | try { |
| 451 | jsonObject = new JSONObject(jsonString); | 458 | jsonObject = new JSONObject(jsonString); |
| 452 | int status = jsonObject.optInt("status"); | 459 | int status = jsonObject.optInt("status"); |
| 453 | if (status == 1) { | 460 | if (status == 1) { |
| 454 | JSONObject jsonObject1 = jsonObject.optJSONObject("result"); | 461 | JSONObject jsonObject1 = jsonObject.optJSONObject("result"); |
| 455 | JSONObject targetInf = jsonObject1.getJSONObject("targetInf"); | 462 | JSONObject targetInf = jsonObject1.getJSONObject("targetInf"); |
| 456 | JSONArray listNotice = jsonObject1.getJSONArray("listNotice"); | 463 | JSONArray listNotice = jsonObject1.getJSONArray("listNotice"); |
| 457 | 464 | ||
| 458 | if (targetInf != null){ | 465 | if (targetInf != null){ |
| 459 | String target = targetInf.optString("target_step"); | 466 | String target = targetInf.optString("target_step"); |
| 460 | Debug.normal("Target: "+ target); | 467 | Debug.normal("Target: "+ target); |
| 461 | mTagetInfo.setTaget(targetInf.optString("target_step")); | 468 | mTagetTopInfo.setTaget(targetInf.optString("target_step")); |
| 462 | mTagetInfo.setSteps(targetInf.optString("num_step")); | 469 | mTagetTopInfo.setSteps(targetInf.optString("num_step")); |
| 463 | mTagetInfo.setStepRemain(targetInf.optString("remaining_step")); | 470 | mTagetTopInfo.setStepRemain(targetInf.optString("remaining_step")); |
| 464 | mTagetInfo.setCompletePercent(targetInf.optString("complete_percent")); | 471 | mTagetTopInfo.setCompletePercent(targetInf.optString("complete_percent")); |
| 465 | } | 472 | } |
| 466 | 473 | ||
| 467 | if (listNotice != null && listNotice.length() > 0) { | 474 | if (listNotice != null && listNotice.length() > 0) { |
| 468 | List<NoticeInfo> infoLists = new ArrayList<>(); | 475 | List<StepHourInfo> infoLists = new ArrayList<>(); |
| 469 | for (int i = 0; i < listNotice.length(); i++){ | 476 | for (int i = 0; i < listNotice.length(); i++){ |
| 470 | NoticeInfo noticeInfo = new NoticeInfo(); | 477 | StepHourInfo noticeInfo = new StepHourInfo(); |
| 471 | JSONObject ob = (JSONObject) listNotice.get(i); | 478 | JSONObject ob = (JSONObject) listNotice.get(i); |
| 472 | noticeInfo.setId(ob.optInt("id")); | 479 | noticeInfo.setId(ob.optInt("id")); |
| 473 | noticeInfo.setContent(ob.optString("notice_content")); | 480 | noticeInfo.setContent(ob.optString("notice_content")); |
| 474 | infoLists.add(noticeInfo); | 481 | infoLists.add(noticeInfo); |
| 475 | } | 482 | } |
| 476 | 483 | ||
| 477 | mTagetInfo.setNoticeList(infoLists); | 484 | mTagetTopInfo.setStepHourList(infoLists); |
| 478 | } | 485 | } |
| 479 | 486 | ||
| 480 | } | 487 | } |
| 481 | } catch (JSONException e) { | 488 | } catch (JSONException e) { |
| 482 | e.printStackTrace(); | 489 | e.printStackTrace(); |
| 483 | mTagetInfo = new TagetInfo();; | 490 | mTagetTopInfo = new TagetTopInfo();; |
| 484 | } | 491 | } |
| 485 | 492 | ||
| 486 | loadUI(); | 493 | loadUI(); |
| 487 | } | 494 | } |
| 488 | 495 | ||
| 489 | 496 | ||
| 490 | private void loadUI(){ | 497 | private void loadUI(){ |
| 491 | tvStep.setText(String.valueOf(mTagetInfo.getSteps())); | 498 | tvStep.setText(String.valueOf(mTagetInfo.getSteps())); |
| 492 | tvRemain.setText(String.valueOf(mTagetInfo.getStepRemain())); | 499 | tvRemain.setText(String.valueOf(mTagetInfo.getStepRemain())); |
| 493 | tvRateDone.setText(getResources().getString(R.string.percent_unit, mTagetInfo.getCompletePercent())); | 500 | tvRateDone.setText(getResources().getString(R.string.percent_unit, mTagetInfo.getCompletePercent())); |
| 494 | 501 | ||
| 495 | List<NoticeInfo> infoLists = mTagetInfo.getNoticeList(); | 502 | List<StepHourInfo> infoLists = mTagetTopInfo.getStepHourList(); |
| 496 | if (infoLists != null && infoLists.size() >0){ | 503 | if (infoLists != null && infoLists.size() >0){ |
| 497 | mNoticeAdapter = new NoticeAdapter(mContext, infoLists); | 504 | mNoticeAdapter = new NoticeAdapter(mContext, infoLists); |
| 498 | lvNotice.setAdapter(mNoticeAdapter); | 505 | lvNotice.setAdapter(mNoticeAdapter); |
| 499 | lvNotice.setExpanded(true); | 506 | lvNotice.setExpanded(true); |
| 500 | } | 507 | } |
| 501 | 508 | ||
| 502 | } | 509 | } |
| 503 | 510 | ||
| 504 | private void updateUI(){ | 511 | private void updateUI(){ |
| 505 | stepTotal = stepTotal + stepCount; | 512 | stepTotal = stepTotal + stepCount; |
| 506 | stepRemain = stepRemain - stepCount; | 513 | stepRemain = stepRemain - stepCount; |
| 507 | double percentDone = stepTotal *100.0 / (stepTotal + stepRemain); | 514 | double percentDone = stepTotal *100.0 / (stepTotal + stepRemain); |
| 508 | tvStep.setText(String.valueOf(stepTotal)); | 515 | tvStep.setText(String.valueOf(stepTotal)); |
| 509 | tvRemain.setText(mTagetInfo.getStepRemain()); | 516 | tvRemain.setText(mTagetInfo.getStepRemain()); |
| 510 | tvRateDone.setText(getResources().getString(R.string.percent_unit, Utils.convert2String2Decimal(percentDone))); | 517 | tvRateDone.setText(getResources().getString(R.string.percent_unit, Utils.convert2String2Decimal(percentDone))); |
| 511 | } | 518 | } |
| 512 | 519 | ||
| 513 | @Override | 520 | @Override |
| 514 | public void onAttach(Context context) { | 521 | public void onAttach(Context context) { |
| 515 | super.onAttach(context); | 522 | super.onAttach(context); |
| 516 | 523 | ||
| 517 | } | 524 | } |
| 518 | 525 | ||
| 519 | private void initData() { | 526 | private void initData() { |
| 520 | 527 | ||
| 521 | } | 528 | } |
| 522 | 529 | ||
| 523 | @Override | 530 | @Override |
| 524 | public void onSaveData() { | 531 | public void onSaveData() { |
| 525 | 532 | ||
| 526 | } | 533 | } |
| 527 | 534 | ||
| 528 | @Override | 535 | @Override |
| 529 | public void onInvalidate(boolean isInit) { | 536 | public void onInvalidate(boolean isInit) { |
| 530 | initData(); | 537 | initData(); |
| 531 | } | 538 | } |
| 532 | 539 | ||
| 533 | @Override | 540 | @Override |
| 534 | public void onViewStateRestored(@Nullable Bundle savedInstanceState) { | 541 | public void onViewStateRestored(@Nullable Bundle savedInstanceState) { |
| 535 | super.onViewStateRestored(savedInstanceState); | 542 | super.onViewStateRestored(savedInstanceState); |
| 536 | 543 | ||
| 537 | initData(); | 544 | initData(); |
| 538 | } | 545 | } |
| 539 | 546 | ||
| 540 | 547 | ||
| 541 | @Override | 548 | @Override |
| 542 | public void onResume() { | 549 | public void onResume() { |
| 543 | super.onResume(); | 550 | super.onResume(); |
| 544 | activityRunning = true; | 551 | activityRunning = true; |
| 545 | Sensor countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER); | 552 | Sensor countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER); |
| 546 | if (countSensor != null) { | 553 | if (countSensor != null) { |
| 547 | sensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_UI); | 554 | sensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_UI); |
| 548 | } else { | 555 | } else { |
| 549 | Toast.makeText(mContext, getResources().getString(R.string.sensor_available), Toast.LENGTH_SHORT).show(); | 556 | Toast.makeText(mContext, getResources().getString(R.string.sensor_available), Toast.LENGTH_SHORT).show(); |
| 550 | } | 557 | } |
| 551 | } | 558 | } |
| 552 | 559 | ||
| 553 | @Override | 560 | @Override |
| 554 | public void onSensorChanged(SensorEvent event) { | 561 | public void onSensorChanged(SensorEvent event) { |
| 555 | if (activityRunning){ | 562 | if (activityRunning){ |
| 556 | stepCount = (int) event.values[0]; | 563 | stepCount = (int) event.values[0]; |
| 557 | // update UI | 564 | // update UI |
| 558 | updateUI(); | 565 | updateUI(); |
| 559 | } | 566 | } |
| 560 | } | 567 | } |
| 561 | 568 | ||
| 562 | @Override | 569 | @Override |
| 563 | public void onAccuracyChanged(Sensor sensor, int accuracy) { | 570 | public void onAccuracyChanged(Sensor sensor, int accuracy) { |
| 564 | 571 | ||
| 565 | } | 572 | } |
| 566 | 573 | ||
| 567 | @Override | 574 | @Override |
| 568 | public void onPause() { | 575 | public void onPause() { |
| 569 | super.onPause(); | 576 | super.onPause(); |
| 570 | activityRunning = false; | 577 | activityRunning = false; |
| 571 | } | 578 | } |
| 572 | 579 | ||
| 573 | @Override | 580 | @Override |
| 574 | public void onDestroy() { | 581 | public void onDestroy() { |
| 575 | super.onDestroy(); | 582 | super.onDestroy(); |
| 576 | 583 | // create log step | |
| 577 | 584 | creatLogStep(); | |
| 578 | } | 585 | } |
| 579 | 586 | ||
| 580 | private void creatLogStep(Date date, Const.STEP_TYPE stepType){ | 587 | private void creatLogStep(){ |
| 581 | 588 | ||
| 582 | LLAPIManager.homePage(date, stepType, new LLAPIManagerListener() { | 589 | LLAPIManager.createLog(stepType, stepCount, mFromDate, mToDate, new LLAPIManagerListener() { |
| 583 | @Override | 590 | @Override |
| 584 | public void onError(Error error) { | 591 | public void onError(Error error) { |
| 585 | Debug.error("Get data history error"); | 592 | Debug.error("Get data history error"); |
| 586 | } | 593 | } |
| 587 | 594 | ||
| 588 | @Override | 595 | @Override |
| 589 | public void onSuccess(String json) { | 596 | public void onSuccess(String json) { |
| 590 | Debug.error("Get data history success"); | 597 | Debug.error("Get data history success"); |
| 591 | hiddenDialog(); | ||
| 592 | loadDataDone(json); | ||
| 593 | } | 598 | } |
| 594 | 599 | ||
| 595 | @Override | 600 | @Override |
| 596 | public void onSuccess(JSONObject object) { | 601 | public void onSuccess(JSONObject object) { |
| 597 | Debug.error("Get data history success"); | 602 | Debug.error("Get data history success"); |
| 598 | } | 603 | } |
| 599 | }); | 604 | }); |
| 600 | } | 605 | } |
| 601 | 606 |
app/src/main/java/com/dinhcv/lifelogpedometer/model/structure/top/StepHourInfo.java
| File was created | 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 | } | ||
| 24 |
app/src/main/java/com/dinhcv/lifelogpedometer/model/structure/top/TagetTopInfo.java
| File was created | 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 | } | ||
| 84 |
app/src/main/java/com/dinhcv/lifelogpedometer/portal/LLAPIManager.java
| 1 | package com.dinhcv.lifelogpedometer.portal; | 1 | package com.dinhcv.lifelogpedometer.portal; |
| 2 | 2 | ||
| 3 | import android.content.Context; | 3 | import android.content.Context; |
| 4 | import android.content.res.AssetManager; | 4 | import android.content.res.AssetManager; |
| 5 | import android.graphics.Bitmap; | 5 | import android.graphics.Bitmap; |
| 6 | import android.graphics.BitmapFactory; | 6 | import android.graphics.BitmapFactory; |
| 7 | import android.os.AsyncTask; | 7 | import android.os.AsyncTask; |
| 8 | import android.provider.Settings; | 8 | import android.provider.Settings; |
| 9 | import android.support.annotation.NonNull; | 9 | import android.support.annotation.NonNull; |
| 10 | import android.util.Base64; | 10 | import android.util.Base64; |
| 11 | 11 | ||
| 12 | import com.dinhcv.lifelogpedometer.interfaces.LLAPIManagerListener; | 12 | import com.dinhcv.lifelogpedometer.interfaces.LLAPIManagerListener; |
| 13 | import com.dinhcv.lifelogpedometer.model.Shareprefer.Setting; | 13 | import com.dinhcv.lifelogpedometer.model.Shareprefer.Setting; |
| 14 | import com.dinhcv.lifelogpedometer.model.structure.RegisterInfo; | 14 | import com.dinhcv.lifelogpedometer.model.structure.RegisterInfo; |
| 15 | import com.dinhcv.lifelogpedometer.utils.Const; | 15 | import com.dinhcv.lifelogpedometer.utils.Const; |
| 16 | import com.dinhcv.lifelogpedometer.utils.Debug; | 16 | import com.dinhcv.lifelogpedometer.utils.Debug; |
| 17 | import com.dinhcv.lifelogpedometer.utils.Utils; | 17 | import com.dinhcv.lifelogpedometer.utils.Utils; |
| 18 | 18 | ||
| 19 | import org.json.JSONArray; | 19 | import org.json.JSONArray; |
| 20 | import org.json.JSONException; | 20 | import org.json.JSONException; |
| 21 | import org.json.JSONObject; | 21 | import org.json.JSONObject; |
| 22 | 22 | ||
| 23 | import java.io.ByteArrayOutputStream; | 23 | import java.io.ByteArrayOutputStream; |
| 24 | import java.io.IOException; | 24 | import java.io.IOException; |
| 25 | import java.io.InputStream; | 25 | import java.io.InputStream; |
| 26 | import java.util.ArrayList; | 26 | import java.util.ArrayList; |
| 27 | import java.util.Arrays; | 27 | import java.util.Arrays; |
| 28 | import java.util.Date; | 28 | import java.util.Date; |
| 29 | import java.util.HashMap; | 29 | import java.util.HashMap; |
| 30 | import java.util.List; | 30 | import java.util.List; |
| 31 | import java.util.UUID; | 31 | import java.util.UUID; |
| 32 | import java.util.concurrent.TimeUnit; | 32 | import java.util.concurrent.TimeUnit; |
| 33 | 33 | ||
| 34 | import okhttp3.Call; | 34 | import okhttp3.Call; |
| 35 | import okhttp3.Callback; | 35 | import okhttp3.Callback; |
| 36 | import okhttp3.HttpUrl; | 36 | import okhttp3.HttpUrl; |
| 37 | import okhttp3.MediaType; | 37 | import okhttp3.MediaType; |
| 38 | import okhttp3.OkHttpClient; | 38 | import okhttp3.OkHttpClient; |
| 39 | import okhttp3.Request; | 39 | import okhttp3.Request; |
| 40 | import okhttp3.RequestBody; | 40 | import okhttp3.RequestBody; |
| 41 | import okhttp3.Response; | 41 | import okhttp3.Response; |
| 42 | import okhttp3.ResponseBody; | 42 | import okhttp3.ResponseBody; |
| 43 | 43 | ||
| 44 | public class LLAPIManager { | 44 | public class LLAPIManager { |
| 45 | public static String URL_ROOT = "http://clover.timesfun.jp:9001/"; | 45 | public static String URL_ROOT = "http://clover.timesfun.jp:9001/"; |
| 46 | //login info | 46 | //login info |
| 47 | public static String URL_LOGIN_INFO = "login"; | 47 | public static String URL_LOGIN_INFO = "login"; |
| 48 | //register info | 48 | //register info |
| 49 | public static String URL_REGISTER_INFO = "register"; | 49 | public static String URL_REGISTER_INFO = "register"; |
| 50 | //upload image info | 50 | //upload image info |
| 51 | public static String URL_UPLOAD_IMAGE_INFO = "upload-image"; | 51 | public static String URL_UPLOAD_IMAGE_INFO = "upload-image"; |
| 52 | //Search zipcode | 52 | //Search zipcode |
| 53 | public static String URL_FORGET_PASS_INFO = "forgetPass"; | 53 | public static String URL_FORGET_PASS_INFO = "forgetPass"; |
| 54 | //history | 54 | //history |
| 55 | public static String URL_HISTORE_INFO = "api/history"; | 55 | public static String URL_HISTORE_INFO = "api/history"; |
| 56 | //history detail | 56 | //history detail |
| 57 | public static String URL_HISTORE_DETAIL_INFO = "api/history/detail"; | 57 | public static String URL_HISTORE_DETAIL_INFO = "api/history/detail"; |
| 58 | // home | 58 | // home |
| 59 | public static String URL_HOME_INFO = "api/home"; | 59 | public static String URL_HOME_INFO = "api/home"; |
| 60 | //Search zipcode | 60 | //Search zipcode |
| 61 | public static String URL_FORGET_PASS_CONFIRM_INFO = "forgetPass/confirm"; | 61 | public static String URL_FORGET_PASS_CONFIRM_INFO = "forgetPass/confirm"; |
| 62 | // Refesh Token | 62 | // Refesh Token |
| 63 | public static String URL_REFESH_TOKEN_INFO = "refreshToken"; | 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"; | ||
| 64 | 68 | ||
| 65 | 69 | ||
| 70 | |||
| 66 | private static int deviationValue = 1; | 71 | private static int deviationValue = 1; |
| 67 | 72 | ||
| 68 | public static void login(final Context context, final String email, final String pass, final LLAPIManagerListener action) { | 73 | public static void login(final Context context, final String email, final String pass, final LLAPIManagerListener action) { |
| 69 | new AsyncTask<Void, Void, String>() { | 74 | new AsyncTask<Void, Void, String>() { |
| 70 | @Override | 75 | @Override |
| 71 | protected void onPreExecute() { | 76 | protected void onPreExecute() { |
| 72 | super.onPreExecute(); | 77 | super.onPreExecute(); |
| 73 | } | 78 | } |
| 74 | 79 | ||
| 75 | @Override | 80 | @Override |
| 76 | protected String doInBackground(Void... voids) { | 81 | protected String doInBackground(Void... voids) { |
| 77 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_LOGIN_INFO); | 82 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_LOGIN_INFO); |
| 78 | JSONObject jsonObject = new JSONObject(); | 83 | JSONObject jsonObject = new JSONObject(); |
| 79 | 84 | ||
| 80 | try { | 85 | try { |
| 81 | jsonObject.put("email", email); | 86 | jsonObject.put("email", email); |
| 82 | jsonObject.put("password", pass); | 87 | jsonObject.put("password", pass); |
| 83 | 88 | ||
| 84 | Debug.warn("Json data: " + jsonObject.toString()); | 89 | Debug.warn("Json data: " + jsonObject.toString()); |
| 85 | } catch (JSONException e) { | 90 | } catch (JSONException e) { |
| 86 | Debug.normal("Error ", e.getMessage()); | 91 | Debug.normal("Error ", e.getMessage()); |
| 87 | } | 92 | } |
| 88 | 93 | ||
| 89 | MediaType JSON | 94 | MediaType JSON |
| 90 | = MediaType.parse("application/json"); | 95 | = MediaType.parse("application/json"); |
| 91 | Debug.normal("JSON STRING: %s", jsonObject.toString()); | 96 | Debug.normal("JSON STRING: %s", jsonObject.toString()); |
| 92 | RequestBody requestBody = RequestBody.create(JSON, jsonObject.toString()); | 97 | RequestBody requestBody = RequestBody.create(JSON, jsonObject.toString()); |
| 93 | 98 | ||
| 94 | final Request.Builder request = new Request.Builder() | 99 | final Request.Builder request = new Request.Builder() |
| 95 | .url(baseUrl) | 100 | .url(baseUrl) |
| 96 | .header("User-Agent", Utils.getCustomUA()) | 101 | .header("User-Agent", Utils.getCustomUA()) |
| 97 | .header("Content-Type", "application/json") | 102 | .header("Content-Type", "application/json") |
| 98 | .post(requestBody); | 103 | .post(requestBody); |
| 99 | 104 | ||
| 100 | OkHttpClient httpClient = new OkHttpClient.Builder() | 105 | OkHttpClient httpClient = new OkHttpClient.Builder() |
| 101 | .readTimeout(30, TimeUnit.SECONDS) | 106 | .readTimeout(30, TimeUnit.SECONDS) |
| 102 | .writeTimeout(30, TimeUnit.SECONDS) | 107 | .writeTimeout(30, TimeUnit.SECONDS) |
| 103 | .connectTimeout(30, TimeUnit.SECONDS) | 108 | .connectTimeout(30, TimeUnit.SECONDS) |
| 104 | .build(); | 109 | .build(); |
| 105 | 110 | ||
| 106 | String jsonString = null; | 111 | String jsonString = null; |
| 107 | try { | 112 | try { |
| 108 | Response response = httpClient.newCall(request.build()).execute(); | 113 | Response response = httpClient.newCall(request.build()).execute(); |
| 109 | if (response == null) { | 114 | if (response == null) { |
| 110 | action.onError(null); | 115 | action.onError(null); |
| 111 | Debug.error("Response is null"); | 116 | Debug.error("Response is null"); |
| 112 | return null; | 117 | return null; |
| 113 | } | 118 | } |
| 114 | jsonString = getResponseData(response.body()); | 119 | jsonString = getResponseData(response.body()); |
| 115 | 120 | ||
| 116 | } catch (IOException e) { | 121 | } catch (IOException e) { |
| 117 | Debug.normal("Error %s", e.getMessage()); | 122 | Debug.normal("Error %s", e.getMessage()); |
| 118 | } | 123 | } |
| 119 | return jsonString; | 124 | return jsonString; |
| 120 | } | 125 | } |
| 121 | 126 | ||
| 122 | @Override | 127 | @Override |
| 123 | protected void onCancelled() { | 128 | protected void onCancelled() { |
| 124 | super.onCancelled(); | 129 | super.onCancelled(); |
| 125 | action.onError(null); | 130 | action.onError(null); |
| 126 | } | 131 | } |
| 127 | 132 | ||
| 128 | @Override | 133 | @Override |
| 129 | protected void onPostExecute(String jsonString) { | 134 | protected void onPostExecute(String jsonString) { |
| 130 | super.onPostExecute(jsonString); | 135 | super.onPostExecute(jsonString); |
| 131 | 136 | ||
| 132 | if ((jsonString == null) || (jsonString.isEmpty())) { | 137 | if ((jsonString == null) || (jsonString.isEmpty())) { |
| 133 | action.onError(null); | 138 | action.onError(null); |
| 134 | return; | 139 | return; |
| 135 | } | 140 | } |
| 136 | Debug.normal("String body: "+jsonString); | 141 | Debug.normal("String body: "+jsonString); |
| 137 | 142 | ||
| 138 | try { | 143 | try { |
| 139 | JSONObject jsonObject = new JSONObject(jsonString); | 144 | JSONObject jsonObject = new JSONObject(jsonString); |
| 140 | int status = jsonObject.optInt("status"); | 145 | int status = jsonObject.optInt("status"); |
| 141 | if (status == 1){ | 146 | if (status == 1){ |
| 142 | JSONObject object = jsonObject.optJSONObject("result"); | 147 | JSONObject object = jsonObject.optJSONObject("result"); |
| 143 | JSONObject objUser = object.getJSONObject("user"); | 148 | JSONObject objUser = object.getJSONObject("user"); |
| 144 | String token = object.optString("token"); | 149 | String token = object.optString("token"); |
| 145 | // save data | 150 | // save data |
| 146 | Setting.setUserDataSharepre(context, Setting.TOKEN_SHAREPRE, token); | 151 | Setting.setUserDataSharepre(context, Setting.TOKEN_SHAREPRE, token); |
| 147 | APIResponse.getInstance().setToken(token); | 152 | APIResponse.getInstance().setToken(token); |
| 148 | Debug.normal("Token: "+ token); | 153 | Debug.normal("Token: "+ token); |
| 149 | action.onSuccess(objUser); | 154 | action.onSuccess(objUser); |
| 150 | }else { | 155 | }else { |
| 151 | action.onError(new Error(jsonObject.optString("message"))); | 156 | action.onError(new Error(jsonObject.optString("message"))); |
| 152 | } | 157 | } |
| 153 | } catch (JSONException e) { | 158 | } catch (JSONException e) { |
| 154 | e.printStackTrace(); | 159 | e.printStackTrace(); |
| 155 | } | 160 | } |
| 156 | } | 161 | } |
| 157 | }.execute(); | 162 | }.execute(); |
| 158 | } | 163 | } |
| 159 | 164 | ||
| 160 | public static void uploadImage(final Bitmap bitmap, final LLAPIManagerListener action) { | 165 | public static void uploadImage(final Bitmap bitmap, final LLAPIManagerListener action) { |
| 161 | new AsyncTask<Void, Void, String>() { | 166 | new AsyncTask<Void, Void, String>() { |
| 162 | @Override | 167 | @Override |
| 163 | protected void onPreExecute() { | 168 | protected void onPreExecute() { |
| 164 | super.onPreExecute(); | 169 | super.onPreExecute(); |
| 165 | } | 170 | } |
| 166 | 171 | ||
| 167 | @Override | 172 | @Override |
| 168 | protected String doInBackground(Void... voids) { | 173 | protected String doInBackground(Void... voids) { |
| 169 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_UPLOAD_IMAGE_INFO); | 174 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_UPLOAD_IMAGE_INFO); |
| 170 | JSONObject jsonObject = new JSONObject(); | 175 | JSONObject jsonObject = new JSONObject(); |
| 171 | byte[] bitmapByte = convertBitmapToByteArray(bitmap); | 176 | byte[] bitmapByte = convertBitmapToByteArray(bitmap); |
| 172 | String imageBase64 = Base64.encodeToString(bitmapByte, Base64.DEFAULT); | 177 | String imageBase64 = Base64.encodeToString(bitmapByte, Base64.DEFAULT); |
| 173 | String imageStr = imageBase64.replace("\n",""); | 178 | String imageStr = imageBase64.replace("\n",""); |
| 174 | 179 | ||
| 175 | String token = APIResponse.getInstance().getToken(); | 180 | String token = APIResponse.getInstance().getToken(); |
| 176 | try { | 181 | try { |
| 177 | if (token != null && !token.isEmpty()) { | 182 | if (token != null && !token.isEmpty()) { |
| 178 | Debug.normal("Token is not null"); | 183 | Debug.normal("Token is not null"); |
| 179 | jsonObject.put("token", token); | 184 | jsonObject.put("token", token); |
| 180 | } | 185 | } |
| 181 | jsonObject.put("img", imageStr); | 186 | jsonObject.put("img", imageStr); |
| 182 | 187 | ||
| 183 | Debug.warn("Json data: " + jsonObject.toString()); | 188 | Debug.warn("Json data: " + jsonObject.toString()); |
| 184 | } catch (JSONException e) { | 189 | } catch (JSONException e) { |
| 185 | Debug.normal("Error ", e.getMessage()); | 190 | Debug.normal("Error ", e.getMessage()); |
| 186 | } | 191 | } |
| 187 | 192 | ||
| 188 | MediaType JSON | 193 | MediaType JSON |
| 189 | = MediaType.parse("application/json; charset=utf-8"); | 194 | = MediaType.parse("application/json; charset=utf-8"); |
| 190 | RequestBody requestBody = RequestBody.create(JSON, jsonObject.toString()); | 195 | RequestBody requestBody = RequestBody.create(JSON, jsonObject.toString()); |
| 191 | 196 | ||
| 192 | final Request.Builder request = new Request.Builder() | 197 | final Request.Builder request = new Request.Builder() |
| 193 | .url(baseUrl) | 198 | .url(baseUrl) |
| 194 | .header("User-Agent", Utils.getCustomUA()) | 199 | .header("User-Agent", Utils.getCustomUA()) |
| 195 | .header("Content-Type", "application/json") | 200 | .header("Content-Type", "application/json") |
| 196 | .post(requestBody); | 201 | .post(requestBody); |
| 197 | 202 | ||
| 198 | OkHttpClient httpClient = new OkHttpClient.Builder() | 203 | OkHttpClient httpClient = new OkHttpClient.Builder() |
| 199 | .readTimeout(30, TimeUnit.SECONDS) | 204 | .readTimeout(30, TimeUnit.SECONDS) |
| 200 | .writeTimeout(30, TimeUnit.SECONDS) | 205 | .writeTimeout(30, TimeUnit.SECONDS) |
| 201 | .connectTimeout(30, TimeUnit.SECONDS) | 206 | .connectTimeout(30, TimeUnit.SECONDS) |
| 202 | .build(); | 207 | .build(); |
| 203 | String jsonString = null; | 208 | String jsonString = null; |
| 204 | try { | 209 | try { |
| 205 | Response response = httpClient.newCall(request.build()).execute(); | 210 | Response response = httpClient.newCall(request.build()).execute(); |
| 206 | if (response == null) { | 211 | if (response == null) { |
| 207 | action.onError(null); | 212 | action.onError(null); |
| 208 | Debug.error("Response is null"); | 213 | Debug.error("Response is null"); |
| 209 | return null; | 214 | return null; |
| 210 | } | 215 | } |
| 211 | jsonString = getResponseData(response.body()); | 216 | jsonString = getResponseData(response.body()); |
| 212 | 217 | ||
| 213 | } catch (IOException e) { | 218 | } catch (IOException e) { |
| 214 | Debug.normal("Error %s", e.getMessage()); | 219 | Debug.normal("Error %s", e.getMessage()); |
| 215 | } | 220 | } |
| 216 | 221 | ||
| 217 | return jsonString; | 222 | return jsonString; |
| 218 | } | 223 | } |
| 219 | 224 | ||
| 220 | @Override | 225 | @Override |
| 221 | protected void onCancelled() { | 226 | protected void onCancelled() { |
| 222 | super.onCancelled(); | 227 | super.onCancelled(); |
| 223 | action.onError(null); | 228 | action.onError(null); |
| 224 | } | 229 | } |
| 225 | 230 | ||
| 226 | @Override | 231 | @Override |
| 227 | protected void onPostExecute(String jsonString) { | 232 | protected void onPostExecute(String jsonString) { |
| 228 | super.onPostExecute(jsonString); | 233 | super.onPostExecute(jsonString); |
| 229 | if ((jsonString == null) || (jsonString.isEmpty())) { | 234 | if ((jsonString == null) || (jsonString.isEmpty())) { |
| 230 | action.onError(null); | 235 | action.onError(null); |
| 231 | return; | 236 | return; |
| 232 | } | 237 | } |
| 233 | Debug.normal("String body: "+jsonString); | 238 | Debug.normal("String body: "+jsonString); |
| 234 | 239 | ||
| 235 | try { | 240 | try { |
| 236 | JSONObject jsonObject = new JSONObject(jsonString); | 241 | JSONObject jsonObject = new JSONObject(jsonString); |
| 237 | int status = jsonObject.optInt("status"); | 242 | int status = jsonObject.optInt("status"); |
| 238 | if (status == 1){ | 243 | if (status == 1){ |
| 239 | action.onSuccess(new JSONObject()); | 244 | action.onSuccess(new JSONObject()); |
| 240 | }else { | 245 | }else { |
| 241 | action.onError(new Error(jsonObject.optString("message"))); | 246 | action.onError(new Error(jsonObject.optString("message"))); |
| 242 | } | 247 | } |
| 243 | } catch (JSONException e) { | 248 | } catch (JSONException e) { |
| 244 | e.printStackTrace(); | 249 | e.printStackTrace(); |
| 245 | } | 250 | } |
| 246 | } | 251 | } |
| 247 | }.execute(); | 252 | }.execute(); |
| 248 | } | 253 | } |
| 249 | 254 | ||
| 250 | public static void register(final RegisterInfo registerInfo, final LLAPIManagerListener action) { | 255 | public static void register(final RegisterInfo registerInfo, final LLAPIManagerListener action) { |
| 251 | new AsyncTask<Void, Void, String>() { | 256 | new AsyncTask<Void, Void, String>() { |
| 252 | @Override | 257 | @Override |
| 253 | protected void onPreExecute() { | 258 | protected void onPreExecute() { |
| 254 | super.onPreExecute(); | 259 | super.onPreExecute(); |
| 255 | } | 260 | } |
| 256 | 261 | ||
| 257 | @Override | 262 | @Override |
| 258 | protected String doInBackground(Void... voids) { | 263 | protected String doInBackground(Void... voids) { |
| 259 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_REGISTER_INFO); | 264 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_REGISTER_INFO); |
| 260 | JSONObject jsonObject = new JSONObject(); | 265 | JSONObject jsonObject = new JSONObject(); |
| 261 | 266 | ||
| 262 | String token = APIResponse.getInstance().getToken(); | 267 | String token = APIResponse.getInstance().getToken(); |
| 263 | try { | 268 | try { |
| 264 | jsonObject.put("username", registerInfo.getUsername()); | 269 | jsonObject.put("username", registerInfo.getUsername()); |
| 265 | jsonObject.put("password", registerInfo.getPassword()); | 270 | jsonObject.put("password", registerInfo.getPassword()); |
| 266 | jsonObject.put("full_name", registerInfo.getFullName()); | 271 | jsonObject.put("full_name", registerInfo.getFullName()); |
| 267 | jsonObject.put("nickname", registerInfo.getNickname()); | 272 | jsonObject.put("nickname", registerInfo.getNickname()); |
| 268 | jsonObject.put("birthday", Utils.dateToStringFormatDayMonthYear(registerInfo.getBirthday())); | 273 | jsonObject.put("birthday", Utils.dateToStringFormatDayMonthYear(registerInfo.getBirthday())); |
| 269 | jsonObject.put("height", ""+registerInfo.getHeight()); | 274 | jsonObject.put("height", ""+registerInfo.getHeight()); |
| 270 | jsonObject.put("weight", ""+registerInfo.getWeight()); | 275 | jsonObject.put("weight", ""+registerInfo.getWeight()); |
| 271 | jsonObject.put("fat_rate", ""+registerInfo.getFatRate()); | 276 | jsonObject.put("fat_rate", ""+registerInfo.getFatRate()); |
| 272 | jsonObject.put("gender", ""+registerInfo.getGender()); | 277 | jsonObject.put("gender", ""+registerInfo.getGender()); |
| 273 | jsonObject.put("address", registerInfo.getAddress()); | 278 | jsonObject.put("address", registerInfo.getAddress()); |
| 274 | jsonObject.put("desciption", ""); | 279 | jsonObject.put("desciption", ""); |
| 275 | jsonObject.put("receive_notification", "0"); | 280 | jsonObject.put("receive_notification", "0"); |
| 276 | //jsonObject.put("phone", "+84986940999"); | 281 | //jsonObject.put("phone", "+84986940999"); |
| 277 | jsonObject.put("share_data", "1"); | 282 | jsonObject.put("share_data", "1"); |
| 278 | jsonObject.put("profile_image", ""+registerInfo.getProfileImage()); | 283 | jsonObject.put("profile_image", ""+registerInfo.getProfileImage()); |
| 279 | jsonObject.put("delete_flag", "0"); | 284 | jsonObject.put("delete_flag", "0"); |
| 280 | jsonObject.put("email", registerInfo.getEmail()); | 285 | jsonObject.put("email", registerInfo.getEmail()); |
| 281 | jsonObject.put("physical_activity", "0"); | 286 | jsonObject.put("physical_activity", "0"); |
| 282 | jsonObject.put("remember_me", "1"); | 287 | jsonObject.put("remember_me", "1"); |
| 283 | 288 | ||
| 284 | Debug.warn("Json data: " + jsonObject.toString()); | 289 | Debug.warn("Json data: " + jsonObject.toString()); |
| 285 | } catch (JSONException e) { | 290 | } catch (JSONException e) { |
| 286 | Debug.normal("Error ", e.getMessage()); | 291 | Debug.normal("Error ", e.getMessage()); |
| 287 | } | 292 | } |
| 288 | 293 | ||
| 289 | MediaType JSON | 294 | MediaType JSON |
| 290 | = MediaType.parse("application/json; charset=utf-8"); | 295 | = MediaType.parse("application/json; charset=utf-8"); |
| 291 | RequestBody requestBody = RequestBody.create(JSON, jsonObject.toString()); | 296 | RequestBody requestBody = RequestBody.create(JSON, jsonObject.toString()); |
| 292 | 297 | ||
| 293 | final Request.Builder request = new Request.Builder() | 298 | final Request.Builder request = new Request.Builder() |
| 294 | .url(baseUrl) | 299 | .url(baseUrl) |
| 295 | .header("User-Agent", Utils.getCustomUA()) | 300 | .header("User-Agent", Utils.getCustomUA()) |
| 296 | .header("Content-Type", "application/json") | 301 | .header("Content-Type", "application/json") |
| 297 | .post(requestBody); | 302 | .post(requestBody); |
| 298 | 303 | ||
| 299 | OkHttpClient httpClient = new OkHttpClient.Builder() | 304 | OkHttpClient httpClient = new OkHttpClient.Builder() |
| 300 | .readTimeout(30, TimeUnit.SECONDS) | 305 | .readTimeout(30, TimeUnit.SECONDS) |
| 301 | .writeTimeout(30, TimeUnit.SECONDS) | 306 | .writeTimeout(30, TimeUnit.SECONDS) |
| 302 | .connectTimeout(30, TimeUnit.SECONDS) | 307 | .connectTimeout(30, TimeUnit.SECONDS) |
| 303 | .build(); | 308 | .build(); |
| 304 | 309 | ||
| 305 | String jsonString = null; | 310 | String jsonString = null; |
| 306 | try { | 311 | try { |
| 307 | Response response = httpClient.newCall(request.build()).execute(); | 312 | Response response = httpClient.newCall(request.build()).execute(); |
| 308 | if (response == null) { | 313 | if (response == null) { |
| 309 | action.onError(null); | 314 | action.onError(null); |
| 310 | Debug.error("Response is null"); | 315 | Debug.error("Response is null"); |
| 311 | return null; | 316 | return null; |
| 312 | } | 317 | } |
| 313 | jsonString = getResponseData(response.body()); | 318 | jsonString = getResponseData(response.body()); |
| 314 | 319 | ||
| 315 | } catch (IOException e) { | 320 | } catch (IOException e) { |
| 316 | Debug.normal("Error %s", e.getMessage()); | 321 | Debug.normal("Error %s", e.getMessage()); |
| 317 | } | 322 | } |
| 318 | return jsonString; | 323 | return jsonString; |
| 319 | } | 324 | } |
| 320 | 325 | ||
| 321 | @Override | 326 | @Override |
| 322 | protected void onCancelled() { | 327 | protected void onCancelled() { |
| 323 | super.onCancelled(); | 328 | super.onCancelled(); |
| 324 | action.onError(null); | 329 | action.onError(null); |
| 325 | } | 330 | } |
| 326 | 331 | ||
| 327 | @Override | 332 | @Override |
| 328 | protected void onPostExecute(String jsonString) { | 333 | protected void onPostExecute(String jsonString) { |
| 329 | super.onPostExecute(jsonString); | 334 | super.onPostExecute(jsonString); |
| 330 | if ((jsonString == null) || (jsonString.isEmpty())) { | 335 | if ((jsonString == null) || (jsonString.isEmpty())) { |
| 331 | action.onError(null); | 336 | action.onError(null); |
| 332 | return; | 337 | return; |
| 333 | } | 338 | } |
| 334 | Debug.normal("String body: "+jsonString); | 339 | Debug.normal("String body: "+jsonString); |
| 335 | 340 | ||
| 336 | try { | 341 | try { |
| 337 | JSONObject jsonObject = new JSONObject(jsonString); | 342 | JSONObject jsonObject = new JSONObject(jsonString); |
| 338 | int status = jsonObject.optInt("status"); | 343 | int status = jsonObject.optInt("status"); |
| 339 | if (status == 1){ | 344 | if (status == 1){ |
| 340 | JSONObject object = jsonObject.optJSONObject("result"); | 345 | JSONObject object = jsonObject.optJSONObject("result"); |
| 341 | JSONObject objUser = object.getJSONObject("user"); | 346 | JSONObject objUser = object.getJSONObject("user"); |
| 342 | String token = object.optString("token"); | 347 | String token = object.optString("token"); |
| 343 | APIResponse.getInstance().setToken(token); | 348 | APIResponse.getInstance().setToken(token); |
| 344 | Debug.normal("Token: "+ token); | 349 | Debug.normal("Token: "+ token); |
| 345 | action.onSuccess(objUser); | 350 | action.onSuccess(objUser); |
| 346 | }else { | 351 | }else { |
| 347 | action.onError(new Error(jsonObject.optString("message"))); | 352 | action.onError(new Error(jsonObject.optString("message"))); |
| 348 | } | 353 | } |
| 349 | } catch (JSONException e) { | 354 | } catch (JSONException e) { |
| 350 | e.printStackTrace(); | 355 | e.printStackTrace(); |
| 351 | } | 356 | } |
| 352 | 357 | ||
| 353 | } | 358 | } |
| 354 | }.execute(); | 359 | }.execute(); |
| 355 | } | 360 | } |
| 356 | 361 | ||
| 357 | private static byte[] convertBitmapToByteArray(Bitmap bitmap) { | 362 | private static byte[] convertBitmapToByteArray(Bitmap bitmap) { |
| 358 | ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); | 363 | ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
| 359 | bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream); | 364 | bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream); |
| 360 | byte[] byteArray = byteArrayOutputStream.toByteArray(); | 365 | byte[] byteArray = byteArrayOutputStream.toByteArray(); |
| 361 | return byteArray; | 366 | return byteArray; |
| 362 | } | 367 | } |
| 363 | 368 | ||
| 364 | public static void forgetPass(final String email, final LLAPIManagerListener action) { | 369 | public static void forgetPass(final String email, final LLAPIManagerListener action) { |
| 365 | new AsyncTask<Void, Void, String>() { | 370 | new AsyncTask<Void, Void, String>() { |
| 366 | 371 | ||
| 367 | 372 | ||
| 368 | @Override | 373 | @Override |
| 369 | protected void onPreExecute() { | 374 | protected void onPreExecute() { |
| 370 | super.onPreExecute(); | 375 | super.onPreExecute(); |
| 371 | } | 376 | } |
| 372 | 377 | ||
| 373 | @Override | 378 | @Override |
| 374 | protected String doInBackground(Void... voids) { | 379 | protected String doInBackground(Void... voids) { |
| 375 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_FORGET_PASS_INFO); | 380 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_FORGET_PASS_INFO); |
| 376 | JSONObject jsonObject = new JSONObject(); | 381 | JSONObject jsonObject = new JSONObject(); |
| 377 | try { | 382 | try { |
| 378 | //Personal info | 383 | //Personal info |
| 379 | jsonObject.put("email", email); | 384 | jsonObject.put("email", email); |
| 380 | 385 | ||
| 381 | Debug.warn("Json data: " + jsonObject.toString()); | 386 | Debug.warn("Json data: " + jsonObject.toString()); |
| 382 | } catch (JSONException e) { | 387 | } catch (JSONException e) { |
| 383 | Debug.normal("Error ", e.getMessage()); | 388 | Debug.normal("Error ", e.getMessage()); |
| 384 | } | 389 | } |
| 385 | 390 | ||
| 386 | MediaType JSON | 391 | MediaType JSON |
| 387 | = MediaType.parse("application/json"); | 392 | = MediaType.parse("application/json"); |
| 388 | RequestBody requestBody = RequestBody.create(JSON, jsonObject.toString()); | 393 | RequestBody requestBody = RequestBody.create(JSON, jsonObject.toString()); |
| 389 | 394 | ||
| 390 | final Request.Builder request = new Request.Builder() | 395 | final Request.Builder request = new Request.Builder() |
| 391 | .url(baseUrl) | 396 | .url(baseUrl) |
| 392 | .header("User-Agent", Utils.getCustomUA()) | 397 | .header("User-Agent", Utils.getCustomUA()) |
| 393 | .header("Content-Type", "application/json") | 398 | .header("Content-Type", "application/json") |
| 394 | .post(requestBody); | 399 | .post(requestBody); |
| 395 | 400 | ||
| 396 | OkHttpClient httpClient = new OkHttpClient.Builder() | 401 | OkHttpClient httpClient = new OkHttpClient.Builder() |
| 397 | .readTimeout(30, TimeUnit.SECONDS) | 402 | .readTimeout(30, TimeUnit.SECONDS) |
| 398 | .writeTimeout(30, TimeUnit.SECONDS) | 403 | .writeTimeout(30, TimeUnit.SECONDS) |
| 399 | .connectTimeout(30, TimeUnit.SECONDS) | 404 | .connectTimeout(30, TimeUnit.SECONDS) |
| 400 | .build(); | 405 | .build(); |
| 401 | 406 | ||
| 402 | String jsonString = null; | 407 | String jsonString = null; |
| 403 | try { | 408 | try { |
| 404 | Response response = httpClient.newCall(request.build()).execute(); | 409 | Response response = httpClient.newCall(request.build()).execute(); |
| 405 | if (response == null) { | 410 | if (response == null) { |
| 406 | action.onError(null); | 411 | action.onError(null); |
| 407 | Debug.error("Response is null"); | 412 | Debug.error("Response is null"); |
| 408 | return null; | 413 | return null; |
| 409 | } | 414 | } |
| 410 | jsonString = getResponseData(response.body()); | 415 | jsonString = getResponseData(response.body()); |
| 411 | 416 | ||
| 412 | } catch (IOException e) { | 417 | } catch (IOException e) { |
| 413 | Debug.normal("Error %s", e.getMessage()); | 418 | Debug.normal("Error %s", e.getMessage()); |
| 414 | } | 419 | } |
| 415 | return jsonString; | 420 | return jsonString; |
| 416 | } | 421 | } |
| 417 | 422 | ||
| 418 | @Override | 423 | @Override |
| 419 | protected void onCancelled() { | 424 | protected void onCancelled() { |
| 420 | super.onCancelled(); | 425 | super.onCancelled(); |
| 421 | action.onError(null); | 426 | action.onError(null); |
| 422 | } | 427 | } |
| 423 | 428 | ||
| 424 | @Override | 429 | @Override |
| 425 | protected void onPostExecute(String jsonString) { | 430 | protected void onPostExecute(String jsonString) { |
| 426 | super.onPostExecute(jsonString); | 431 | super.onPostExecute(jsonString); |
| 427 | if ((jsonString == null) || (jsonString.isEmpty())) { | 432 | if ((jsonString == null) || (jsonString.isEmpty())) { |
| 428 | action.onError(null); | 433 | action.onError(null); |
| 429 | return; | 434 | return; |
| 430 | } | 435 | } |
| 431 | Debug.normal("String body: "+jsonString); | 436 | Debug.normal("String body: "+jsonString); |
| 432 | 437 | ||
| 433 | try { | 438 | try { |
| 434 | JSONObject jsonObject = new JSONObject(jsonString); | 439 | JSONObject jsonObject = new JSONObject(jsonString); |
| 435 | int status = jsonObject.optInt("status"); | 440 | int status = jsonObject.optInt("status"); |
| 436 | if (status == 1) { | 441 | if (status == 1) { |
| 437 | action.onSuccess(jsonString); | 442 | action.onSuccess(jsonString); |
| 438 | } else { | 443 | } else { |
| 439 | action.onError(new Error(jsonObject.getString("message"))); | 444 | action.onError(new Error(jsonObject.getString("message"))); |
| 440 | } | 445 | } |
| 441 | } catch (JSONException e) { | 446 | } catch (JSONException e) { |
| 442 | action.onError(null); | 447 | action.onError(null); |
| 443 | } | 448 | } |
| 444 | } | 449 | } |
| 445 | }.execute(); | 450 | }.execute(); |
| 446 | } | 451 | } |
| 447 | 452 | ||
| 448 | public static void forgetPassConfirm(final String email, final String codeConfirm, final LLAPIManagerListener action) { | 453 | public static void forgetPassConfirm(final String email, final String codeConfirm, final LLAPIManagerListener action) { |
| 449 | new AsyncTask<Void, Void, String>() { | 454 | new AsyncTask<Void, Void, String>() { |
| 450 | 455 | ||
| 451 | 456 | ||
| 452 | @Override | 457 | @Override |
| 453 | protected void onPreExecute() { | 458 | protected void onPreExecute() { |
| 454 | super.onPreExecute(); | 459 | super.onPreExecute(); |
| 455 | } | 460 | } |
| 456 | 461 | ||
| 457 | @Override | 462 | @Override |
| 458 | protected String doInBackground(Void... voids) { | 463 | protected String doInBackground(Void... voids) { |
| 459 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_FORGET_PASS_CONFIRM_INFO); | 464 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_FORGET_PASS_CONFIRM_INFO); |
| 460 | JSONObject jsonObject = new JSONObject(); | 465 | JSONObject jsonObject = new JSONObject(); |
| 461 | try { | 466 | try { |
| 462 | //Personal info | 467 | //Personal info |
| 463 | jsonObject.put("email", email); | 468 | jsonObject.put("email", email); |
| 464 | jsonObject.put("code_confirm", codeConfirm); | 469 | jsonObject.put("code_confirm", codeConfirm); |
| 465 | 470 | ||
| 466 | Debug.warn("Json data: " + jsonObject.toString()); | 471 | Debug.warn("Json data: " + jsonObject.toString()); |
| 467 | } catch (JSONException e) { | 472 | } catch (JSONException e) { |
| 468 | Debug.normal("Error ", e.getMessage()); | 473 | Debug.normal("Error ", e.getMessage()); |
| 469 | } | 474 | } |
| 470 | 475 | ||
| 471 | MediaType JSON | 476 | MediaType JSON |
| 472 | = MediaType.parse("application/json"); | 477 | = MediaType.parse("application/json"); |
| 473 | RequestBody requestBody = RequestBody.create(JSON, jsonObject.toString()); | 478 | RequestBody requestBody = RequestBody.create(JSON, jsonObject.toString()); |
| 474 | 479 | ||
| 475 | final Request.Builder request = new Request.Builder() | 480 | final Request.Builder request = new Request.Builder() |
| 476 | .url(baseUrl) | 481 | .url(baseUrl) |
| 477 | .header("User-Agent", Utils.getCustomUA()) | 482 | .header("User-Agent", Utils.getCustomUA()) |
| 478 | .header("Content-Type", "application/json") | 483 | .header("Content-Type", "application/json") |
| 479 | .post(requestBody); | 484 | .post(requestBody); |
| 480 | 485 | ||
| 481 | OkHttpClient httpClient = new OkHttpClient.Builder() | 486 | OkHttpClient httpClient = new OkHttpClient.Builder() |
| 482 | .readTimeout(30, TimeUnit.SECONDS) | 487 | .readTimeout(30, TimeUnit.SECONDS) |
| 483 | .writeTimeout(30, TimeUnit.SECONDS) | 488 | .writeTimeout(30, TimeUnit.SECONDS) |
| 484 | .connectTimeout(30, TimeUnit.SECONDS) | 489 | .connectTimeout(30, TimeUnit.SECONDS) |
| 485 | .build(); | 490 | .build(); |
| 486 | String jsonString = null; | 491 | String jsonString = null; |
| 487 | try { | 492 | try { |
| 488 | Response response = httpClient.newCall(request.build()).execute(); | 493 | Response response = httpClient.newCall(request.build()).execute(); |
| 489 | if (response == null) { | 494 | if (response == null) { |
| 490 | action.onError(null); | 495 | action.onError(null); |
| 491 | Debug.error("Response is null"); | 496 | Debug.error("Response is null"); |
| 492 | return null; | 497 | return null; |
| 493 | } | 498 | } |
| 494 | jsonString = getResponseData(response.body()); | 499 | jsonString = getResponseData(response.body()); |
| 495 | 500 | ||
| 496 | } catch (IOException e) { | 501 | } catch (IOException e) { |
| 497 | Debug.normal("Error %s", e.getMessage()); | 502 | Debug.normal("Error %s", e.getMessage()); |
| 498 | } | 503 | } |
| 499 | return jsonString; | 504 | return jsonString; |
| 500 | } | 505 | } |
| 501 | 506 | ||
| 502 | @Override | 507 | @Override |
| 503 | protected void onCancelled() { | 508 | protected void onCancelled() { |
| 504 | super.onCancelled(); | 509 | super.onCancelled(); |
| 505 | action.onError(null); | 510 | action.onError(null); |
| 506 | } | 511 | } |
| 507 | 512 | ||
| 508 | @Override | 513 | @Override |
| 509 | protected void onPostExecute(String jsonString) { | 514 | protected void onPostExecute(String jsonString) { |
| 510 | super.onPostExecute(jsonString); | 515 | super.onPostExecute(jsonString); |
| 511 | if ((jsonString == null) || (jsonString.isEmpty())) { | 516 | if ((jsonString == null) || (jsonString.isEmpty())) { |
| 512 | action.onError(null); | 517 | action.onError(null); |
| 513 | return; | 518 | return; |
| 514 | } | 519 | } |
| 515 | Debug.normal("String body: "+jsonString); | 520 | Debug.normal("String body: "+jsonString); |
| 516 | 521 | ||
| 517 | try { | 522 | try { |
| 518 | JSONObject jsonObject = new JSONObject(jsonString); | 523 | JSONObject jsonObject = new JSONObject(jsonString); |
| 519 | int status = jsonObject.optInt("status"); | 524 | int status = jsonObject.optInt("status"); |
| 520 | if (status == 1) { | 525 | if (status == 1) { |
| 521 | action.onSuccess(jsonString); | 526 | action.onSuccess(jsonString); |
| 522 | } else { | 527 | } else { |
| 523 | action.onError(new Error(jsonObject.getString("message"))); | 528 | action.onError(new Error(jsonObject.getString("message"))); |
| 524 | } | 529 | } |
| 525 | } catch (JSONException e) { | 530 | } catch (JSONException e) { |
| 526 | action.onError(null); | 531 | action.onError(null); |
| 527 | } | 532 | } |
| 528 | } | 533 | } |
| 529 | }.execute(); | 534 | }.execute(); |
| 530 | } | 535 | } |
| 531 | 536 | ||
| 532 | public static void history(final Date fromDate, final Date toDate, final LLAPIManagerListener action) { | 537 | public static void history(final Date fromDate, final Date toDate, final LLAPIManagerListener action) { |
| 533 | new AsyncTask<Void, Void, String>() { | 538 | new AsyncTask<Void, Void, String>() { |
| 534 | 539 | ||
| 535 | 540 | ||
| 536 | @Override | 541 | @Override |
| 537 | protected void onPreExecute() { | 542 | protected void onPreExecute() { |
| 538 | super.onPreExecute(); | 543 | super.onPreExecute(); |
| 539 | } | 544 | } |
| 540 | 545 | ||
| 541 | @Override | 546 | @Override |
| 542 | protected String doInBackground(Void... voids) { | 547 | protected String doInBackground(Void... voids) { |
| 543 | 548 | ||
| 544 | String fromStr = Utils.dateToStringFormatYearMonthDay(fromDate); | 549 | String fromStr = Utils.dateToStringFormatYearMonthDay(fromDate); |
| 545 | String toStr = Utils.dateToStringFormatYearMonthDay(toDate); | 550 | String toStr = Utils.dateToStringFormatYearMonthDay(toDate); |
| 546 | Debug.normal("Token: "+ APIResponse.getInstance().getToken()); | 551 | Debug.normal("Token: "+ APIResponse.getInstance().getToken()); |
| 547 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_HISTORE_INFO + "/"+ fromStr +"/"+ toStr); | 552 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_HISTORE_INFO + "/"+ fromStr +"/"+ toStr); |
| 548 | Debug.normal("URL: "+baseUrl.toString()); | 553 | Debug.normal("URL: "+baseUrl.toString()); |
| 549 | final Request.Builder request = new Request.Builder() | 554 | final Request.Builder request = new Request.Builder() |
| 550 | .url(baseUrl) | 555 | .url(baseUrl) |
| 551 | .header("User-Agent", Utils.getCustomUA()) | 556 | .header("User-Agent", Utils.getCustomUA()) |
| 552 | .header("Content-Type", "application/json") | 557 | .header("Content-Type", "application/json") |
| 553 | .addHeader("token", APIResponse.getInstance().getToken()) | 558 | .addHeader("token", APIResponse.getInstance().getToken()) |
| 554 | .get(); | 559 | .get(); |
| 555 | 560 | ||
| 556 | OkHttpClient httpClient = new OkHttpClient.Builder() | 561 | OkHttpClient httpClient = new OkHttpClient.Builder() |
| 557 | .readTimeout(30, TimeUnit.SECONDS) | 562 | .readTimeout(30, TimeUnit.SECONDS) |
| 558 | .writeTimeout(30, TimeUnit.SECONDS) | 563 | .writeTimeout(30, TimeUnit.SECONDS) |
| 559 | .connectTimeout(30, TimeUnit.SECONDS) | 564 | .connectTimeout(30, TimeUnit.SECONDS) |
| 560 | .build(); | 565 | .build(); |
| 561 | 566 | ||
| 562 | String jsonString = null; | 567 | String jsonString = null; |
| 563 | try { | 568 | try { |
| 564 | Response response = httpClient.newCall(request.build()).execute(); | 569 | Response response = httpClient.newCall(request.build()).execute(); |
| 565 | if (response == null) { | 570 | if (response == null) { |
| 566 | action.onError(null); | 571 | action.onError(null); |
| 567 | Debug.error("Response is null"); | 572 | Debug.error("Response is null"); |
| 568 | return null; | 573 | return null; |
| 569 | } | 574 | } |
| 570 | jsonString = getResponseData(response.body()); | 575 | jsonString = getResponseData(response.body()); |
| 571 | 576 | ||
| 572 | } catch (IOException e) { | 577 | } catch (IOException e) { |
| 573 | Debug.normal("Error %s", e.getMessage()); | 578 | Debug.normal("Error %s", e.getMessage()); |
| 574 | } | 579 | } |
| 575 | return jsonString; | 580 | return jsonString; |
| 576 | } | 581 | } |
| 577 | 582 | ||
| 578 | @Override | 583 | @Override |
| 579 | protected void onCancelled() { | 584 | protected void onCancelled() { |
| 580 | super.onCancelled(); | 585 | super.onCancelled(); |
| 581 | action.onError(null); | 586 | action.onError(null); |
| 582 | } | 587 | } |
| 583 | 588 | ||
| 584 | @Override | 589 | @Override |
| 585 | protected void onPostExecute(String jsonString) { | 590 | protected void onPostExecute(String jsonString) { |
| 586 | super.onPostExecute(jsonString); | 591 | super.onPostExecute(jsonString); |
| 587 | if ((jsonString == null) || (jsonString.isEmpty())) { | 592 | if ((jsonString == null) || (jsonString.isEmpty())) { |
| 588 | action.onError(null); | 593 | action.onError(null); |
| 589 | return; | 594 | return; |
| 590 | } | 595 | } |
| 591 | Debug.normal("String body: "+jsonString); | 596 | Debug.normal("String body: "+jsonString); |
| 592 | 597 | ||
| 593 | try { | 598 | try { |
| 594 | JSONObject jsonObject = new JSONObject(jsonString); | 599 | JSONObject jsonObject = new JSONObject(jsonString); |
| 595 | int status = jsonObject.optInt("status"); | 600 | int status = jsonObject.optInt("status"); |
| 596 | if (status == 1) { | 601 | if (status == 1) { |
| 597 | action.onSuccess(jsonString); | 602 | action.onSuccess(jsonString); |
| 598 | } else { | 603 | } else { |
| 599 | action.onError(new Error(jsonObject.getString("message"))); | 604 | action.onError(new Error(jsonObject.getString("message"))); |
| 600 | } | 605 | } |
| 601 | } catch (JSONException e) { | 606 | } catch (JSONException e) { |
| 602 | action.onError(null); | 607 | action.onError(null); |
| 603 | } | 608 | } |
| 604 | } | 609 | } |
| 605 | }.execute(); | 610 | }.execute(); |
| 606 | } | 611 | } |
| 607 | 612 | ||
| 608 | public static void historyDetail(final Date fromDate, final Date toDate, final LLAPIManagerListener action) { | 613 | public static void historyDetail(final Date fromDate, final Date toDate, final LLAPIManagerListener action) { |
| 609 | new AsyncTask<Void, Void, String>() { | 614 | new AsyncTask<Void, Void, String>() { |
| 610 | 615 | ||
| 611 | 616 | ||
| 612 | @Override | 617 | @Override |
| 613 | protected void onPreExecute() { | 618 | protected void onPreExecute() { |
| 614 | super.onPreExecute(); | 619 | super.onPreExecute(); |
| 615 | } | 620 | } |
| 616 | 621 | ||
| 617 | @Override | 622 | @Override |
| 618 | protected String doInBackground(Void... voids) { | 623 | protected String doInBackground(Void... voids) { |
| 619 | 624 | ||
| 620 | String fromStr = Utils.dateToStringFormatDayMonthYear(fromDate); | 625 | String fromStr = Utils.dateToStringFormatDayMonthYear(fromDate); |
| 621 | String toStr = Utils.dateToStringFormatDayMonthYear(toDate); | 626 | String toStr = Utils.dateToStringFormatDayMonthYear(toDate); |
| 622 | Debug.normal("Token: "+ APIResponse.getInstance().getToken()); | 627 | Debug.normal("Token: "+ APIResponse.getInstance().getToken()); |
| 623 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_HISTORE_DETAIL_INFO + "/"+ fromStr +"/"+ toStr); | 628 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_HISTORE_DETAIL_INFO + "/"+ fromStr +"/"+ toStr); |
| 624 | Debug.normal("URL: "+baseUrl.toString()); | 629 | Debug.normal("URL: "+baseUrl.toString()); |
| 625 | final Request.Builder request = new Request.Builder() | 630 | final Request.Builder request = new Request.Builder() |
| 626 | .url(baseUrl) | 631 | .url(baseUrl) |
| 627 | .header("User-Agent", Utils.getCustomUA()) | 632 | .header("User-Agent", Utils.getCustomUA()) |
| 628 | .header("Content-Type", "application/json") | 633 | .header("Content-Type", "application/json") |
| 629 | .addHeader("token", APIResponse.getInstance().getToken()) | 634 | .addHeader("token", APIResponse.getInstance().getToken()) |
| 630 | .get(); | 635 | .get(); |
| 631 | 636 | ||
| 632 | OkHttpClient httpClient = new OkHttpClient.Builder() | 637 | OkHttpClient httpClient = new OkHttpClient.Builder() |
| 633 | .readTimeout(30, TimeUnit.SECONDS) | 638 | .readTimeout(30, TimeUnit.SECONDS) |
| 634 | .writeTimeout(30, TimeUnit.SECONDS) | 639 | .writeTimeout(30, TimeUnit.SECONDS) |
| 635 | .connectTimeout(30, TimeUnit.SECONDS) | 640 | .connectTimeout(30, TimeUnit.SECONDS) |
| 636 | .build(); | 641 | .build(); |
| 637 | 642 | ||
| 638 | String jsonString = null; | 643 | String jsonString = null; |
| 639 | try { | 644 | try { |
| 640 | Response response = httpClient.newCall(request.build()).execute(); | 645 | Response response = httpClient.newCall(request.build()).execute(); |
| 641 | if (response == null) { | 646 | if (response == null) { |
| 642 | action.onError(null); | 647 | action.onError(null); |
| 643 | Debug.error("Response is null"); | 648 | Debug.error("Response is null"); |
| 644 | return null; | 649 | return null; |
| 645 | } | 650 | } |
| 646 | jsonString = getResponseData(response.body()); | 651 | jsonString = getResponseData(response.body()); |
| 647 | 652 | ||
| 648 | } catch (IOException e) { | 653 | } catch (IOException e) { |
| 649 | Debug.normal("Error %s", e.getMessage()); | 654 | Debug.normal("Error %s", e.getMessage()); |
| 650 | } | 655 | } |
| 651 | return jsonString; | 656 | return jsonString; |
| 652 | } | 657 | } |
| 653 | 658 | ||
| 654 | @Override | 659 | @Override |
| 655 | protected void onCancelled() { | 660 | protected void onCancelled() { |
| 656 | super.onCancelled(); | 661 | super.onCancelled(); |
| 657 | action.onError(null); | 662 | action.onError(null); |
| 658 | } | 663 | } |
| 659 | 664 | ||
| 660 | @Override | 665 | @Override |
| 661 | protected void onPostExecute(String jsonString) { | 666 | protected void onPostExecute(String jsonString) { |
| 662 | super.onPostExecute(jsonString); | 667 | super.onPostExecute(jsonString); |
| 663 | if ((jsonString == null) || (jsonString.isEmpty())) { | 668 | if ((jsonString == null) || (jsonString.isEmpty())) { |
| 664 | action.onError(null); | 669 | action.onError(null); |
| 665 | return; | 670 | return; |
| 666 | } | 671 | } |
| 667 | Debug.normal("String body: "+jsonString); | 672 | Debug.normal("String body: "+jsonString); |
| 668 | 673 | ||
| 669 | try { | 674 | try { |
| 670 | JSONObject jsonObject = new JSONObject(jsonString); | 675 | JSONObject jsonObject = new JSONObject(jsonString); |
| 671 | int status = jsonObject.optInt("status"); | 676 | int status = jsonObject.optInt("status"); |
| 672 | if (status == 1) { | 677 | if (status == 1) { |
| 673 | action.onSuccess(jsonString); | 678 | action.onSuccess(jsonString); |
| 674 | } else { | 679 | } else { |
| 675 | action.onError(new Error(jsonObject.getString("message"))); | 680 | action.onError(new Error(jsonObject.getString("message"))); |
| 676 | } | 681 | } |
| 677 | } catch (JSONException e) { | 682 | } catch (JSONException e) { |
| 678 | action.onError(null); | 683 | action.onError(null); |
| 679 | } | 684 | } |
| 680 | } | 685 | } |
| 681 | }.execute(); | 686 | }.execute(); |
| 682 | } | 687 | } |
| 683 | 688 | ||
| 684 | public static void homePage(final Date date, final Const.STEP_TYPE stepType, final LLAPIManagerListener action) { | 689 | public static void homePage(final Date date, final Const.STEP_TYPE stepType, final LLAPIManagerListener action) { |
| 685 | new AsyncTask<Void, Void, String>() { | 690 | new AsyncTask<Void, Void, String>() { |
| 686 | 691 | ||
| 687 | 692 | ||
| 688 | @Override | 693 | @Override |
| 689 | protected void onPreExecute() { | 694 | protected void onPreExecute() { |
| 690 | super.onPreExecute(); | 695 | super.onPreExecute(); |
| 691 | } | 696 | } |
| 692 | 697 | ||
| 693 | @Override | 698 | @Override |
| 694 | protected String doInBackground(Void... voids) { | 699 | protected String doInBackground(Void... voids) { |
| 695 | 700 | ||
| 696 | String dateStr = Utils.dateToStringFormatDayMonthYear(date); | 701 | String dateStr = Utils.dateToStringFormatDayMonthYear(date); |
| 697 | Debug.normal("Token: "+ APIResponse.getInstance().getToken()); | 702 | Debug.normal("Token: "+ APIResponse.getInstance().getToken()); |
| 698 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_HOME_INFO + "/"+ stepType.value +"/"+ dateStr); | 703 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_HOME_INFO + "/"+ stepType.value +"/"+ dateStr); |
| 699 | Debug.normal("URL: "+baseUrl.toString()); | 704 | Debug.normal("URL: "+baseUrl.toString()); |
| 700 | final Request.Builder request = new Request.Builder() | 705 | final Request.Builder request = new Request.Builder() |
| 701 | .url(baseUrl) | 706 | .url(baseUrl) |
| 702 | .header("User-Agent", Utils.getCustomUA()) | 707 | .header("User-Agent", Utils.getCustomUA()) |
| 703 | .header("Content-Type", "application/json") | 708 | .header("Content-Type", "application/json") |
| 704 | .addHeader("token", APIResponse.getInstance().getToken()) | 709 | .addHeader("token", APIResponse.getInstance().getToken()) |
| 705 | .get(); | 710 | .get(); |
| 711 | |||
| 712 | OkHttpClient httpClient = new OkHttpClient.Builder() | ||
| 713 | .readTimeout(30, TimeUnit.SECONDS) | ||
| 714 | .writeTimeout(30, TimeUnit.SECONDS) | ||
| 715 | .connectTimeout(30, TimeUnit.SECONDS) | ||
| 716 | .build(); | ||
| 717 | |||
| 718 | String jsonString = null; | ||
| 719 | try { | ||
| 720 | Response response = httpClient.newCall(request.build()).execute(); | ||
| 721 | if (response == null) { | ||
| 722 | action.onError(null); | ||
| 723 | Debug.error("Response is null"); | ||
| 724 | return null; | ||
| 725 | } | ||
| 726 | jsonString = getResponseData(response.body()); | ||
| 727 | |||
| 728 | } catch (IOException e) { | ||
| 729 | Debug.normal("Error %s", e.getMessage()); | ||
| 730 | } | ||
| 731 | return jsonString; | ||
| 732 | } | ||
| 733 | |||
| 734 | @Override | ||
| 735 | protected void onCancelled() { | ||
| 736 | super.onCancelled(); | ||
| 737 | action.onError(null); | ||
| 738 | } | ||
| 739 | |||
| 740 | @Override | ||
| 741 | protected void onPostExecute(String jsonString) { | ||
| 742 | super.onPostExecute(jsonString); | ||
| 743 | if ((jsonString == null) || (jsonString.isEmpty())) { | ||
| 744 | action.onError(null); | ||
| 745 | return; | ||
| 746 | } | ||
| 747 | Debug.normal("String body: "+jsonString); | ||
| 748 | |||
| 749 | try { | ||
| 750 | JSONObject jsonObject = new JSONObject(jsonString); | ||
| 751 | int status = jsonObject.optInt("status"); | ||
| 752 | if (status == 1) { | ||
| 753 | action.onSuccess(jsonString); | ||
| 754 | } else { | ||
| 755 | action.onError(new Error(jsonObject.getString("message"))); | ||
| 756 | } | ||
| 757 | } catch (JSONException e) { | ||
| 758 | action.onError(null); | ||
| 759 | } | ||
| 760 | } | ||
| 761 | }.execute(); | ||
| 762 | } | ||
| 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 | super.onPreExecute(); | ||
| 847 | } | ||
| 848 | |||
| 849 | @Override | ||
| 850 | protected String doInBackground(Void... voids) { | ||
| 851 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_CREATE_LOG_INFO); | ||
| 852 | JSONObject jsonObject = new JSONObject(); | ||
| 853 | String startT = Utils.convertDate2DateTimeString(startTime); | ||
| 854 | String endT = Utils.convertDate2DateTimeString(endTime); | ||
| 855 | try { | ||
| 856 | jsonObject.put("mode", stepType.value); | ||
| 857 | jsonObject.put("numStep", stepCount); | ||
| 858 | jsonObject.put("startTime", startT); | ||
| 859 | jsonObject.put("endTime", endT); | ||
| 860 | |||
| 861 | Debug.warn("Json data: " + jsonObject.toString()); | ||
| 862 | } catch (JSONException e) { | ||
| 863 | Debug.normal("Error ", e.getMessage()); | ||
| 864 | } | ||
| 865 | |||
| 866 | MediaType JSON | ||
| 867 | = MediaType.parse("application/json"); | ||
| 868 | RequestBody requestBody = RequestBody.create(JSON, jsonObject.toString()); | ||
| 869 | |||
| 870 | final Request.Builder request = new Request.Builder() | ||
| 871 | .url(baseUrl) | ||
| 872 | .header("User-Agent", Utils.getCustomUA()) | ||
| 873 | .header("Content-Type", "application/json") | ||
| 874 | .addHeader("token", APIResponse.getInstance().getToken()) | ||
| 875 | .post(requestBody); | ||
| 706 | 876 | ||
| 707 | OkHttpClient httpClient = new OkHttpClient.Builder() | 877 | OkHttpClient httpClient = new OkHttpClient.Builder() |
| 708 | .readTimeout(30, TimeUnit.SECONDS) | 878 | .readTimeout(30, TimeUnit.SECONDS) |
| 709 | .writeTimeout(30, TimeUnit.SECONDS) | 879 | .writeTimeout(30, TimeUnit.SECONDS) |
| 710 | .connectTimeout(30, TimeUnit.SECONDS) | 880 | .connectTimeout(30, TimeUnit.SECONDS) |
| 711 | .build(); | 881 | .build(); |
| 712 | 882 | ||
| 713 | String jsonString = null; | 883 | String jsonString = null; |
| 714 | try { | 884 | try { |
| 715 | Response response = httpClient.newCall(request.build()).execute(); | 885 | Response response = httpClient.newCall(request.build()).execute(); |
| 716 | if (response == null) { | 886 | if (response == null) { |
| 717 | action.onError(null); | 887 | action.onError(null); |
| 718 | Debug.error("Response is null"); | 888 | Debug.error("Response is null"); |
| 719 | return null; | 889 | return null; |
| 720 | } | 890 | } |
| 721 | jsonString = getResponseData(response.body()); | 891 | jsonString = getResponseData(response.body()); |
| 722 | 892 | ||
| 723 | } catch (IOException e) { | 893 | } catch (IOException e) { |
| 724 | Debug.normal("Error %s", e.getMessage()); | 894 | Debug.normal("Error %s", e.getMessage()); |
| 725 | } | 895 | } |
| 726 | return jsonString; | 896 | return jsonString; |
| 727 | } | 897 | } |
| 728 | 898 | ||
| 729 | @Override | 899 | @Override |
| 730 | protected void onCancelled() { | 900 | protected void onCancelled() { |
| 731 | super.onCancelled(); | 901 | super.onCancelled(); |
| 732 | action.onError(null); | 902 | action.onError(null); |
| 733 | } | 903 | } |
| 734 | 904 | ||
| 735 | @Override | 905 | @Override |
| 736 | protected void onPostExecute(String jsonString) { | 906 | protected void onPostExecute(String jsonString) { |
| 737 | super.onPostExecute(jsonString); | 907 | super.onPostExecute(jsonString); |
| 738 | if ((jsonString == null) || (jsonString.isEmpty())) { | 908 | if ((jsonString == null) || (jsonString.isEmpty())) { |
| 739 | action.onError(null); | 909 | action.onError(null); |
| 740 | return; | 910 | return; |
| 741 | } | 911 | } |
| 742 | Debug.normal("String body: "+jsonString); | 912 | Debug.normal("String body: "+jsonString); |
| 743 | 913 | ||
| 744 | try { | 914 | try { |
| 745 | JSONObject jsonObject = new JSONObject(jsonString); | 915 | JSONObject jsonObject = new JSONObject(jsonString); |
| 746 | int status = jsonObject.optInt("status"); | 916 | int status = jsonObject.optInt("status"); |
| 747 | if (status == 1) { | 917 | if (status == 1) { |
| 748 | action.onSuccess(jsonString); | 918 | action.onSuccess(jsonString); |
| 749 | } else { | 919 | } else { |
| 750 | action.onError(new Error(jsonObject.getString("message"))); | 920 | action.onError(new Error(jsonObject.getString("message"))); |
| 751 | } | 921 | } |
| 752 | } catch (JSONException e) { | 922 | } catch (JSONException e) { |
| 753 | action.onError(null); | 923 | action.onError(null); |
| 754 | } | 924 | } |
| 755 | } | 925 | } |
| 756 | }.execute(); | 926 | }.execute(); |
| 757 | } | 927 | } |
| 758 | 928 | ||
| 759 | public static void refreshToken(final Context context, final int userId, final LLAPIManagerListener action) { | 929 | public static void refreshToken(final Context context, final int userId, final LLAPIManagerListener action) { |
| 760 | new AsyncTask<Void, Void, String>() { | 930 | new AsyncTask<Void, Void, String>() { |
| 761 | 931 | ||
| 762 | 932 | ||
| 763 | @Override | 933 | @Override |
| 764 | protected void onPreExecute() { | 934 | protected void onPreExecute() { |
| 765 | super.onPreExecute(); | 935 | super.onPreExecute(); |
| 766 | } | 936 | } |
| 767 | 937 | ||
| 768 | @Override | 938 | @Override |
| 769 | protected String doInBackground(Void... voids) { | 939 | protected String doInBackground(Void... voids) { |
| 770 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_REFESH_TOKEN_INFO); | 940 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_REFESH_TOKEN_INFO); |
| 771 | JSONObject jsonObject = new JSONObject(); | 941 | JSONObject jsonObject = new JSONObject(); |
| 772 | try { | 942 | try { |
| 773 | //Personal info | 943 | //Personal info |
| 774 | jsonObject.put("userId", userId); | 944 | jsonObject.put("userId", userId); |
| 775 | 945 | ||
| 776 | Debug.warn("Json data: " + jsonObject.toString()); | 946 | Debug.warn("Json data: " + jsonObject.toString()); |
| 777 | } catch (JSONException e) { | 947 | } catch (JSONException e) { |
| 778 | Debug.normal("Error ", e.getMessage()); | 948 | Debug.normal("Error ", e.getMessage()); |
| 779 | } | 949 | } |
| 780 | 950 | ||
| 781 | MediaType JSON | 951 | MediaType JSON |
| 782 | = MediaType.parse("application/json"); | 952 | = MediaType.parse("application/json"); |
| 783 | RequestBody requestBody = RequestBody.create(JSON, jsonObject.toString()); | 953 | RequestBody requestBody = RequestBody.create(JSON, jsonObject.toString()); |
| 784 | 954 | ||
| 785 | final Request.Builder request = new Request.Builder() | 955 | final Request.Builder request = new Request.Builder() |
| 786 | .url(baseUrl) | 956 | .url(baseUrl) |
| 787 | .header("User-Agent", Utils.getCustomUA()) | 957 | .header("User-Agent", Utils.getCustomUA()) |
| 788 | .header("Content-Type", "application/json") | 958 | .header("Content-Type", "application/json") |
| 789 | .post(requestBody); | 959 | .post(requestBody); |
| 790 | 960 | ||
| 791 | OkHttpClient httpClient = new OkHttpClient.Builder() | 961 | OkHttpClient httpClient = new OkHttpClient.Builder() |
| 792 | .readTimeout(30, TimeUnit.SECONDS) | 962 | .readTimeout(30, TimeUnit.SECONDS) |
| 793 | .writeTimeout(30, TimeUnit.SECONDS) | 963 | .writeTimeout(30, TimeUnit.SECONDS) |
| 794 | .connectTimeout(30, TimeUnit.SECONDS) | 964 | .connectTimeout(30, TimeUnit.SECONDS) |
| 795 | .build(); | 965 | .build(); |
| 796 | 966 | ||
| 797 | String jsonString = null; | 967 | String jsonString = null; |
| 798 | try { | 968 | try { |
| 799 | Response response = httpClient.newCall(request.build()).execute(); | 969 | Response response = httpClient.newCall(request.build()).execute(); |
| 800 | if (response == null) { | 970 | if (response == null) { |
| 801 | action.onError(null); | 971 | action.onError(null); |
| 802 | Debug.error("Response is null"); | 972 | Debug.error("Response is null"); |
| 803 | return null; | 973 | return null; |
| 804 | } | 974 | } |
| 805 | jsonString = getResponseData(response.body()); | 975 | jsonString = getResponseData(response.body()); |
| 806 | 976 | ||
| 807 | } catch (IOException e) { | 977 | } catch (IOException e) { |
| 808 | Debug.normal("Error %s", e.getMessage()); | 978 | Debug.normal("Error %s", e.getMessage()); |
| 809 | } | 979 | } |
| 810 | return jsonString; | 980 | return jsonString; |
| 811 | } | 981 | } |
| 812 | 982 | ||
| 813 | @Override | 983 | @Override |
| 814 | protected void onCancelled() { | 984 | protected void onCancelled() { |
| 815 | super.onCancelled(); | 985 | super.onCancelled(); |
| 816 | action.onError(null); | 986 | action.onError(null); |
| 817 | } | 987 | } |
| 818 | 988 | ||
| 819 | @Override | 989 | @Override |
| 820 | protected void onPostExecute(String jsonString) { | 990 | protected void onPostExecute(String jsonString) { |
| 821 | super.onPostExecute(jsonString); | 991 | super.onPostExecute(jsonString); |
| 822 | if ((jsonString == null) || (jsonString.isEmpty())) { | 992 | if ((jsonString == null) || (jsonString.isEmpty())) { |
| 823 | action.onError(null); | 993 | action.onError(null); |
| 824 | return; | 994 | return; |
| 825 | } | 995 | } |
| 826 | Debug.normal("String body: "+jsonString); | 996 | Debug.normal("String body: "+jsonString); |
| 827 | 997 | ||
| 828 | try { | 998 | try { |
| 829 | JSONObject jsonObject = new JSONObject(jsonString); | 999 | JSONObject jsonObject = new JSONObject(jsonString); |
| 830 | int status = jsonObject.optInt("status"); | 1000 | int status = jsonObject.optInt("status"); |
| 831 | if (status == 1) { | 1001 | if (status == 1) { |
| 832 | JSONArray tokenArr = jsonObject.optJSONArray("result"); | 1002 | JSONArray tokenArr = jsonObject.optJSONArray("result"); |
| 833 | String token = tokenArr.get(0).toString(); | 1003 | String token = tokenArr.get(0).toString(); |
| 834 | Debug.normal("Refresh Token: "+ token); | 1004 | Debug.normal("Refresh Token: "+ token); |
| 835 | // save data | 1005 | // save data |
| 836 | Setting.setUserDataSharepre(context, Setting.TOKEN_SHAREPRE, token); | 1006 | Setting.setUserDataSharepre(context, Setting.TOKEN_SHAREPRE, token); |
| 837 | APIResponse.getInstance().setToken(token); | 1007 | APIResponse.getInstance().setToken(token); |
| 838 | action.onSuccess(jsonString); | 1008 | action.onSuccess(jsonString); |
| 839 | } else { | 1009 | } else { |
| 840 | action.onError(new Error(jsonObject.getString("message"))); | 1010 | action.onError(new Error(jsonObject.getString("message"))); |
| 841 | } | 1011 | } |
| 842 | } catch (JSONException e) { | 1012 | } catch (JSONException e) { |
| 843 | action.onError(null); | 1013 | action.onError(null); |
| 844 | } | 1014 | } |
| 845 | } | 1015 | } |
| 846 | }.execute(); | 1016 | }.execute(); |
| 847 | } | 1017 | } |
| 848 | 1018 | ||
| 849 | public static Bitmap getBitmapFromAsset(Context context, String filePath) { | 1019 | public static Bitmap getBitmapFromAsset(Context context, String filePath) { |
| 850 | AssetManager assetManager = context.getAssets(); | 1020 | AssetManager assetManager = context.getAssets(); |
| 851 | InputStream istr; | 1021 | InputStream istr; |
| 852 | Bitmap bitmap = null; | 1022 | Bitmap bitmap = null; |
| 853 | try { | 1023 | try { |
| 854 | istr = assetManager.open(filePath); | 1024 | istr = assetManager.open(filePath); |
| 855 | bitmap = BitmapFactory.decodeStream(istr); | 1025 | bitmap = BitmapFactory.decodeStream(istr); |
| 856 | } catch (IOException e) { | 1026 | } catch (IOException e) { |
| 857 | Debug.normal("error: %s", e.getMessage()); | 1027 | Debug.normal("error: %s", e.getMessage()); |
| 858 | } | 1028 | } |
| 859 | return bitmap; | 1029 | return bitmap; |
| 860 | } | 1030 | } |
| 861 | 1031 | ||
| 862 | public static String getResponseData(ResponseBody response) { | 1032 | public static String getResponseData(ResponseBody response) { |
| 863 | 1033 | ||
| 864 | String unzipString = ""; | 1034 | String unzipString = ""; |
| 865 | try { | 1035 | try { |
| 866 | unzipString = response.string(); | 1036 | unzipString = response.string(); |
| 867 | Debug.normal("RESPONSE: "+unzipString); | 1037 | Debug.normal("RESPONSE: "+unzipString); |
| 868 | } catch (IOException e) { | 1038 | } catch (IOException e) { |
| 869 | e.printStackTrace(); | 1039 | e.printStackTrace(); |
| 870 | } | 1040 | } |
| 871 | return unzipString; | 1041 | return unzipString; |
| 872 | } | 1042 | } |
| 873 | 1043 | ||
| 874 | } | 1044 | } |
| 875 | 1045 |
app/src/main/java/com/dinhcv/lifelogpedometer/utils/Utils.java
| 1 | package com.dinhcv.lifelogpedometer.utils; | 1 | package com.dinhcv.lifelogpedometer.utils; |
| 2 | 2 | ||
| 3 | import android.content.Context; | 3 | import android.content.Context; |
| 4 | import android.graphics.drawable.Drawable; | 4 | import android.graphics.drawable.Drawable; |
| 5 | import android.os.Build; | 5 | import android.os.Build; |
| 6 | 6 | ||
| 7 | import com.dinhcv.lifelogpedometer.LifeLogApplication; | 7 | import com.dinhcv.lifelogpedometer.LifeLogApplication; |
| 8 | 8 | ||
| 9 | import java.text.DateFormat; | 9 | import java.text.DateFormat; |
| 10 | import java.text.DecimalFormat; | 10 | import java.text.DecimalFormat; |
| 11 | import java.text.ParseException; | 11 | import java.text.ParseException; |
| 12 | import java.text.SimpleDateFormat; | 12 | import java.text.SimpleDateFormat; |
| 13 | import java.util.Calendar; | 13 | import java.util.Calendar; |
| 14 | import java.util.Date; | 14 | import java.util.Date; |
| 15 | import java.util.Locale; | 15 | import java.util.Locale; |
| 16 | import java.util.TimeZone; | 16 | import java.util.TimeZone; |
| 17 | import java.util.concurrent.TimeUnit; | 17 | import java.util.concurrent.TimeUnit; |
| 18 | import java.util.regex.Matcher; | 18 | import java.util.regex.Matcher; |
| 19 | import java.util.regex.Pattern; | 19 | import java.util.regex.Pattern; |
| 20 | 20 | ||
| 21 | 21 | ||
| 22 | public class Utils { | 22 | public class Utils { |
| 23 | private Utils() { | 23 | private Utils() { |
| 24 | 24 | ||
| 25 | } | 25 | } |
| 26 | 26 | ||
| 27 | /** | 27 | /** |
| 28 | * Get color wrapper | 28 | * Get color wrapper |
| 29 | * | 29 | * |
| 30 | * @param context | 30 | * @param context |
| 31 | * @param id | 31 | * @param id |
| 32 | * @return: | 32 | * @return: |
| 33 | */ | 33 | */ |
| 34 | public static int getColorWrapper(Context context, int id) { | 34 | public static int getColorWrapper(Context context, int id) { |
| 35 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | 35 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 36 | return context.getColor(id); | 36 | return context.getColor(id); |
| 37 | } else { | 37 | } else { |
| 38 | //noinspection deprecation | 38 | //noinspection deprecation |
| 39 | return context.getResources().getColor(id); | 39 | return context.getResources().getColor(id); |
| 40 | } | 40 | } |
| 41 | } | 41 | } |
| 42 | 42 | ||
| 43 | /** | 43 | /** |
| 44 | * Get drawable wrapper | 44 | * Get drawable wrapper |
| 45 | * | 45 | * |
| 46 | * @param context | 46 | * @param context |
| 47 | * @param id | 47 | * @param id |
| 48 | * @return: | 48 | * @return: |
| 49 | */ | 49 | */ |
| 50 | public static Drawable getDrawableWrapper(Context context, int id) { | 50 | public static Drawable getDrawableWrapper(Context context, int id) { |
| 51 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | 51 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 52 | return context.getDrawable(id); | 52 | return context.getDrawable(id); |
| 53 | } else { | 53 | } else { |
| 54 | //noinspection deprecation | 54 | //noinspection deprecation |
| 55 | return context.getResources().getDrawable(id); | 55 | return context.getResources().getDrawable(id); |
| 56 | } | 56 | } |
| 57 | } | 57 | } |
| 58 | 58 | ||
| 59 | /** | 59 | /** |
| 60 | * Convert value to string | 60 | * Convert value to string |
| 61 | * | 61 | * |
| 62 | * @param value | 62 | * @param value |
| 63 | * @return | 63 | * @return |
| 64 | */ | 64 | */ |
| 65 | public static String convert2String2Decimal(double value) { | 65 | public static String convert2String2Decimal(double value) { |
| 66 | String valueStr = null; | 66 | String valueStr = null; |
| 67 | DecimalFormat dFormat = new DecimalFormat("####,###,##0.00"); | 67 | DecimalFormat dFormat = new DecimalFormat("####,###,##0.00"); |
| 68 | valueStr = dFormat.format(value); | 68 | valueStr = dFormat.format(value); |
| 69 | return valueStr; | 69 | return valueStr; |
| 70 | } | 70 | } |
| 71 | 71 | ||
| 72 | /** | 72 | /** |
| 73 | * Convert value to string | 73 | * Convert value to string |
| 74 | * | 74 | * |
| 75 | * @param value | 75 | * @param value |
| 76 | * @return | 76 | * @return |
| 77 | */ | 77 | */ |
| 78 | public static String convert2StringAroundNum(double value) { | 78 | public static String convert2StringAroundNum(double value) { |
| 79 | String valueStr = null; | 79 | String valueStr = null; |
| 80 | DecimalFormat dFormat = new DecimalFormat("####,###,###"); | 80 | DecimalFormat dFormat = new DecimalFormat("####,###,###"); |
| 81 | valueStr = dFormat.format(value); | 81 | valueStr = dFormat.format(value); |
| 82 | return valueStr; | 82 | return valueStr; |
| 83 | } | 83 | } |
| 84 | 84 | ||
| 85 | public static String convertDateToStringDialogSelect(Date input) { | 85 | public static String convertDateToStringDialogSelect(Date input) { |
| 86 | //昭和yyyy年MM月dd日 | 86 | //昭和yyyy年MM月dd日 |
| 87 | SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日", Locale.JAPAN); | 87 | SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日", Locale.JAPAN); |
| 88 | format.setTimeZone(TimeZone.getTimeZone("GMT+07:00")); | 88 | format.setTimeZone(TimeZone.getTimeZone("GMT+07:00")); |
| 89 | return format.format(input); | 89 | return format.format(input); |
| 90 | } | 90 | } |
| 91 | 91 | ||
| 92 | /** | 92 | /** |
| 93 | * * Convert date to string with format date month year | 93 | * * Convert date to string with format date month year |
| 94 | * | 94 | * |
| 95 | * @param date date | 95 | * @param date date |
| 96 | * @return date string | 96 | * @return date string |
| 97 | */ | 97 | */ |
| 98 | public static String dateToStringFormatDayMonthYearJp(Date date) { | 98 | public static String dateToStringFormatDayMonthYearJp(Date date) { |
| 99 | String dateStr = null; | 99 | String dateStr = null; |
| 100 | if (date != null) { | 100 | if (date != null) { |
| 101 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日", Locale.JAPAN); | 101 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日", Locale.JAPAN); |
| 102 | dateStr = sdf.format(date); | 102 | dateStr = sdf.format(date); |
| 103 | } | 103 | } |
| 104 | return dateStr; | 104 | return dateStr; |
| 105 | } | 105 | } |
| 106 | 106 | ||
| 107 | /** | 107 | /** |
| 108 | * * Convert date to string with format date month year | 108 | * * Convert date to string with format date month year |
| 109 | * | 109 | * |
| 110 | * @param date date | 110 | * @param date date |
| 111 | * @return date string | 111 | * @return date string |
| 112 | */ | 112 | */ |
| 113 | public static String dateToStringFormatDayMonthYear(Date date) { | 113 | public static String dateToStringFormatDayMonthYear(Date date) { |
| 114 | String dateStr = null; | 114 | String dateStr = null; |
| 115 | if (date != null) { | 115 | if (date != null) { |
| 116 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.JAPAN); | 116 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.JAPAN); |
| 117 | dateStr = sdf.format(date); | 117 | dateStr = sdf.format(date); |
| 118 | } | 118 | } |
| 119 | return dateStr; | 119 | return dateStr; |
| 120 | } | 120 | } |
| 121 | 121 | ||
| 122 | /** | 122 | /** |
| 123 | * * Convert date to string with format date month year | 123 | * * Convert date to string with format date month year |
| 124 | * @param date date | 124 | * @param date date |
| 125 | * @return date string | 125 | * @return date string |
| 126 | */ | 126 | */ |
| 127 | public static String dateToStringFormatYearMonthDay(Date date) { | 127 | public static String dateToStringFormatYearMonthDay(Date date) { |
| 128 | String dateStr = null; | 128 | String dateStr = null; |
| 129 | if ( date != null ) { | 129 | if ( date != null ) { |
| 130 | SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd", Locale.JAPAN); | 130 | SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd", Locale.JAPAN); |
| 131 | dateStr = sdf.format(date); | 131 | dateStr = sdf.format(date); |
| 132 | } | 132 | } |
| 133 | Debug.normal("DATE format: "+dateStr); | 133 | Debug.normal("DATE format: "+dateStr); |
| 134 | return dateStr; | 134 | return dateStr; |
| 135 | } | 135 | } |
| 136 | 136 | ||
| 137 | public static long getDateDiff(Date date1, Date date2, TimeUnit timeUnit) { | 137 | public static long getDateDiff(Date date1, Date date2, TimeUnit timeUnit) { |
| 138 | long diffInMillies = date2.getTime() - date1.getTime(); | 138 | long diffInMillies = date2.getTime() - date1.getTime(); |
| 139 | return timeUnit.convert(diffInMillies, TimeUnit.MILLISECONDS); | 139 | return timeUnit.convert(diffInMillies, TimeUnit.MILLISECONDS); |
| 140 | } | 140 | } |
| 141 | 141 | ||
| 142 | public static boolean checkMailFormat(String text) { | 142 | public static boolean checkMailFormat(String text) { |
| 143 | String regex = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$"; | 143 | String regex = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$"; |
| 144 | Pattern pattern = Pattern.compile(regex); | 144 | Pattern pattern = Pattern.compile(regex); |
| 145 | Matcher matcher = pattern.matcher(text); | 145 | Matcher matcher = pattern.matcher(text); |
| 146 | if (matcher.matches()) { | 146 | if (matcher.matches()) { |
| 147 | Debug.normal("String is Full width character"); | 147 | Debug.normal("String is Full width character"); |
| 148 | return true; | 148 | return true; |
| 149 | } else { | 149 | } else { |
| 150 | Debug.normal("String is not Full width character"); | 150 | Debug.normal("String is not Full width character"); |
| 151 | return false; | 151 | return false; |
| 152 | } | 152 | } |
| 153 | 153 | ||
| 154 | } | 154 | } |
| 155 | 155 | ||
| 156 | public static long getToday() { | 156 | public static long getToday() { |
| 157 | Calendar c = Calendar.getInstance(); | 157 | Calendar c = Calendar.getInstance(); |
| 158 | c.setTimeInMillis(System.currentTimeMillis()); | 158 | c.setTimeInMillis(System.currentTimeMillis()); |
| 159 | c.set(Calendar.HOUR_OF_DAY, 0); | 159 | c.set(Calendar.HOUR_OF_DAY, 0); |
| 160 | c.set(Calendar.MINUTE, 0); | 160 | c.set(Calendar.MINUTE, 0); |
| 161 | c.set(Calendar.SECOND, 0); | 161 | c.set(Calendar.SECOND, 0); |
| 162 | c.set(Calendar.MILLISECOND, 0); | 162 | c.set(Calendar.MILLISECOND, 0); |
| 163 | return c.getTimeInMillis(); | 163 | return c.getTimeInMillis(); |
| 164 | } | 164 | } |
| 165 | 165 | ||
| 166 | /** | 166 | /** |
| 167 | * @return milliseconds since 1.1.1970 for tomorrow 0:00:01 local timezone | 167 | * @return milliseconds since 1.1.1970 for tomorrow 0:00:01 local timezone |
| 168 | */ | 168 | */ |
| 169 | public static long getTomorrow() { | 169 | public static long getTomorrow() { |
| 170 | Calendar c = Calendar.getInstance(); | 170 | Calendar c = Calendar.getInstance(); |
| 171 | c.setTimeInMillis(System.currentTimeMillis()); | 171 | c.setTimeInMillis(System.currentTimeMillis()); |
| 172 | c.set(Calendar.HOUR_OF_DAY, 0); | 172 | c.set(Calendar.HOUR_OF_DAY, 0); |
| 173 | c.set(Calendar.MINUTE, 0); | 173 | c.set(Calendar.MINUTE, 0); |
| 174 | c.set(Calendar.SECOND, 1); | 174 | c.set(Calendar.SECOND, 1); |
| 175 | c.set(Calendar.MILLISECOND, 0); | 175 | c.set(Calendar.MILLISECOND, 0); |
| 176 | c.add(Calendar.DATE, 1); | 176 | c.add(Calendar.DATE, 1); |
| 177 | return c.getTimeInMillis(); | 177 | return c.getTimeInMillis(); |
| 178 | } | 178 | } |
| 179 | 179 | ||
| 180 | public static long getStandarDate(Date date) { | 180 | public static long getStandarDate(Date date) { |
| 181 | Calendar c = Calendar.getInstance(); | 181 | Calendar c = Calendar.getInstance(); |
| 182 | c.setTime(date); | 182 | c.setTime(date); |
| 183 | c.set(Calendar.HOUR_OF_DAY, 0); | 183 | c.set(Calendar.HOUR_OF_DAY, 0); |
| 184 | c.set(Calendar.MINUTE, 0); | 184 | c.set(Calendar.MINUTE, 0); |
| 185 | c.set(Calendar.SECOND, 0); | 185 | c.set(Calendar.SECOND, 0); |
| 186 | c.set(Calendar.MILLISECOND, 0); | 186 | c.set(Calendar.MILLISECOND, 0); |
| 187 | return c.getTimeInMillis(); | 187 | return c.getTimeInMillis(); |
| 188 | } | 188 | } |
| 189 | 189 | ||
| 190 | 190 | ||
| 191 | public static Date getFromDate(Date date, Const.DATA_TYPE dataType ) { | 191 | public static Date getFromDate(Date date, Const.DATA_TYPE dataType ) { |
| 192 | Calendar c = Calendar.getInstance(); | 192 | Calendar c = Calendar.getInstance(); |
| 193 | c.setTime(date); | 193 | c.setTime(date); |
| 194 | if (dataType == Const.DATA_TYPE.ONE_DAY){ | 194 | if (dataType == Const.DATA_TYPE.ONE_DAY){ |
| 195 | c.add(Calendar.DAY_OF_YEAR, -1); | 195 | c.add(Calendar.DAY_OF_YEAR, -1); |
| 196 | }else if (dataType == Const.DATA_TYPE.ONE_WEEK){ | 196 | }else if (dataType == Const.DATA_TYPE.ONE_WEEK){ |
| 197 | c.add(Calendar.DAY_OF_YEAR, -7); | 197 | c.add(Calendar.DAY_OF_YEAR, -7); |
| 198 | }else if (dataType == Const.DATA_TYPE.ONE_MONTH){ | 198 | }else if (dataType == Const.DATA_TYPE.ONE_MONTH){ |
| 199 | c.add(Calendar.DAY_OF_YEAR, -30); | 199 | c.add(Calendar.DAY_OF_YEAR, -30); |
| 200 | }else if (dataType == Const.DATA_TYPE.THREE_MONTH){ | 200 | }else if (dataType == Const.DATA_TYPE.THREE_MONTH){ |
| 201 | c.add(Calendar.DAY_OF_YEAR, - 30 * 3); | 201 | c.add(Calendar.DAY_OF_YEAR, - 30 * 3); |
| 202 | }else if (dataType == Const.DATA_TYPE.SIX_MONTH){ | 202 | }else if (dataType == Const.DATA_TYPE.SIX_MONTH){ |
| 203 | c.add(Calendar.DAY_OF_YEAR, - 30 * 6); | 203 | c.add(Calendar.DAY_OF_YEAR, - 30 * 6); |
| 204 | } | 204 | } |
| 205 | return c.getTime(); | 205 | return c.getTime(); |
| 206 | } | 206 | } |
| 207 | 207 | ||
| 208 | public static int getMonth(Date date) { | 208 | public static int getMonth(Date date) { |
| 209 | 209 | ||
| 210 | int month = 0; | 210 | int month = 0; |
| 211 | if (date != null) { | 211 | if (date != null) { |
| 212 | SimpleDateFormat sdf = new SimpleDateFormat("MM", Locale.JAPAN); | 212 | SimpleDateFormat sdf = new SimpleDateFormat("MM", Locale.JAPAN); |
| 213 | String dateStr = sdf.format(date); | 213 | String dateStr = sdf.format(date); |
| 214 | if (dateStr != null) month = Integer.valueOf(dateStr); | 214 | if (dateStr != null) month = Integer.valueOf(dateStr); |
| 215 | } | 215 | } |
| 216 | Debug.normal("Month: " + month); | 216 | Debug.normal("Month: " + month); |
| 217 | return month; | 217 | return month; |
| 218 | } | 218 | } |
| 219 | 219 | ||
| 220 | public static int getDay(Date date) { | 220 | public static int getDay(Date date) { |
| 221 | int month = 0; | 221 | int month = 0; |
| 222 | if (date != null) { | 222 | if (date != null) { |
| 223 | SimpleDateFormat sdf = new SimpleDateFormat("dd", Locale.JAPAN); | 223 | SimpleDateFormat sdf = new SimpleDateFormat("dd", Locale.JAPAN); |
| 224 | String dateStr = sdf.format(date); | 224 | String dateStr = sdf.format(date); |
| 225 | if (dateStr != null) month = Integer.valueOf(dateStr); | 225 | if (dateStr != null) month = Integer.valueOf(dateStr); |
| 226 | } | 226 | } |
| 227 | Debug.normal("Month: " + month); | 227 | Debug.normal("Month: " + month); |
| 228 | return month; | 228 | return month; |
| 229 | } | 229 | } |
| 230 | 230 | ||
| 231 | public static String userAgent = null; | 231 | public static String userAgent = null; |
| 232 | 232 | ||
| 233 | public static void setCustomUA(String ua) { | 233 | public static void setCustomUA(String ua) { |
| 234 | userAgent = ua; | 234 | userAgent = ua; |
| 235 | } | 235 | } |
| 236 | 236 | ||
| 237 | public static String getCustomUA() { | 237 | public static String getCustomUA() { |
| 238 | Context context = LifeLogApplication.context; | 238 | Context context = LifeLogApplication.context; |
| 239 | String packageName = context.getPackageName(); //{2} | 239 | String packageName = context.getPackageName(); //{2} |
| 240 | String appVersion = "Android" + "." + "1.0.1"; //{3} | 240 | String appVersion = "Android" + "." + "1.0.1"; //{3} |
| 241 | String osVersion = Build.VERSION.RELEASE; //{4} | 241 | String osVersion = Build.VERSION.RELEASE; //{4} |
| 242 | String modelName = Build.MODEL; //{6} | 242 | String modelName = Build.MODEL; //{6} |
| 243 | String uaString = packageName + "/" + appVersion + "(Android " + osVersion + ";" + modelName + ")" + " " + userAgent; | 243 | String uaString = packageName + "/" + appVersion + "(Android " + osVersion + ";" + modelName + ")" + " " + userAgent; |
| 244 | return uaString; | 244 | return uaString; |
| 245 | } | 245 | } |
| 246 | 246 | ||
| 247 | public static Date convertString2Date(String time) { | 247 | public static Date convertString2Date(String time) { |
| 248 | DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | 248 | DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| 249 | Date date = null; | 249 | Date date = null; |
| 250 | try { | 250 | try { |
| 251 | date = df.parse(time); | 251 | date = df.parse(time); |
| 252 | } catch (ParseException e) { | 252 | } catch (ParseException e) { |
| 253 | e.printStackTrace(); | 253 | e.printStackTrace(); |
| 254 | } | 254 | } |
| 255 | return date; | 255 | return date; |
| 256 | } | 256 | } |
| 257 | 257 | ||
| 258 | public static String convertDate2DateTimeString(Date date) { | ||
| 259 | DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | ||
| 260 | return df.format(date); | ||
| 261 | } | ||
| 262 | |||
| 258 | public static String convertDate2DayString(Date date) { | 263 | public static String convertDate2DayString(Date date) { |
| 259 | DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); | 264 | DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); |
| 260 | return df.format(date); | 265 | return df.format(date); |
| 261 | } | 266 | } |
| 262 | 267 | ||
| 263 | public static String convertDate2TimeString(Date date) { | 268 | public static String convertDate2TimeString(Date date) { |
| 264 | DateFormat df = new SimpleDateFormat("HH:mm"); | 269 | DateFormat df = new SimpleDateFormat("HH:mm"); |
| 265 | return df.format(date); | 270 | return df.format(date); |
| 266 | } | 271 | } |
| 267 | 272 | ||
| 268 | 273 | ||
| 269 | public static String convertTimeStringFromString(int time){ | 274 | public static String convertTimeStringFromString(int time){ |
| 270 | String timeCv = null; | 275 | String timeCv = null; |
| 271 | String timeStr = String.valueOf(time); | 276 | String timeStr = String.valueOf(time); |
| 272 | int length = timeStr.length(); | 277 | int length = timeStr.length(); |
| 273 | switch (length){ | 278 | switch (length){ |
| 274 | case 0: | 279 | case 0: |
| 275 | timeCv = "00:00:00"; | 280 | timeCv = "00:00:00"; |
| 276 | break; | 281 | break; |
| 277 | case 1: | 282 | case 1: |
| 278 | timeCv = "00:00:0"+time; | 283 | timeCv = "00:00:0"+time; |
| 279 | break; | 284 | break; |
| 280 | case 2: | 285 | case 2: |
| 281 | timeCv = "00:00:"+time; | 286 | timeCv = "00:00:"+time; |
| 282 | break; | 287 | break; |
| 283 | case 3: | 288 | case 3: |
| 284 | timeCv = "00:0"+timeStr.substring(0, 1)+":"+ timeStr.substring(1, timeStr.length()); | 289 | timeCv = "00:0"+timeStr.substring(0, 1)+":"+ timeStr.substring(1, timeStr.length()); |
| 285 | break; | 290 | break; |
| 286 | case 4: | 291 | case 4: |
| 287 | timeCv = "00:"+timeStr.substring(0, 2)+":"+ timeStr.substring(2, timeStr.length()); | 292 | timeCv = "00:"+timeStr.substring(0, 2)+":"+ timeStr.substring(2, timeStr.length()); |
| 288 | break; | 293 | break; |
| 289 | case 5: | 294 | case 5: |
| 290 | timeCv = timeStr.substring(0, 1)+ ":"+timeStr.substring(1, 3)+":"+ timeStr.substring(3, timeStr.length()); | 295 | timeCv = timeStr.substring(0, 1)+ ":"+timeStr.substring(1, 3)+":"+ timeStr.substring(3, timeStr.length()); |
| 291 | break; | 296 | break; |
| 292 | case 6: | 297 | case 6: |
| 293 | timeCv = timeStr.substring(0, 2)+":"+timeStr.substring(2, 4)+":"+ timeStr.substring(4, timeStr.length()); | 298 | timeCv = timeStr.substring(0, 2)+":"+timeStr.substring(2, 4)+":"+ timeStr.substring(4, timeStr.length()); |
| 294 | break; | 299 | break; |
| 295 | default: | 300 | default: |
| 296 | timeCv = timeStr; | 301 | timeCv = timeStr; |
| 297 | break; | 302 | break; |
| 298 | } | 303 | } |
| 299 | 304 | ||
| 300 | return timeCv; | 305 | return timeCv; |
| 301 | } | 306 | } |
| 302 | 307 | ||
| 303 | public static String convertSecond2HourMinSecString(int total){ | 308 | public static String convertSecond2HourMinSecString(int total){ |
| 304 | int hours = total / 3600; | 309 | int hours = total / 3600; |
| 305 | int minutes = (total % 3600) / 60; | 310 | int minutes = (total % 3600) / 60; |
| 306 | int seconds = total % 60; | 311 | int seconds = total % 60; |
| 307 | 312 | ||
| 308 | return String.format("%02d:%02d:%02d", hours, minutes, seconds); | 313 | return String.format("%02d:%02d:%02d", hours, minutes, seconds); |
| 309 | } | 314 | } |
| 310 | 315 | ||
| 311 | public static String formatInt2LengthDefault(int value){ | 316 | public static String formatInt2LengthDefault(int value){ |
| 312 | DecimalFormat dFormat = new DecimalFormat("00"); | 317 | DecimalFormat dFormat = new DecimalFormat("00"); |
| 313 | String data = dFormat.format(value); | 318 | String data = dFormat.format(value); |
| 314 | return data; | 319 | return data; |
| 315 | } | 320 | } |
| 316 | 321 | ||
| 317 | public static String getWeekdayFromDate(Date date) { | 322 | public static String getWeekdayFromDate(Date date) { |
| 318 | SimpleDateFormat outFormat = new SimpleDateFormat("EEEE", Locale.JAPAN); | 323 | SimpleDateFormat outFormat = new SimpleDateFormat("EEEE", Locale.JAPAN); |
| 319 | String goal = outFormat.format(date); | 324 | String goal = outFormat.format(date); |
| 320 | Debug.normal("WEEKDAY: " + goal); | 325 | Debug.normal("WEEKDAY: " + goal); |
| 321 | return goal; | 326 | return goal; |
| 322 | } | 327 | } |
| 323 | public static int parseString2Int(String input, int defaul) { | 328 | public static int parseString2Int(String input, int defaul) { |
| 324 | try { | 329 | try { |
| 325 | return Integer.parseInt(input); | 330 | return Integer.parseInt(input); |
| 326 | } catch (NumberFormatException e) { | 331 | } catch (NumberFormatException e) { |
| 327 | return defaul; | 332 | return defaul; |
| 328 | } | 333 | } |
| 329 | 334 | ||
| 330 | } | 335 | } |
| 331 | } | 336 | } |
| 332 | /****************************************************************************** | 337 | /****************************************************************************** |
| 333 | * End of file | 338 | * End of file |
| 334 | *****************************************************************************/ | 339 | *****************************************************************************/ |