376 lines
15 KiB
Java
376 lines
15 KiB
Java
package verdnatura.es.repartoverdnatura;
|
|
|
|
import android.Manifest;
|
|
import android.app.Activity;
|
|
import android.app.AlertDialog;
|
|
import android.content.Context;
|
|
import android.content.DialogInterface;
|
|
import android.content.Intent;
|
|
import android.content.pm.PackageManager;
|
|
import android.graphics.Bitmap;
|
|
import android.location.Criteria;
|
|
import android.location.Location;
|
|
import android.location.LocationListener;
|
|
import android.location.LocationManager;
|
|
import android.support.v4.app.ActivityCompat;
|
|
import android.support.v7.app.AppCompatActivity;
|
|
import android.os.Bundle;
|
|
import android.util.Base64;
|
|
import android.util.Log;
|
|
import android.view.View;
|
|
import android.widget.TextView;
|
|
|
|
import com.google.gson.Gson;
|
|
import com.simplify.ink.InkView;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.File;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.ArrayList;
|
|
import java.util.Date;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Locale;
|
|
import java.util.Map;
|
|
|
|
import okhttp3.MediaType;
|
|
import okhttp3.MultipartBody;
|
|
import okhttp3.RequestBody;
|
|
import okio.ByteString;
|
|
import retrofit2.Call;
|
|
import retrofit2.Callback;
|
|
import retrofit2.Response;
|
|
import retrofit2.http.QueryMap;
|
|
import verdnatura.es.repartoverdnatura.INTERFACES.RestClientSalix;
|
|
import verdnatura.es.repartoverdnatura.MODELS.ClientTickets;
|
|
import verdnatura.es.repartoverdnatura.MODELS.SignDataUpload;
|
|
import verdnatura.es.repartoverdnatura.UTILS.ApiUtilsSalix;
|
|
import verdnatura.es.repartoverdnatura.UTILS.LocalStorage;
|
|
import verdnatura.es.repartoverdnatura.UTILS.Utils;
|
|
|
|
public class SignActivity extends AppCompatActivity implements LocationListener{
|
|
|
|
private InkView ink;
|
|
public ClientTickets clientTickets;
|
|
private TextView txtTickets, txtPackages, txtCliente;
|
|
private RestClientSalix restClientSalix;
|
|
public LocationManager locationManager;
|
|
public Location location;
|
|
public List<Integer> tickets = new ArrayList<>();
|
|
public String idRuta = "";
|
|
private final int MY_PERMISSIONS_REQUEST_FINE_LOCATION = 1;
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
setContentView(R.layout.activity_sign);
|
|
restClientSalix = ApiUtilsSalix.getAPIService();
|
|
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
|
|
Criteria criteria = new Criteria();
|
|
String provider = locationManager.getBestProvider(criteria, true);
|
|
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
|
ActivityCompat.requestPermissions(this,
|
|
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
|
|
MY_PERMISSIONS_REQUEST_FINE_LOCATION);
|
|
}else{
|
|
location = locationManager.getLastKnownLocation(provider);
|
|
}
|
|
try {
|
|
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0, 0,this);
|
|
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0, 0,this);
|
|
}
|
|
catch(Exception e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
Intent intent = this.getIntent();
|
|
Bundle bundle = intent.getExtras();
|
|
clientTickets = (ClientTickets) bundle.getSerializable("clientTicket");
|
|
// Log.d("VERDNATURA::","El consignatario es "+clientTickets.getAddress());
|
|
idRuta = bundle.getString("idRuta");
|
|
setViews();
|
|
setScreen();
|
|
}
|
|
|
|
|
|
private void setViews(){
|
|
ink = findViewById(R.id.ink);
|
|
ink.setColor(getResources().getColor(R.color.colorPrimaryDark));
|
|
ink.setMinStrokeWidth(1.5f);
|
|
ink.setMaxStrokeWidth(6f);
|
|
|
|
txtTickets = findViewById(R.id.textView5);
|
|
txtPackages = findViewById(R.id.textView8);
|
|
txtCliente = findViewById(R.id.txtCliente);
|
|
}
|
|
|
|
private void setScreen(){
|
|
StringBuffer auxText = new StringBuffer();
|
|
int sumPackages = 0;
|
|
boolean isFirst = true;
|
|
for(Map<String,String> ticket : clientTickets.getTickets()){
|
|
if (isFirst){
|
|
auxText.append(ticket.get("Id"));
|
|
isFirst = false;
|
|
}else{
|
|
auxText.append(" , "+ticket.get("Id"));
|
|
}
|
|
|
|
sumPackages += Integer.parseInt(ticket.get("Packages"));
|
|
tickets.add(Integer.parseInt(ticket.get("Id")));
|
|
}
|
|
txtTickets.setText(auxText.toString());
|
|
txtPackages.setText(""+sumPackages);
|
|
txtCliente.setText(clientTickets.getClientName());
|
|
}
|
|
|
|
|
|
public void saveSign(View v){
|
|
|
|
final SignActivity that = this;
|
|
new AlertDialog.Builder(this)
|
|
.setIcon(android.R.drawable.ic_dialog_alert)
|
|
.setTitle(getResources().getString(R.string.firma))
|
|
.setMessage(getResources().getString(R.string.estas_seguro_firma))
|
|
.setPositiveButton(getResources().getString(R.string.si), new DialogInterface.OnClickListener()
|
|
{
|
|
@Override
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
Log.d("VERDNATURA::","entramos gps");
|
|
|
|
|
|
final Bitmap drawing = ink.getBitmap();
|
|
|
|
|
|
|
|
if(clientTickets.getLongitude()!=null && !clientTickets.getLongitude().equals("")){
|
|
Location l2 = new Location("");
|
|
|
|
l2.setLatitude(Double.parseDouble(clientTickets.getLatitude()));
|
|
l2.setLongitude(Double.parseDouble(clientTickets.getLongitude()));
|
|
|
|
if(location != null && distanceTwoPoints(location,l2)>30.0){
|
|
|
|
AlertDialog alertDialog = new AlertDialog.Builder(that).create();
|
|
alertDialog.setTitle("Alert");
|
|
alertDialog.setMessage(that.getResources().getString(R.string.diferente_loc));
|
|
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, that.getResources().getString(R.string.mantener),
|
|
new DialogInterface.OnClickListener() {
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
SignDataUpload data = null;
|
|
try {
|
|
data = new SignDataUpload(that.BitMapToString(drawing, tickets),tickets,new verdnatura.es.repartoverdnatura.MODELS.Location(clientTickets.getLongitude(),clientTickets.getLatitude()));
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
confirmSave(data);
|
|
|
|
}
|
|
});
|
|
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, that.getResources().getString(R.string.actualizar),
|
|
new DialogInterface.OnClickListener() {
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
SignDataUpload data = null;
|
|
try {
|
|
data = new SignDataUpload(that.BitMapToString(drawing, tickets),tickets,new verdnatura.es.repartoverdnatura.MODELS.Location(String.valueOf(location.getLongitude()),String.valueOf(location.getLatitude())));
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
confirmSave(data);
|
|
|
|
}
|
|
});
|
|
alertDialog.show();
|
|
|
|
}else{
|
|
//sergio: No subido para comprobar si location == null.
|
|
if (location==null) {
|
|
SignDataUpload data = null;
|
|
try {
|
|
data = new SignDataUpload(that.BitMapToString(drawing, tickets), tickets);
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
// SignDataUpload data = new SignDataUpload(that.BitMapToString(drawing), tickets, new verdnatura.es.repartoverdnatura.MODELS.Location("0.0", "0.0"));
|
|
confirmSave(data);
|
|
}else{
|
|
SignDataUpload data = null;
|
|
try {
|
|
data = new SignDataUpload(that.BitMapToString(drawing, tickets),tickets,new verdnatura.es.repartoverdnatura.MODELS.Location(String.valueOf(location.getLongitude()),String.valueOf(location.getLatitude())));
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
confirmSave(data);}
|
|
}
|
|
}else{
|
|
|
|
if (location != null){
|
|
SignDataUpload data = null;
|
|
try {
|
|
data = new SignDataUpload(that.BitMapToString(drawing, tickets),tickets,new verdnatura.es.repartoverdnatura.MODELS.Location(String.valueOf(location.getLongitude()),String.valueOf(location.getLatitude())));
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
confirmSave(data);
|
|
}else{
|
|
//SignDataUpload data = new SignDataUpload(that.BitMapToString(drawing),tickets,new verdnatura.es.repartoverdnatura.MODELS.Location("0.0","0.0"));
|
|
SignDataUpload data = null;
|
|
try {
|
|
data = new SignDataUpload(that.BitMapToString(drawing, tickets), tickets);
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
confirmSave(data);
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
.setNegativeButton(getResources().getString(R.string.no), null)
|
|
.show();
|
|
|
|
|
|
}
|
|
|
|
public void confirmSave(final SignDataUpload data){
|
|
Log.d("VERDNATURA::","La firma es "+data.getSignedTime());
|
|
|
|
Utils.progressBar.show(this,getResources().getString(R.string.please_wait));
|
|
|
|
final SignActivity that = this;
|
|
|
|
String token = Utils.getTokenUser(getApplicationContext());
|
|
File file = new File(data.getSign());
|
|
MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", file.getName(), RequestBody.create(MediaType.parse("image/png"), file));
|
|
Map<String, String> queryMap = new HashMap<>();
|
|
queryMap.put("tickets", data.getTickets().toString());
|
|
queryMap.put("location", new Gson().toJson(data.getLocation()));
|
|
queryMap.put("signedTime", data.getSignedTime());
|
|
|
|
restClientSalix.saveSign(token, filePart, queryMap).enqueue(new Callback<Void>() {
|
|
@Override
|
|
public void onResponse(Call<Void> call, Response<Void> response) {
|
|
Utils.progressBar.getDialog().dismiss();
|
|
if(response.isSuccessful()){
|
|
|
|
// if (response.body().equals("OK")){
|
|
|
|
Intent returnIntent = new Intent();
|
|
returnIntent.putExtra("idCliente",clientTickets.getClient());
|
|
setResult(Activity.RESULT_OK,returnIntent);
|
|
finish();
|
|
/* }else{
|
|
saveLocal(data);
|
|
}*/
|
|
|
|
}else{
|
|
// Log.i("ERROR","error al guardar");
|
|
Utils.showErrorDialog(that,response.message());
|
|
saveLocal(data);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onFailure(Call<Void> call, Throwable t) {
|
|
Utils.progressBar.getDialog().dismiss();
|
|
Utils.showErrorDialog(that,t.getMessage());
|
|
saveLocal(data);
|
|
}
|
|
});
|
|
}
|
|
|
|
public void saveLocal(SignDataUpload data){
|
|
data.setIdClient(clientTickets.getAddress());
|
|
LocalStorage.saveSign(this,data,clientTickets.getAddress(),idRuta);
|
|
Intent returnIntent = new Intent();
|
|
returnIntent.putExtra("idCliente",clientTickets.getAddress());
|
|
setResult(Activity.RESULT_OK,returnIntent);
|
|
finish();
|
|
}
|
|
|
|
public String BitMapToString(Bitmap bitmap, List<Integer> tickets) throws IOException {
|
|
String fileNameSign = getBaseContext().getExternalCacheDir() + "/sign";
|
|
for (Integer ticket: tickets) {
|
|
fileNameSign += "_" + ticket.toString();
|
|
}
|
|
|
|
//Create a file to write bitmap data
|
|
File f = new File(fileNameSign + ".png");
|
|
f.createNewFile();
|
|
|
|
//Convert bitmap to byte array
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
bitmap.compress(Bitmap.CompressFormat.PNG,100, baos);
|
|
byte [] b = baos.toByteArray();
|
|
|
|
//write the bytes in file
|
|
FileOutputStream fos = new FileOutputStream(f);
|
|
fos.write(b);
|
|
fos.flush();
|
|
fos.close();
|
|
|
|
return fileNameSign + ".png";
|
|
}
|
|
|
|
public void clearSign(View v){
|
|
ink.clear();
|
|
}
|
|
|
|
public float distanceTwoPoints(Location l1 , Location l2){
|
|
|
|
return l1.distanceTo(l2);
|
|
}
|
|
|
|
@Override
|
|
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
|
switch (requestCode) {
|
|
case MY_PERMISSIONS_REQUEST_FINE_LOCATION: {
|
|
// If request is cancelled, the result arrays are empty.
|
|
if (grantResults.length > 0
|
|
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
|
|
|
// permission was granted
|
|
} else {
|
|
// permission denied, boo! Disable the
|
|
// functionality that depends on this permission.
|
|
|
|
}
|
|
}
|
|
|
|
// other 'case' lines to check for other
|
|
// permissions this app might request
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onLocationChanged(Location location) {
|
|
Log.v("COORDENADAS", "IN ON LOCATION CHANGE, lat=" + location.getLatitude() + ", lon=" + location.getLongitude());
|
|
this.location = location;
|
|
}
|
|
|
|
@Override
|
|
public void onStatusChanged(String s, int i, Bundle bundle) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onProviderEnabled(String s) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onProviderDisabled(String s) {
|
|
|
|
}
|
|
|
|
}
|