-> Añadido editText: Se envian dos mensajes, uno con el editText y otro con las coordenadas
-> Retirado código de loops -> Limpieza de código
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package com.journaldev.gpslocationtracking;
|
||||
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
public class GetToken {
|
||||
|
||||
public String getToken() throws IOException {
|
||||
String url = "TOKENURL";
|
||||
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
|
||||
con.setRequestMethod("GET");
|
||||
//response status
|
||||
int status = con.getResponseCode();
|
||||
System.out.println(status);
|
||||
//response body
|
||||
if (status == 200) {
|
||||
|
||||
String line;
|
||||
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
while ((line = in.readLine()) != null) {
|
||||
|
||||
sb.append(line);
|
||||
}
|
||||
return sb.toString();
|
||||
} else {
|
||||
System.out.println("error GET info");
|
||||
}
|
||||
|
||||
return url;
|
||||
}}
|
||||
@@ -8,32 +8,29 @@ import android.os.StrictMode;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
import static android.Manifest.permission.ACCESS_COARSE_LOCATION;
|
||||
import static android.Manifest.permission.ACCESS_FINE_LOCATION;
|
||||
import static android.Manifest.permission.*;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
|
||||
private ArrayList<String> permissionsToRequest;
|
||||
private ArrayList<String> permissionsRejected = new ArrayList<>();
|
||||
private ArrayList<String> permissions = new ArrayList<>();
|
||||
private final ArrayList<String> permissionsRejected = new ArrayList<>();
|
||||
private final ArrayList<String> permissions = new ArrayList<>();
|
||||
|
||||
private final static int ALL_PERMISSIONS_RESULT = 101;
|
||||
LocationTrack locationTrack;
|
||||
HashMap<String, String> listadoCoordenadas = new HashMap<String, String>();
|
||||
|
||||
SendMessage sendMessage = new SendMessage();
|
||||
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
@@ -41,6 +38,7 @@ public class MainActivity extends AppCompatActivity {
|
||||
|
||||
permissions.add(ACCESS_FINE_LOCATION);
|
||||
permissions.add(ACCESS_COARSE_LOCATION);
|
||||
permissions.add(INTERNET);
|
||||
|
||||
permissionsToRequest = findUnAskedPermissions(permissions);
|
||||
//get the permissions we have asked for before but are not granted..
|
||||
@@ -49,7 +47,6 @@ public class MainActivity extends AppCompatActivity {
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
|
||||
|
||||
if (permissionsToRequest.size() > 0)
|
||||
requestPermissions(permissionsToRequest.toArray(new String[permissionsToRequest.size()]), ALL_PERMISSIONS_RESULT);
|
||||
}
|
||||
@@ -73,10 +70,9 @@ public class MainActivity extends AppCompatActivity {
|
||||
double latitude = locationTrack.getLatitude();
|
||||
|
||||
// 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);
|
||||
Toast.makeText(getApplicationContext(), "Latitud:" + latitude + "\nLongitud:" + longitude, Toast.LENGTH_SHORT).show();
|
||||
fileManager.writeToFile("Latitud: " + latitude + "\nLongitud:" + longitude, MainActivity.this);
|
||||
|
||||
listadoCoordenadas.put(Double.toString(latitude), Double.toString(longitude));
|
||||
|
||||
} else {
|
||||
|
||||
@@ -87,29 +83,34 @@ public class MainActivity extends AppCompatActivity {
|
||||
});
|
||||
|
||||
Button btn2 = (Button) findViewById(R.id.btn2);
|
||||
EditText textInput = (EditText)findViewById(R.id.text01);
|
||||
btn2.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
strictModeDisabler();
|
||||
|
||||
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);
|
||||
String token = null;
|
||||
//Request the token URL
|
||||
GetToken getToken = new GetToken();
|
||||
try {
|
||||
token = getToken.getToken();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Toast.makeText(getApplicationContext(), "Enviando datos", Toast.LENGTH_SHORT).show();
|
||||
|
||||
|
||||
//TODO: Generar fichero con los puntos de trayectoria
|
||||
|
||||
//Codigo para enviar ubicacion cada 5 segundos
|
||||
// try {
|
||||
// while (true) {
|
||||
// String url = "https://www.google.com/maps/search/?api=1&query="+Double.toString(latitude)+","+Double.toString(longitude);
|
||||
// sendMessage.sendMessage(Double.toString(longitude), Double.toString(latitude), url);
|
||||
// sendMessage.sendMessage(Double.toString(longitude), Double.toString(latitude), url, token);
|
||||
// Thread.sleep(5 * 1000);
|
||||
// }
|
||||
// } catch (InterruptedException e) {
|
||||
@@ -118,26 +119,53 @@ public class MainActivity extends AppCompatActivity {
|
||||
|
||||
|
||||
//Transferimos latitud, longitud y url de maps al sistema
|
||||
String url = "https://www.google.com/maps/search/?api=1&query="+Double.toString(latitude)+","+Double.toString(longitude);
|
||||
sendMessage.sendMessage(Double.toString(longitude), Double.toString(latitude), url);
|
||||
// while (loopController == 1) {
|
||||
// startLoop(longitude, latitude, token);
|
||||
// }
|
||||
|
||||
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
|
||||
// if (loopController != 1) {
|
||||
// String url = "https://www.google.com/maps/search/?api=1&query=" + Double.toString(latitude) + "," + Double.toString(longitude);
|
||||
// sendMessage.sendMessage(Double.toString(longitude), Double.toString(latitude), url, token);
|
||||
// }
|
||||
|
||||
String url = "https://www.google.com/maps/search/?api=1&query=" + latitude + "," + longitude;
|
||||
sendMessage.sendMessage(Double.toString(longitude), Double.toString(latitude), url, token);
|
||||
|
||||
if (textInput.getText().toString() != ""){
|
||||
sendMessage.sendMessageWithoutCoordinates(textInput.getText().toString(), url, token);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void strictModeDisabler() {
|
||||
if (Build.VERSION.SDK_INT > 9) {
|
||||
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
|
||||
StrictMode.setThreadPolicy(policy);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
//
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// try {
|
||||
// while (true) {
|
||||
// String url = "https://www.google.com/maps/search/?api=1&query=" + Double.toString(latitude) + "," + Double.toString(longitude);
|
||||
// sendMessage.sendMessage(Double.toString(longitude), Double.toString(latitude), url, token);
|
||||
// Thread.sleep(5 * 1000);
|
||||
// }
|
||||
// } catch (InterruptedException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
||||
private ArrayList<String> findUnAskedPermissions(ArrayList<String> wanted) {
|
||||
ArrayList<String> result = new ArrayList<String>();
|
||||
|
||||
|
||||
@@ -5,19 +5,37 @@ import java.util.Map;
|
||||
|
||||
public class SendMessage {
|
||||
|
||||
public void sendMessage(String longitude, String latitude, String url) {
|
||||
public void sendMessage(String longitude, String latitude, String url, String token) {
|
||||
try {
|
||||
// Headers
|
||||
Map<String, String> 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=AMnezCx8jGxCZUr", "utf-8", headers);
|
||||
HttpPostForm httpPostForm = new HttpPostForm("https://msg.redp.icu/message?token="+token, "utf-8", headers);
|
||||
// Add form field
|
||||
httpPostForm.addFormField("title", "Información de ubicación");
|
||||
httpPostForm.addFormField("message", "Longitud: " +longitude+" . Latitud: "+latitude+". ||| URL MAPS: "+url);
|
||||
httpPostForm.addFormField("priority", "5");
|
||||
// Result
|
||||
String response = httpPostForm.finish();
|
||||
System.out.println(response);
|
||||
//System.out.println(response);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void sendMessageWithoutCoordinates(String message, String url, String token) {
|
||||
try {
|
||||
// Headers
|
||||
Map<String, String> 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="+token, "utf-8", headers);
|
||||
// Add form field
|
||||
httpPostForm.addFormField("title", "Mensaje");
|
||||
httpPostForm.addFormField("message", message);
|
||||
httpPostForm.addFormField("priority", "5");
|
||||
// Result
|
||||
String response = httpPostForm.finish();
|
||||
//System.out.println(response);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -10,17 +10,27 @@
|
||||
android:paddingTop="@dimen/activity_vertical_margin"
|
||||
tools:context="com.journaldev.gpslocationtracking.MainActivity">
|
||||
|
||||
|
||||
<Button
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/btn2"
|
||||
android:text="Save Data"
|
||||
android:layout_centerHorizontal="true" android:layout_alignParentBottom="true"
|
||||
android:layout_marginBottom="153dp"/>
|
||||
android:layout_marginBottom="101dp"/>
|
||||
<Button
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/btn"
|
||||
android:layout_centerInParent="true"
|
||||
android:text="GET LOCATION" />
|
||||
<EditText
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:inputType="textPersonName"
|
||||
android:text=""
|
||||
android:ems="10"
|
||||
android:id="@+id/text01" android:layout_centerHorizontal="true"
|
||||
android:layout_alignParentTop="true" android:layout_marginTop="216dp"/>
|
||||
<!---->
|
||||
</RelativeLayout>
|
||||
|
||||
Reference in New Issue
Block a user