Commit 9c431985f72992e326242adb85697b35bb2a132b

Authored by Dinh Chu
1 parent 260f822ff9
Exists in master

ipdate

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

app/src/main/AndroidManifest.xml
... ... @@ -10,6 +10,7 @@
10 10 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
11 11 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
12 12 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  13 + <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
13 14 <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
14 15 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
15 16 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
app/src/main/java/com/dinhcv/lifelogpedometer/activity/PedometerActivity.java
... ... @@ -27,6 +27,7 @@
27 27  
28 28 import com.dinhcv.lifelogpedometer.R;
29 29 import com.dinhcv.lifelogpedometer.model.Shareprefer.Setting;
  30 +import com.dinhcv.lifelogpedometer.portal.APIResponse;
30 31 import com.dinhcv.lifelogpedometer.utils.Debug;
31 32 import com.dinhcv.lifelogpedometer.utils.Utils;
32 33  
... ... @@ -397,6 +398,7 @@
397 398 private void logout(){
398 399 // clear data
399 400 Setting.clearUserIdSharepre(PedometerActivity.this);
  401 + APIResponse.getInstance().setToken(null);
400 402 // go to login
401 403 Intent intent = new Intent(PedometerActivity.this, LoginActivity.class);
402 404 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
app/src/main/java/com/dinhcv/lifelogpedometer/activity/RegisterActivity.java
... ... @@ -323,11 +323,12 @@
323 323 }
324 324  
325 325 @Override
326   - public void onSuccess(String json) {
327   - if (json != null) {
  326 + public void onSuccess(String urlImage) {
  327 + if (urlImage != null) {
  328 + progressDialog.dismiss();
328 329 Debug.warn(" Upload data success success");
329   - Debug.warn("DATA JSON result: " + json.toString());
330   - uploadImageDone(json);
  330 + Debug.warn("DATA JSON result: " + urlImage);
  331 + uploadImageDone(urlImage);
331 332 } else {
332 333 progressDialog.dismiss();
333 334 Debug.warn(" Upload data fail: response null");
app/src/main/java/com/dinhcv/lifelogpedometer/activity/WelcomeActivity.java
... ... @@ -116,6 +116,7 @@
116 116 if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
117 117 if ((ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
118 118 == PackageManager.PERMISSION_GRANTED) && (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
  119 + == PackageManager.PERMISSION_GRANTED)&& (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
119 120 == PackageManager.PERMISSION_GRANTED)) {
120 121 checkRefreshToken();
121 122  
122 123  
123 124  
... ... @@ -124,16 +125,17 @@
124 125 if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission
125 126 .ACCESS_COARSE_LOCATION)
126 127 && ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest
  128 + .permission.ACCESS_FINE_LOCATION) && ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest
127 129 .permission.ACCESS_FINE_LOCATION)) {
128 130 ActivityCompat.requestPermissions(this,
129 131 new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
130   - Manifest.permission.ACCESS_FINE_LOCATION},
  132 + Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE},
131 133 PERMISSIONS_REQUEST);
132 134  
133 135 } else {
134 136 ActivityCompat.requestPermissions(this,
135 137 new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
136   - Manifest.permission.ACCESS_FINE_LOCATION},
  138 + Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE},
137 139 PERMISSIONS_REQUEST);
138 140 }
139 141 }
app/src/main/java/com/dinhcv/lifelogpedometer/portal/LLAPIManager.java
... ... @@ -5,9 +5,9 @@
5 5 import android.graphics.Bitmap;
6 6 import android.graphics.BitmapFactory;
7 7 import android.os.AsyncTask;
8   -import android.provider.Settings;
9   -import android.support.annotation.NonNull;
  8 +import android.os.Environment;
10 9 import android.util.Base64;
  10 +import android.util.Log;
11 11  
12 12 import com.dinhcv.lifelogpedometer.interfaces.LLAPIManagerListener;
13 13 import com.dinhcv.lifelogpedometer.model.Shareprefer.Setting;
14 14  
15 15  
... ... @@ -21,9 +21,16 @@
21 21 import org.json.JSONObject;
22 22  
23 23 import java.io.ByteArrayOutputStream;
  24 +import java.io.File;
  25 +import java.io.FileOutputStream;
24 26 import java.io.IOException;
25 27 import java.io.InputStream;
  28 +import java.io.OutputStreamWriter;
  29 +import java.io.UnsupportedEncodingException;
  30 +import java.net.URLEncoder;
26 31 import java.util.Date;
  32 +import java.util.HashMap;
  33 +import java.util.Map;
27 34 import java.util.concurrent.TimeUnit;
28 35  
29 36 import okhttp3.HttpUrl;
30 37  
31 38  
32 39  
33 40  
34 41  
35 42  
36 43  
37 44  
... ... @@ -182,55 +189,37 @@
182 189  
183 190 @Override
184 191 protected String doInBackground(Void... voids) {
185   - HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_UPLOAD_IMAGE_INFO);
186   - JSONObject jsonObject = new JSONObject();
187   - byte[] bitmapByte = convertBitmapToByteArray(bitmap);
  192 + String baseUrl = URL_ROOT + URL_UPLOAD_IMAGE_INFO;
  193 +
  194 + Bitmap b = Bitmap.createScaledBitmap(bitmap, 120, 120, false);
  195 + byte[] bitmapByte = convertBitmapToByteArray(b);
188 196 String imageBase64 = Base64.encodeToString(bitmapByte, Base64.DEFAULT);
189   - String imageStr = imageBase64.replace("\n","");
190 197  
191 198 String token = APIResponse.getInstance().getToken();
192   - try {
193   - if (token != null && !token.isEmpty()) {
194   - Debug.normal("Token is not null");
195   - jsonObject.put("token", token);
196   - }
197   - jsonObject.put("img", imageStr);
198   -
199   - Debug.warn("Json data: " + jsonObject.toString());
200   - } catch (JSONException e) {
201   - Debug.normal("Error ", e.getMessage());
  199 + //generate hashMap to store encodedImage and the name
  200 + HashMap<String,String> detail = new HashMap<>();
  201 + detail.put("img", imageBase64);
  202 + if (token != null && !token.isEmpty()) {
  203 + Debug.normal("Token is not null");
  204 + detail.put("token", token);
202 205 }
203 206  
204   - MediaType JSON
205   - = MediaType.parse("application/json; charset=utf-8");
206   - RequestBody requestBody = RequestBody.create(JSON, jsonObject.toString());
207   -
208   - final Request.Builder request = new Request.Builder()
209   - .url(baseUrl)
210   - .header("User-Agent", Utils.getCustomUA())
211   - .header("Content-Type", "application/json")
212   - .post(requestBody);
213   -
214   - OkHttpClient httpClient = new OkHttpClient.Builder()
215   - .readTimeout(30, TimeUnit.SECONDS)
216   - .writeTimeout(30, TimeUnit.SECONDS)
217   - .connectTimeout(30, TimeUnit.SECONDS)
218   - .build();
219   - String jsonString = null;
220 207 try {
221   - Response response = httpClient.newCall(request.build()).execute();
  208 + String dataToSend = hashMapToUrl(detail);
  209 +
  210 + String response = RequestPostImage.post(baseUrl,dataToSend);
222 211 if (response == null) {
223 212 action.onError(null);
224 213 Debug.error("Response is null");
225 214 return null;
226 215 }
227   - jsonString = getResponseData(response.body());
228 216  
  217 + Debug.normal("Response: "+response);
  218 + return response;
229 219 } catch (IOException e) {
230 220 Debug.normal("Error %s", e.getMessage());
  221 + return null;
231 222 }
232   -
233   - return jsonString;
234 223 }
235 224  
236 225 @Override
... ... @@ -250,9 +239,9 @@
250 239  
251 240 try {
252 241 JSONObject jsonObject = new JSONObject(jsonString);
253   - int status = jsonObject.optInt("status");
254   - if (status == 1){
255   - action.onSuccess(new JSONObject());
  242 + int status = jsonObject.optInt("httpCode");
  243 + if (status != 0){
  244 + action.onSuccess(jsonObject.optString("message"));
256 245 }else {
257 246 action.onError(new Error(jsonObject.optString("message")));
258 247 }
... ... @@ -262,6 +251,104 @@
262 251 }
263 252 }
264 253  
  254 + public static class UploadImage1 extends AsyncTask<Void, Void, String>{
  255 + private Bitmap bitmap;
  256 + private LLAPIManagerListener action;
  257 +
  258 + public UploadImage1(Bitmap bitmap, LLAPIManagerListener action) {
  259 + this.bitmap = bitmap;
  260 + this.action = action;
  261 + }
  262 +
  263 + @Override
  264 + protected void onPreExecute() {
  265 + super.onPreExecute();
  266 + }
  267 +
  268 + @Override
  269 + protected String doInBackground(Void... voids) {
  270 + HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_UPLOAD_IMAGE_INFO);
  271 + JSONObject jsonObject = new JSONObject();
  272 + Bitmap b = Bitmap.createScaledBitmap(bitmap, 120, 120, false);
  273 + byte[] bitmapByte = convertBitmapToByteArray(b);
  274 + String imageBase64 = Base64.encodeToString(bitmapByte, Base64.DEFAULT);
  275 + //String imageStr = imageBase64.replace("\n","");
  276 + //writeToFile(imageBase64);
  277 +
  278 + String token = APIResponse.getInstance().getToken();
  279 + try {
  280 + if (token != null && !token.isEmpty()) {
  281 + Debug.normal("Token is not null");
  282 + jsonObject.put("token", token);
  283 + }
  284 + jsonObject.put("img", imageBase64);
  285 +
  286 + //Debug.warn("Json data: " + jsonObject.toString());
  287 + } catch (JSONException e) {
  288 + Debug.normal("Error ", e.getMessage());
  289 + }
  290 +
  291 + MediaType JSON
  292 + = MediaType.parse("application/json; charset=utf-8");
  293 + RequestBody requestBody = RequestBody.create(JSON, jsonObject.toString());
  294 +
  295 + final Request.Builder request = new Request.Builder()
  296 + .url(baseUrl)
  297 + .header("Content-Type", "application/json")
  298 + .post(requestBody);
  299 +
  300 + OkHttpClient httpClient = new OkHttpClient.Builder()
  301 + .readTimeout(30, TimeUnit.SECONDS)
  302 + .writeTimeout(30, TimeUnit.SECONDS)
  303 + .connectTimeout(30, TimeUnit.SECONDS)
  304 + .build();
  305 + String jsonString = null;
  306 + try {
  307 + Response response = httpClient.newCall(request.build()).execute();
  308 + if (response == null) {
  309 + action.onError(null);
  310 + Debug.error("Response is null");
  311 + return null;
  312 + }
  313 + jsonString = getResponseData(response.body());
  314 +
  315 + } catch (IOException e) {
  316 + Debug.normal("Error %s", e.getMessage());
  317 + }
  318 +
  319 + return jsonString;
  320 + }
  321 +
  322 + @Override
  323 + protected void onCancelled() {
  324 + super.onCancelled();
  325 + action.onError(null);
  326 + }
  327 +
  328 + @Override
  329 + protected void onPostExecute(String jsonString) {
  330 + super.onPostExecute(jsonString);
  331 + if ((jsonString == null) || (jsonString.isEmpty())) {
  332 + action.onError(null);
  333 + return;
  334 + }
  335 + Debug.normal("String body: "+jsonString);
  336 +
  337 + try {
  338 + JSONObject jsonObject = new JSONObject(jsonString);
  339 + int status = jsonObject.optInt("status");
  340 + if (status == 1){
  341 + action.onSuccess(new JSONObject());
  342 + }else {
  343 + action.onError(new Error(jsonObject.optString("message")));
  344 + }
  345 + } catch (JSONException e) {
  346 + e.printStackTrace();
  347 + }
  348 + }
  349 + }
  350 +
  351 +
265 352 public static class Register extends AsyncTask<Void, Void, String>{
266 353 private RegisterInfo registerInfo;
267 354 private LLAPIManagerListener action;
268 355  
269 356  
270 357  
271 358  
... ... @@ -285,23 +372,24 @@
285 372 try {
286 373 jsonObject.put("username", registerInfo.getUsername());
287 374 jsonObject.put("password", registerInfo.getPassword());
  375 + jsonObject.put("password_confirmation", registerInfo.getPassword());
288 376 jsonObject.put("full_name", registerInfo.getFullName());
289   - jsonObject.put("nickname", registerInfo.getNickname());
  377 + //jsonObject.put("nickname", registerInfo.getNickname());
290 378 jsonObject.put("birthday", Utils.dateToStringFormatDayMonthYear(registerInfo.getBirthday()));
291 379 jsonObject.put("height", ""+registerInfo.getHeight());
292 380 jsonObject.put("weight", ""+registerInfo.getWeight());
293 381 jsonObject.put("fat_rate", ""+registerInfo.getFatRate());
294 382 jsonObject.put("gender", ""+registerInfo.getGender());
295   - jsonObject.put("address", registerInfo.getAddress());
296   - jsonObject.put("desciption", "");
  383 + //jsonObject.put("address", registerInfo.getAddress());
  384 + //jsonObject.put("desciption", "");
297 385 jsonObject.put("receive_notification", "0");
298 386 //jsonObject.put("phone", "+84986940999");
299 387 jsonObject.put("share_data", "1");
300 388 jsonObject.put("profile_image", ""+registerInfo.getProfileImage());
301   - jsonObject.put("delete_flag", "0");
  389 + //jsonObject.put("delete_flag", "0");
302 390 jsonObject.put("email", registerInfo.getEmail());
303 391 jsonObject.put("physical_activity", "0");
304   - jsonObject.put("remember_me", "1");
  392 + //jsonObject.put("remember", "1");
305 393  
306 394 Debug.warn("Json data: " + jsonObject.toString());
307 395 } catch (JSONException e) {
... ... @@ -1190,5 +1278,52 @@
1190 1278 return unzipString;
1191 1279 }
1192 1280  
  1281 +
  1282 + public static void writeToFile(String data)
  1283 + {
  1284 + String path =
  1285 + Environment.getExternalStorageDirectory() + File.separator + "yourFolder";
  1286 + // Create the folder.
  1287 + File folder = new File(path);
  1288 + folder.mkdirs();
  1289 +
  1290 + // Create the file.
  1291 + File file = new File(folder, "config.txt");
  1292 + // Save your stream, don't forget to flush() it before closing it.
  1293 +
  1294 + try
  1295 + {
  1296 + file.createNewFile();
  1297 + FileOutputStream fOut = new FileOutputStream(file);
  1298 + OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
  1299 + myOutWriter.append(data);
  1300 +
  1301 + myOutWriter.close();
  1302 +
  1303 + fOut.flush();
  1304 + fOut.close();
  1305 + }
  1306 + catch (IOException e)
  1307 + {
  1308 + Log.e("Exception", "File write failed: " + e.toString());
  1309 + }
  1310 + }
  1311 +
  1312 + private static String hashMapToUrl(HashMap<String, String> params) throws UnsupportedEncodingException {
  1313 + StringBuilder result = new StringBuilder();
  1314 + boolean first = true;
  1315 + for(Map.Entry<String, String> entry : params.entrySet()){
  1316 + if (first)
  1317 + first = false;
  1318 + else
  1319 + result.append("&");
  1320 +
  1321 + result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
  1322 + result.append("=");
  1323 + result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
  1324 + }
  1325 +
  1326 + return result.toString();
  1327 + }
1193 1328 }
app/src/main/java/com/dinhcv/lifelogpedometer/portal/RequestPostImage.java
  1 +package com.dinhcv.lifelogpedometer.portal;
  2 +
  3 +/**
  4 + * Created by Administrator on 25/12/2017.
  5 + */
  6 +import android.util.Log;
  7 +
  8 +import java.io.BufferedReader;
  9 +import java.io.BufferedWriter;
  10 +import java.io.IOException;
  11 +import java.io.InputStreamReader;
  12 +import java.io.OutputStream;
  13 +import java.io.OutputStreamWriter;
  14 +import java.net.HttpURLConnection;
  15 +import java.net.URL;
  16 +
  17 +/**
  18 + * class to make Http RequestPostImage to the web
  19 + */
  20 +public class RequestPostImage {
  21 +
  22 + private static final String TAG = RequestPostImage.class.getSimpleName();
  23 +
  24 + public static String post(String serverUrl,String dataToSend){
  25 + try {
  26 + URL url = new URL(serverUrl);
  27 + HttpURLConnection con = (HttpURLConnection) url.openConnection();
  28 + //set timeout of 30 seconds
  29 + con.setConnectTimeout(1000 * 30);
  30 + con.setReadTimeout(1000 * 30);
  31 + //method
  32 + con.setRequestMethod("POST");
  33 + con.setDoOutput(true);
  34 +
  35 + OutputStream os = con.getOutputStream();
  36 + BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
  37 +
  38 + //make request
  39 + writer.write(dataToSend);
  40 + writer.flush();
  41 + writer.close();
  42 + os.close();
  43 +
  44 + //get the response
  45 + int responseCode = con.getResponseCode();
  46 +
  47 + if(responseCode == HttpURLConnection.HTTP_OK){
  48 + //read the response
  49 + StringBuilder sb = new StringBuilder();
  50 +
  51 + BufferedReader reader = new BufferedReader(
  52 + new InputStreamReader(con.getInputStream()));
  53 + String line;
  54 +
  55 + //loop through the response from the server
  56 + while ((line = reader.readLine()) != null){
  57 + sb.append(line).append("\n");
  58 + }
  59 +
  60 + //return the response
  61 + return sb.toString();
  62 + }else{
  63 + Log.e(TAG,"ERROR - Invalid response code from server "+ responseCode);
  64 + return null;
  65 + }
  66 +
  67 + } catch (IOException e) {
  68 + e.printStackTrace();
  69 + Log.e(TAG,"ERROR "+e);
  70 + return null;
  71 + }
  72 + }
  73 +}
app/src/main/res/drawable/add_ic.PNG

2.33 KB | W: | H:

838 Bytes | W: | H:

  • 2-up
  • Swipe
  • Onion skin
app/src/main/res/layout/activity_register.xml
... ... @@ -4,6 +4,7 @@
4 4 android:layout_height="match_parent"
5 5 android:gravity="center_horizontal"
6 6 android:orientation="vertical"
  7 + android:background="@drawable/bg"
7 8 tools:context="com.dinhcv.lifelogpedometer.activity.LoginActivity">
8 9  
9 10 <include layout="@layout/layout_toolbar" />
... ... @@ -19,7 +20,6 @@
19 20 android:id="@+id/email_login_form"
20 21 android:layout_width="match_parent"
21 22 android:layout_height="wrap_content"
22   - android:background="@color/background_main"
23 23 android:orientation="vertical"
24 24 android:paddingTop="30dp">
25 25  
... ... @@ -36,7 +36,7 @@
36 36 android:layout_height="@dimen/add_img_height"
37 37 android:src="@drawable/add_ic"
38 38 app:civ_border_width="2dp"
39   - app:civ_border_color="#FF000000"/>
  39 + app:civ_border_color="#FFFFFF"/>
40 40  
41 41 <LinearLayout
42 42 android:layout_width="match_parent"