diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 5ffd52a..04d9a44 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -8,7 +8,6 @@ - diff --git a/app/build.gradle b/app/build.gradle index 42f2d41..f212176 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -6,7 +6,7 @@ android { defaultConfig { minSdkVersion 19 targetSdkVersion 24 - applicationId "com.journaldev.gpslocationtracking" + applicationId "com.redp.geotrack" versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" @@ -17,6 +17,10 @@ android { proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } } dependencies { diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f8cef6c..3f0eddf 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -4,6 +4,7 @@ + queryParams; + private String charset; + + /** + * This constructor initializes a new HTTP POST request with content type + * is set to multipart/form-data + * + * @param requestURL + * @param charset + * @param headers + * @param queryParams + * @throws IOException + */ + public HttpPostForm(String requestURL, String charset, Map headers, Map queryParams) throws IOException { + this.charset = charset; + if (queryParams == null) { + this.queryParams = new HashMap<>(); + } else { + this.queryParams = queryParams; + } + URL url = new URL(requestURL); + httpConn = (HttpURLConnection) url.openConnection(); + httpConn.setUseCaches(false); + httpConn.setDoOutput(true); // indicates POST method + httpConn.setDoInput(true); + httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); + if (headers != null && headers.size() > 0) { + Iterator it = headers.keySet().iterator(); + while (it.hasNext()) { + String key = it.next(); + String value = headers.get(key); + httpConn.setRequestProperty(key, value); + } + } + } + + public HttpPostForm(String requestURL, String charset, Map headers) throws IOException { + this(requestURL, charset, headers, null); + } + + public HttpPostForm(String requestURL, String charset) throws IOException { + this(requestURL, charset, null, null); + } + + /** + * Adds a form field to the request + * + * @param name field name + * @param value field value + */ + public void addFormField(String name, Object value) { + queryParams.put(name, value); + } + + /** + * Adds a header to the request + * + * @param key + * @param value + */ + public void addHeader(String key, String value) { + httpConn.setRequestProperty(key, value); + } + + /** + * Convert the request fields to a byte array + * + * @param params + * @return + */ + private byte[] getParamsByte(Map params) { + byte[] result = null; + StringBuilder postData = new StringBuilder(); + for (Map.Entry param : params.entrySet()) { + if (postData.length() != 0) { + postData.append('&'); + } + postData.append(this.encodeParam(param.getKey())); + postData.append('='); + postData.append(this.encodeParam(String.valueOf(param.getValue()))); + } + try { + result = postData.toString().getBytes("UTF-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + return result; + } + + /** + * URL-encoding keys and values + * + * @param data + * @return + */ + private String encodeParam(String data) { + String result = ""; + try { + result = URLEncoder.encode(data, "UTF-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + return result; + } + + /** + * Completes the request and receives response from the server. + * + * @return String as response in case the server returned + * status OK, otherwise an exception is thrown. + * @throws IOException + */ + public String finish() throws IOException { + String response = ""; + byte[] postDataBytes = this.getParamsByte(queryParams); + httpConn.getOutputStream().write(postDataBytes); + // Check the http status + int status = httpConn.getResponseCode(); + if (status == HttpURLConnection.HTTP_OK) { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024]; + int length; + while ((length = httpConn.getInputStream().read(buffer)) != -1) { + result.write(buffer, 0, length); + } + response = result.toString(this.charset); + httpConn.disconnect(); + } else { + throw new IOException("Server returned non-OK status: " + status); + } + return response; + } +} \ No newline at end of file diff --git a/app/src/main/java/com/journaldev/gpslocationtracking/MainActivity.java b/app/src/main/java/com/journaldev/gpslocationtracking/MainActivity.java index 3e45285..6a96c31 100644 --- a/app/src/main/java/com/journaldev/gpslocationtracking/MainActivity.java +++ b/app/src/main/java/com/journaldev/gpslocationtracking/MainActivity.java @@ -4,6 +4,7 @@ import android.annotation.TargetApi; import android.content.DialogInterface; import android.content.pm.PackageManager; import android.os.Build; +import android.os.StrictMode; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; @@ -13,6 +14,9 @@ import android.widget.Button; import android.widget.Toast; import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; import static android.Manifest.permission.ACCESS_COARSE_LOCATION; import static android.Manifest.permission.ACCESS_FINE_LOCATION; @@ -26,6 +30,9 @@ public class MainActivity extends AppCompatActivity { private final static int ALL_PERMISSIONS_RESULT = 101; LocationTrack locationTrack; + HashMap listadoCoordenadas = new HashMap(); + + SendMessage sendMessage = new SendMessage(); @Override protected void onCreate(Bundle savedInstanceState) { @@ -56,6 +63,7 @@ public class MainActivity extends AppCompatActivity { public void onClick(View view) { locationTrack = new LocationTrack(MainActivity.this); + FIleManager fileManager = new FIleManager(); if (locationTrack.canGetLocation()) { @@ -64,7 +72,12 @@ public class MainActivity extends AppCompatActivity { double longitude = locationTrack.getLongitude(); double latitude = locationTrack.getLatitude(); - Toast.makeText(getApplicationContext(), "Longitude:" + Double.toString(longitude) + "\nLatitude:" + Double.toString(latitude), Toast.LENGTH_SHORT).show(); +// Toast.makeText(getApplicationContext(), "Longitude:" + Double.toString(longitude) + "\nLatitude:" + Double.toString(latitude), Toast.LENGTH_SHORT).show(); + Toast.makeText(getApplicationContext(), "Latitud:" + Double.toString(latitude) + "\nLongitud:" + Double.toString(longitude), Toast.LENGTH_SHORT).show(); + fileManager.writeToFile("Latitud: "+Double.toString(latitude) + "\nLongitud:" + Double.toString(longitude), MainActivity.this); + + listadoCoordenadas.put(Double.toString(latitude), Double.toString(longitude)); + } else { locationTrack.showSettingsAlert(); @@ -73,9 +86,42 @@ public class MainActivity extends AppCompatActivity { } }); + Button btn2 = (Button) findViewById(R.id.btn2); + btn2.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + locationTrack = new LocationTrack(MainActivity.this); + double longitude = locationTrack.getLongitude(); + double latitude = locationTrack.getLatitude(); + + Toast.makeText(getApplicationContext(), "Guardando datos", Toast.LENGTH_SHORT).show(); + //fileManager.writeToFile("Latitud: "+Double.toString(latitude) + "\nLongitud:" + Double.toString(longitude), MainActivity.this); + + if (android.os.Build.VERSION.SDK_INT > 9) + { + StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); + StrictMode.setThreadPolicy(policy); + } + + + sendMessage.sendMessage(Double.toString(latitude), Double.toString(longitude)); + Iterator it = listadoCoordenadas.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry pair = (Map.Entry)it.next(); + System.out.println("control"+pair.getKey() + " = " + pair.getValue()); + it.remove(); // avoids a ConcurrentModificationException + } + + + } + }); + + } + + private ArrayList findUnAskedPermissions(ArrayList wanted) { ArrayList result = new ArrayList(); diff --git a/app/src/main/java/com/journaldev/gpslocationtracking/SendMessage.java b/app/src/main/java/com/journaldev/gpslocationtracking/SendMessage.java new file mode 100644 index 0000000..6532d55 --- /dev/null +++ b/app/src/main/java/com/journaldev/gpslocationtracking/SendMessage.java @@ -0,0 +1,25 @@ +package com.journaldev.gpslocationtracking; + +import java.util.HashMap; +import java.util.Map; + +public class SendMessage { + + public void sendMessage(String longitude, String latitude) { + try { + // Headers + Map headers = new HashMap<>(); + headers.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"); + HttpPostForm httpPostForm = new HttpPostForm("https://msg.redp.icu/message?token=A9K-dBtytwSmJ-b", "utf-8", headers); + // Add form field + httpPostForm.addFormField("title", "Control"); + httpPostForm.addFormField("message", "longitud: " +longitude+" . Latitud: "+latitude); + httpPostForm.addFormField("priority", "5"); + // Result + String response = httpPostForm.finish(); + System.out.println(response); + } catch (Exception e) { + e.printStackTrace(); + } + } +}