220 lines
7.9 KiB
Java
220 lines
7.9 KiB
Java
package com.journaldev.gpslocationtracking;
|
|
|
|
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;
|
|
import android.util.Log;
|
|
import android.view.View;
|
|
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;
|
|
|
|
public class MainActivity extends AppCompatActivity {
|
|
|
|
|
|
private ArrayList<String> permissionsToRequest;
|
|
private ArrayList<String> permissionsRejected = new ArrayList<>();
|
|
private 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);
|
|
setContentView(R.layout.activity_main);
|
|
|
|
permissions.add(ACCESS_FINE_LOCATION);
|
|
permissions.add(ACCESS_COARSE_LOCATION);
|
|
|
|
permissionsToRequest = findUnAskedPermissions(permissions);
|
|
//get the permissions we have asked for before but are not granted..
|
|
//we will store this in a global list to access later.
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
|
|
|
|
if (permissionsToRequest.size() > 0)
|
|
requestPermissions(permissionsToRequest.toArray(new String[permissionsToRequest.size()]), ALL_PERMISSIONS_RESULT);
|
|
}
|
|
|
|
|
|
Button btn = (Button) findViewById(R.id.btn);
|
|
|
|
|
|
btn.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View view) {
|
|
|
|
locationTrack = new LocationTrack(MainActivity.this);
|
|
FIleManager fileManager = new FIleManager();
|
|
|
|
|
|
if (locationTrack.canGetLocation()) {
|
|
|
|
|
|
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(), "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();
|
|
}
|
|
|
|
}
|
|
});
|
|
|
|
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);
|
|
}
|
|
|
|
//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);
|
|
// Thread.sleep(5 * 1000);
|
|
// }
|
|
// } catch (InterruptedException e) {
|
|
// e.printStackTrace();
|
|
// }
|
|
|
|
|
|
//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);
|
|
|
|
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<String> findUnAskedPermissions(ArrayList<String> wanted) {
|
|
ArrayList<String> result = new ArrayList<String>();
|
|
|
|
for (String perm : wanted) {
|
|
if (!hasPermission(perm)) {
|
|
result.add(perm);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
private boolean hasPermission(String permission) {
|
|
if (canMakeSmores()) {
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
return (checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED);
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private boolean canMakeSmores() {
|
|
return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1);
|
|
}
|
|
|
|
|
|
@TargetApi(Build.VERSION_CODES.M)
|
|
@Override
|
|
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
|
|
|
switch (requestCode) {
|
|
|
|
case ALL_PERMISSIONS_RESULT:
|
|
for (String perms : permissionsToRequest) {
|
|
if (!hasPermission(perms)) {
|
|
permissionsRejected.add(perms);
|
|
}
|
|
}
|
|
|
|
if (permissionsRejected.size() > 0) {
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
if (shouldShowRequestPermissionRationale(permissionsRejected.get(0))) {
|
|
showMessageOKCancel("These permissions are mandatory for the application. Please allow access.",
|
|
new DialogInterface.OnClickListener() {
|
|
@Override
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
requestPermissions(permissionsRejected.toArray(new String[permissionsRejected.size()]), ALL_PERMISSIONS_RESULT);
|
|
}
|
|
}
|
|
});
|
|
return;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
|
|
new AlertDialog.Builder(MainActivity.this)
|
|
.setMessage(message)
|
|
.setPositiveButton("OK", okListener)
|
|
.setNegativeButton("Cancel", null)
|
|
.create()
|
|
.show();
|
|
}
|
|
|
|
@Override
|
|
protected void onDestroy() {
|
|
super.onDestroy();
|
|
locationTrack.stopListener();
|
|
}
|
|
}
|