Commit 90cd21c2ba361d9d266b3351bc2eeefcf9038496
1 parent
9c431985f7
Exists in
master
update handle register account
Showing 3 changed files with 17 additions and 6 deletions Inline Diff
app/src/main/java/com/dinhcv/lifelogpedometer/activity/RegisterActivity.java
| 1 | package com.dinhcv.lifelogpedometer.activity; | 1 | package com.dinhcv.lifelogpedometer.activity; |
| 2 | 2 | ||
| 3 | import android.app.AlertDialog; | 3 | import android.app.AlertDialog; |
| 4 | import android.app.DatePickerDialog; | 4 | import android.app.DatePickerDialog; |
| 5 | import android.app.ProgressDialog; | 5 | import android.app.ProgressDialog; |
| 6 | import android.content.DialogInterface; | 6 | import android.content.DialogInterface; |
| 7 | import android.content.Intent; | 7 | import android.content.Intent; |
| 8 | import android.graphics.Bitmap; | 8 | import android.graphics.Bitmap; |
| 9 | import android.graphics.BitmapFactory; | 9 | import android.graphics.BitmapFactory; |
| 10 | import android.net.Uri; | 10 | import android.net.Uri; |
| 11 | import android.os.AsyncTask; | 11 | import android.os.AsyncTask; |
| 12 | import android.os.Bundle; | 12 | import android.os.Bundle; |
| 13 | import android.view.View; | 13 | import android.view.View; |
| 14 | import android.view.WindowManager; | 14 | import android.view.WindowManager; |
| 15 | import android.widget.DatePicker; | 15 | import android.widget.DatePicker; |
| 16 | import android.widget.EditText; | 16 | import android.widget.EditText; |
| 17 | import android.widget.LinearLayout; | 17 | import android.widget.LinearLayout; |
| 18 | import android.widget.TextView; | 18 | import android.widget.TextView; |
| 19 | import android.widget.Toast; | 19 | import android.widget.Toast; |
| 20 | 20 | ||
| 21 | import com.dinhcv.lifelogpedometer.LifeLogApplication; | 21 | import com.dinhcv.lifelogpedometer.LifeLogApplication; |
| 22 | import com.dinhcv.lifelogpedometer.R; | 22 | import com.dinhcv.lifelogpedometer.R; |
| 23 | import com.dinhcv.lifelogpedometer.customview.SimpleSelectBoxDialogBuilder; | 23 | import com.dinhcv.lifelogpedometer.customview.SimpleSelectBoxDialogBuilder; |
| 24 | import com.dinhcv.lifelogpedometer.interfaces.LLAPIManagerListener; | 24 | import com.dinhcv.lifelogpedometer.interfaces.LLAPIManagerListener; |
| 25 | import com.dinhcv.lifelogpedometer.interfaces.OnSelectResultListener; | 25 | import com.dinhcv.lifelogpedometer.interfaces.OnSelectResultListener; |
| 26 | import com.dinhcv.lifelogpedometer.model.Shareprefer.Setting; | 26 | import com.dinhcv.lifelogpedometer.model.Shareprefer.Setting; |
| 27 | import com.dinhcv.lifelogpedometer.model.structure.RegisterInfo; | 27 | import com.dinhcv.lifelogpedometer.model.structure.RegisterInfo; |
| 28 | import com.dinhcv.lifelogpedometer.model.structure.SelectItemInfo; | 28 | import com.dinhcv.lifelogpedometer.model.structure.SelectItemInfo; |
| 29 | import com.dinhcv.lifelogpedometer.portal.ApiServices; | 29 | import com.dinhcv.lifelogpedometer.portal.ApiServices; |
| 30 | import com.dinhcv.lifelogpedometer.portal.LLAPIManager; | 30 | import com.dinhcv.lifelogpedometer.portal.LLAPIManager; |
| 31 | import com.dinhcv.lifelogpedometer.utils.Debug; | 31 | import com.dinhcv.lifelogpedometer.utils.Debug; |
| 32 | import com.dinhcv.lifelogpedometer.utils.Utils; | 32 | import com.dinhcv.lifelogpedometer.utils.Utils; |
| 33 | 33 | ||
| 34 | import org.json.JSONArray; | 34 | import org.json.JSONArray; |
| 35 | import org.json.JSONObject; | 35 | import org.json.JSONObject; |
| 36 | 36 | ||
| 37 | import java.io.FileNotFoundException; | 37 | import java.io.FileNotFoundException; |
| 38 | import java.io.InputStream; | 38 | import java.io.InputStream; |
| 39 | import java.util.Calendar; | 39 | import java.util.Calendar; |
| 40 | import java.util.Date; | 40 | import java.util.Date; |
| 41 | import java.util.List; | 41 | import java.util.List; |
| 42 | 42 | ||
| 43 | import butterknife.BindView; | 43 | import butterknife.BindView; |
| 44 | import butterknife.ButterKnife; | 44 | import butterknife.ButterKnife; |
| 45 | import butterknife.OnClick; | 45 | import butterknife.OnClick; |
| 46 | import de.hdodenhof.circleimageview.CircleImageView; | 46 | import de.hdodenhof.circleimageview.CircleImageView; |
| 47 | 47 | ||
| 48 | public class RegisterActivity extends ActivityBase { | 48 | public class RegisterActivity extends ActivityBase { |
| 49 | 49 | ||
| 50 | @BindView(R.id.edt_user) | 50 | @BindView(R.id.edt_user) |
| 51 | EditText edtUsername; | 51 | EditText edtUsername; |
| 52 | @BindView(R.id.edt_name) | 52 | @BindView(R.id.edt_name) |
| 53 | EditText edtName; | 53 | EditText edtName; |
| 54 | @BindView(R.id.edt_nickName) | 54 | @BindView(R.id.edt_nickName) |
| 55 | EditText edtNickName; | 55 | EditText edtNickName; |
| 56 | @BindView(R.id.edt_email) | 56 | @BindView(R.id.edt_email) |
| 57 | EditText edtEmail; | 57 | EditText edtEmail; |
| 58 | @BindView(R.id.edt_password) | 58 | @BindView(R.id.edt_password) |
| 59 | EditText edtPassword; | 59 | EditText edtPassword; |
| 60 | @BindView(R.id.edt_confirmPass) | 60 | @BindView(R.id.edt_confirmPass) |
| 61 | EditText edtConfirmPass; | 61 | EditText edtConfirmPass; |
| 62 | @BindView(R.id.tv_sex) | 62 | @BindView(R.id.tv_sex) |
| 63 | TextView tvSex; | 63 | TextView tvSex; |
| 64 | @BindView(R.id.tv_birthday) | 64 | @BindView(R.id.tv_birthday) |
| 65 | TextView tvBirthday; | 65 | TextView tvBirthday; |
| 66 | @BindView(R.id.edt_height) | 66 | @BindView(R.id.edt_height) |
| 67 | EditText edtHeight; | 67 | EditText edtHeight; |
| 68 | @BindView(R.id.edt_weight) | 68 | @BindView(R.id.edt_weight) |
| 69 | EditText edtWeight; | 69 | EditText edtWeight; |
| 70 | @BindView(R.id.edt_bodyFat) | 70 | @BindView(R.id.edt_bodyFat) |
| 71 | EditText edtBodyFat; | 71 | EditText edtBodyFat; |
| 72 | @BindView(R.id.tv_address) | 72 | @BindView(R.id.tv_address) |
| 73 | TextView tvAddress; | 73 | TextView tvAddress; |
| 74 | @BindView(R.id.edt_comment) | 74 | @BindView(R.id.edt_comment) |
| 75 | EditText edtComment; | 75 | EditText edtComment; |
| 76 | 76 | ||
| 77 | @BindView(R.id.ll_low) | 77 | @BindView(R.id.ll_low) |
| 78 | LinearLayout llLow; | 78 | LinearLayout llLow; |
| 79 | @BindView(R.id.tv_low) | 79 | @BindView(R.id.tv_low) |
| 80 | TextView tvLow; | 80 | TextView tvLow; |
| 81 | @BindView(R.id.ll_mid) | 81 | @BindView(R.id.ll_mid) |
| 82 | LinearLayout llMid; | 82 | LinearLayout llMid; |
| 83 | @BindView(R.id.tv_mid) | 83 | @BindView(R.id.tv_mid) |
| 84 | TextView tvMid; | 84 | TextView tvMid; |
| 85 | @BindView(R.id.ll_hight) | 85 | @BindView(R.id.ll_hight) |
| 86 | LinearLayout llHight; | 86 | LinearLayout llHight; |
| 87 | @BindView(R.id.tv_hight) | 87 | @BindView(R.id.tv_hight) |
| 88 | TextView tvHight; | 88 | TextView tvHight; |
| 89 | @BindView(R.id.profile_image) | 89 | @BindView(R.id.profile_image) |
| 90 | CircleImageView profileImage; | 90 | CircleImageView profileImage; |
| 91 | 91 | ||
| 92 | private List<SelectItemInfo> sexList; | 92 | private List<SelectItemInfo> sexList; |
| 93 | private List<SelectItemInfo> provinceList; | 93 | private List<SelectItemInfo> provinceList; |
| 94 | private LifeLogApplication mLifeLogApplication; | 94 | private LifeLogApplication mLifeLogApplication; |
| 95 | private SelectItemInfo sexInfo; | 95 | private SelectItemInfo sexInfo; |
| 96 | private SelectItemInfo provinceInfo; | 96 | private SelectItemInfo provinceInfo; |
| 97 | 97 | ||
| 98 | private Calendar mCalendar; | 98 | private Calendar mCalendar; |
| 99 | private int mDay; | 99 | private int mDay; |
| 100 | private int mMonth; | 100 | private int mMonth; |
| 101 | private int mYear; | 101 | private int mYear; |
| 102 | private Date mBirthDay; | 102 | private Date mBirthDay; |
| 103 | 103 | ||
| 104 | private int level = 0; | 104 | private int level = 0; |
| 105 | private static final int RESULT_LOAD_IMG = 99; | 105 | private static final int RESULT_LOAD_IMG = 99; |
| 106 | private RegisterInfo mRegisterInfo; | 106 | private RegisterInfo mRegisterInfo; |
| 107 | private String profileImagePath; | 107 | private String profileImagePath; |
| 108 | private ProgressDialog progressDialog; | 108 | private ProgressDialog progressDialog; |
| 109 | 109 | ||
| 110 | @Override | 110 | @Override |
| 111 | protected void onCreate(Bundle savedInstanceState) { | 111 | protected void onCreate(Bundle savedInstanceState) { |
| 112 | super.onCreate(savedInstanceState); | 112 | super.onCreate(savedInstanceState); |
| 113 | setContentView(R.layout.activity_register); | 113 | setContentView(R.layout.activity_register); |
| 114 | getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); | 114 | getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); |
| 115 | 115 | ||
| 116 | ButterKnife.bind(this); | 116 | ButterKnife.bind(this); |
| 117 | mLifeLogApplication = (LifeLogApplication) getApplication(); | 117 | mLifeLogApplication = (LifeLogApplication) getApplication(); |
| 118 | 118 | ||
| 119 | initData(); | 119 | initData(); |
| 120 | 120 | ||
| 121 | initView(); | 121 | initView(); |
| 122 | 122 | ||
| 123 | } | 123 | } |
| 124 | 124 | ||
| 125 | private void initData() { | 125 | private void initData() { |
| 126 | sexList = mLifeLogApplication.getSexList(); | 126 | sexList = mLifeLogApplication.getSexList(); |
| 127 | provinceList = mLifeLogApplication.getProvinceList(); | 127 | provinceList = mLifeLogApplication.getProvinceList(); |
| 128 | 128 | ||
| 129 | mCalendar = Calendar.getInstance(); | 129 | mCalendar = Calendar.getInstance(); |
| 130 | mDay = mCalendar.get(Calendar.DAY_OF_MONTH); | 130 | mDay = mCalendar.get(Calendar.DAY_OF_MONTH); |
| 131 | mMonth = mCalendar.get(Calendar.MONTH); | 131 | mMonth = mCalendar.get(Calendar.MONTH); |
| 132 | mYear = mCalendar.get(Calendar.YEAR); | 132 | mYear = mCalendar.get(Calendar.YEAR); |
| 133 | } | 133 | } |
| 134 | 134 | ||
| 135 | 135 | ||
| 136 | private void initView() { | 136 | private void initView() { |
| 137 | 137 | ||
| 138 | 138 | ||
| 139 | } | 139 | } |
| 140 | 140 | ||
| 141 | private boolean checkData() { | 141 | private boolean checkData() { |
| 142 | String userName = edtUsername.getText().toString(); | 142 | String userName = edtUsername.getText().toString(); |
| 143 | String name = edtName.getText().toString(); | 143 | String name = edtName.getText().toString(); |
| 144 | String nickName = edtNickName.getText().toString(); | 144 | String nickName = edtNickName.getText().toString(); |
| 145 | String email = edtEmail.getText().toString(); | 145 | String email = edtEmail.getText().toString(); |
| 146 | String pass = edtPassword.getText().toString(); | 146 | String pass = edtPassword.getText().toString(); |
| 147 | String confirmPass = edtConfirmPass.getText().toString(); | 147 | String confirmPass = edtConfirmPass.getText().toString(); |
| 148 | //sexInfo | 148 | //sexInfo |
| 149 | //birthday | 149 | //birthday |
| 150 | String height = edtHeight.getText().toString(); | 150 | String height = edtHeight.getText().toString(); |
| 151 | String weight = edtWeight.getText().toString(); | 151 | String weight = edtWeight.getText().toString(); |
| 152 | String bodyFat = edtBodyFat.getText().toString(); | 152 | String bodyFat = edtBodyFat.getText().toString(); |
| 153 | // address | 153 | // address |
| 154 | String comment = edtComment.getText().toString(); | 154 | String comment = edtComment.getText().toString(); |
| 155 | 155 | ||
| 156 | 156 | ||
| 157 | if (userName.isEmpty()) { | 157 | if (userName.isEmpty()) { |
| 158 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.username_null)); | 158 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.username_null)); |
| 159 | return false; | 159 | return false; |
| 160 | } | 160 | } |
| 161 | 161 | ||
| 162 | if (name.isEmpty()) { | 162 | if (name.isEmpty()) { |
| 163 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.name_null)); | 163 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.name_null)); |
| 164 | return false; | 164 | return false; |
| 165 | } | 165 | } |
| 166 | 166 | ||
| 167 | if (nickName.isEmpty()) { | 167 | if (nickName.isEmpty()) { |
| 168 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.nickname_null)); | 168 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.nickname_null)); |
| 169 | return false; | 169 | return false; |
| 170 | } | 170 | } |
| 171 | 171 | ||
| 172 | if (email.isEmpty()) { | 172 | if (email.isEmpty()) { |
| 173 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.mail_null)); | 173 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.mail_null)); |
| 174 | return false; | 174 | return false; |
| 175 | } else { | 175 | } else { |
| 176 | if (!Utils.checkMailFormat(email)) { | 176 | if (!Utils.checkMailFormat(email)) { |
| 177 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.mail_invalid)); | 177 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.mail_invalid)); |
| 178 | return false; | 178 | return false; |
| 179 | } | 179 | } |
| 180 | } | 180 | } |
| 181 | 181 | ||
| 182 | if (pass.isEmpty()) { | 182 | if (pass.isEmpty()) { |
| 183 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.password_null)); | 183 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.password_null)); |
| 184 | return false; | 184 | return false; |
| 185 | }else { | 185 | }else { |
| 186 | if (pass.length() < 6) { | 186 | if (pass.length() < 6) { |
| 187 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.password_less_6)); | 187 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.password_less_6)); |
| 188 | return false; | 188 | return false; |
| 189 | } | 189 | } |
| 190 | } | 190 | } |
| 191 | 191 | ||
| 192 | if (confirmPass.isEmpty()) { | 192 | if (confirmPass.isEmpty()) { |
| 193 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.confirm_pass_null)); | 193 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.confirm_pass_null)); |
| 194 | return false; | 194 | return false; |
| 195 | } | 195 | } |
| 196 | 196 | ||
| 197 | if (!pass.equals(confirmPass)) { | 197 | if (!pass.equals(confirmPass)) { |
| 198 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.confirm_pass_wrong)); | 198 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.confirm_pass_wrong)); |
| 199 | return false; | 199 | return false; |
| 200 | } | 200 | } |
| 201 | 201 | ||
| 202 | if (sexInfo == null) { | 202 | if (sexInfo == null) { |
| 203 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.sex_not_select)); | 203 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.sex_not_select)); |
| 204 | return false; | 204 | return false; |
| 205 | } | 205 | } |
| 206 | 206 | ||
| 207 | if (mBirthDay == null) { | 207 | if (mBirthDay == null) { |
| 208 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.birthday_not_select)); | 208 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.birthday_not_select)); |
| 209 | return false; | 209 | return false; |
| 210 | } | 210 | } |
| 211 | 211 | ||
| 212 | if (height.isEmpty()) { | 212 | if (height.isEmpty()) { |
| 213 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.height_null)); | 213 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.height_null)); |
| 214 | return false; | 214 | return false; |
| 215 | } | 215 | } |
| 216 | 216 | ||
| 217 | if (weight.isEmpty()) { | 217 | if (weight.isEmpty()) { |
| 218 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.weight_null)); | 218 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.weight_null)); |
| 219 | return false; | 219 | return false; |
| 220 | } | 220 | } |
| 221 | 221 | ||
| 222 | if (bodyFat.isEmpty()) { | 222 | if (bodyFat.isEmpty()) { |
| 223 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.body_fat_percent_null)); | 223 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.body_fat_percent_null)); |
| 224 | return false; | 224 | return false; |
| 225 | } | 225 | } |
| 226 | 226 | ||
| 227 | if (provinceInfo == null) { | 227 | if (provinceInfo == null) { |
| 228 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.province_not_select)); | 228 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.province_not_select)); |
| 229 | return false; | 229 | return false; |
| 230 | } | 230 | } |
| 231 | 231 | ||
| 232 | if (level == 0) { | 232 | if (level == 0) { |
| 233 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.level_not_select)); | 233 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.level_not_select)); |
| 234 | return false; | 234 | return false; |
| 235 | } | 235 | } |
| 236 | 236 | ||
| 237 | if (comment.isEmpty()) { | 237 | if (comment.isEmpty()) { |
| 238 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.comment_null)); | 238 | showAlerDialog(RegisterActivity.this, getResources().getString(R.string.comment_null)); |
| 239 | return false; | 239 | return false; |
| 240 | } | 240 | } |
| 241 | 241 | ||
| 242 | mRegisterInfo = new RegisterInfo(); | 242 | mRegisterInfo = new RegisterInfo(); |
| 243 | mRegisterInfo.setUsername(userName); | 243 | mRegisterInfo.setUsername(userName); |
| 244 | mRegisterInfo.setFullName(name); | 244 | mRegisterInfo.setFullName(name); |
| 245 | mRegisterInfo.setNickname(nickName); | 245 | mRegisterInfo.setNickname(nickName); |
| 246 | mRegisterInfo.setPassword(pass); | 246 | mRegisterInfo.setPassword(pass); |
| 247 | mRegisterInfo.setBirthday(mBirthDay); | 247 | mRegisterInfo.setBirthday(mBirthDay); |
| 248 | mRegisterInfo.setHeight(Integer.valueOf(height)); | 248 | mRegisterInfo.setHeight(Integer.valueOf(height)); |
| 249 | mRegisterInfo.setWeight(Integer.valueOf(weight)); | 249 | mRegisterInfo.setWeight(Integer.valueOf(weight)); |
| 250 | mRegisterInfo.setFatRate(Integer.valueOf(bodyFat)); | 250 | mRegisterInfo.setFatRate(Integer.valueOf(bodyFat)); |
| 251 | mRegisterInfo.setGender(sexInfo.getId()); | 251 | mRegisterInfo.setGender(sexInfo.getId()); |
| 252 | mRegisterInfo.setAddress(provinceInfo.getName()); | 252 | mRegisterInfo.setAddress(provinceInfo.getName()); |
| 253 | mRegisterInfo.setEmail(email); | 253 | mRegisterInfo.setEmail(email); |
| 254 | mRegisterInfo.setProfileImage(profileImagePath); | 254 | mRegisterInfo.setProfileImage(profileImagePath); |
| 255 | mRegisterInfo.setDescription(comment); | ||
| 255 | 256 | ||
| 256 | return true; | 257 | return true; |
| 257 | } | 258 | } |
| 258 | 259 | ||
| 259 | private void handleRegister() { | 260 | private void handleRegister() { |
| 260 | progressDialog = new ProgressDialog(RegisterActivity.this); | 261 | progressDialog = new ProgressDialog(RegisterActivity.this); |
| 261 | progressDialog.setMessage(getResources().getString(R.string.uploading)); | 262 | progressDialog.setMessage(getResources().getString(R.string.uploading)); |
| 262 | progressDialog.setCancelable(false); | 263 | progressDialog.setCancelable(false); |
| 263 | progressDialog.show(); | 264 | progressDialog.show(); |
| 264 | 265 | ||
| 265 | ApiServices.register(mRegisterInfo, new LLAPIManagerListener() { | 266 | ApiServices.register(mRegisterInfo, new LLAPIManagerListener() { |
| 266 | @Override | 267 | @Override |
| 267 | public void onError(Error error) { | 268 | public void onError(Error error) { |
| 268 | progressDialog.dismiss(); | 269 | progressDialog.dismiss(); |
| 269 | Debug.error(" Can not upload data"); | 270 | Debug.error(" Can not upload data"); |
| 270 | if (error != null) { | 271 | if (error != null) { |
| 271 | String err = getResources().getString(R.string.register_err); | 272 | String err = getResources().getString(R.string.register_err); |
| 272 | notifyErr(err); | 273 | notifyErr(err); |
| 273 | } | 274 | } |
| 274 | } | 275 | } |
| 275 | 276 | ||
| 276 | @Override | 277 | @Override |
| 277 | public void onSuccess(String json) { | 278 | public void onSuccess(String json) { |
| 278 | progressDialog.dismiss(); | 279 | progressDialog.dismiss(); |
| 279 | if (json != null) { | 280 | if (json != null) { |
| 280 | Debug.warn(" Upload data success success"); | 281 | Debug.warn(" Upload data success success"); |
| 281 | Debug.warn("DATA JSON result: " + json.toString()); | 282 | Debug.warn("DATA JSON result: " + json.toString()); |
| 282 | registerDone(); | 283 | registerDone(); |
| 283 | } else { | 284 | } else { |
| 284 | Debug.warn(" Upload data fail: response null"); | 285 | Debug.warn(" Upload data fail: response null"); |
| 285 | String err = getResources().getString(R.string.err_exception); | 286 | String err = getResources().getString(R.string.err_exception); |
| 286 | notifyErr(err); | 287 | notifyErr(err); |
| 287 | } | 288 | } |
| 288 | } | 289 | } |
| 289 | 290 | ||
| 290 | @Override | 291 | @Override |
| 291 | public void onSuccess(JSONObject object) { | 292 | public void onSuccess(JSONObject object) { |
| 292 | Debug.warn("Version JSON object result: Success"); | 293 | Debug.warn("Version JSON object result: Success"); |
| 293 | progressDialog.dismiss(); | 294 | progressDialog.dismiss(); |
| 294 | 295 | ||
| 295 | String email = object.optString("email"); | 296 | String email = object.optString("email"); |
| 296 | String username = object.optString("username"); | 297 | String username = object.optString("username"); |
| 297 | String pass = object.optString("password"); | 298 | String pass = object.optString("password"); |
| 298 | Debug.normal("Email: "+email +"\n"+ "Username: "+username); | 299 | Debug.normal("Email: "+email +"\n"+ "Username: "+username); |
| 299 | // save data | 300 | // save data |
| 300 | Setting.setUserDataSharepre(RegisterActivity.this, Setting.EMAIL_SHAREPRE, email); | 301 | Setting.setUserDataSharepre(RegisterActivity.this, Setting.EMAIL_SHAREPRE, email); |
| 301 | Setting.setUserDataSharepre(RegisterActivity.this, Setting.USER_SHAREPRE, username); | 302 | Setting.setUserDataSharepre(RegisterActivity.this, Setting.USER_SHAREPRE, username); |
| 302 | Setting.setUserDataSharepre(RegisterActivity.this, Setting.PASS_SHAREPRE, pass); | 303 | Setting.setUserDataSharepre(RegisterActivity.this, Setting.PASS_SHAREPRE, pass); |
| 303 | registerDone(); | 304 | registerDone(); |
| 304 | } | 305 | } |
| 305 | }); | 306 | }); |
| 306 | } | 307 | } |
| 307 | 308 | ||
| 308 | private void hanleGetImagePath(Bitmap bitmap){ | 309 | private void hanleGetImagePath(Bitmap bitmap){ |
| 309 | progressDialog = new ProgressDialog(RegisterActivity.this); | 310 | progressDialog = new ProgressDialog(RegisterActivity.this); |
| 310 | progressDialog.setMessage(getResources().getString(R.string.uploading)); | 311 | progressDialog.setMessage(getResources().getString(R.string.uploading)); |
| 311 | progressDialog.setCancelable(false); | 312 | progressDialog.setCancelable(false); |
| 312 | progressDialog.show(); | 313 | progressDialog.show(); |
| 313 | 314 | ||
| 314 | ApiServices.uploadImage(bitmap, new LLAPIManagerListener() { | 315 | ApiServices.uploadImage(bitmap, new LLAPIManagerListener() { |
| 315 | @Override | 316 | @Override |
| 316 | public void onError(Error error) { | 317 | public void onError(Error error) { |
| 317 | progressDialog.dismiss(); | 318 | progressDialog.dismiss(); |
| 318 | Debug.error(" Can not upload data"); | 319 | Debug.error(" Can not upload data"); |
| 319 | if (error != null) { | 320 | if (error != null) { |
| 320 | String err = getResources().getString(R.string.register_err); | 321 | String err = getResources().getString(R.string.register_err); |
| 321 | notifyErr(err); | 322 | notifyErr(err); |
| 322 | } | 323 | } |
| 323 | } | 324 | } |
| 324 | 325 | ||
| 325 | @Override | 326 | @Override |
| 326 | public void onSuccess(String urlImage) { | 327 | public void onSuccess(String urlImage) { |
| 327 | if (urlImage != null) { | 328 | if (urlImage != null) { |
| 328 | progressDialog.dismiss(); | 329 | progressDialog.dismiss(); |
| 329 | Debug.warn(" Upload data success success"); | 330 | Debug.warn(" Upload data success success"); |
| 330 | Debug.warn("DATA JSON result: " + urlImage); | 331 | Debug.warn("DATA JSON result: " + urlImage); |
| 331 | uploadImageDone(urlImage); | 332 | uploadImageDone(urlImage); |
| 332 | } else { | 333 | } else { |
| 333 | progressDialog.dismiss(); | 334 | progressDialog.dismiss(); |
| 334 | Debug.warn(" Upload data fail: response null"); | 335 | Debug.warn(" Upload data fail: response null"); |
| 335 | String err = getResources().getString(R.string.err_exception); | 336 | String err = getResources().getString(R.string.err_exception); |
| 336 | notifyErr(err); | 337 | notifyErr(err); |
| 337 | } | 338 | } |
| 338 | } | 339 | } |
| 339 | 340 | ||
| 340 | @Override | 341 | @Override |
| 341 | public void onSuccess(JSONObject array) { | 342 | public void onSuccess(JSONObject array) { |
| 342 | progressDialog.dismiss(); | 343 | progressDialog.dismiss(); |
| 343 | Debug.warn(" Upload data success array"); | 344 | Debug.warn(" Upload data success array"); |
| 344 | } | 345 | } |
| 345 | }); | 346 | }); |
| 346 | } | 347 | } |
| 347 | 348 | ||
| 348 | private void registerDone(){ | 349 | private void registerDone(){ |
| 349 | Toast.makeText(RegisterActivity.this, R.string.register_success, Toast.LENGTH_SHORT).show(); | 350 | Toast.makeText(RegisterActivity.this, R.string.register_success, Toast.LENGTH_SHORT).show(); |
| 350 | finish(); | 351 | finish(); |
| 351 | } | 352 | } |
| 352 | 353 | ||
| 353 | private void uploadImageDone(String path){ | 354 | private void uploadImageDone(String path){ |
| 354 | profileImagePath = path; | 355 | profileImagePath = path; |
| 355 | } | 356 | } |
| 356 | 357 | ||
| 357 | private void notifyErr(String err) { | 358 | private void notifyErr(String err) { |
| 358 | AlertDialog.Builder alertDialog = new AlertDialog.Builder(RegisterActivity.this); | 359 | AlertDialog.Builder alertDialog = new AlertDialog.Builder(RegisterActivity.this); |
| 359 | alertDialog.setMessage(err); | 360 | alertDialog.setMessage(err); |
| 360 | alertDialog.setCancelable(false); | 361 | alertDialog.setCancelable(false); |
| 361 | alertDialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { | 362 | alertDialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { |
| 362 | @Override | 363 | @Override |
| 363 | public void onClick(DialogInterface dialog, int which) { | 364 | public void onClick(DialogInterface dialog, int which) { |
| 364 | finish(); | 365 | finish(); |
| 365 | } | 366 | } |
| 366 | }); | 367 | }); |
| 367 | alertDialog.show(); | 368 | alertDialog.show(); |
| 368 | } | 369 | } |
| 369 | 370 | ||
| 370 | private void doSelectSex() { | 371 | private void doSelectSex() { |
| 371 | String title = getResources().getString(R.string.sex); | 372 | String title = getResources().getString(R.string.sex); |
| 372 | new SimpleSelectBoxDialogBuilder().create(RegisterActivity.this, title, sexList, new OnSelectResultListener() { | 373 | new SimpleSelectBoxDialogBuilder().create(RegisterActivity.this, title, sexList, new OnSelectResultListener() { |
| 373 | @Override | 374 | @Override |
| 374 | public void onSelectedItem(SelectItemInfo selectItemInfo) { | 375 | public void onSelectedItem(SelectItemInfo selectItemInfo) { |
| 375 | if (selectItemInfo != null) { | 376 | if (selectItemInfo != null) { |
| 376 | Debug.normal("Item selected: " + selectItemInfo.getName()); | 377 | Debug.normal("Item selected: " + selectItemInfo.getName()); |
| 377 | sexInfo = selectItemInfo; | 378 | sexInfo = selectItemInfo; |
| 378 | tvSex.setText(sexInfo.getName()); | 379 | tvSex.setText(sexInfo.getName()); |
| 379 | } else { | 380 | } else { |
| 380 | Debug.normal("Have not item selected"); | 381 | Debug.normal("Have not item selected"); |
| 381 | } | 382 | } |
| 382 | } | 383 | } |
| 383 | }).show(); | 384 | }).show(); |
| 384 | } | 385 | } |
| 385 | 386 | ||
| 386 | private void doSelectProvince() { | 387 | private void doSelectProvince() { |
| 387 | String title = getResources().getString(R.string.prefecture); | 388 | String title = getResources().getString(R.string.prefecture); |
| 388 | new SimpleSelectBoxDialogBuilder().create(RegisterActivity.this, title, provinceList, new OnSelectResultListener() { | 389 | new SimpleSelectBoxDialogBuilder().create(RegisterActivity.this, title, provinceList, new OnSelectResultListener() { |
| 389 | @Override | 390 | @Override |
| 390 | public void onSelectedItem(SelectItemInfo selectItemInfo) { | 391 | public void onSelectedItem(SelectItemInfo selectItemInfo) { |
| 391 | if (selectItemInfo != null) { | 392 | if (selectItemInfo != null) { |
| 392 | Debug.normal("Item selected: " + selectItemInfo.getName()); | 393 | Debug.normal("Item selected: " + selectItemInfo.getName()); |
| 393 | provinceInfo = selectItemInfo; | 394 | provinceInfo = selectItemInfo; |
| 394 | tvAddress.setText(provinceInfo.getName()); | 395 | tvAddress.setText(provinceInfo.getName()); |
| 395 | } else { | 396 | } else { |
| 396 | Debug.normal("Have not item selected"); | 397 | Debug.normal("Have not item selected"); |
| 397 | } | 398 | } |
| 398 | } | 399 | } |
| 399 | }).show(); | 400 | }).show(); |
| 400 | } | 401 | } |
| 401 | 402 | ||
| 402 | private void hanleSelectDate() { | 403 | private void hanleSelectDate() { |
| 403 | 404 | ||
| 404 | DatePickerDialog datePicker = new DatePickerDialog(RegisterActivity.this, new DatePickerDialog.OnDateSetListener() { | 405 | DatePickerDialog datePicker = new DatePickerDialog(RegisterActivity.this, new DatePickerDialog.OnDateSetListener() { |
| 405 | @Override | 406 | @Override |
| 406 | public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { | 407 | public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { |
| 407 | 408 | ||
| 408 | mDay = dayOfMonth; | 409 | mDay = dayOfMonth; |
| 409 | mMonth = month; | 410 | mMonth = month; |
| 410 | mYear = year; | 411 | mYear = year; |
| 411 | mCalendar = Calendar.getInstance(); | 412 | mCalendar = Calendar.getInstance(); |
| 412 | mCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth); | 413 | mCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth); |
| 413 | mCalendar.set(Calendar.MONTH, month); | 414 | mCalendar.set(Calendar.MONTH, month); |
| 414 | mCalendar.set(Calendar.YEAR, year); | 415 | mCalendar.set(Calendar.YEAR, year); |
| 415 | mBirthDay = mCalendar.getTime(); | 416 | mBirthDay = mCalendar.getTime(); |
| 416 | 417 | ||
| 417 | String dateStr = Utils.convertDateToStringDialogSelect(mBirthDay); | 418 | String dateStr = Utils.convertDateToStringDialogSelect(mBirthDay); |
| 418 | tvBirthday.setText(dateStr); | 419 | tvBirthday.setText(dateStr); |
| 419 | } | 420 | } |
| 420 | }, mYear, mMonth, mDay); | 421 | }, mYear, mMonth, mDay); |
| 421 | datePicker.getDatePicker().setMaxDate(new Date().getTime()); | 422 | datePicker.getDatePicker().setMaxDate(new Date().getTime()); |
| 422 | datePicker.show(); | 423 | datePicker.show(); |
| 423 | } | 424 | } |
| 424 | 425 | ||
| 425 | private void updateUILevel() { | 426 | private void updateUILevel() { |
| 426 | if (level == 1) { | 427 | if (level == 1) { |
| 427 | setLowButtonSelected(true); | 428 | setLowButtonSelected(true); |
| 428 | setMidButtonSelected(false); | 429 | setMidButtonSelected(false); |
| 429 | setHightButtonSelected(false); | 430 | setHightButtonSelected(false); |
| 430 | } else if (level == 2) { | 431 | } else if (level == 2) { |
| 431 | setLowButtonSelected(false); | 432 | setLowButtonSelected(false); |
| 432 | setMidButtonSelected(true); | 433 | setMidButtonSelected(true); |
| 433 | setHightButtonSelected(false); | 434 | setHightButtonSelected(false); |
| 434 | } else if (level == 3) { | 435 | } else if (level == 3) { |
| 435 | setLowButtonSelected(false); | 436 | setLowButtonSelected(false); |
| 436 | setMidButtonSelected(false); | 437 | setMidButtonSelected(false); |
| 437 | setHightButtonSelected(true); | 438 | setHightButtonSelected(true); |
| 438 | } else { | 439 | } else { |
| 439 | setLowButtonSelected(false); | 440 | setLowButtonSelected(false); |
| 440 | setMidButtonSelected(false); | 441 | setMidButtonSelected(false); |
| 441 | setHightButtonSelected(false); | 442 | setHightButtonSelected(false); |
| 442 | } | 443 | } |
| 443 | } | 444 | } |
| 444 | 445 | ||
| 445 | private void setLowButtonSelected(boolean b) { | 446 | private void setLowButtonSelected(boolean b) { |
| 446 | if (b) { | 447 | if (b) { |
| 447 | llLow.setBackgroundResource(R.drawable.left_selected_lev_bg); | 448 | llLow.setBackgroundResource(R.drawable.left_selected_lev_bg); |
| 448 | tvLow.setTextColor(getResources().getColor(R.color.white)); | 449 | tvLow.setTextColor(getResources().getColor(R.color.white)); |
| 449 | } else { | 450 | } else { |
| 450 | llLow.setBackgroundResource(R.drawable.left_unselected_lev_bg); | 451 | llLow.setBackgroundResource(R.drawable.left_unselected_lev_bg); |
| 451 | tvLow.setTextColor(getResources().getColor(R.color.black)); | 452 | tvLow.setTextColor(getResources().getColor(R.color.black)); |
| 452 | } | 453 | } |
| 453 | } | 454 | } |
| 454 | 455 | ||
| 455 | private void setMidButtonSelected(boolean b) { | 456 | private void setMidButtonSelected(boolean b) { |
| 456 | if (b) { | 457 | if (b) { |
| 457 | llMid.setBackgroundResource(R.drawable.mid_selected_lev_bg); | 458 | llMid.setBackgroundResource(R.drawable.mid_selected_lev_bg); |
| 458 | tvMid.setTextColor(getResources().getColor(R.color.white)); | 459 | tvMid.setTextColor(getResources().getColor(R.color.white)); |
| 459 | } else { | 460 | } else { |
| 460 | llMid.setBackgroundResource(R.drawable.mid_unselected_lev_bg); | 461 | llMid.setBackgroundResource(R.drawable.mid_unselected_lev_bg); |
| 461 | tvMid.setTextColor(getResources().getColor(R.color.black)); | 462 | tvMid.setTextColor(getResources().getColor(R.color.black)); |
| 462 | } | 463 | } |
| 463 | } | 464 | } |
| 464 | 465 | ||
| 465 | private void setHightButtonSelected(boolean b) { | 466 | private void setHightButtonSelected(boolean b) { |
| 466 | if (b) { | 467 | if (b) { |
| 467 | llHight.setBackgroundResource(R.drawable.right_selected_lev_bg); | 468 | llHight.setBackgroundResource(R.drawable.right_selected_lev_bg); |
| 468 | tvHight.setTextColor(getResources().getColor(R.color.white)); | 469 | tvHight.setTextColor(getResources().getColor(R.color.white)); |
| 469 | } else { | 470 | } else { |
| 470 | llHight.setBackgroundResource(R.drawable.right_unselected_lev_bg); | 471 | llHight.setBackgroundResource(R.drawable.right_unselected_lev_bg); |
| 471 | tvHight.setTextColor(getResources().getColor(R.color.black)); | 472 | tvHight.setTextColor(getResources().getColor(R.color.black)); |
| 472 | } | 473 | } |
| 473 | } | 474 | } |
| 474 | 475 | ||
| 475 | private void getImageBrower() { | 476 | private void getImageBrower() { |
| 476 | Intent intent = new Intent(Intent.ACTION_PICK); | 477 | Intent intent = new Intent(Intent.ACTION_PICK); |
| 477 | intent.setType("image/*"); | 478 | intent.setType("image/*"); |
| 478 | startActivityForResult(intent, RESULT_LOAD_IMG); | 479 | startActivityForResult(intent, RESULT_LOAD_IMG); |
| 479 | } | 480 | } |
| 480 | 481 | ||
| 481 | 482 | ||
| 482 | @OnClick({R.id.btn_register, R.id.rl_sex, R.id.rl_birthday, R.id.rl_address, R.id.ll_low, | 483 | @OnClick({R.id.btn_register, R.id.rl_sex, R.id.rl_birthday, R.id.rl_address, R.id.ll_low, |
| 483 | R.id.ll_mid, R.id.ll_hight, R.id.profile_image}) | 484 | R.id.ll_mid, R.id.ll_hight, R.id.profile_image}) |
| 484 | public void onClick(View v) { | 485 | public void onClick(View v) { |
| 485 | switch (v.getId()) { | 486 | switch (v.getId()) { |
| 486 | case R.id.btn_register: | 487 | case R.id.btn_register: |
| 487 | if (checkData()) { | 488 | if (checkData()) { |
| 488 | handleRegister(); | 489 | handleRegister(); |
| 489 | } | 490 | } |
| 490 | break; | 491 | break; |
| 491 | 492 | ||
| 492 | case R.id.rl_sex: | 493 | case R.id.rl_sex: |
| 493 | doSelectSex(); | 494 | doSelectSex(); |
| 494 | break; | 495 | break; |
| 495 | 496 | ||
| 496 | 497 | ||
| 497 | case R.id.rl_birthday: | 498 | case R.id.rl_birthday: |
| 498 | hanleSelectDate(); | 499 | hanleSelectDate(); |
| 499 | break; | 500 | break; |
| 500 | 501 | ||
| 501 | case R.id.rl_address: | 502 | case R.id.rl_address: |
| 502 | doSelectProvince(); | 503 | doSelectProvince(); |
| 503 | break; | 504 | break; |
| 504 | 505 | ||
| 505 | case R.id.ll_low: | 506 | case R.id.ll_low: |
| 506 | level = 1; | 507 | level = 1; |
| 507 | updateUILevel(); | 508 | updateUILevel(); |
| 508 | break; | 509 | break; |
| 509 | case R.id.ll_mid: | 510 | case R.id.ll_mid: |
| 510 | level = 2; | 511 | level = 2; |
| 511 | updateUILevel(); | 512 | updateUILevel(); |
| 512 | break; | 513 | break; |
| 513 | case R.id.ll_hight: | 514 | case R.id.ll_hight: |
| 514 | level = 3; | 515 | level = 3; |
| 515 | updateUILevel(); | 516 | updateUILevel(); |
| 516 | break; | 517 | break; |
| 517 | case R.id.profile_image: | 518 | case R.id.profile_image: |
| 518 | getImageBrower(); | 519 | getImageBrower(); |
| 519 | break; | 520 | break; |
| 520 | 521 | ||
| 521 | default: | 522 | default: |
| 522 | break; | 523 | break; |
| 523 | } | 524 | } |
| 524 | } | 525 | } |
| 525 | 526 | ||
| 526 | @Override | 527 | @Override |
| 527 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { | 528 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 528 | super.onActivityResult(requestCode, resultCode, data); | 529 | super.onActivityResult(requestCode, resultCode, data); |
| 529 | 530 | ||
| 530 | if (resultCode == RESULT_OK) { | 531 | if (resultCode == RESULT_OK) { |
| 531 | if (requestCode == RESULT_LOAD_IMG) { | 532 | if (requestCode == RESULT_LOAD_IMG) { |
| 532 | try { | 533 | try { |
| 533 | final Uri imageUri = data.getData(); | 534 | final Uri imageUri = data.getData(); |
| 534 | final InputStream imageStream = getContentResolver().openInputStream(imageUri); | 535 | final InputStream imageStream = getContentResolver().openInputStream(imageUri); |
| 535 | final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream); | 536 | final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream); |
| 536 | hanleGetImagePath(selectedImage); | 537 | hanleGetImagePath(selectedImage); |
| 537 | profileImage.setImageBitmap(selectedImage); | 538 | profileImage.setImageBitmap(selectedImage); |
| 538 | } catch (FileNotFoundException e) { | 539 | } catch (FileNotFoundException e) { |
| 539 | e.printStackTrace(); | 540 | e.printStackTrace(); |
| 540 | Toast.makeText(RegisterActivity.this, "Something went wrong", Toast.LENGTH_LONG).show(); | 541 | Toast.makeText(RegisterActivity.this, "Something went wrong", Toast.LENGTH_LONG).show(); |
| 541 | } | 542 | } |
| 542 | 543 | ||
| 543 | } else { | 544 | } else { |
| 544 | Toast.makeText(RegisterActivity.this, "You haven't picked Image", Toast.LENGTH_LONG).show(); | 545 | Toast.makeText(RegisterActivity.this, "You haven't picked Image", Toast.LENGTH_LONG).show(); |
| 545 | } | 546 | } |
| 546 | 547 | ||
| 547 | } | 548 | } |
| 548 | } | 549 | } |
| 549 | } | 550 | } |
app/src/main/java/com/dinhcv/lifelogpedometer/model/structure/RegisterInfo.java
| 1 | package com.dinhcv.lifelogpedometer.model.structure; | 1 | package com.dinhcv.lifelogpedometer.model.structure; |
| 2 | 2 | ||
| 3 | import com.google.gson.annotations.SerializedName; | 3 | import com.google.gson.annotations.SerializedName; |
| 4 | 4 | ||
| 5 | import java.util.Date; | 5 | import java.util.Date; |
| 6 | 6 | ||
| 7 | public class RegisterInfo { | 7 | public class RegisterInfo { |
| 8 | private String username; | 8 | private String username; |
| 9 | private String password; | 9 | private String password; |
| 10 | private String fullName; | 10 | private String fullName; |
| 11 | private String nickname; | 11 | private String nickname; |
| 12 | private Date birthday; | 12 | private Date birthday; |
| 13 | private int height; | 13 | private int height; |
| 14 | private int weight; | 14 | private int weight; |
| 15 | private int fatRate; | 15 | private int fatRate; |
| 16 | private int gender = 1; | 16 | private int gender = 1; |
| 17 | private String address; | 17 | private String address; |
| 18 | private String profileImage; | 18 | private String profileImage; |
| 19 | private String email; | 19 | private String email; |
| 20 | private String description; | ||
| 20 | 21 | ||
| 21 | 22 | ||
| 22 | public String getUsername() { | 23 | public String getUsername() { |
| 23 | return username; | 24 | return username; |
| 24 | } | 25 | } |
| 25 | 26 | ||
| 26 | public void setUsername(String username) { | 27 | public void setUsername(String username) { |
| 27 | this.username = username; | 28 | this.username = username; |
| 28 | } | 29 | } |
| 29 | 30 | ||
| 30 | public String getPassword() { | 31 | public String getPassword() { |
| 31 | return password; | 32 | return password; |
| 32 | } | 33 | } |
| 33 | 34 | ||
| 34 | public void setPassword(String password) { | 35 | public void setPassword(String password) { |
| 35 | this.password = password; | 36 | this.password = password; |
| 36 | } | 37 | } |
| 37 | 38 | ||
| 38 | public String getFullName() { | 39 | public String getFullName() { |
| 39 | return fullName; | 40 | return fullName; |
| 40 | } | 41 | } |
| 41 | 42 | ||
| 42 | public void setFullName(String fullName) { | 43 | public void setFullName(String fullName) { |
| 43 | this.fullName = fullName; | 44 | this.fullName = fullName; |
| 44 | } | 45 | } |
| 45 | 46 | ||
| 46 | public String getNickname() { | 47 | public String getNickname() { |
| 47 | return nickname; | 48 | return nickname; |
| 48 | } | 49 | } |
| 49 | 50 | ||
| 50 | public void setNickname(String nickname) { | 51 | public void setNickname(String nickname) { |
| 51 | this.nickname = nickname; | 52 | this.nickname = nickname; |
| 52 | } | 53 | } |
| 53 | 54 | ||
| 54 | public Date getBirthday() { | 55 | public Date getBirthday() { |
| 55 | return birthday; | 56 | return birthday; |
| 56 | } | 57 | } |
| 57 | 58 | ||
| 58 | public void setBirthday(Date birthday) { | 59 | public void setBirthday(Date birthday) { |
| 59 | this.birthday = birthday; | 60 | this.birthday = birthday; |
| 60 | } | 61 | } |
| 61 | 62 | ||
| 62 | public int getHeight() { | 63 | public int getHeight() { |
| 63 | return height; | 64 | return height; |
| 64 | } | 65 | } |
| 65 | 66 | ||
| 66 | public void setHeight(int height) { | 67 | public void setHeight(int height) { |
| 67 | this.height = height; | 68 | this.height = height; |
| 68 | } | 69 | } |
| 69 | 70 | ||
| 70 | public int getWeight() { | 71 | public int getWeight() { |
| 71 | return weight; | 72 | return weight; |
| 72 | } | 73 | } |
| 73 | 74 | ||
| 74 | public void setWeight(int weight) { | 75 | public void setWeight(int weight) { |
| 75 | this.weight = weight; | 76 | this.weight = weight; |
| 76 | } | 77 | } |
| 77 | 78 | ||
| 78 | public int getFatRate() { | 79 | public int getFatRate() { |
| 79 | return fatRate; | 80 | return fatRate; |
| 80 | } | 81 | } |
| 81 | 82 | ||
| 82 | public void setFatRate(int fatRate) { | 83 | public void setFatRate(int fatRate) { |
| 83 | this.fatRate = fatRate; | 84 | this.fatRate = fatRate; |
| 84 | } | 85 | } |
| 85 | 86 | ||
| 86 | 87 | ||
| 87 | public int getGender() { | 88 | public int getGender() { |
| 88 | return gender; | 89 | return gender; |
| 89 | } | 90 | } |
| 90 | 91 | ||
| 91 | public void setGender(int gender) { | 92 | public void setGender(int gender) { |
| 92 | this.gender = gender; | 93 | this.gender = gender; |
| 93 | } | 94 | } |
| 94 | 95 | ||
| 95 | public String getAddress() { | 96 | public String getAddress() { |
| 96 | return address; | 97 | return address; |
| 97 | } | 98 | } |
| 98 | 99 | ||
| 99 | public void setAddress(String address) { | 100 | public void setAddress(String address) { |
| 100 | this.address = address; | 101 | this.address = address; |
| 101 | } | 102 | } |
| 102 | 103 | ||
| 103 | public String getProfileImage() { | 104 | public String getProfileImage() { |
| 104 | return profileImage; | 105 | return profileImage; |
| 105 | } | 106 | } |
| 106 | 107 | ||
| 107 | public void setProfileImage(String profileImage) { | 108 | public void setProfileImage(String profileImage) { |
| 108 | this.profileImage = profileImage; | 109 | this.profileImage = profileImage; |
| 109 | } | 110 | } |
| 110 | 111 | ||
| 111 | public String getEmail() { | 112 | public String getEmail() { |
| 112 | return email; | 113 | return email; |
| 113 | } | 114 | } |
| 114 | 115 | ||
| 115 | public void setEmail(String email) { | 116 | public void setEmail(String email) { |
| 116 | this.email = email; | 117 | this.email = email; |
| 118 | } | ||
| 119 | |||
| 120 | |||
| 121 | public String getDescription() { | ||
| 122 | return description; | ||
| 123 | } | ||
| 124 | |||
| 125 | public void setDescription(String description) { | ||
| 126 | this.description = description; | ||
| 117 | } | 127 | } |
| 118 | } | 128 | } |
| 119 | 129 |
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.os.Environment; | 8 | import android.os.Environment; |
| 9 | import android.util.Base64; | 9 | import android.util.Base64; |
| 10 | import android.util.Log; | 10 | import android.util.Log; |
| 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.File; | 24 | import java.io.File; |
| 25 | import java.io.FileOutputStream; | 25 | import java.io.FileOutputStream; |
| 26 | import java.io.IOException; | 26 | import java.io.IOException; |
| 27 | import java.io.InputStream; | 27 | import java.io.InputStream; |
| 28 | import java.io.OutputStreamWriter; | 28 | import java.io.OutputStreamWriter; |
| 29 | import java.io.UnsupportedEncodingException; | 29 | import java.io.UnsupportedEncodingException; |
| 30 | import java.net.URLEncoder; | 30 | import java.net.URLEncoder; |
| 31 | import java.util.Date; | 31 | import java.util.Date; |
| 32 | import java.util.HashMap; | 32 | import java.util.HashMap; |
| 33 | import java.util.Map; | 33 | import java.util.Map; |
| 34 | import java.util.concurrent.TimeUnit; | 34 | import java.util.concurrent.TimeUnit; |
| 35 | 35 | ||
| 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 | private static String URL_ROOT = "http://clover.timesfun.jp:9001/"; | 45 | private static String URL_ROOT = "http://clover.timesfun.jp:9001/"; |
| 46 | //login info | 46 | //login info |
| 47 | private static String URL_LOGIN_INFO = "login"; | 47 | private static String URL_LOGIN_INFO = "login"; |
| 48 | //register info | 48 | //register info |
| 49 | private static String URL_REGISTER_INFO = "register"; | 49 | private static String URL_REGISTER_INFO = "register"; |
| 50 | //upload image info | 50 | //upload image info |
| 51 | private static String URL_UPLOAD_IMAGE_INFO = "upload-image"; | 51 | private static String URL_UPLOAD_IMAGE_INFO = "upload-image"; |
| 52 | //Search zipcode | 52 | //Search zipcode |
| 53 | private static String URL_FORGET_PASS_INFO = "forgetPass"; | 53 | private static String URL_FORGET_PASS_INFO = "forgetPass"; |
| 54 | //history | 54 | //history |
| 55 | private static String URL_HISTORE_INFO = "api/history"; | 55 | private static String URL_HISTORE_INFO = "api/history"; |
| 56 | //history detail | 56 | //history detail |
| 57 | private static String URL_HISTORE_DETAIL_INFO = "api/history/detail"; | 57 | private static String URL_HISTORE_DETAIL_INFO = "api/history/detail"; |
| 58 | // home | 58 | // home |
| 59 | private static String URL_HOME_INFO = "api/home"; | 59 | private static String URL_HOME_INFO = "api/home"; |
| 60 | //Search zipcode | 60 | //Search zipcode |
| 61 | private static String URL_FORGET_PASS_CONFIRM_INFO = "forgetPass/confirm"; | 61 | private static String URL_FORGET_PASS_CONFIRM_INFO = "forgetPass/confirm"; |
| 62 | // Refesh Token | 62 | // Refesh Token |
| 63 | private static String URL_REFESH_TOKEN_INFO = "refreshToken"; | 63 | private static String URL_REFESH_TOKEN_INFO = "refreshToken"; |
| 64 | // Top info | 64 | // Top info |
| 65 | private static String URL_TOP_INFO = "api/top"; | 65 | private static String URL_TOP_INFO = "api/top"; |
| 66 | // Refesh Token | 66 | // Refesh Token |
| 67 | private static String URL_CREATE_LOG_INFO = "api/createLog"; | 67 | private static String URL_CREATE_LOG_INFO = "api/createLog"; |
| 68 | // Get news | 68 | // Get news |
| 69 | private static String URL_GET_NEWS_INFO = "api/getnoticesbydate"; | 69 | private static String URL_GET_NEWS_INFO = "api/getnoticesbydate"; |
| 70 | 70 | ||
| 71 | 71 | ||
| 72 | private static int deviationValue = 1; | 72 | private static int deviationValue = 1; |
| 73 | 73 | ||
| 74 | public static class Login extends AsyncTask<Void, Void, String>{ | 74 | public static class Login extends AsyncTask<Void, Void, String>{ |
| 75 | private Context context; | 75 | private Context context; |
| 76 | private String email; | 76 | private String email; |
| 77 | private String pass; | 77 | private String pass; |
| 78 | private LLAPIManagerListener action; | 78 | private LLAPIManagerListener action; |
| 79 | 79 | ||
| 80 | public Login(Context context, String email, String pass, LLAPIManagerListener action) { | 80 | public Login(Context context, String email, String pass, LLAPIManagerListener action) { |
| 81 | this.context = context; | 81 | this.context = context; |
| 82 | this.email = email; | 82 | this.email = email; |
| 83 | this.pass = pass; | 83 | this.pass = pass; |
| 84 | this.action = action; | 84 | this.action = action; |
| 85 | } | 85 | } |
| 86 | 86 | ||
| 87 | @Override | 87 | @Override |
| 88 | protected void onPreExecute() { | 88 | protected void onPreExecute() { |
| 89 | super.onPreExecute(); | 89 | super.onPreExecute(); |
| 90 | } | 90 | } |
| 91 | 91 | ||
| 92 | @Override | 92 | @Override |
| 93 | protected String doInBackground(Void... voids) { | 93 | protected String doInBackground(Void... voids) { |
| 94 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_LOGIN_INFO); | 94 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_LOGIN_INFO); |
| 95 | JSONObject jsonObject = new JSONObject(); | 95 | JSONObject jsonObject = new JSONObject(); |
| 96 | 96 | ||
| 97 | try { | 97 | try { |
| 98 | jsonObject.put("email", email); | 98 | jsonObject.put("email", email); |
| 99 | jsonObject.put("password", pass); | 99 | jsonObject.put("password", pass); |
| 100 | 100 | ||
| 101 | Debug.warn("Json data: " + jsonObject.toString()); | 101 | Debug.warn("Json data: " + jsonObject.toString()); |
| 102 | } catch (JSONException e) { | 102 | } catch (JSONException e) { |
| 103 | Debug.normal("Error ", e.getMessage()); | 103 | Debug.normal("Error ", e.getMessage()); |
| 104 | } | 104 | } |
| 105 | 105 | ||
| 106 | MediaType JSON | 106 | MediaType JSON |
| 107 | = MediaType.parse("application/json"); | 107 | = MediaType.parse("application/json"); |
| 108 | Debug.normal("JSON STRING: %s", jsonObject.toString()); | 108 | Debug.normal("JSON STRING: %s", jsonObject.toString()); |
| 109 | RequestBody requestBody = RequestBody.create(JSON, jsonObject.toString()); | 109 | RequestBody requestBody = RequestBody.create(JSON, jsonObject.toString()); |
| 110 | 110 | ||
| 111 | final Request.Builder request = new Request.Builder() | 111 | final Request.Builder request = new Request.Builder() |
| 112 | .url(baseUrl) | 112 | .url(baseUrl) |
| 113 | .header("User-Agent", Utils.getCustomUA()) | 113 | .header("User-Agent", Utils.getCustomUA()) |
| 114 | .header("Content-Type", "application/json") | 114 | .header("Content-Type", "application/json") |
| 115 | .post(requestBody); | 115 | .post(requestBody); |
| 116 | 116 | ||
| 117 | OkHttpClient httpClient = new OkHttpClient.Builder() | 117 | OkHttpClient httpClient = new OkHttpClient.Builder() |
| 118 | .readTimeout(30, TimeUnit.SECONDS) | 118 | .readTimeout(30, TimeUnit.SECONDS) |
| 119 | .writeTimeout(30, TimeUnit.SECONDS) | 119 | .writeTimeout(30, TimeUnit.SECONDS) |
| 120 | .connectTimeout(30, TimeUnit.SECONDS) | 120 | .connectTimeout(30, TimeUnit.SECONDS) |
| 121 | .build(); | 121 | .build(); |
| 122 | 122 | ||
| 123 | String jsonString = null; | 123 | String jsonString = null; |
| 124 | try { | 124 | try { |
| 125 | Response response = httpClient.newCall(request.build()).execute(); | 125 | Response response = httpClient.newCall(request.build()).execute(); |
| 126 | if (response == null) { | 126 | if (response == null) { |
| 127 | action.onError(null); | 127 | action.onError(null); |
| 128 | Debug.error("Response is null"); | 128 | Debug.error("Response is null"); |
| 129 | return null; | 129 | return null; |
| 130 | } | 130 | } |
| 131 | jsonString = getResponseData(response.body()); | 131 | jsonString = getResponseData(response.body()); |
| 132 | 132 | ||
| 133 | } catch (IOException e) { | 133 | } catch (IOException e) { |
| 134 | Debug.normal("Error %s", e.getMessage()); | 134 | Debug.normal("Error %s", e.getMessage()); |
| 135 | } | 135 | } |
| 136 | return jsonString; | 136 | return jsonString; |
| 137 | } | 137 | } |
| 138 | 138 | ||
| 139 | @Override | 139 | @Override |
| 140 | protected void onCancelled() { | 140 | protected void onCancelled() { |
| 141 | super.onCancelled(); | 141 | super.onCancelled(); |
| 142 | action.onError(null); | 142 | action.onError(null); |
| 143 | } | 143 | } |
| 144 | 144 | ||
| 145 | @Override | 145 | @Override |
| 146 | protected void onPostExecute(String jsonString) { | 146 | protected void onPostExecute(String jsonString) { |
| 147 | super.onPostExecute(jsonString); | 147 | super.onPostExecute(jsonString); |
| 148 | 148 | ||
| 149 | if ((jsonString == null) || (jsonString.isEmpty())) { | 149 | if ((jsonString == null) || (jsonString.isEmpty())) { |
| 150 | action.onError(null); | 150 | action.onError(null); |
| 151 | return; | 151 | return; |
| 152 | } | 152 | } |
| 153 | Debug.normal("String body: "+jsonString); | 153 | Debug.normal("String body: "+jsonString); |
| 154 | 154 | ||
| 155 | try { | 155 | try { |
| 156 | JSONObject jsonObject = new JSONObject(jsonString); | 156 | JSONObject jsonObject = new JSONObject(jsonString); |
| 157 | int status = jsonObject.optInt("status"); | 157 | int status = jsonObject.optInt("status"); |
| 158 | if (status == 1){ | 158 | if (status == 1){ |
| 159 | JSONObject object = jsonObject.optJSONObject("result"); | 159 | JSONObject object = jsonObject.optJSONObject("result"); |
| 160 | JSONObject objUser = object.getJSONObject("user"); | 160 | JSONObject objUser = object.getJSONObject("user"); |
| 161 | String token = object.optString("token"); | 161 | String token = object.optString("token"); |
| 162 | // save data | 162 | // save data |
| 163 | Setting.setUserDataSharepre(context, Setting.TOKEN_SHAREPRE, token); | 163 | Setting.setUserDataSharepre(context, Setting.TOKEN_SHAREPRE, token); |
| 164 | APIResponse.getInstance().setToken(token); | 164 | APIResponse.getInstance().setToken(token); |
| 165 | Debug.normal("Token: "+ token); | 165 | Debug.normal("Token: "+ token); |
| 166 | action.onSuccess(objUser); | 166 | action.onSuccess(objUser); |
| 167 | }else { | 167 | }else { |
| 168 | action.onError(new Error(jsonObject.optString("message"))); | 168 | action.onError(new Error(jsonObject.optString("message"))); |
| 169 | } | 169 | } |
| 170 | } catch (JSONException e) { | 170 | } catch (JSONException e) { |
| 171 | e.printStackTrace(); | 171 | e.printStackTrace(); |
| 172 | } | 172 | } |
| 173 | } | 173 | } |
| 174 | } | 174 | } |
| 175 | 175 | ||
| 176 | public static class UploadImage extends AsyncTask<Void, Void, String>{ | 176 | public static class UploadImage extends AsyncTask<Void, Void, String>{ |
| 177 | private Bitmap bitmap; | 177 | private Bitmap bitmap; |
| 178 | private LLAPIManagerListener action; | 178 | private LLAPIManagerListener action; |
| 179 | 179 | ||
| 180 | public UploadImage(Bitmap bitmap, LLAPIManagerListener action) { | 180 | public UploadImage(Bitmap bitmap, LLAPIManagerListener action) { |
| 181 | this.bitmap = bitmap; | 181 | this.bitmap = bitmap; |
| 182 | this.action = action; | 182 | this.action = action; |
| 183 | } | 183 | } |
| 184 | 184 | ||
| 185 | @Override | 185 | @Override |
| 186 | protected void onPreExecute() { | 186 | protected void onPreExecute() { |
| 187 | super.onPreExecute(); | 187 | super.onPreExecute(); |
| 188 | } | 188 | } |
| 189 | 189 | ||
| 190 | @Override | 190 | @Override |
| 191 | protected String doInBackground(Void... voids) { | 191 | protected String doInBackground(Void... voids) { |
| 192 | String baseUrl = URL_ROOT + URL_UPLOAD_IMAGE_INFO; | 192 | String baseUrl = URL_ROOT + URL_UPLOAD_IMAGE_INFO; |
| 193 | 193 | ||
| 194 | Bitmap b = Bitmap.createScaledBitmap(bitmap, 120, 120, false); | 194 | Bitmap b = Bitmap.createScaledBitmap(bitmap, 120, 120, false); |
| 195 | byte[] bitmapByte = convertBitmapToByteArray(b); | 195 | byte[] bitmapByte = convertBitmapToByteArray(b); |
| 196 | String imageBase64 = Base64.encodeToString(bitmapByte, Base64.DEFAULT); | 196 | String imageBase64 = Base64.encodeToString(bitmapByte, Base64.DEFAULT); |
| 197 | 197 | ||
| 198 | String token = APIResponse.getInstance().getToken(); | 198 | String token = APIResponse.getInstance().getToken(); |
| 199 | //generate hashMap to store encodedImage and the name | 199 | //generate hashMap to store encodedImage and the name |
| 200 | HashMap<String,String> detail = new HashMap<>(); | 200 | HashMap<String,String> detail = new HashMap<>(); |
| 201 | detail.put("img", imageBase64); | 201 | detail.put("img", imageBase64); |
| 202 | if (token != null && !token.isEmpty()) { | 202 | if (token != null && !token.isEmpty()) { |
| 203 | Debug.normal("Token is not null"); | 203 | Debug.normal("Token is not null"); |
| 204 | detail.put("token", token); | 204 | detail.put("token", token); |
| 205 | } | 205 | } |
| 206 | 206 | ||
| 207 | try { | 207 | try { |
| 208 | String dataToSend = hashMapToUrl(detail); | 208 | String dataToSend = hashMapToUrl(detail); |
| 209 | 209 | ||
| 210 | String response = RequestPostImage.post(baseUrl,dataToSend); | 210 | String response = RequestPostImage.post(baseUrl,dataToSend); |
| 211 | if (response == null) { | 211 | if (response == null) { |
| 212 | action.onError(null); | 212 | action.onError(null); |
| 213 | Debug.error("Response is null"); | 213 | Debug.error("Response is null"); |
| 214 | return null; | 214 | return null; |
| 215 | } | 215 | } |
| 216 | 216 | ||
| 217 | Debug.normal("Response: "+response); | 217 | Debug.normal("Response: "+response); |
| 218 | return response; | 218 | return response; |
| 219 | } catch (IOException e) { | 219 | } catch (IOException e) { |
| 220 | Debug.normal("Error %s", e.getMessage()); | 220 | Debug.normal("Error %s", e.getMessage()); |
| 221 | return null; | 221 | return null; |
| 222 | } | 222 | } |
| 223 | } | 223 | } |
| 224 | 224 | ||
| 225 | @Override | 225 | @Override |
| 226 | protected void onCancelled() { | 226 | protected void onCancelled() { |
| 227 | super.onCancelled(); | 227 | super.onCancelled(); |
| 228 | action.onError(null); | 228 | action.onError(null); |
| 229 | } | 229 | } |
| 230 | 230 | ||
| 231 | @Override | 231 | @Override |
| 232 | protected void onPostExecute(String jsonString) { | 232 | protected void onPostExecute(String jsonString) { |
| 233 | super.onPostExecute(jsonString); | 233 | super.onPostExecute(jsonString); |
| 234 | if ((jsonString == null) || (jsonString.isEmpty())) { | 234 | if ((jsonString == null) || (jsonString.isEmpty())) { |
| 235 | action.onError(null); | 235 | action.onError(null); |
| 236 | return; | 236 | return; |
| 237 | } | 237 | } |
| 238 | Debug.normal("String body: "+jsonString); | 238 | Debug.normal("String body: "+jsonString); |
| 239 | 239 | ||
| 240 | try { | 240 | try { |
| 241 | JSONObject jsonObject = new JSONObject(jsonString); | 241 | JSONObject jsonObject = new JSONObject(jsonString); |
| 242 | int status = jsonObject.optInt("httpCode"); | 242 | int status = jsonObject.optInt("httpCode"); |
| 243 | if (status != 0){ | 243 | if (status != 0){ |
| 244 | action.onSuccess(jsonObject.optString("message")); | 244 | action.onSuccess(jsonObject.optString("message")); |
| 245 | }else { | 245 | }else { |
| 246 | action.onError(new Error(jsonObject.optString("message"))); | 246 | action.onError(new Error(jsonObject.optString("message"))); |
| 247 | } | 247 | } |
| 248 | } catch (JSONException e) { | 248 | } catch (JSONException e) { |
| 249 | e.printStackTrace(); | 249 | e.printStackTrace(); |
| 250 | } | 250 | } |
| 251 | } | 251 | } |
| 252 | } | 252 | } |
| 253 | 253 | ||
| 254 | public static class UploadImage1 extends AsyncTask<Void, Void, String>{ | 254 | public static class UploadImage1 extends AsyncTask<Void, Void, String>{ |
| 255 | private Bitmap bitmap; | 255 | private Bitmap bitmap; |
| 256 | private LLAPIManagerListener action; | 256 | private LLAPIManagerListener action; |
| 257 | 257 | ||
| 258 | public UploadImage1(Bitmap bitmap, LLAPIManagerListener action) { | 258 | public UploadImage1(Bitmap bitmap, LLAPIManagerListener action) { |
| 259 | this.bitmap = bitmap; | 259 | this.bitmap = bitmap; |
| 260 | this.action = action; | 260 | this.action = action; |
| 261 | } | 261 | } |
| 262 | 262 | ||
| 263 | @Override | 263 | @Override |
| 264 | protected void onPreExecute() { | 264 | protected void onPreExecute() { |
| 265 | super.onPreExecute(); | 265 | super.onPreExecute(); |
| 266 | } | 266 | } |
| 267 | 267 | ||
| 268 | @Override | 268 | @Override |
| 269 | protected String doInBackground(Void... voids) { | 269 | protected String doInBackground(Void... voids) { |
| 270 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_UPLOAD_IMAGE_INFO); | 270 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_UPLOAD_IMAGE_INFO); |
| 271 | JSONObject jsonObject = new JSONObject(); | 271 | JSONObject jsonObject = new JSONObject(); |
| 272 | Bitmap b = Bitmap.createScaledBitmap(bitmap, 120, 120, false); | 272 | Bitmap b = Bitmap.createScaledBitmap(bitmap, 120, 120, false); |
| 273 | byte[] bitmapByte = convertBitmapToByteArray(b); | 273 | byte[] bitmapByte = convertBitmapToByteArray(b); |
| 274 | String imageBase64 = Base64.encodeToString(bitmapByte, Base64.DEFAULT); | 274 | String imageBase64 = Base64.encodeToString(bitmapByte, Base64.DEFAULT); |
| 275 | //String imageStr = imageBase64.replace("\n",""); | 275 | //String imageStr = imageBase64.replace("\n",""); |
| 276 | //writeToFile(imageBase64); | 276 | //writeToFile(imageBase64); |
| 277 | 277 | ||
| 278 | String token = APIResponse.getInstance().getToken(); | 278 | String token = APIResponse.getInstance().getToken(); |
| 279 | try { | 279 | try { |
| 280 | if (token != null && !token.isEmpty()) { | 280 | if (token != null && !token.isEmpty()) { |
| 281 | Debug.normal("Token is not null"); | 281 | Debug.normal("Token is not null"); |
| 282 | jsonObject.put("token", token); | 282 | jsonObject.put("token", token); |
| 283 | } | 283 | } |
| 284 | jsonObject.put("img", imageBase64); | 284 | jsonObject.put("img", imageBase64); |
| 285 | 285 | ||
| 286 | //Debug.warn("Json data: " + jsonObject.toString()); | 286 | //Debug.warn("Json data: " + jsonObject.toString()); |
| 287 | } catch (JSONException e) { | 287 | } catch (JSONException e) { |
| 288 | Debug.normal("Error ", e.getMessage()); | 288 | Debug.normal("Error ", e.getMessage()); |
| 289 | } | 289 | } |
| 290 | 290 | ||
| 291 | MediaType JSON | 291 | MediaType JSON |
| 292 | = MediaType.parse("application/json; charset=utf-8"); | 292 | = MediaType.parse("application/json; charset=utf-8"); |
| 293 | RequestBody requestBody = RequestBody.create(JSON, jsonObject.toString()); | 293 | RequestBody requestBody = RequestBody.create(JSON, jsonObject.toString()); |
| 294 | 294 | ||
| 295 | final Request.Builder request = new Request.Builder() | 295 | final Request.Builder request = new Request.Builder() |
| 296 | .url(baseUrl) | 296 | .url(baseUrl) |
| 297 | .header("Content-Type", "application/json") | 297 | .header("Content-Type", "application/json") |
| 298 | .post(requestBody); | 298 | .post(requestBody); |
| 299 | 299 | ||
| 300 | OkHttpClient httpClient = new OkHttpClient.Builder() | 300 | OkHttpClient httpClient = new OkHttpClient.Builder() |
| 301 | .readTimeout(30, TimeUnit.SECONDS) | 301 | .readTimeout(30, TimeUnit.SECONDS) |
| 302 | .writeTimeout(30, TimeUnit.SECONDS) | 302 | .writeTimeout(30, TimeUnit.SECONDS) |
| 303 | .connectTimeout(30, TimeUnit.SECONDS) | 303 | .connectTimeout(30, TimeUnit.SECONDS) |
| 304 | .build(); | 304 | .build(); |
| 305 | String jsonString = null; | 305 | String jsonString = null; |
| 306 | try { | 306 | try { |
| 307 | Response response = httpClient.newCall(request.build()).execute(); | 307 | Response response = httpClient.newCall(request.build()).execute(); |
| 308 | if (response == null) { | 308 | if (response == null) { |
| 309 | action.onError(null); | 309 | action.onError(null); |
| 310 | Debug.error("Response is null"); | 310 | Debug.error("Response is null"); |
| 311 | return null; | 311 | return null; |
| 312 | } | 312 | } |
| 313 | jsonString = getResponseData(response.body()); | 313 | jsonString = getResponseData(response.body()); |
| 314 | 314 | ||
| 315 | } catch (IOException e) { | 315 | } catch (IOException e) { |
| 316 | Debug.normal("Error %s", e.getMessage()); | 316 | Debug.normal("Error %s", e.getMessage()); |
| 317 | } | 317 | } |
| 318 | 318 | ||
| 319 | return jsonString; | 319 | return jsonString; |
| 320 | } | 320 | } |
| 321 | 321 | ||
| 322 | @Override | 322 | @Override |
| 323 | protected void onCancelled() { | 323 | protected void onCancelled() { |
| 324 | super.onCancelled(); | 324 | super.onCancelled(); |
| 325 | action.onError(null); | 325 | action.onError(null); |
| 326 | } | 326 | } |
| 327 | 327 | ||
| 328 | @Override | 328 | @Override |
| 329 | protected void onPostExecute(String jsonString) { | 329 | protected void onPostExecute(String jsonString) { |
| 330 | super.onPostExecute(jsonString); | 330 | super.onPostExecute(jsonString); |
| 331 | if ((jsonString == null) || (jsonString.isEmpty())) { | 331 | if ((jsonString == null) || (jsonString.isEmpty())) { |
| 332 | action.onError(null); | 332 | action.onError(null); |
| 333 | return; | 333 | return; |
| 334 | } | 334 | } |
| 335 | Debug.normal("String body: "+jsonString); | 335 | Debug.normal("String body: "+jsonString); |
| 336 | 336 | ||
| 337 | try { | 337 | try { |
| 338 | JSONObject jsonObject = new JSONObject(jsonString); | 338 | JSONObject jsonObject = new JSONObject(jsonString); |
| 339 | int status = jsonObject.optInt("status"); | 339 | int status = jsonObject.optInt("status"); |
| 340 | if (status == 1){ | 340 | if (status == 1){ |
| 341 | action.onSuccess(new JSONObject()); | 341 | action.onSuccess(new JSONObject()); |
| 342 | }else { | 342 | }else { |
| 343 | action.onError(new Error(jsonObject.optString("message"))); | 343 | action.onError(new Error(jsonObject.optString("message"))); |
| 344 | } | 344 | } |
| 345 | } catch (JSONException e) { | 345 | } catch (JSONException e) { |
| 346 | e.printStackTrace(); | 346 | e.printStackTrace(); |
| 347 | } | 347 | } |
| 348 | } | 348 | } |
| 349 | } | 349 | } |
| 350 | 350 | ||
| 351 | 351 | ||
| 352 | public static class Register extends AsyncTask<Void, Void, String>{ | 352 | public static class Register extends AsyncTask<Void, Void, String>{ |
| 353 | private RegisterInfo registerInfo; | 353 | private RegisterInfo registerInfo; |
| 354 | private LLAPIManagerListener action; | 354 | private LLAPIManagerListener action; |
| 355 | 355 | ||
| 356 | public Register(RegisterInfo registerInfo, LLAPIManagerListener action) { | 356 | public Register(RegisterInfo registerInfo, LLAPIManagerListener action) { |
| 357 | this.registerInfo = registerInfo; | 357 | this.registerInfo = registerInfo; |
| 358 | this.action = action; | 358 | this.action = action; |
| 359 | } | 359 | } |
| 360 | 360 | ||
| 361 | @Override | 361 | @Override |
| 362 | protected void onPreExecute() { | 362 | protected void onPreExecute() { |
| 363 | super.onPreExecute(); | 363 | super.onPreExecute(); |
| 364 | } | 364 | } |
| 365 | 365 | ||
| 366 | @Override | 366 | @Override |
| 367 | protected String doInBackground(Void... voids) { | 367 | protected String doInBackground(Void... voids) { |
| 368 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_REGISTER_INFO); | 368 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_REGISTER_INFO); |
| 369 | JSONObject jsonObject = new JSONObject(); | 369 | JSONObject jsonObject = new JSONObject(); |
| 370 | 370 | ||
| 371 | String token = APIResponse.getInstance().getToken(); | 371 | String token = APIResponse.getInstance().getToken(); |
| 372 | try { | 372 | try { |
| 373 | jsonObject.put("username", registerInfo.getUsername()); | 373 | jsonObject.put("username", registerInfo.getUsername()); |
| 374 | jsonObject.put("password", registerInfo.getPassword()); | 374 | jsonObject.put("password", registerInfo.getPassword()); |
| 375 | jsonObject.put("password_confirmation", registerInfo.getPassword()); | 375 | jsonObject.put("password_confirmation", registerInfo.getPassword()); |
| 376 | jsonObject.put("full_name", registerInfo.getFullName()); | 376 | jsonObject.put("full_name", registerInfo.getFullName()); |
| 377 | //jsonObject.put("nickname", registerInfo.getNickname()); | 377 | jsonObject.put("nickname", registerInfo.getNickname()); |
| 378 | jsonObject.put("birthday", Utils.dateToStringFormatDayMonthYear(registerInfo.getBirthday())); | 378 | jsonObject.put("birthday", Utils.dateToStringFormatDayMonthYear(registerInfo.getBirthday())); |
| 379 | jsonObject.put("height", ""+registerInfo.getHeight()); | 379 | jsonObject.put("height", ""+registerInfo.getHeight()); |
| 380 | jsonObject.put("weight", ""+registerInfo.getWeight()); | 380 | jsonObject.put("weight", ""+registerInfo.getWeight()); |
| 381 | jsonObject.put("fat_rate", ""+registerInfo.getFatRate()); | 381 | jsonObject.put("fat_rate", ""+registerInfo.getFatRate()); |
| 382 | jsonObject.put("gender", ""+registerInfo.getGender()); | 382 | jsonObject.put("gender", ""+registerInfo.getGender()); |
| 383 | //jsonObject.put("address", registerInfo.getAddress()); | 383 | jsonObject.put("address", ""+registerInfo.getAddress()); |
| 384 | //jsonObject.put("desciption", ""); | 384 | jsonObject.put("desciption", ""+registerInfo.getDescription()); |
| 385 | jsonObject.put("receive_notification", "0"); | 385 | jsonObject.put("receive_notification", "0"); |
| 386 | //jsonObject.put("phone", "+84986940999"); | 386 | jsonObject.put("phone", "0123-849-869"); |
| 387 | jsonObject.put("share_data", "1"); | 387 | jsonObject.put("share_data", "0"); |
| 388 | jsonObject.put("profile_image", ""+registerInfo.getProfileImage()); | 388 | jsonObject.put("profile_image", ""+registerInfo.getProfileImage()); |
| 389 | //jsonObject.put("delete_flag", "0"); | 389 | //jsonObject.put("delete_flag", "0"); |
| 390 | jsonObject.put("email", registerInfo.getEmail()); | 390 | jsonObject.put("email", registerInfo.getEmail()); |
| 391 | jsonObject.put("physical_activity", "0"); | 391 | jsonObject.put("physical_activity", "0"); |
| 392 | //jsonObject.put("remember", "1"); | 392 | jsonObject.put("remember_me", "1"); |
| 393 | 393 | ||
| 394 | Debug.warn("Json data: " + jsonObject.toString()); | 394 | Debug.warn("Json data: " + jsonObject.toString()); |
| 395 | } catch (JSONException e) { | 395 | } catch (JSONException e) { |
| 396 | Debug.normal("Error ", e.getMessage()); | 396 | Debug.normal("Error ", e.getMessage()); |
| 397 | } | 397 | } |
| 398 | 398 | ||
| 399 | MediaType JSON | 399 | MediaType JSON |
| 400 | = MediaType.parse("application/json; charset=utf-8"); | 400 | = MediaType.parse("application/json; charset=utf-8"); |
| 401 | RequestBody requestBody = RequestBody.create(JSON, jsonObject.toString()); | 401 | RequestBody requestBody = RequestBody.create(JSON, jsonObject.toString()); |
| 402 | 402 | ||
| 403 | final Request.Builder request = new Request.Builder() | 403 | final Request.Builder request = new Request.Builder() |
| 404 | .url(baseUrl) | 404 | .url(baseUrl) |
| 405 | .header("User-Agent", Utils.getCustomUA()) | 405 | .header("User-Agent", Utils.getCustomUA()) |
| 406 | .header("Content-Type", "application/json") | 406 | .header("Content-Type", "application/json") |
| 407 | .post(requestBody); | 407 | .post(requestBody); |
| 408 | 408 | ||
| 409 | OkHttpClient httpClient = new OkHttpClient.Builder() | 409 | OkHttpClient httpClient = new OkHttpClient.Builder() |
| 410 | .readTimeout(30, TimeUnit.SECONDS) | 410 | .readTimeout(30, TimeUnit.SECONDS) |
| 411 | .writeTimeout(30, TimeUnit.SECONDS) | 411 | .writeTimeout(30, TimeUnit.SECONDS) |
| 412 | .connectTimeout(30, TimeUnit.SECONDS) | 412 | .connectTimeout(30, TimeUnit.SECONDS) |
| 413 | .build(); | 413 | .build(); |
| 414 | 414 | ||
| 415 | String jsonString = null; | 415 | String jsonString = null; |
| 416 | try { | 416 | try { |
| 417 | Response response = httpClient.newCall(request.build()).execute(); | 417 | Response response = httpClient.newCall(request.build()).execute(); |
| 418 | if (response == null) { | 418 | if (response == null) { |
| 419 | action.onError(null); | 419 | action.onError(null); |
| 420 | Debug.error("Response is null"); | 420 | Debug.error("Response is null"); |
| 421 | return null; | 421 | return null; |
| 422 | } | 422 | } |
| 423 | jsonString = getResponseData(response.body()); | 423 | jsonString = getResponseData(response.body()); |
| 424 | 424 | ||
| 425 | } catch (IOException e) { | 425 | } catch (IOException e) { |
| 426 | Debug.normal("Error %s", e.getMessage()); | 426 | Debug.normal("Error %s", e.getMessage()); |
| 427 | } | 427 | } |
| 428 | return jsonString; | 428 | return jsonString; |
| 429 | } | 429 | } |
| 430 | 430 | ||
| 431 | @Override | 431 | @Override |
| 432 | protected void onCancelled() { | 432 | protected void onCancelled() { |
| 433 | super.onCancelled(); | 433 | super.onCancelled(); |
| 434 | action.onError(null); | 434 | action.onError(null); |
| 435 | } | 435 | } |
| 436 | 436 | ||
| 437 | @Override | 437 | @Override |
| 438 | protected void onPostExecute(String jsonString) { | 438 | protected void onPostExecute(String jsonString) { |
| 439 | super.onPostExecute(jsonString); | 439 | super.onPostExecute(jsonString); |
| 440 | if ((jsonString == null) || (jsonString.isEmpty())) { | 440 | if ((jsonString == null) || (jsonString.isEmpty())) { |
| 441 | action.onError(null); | 441 | action.onError(null); |
| 442 | return; | 442 | return; |
| 443 | } | 443 | } |
| 444 | Debug.normal("String body: "+jsonString); | 444 | Debug.normal("String body: "+jsonString); |
| 445 | 445 | ||
| 446 | try { | 446 | try { |
| 447 | JSONObject jsonObject = new JSONObject(jsonString); | 447 | JSONObject jsonObject = new JSONObject(jsonString); |
| 448 | int status = jsonObject.optInt("status"); | 448 | int status = jsonObject.optInt("status"); |
| 449 | if (status == 1){ | 449 | if (status == 1){ |
| 450 | JSONObject object = jsonObject.optJSONObject("result"); | 450 | JSONObject object = jsonObject.optJSONObject("result"); |
| 451 | JSONObject objUser = object.getJSONObject("user"); | 451 | JSONObject objUser = object.getJSONObject("user"); |
| 452 | String token = object.optString("token"); | 452 | String token = object.optString("token"); |
| 453 | APIResponse.getInstance().setToken(token); | 453 | APIResponse.getInstance().setToken(token); |
| 454 | Debug.normal("Token: "+ token); | 454 | Debug.normal("Token: "+ token); |
| 455 | action.onSuccess(objUser); | 455 | action.onSuccess(objUser); |
| 456 | }else { | 456 | }else { |
| 457 | action.onError(new Error(jsonObject.optString("message"))); | 457 | action.onError(new Error(jsonObject.optString("message"))); |
| 458 | } | 458 | } |
| 459 | } catch (JSONException e) { | 459 | } catch (JSONException e) { |
| 460 | e.printStackTrace(); | 460 | e.printStackTrace(); |
| 461 | } | 461 | } |
| 462 | 462 | ||
| 463 | } | 463 | } |
| 464 | } | 464 | } |
| 465 | 465 | ||
| 466 | private static byte[] convertBitmapToByteArray(Bitmap bitmap) { | 466 | private static byte[] convertBitmapToByteArray(Bitmap bitmap) { |
| 467 | ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); | 467 | ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
| 468 | bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream); | 468 | bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream); |
| 469 | byte[] byteArray = byteArrayOutputStream.toByteArray(); | 469 | byte[] byteArray = byteArrayOutputStream.toByteArray(); |
| 470 | return byteArray; | 470 | return byteArray; |
| 471 | } | 471 | } |
| 472 | 472 | ||
| 473 | public static class ForgetPass extends AsyncTask<Void, Void, String>{ | 473 | public static class ForgetPass extends AsyncTask<Void, Void, String>{ |
| 474 | private String email; | 474 | private String email; |
| 475 | private LLAPIManagerListener action; | 475 | private LLAPIManagerListener action; |
| 476 | 476 | ||
| 477 | public ForgetPass(String email, LLAPIManagerListener action) { | 477 | public ForgetPass(String email, LLAPIManagerListener action) { |
| 478 | this.email = email; | 478 | this.email = email; |
| 479 | this.action = action; | 479 | this.action = action; |
| 480 | } | 480 | } |
| 481 | 481 | ||
| 482 | @Override | 482 | @Override |
| 483 | protected void onPreExecute() { | 483 | protected void onPreExecute() { |
| 484 | super.onPreExecute(); | 484 | super.onPreExecute(); |
| 485 | } | 485 | } |
| 486 | 486 | ||
| 487 | @Override | 487 | @Override |
| 488 | protected String doInBackground(Void... voids) { | 488 | protected String doInBackground(Void... voids) { |
| 489 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_FORGET_PASS_INFO); | 489 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_FORGET_PASS_INFO); |
| 490 | JSONObject jsonObject = new JSONObject(); | 490 | JSONObject jsonObject = new JSONObject(); |
| 491 | try { | 491 | try { |
| 492 | //Personal info | 492 | //Personal info |
| 493 | jsonObject.put("email", email); | 493 | jsonObject.put("email", email); |
| 494 | 494 | ||
| 495 | Debug.warn("Json data: " + jsonObject.toString()); | 495 | Debug.warn("Json data: " + jsonObject.toString()); |
| 496 | } catch (JSONException e) { | 496 | } catch (JSONException e) { |
| 497 | Debug.normal("Error ", e.getMessage()); | 497 | Debug.normal("Error ", e.getMessage()); |
| 498 | } | 498 | } |
| 499 | 499 | ||
| 500 | MediaType JSON | 500 | MediaType JSON |
| 501 | = MediaType.parse("application/json"); | 501 | = MediaType.parse("application/json"); |
| 502 | RequestBody requestBody = RequestBody.create(JSON, jsonObject.toString()); | 502 | RequestBody requestBody = RequestBody.create(JSON, jsonObject.toString()); |
| 503 | 503 | ||
| 504 | final Request.Builder request = new Request.Builder() | 504 | final Request.Builder request = new Request.Builder() |
| 505 | .url(baseUrl) | 505 | .url(baseUrl) |
| 506 | .header("User-Agent", Utils.getCustomUA()) | 506 | .header("User-Agent", Utils.getCustomUA()) |
| 507 | .header("Content-Type", "application/json") | 507 | .header("Content-Type", "application/json") |
| 508 | .post(requestBody); | 508 | .post(requestBody); |
| 509 | 509 | ||
| 510 | OkHttpClient httpClient = new OkHttpClient.Builder() | 510 | OkHttpClient httpClient = new OkHttpClient.Builder() |
| 511 | .readTimeout(30, TimeUnit.SECONDS) | 511 | .readTimeout(30, TimeUnit.SECONDS) |
| 512 | .writeTimeout(30, TimeUnit.SECONDS) | 512 | .writeTimeout(30, TimeUnit.SECONDS) |
| 513 | .connectTimeout(30, TimeUnit.SECONDS) | 513 | .connectTimeout(30, TimeUnit.SECONDS) |
| 514 | .build(); | 514 | .build(); |
| 515 | 515 | ||
| 516 | String jsonString = null; | 516 | String jsonString = null; |
| 517 | try { | 517 | try { |
| 518 | Response response = httpClient.newCall(request.build()).execute(); | 518 | Response response = httpClient.newCall(request.build()).execute(); |
| 519 | if (response == null) { | 519 | if (response == null) { |
| 520 | action.onError(null); | 520 | action.onError(null); |
| 521 | Debug.error("Response is null"); | 521 | Debug.error("Response is null"); |
| 522 | return null; | 522 | return null; |
| 523 | } | 523 | } |
| 524 | jsonString = getResponseData(response.body()); | 524 | jsonString = getResponseData(response.body()); |
| 525 | 525 | ||
| 526 | } catch (IOException e) { | 526 | } catch (IOException e) { |
| 527 | Debug.normal("Error %s", e.getMessage()); | 527 | Debug.normal("Error %s", e.getMessage()); |
| 528 | } | 528 | } |
| 529 | return jsonString; | 529 | return jsonString; |
| 530 | } | 530 | } |
| 531 | 531 | ||
| 532 | @Override | 532 | @Override |
| 533 | protected void onCancelled() { | 533 | protected void onCancelled() { |
| 534 | super.onCancelled(); | 534 | super.onCancelled(); |
| 535 | action.onError(null); | 535 | action.onError(null); |
| 536 | } | 536 | } |
| 537 | 537 | ||
| 538 | @Override | 538 | @Override |
| 539 | protected void onPostExecute(String jsonString) { | 539 | protected void onPostExecute(String jsonString) { |
| 540 | super.onPostExecute(jsonString); | 540 | super.onPostExecute(jsonString); |
| 541 | if ((jsonString == null) || (jsonString.isEmpty())) { | 541 | if ((jsonString == null) || (jsonString.isEmpty())) { |
| 542 | action.onError(null); | 542 | action.onError(null); |
| 543 | return; | 543 | return; |
| 544 | } | 544 | } |
| 545 | Debug.normal("String body: "+jsonString); | 545 | Debug.normal("String body: "+jsonString); |
| 546 | 546 | ||
| 547 | try { | 547 | try { |
| 548 | JSONObject jsonObject = new JSONObject(jsonString); | 548 | JSONObject jsonObject = new JSONObject(jsonString); |
| 549 | int status = jsonObject.optInt("status"); | 549 | int status = jsonObject.optInt("status"); |
| 550 | if (status == 1) { | 550 | if (status == 1) { |
| 551 | action.onSuccess(jsonString); | 551 | action.onSuccess(jsonString); |
| 552 | } else { | 552 | } else { |
| 553 | action.onError(new Error(jsonObject.getString("message"))); | 553 | action.onError(new Error(jsonObject.getString("message"))); |
| 554 | } | 554 | } |
| 555 | } catch (JSONException e) { | 555 | } catch (JSONException e) { |
| 556 | action.onError(null); | 556 | action.onError(null); |
| 557 | } | 557 | } |
| 558 | } | 558 | } |
| 559 | 559 | ||
| 560 | } | 560 | } |
| 561 | 561 | ||
| 562 | public static class ForgetPassConfirm extends AsyncTask<Void, Void, String>{ | 562 | public static class ForgetPassConfirm extends AsyncTask<Void, Void, String>{ |
| 563 | private String email; | 563 | private String email; |
| 564 | private String codeConfirm; | 564 | private String codeConfirm; |
| 565 | private LLAPIManagerListener action; | 565 | private LLAPIManagerListener action; |
| 566 | 566 | ||
| 567 | public ForgetPassConfirm(String email, String codeConfirm, LLAPIManagerListener action) { | 567 | public ForgetPassConfirm(String email, String codeConfirm, LLAPIManagerListener action) { |
| 568 | this.email = email; | 568 | this.email = email; |
| 569 | this.codeConfirm = codeConfirm; | 569 | this.codeConfirm = codeConfirm; |
| 570 | this.action = action; | 570 | this.action = action; |
| 571 | } | 571 | } |
| 572 | 572 | ||
| 573 | @Override | 573 | @Override |
| 574 | protected void onPreExecute() { | 574 | protected void onPreExecute() { |
| 575 | super.onPreExecute(); | 575 | super.onPreExecute(); |
| 576 | } | 576 | } |
| 577 | 577 | ||
| 578 | @Override | 578 | @Override |
| 579 | protected String doInBackground(Void... voids) { | 579 | protected String doInBackground(Void... voids) { |
| 580 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_FORGET_PASS_CONFIRM_INFO); | 580 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_FORGET_PASS_CONFIRM_INFO); |
| 581 | JSONObject jsonObject = new JSONObject(); | 581 | JSONObject jsonObject = new JSONObject(); |
| 582 | try { | 582 | try { |
| 583 | //Personal info | 583 | //Personal info |
| 584 | jsonObject.put("email", email); | 584 | jsonObject.put("email", email); |
| 585 | jsonObject.put("code_confirm", codeConfirm); | 585 | jsonObject.put("code_confirm", codeConfirm); |
| 586 | 586 | ||
| 587 | Debug.warn("Json data: " + jsonObject.toString()); | 587 | Debug.warn("Json data: " + jsonObject.toString()); |
| 588 | } catch (JSONException e) { | 588 | } catch (JSONException e) { |
| 589 | Debug.normal("Error ", e.getMessage()); | 589 | Debug.normal("Error ", e.getMessage()); |
| 590 | } | 590 | } |
| 591 | 591 | ||
| 592 | MediaType JSON | 592 | MediaType JSON |
| 593 | = MediaType.parse("application/json"); | 593 | = MediaType.parse("application/json"); |
| 594 | RequestBody requestBody = RequestBody.create(JSON, jsonObject.toString()); | 594 | RequestBody requestBody = RequestBody.create(JSON, jsonObject.toString()); |
| 595 | 595 | ||
| 596 | final Request.Builder request = new Request.Builder() | 596 | final Request.Builder request = new Request.Builder() |
| 597 | .url(baseUrl) | 597 | .url(baseUrl) |
| 598 | .header("User-Agent", Utils.getCustomUA()) | 598 | .header("User-Agent", Utils.getCustomUA()) |
| 599 | .header("Content-Type", "application/json") | 599 | .header("Content-Type", "application/json") |
| 600 | .post(requestBody); | 600 | .post(requestBody); |
| 601 | 601 | ||
| 602 | OkHttpClient httpClient = new OkHttpClient.Builder() | 602 | OkHttpClient httpClient = new OkHttpClient.Builder() |
| 603 | .readTimeout(30, TimeUnit.SECONDS) | 603 | .readTimeout(30, TimeUnit.SECONDS) |
| 604 | .writeTimeout(30, TimeUnit.SECONDS) | 604 | .writeTimeout(30, TimeUnit.SECONDS) |
| 605 | .connectTimeout(30, TimeUnit.SECONDS) | 605 | .connectTimeout(30, TimeUnit.SECONDS) |
| 606 | .build(); | 606 | .build(); |
| 607 | String jsonString = null; | 607 | String jsonString = null; |
| 608 | try { | 608 | try { |
| 609 | Response response = httpClient.newCall(request.build()).execute(); | 609 | Response response = httpClient.newCall(request.build()).execute(); |
| 610 | if (response == null) { | 610 | if (response == null) { |
| 611 | action.onError(null); | 611 | action.onError(null); |
| 612 | Debug.error("Response is null"); | 612 | Debug.error("Response is null"); |
| 613 | return null; | 613 | return null; |
| 614 | } | 614 | } |
| 615 | jsonString = getResponseData(response.body()); | 615 | jsonString = getResponseData(response.body()); |
| 616 | 616 | ||
| 617 | } catch (IOException e) { | 617 | } catch (IOException e) { |
| 618 | Debug.normal("Error %s", e.getMessage()); | 618 | Debug.normal("Error %s", e.getMessage()); |
| 619 | } | 619 | } |
| 620 | return jsonString; | 620 | return jsonString; |
| 621 | } | 621 | } |
| 622 | 622 | ||
| 623 | @Override | 623 | @Override |
| 624 | protected void onCancelled() { | 624 | protected void onCancelled() { |
| 625 | super.onCancelled(); | 625 | super.onCancelled(); |
| 626 | action.onError(null); | 626 | action.onError(null); |
| 627 | } | 627 | } |
| 628 | 628 | ||
| 629 | @Override | 629 | @Override |
| 630 | protected void onPostExecute(String jsonString) { | 630 | protected void onPostExecute(String jsonString) { |
| 631 | super.onPostExecute(jsonString); | 631 | super.onPostExecute(jsonString); |
| 632 | if ((jsonString == null) || (jsonString.isEmpty())) { | 632 | if ((jsonString == null) || (jsonString.isEmpty())) { |
| 633 | action.onError(null); | 633 | action.onError(null); |
| 634 | return; | 634 | return; |
| 635 | } | 635 | } |
| 636 | Debug.normal("String body: "+jsonString); | 636 | Debug.normal("String body: "+jsonString); |
| 637 | 637 | ||
| 638 | try { | 638 | try { |
| 639 | JSONObject jsonObject = new JSONObject(jsonString); | 639 | JSONObject jsonObject = new JSONObject(jsonString); |
| 640 | int status = jsonObject.optInt("status"); | 640 | int status = jsonObject.optInt("status"); |
| 641 | if (status == 1) { | 641 | if (status == 1) { |
| 642 | action.onSuccess(jsonString); | 642 | action.onSuccess(jsonString); |
| 643 | } else { | 643 | } else { |
| 644 | action.onError(new Error(jsonObject.getString("message"))); | 644 | action.onError(new Error(jsonObject.getString("message"))); |
| 645 | } | 645 | } |
| 646 | } catch (JSONException e) { | 646 | } catch (JSONException e) { |
| 647 | action.onError(null); | 647 | action.onError(null); |
| 648 | } | 648 | } |
| 649 | } | 649 | } |
| 650 | } | 650 | } |
| 651 | 651 | ||
| 652 | public static class History extends AsyncTask<Void, Void, String>{ | 652 | public static class History extends AsyncTask<Void, Void, String>{ |
| 653 | private Date fromDate; | 653 | private Date fromDate; |
| 654 | private Date toDate; | 654 | private Date toDate; |
| 655 | private LLAPIManagerListener action; | 655 | private LLAPIManagerListener action; |
| 656 | 656 | ||
| 657 | public History(Date fromDate, Date toDate, LLAPIManagerListener action) { | 657 | public History(Date fromDate, Date toDate, LLAPIManagerListener action) { |
| 658 | this.fromDate = fromDate; | 658 | this.fromDate = fromDate; |
| 659 | this.toDate = toDate; | 659 | this.toDate = toDate; |
| 660 | this.action = action; | 660 | this.action = action; |
| 661 | } | 661 | } |
| 662 | 662 | ||
| 663 | @Override | 663 | @Override |
| 664 | protected void onPreExecute() { | 664 | protected void onPreExecute() { |
| 665 | super.onPreExecute(); | 665 | super.onPreExecute(); |
| 666 | } | 666 | } |
| 667 | 667 | ||
| 668 | @Override | 668 | @Override |
| 669 | protected String doInBackground(Void... voids) { | 669 | protected String doInBackground(Void... voids) { |
| 670 | 670 | ||
| 671 | String fromStr = Utils.dateToStringFormatYearMonthDay(fromDate); | 671 | String fromStr = Utils.dateToStringFormatYearMonthDay(fromDate); |
| 672 | String toStr = Utils.dateToStringFormatYearMonthDay(toDate); | 672 | String toStr = Utils.dateToStringFormatYearMonthDay(toDate); |
| 673 | Debug.normal("Token: "+ APIResponse.getInstance().getToken()); | 673 | Debug.normal("Token: "+ APIResponse.getInstance().getToken()); |
| 674 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_HISTORE_INFO + "/"+ fromStr +"/"+ toStr); | 674 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_HISTORE_INFO + "/"+ fromStr +"/"+ toStr); |
| 675 | Debug.normal("URL: "+baseUrl.toString()); | 675 | Debug.normal("URL: "+baseUrl.toString()); |
| 676 | final Request.Builder request = new Request.Builder() | 676 | final Request.Builder request = new Request.Builder() |
| 677 | .url(baseUrl) | 677 | .url(baseUrl) |
| 678 | .header("User-Agent", Utils.getCustomUA()) | 678 | .header("User-Agent", Utils.getCustomUA()) |
| 679 | .header("Content-Type", "application/json") | 679 | .header("Content-Type", "application/json") |
| 680 | .addHeader("token", APIResponse.getInstance().getToken()) | 680 | .addHeader("token", APIResponse.getInstance().getToken()) |
| 681 | .get(); | 681 | .get(); |
| 682 | 682 | ||
| 683 | OkHttpClient httpClient = new OkHttpClient.Builder() | 683 | OkHttpClient httpClient = new OkHttpClient.Builder() |
| 684 | .readTimeout(30, TimeUnit.SECONDS) | 684 | .readTimeout(30, TimeUnit.SECONDS) |
| 685 | .writeTimeout(30, TimeUnit.SECONDS) | 685 | .writeTimeout(30, TimeUnit.SECONDS) |
| 686 | .connectTimeout(30, TimeUnit.SECONDS) | 686 | .connectTimeout(30, TimeUnit.SECONDS) |
| 687 | .build(); | 687 | .build(); |
| 688 | 688 | ||
| 689 | String jsonString = null; | 689 | String jsonString = null; |
| 690 | try { | 690 | try { |
| 691 | Response response = httpClient.newCall(request.build()).execute(); | 691 | Response response = httpClient.newCall(request.build()).execute(); |
| 692 | if (response == null) { | 692 | if (response == null) { |
| 693 | action.onError(null); | 693 | action.onError(null); |
| 694 | Debug.error("Response is null"); | 694 | Debug.error("Response is null"); |
| 695 | return null; | 695 | return null; |
| 696 | } | 696 | } |
| 697 | jsonString = getResponseData(response.body()); | 697 | jsonString = getResponseData(response.body()); |
| 698 | 698 | ||
| 699 | } catch (IOException e) { | 699 | } catch (IOException e) { |
| 700 | Debug.normal("Error %s", e.getMessage()); | 700 | Debug.normal("Error %s", e.getMessage()); |
| 701 | } | 701 | } |
| 702 | return jsonString; | 702 | return jsonString; |
| 703 | } | 703 | } |
| 704 | 704 | ||
| 705 | @Override | 705 | @Override |
| 706 | protected void onCancelled() { | 706 | protected void onCancelled() { |
| 707 | super.onCancelled(); | 707 | super.onCancelled(); |
| 708 | action.onError(null); | 708 | action.onError(null); |
| 709 | } | 709 | } |
| 710 | 710 | ||
| 711 | @Override | 711 | @Override |
| 712 | protected void onPostExecute(String jsonString) { | 712 | protected void onPostExecute(String jsonString) { |
| 713 | super.onPostExecute(jsonString); | 713 | super.onPostExecute(jsonString); |
| 714 | if ((jsonString == null) || (jsonString.isEmpty())) { | 714 | if ((jsonString == null) || (jsonString.isEmpty())) { |
| 715 | action.onError(null); | 715 | action.onError(null); |
| 716 | return; | 716 | return; |
| 717 | } | 717 | } |
| 718 | Debug.normal("String body: "+jsonString); | 718 | Debug.normal("String body: "+jsonString); |
| 719 | 719 | ||
| 720 | try { | 720 | try { |
| 721 | JSONObject jsonObject = new JSONObject(jsonString); | 721 | JSONObject jsonObject = new JSONObject(jsonString); |
| 722 | int status = jsonObject.optInt("status"); | 722 | int status = jsonObject.optInt("status"); |
| 723 | if (status == 1) { | 723 | if (status == 1) { |
| 724 | action.onSuccess(jsonString); | 724 | action.onSuccess(jsonString); |
| 725 | } else { | 725 | } else { |
| 726 | action.onError(new Error(jsonObject.getString("message"))); | 726 | action.onError(new Error(jsonObject.getString("message"))); |
| 727 | } | 727 | } |
| 728 | } catch (JSONException e) { | 728 | } catch (JSONException e) { |
| 729 | action.onError(null); | 729 | action.onError(null); |
| 730 | } | 730 | } |
| 731 | } | 731 | } |
| 732 | } | 732 | } |
| 733 | 733 | ||
| 734 | public static class HistoryDetail extends AsyncTask<Void, Void, String>{ | 734 | public static class HistoryDetail extends AsyncTask<Void, Void, String>{ |
| 735 | private Date fromDate; | 735 | private Date fromDate; |
| 736 | private Date toDate; | 736 | private Date toDate; |
| 737 | private LLAPIManagerListener action; | 737 | private LLAPIManagerListener action; |
| 738 | 738 | ||
| 739 | public HistoryDetail(Date fromDate, Date toDate, LLAPIManagerListener action) { | 739 | public HistoryDetail(Date fromDate, Date toDate, LLAPIManagerListener action) { |
| 740 | this.fromDate = fromDate; | 740 | this.fromDate = fromDate; |
| 741 | this.toDate = toDate; | 741 | this.toDate = toDate; |
| 742 | this.action = action; | 742 | this.action = action; |
| 743 | } | 743 | } |
| 744 | 744 | ||
| 745 | @Override | 745 | @Override |
| 746 | protected void onPreExecute() { | 746 | protected void onPreExecute() { |
| 747 | super.onPreExecute(); | 747 | super.onPreExecute(); |
| 748 | } | 748 | } |
| 749 | 749 | ||
| 750 | @Override | 750 | @Override |
| 751 | protected String doInBackground(Void... voids) { | 751 | protected String doInBackground(Void... voids) { |
| 752 | 752 | ||
| 753 | String fromStr = Utils.dateToStringFormatDayMonthYear(fromDate); | 753 | String fromStr = Utils.dateToStringFormatDayMonthYear(fromDate); |
| 754 | String toStr = Utils.dateToStringFormatDayMonthYear(toDate); | 754 | String toStr = Utils.dateToStringFormatDayMonthYear(toDate); |
| 755 | Debug.normal("Token: "+ APIResponse.getInstance().getToken()); | 755 | Debug.normal("Token: "+ APIResponse.getInstance().getToken()); |
| 756 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_HISTORE_DETAIL_INFO + "/"+ fromStr +"/"+ toStr); | 756 | HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_HISTORE_DETAIL_INFO + "/"+ fromStr +"/"+ toStr); |
| 757 | Debug.normal("URL: "+baseUrl.toString()); | 757 | Debug.normal("URL: "+baseUrl.toString()); |
| 758 | final Request.Builder request = new Request.Builder() | 758 | final Request.Builder request = new Request.Builder() |
| 759 | .url(baseUrl) | 759 | .url(baseUrl) |
| 760 | .header("User-Agent", Utils.getCustomUA()) | 760 | .header("User-Agent", Utils.getCustomUA()) |
| 761 | .header("Content-Type", "application/json") | 761 | .header("Content-Type", "application/json") |
| 762 | .addHeader("token", APIResponse.getInstance().getToken()) | 762 | .addHeader("token", APIResponse.getInstance().getToken()) |
| 763 | .get(); | 763 | .get(); |
| 764 | 764 | ||
| 765 | OkHttpClient httpClient = new OkHttpClient.Builder() | 765 | OkHttpClient httpClient = new OkHttpClient.Builder() |
| 766 | .readTimeout(30, TimeUnit.SECONDS) | 766 | .readTimeout(30, TimeUnit.SECONDS) |
| 767 | .writeTimeout(30, TimeUnit.SECONDS) | 767 | .writeTimeout(30, TimeUnit.SECONDS) |
| 768 | .connectTimeout(30, TimeUnit.SECONDS) | 768 | .connectTimeout(30, TimeUnit.SECONDS) |
| 769 | .build(); | 769 | .build(); |
| 770 | 770 | ||
| 771 | String jsonString = null; | 771 | String jsonString = null; |