[8379cd8] | 1 | /* |
---|
| 2 | Tibisay Movil |
---|
| 3 | |
---|
| 4 | Copyright (C) 2013 Antonio Araujo (aaraujo@cenditel.gob.ve), Jose Ruiz |
---|
| 5 | (jruiz@cenditel.gob.ve), Fundacion Centro Nacional de Desarrollo e |
---|
| 6 | Investigacion en Tecnologias Libres - CENDITEL. |
---|
| 7 | |
---|
| 8 | La Fundación CENDITEL concede permiso para usar, copiar, distribuir y/o |
---|
| 9 | modificar este programa, reconociendo el derecho que la humanidad posee al |
---|
| 10 | libre acceso al conocimiento, bajo los términos de la licencia de software |
---|
| 11 | GPL versión 2.0 de la Free Software Foundation. |
---|
| 12 | |
---|
| 13 | Este programa se distribuye con la esperanza de que sea util, pero SIN |
---|
| 14 | NINGUNA GARANTIA; tampoco las implicitas garantias de MERCANTILIDAD o |
---|
| 15 | ADECUACION A UN PROPOSITO PARTICULAR. |
---|
| 16 | |
---|
| 17 | Para mayor información sobre los términos de la licencia ver el archivo |
---|
| 18 | llamado "gpl-2.0.txt" en ingles. |
---|
| 19 | */ |
---|
| 20 | |
---|
| 21 | |
---|
[6638d33] | 22 | package ve.gob.cenditel.tibisaymovil; |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | import java.io.BufferedInputStream; |
---|
| 26 | import java.io.File; |
---|
| 27 | import java.io.FileInputStream; |
---|
| 28 | import java.io.FileOutputStream; |
---|
[56b06e2] | 29 | import java.io.IOException; |
---|
[6638d33] | 30 | import java.io.InputStream; |
---|
| 31 | import java.io.OutputStream; |
---|
[56b06e2] | 32 | import java.lang.reflect.Field; |
---|
[6638d33] | 33 | import java.net.HttpURLConnection; |
---|
| 34 | import java.net.URL; |
---|
| 35 | import java.security.KeyStore; |
---|
| 36 | import java.security.cert.Certificate; |
---|
| 37 | import java.security.cert.CertificateFactory; |
---|
| 38 | import java.util.ArrayList; |
---|
| 39 | |
---|
| 40 | import javax.net.ssl.HttpsURLConnection; |
---|
| 41 | import javax.net.ssl.SSLContext; |
---|
| 42 | import javax.net.ssl.TrustManagerFactory; |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | import android.net.Uri; |
---|
| 46 | import android.os.AsyncTask; |
---|
| 47 | import android.os.Bundle; |
---|
| 48 | import android.os.Environment; |
---|
| 49 | import android.os.Looper; |
---|
| 50 | import android.app.Activity; |
---|
| 51 | import android.app.AlertDialog; |
---|
| 52 | import android.app.Dialog; |
---|
| 53 | import android.app.ProgressDialog; |
---|
| 54 | import android.content.DialogInterface; |
---|
| 55 | import android.content.Intent; |
---|
| 56 | import android.util.Log; |
---|
| 57 | import android.view.Menu; |
---|
| 58 | import android.view.Window; |
---|
| 59 | import android.webkit.MimeTypeMap; |
---|
| 60 | import android.widget.Toast; |
---|
| 61 | |
---|
| 62 | public class DownloaderActivity extends Activity { |
---|
| 63 | |
---|
| 64 | // cadena que mantiene la ruta para almacenar los archivos |
---|
| 65 | // extraidos de un contendor BDOC |
---|
| 66 | private String extractedDirFiles; |
---|
| 67 | |
---|
| 68 | // cadena que mantiene la ruta para almacenar los archivos |
---|
| 69 | // descargados desde un servidor para verificar su firma |
---|
| 70 | private String downloadedDirFiles; |
---|
| 71 | |
---|
| 72 | // ruta absoluta al archivo a verificar |
---|
| 73 | private String fileToVerify; |
---|
| 74 | |
---|
| 75 | // extension del archivo a verificar |
---|
| 76 | private String fileToVerifyExtension; |
---|
| 77 | |
---|
| 78 | // cadena que mantiene la URL al descargar archivo con https |
---|
| 79 | String urlhttps = null; |
---|
| 80 | |
---|
| 81 | // Progress Dialog |
---|
| 82 | private ProgressDialog mProgressDialog; |
---|
| 83 | |
---|
| 84 | // Progress dialog type (0 - for Horizontal progress bar) |
---|
| 85 | public static final int DIALOG_DOWNLOAD_PROGRESS = 0; |
---|
| 86 | |
---|
| 87 | |
---|
| 88 | |
---|
| 89 | @Override |
---|
| 90 | protected void onCreate(Bundle savedInstanceState) { |
---|
| 91 | |
---|
| 92 | //Estilando la barra de titulo |
---|
| 93 | final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); |
---|
| 94 | |
---|
| 95 | super.onCreate(savedInstanceState); |
---|
| 96 | //setContentView(R.layout.activity_downloader); |
---|
| 97 | setContentView(R.layout.activity_verify_result_bdoc); |
---|
| 98 | |
---|
| 99 | //Estilando Barra de titulo |
---|
| 100 | if(customTitleSupported) |
---|
| 101 | getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar); |
---|
| 102 | |
---|
[56b06e2] | 103 | // ------------------------------------------------------------------------------------ |
---|
| 104 | // el siguiente segmento de codigo permite obtener los nombres de los archivos |
---|
| 105 | // que estan en /res/raw |
---|
| 106 | ArrayList<String> list = new ArrayList<String>(); |
---|
| 107 | Field[] fields = R.raw.class.getFields(); |
---|
| 108 | |
---|
| 109 | for (int l=0; l < fields.length; l++) { |
---|
| 110 | try { |
---|
| 111 | Toast.makeText(getApplicationContext(), "DownloaderActivity: "+Integer.toString(l)+ " "+ Integer.toString(fields[l].getInt(null)), Toast.LENGTH_SHORT).show(); |
---|
| 112 | } catch (IllegalArgumentException e) { |
---|
| 113 | // TODO Auto-generated catch block |
---|
| 114 | Toast.makeText(getApplicationContext(), "DownloaderActivity: IllegalArgumentException "+e.getMessage(), Toast.LENGTH_SHORT).show(); |
---|
| 115 | } catch (IllegalAccessException e) { |
---|
| 116 | // TODO Auto-generated catch block |
---|
| 117 | Toast.makeText(getApplicationContext(), "DownloaderActivity: IllegalAccessException "+e.getMessage(), Toast.LENGTH_SHORT).show(); |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | /* |
---|
| 122 | for(Field f : fields) |
---|
| 123 | try { |
---|
| 124 | Toast.makeText(getApplicationContext(), "DownloaderActivity: Fields.length: "+Integer.toString(fields.length), Toast.LENGTH_SHORT).show(); |
---|
| 125 | Toast.makeText(getApplicationContext(), "DownloaderActivity: "+f.getName(), Toast.LENGTH_SHORT).show(); |
---|
| 126 | //list.add(splitString[0]); |
---|
| 127 | } catch (IllegalArgumentException e) { |
---|
| 128 | Toast.makeText(getApplicationContext(), "DownloaderActivity: IllegalArgumentException "+e.getMessage(), Toast.LENGTH_SHORT).show(); |
---|
| 129 | } |
---|
| 130 | */ |
---|
| 131 | // ------------------------------------------------------------------------------------ |
---|
| 132 | |
---|
[6638d33] | 133 | |
---|
| 134 | |
---|
| 135 | // chequear si intent tiene data |
---|
| 136 | final android.content.Intent intent = getIntent(); |
---|
| 137 | |
---|
| 138 | final Bundle bundle = getIntent().getExtras(); |
---|
| 139 | |
---|
| 140 | if (intent != null) { |
---|
[ba6bc7c] | 141 | //Toast.makeText(getApplicationContext(), "DownloaderActivity: intent != null", Toast.LENGTH_SHORT).show(); |
---|
[6638d33] | 142 | |
---|
| 143 | final android.net.Uri data = intent.getData (); |
---|
| 144 | |
---|
| 145 | if (data != null) { |
---|
[ba6bc7c] | 146 | //Toast.makeText(getApplicationContext(), "data != null", Toast.LENGTH_SHORT).show(); |
---|
[6638d33] | 147 | |
---|
| 148 | // verificar el tipo de scheme |
---|
| 149 | String scheme = data.getScheme(); |
---|
| 150 | |
---|
| 151 | // verificacion de un archivo que esta en el dispositivo |
---|
| 152 | if (scheme.equals("file")) { |
---|
[ba6bc7c] | 153 | //Toast.makeText(getApplicationContext(), "file: "+data.getPath(), Toast.LENGTH_SHORT).show(); |
---|
[6638d33] | 154 | |
---|
| 155 | // verificar el archivo |
---|
| 156 | if (data.getPath().endsWith("bdoc")){ |
---|
| 157 | fileToVerify = data.getPath(); |
---|
| 158 | |
---|
| 159 | // ejecutar la verificacion |
---|
| 160 | //doBdocVerification(data.getPath()); |
---|
| 161 | }else{ |
---|
[ba6bc7c] | 162 | //Toast.makeText(getApplicationContext(), "¡Por Implementar!", Toast.LENGTH_SHORT).show(); |
---|
[6638d33] | 163 | |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | // verificacion de un archivo que se debe descargar |
---|
| 169 | if (scheme.equals("https")) { |
---|
[ba6bc7c] | 170 | //Toast.makeText(getApplicationContext(), "scheme: "+data.toString(), Toast.LENGTH_SHORT).show(); |
---|
[6638d33] | 171 | |
---|
| 172 | //Toast.makeText(getApplicationContext(), "externalStorage: "+Environment.getExternalStorageDirectory().toString(), Toast.LENGTH_SHORT).show(); |
---|
| 173 | |
---|
| 174 | urlhttps = data.toString(); |
---|
| 175 | new DownloadFileAsync().execute(data.toString(), "false"); |
---|
| 176 | |
---|
| 177 | //new DownloadFileFromURL().execute("http://farm1.static.flickr.com/114/298125983_0e4bf66782_b.jpg"); |
---|
| 178 | |
---|
| 179 | //Toast.makeText(getApplicationContext(), "****despues de execute()", Toast.LENGTH_SHORT).show(); |
---|
| 180 | // verificar el archivo |
---|
| 181 | //Toast.makeText(getApplicationContext(), "Ahora viene la verificación del archivo", Toast.LENGTH_SHORT).show(); |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | |
---|
| 185 | return; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | // verificacion de archivo desde la Activity principal |
---|
| 189 | if (bundle != null) { |
---|
| 190 | fileToVerify = bundle.getString("fileToVerify"); |
---|
| 191 | fileToVerifyExtension = bundle.getString("fileExtension"); |
---|
| 192 | |
---|
| 193 | |
---|
[ba6bc7c] | 194 | //Toast.makeText(getApplicationContext(), "fileToVerify bundle!=null: "+fileToVerify, Toast.LENGTH_SHORT).show(); |
---|
[6638d33] | 195 | |
---|
| 196 | if (fileToVerifyExtension.equals("bdoc")){ |
---|
[ba6bc7c] | 197 | //Toast.makeText(getApplicationContext(), "verificacion de archivo desde la Activity principal", Toast.LENGTH_SHORT).show(); |
---|
[6638d33] | 198 | // ejecutar la verificacion |
---|
| 199 | //doBdocVerification(fileToVerify); |
---|
| 200 | }else{ |
---|
[ba6bc7c] | 201 | //Toast.makeText(getApplicationContext(), "¡Por Implementar!", Toast.LENGTH_SHORT).show(); |
---|
[6638d33] | 202 | |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | //return; |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | }else{ |
---|
[ba6bc7c] | 209 | //Toast.makeText(getApplicationContext(), "intent == null", Toast.LENGTH_SHORT).show(); |
---|
[6638d33] | 210 | } |
---|
| 211 | |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | |
---|
| 215 | /** |
---|
| 216 | * Prepara directorio |
---|
| 217 | * @return boolean |
---|
| 218 | */ |
---|
| 219 | private boolean prepareDirectory(String dir) |
---|
| 220 | { |
---|
| 221 | try |
---|
| 222 | { |
---|
| 223 | if (makedirs(dir)) |
---|
| 224 | { |
---|
| 225 | return true; |
---|
| 226 | } else { |
---|
| 227 | return false; |
---|
| 228 | } |
---|
| 229 | } catch (Exception e) |
---|
| 230 | { |
---|
| 231 | e.printStackTrace(); |
---|
[ba6bc7c] | 232 | //Toast.makeText(this, "Could not initiate File System.. Is Sdcard mounted properly?", Toast.LENGTH_LONG).show(); |
---|
| 233 | Toast.makeText(this, "No se pudo iniciar el sistema de archivos. ¿Está la SDCARD montada?", Toast.LENGTH_LONG).show(); |
---|
[6638d33] | 234 | return false; |
---|
| 235 | } |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | /** |
---|
| 239 | * Crea directorio utilizando la variable tmpDir |
---|
| 240 | * @return boolean |
---|
| 241 | */ |
---|
| 242 | private boolean makedirs(String dir) |
---|
| 243 | { |
---|
| 244 | //File tempdir = new File(extractedDirFiles); |
---|
| 245 | File tempdir = new File(dir); |
---|
| 246 | if (!tempdir.exists()) |
---|
| 247 | tempdir.mkdirs(); |
---|
| 248 | |
---|
| 249 | // if (tempdir.isDirectory()) |
---|
| 250 | // { |
---|
| 251 | // File[] files = tempdir.listFiles(); |
---|
| 252 | // for (File file : files) |
---|
| 253 | // { |
---|
| 254 | // if (!file.delete()) |
---|
| 255 | // { |
---|
| 256 | // System.out.println("Failed to delete " + file); |
---|
| 257 | // } |
---|
| 258 | // } |
---|
| 259 | // } |
---|
| 260 | return (tempdir.isDirectory()); |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | |
---|
| 264 | /** |
---|
| 265 | * Showing Dialog for downloading file |
---|
| 266 | * */ |
---|
| 267 | @Override |
---|
| 268 | protected Dialog onCreateDialog(int id) { |
---|
| 269 | switch (id) { |
---|
| 270 | case DIALOG_DOWNLOAD_PROGRESS: // we set this to 0 |
---|
| 271 | mProgressDialog = new ProgressDialog(this); |
---|
| 272 | mProgressDialog.setMessage("Descargando archivo. Por favor espere..."); |
---|
| 273 | mProgressDialog.setIndeterminate(false); |
---|
| 274 | mProgressDialog.setMax(100); |
---|
| 275 | mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); |
---|
| 276 | mProgressDialog.setCancelable(true); |
---|
| 277 | mProgressDialog.show(); |
---|
| 278 | return mProgressDialog; |
---|
| 279 | default: |
---|
| 280 | return null; |
---|
| 281 | } |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | |
---|
| 285 | // clase para descargar archivo |
---|
| 286 | private class DownloadFileAsync extends AsyncTask<String, String, ArrayList<String>> { |
---|
| 287 | |
---|
| 288 | File rootDir = Environment.getExternalStorageDirectory(); |
---|
| 289 | |
---|
| 290 | /** |
---|
| 291 | * Before starting background thread |
---|
| 292 | * Show Progress Bar Dialog |
---|
| 293 | * */ |
---|
| 294 | @Override |
---|
| 295 | protected void onPreExecute() { |
---|
| 296 | super.onPreExecute(); |
---|
| 297 | showDialog(DIALOG_DOWNLOAD_PROGRESS); |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | |
---|
| 301 | /** |
---|
| 302 | * Downloading file in background thread |
---|
| 303 | * */ |
---|
| 304 | @Override |
---|
| 305 | protected ArrayList<String> doInBackground(String... params) { |
---|
| 306 | |
---|
| 307 | // para solventar error: |
---|
| 308 | // Can't create handler inside thread that has not called Looper.prepare() |
---|
| 309 | Looper.prepare(); |
---|
| 310 | |
---|
| 311 | int count; |
---|
| 312 | |
---|
| 313 | Log.d("doInBackground", params[0]); |
---|
| 314 | |
---|
| 315 | // resultArray [0] -> 0 para ruta de archivo valida |
---|
| 316 | // resultArray [0] -> 1 ocurrio un error |
---|
| 317 | // resultArray [1] -> ruta del archivo descargado |
---|
| 318 | // resultArray [1] -> mensaje de la excepcion |
---|
| 319 | ArrayList<String> resultArray = new ArrayList<String>(); |
---|
| 320 | resultArray.clear(); |
---|
| 321 | |
---|
| 322 | |
---|
| 323 | String outputString = null; |
---|
| 324 | try { |
---|
| 325 | URL url = new URL(params[0]); |
---|
| 326 | |
---|
| 327 | int lenghtOfFile = 0; |
---|
| 328 | InputStream input = null; |
---|
| 329 | |
---|
| 330 | if (url.getProtocol().equals("http")){ |
---|
| 331 | HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection(); |
---|
| 332 | input = new BufferedInputStream(httpConnection.getInputStream()); |
---|
| 333 | lenghtOfFile = httpConnection.getContentLength(); |
---|
| 334 | |
---|
| 335 | }else{ // https |
---|
| 336 | |
---|
| 337 | // ejecutar la descarga despues de haber aceptado que el certificado |
---|
| 338 | // no es conocido |
---|
| 339 | if (params[1].equals("true")){ |
---|
| 340 | Log.d("***", "params[1].equals(true)"); |
---|
| 341 | |
---|
| 342 | // descarga del certificado del servidor |
---|
[56b06e2] | 343 | |
---|
| 344 | // crear certificate factory |
---|
| 345 | CertificateFactory cf = CertificateFactory.getInstance("X.509"); |
---|
| 346 | |
---|
| 347 | // crear un KeyStore |
---|
| 348 | String keyStoreType = KeyStore.getDefaultType(); |
---|
| 349 | KeyStore keyStore = KeyStore.getInstance(keyStoreType); |
---|
| 350 | keyStore.load(null, null); |
---|
| 351 | |
---|
| 352 | // obtener vector de campos de /res/raw |
---|
| 353 | Field[] fields = R.raw.class.getFields(); |
---|
| 354 | |
---|
| 355 | InputStream caInput = null; |
---|
| 356 | |
---|
| 357 | // iterar sobre todos los certificados incluidos en /res/raw y cargarlos |
---|
| 358 | // en el KeyStore |
---|
| 359 | for (int l=0; l < fields.length; l++) { |
---|
| 360 | try { |
---|
[ba6bc7c] | 361 | //Toast.makeText(getApplicationContext(), "DownloaderActivity: "+Integer.toString(l)+ " "+ |
---|
| 362 | // Integer.toString(fields[l].getInt(null)), Toast.LENGTH_SHORT).show(); |
---|
[56b06e2] | 363 | |
---|
| 364 | caInput = new BufferedInputStream(getResources().openRawResource(fields[l].getInt(null))); |
---|
| 365 | |
---|
| 366 | } catch (IllegalArgumentException e) { |
---|
| 367 | // TODO Auto-generated catch block |
---|
| 368 | Toast.makeText(getApplicationContext(), "DownloaderActivity: IllegalArgumentException "+e.getMessage(), Toast.LENGTH_SHORT).show(); |
---|
| 369 | } catch (IllegalAccessException e) { |
---|
| 370 | // TODO Auto-generated catch block |
---|
| 371 | Toast.makeText(getApplicationContext(), "DownloaderActivity: IllegalAccessException "+e.getMessage(), Toast.LENGTH_SHORT).show(); |
---|
| 372 | } |
---|
| 373 | |
---|
| 374 | Certificate ca; |
---|
| 375 | try { |
---|
| 376 | ca = cf.generateCertificate(caInput); |
---|
| 377 | //Log.d("**ca:", ((X509Certificate) ca).getSubjectDN()); |
---|
| 378 | |
---|
| 379 | //Log.d("*** try: ca.toString()", ca.toString()); |
---|
| 380 | } finally { |
---|
| 381 | caInput.close(); |
---|
| 382 | } |
---|
| 383 | keyStore.setCertificateEntry("ca"+Integer.toString(l), ca); |
---|
| 384 | }// fin de la iteracion sobre /res/raw |
---|
| 385 | |
---|
| 386 | // Create a TrustManager that trusts the CAs in our KeyStore |
---|
| 387 | String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm(); |
---|
| 388 | TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm); |
---|
| 389 | tmf.init(keyStore); |
---|
| 390 | Log.d("***", "despues de crear TrustManager"); |
---|
| 391 | |
---|
| 392 | // Create an SSLContext that uses our TrustManager |
---|
| 393 | SSLContext context = SSLContext.getInstance("TLS"); |
---|
| 394 | context.init(null, tmf.getTrustManagers(), null); |
---|
| 395 | Log.d("***", "despues de crear SSLContext"); |
---|
| 396 | |
---|
| 397 | HttpsURLConnection httpsConnection = (HttpsURLConnection) url.openConnection(); |
---|
| 398 | Log.d("***", "despues de crear httpsConnection"); |
---|
| 399 | |
---|
| 400 | httpsConnection.setSSLSocketFactory(context.getSocketFactory()); |
---|
| 401 | Log.d("***", "despues de crear setSSLSocketFactory"); |
---|
| 402 | |
---|
| 403 | //input = new BufferedInputStream(httpsConnection.getInputStream()); |
---|
| 404 | input = httpsConnection.getInputStream(); |
---|
| 405 | |
---|
| 406 | Log.d("***", "despues de crear BufferedInputStream"); |
---|
| 407 | |
---|
| 408 | lenghtOfFile = httpsConnection.getContentLength(); |
---|
| 409 | Log.d("***", "se acepto la excepcion"); |
---|
| 410 | |
---|
| 411 | |
---|
| 412 | /* |
---|
| 413 | // descarga del certificado del servidor |
---|
[6638d33] | 414 | CertificateFactory cf = CertificateFactory.getInstance("X.509"); |
---|
| 415 | InputStream caInput = new BufferedInputStream(getResources().openRawResource(R.raw.gestion)); |
---|
| 416 | Certificate ca; |
---|
| 417 | try { |
---|
| 418 | ca = cf.generateCertificate(caInput); |
---|
| 419 | //Log.d("**ca:", ((X509Certificate) ca).getSubjectDN()); |
---|
| 420 | |
---|
| 421 | //Log.d("*** try: ca.toString()", ca.toString()); |
---|
| 422 | } finally { |
---|
| 423 | caInput.close(); |
---|
| 424 | } |
---|
| 425 | Log.d("***", "despues de Certificate ca"); |
---|
[56b06e2] | 426 | |
---|
[6638d33] | 427 | // Create a KeyStore containing our trusted CAs |
---|
| 428 | String keyStoreType = KeyStore.getDefaultType(); |
---|
| 429 | KeyStore keyStore = KeyStore.getInstance(keyStoreType); |
---|
| 430 | keyStore.load(null, null); |
---|
| 431 | keyStore.setCertificateEntry("ca", ca); |
---|
| 432 | Log.d("***", "despues de crear KeyStore"); |
---|
| 433 | |
---|
| 434 | // Create a TrustManager that trusts the CAs in our KeyStore |
---|
| 435 | String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm(); |
---|
| 436 | TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm); |
---|
| 437 | tmf.init(keyStore); |
---|
| 438 | Log.d("***", "despues de crear TrustManager"); |
---|
| 439 | |
---|
| 440 | // Create an SSLContext that uses our TrustManager |
---|
| 441 | SSLContext context = SSLContext.getInstance("TLS"); |
---|
| 442 | context.init(null, tmf.getTrustManagers(), null); |
---|
| 443 | Log.d("***", "despues de crear SSLContext"); |
---|
| 444 | |
---|
| 445 | HttpsURLConnection httpsConnection = (HttpsURLConnection) url.openConnection(); |
---|
| 446 | Log.d("***", "despues de crear httpsConnection"); |
---|
| 447 | |
---|
| 448 | httpsConnection.setSSLSocketFactory(context.getSocketFactory()); |
---|
| 449 | Log.d("***", "despues de crear setSSLSocketFactory"); |
---|
| 450 | |
---|
| 451 | //input = new BufferedInputStream(httpsConnection.getInputStream()); |
---|
| 452 | input = httpsConnection.getInputStream(); |
---|
| 453 | |
---|
| 454 | Log.d("***", "despues de crear BufferedInputStream"); |
---|
| 455 | |
---|
| 456 | lenghtOfFile = httpsConnection.getContentLength(); |
---|
| 457 | Log.d("***", "se acepto la excepcion"); |
---|
| 458 | |
---|
[56b06e2] | 459 | */ |
---|
| 460 | |
---|
[6638d33] | 461 | }else{ |
---|
| 462 | HttpsURLConnection httpsConnection = (HttpsURLConnection) url.openConnection(); |
---|
| 463 | input = new BufferedInputStream(httpsConnection.getInputStream()); |
---|
| 464 | lenghtOfFile = httpsConnection.getContentLength(); |
---|
| 465 | } |
---|
| 466 | } |
---|
| 467 | |
---|
| 468 | |
---|
| 469 | // Crear directorio DownloadedFiles donde se almacenan los archivos descargados |
---|
| 470 | // desde un servidor para verificar su firma |
---|
| 471 | downloadedDirFiles = Environment.getExternalStorageDirectory() + "/" + |
---|
| 472 | getResources().getString(R.string.app_name) + "/" + |
---|
| 473 | getResources().getString(R.string.downloaded_dir_files) + "/"; |
---|
| 474 | prepareDirectory(downloadedDirFiles); |
---|
| 475 | |
---|
| 476 | //int lenghtOfFile = conexion.getContentLength(); |
---|
| 477 | Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile); |
---|
| 478 | |
---|
| 479 | //InputStream input = new BufferedInputStream(url.openStream()); |
---|
| 480 | //InputStream input = new BufferedInputStream(conexion.getInputStream()); |
---|
| 481 | |
---|
| 482 | //OutputStream output = new FileOutputStream("/mnt/sdcard/TibisayMovil/ExtractedFiles/photo.jpg"); |
---|
| 483 | |
---|
| 484 | //OutputStream output = new FileOutputStream(new File(rootDir+"/midescarga/", "foto.jpg")); |
---|
| 485 | |
---|
| 486 | String urlString = url.toString(); |
---|
| 487 | String[] values = urlString.split("/"); |
---|
| 488 | |
---|
| 489 | //OutputStream output = new FileOutputStream(new File(downloadedDirFiles, "acta.2.bdoc")); |
---|
| 490 | OutputStream output = new FileOutputStream(new File(downloadedDirFiles, values[values.length-1])); |
---|
| 491 | |
---|
| 492 | byte data[] = new byte[1024]; |
---|
| 493 | |
---|
| 494 | long total = 0; |
---|
| 495 | |
---|
| 496 | while ((count = input.read(data)) != -1) { |
---|
| 497 | total += count; |
---|
| 498 | publishProgress(""+(int)((total*100)/lenghtOfFile)); |
---|
| 499 | output.write(data, 0, count); |
---|
| 500 | } |
---|
| 501 | |
---|
| 502 | output.flush(); |
---|
| 503 | |
---|
| 504 | output.close(); |
---|
| 505 | input.close(); |
---|
| 506 | |
---|
| 507 | resultArray.add("0"); |
---|
| 508 | //resultArray.add(output.toString()); |
---|
| 509 | resultArray.add(downloadedDirFiles+values[values.length-1]); |
---|
| 510 | |
---|
| 511 | } catch (Exception e) { |
---|
| 512 | |
---|
| 513 | Log.e("Error: ", e.getMessage()); |
---|
| 514 | outputString = e.getMessage(); |
---|
| 515 | |
---|
| 516 | resultArray.add("1"); |
---|
| 517 | resultArray.add(e.toString()); |
---|
| 518 | |
---|
| 519 | } |
---|
| 520 | |
---|
| 521 | //return null; |
---|
| 522 | return resultArray; |
---|
| 523 | |
---|
| 524 | } |
---|
| 525 | |
---|
| 526 | protected void onProgressUpdate(String... progress) { |
---|
| 527 | Log.d("ANDRO_ASYNC",progress[0]); |
---|
| 528 | mProgressDialog.setProgress(Integer.parseInt(progress[0])); |
---|
| 529 | } |
---|
| 530 | |
---|
| 531 | @Override |
---|
| 532 | protected void onPostExecute(ArrayList<String> result) { |
---|
| 533 | dismissDialog(DIALOG_DOWNLOAD_PROGRESS); |
---|
| 534 | //Toast.makeText(getApplicationContext(), "onPostExecute: "+ result.get(0), Toast.LENGTH_LONG).show(); |
---|
| 535 | |
---|
| 536 | // ocurrio una excepcion |
---|
| 537 | if (result.get(0).equals("1")){ |
---|
| 538 | |
---|
| 539 | //Toast.makeText(getApplicationContext(), "onPostExecute: dentro del if" , Toast.LENGTH_LONG).show(); |
---|
| 540 | |
---|
| 541 | // ocurrio un problema |
---|
| 542 | AlertDialog.Builder builder = new AlertDialog.Builder(DownloaderActivity.this); |
---|
| 543 | |
---|
| 544 | String exception = ""; |
---|
| 545 | boolean fileFound = true; |
---|
| 546 | |
---|
| 547 | if (result.get(1).contains("Trust anchor for certification path not found")){ |
---|
| 548 | exception = "Intenta descargar un archivo de un servidor con certificado no confiable. ¿Desea continuar?."; |
---|
| 549 | |
---|
| 550 | } |
---|
| 551 | if (result.get(1).contains("java.io.FileNotFoundException")){ |
---|
| 552 | String [] v = result.get(1).split("/"); |
---|
| 553 | int length = v.length; |
---|
| 554 | |
---|
| 555 | exception = "No se encontró el archivo "+ v[length-1] +" en el servidor."; |
---|
| 556 | fileFound = false; |
---|
| 557 | } |
---|
| 558 | |
---|
| 559 | builder.setMessage(exception).setTitle("Error al descargar el archivo"); |
---|
| 560 | |
---|
| 561 | if (fileFound){ |
---|
| 562 | |
---|
| 563 | builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { |
---|
| 564 | public void onClick(DialogInterface dialog, int id) { |
---|
| 565 | // User clicked OK button |
---|
| 566 | |
---|
| 567 | Log.d("***", "DownloadFileAsync().execute(urlhttps, true)"); |
---|
[ba6bc7c] | 568 | //Toast.makeText(getApplicationContext(), "DownloadFileAsync().execute("+urlhttps+", true)", Toast.LENGTH_LONG).show(); |
---|
[6638d33] | 569 | // pasar como segundo argmento que se acepta que el sertivor es desconocido |
---|
| 570 | new DownloadFileAsync().execute(urlhttps, "true"); |
---|
| 571 | } |
---|
| 572 | }); |
---|
| 573 | |
---|
| 574 | } |
---|
| 575 | |
---|
| 576 | builder.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() { |
---|
| 577 | public void onClick(DialogInterface dialog, int id) { |
---|
| 578 | // User cancelled the dialog |
---|
| 579 | DownloaderActivity.this.finish(); |
---|
| 580 | } |
---|
| 581 | }); |
---|
| 582 | AlertDialog dialog = builder.create(); |
---|
| 583 | dialog.show(); |
---|
| 584 | |
---|
| 585 | }else{ |
---|
| 586 | // no ocurrió excepcion |
---|
[ba6bc7c] | 587 | //Toast.makeText(getApplicationContext(), "Se descargo correctamente el archivo", Toast.LENGTH_LONG).show(); |
---|
[6638d33] | 588 | |
---|
[ba6bc7c] | 589 | //Toast.makeText(getApplicationContext(), "result.get(0): "+result.get(0), Toast.LENGTH_LONG).show(); |
---|
| 590 | //Toast.makeText(getApplicationContext(), "result.get(1): "+result.get(1), Toast.LENGTH_LONG).show(); |
---|
| 591 | //Toast.makeText(getApplicationContext(), "downloadedDirFiles: "+downloadedDirFiles, Toast.LENGTH_LONG).show(); |
---|
[6638d33] | 592 | |
---|
| 593 | // lanzar activity para mostrar resultados de verificacion |
---|
| 594 | Intent intent = new Intent(DownloaderActivity.this, BDOCVerifyResultActivity.class); |
---|
| 595 | intent.putExtra("fileToVerify", result.get(1)); |
---|
[ba6bc7c] | 596 | //Toast.makeText(getApplicationContext(), "putExtra1: "+result.get(1), Toast.LENGTH_LONG).show(); |
---|
[6638d33] | 597 | |
---|
| 598 | File f = new File(result.get(1)); |
---|
| 599 | Uri selectedUri = Uri.fromFile(f); |
---|
| 600 | String fileExtension = MimeTypeMap.getFileExtensionFromUrl(selectedUri.toString()); |
---|
| 601 | intent.putExtra("fileExtension", fileExtension); |
---|
| 602 | |
---|
[ba6bc7c] | 603 | //Toast.makeText(getApplicationContext(), "putExtra2: "+fileExtension, Toast.LENGTH_LONG).show(); |
---|
[6638d33] | 604 | |
---|
| 605 | startActivity(intent); |
---|
| 606 | |
---|
| 607 | DownloaderActivity.this.finish(); |
---|
| 608 | } |
---|
| 609 | |
---|
| 610 | |
---|
| 611 | } |
---|
| 612 | |
---|
| 613 | |
---|
| 614 | |
---|
| 615 | |
---|
| 616 | |
---|
| 617 | |
---|
| 618 | } // fin de la clase DownloadFileAsync |
---|
| 619 | |
---|
| 620 | |
---|
| 621 | } // fin de DownloaderActivity |
---|