[5147e01] | 1 | package ve.gob.cenditel.tibisaymovil; |
---|
| 2 | |
---|
| 3 | import java.io.BufferedInputStream; |
---|
| 4 | import java.io.File; |
---|
| 5 | import java.io.FileInputStream; |
---|
| 6 | import java.io.FileOutputStream; |
---|
[c95e4a6] | 7 | import java.io.IOException; |
---|
[5147e01] | 8 | import java.io.InputStream; |
---|
[c95e4a6] | 9 | import java.io.OutputStream; |
---|
| 10 | import java.net.HttpURLConnection; |
---|
| 11 | import java.net.MalformedURLException; |
---|
| 12 | import java.net.URL; |
---|
| 13 | import java.net.URLConnection; |
---|
[6638d33] | 14 | import java.security.KeyStore; |
---|
| 15 | import java.security.cert.Certificate; |
---|
| 16 | import java.security.cert.CertificateFactory; |
---|
[5147e01] | 17 | import java.util.ArrayList; |
---|
| 18 | |
---|
[c95e4a6] | 19 | import javax.net.ssl.HttpsURLConnection; |
---|
[6638d33] | 20 | import javax.net.ssl.SSLContext; |
---|
| 21 | import javax.net.ssl.TrustManagerFactory; |
---|
[c95e4a6] | 22 | |
---|
[88eadd6] | 23 | |
---|
[5147e01] | 24 | import ee.sk.digidoc.DataFile; |
---|
[88eadd6] | 25 | import ee.sk.digidoc.DigiDocException; |
---|
[5147e01] | 26 | import ee.sk.digidoc.Signature; |
---|
| 27 | import ee.sk.digidoc.SignedDoc; |
---|
| 28 | import ee.sk.digidoc.factory.DigiDocFactory; |
---|
| 29 | import ee.sk.utils.ConfigManager; |
---|
| 30 | import android.net.Uri; |
---|
[c95e4a6] | 31 | import android.os.AsyncTask; |
---|
[5147e01] | 32 | import android.os.Bundle; |
---|
| 33 | import android.os.Environment; |
---|
[c95e4a6] | 34 | import android.os.Looper; |
---|
[5147e01] | 35 | import android.app.Activity; |
---|
[6638d33] | 36 | import android.app.AlertDialog; |
---|
[c95e4a6] | 37 | import android.app.Dialog; |
---|
| 38 | import android.app.ProgressDialog; |
---|
[5147e01] | 39 | import android.content.Context; |
---|
[6638d33] | 40 | import android.content.DialogInterface; |
---|
[5147e01] | 41 | import android.content.Intent; |
---|
| 42 | import android.util.Log; |
---|
| 43 | import android.view.LayoutInflater; |
---|
| 44 | import android.view.Menu; |
---|
| 45 | import android.view.View; |
---|
| 46 | import android.view.ViewGroup; |
---|
| 47 | import android.view.Window; |
---|
| 48 | import android.view.View.OnClickListener; |
---|
| 49 | import android.webkit.MimeTypeMap; |
---|
| 50 | import android.widget.AdapterView; |
---|
| 51 | import android.widget.BaseAdapter; |
---|
| 52 | import android.widget.EditText; |
---|
| 53 | import android.widget.ImageView; |
---|
| 54 | import android.widget.LinearLayout; |
---|
| 55 | import android.widget.ListView; |
---|
| 56 | import android.widget.TextView; |
---|
| 57 | import android.widget.Toast; |
---|
| 58 | |
---|
| 59 | public class BDOCVerifyResultActivity extends Activity { |
---|
| 60 | |
---|
| 61 | |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | // linear |
---|
| 65 | private LinearLayout button_share; |
---|
| 66 | private LinearLayout button_finish; |
---|
| 67 | private ListView documents_list; |
---|
[88eadd6] | 68 | private ImageView arrowShowErrors; |
---|
[5147e01] | 69 | |
---|
| 70 | // cadena que mantiene la ruta para almacenar los archivos |
---|
| 71 | // extraidos de un contendor BDOC |
---|
| 72 | private String extractedDirFiles; |
---|
| 73 | |
---|
[6638d33] | 74 | // cadena que mantiene la ruta para almacenar los archivos |
---|
| 75 | // descargados desde un servidor para verificar su firma |
---|
| 76 | private String downloadedDirFiles; |
---|
| 77 | |
---|
[5147e01] | 78 | // ruta absoluta al archivo a verificar |
---|
| 79 | private String fileToVerify; |
---|
| 80 | |
---|
| 81 | // extension del archivo a verificar |
---|
| 82 | private String fileToVerifyExtension; |
---|
| 83 | |
---|
| 84 | |
---|
| 85 | // mimetype del archivo a verificar |
---|
| 86 | private String mimeTypeFileToVerify; |
---|
| 87 | |
---|
[88eadd6] | 88 | |
---|
| 89 | private ArrayList<SignatureErrorDetail> errorList; |
---|
| 90 | |
---|
[c95e4a6] | 91 | // Progress Dialog |
---|
[6638d33] | 92 | private ProgressDialog mProgressDialog; |
---|
[c95e4a6] | 93 | |
---|
| 94 | // Progress dialog type (0 - for Horizontal progress bar) |
---|
[6638d33] | 95 | public static final int DIALOG_DOWNLOAD_PROGRESS = 0; |
---|
| 96 | |
---|
| 97 | //String urlhttps = "https://tibisay.cenditel.gob.ve/trac/raw-attachment/wiki/WikiStart/actados.2.bdoc"; |
---|
| 98 | String urlhttps = null; |
---|
[c95e4a6] | 99 | |
---|
[5147e01] | 100 | @Override |
---|
| 101 | protected void onCreate(Bundle savedInstanceState) { |
---|
| 102 | |
---|
| 103 | //Estilando la barra de titulo |
---|
| 104 | final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); |
---|
| 105 | Log.d("onCreate", "antes de super"); |
---|
| 106 | super.onCreate(savedInstanceState); |
---|
| 107 | |
---|
| 108 | setContentView(R.layout.activity_verify_result_bdoc); |
---|
| 109 | |
---|
| 110 | //Estilando Barra de titulo |
---|
| 111 | if(customTitleSupported) |
---|
| 112 | getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar); |
---|
| 113 | |
---|
[88eadd6] | 114 | arrowShowErrors = (ImageView) this.findViewById(R.id.arrow_show_errors); |
---|
| 115 | arrowShowErrors.setVisibility(ImageView.GONE); |
---|
| 116 | |
---|
[4528953] | 117 | button_share = (LinearLayout) this.findViewById(R.id.button_remove_certificate_zone); |
---|
| 118 | button_finish = (LinearLayout) this.findViewById(R.id.button_add_certificate_zone); |
---|
[5147e01] | 119 | |
---|
[c95e4a6] | 120 | // inicializa la lista de objetos de errores de firma |
---|
[95d372e] | 121 | errorList = new ArrayList<SignatureErrorDetail>(); |
---|
[c95e4a6] | 122 | |
---|
[88eadd6] | 123 | |
---|
| 124 | // habilita el click sobre la acción de finalizar la verificacion de firma y regresar |
---|
[5147e01] | 125 | button_finish.setOnClickListener(new OnClickListener() |
---|
| 126 | { |
---|
| 127 | public void onClick(View v) |
---|
| 128 | { |
---|
| 129 | |
---|
| 130 | Intent oIntent = new Intent(BDOCVerifyResultActivity.this, TibisayMovilActivity.class); |
---|
| 131 | oIntent.setAction(Intent.ACTION_VIEW); |
---|
| 132 | oIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); |
---|
| 133 | startActivity(oIntent); |
---|
| 134 | |
---|
| 135 | finish(); |
---|
| 136 | } |
---|
| 137 | }); |
---|
[88eadd6] | 138 | |
---|
| 139 | // habilita el click sobre la acción de compartir el archivo |
---|
[5147e01] | 140 | button_share.setOnClickListener(new OnClickListener() |
---|
| 141 | { |
---|
| 142 | public void onClick(View v) |
---|
| 143 | { |
---|
| 144 | shareIt(); |
---|
| 145 | } |
---|
| 146 | }); |
---|
| 147 | |
---|
[88eadd6] | 148 | // habilita el click sobre la flecha para mostrar los errores de la firma |
---|
| 149 | arrowShowErrors.setOnClickListener(new OnClickListener(){ |
---|
| 150 | |
---|
| 151 | public void onClick(View arg0) { |
---|
| 152 | |
---|
| 153 | Intent intent = new Intent(BDOCVerifyResultActivity.this, ActivitySignatureErrors.class); |
---|
| 154 | |
---|
[8860b8f] | 155 | |
---|
[88eadd6] | 156 | |
---|
| 157 | intent.setClass(BDOCVerifyResultActivity.this, ActivitySignatureErrors.class); |
---|
| 158 | intent.putExtra("SignatureErrorDetailList", errorList); |
---|
[8860b8f] | 159 | intent.putExtra("numeroObjetoError", Integer.toString(errorList.size())); |
---|
[88eadd6] | 160 | |
---|
| 161 | startActivity(intent); |
---|
| 162 | |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | }); |
---|
[c95e4a6] | 166 | |
---|
| 167 | // chequear si intent tiene data |
---|
| 168 | final android.content.Intent intent = getIntent(); |
---|
| 169 | |
---|
| 170 | final Bundle bundle = getIntent().getExtras(); |
---|
| 171 | |
---|
| 172 | if (intent != null) { |
---|
| 173 | Toast.makeText(getApplicationContext(), "intent != null", Toast.LENGTH_SHORT).show(); |
---|
| 174 | |
---|
| 175 | final android.net.Uri data = intent.getData (); |
---|
| 176 | |
---|
| 177 | if (data != null) { |
---|
| 178 | Toast.makeText(getApplicationContext(), "data != null", Toast.LENGTH_SHORT).show(); |
---|
| 179 | |
---|
| 180 | // verificar el tipo de scheme |
---|
| 181 | String scheme = data.getScheme(); |
---|
| 182 | |
---|
| 183 | // verificacion de un archivo que esta en el dispositivo |
---|
| 184 | if (scheme.equals("file")) { |
---|
| 185 | Toast.makeText(getApplicationContext(), "file: "+data.getPath(), Toast.LENGTH_SHORT).show(); |
---|
| 186 | |
---|
| 187 | // verificar el archivo |
---|
| 188 | if (data.getPath().endsWith("bdoc")){ |
---|
[308adc7] | 189 | fileToVerify = data.getPath(); |
---|
| 190 | |
---|
| 191 | // ejecutar la verificacion |
---|
| 192 | doBdocVerification(data.getPath()); |
---|
[c95e4a6] | 193 | }else{ |
---|
[e68708b] | 194 | Toast.makeText(getApplicationContext(), "¡Por Implementar! scheme == file", Toast.LENGTH_SHORT).show(); |
---|
[c95e4a6] | 195 | |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | // verificacion de un archivo que se debe descargar |
---|
| 201 | if (scheme.equals("https")) { |
---|
| 202 | Toast.makeText(getApplicationContext(), "scheme: "+data.toString(), Toast.LENGTH_SHORT).show(); |
---|
| 203 | |
---|
| 204 | Toast.makeText(getApplicationContext(), "externalStorage: "+Environment.getExternalStorageDirectory().toString(), Toast.LENGTH_SHORT).show(); |
---|
| 205 | |
---|
[6638d33] | 206 | urlhttps = data.toString(); |
---|
| 207 | new DownloadFileAsync().execute(data.toString(), "false"); |
---|
[c95e4a6] | 208 | |
---|
| 209 | //new DownloadFileFromURL().execute("http://farm1.static.flickr.com/114/298125983_0e4bf66782_b.jpg"); |
---|
| 210 | |
---|
| 211 | //Toast.makeText(getApplicationContext(), "****despues de execute()", Toast.LENGTH_SHORT).show(); |
---|
| 212 | // verificar el archivo |
---|
[6638d33] | 213 | Toast.makeText(getApplicationContext(), "Ahora viene la verificación del archivo", Toast.LENGTH_SHORT).show(); |
---|
[c95e4a6] | 214 | } |
---|
| 215 | |
---|
| 216 | |
---|
| 217 | return; |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | // verificacion de archivo desde la Activity principal |
---|
| 221 | if (bundle != null) { |
---|
| 222 | fileToVerify = bundle.getString("fileToVerify"); |
---|
| 223 | fileToVerifyExtension = bundle.getString("fileExtension"); |
---|
| 224 | mimeTypeFileToVerify = getMimeType(fileToVerify); |
---|
| 225 | |
---|
[6638d33] | 226 | Toast.makeText(getApplicationContext(), "BDOCVerifyResultActivity fileToVerify: "+fileToVerify, Toast.LENGTH_SHORT).show(); |
---|
| 227 | Toast.makeText(getApplicationContext(), "BDOCVerifyResultActivity fileToVerifyExtension: "+fileToVerifyExtension, Toast.LENGTH_SHORT).show(); |
---|
[c95e4a6] | 228 | |
---|
| 229 | if (fileToVerifyExtension.equals("bdoc")){ |
---|
[6638d33] | 230 | Toast.makeText(getApplicationContext(), "BDOCVerifyResultActivity: -> doBdocVerification(fileToVerify)", Toast.LENGTH_SHORT).show(); |
---|
[c95e4a6] | 231 | // ejecutar la verificacion |
---|
| 232 | doBdocVerification(fileToVerify); |
---|
| 233 | }else{ |
---|
[e68708b] | 234 | Toast.makeText(getApplicationContext(), "¡Por Implementar bundle != null!", Toast.LENGTH_SHORT).show(); |
---|
[c95e4a6] | 235 | |
---|
[e68708b] | 236 | // ejecutar la verificacion del pdf firmado |
---|
| 237 | doPdfVerification(fileToVerify); |
---|
[c95e4a6] | 238 | } |
---|
| 239 | |
---|
| 240 | //return; |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | }else{ |
---|
| 244 | Toast.makeText(getApplicationContext(), "intent == null", Toast.LENGTH_SHORT).show(); |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | |
---|
| 248 | //Bundle bundle=getIntent().getExtras(); |
---|
| 249 | //fileToVerify = bundle.getString("fileToVerify"); |
---|
| 250 | //fileToVerifyExtension = bundle.getString("fileExtension"); |
---|
| 251 | //mimeTypeFileToVerify = getMimeType(fileToVerify); |
---|
| 252 | //Toast.makeText(getApplicationContext(), "fileToVerify: "+fileToVerify, Toast.LENGTH_SHORT).show(); |
---|
| 253 | |
---|
| 254 | |
---|
| 255 | /* |
---|
| 256 | if (fileToVerifyExtension.equals("bdoc")){ |
---|
| 257 | // ejecutar la verificacion |
---|
| 258 | doBdocVerification(fileToVerify); |
---|
| 259 | }else{ |
---|
| 260 | Toast.makeText(getApplicationContext(), "¡Por Implementar!", Toast.LENGTH_SHORT).show(); |
---|
| 261 | |
---|
| 262 | } |
---|
| 263 | */ |
---|
| 264 | |
---|
| 265 | |
---|
[5147e01] | 266 | |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | |
---|
| 270 | |
---|
| 271 | |
---|
| 272 | |
---|
[c95e4a6] | 273 | @Override |
---|
| 274 | public void onStart () { |
---|
| 275 | |
---|
| 276 | android.util.Log.d ("**onStart(): ", " onStart"); |
---|
| 277 | super.onStart (); |
---|
| 278 | |
---|
| 279 | |
---|
| 280 | final android.content.Intent intent = getIntent (); |
---|
| 281 | |
---|
| 282 | if (intent != null) |
---|
| 283 | { |
---|
| 284 | android.util.Log.d ("**onStart(): ", "> Got intent : " + intent); |
---|
| 285 | |
---|
| 286 | final android.net.Uri data = intent.getData (); |
---|
| 287 | |
---|
| 288 | if (data != null) |
---|
| 289 | { |
---|
| 290 | android.util.Log.d ("**onStart(): ", "> Got data : " + data); |
---|
| 291 | |
---|
| 292 | final String filePath = data.getEncodedPath (); |
---|
| 293 | |
---|
| 294 | android.util.Log.d ("**onStart(): ", "> Open file : " + filePath); |
---|
| 295 | |
---|
| 296 | // file loading comes here. |
---|
| 297 | } // if |
---|
| 298 | } // if |
---|
| 299 | android.util.Log.d ("**onStart(): ", "- saliendo onStart"); |
---|
| 300 | return; |
---|
| 301 | |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | |
---|
[5147e01] | 305 | |
---|
| 306 | // funcion para verificar un documento |
---|
| 307 | private void doBdocVerification(String absolutePathFileToVerify){ |
---|
| 308 | |
---|
[6638d33] | 309 | Toast.makeText(getApplicationContext(), "BDOCVerifyResultActivity: dentro doBdocVerification(fileToVerify)"+absolutePathFileToVerify, Toast.LENGTH_SHORT).show(); |
---|
[5147e01] | 310 | try { |
---|
| 311 | Log.d("doBdocVerification:", "Inicializando jdigidoc ..."); |
---|
| 312 | ConfigManager.init("jar://jdigidoc.cfg"); |
---|
| 313 | Log.d("despues de:", "ConfigManager.init"); |
---|
| 314 | |
---|
| 315 | Log.d("antes de:", "digFac"); |
---|
| 316 | DigiDocFactory digFac = ConfigManager.instance().getDigiDocFactory(); |
---|
| 317 | |
---|
| 318 | Log.d("getFilesDir:", getFilesDir().getAbsolutePath()); |
---|
| 319 | |
---|
| 320 | Log.d("PATH", Environment.getExternalStorageDirectory().toString()); |
---|
| 321 | |
---|
| 322 | File file = new File(absolutePathFileToVerify); |
---|
| 323 | InputStream selectedFile = null; |
---|
| 324 | selectedFile = new BufferedInputStream(new FileInputStream(file)); |
---|
| 325 | |
---|
| 326 | SignedDoc sdoc = digFac.readSignedDocFromStreamOfType(selectedFile, true); |
---|
| 327 | selectedFile.close(); |
---|
| 328 | |
---|
| 329 | //SignedDoc sdoc = digFac.readSignedDocFromStreamOfType(fraw, true); |
---|
| 330 | Signature signature = sdoc.getSignature(0); |
---|
| 331 | |
---|
[824553a] | 332 | // TextView para colocar el resultado de la firma |
---|
[fd4a5b9] | 333 | TextView signatureVerificationResult = (TextView) findViewById(R.id.archivo_original_a_descifrar); |
---|
[824553a] | 334 | |
---|
| 335 | |
---|
| 336 | // array de excepciones lanzadas al verificar firmas |
---|
| 337 | ArrayList arraySignaturesErrors = null; |
---|
[8860b8f] | 338 | |
---|
[824553a] | 339 | // verificar cada una de las firmas del contenedor |
---|
| 340 | for (int i = 0; i < sdoc.countSignatures(); i++){ |
---|
| 341 | Signature tmpSignature = sdoc.getSignature(i); |
---|
| 342 | arraySignaturesErrors = new ArrayList(); |
---|
| 343 | arraySignaturesErrors = tmpSignature.verify(sdoc, false, false); |
---|
[8860b8f] | 344 | Log.d("**La Firma: "+Integer.toString(i),"tiene : "+ arraySignaturesErrors.size()+" errores."); |
---|
[88eadd6] | 345 | |
---|
[8860b8f] | 346 | // lista de cadenas que almancenara los errores para el firmante i |
---|
| 347 | ArrayList<String> signatureVerificationErrors = new ArrayList<String>(); |
---|
[88eadd6] | 348 | |
---|
[8860b8f] | 349 | for (int j = 0; j < arraySignaturesErrors.size(); j++){ |
---|
| 350 | // agregar la cadena de error particular a la lista |
---|
[95d372e] | 351 | signatureVerificationErrors.add(arraySignaturesErrors.get(j).toString() + "\n"); |
---|
[824553a] | 352 | } |
---|
| 353 | |
---|
[88eadd6] | 354 | //String cn = SignedDoc.getCommonName(sdoc.getSignature(i).getKeyInfo().getSignersCertificate().getSubjectDN().getName()); |
---|
[8860b8f] | 355 | String signer = sdoc.getSignature(i).getKeyInfo().getSubjectLastName(); |
---|
[88eadd6] | 356 | |
---|
[8860b8f] | 357 | // crear objeto SignatureErrorDetail con detalles de error para la firma i |
---|
| 358 | SignatureErrorDetail object = new SignatureErrorDetail(signer, signatureVerificationErrors); |
---|
[88eadd6] | 359 | |
---|
[8860b8f] | 360 | // agregar el objeto SignatureErrorDetail a lista |
---|
[95d372e] | 361 | errorList.add(object); |
---|
[88eadd6] | 362 | |
---|
[8860b8f] | 363 | } // fin del for para la firma i |
---|
[824553a] | 364 | |
---|
| 365 | if (arraySignaturesErrors.isEmpty()) { |
---|
[8860b8f] | 366 | signatureVerificationResult.setText(getResources().getString(R.string.string_valid_signature)); |
---|
[824553a] | 367 | Log.d("signature.verify", "NO HUBO ERROR EN LA VERIFICACION DE FIRMA"); |
---|
| 368 | Toast.makeText(getApplicationContext(), "¡FIRMA CORRECTA!", Toast.LENGTH_SHORT).show(); |
---|
| 369 | |
---|
| 370 | }else{ |
---|
[88eadd6] | 371 | |
---|
| 372 | // habilitar la flecha para mostrar los errores de firma |
---|
| 373 | arrowShowErrors.setVisibility(ImageView.VISIBLE); |
---|
| 374 | |
---|
[8860b8f] | 375 | //signatureVerificationResult.setText("Inválida."); |
---|
| 376 | signatureVerificationResult.setText(getResources().getString(R.string.string_invalid_signature)); |
---|
[824553a] | 377 | Log.d("************", "SI HUBO ERROR"); |
---|
| 378 | |
---|
| 379 | Toast.makeText(getApplicationContext(), "ERROR DE VERIFICACION DE FIRMA", Toast.LENGTH_SHORT).show(); |
---|
| 380 | } |
---|
| 381 | |
---|
[5147e01] | 382 | |
---|
| 383 | //ArrayList sigError = signature.verify(sdoc, false, false); |
---|
[824553a] | 384 | //ArrayList sigError = sdoc.verify(false, false); |
---|
[5147e01] | 385 | |
---|
| 386 | // se crea el adaptador para mostrar los documentos firmados |
---|
| 387 | ListView documentListView = (ListView) findViewById(R.id.documents_list); |
---|
| 388 | final ArrayList<DocumentFromBDOC> docs = new ArrayList<DocumentFromBDOC>(); |
---|
| 389 | |
---|
| 390 | // Crear directorio ExtractedFiles donde se almacenan los archivos extraidos |
---|
| 391 | // de los contenedores BDOC |
---|
| 392 | extractedDirFiles = Environment.getExternalStorageDirectory() + "/" + |
---|
| 393 | getResources().getString(R.string.app_name) + "/" + |
---|
| 394 | getResources().getString(R.string.extracted_dir_files) + "/"; |
---|
[6638d33] | 395 | prepareDirectory(extractedDirFiles); |
---|
[5147e01] | 396 | |
---|
| 397 | for (int i = 0; i < sdoc.countDataFiles(); i++){ |
---|
| 398 | // obtener el DataFile |
---|
| 399 | DataFile df = sdoc.getDataFile(i); |
---|
| 400 | docs.add(new DocumentFromBDOC(df.getFileName())); |
---|
| 401 | Log.d("MIMETYPE:", df.getMimeType()); |
---|
| 402 | // Extraer el archivo y guardarlo en /mnt/sdcard/TibisayMovil/ExtractedFiles |
---|
| 403 | FileOutputStream fos = new FileOutputStream(extractedDirFiles+df.getFileName()); |
---|
| 404 | InputStream is = df.getBodyAsStream(); |
---|
| 405 | if(is == null) { |
---|
| 406 | Log.d("ERROR al extrarer archivo", "DataFile has no data!"); |
---|
| 407 | //return false; |
---|
| 408 | } |
---|
| 409 | byte[] data = new byte[4096]; |
---|
| 410 | int n = 0, m = 0; |
---|
| 411 | while((n = is.read(data)) > 0) { |
---|
| 412 | fos.write(data, 0, n); |
---|
| 413 | m += n; |
---|
| 414 | } |
---|
| 415 | fos.close(); |
---|
| 416 | is.close(); |
---|
| 417 | } |
---|
| 418 | |
---|
| 419 | DocumentFromBDOCAdapter documentAdapter = new DocumentFromBDOCAdapter(BDOCVerifyResultActivity.this, docs); |
---|
| 420 | documentListView.setAdapter(documentAdapter); |
---|
| 421 | documentListView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() { |
---|
| 422 | |
---|
| 423 | @Override |
---|
| 424 | public void onItemClick(AdapterView<?> parent, View view, |
---|
| 425 | int position, long id) { |
---|
| 426 | String absolutePathToOpen = extractedDirFiles + docs.get(position).getName(); |
---|
| 427 | Toast.makeText(getApplicationContext(), getMimeType(absolutePathToOpen), Toast.LENGTH_SHORT).show(); |
---|
| 428 | openIt(absolutePathToOpen, getMimeType(absolutePathToOpen)); |
---|
| 429 | |
---|
| 430 | |
---|
| 431 | } |
---|
| 432 | |
---|
| 433 | }); |
---|
| 434 | |
---|
| 435 | // se crea el adaptador para mostrar los firmantes del contenedor |
---|
| 436 | ListView signerListView = (ListView) findViewById(R.id.signatures_list); |
---|
[064d990] | 437 | final ArrayList<SignerFromBDOC> signers = new ArrayList<SignerFromBDOC>(); |
---|
[5147e01] | 438 | |
---|
| 439 | for (int i = 0; i < sdoc.countSignatures(); i++){ |
---|
| 440 | Signature signatureTemp = sdoc.getSignature(i); |
---|
[064d990] | 441 | //signers.add(new SignerFromBDOC(signatureTemp.getSubject())); |
---|
| 442 | |
---|
| 443 | String cn = SignedDoc.getCommonName( |
---|
| 444 | sdoc.getSignature(i).getKeyInfo().getSignersCertificate().getSubjectDN().getName()); |
---|
| 445 | String name = sdoc.getSignature(i).getKeyInfo().getSubjectLastName(); |
---|
| 446 | String date = sdoc.getSignature(i).getSignedProperties().getSigningTime().toString(); |
---|
| 447 | String certificateSerial = sdoc.getSignature(i).getKeyInfo().getSignersCertificate().getSerialNumber().toString(); |
---|
| 448 | String validFrom = sdoc.getSignature(i).getKeyInfo().getSignersCertificate().getNotBefore().toLocaleString(); |
---|
| 449 | String validUntil = sdoc.getSignature(i).getKeyInfo().getSignersCertificate().getNotAfter().toLocaleString(); |
---|
| 450 | String certificateIssuer = sdoc.getSignature(i).getKeyInfo().getSignersCertificate().getIssuerX500Principal().toString(); |
---|
| 451 | |
---|
| 452 | // agregar el nombre comun del firmante y la fecha y la hora |
---|
| 453 | signers.add(new SignerFromBDOC(name, cn, date, certificateSerial, validFrom, validUntil, certificateIssuer)); |
---|
| 454 | |
---|
[5147e01] | 455 | } |
---|
| 456 | SignerFromBDOCAdapter signerAdapter = new SignerFromBDOCAdapter(BDOCVerifyResultActivity.this, signers); |
---|
| 457 | signerListView.setAdapter(signerAdapter); |
---|
| 458 | signerListView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() { |
---|
| 459 | |
---|
[c95e4a6] | 460 | |
---|
[5147e01] | 461 | @Override |
---|
| 462 | public void onItemClick(AdapterView<?> parent, View view, |
---|
| 463 | int position, long id) { |
---|
| 464 | |
---|
| 465 | //String signer = parent.getItemAtPosition(position).toString(); |
---|
| 466 | |
---|
| 467 | Toast.makeText(getApplicationContext(), "SE DEBE MOSTRAR INFORMACIÓN DEL FIRMANTE", Toast.LENGTH_SHORT).show(); |
---|
[064d990] | 468 | showSignatureDetails(position, signers); |
---|
[5147e01] | 469 | } |
---|
| 470 | |
---|
| 471 | }); |
---|
| 472 | |
---|
| 473 | |
---|
| 474 | |
---|
| 475 | } catch (Exception e) { |
---|
| 476 | // TODO Auto-generated catch block |
---|
| 477 | |
---|
| 478 | //showOneButtonDialog(e.getMessage()); |
---|
| 479 | |
---|
[6638d33] | 480 | Log.d("****exception:", e.getMessage()); |
---|
[5147e01] | 481 | e.printStackTrace(); |
---|
[6638d33] | 482 | Toast.makeText(getApplicationContext(), "BDOCVerifyResultActivity: "+e.getMessage(), Toast.LENGTH_LONG).show(); |
---|
[5147e01] | 483 | } |
---|
| 484 | |
---|
| 485 | } // fin de doVerification(String f) |
---|
| 486 | |
---|
| 487 | |
---|
| 488 | |
---|
| 489 | |
---|
| 490 | |
---|
| 491 | |
---|
| 492 | |
---|
| 493 | |
---|
| 494 | |
---|
| 495 | |
---|
| 496 | // clase que abstrae un documento incluido dentro de un contenedor BDOC |
---|
| 497 | public class DocumentFromBDOC { |
---|
| 498 | private String mName; |
---|
| 499 | |
---|
| 500 | public DocumentFromBDOC(String name){ |
---|
| 501 | mName = name; |
---|
| 502 | } |
---|
| 503 | |
---|
| 504 | public void setName(String name) { |
---|
| 505 | this.mName = name; |
---|
| 506 | } |
---|
| 507 | |
---|
| 508 | public String getName() { |
---|
| 509 | return mName; |
---|
| 510 | } |
---|
| 511 | } // fin de clase DocumentFromBDOC |
---|
| 512 | |
---|
| 513 | // clase que abstrae el adaptador para llenar el ListView de documentos que |
---|
| 514 | // se encuentran dentro del contenedor BDOC |
---|
| 515 | private class DocumentFromBDOCAdapter extends BaseAdapter { |
---|
| 516 | |
---|
| 517 | private ArrayList<DocumentFromBDOC> mDocumentFromBDOCList; |
---|
| 518 | private LayoutInflater lInflater; |
---|
| 519 | |
---|
| 520 | private DocumentFromBDOCAdapter(Context context, ArrayList<DocumentFromBDOC> documents) { |
---|
| 521 | this.lInflater = LayoutInflater.from(context); |
---|
| 522 | this.mDocumentFromBDOCList = documents; |
---|
| 523 | |
---|
| 524 | } |
---|
| 525 | |
---|
| 526 | |
---|
| 527 | @Override |
---|
| 528 | public int getCount() { |
---|
| 529 | return mDocumentFromBDOCList.size(); |
---|
| 530 | } |
---|
| 531 | |
---|
| 532 | @Override |
---|
| 533 | public Object getItem(int position) { |
---|
| 534 | return mDocumentFromBDOCList.get(position); |
---|
| 535 | } |
---|
| 536 | |
---|
| 537 | @Override |
---|
| 538 | public long getItemId(int position) { |
---|
| 539 | return position; |
---|
| 540 | } |
---|
| 541 | |
---|
| 542 | @Override |
---|
| 543 | public View getView(int position, View convertView, ViewGroup parent) { |
---|
| 544 | ViewHolder container = null; |
---|
| 545 | |
---|
| 546 | if (convertView == null){ |
---|
| 547 | container = new ViewHolder(); |
---|
| 548 | convertView = lInflater.inflate(R.layout.document_inside_bdoc_item, null); |
---|
| 549 | |
---|
| 550 | container.filename_text = (TextView) convertView.findViewById(R.id.filename_text); |
---|
| 551 | container.filename_modified = (TextView) convertView.findViewById(R.id.filename_modified); |
---|
| 552 | container.type_image = (ImageView) convertView.findViewById(R.id.type_image); |
---|
| 553 | convertView.setTag(container); |
---|
| 554 | }else{ |
---|
| 555 | container = (ViewHolder) convertView.getTag(); |
---|
| 556 | } |
---|
| 557 | |
---|
| 558 | DocumentFromBDOC doc = (DocumentFromBDOC) getItem(position); |
---|
| 559 | container.filename_text.setText(doc.getName()); |
---|
| 560 | container.filename_modified.setText(doc.getName()); |
---|
| 561 | container.type_image.setImageResource(R.drawable.ic_archivo); |
---|
| 562 | |
---|
| 563 | return convertView; |
---|
| 564 | } |
---|
| 565 | |
---|
| 566 | |
---|
| 567 | class ViewHolder{ |
---|
| 568 | TextView filename_text; |
---|
| 569 | TextView filename_modified; |
---|
| 570 | ImageView type_image; |
---|
| 571 | } |
---|
| 572 | |
---|
| 573 | } |
---|
| 574 | |
---|
| 575 | |
---|
| 576 | // class que abstrae un firmante incluido en un contenedor BDOC |
---|
| 577 | private class SignerFromBDOC { |
---|
[064d990] | 578 | |
---|
| 579 | // nombre del firmante |
---|
[5147e01] | 580 | private String mName; |
---|
| 581 | |
---|
[064d990] | 582 | // nombre comun del firmante |
---|
| 583 | private String mCN; |
---|
| 584 | |
---|
| 585 | // fecha y hora de la firma |
---|
| 586 | private String mDate; |
---|
| 587 | |
---|
| 588 | // serial del certificado |
---|
| 589 | private String mCertificateSerial; |
---|
| 590 | |
---|
| 591 | // fecha de validez inicial |
---|
| 592 | private String mValidFrom; |
---|
| 593 | |
---|
| 594 | // fecha de validez final |
---|
| 595 | private String mValidUntil; |
---|
| 596 | |
---|
| 597 | // emisor del certificado |
---|
| 598 | private String mCertificateIssuer; |
---|
| 599 | |
---|
| 600 | public SignerFromBDOC(String name, String cn, String date, String serial, String validFrom, String validUntil, String issuer){ |
---|
[5147e01] | 601 | mName = name; |
---|
[064d990] | 602 | mCN = cn; |
---|
| 603 | mDate = date; |
---|
| 604 | mCertificateSerial = serial; |
---|
| 605 | mValidFrom = validFrom; |
---|
| 606 | mValidUntil = validUntil; |
---|
| 607 | mCertificateIssuer = issuer; |
---|
[5147e01] | 608 | } |
---|
| 609 | |
---|
| 610 | public void setName(String name) { |
---|
| 611 | this.mName = name; |
---|
| 612 | } |
---|
| 613 | |
---|
| 614 | public String getName() { |
---|
| 615 | return mName; |
---|
| 616 | } |
---|
[064d990] | 617 | |
---|
| 618 | public void setCN(String cn) { |
---|
| 619 | this.mCN = cn; |
---|
| 620 | } |
---|
| 621 | |
---|
| 622 | public String getCN() { |
---|
| 623 | return mCN; |
---|
| 624 | } |
---|
| 625 | |
---|
| 626 | public void setDate(String date) { |
---|
| 627 | this.mDate = date; |
---|
| 628 | } |
---|
| 629 | |
---|
| 630 | public String getDate() { |
---|
| 631 | return mDate; |
---|
| 632 | } |
---|
| 633 | |
---|
| 634 | public void setCertificateSerial(String certSerial) { |
---|
| 635 | this.mCertificateSerial = certSerial; |
---|
| 636 | } |
---|
| 637 | |
---|
| 638 | public String getCertificateSerial() { |
---|
| 639 | return mCertificateSerial; |
---|
| 640 | } |
---|
| 641 | |
---|
| 642 | public void setValidFrom(String validFrom) { |
---|
| 643 | this.mValidFrom = validFrom; |
---|
| 644 | } |
---|
| 645 | |
---|
| 646 | public String getValidFrom() { |
---|
| 647 | return mValidFrom; |
---|
| 648 | } |
---|
| 649 | |
---|
| 650 | public void setValidUntil(String validUntil) { |
---|
| 651 | this.mValidUntil = validUntil; |
---|
| 652 | } |
---|
| 653 | |
---|
| 654 | public String getValidUntil() { |
---|
| 655 | return mValidUntil; |
---|
| 656 | } |
---|
| 657 | |
---|
| 658 | public void setCertificateIssuer(String certIssuer) { |
---|
| 659 | this.mCertificateIssuer = certIssuer; |
---|
| 660 | } |
---|
| 661 | |
---|
| 662 | public String getCertificateIssuer() { |
---|
| 663 | return mCertificateIssuer; |
---|
| 664 | } |
---|
| 665 | |
---|
| 666 | } // fin de clase SignerFromBDOC |
---|
[5147e01] | 667 | |
---|
| 668 | // clase que abstrae el adaptador para llenar el ListView de documentos que |
---|
| 669 | // se encuentran dentro del contenedor BDOC |
---|
| 670 | private class SignerFromBDOCAdapter extends BaseAdapter { |
---|
| 671 | |
---|
| 672 | private ArrayList<SignerFromBDOC> mSignerFromBDOCList; |
---|
| 673 | private LayoutInflater lInflater; |
---|
| 674 | |
---|
| 675 | private SignerFromBDOCAdapter(Context context, ArrayList<SignerFromBDOC> signers) { |
---|
| 676 | this.lInflater = LayoutInflater.from(context); |
---|
| 677 | this.mSignerFromBDOCList = signers; |
---|
| 678 | |
---|
| 679 | } |
---|
| 680 | |
---|
| 681 | |
---|
| 682 | @Override |
---|
| 683 | public int getCount() { |
---|
| 684 | return mSignerFromBDOCList.size(); |
---|
| 685 | } |
---|
| 686 | |
---|
| 687 | @Override |
---|
| 688 | public Object getItem(int position) { |
---|
| 689 | return mSignerFromBDOCList.get(position); |
---|
| 690 | } |
---|
| 691 | |
---|
| 692 | @Override |
---|
| 693 | public long getItemId(int position) { |
---|
| 694 | return position; |
---|
| 695 | } |
---|
| 696 | |
---|
| 697 | @Override |
---|
| 698 | public View getView(int position, View convertView, ViewGroup parent) { |
---|
| 699 | ViewHolder container = null; |
---|
| 700 | |
---|
| 701 | if (convertView == null){ |
---|
| 702 | container = new ViewHolder(); |
---|
| 703 | convertView = lInflater.inflate(R.layout.signer_inside_bdoc_item, null); |
---|
| 704 | |
---|
[88eadd6] | 705 | container.signer_text = (TextView) convertView.findViewById(R.id.signer_info); |
---|
| 706 | container.signer_modified = (TextView) convertView.findViewById(R.id.signature_errors); |
---|
[5147e01] | 707 | container.type_image = (ImageView) convertView.findViewById(R.id.type_image); |
---|
| 708 | convertView.setTag(container); |
---|
| 709 | }else{ |
---|
| 710 | container = (ViewHolder) convertView.getTag(); |
---|
| 711 | } |
---|
| 712 | |
---|
| 713 | SignerFromBDOC doc = (SignerFromBDOC) getItem(position); |
---|
| 714 | container.signer_text.setText(doc.getName()); |
---|
| 715 | container.signer_modified.setText(doc.getName()); |
---|
| 716 | container.type_image.setImageResource(R.drawable.ic_pluma); |
---|
| 717 | |
---|
| 718 | return convertView; |
---|
| 719 | } |
---|
| 720 | |
---|
| 721 | |
---|
| 722 | class ViewHolder{ |
---|
| 723 | TextView signer_text; |
---|
| 724 | TextView signer_modified; |
---|
| 725 | ImageView type_image; |
---|
| 726 | } |
---|
| 727 | |
---|
| 728 | } |
---|
| 729 | |
---|
| 730 | |
---|
| 731 | /** |
---|
| 732 | * Prepara directorio |
---|
| 733 | * @return boolean |
---|
| 734 | */ |
---|
[6638d33] | 735 | private boolean prepareDirectory(String dir) |
---|
[5147e01] | 736 | { |
---|
| 737 | try |
---|
| 738 | { |
---|
[6638d33] | 739 | if (makedirs(dir)) |
---|
[5147e01] | 740 | { |
---|
| 741 | return true; |
---|
| 742 | } else { |
---|
| 743 | return false; |
---|
| 744 | } |
---|
| 745 | } catch (Exception e) |
---|
| 746 | { |
---|
| 747 | e.printStackTrace(); |
---|
| 748 | Toast.makeText(this, "Could not initiate File System.. Is Sdcard mounted properly?", Toast.LENGTH_LONG).show(); |
---|
| 749 | return false; |
---|
| 750 | } |
---|
| 751 | } |
---|
| 752 | |
---|
| 753 | /** |
---|
| 754 | * Crea directorio utilizando la variable tmpDir |
---|
| 755 | * @return boolean |
---|
| 756 | */ |
---|
[6638d33] | 757 | private boolean makedirs(String dir) |
---|
[5147e01] | 758 | { |
---|
[6638d33] | 759 | //File tempdir = new File(extractedDirFiles); |
---|
| 760 | File tempdir = new File(dir); |
---|
[5147e01] | 761 | if (!tempdir.exists()) |
---|
| 762 | tempdir.mkdirs(); |
---|
| 763 | |
---|
| 764 | // if (tempdir.isDirectory()) |
---|
| 765 | // { |
---|
| 766 | // File[] files = tempdir.listFiles(); |
---|
| 767 | // for (File file : files) |
---|
| 768 | // { |
---|
| 769 | // if (!file.delete()) |
---|
| 770 | // { |
---|
| 771 | // System.out.println("Failed to delete " + file); |
---|
| 772 | // } |
---|
| 773 | // } |
---|
| 774 | // } |
---|
| 775 | return (tempdir.isDirectory()); |
---|
| 776 | } |
---|
| 777 | |
---|
| 778 | // funcion para lanzar un intent que abra un archivo |
---|
| 779 | private void openIt(String filePath, String mimeType) { |
---|
| 780 | |
---|
| 781 | Intent shareIntent = new Intent(); |
---|
| 782 | shareIntent.setAction(Intent.ACTION_VIEW); |
---|
| 783 | File file = new File(filePath); |
---|
| 784 | Uri uri = Uri.fromFile(file); |
---|
| 785 | Log.i("DEBUG", file.getPath()); |
---|
| 786 | shareIntent.setDataAndType(uri, mimeType); |
---|
| 787 | startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.open_it_using))); |
---|
| 788 | } |
---|
| 789 | |
---|
| 790 | // funcion para obtener el tipo mime de un archivo |
---|
| 791 | public static String getMimeType(String url) |
---|
| 792 | { |
---|
| 793 | String extension = url.substring(url.lastIndexOf(".")); |
---|
| 794 | String mimeTypeMap = MimeTypeMap.getFileExtensionFromUrl(extension); |
---|
| 795 | String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(mimeTypeMap); |
---|
| 796 | return mimeType; |
---|
| 797 | } |
---|
| 798 | |
---|
| 799 | // funcion para compartir el documento |
---|
| 800 | private void shareIt() { |
---|
| 801 | |
---|
| 802 | Intent shareIntent = new Intent(); |
---|
| 803 | shareIntent.setAction(Intent.ACTION_SEND); |
---|
| 804 | File file = new File(fileToVerify); |
---|
| 805 | |
---|
| 806 | |
---|
| 807 | Uri uri = Uri.fromFile(file); |
---|
| 808 | Log.i("DEBUG", file.getPath()); |
---|
| 809 | //Log.d("******", getMimeType(file.getPath())); |
---|
| 810 | //shareIntent.setDataAndType(uri, getMimeType(file.getPath())); |
---|
| 811 | shareIntent.putExtra(Intent.EXTRA_STREAM, uri); |
---|
| 812 | shareIntent.setType("application/*"); |
---|
| 813 | startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.share_it_using))); |
---|
| 814 | } |
---|
| 815 | |
---|
| 816 | @Override |
---|
| 817 | public boolean onCreateOptionsMenu(Menu menu) { |
---|
| 818 | // Inflate the menu; this adds items to the action bar if it is present. |
---|
| 819 | getMenuInflater().inflate(R.menu.bdocverify_result, menu); |
---|
| 820 | return true; |
---|
| 821 | } |
---|
| 822 | |
---|
[064d990] | 823 | private void showSignatureDetails(int position, ArrayList<SignerFromBDOC> signers) { |
---|
| 824 | |
---|
| 825 | Log.d("**showSignatureDetails", Integer.toString(position)); |
---|
| 826 | Log.d("** nombre", signers.get(position).getName()); |
---|
| 827 | Intent intent = new Intent(this, ActivitySignerInfo.class); |
---|
| 828 | |
---|
| 829 | intent.putExtra("name", signers.get(position).getName()); |
---|
| 830 | intent.putExtra("cn", signers.get(position).getCN()); |
---|
| 831 | intent.putExtra("date", signers.get(position).getDate()); |
---|
| 832 | intent.putExtra("certificateSerial", signers.get(position).getCertificateSerial()); |
---|
| 833 | intent.putExtra("validFrom", signers.get(position).getValidFrom()); |
---|
| 834 | intent.putExtra("validUntil", signers.get(position).getValidUntil()); |
---|
| 835 | intent.putExtra("certificateIssuer", signers.get(position).getCertificateIssuer()); |
---|
| 836 | startActivity(intent); |
---|
| 837 | } |
---|
| 838 | |
---|
[88eadd6] | 839 | // funcion para desplegar la interfaz con los detalles de errores de la firma |
---|
| 840 | private void showSignatureDetails() { |
---|
| 841 | Log.d("showSignatureDetails()", "..."); |
---|
| 842 | } |
---|
| 843 | |
---|
[c95e4a6] | 844 | /** |
---|
| 845 | * Showing Dialog for downloading file |
---|
| 846 | * */ |
---|
| 847 | @Override |
---|
| 848 | protected Dialog onCreateDialog(int id) { |
---|
| 849 | switch (id) { |
---|
[6638d33] | 850 | case DIALOG_DOWNLOAD_PROGRESS: // we set this to 0 |
---|
| 851 | mProgressDialog = new ProgressDialog(this); |
---|
| 852 | mProgressDialog.setMessage("Descargando archivo. Por favor espere..."); |
---|
| 853 | mProgressDialog.setIndeterminate(false); |
---|
| 854 | mProgressDialog.setMax(100); |
---|
| 855 | mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); |
---|
| 856 | mProgressDialog.setCancelable(true); |
---|
| 857 | mProgressDialog.show(); |
---|
| 858 | return mProgressDialog; |
---|
[c95e4a6] | 859 | default: |
---|
| 860 | return null; |
---|
| 861 | } |
---|
| 862 | } |
---|
| 863 | |
---|
| 864 | // clase para descargar archivo |
---|
[6638d33] | 865 | private class DownloadFileAsync extends AsyncTask<String, String, ArrayList<String>> { |
---|
[c95e4a6] | 866 | |
---|
[6638d33] | 867 | File rootDir = Environment.getExternalStorageDirectory(); |
---|
[c95e4a6] | 868 | |
---|
| 869 | /** |
---|
| 870 | * Before starting background thread |
---|
| 871 | * Show Progress Bar Dialog |
---|
| 872 | * */ |
---|
| 873 | @Override |
---|
| 874 | protected void onPreExecute() { |
---|
| 875 | super.onPreExecute(); |
---|
[6638d33] | 876 | showDialog(DIALOG_DOWNLOAD_PROGRESS); |
---|
[c95e4a6] | 877 | } |
---|
| 878 | |
---|
| 879 | |
---|
| 880 | /** |
---|
| 881 | * Downloading file in background thread |
---|
| 882 | * */ |
---|
| 883 | @Override |
---|
[6638d33] | 884 | protected ArrayList<String> doInBackground(String... params) { |
---|
[c95e4a6] | 885 | |
---|
| 886 | // para solventar error: |
---|
| 887 | // Can't create handler inside thread that has not called Looper.prepare() |
---|
| 888 | Looper.prepare(); |
---|
| 889 | |
---|
| 890 | int count; |
---|
[6638d33] | 891 | |
---|
| 892 | Log.d("doInBackground", params[0]); |
---|
| 893 | |
---|
| 894 | // resultArray [0] -> 0 para ruta de archivo valida |
---|
| 895 | // resultArray [0] -> 1 ocurrio un error |
---|
| 896 | // resultArray [1] -> ruta del archivo descargado |
---|
| 897 | // resultArray [1] -> mensaje de la excepcion |
---|
| 898 | ArrayList<String> resultArray = new ArrayList<String>(); |
---|
| 899 | |
---|
| 900 | |
---|
[c95e4a6] | 901 | |
---|
| 902 | String outputString = null; |
---|
| 903 | try { |
---|
[6638d33] | 904 | URL url = new URL(params[0]); |
---|
[c95e4a6] | 905 | |
---|
[6638d33] | 906 | int lenghtOfFile = 0; |
---|
| 907 | InputStream input = null; |
---|
[c95e4a6] | 908 | |
---|
[6638d33] | 909 | if (url.getProtocol().equals("http")){ |
---|
| 910 | HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection(); |
---|
| 911 | input = new BufferedInputStream(httpConnection.getInputStream()); |
---|
| 912 | lenghtOfFile = httpConnection.getContentLength(); |
---|
| 913 | |
---|
| 914 | }else{ // https |
---|
| 915 | |
---|
| 916 | // ejecutar la descarga despues de haber aceptado que el certificado |
---|
| 917 | // no es conocido |
---|
| 918 | if (params[1].equals("true")){ |
---|
| 919 | Log.d("***", "params[1].equals(true)"); |
---|
| 920 | |
---|
| 921 | // descarga del certificado del servidor |
---|
| 922 | CertificateFactory cf = CertificateFactory.getInstance("X.509"); |
---|
| 923 | InputStream caInput = new BufferedInputStream(new FileInputStream(rootDir+"/tibisay.cenditel.gob.ve.pem")); |
---|
| 924 | //InputStream caInput = new BufferedInputStream(new FileInputStream(rootDir+"/tibisay.pem")); |
---|
| 925 | Certificate ca; |
---|
| 926 | try { |
---|
| 927 | ca = cf.generateCertificate(caInput); |
---|
| 928 | //Log.d("**ca:", ((X509Certificate) ca).getSubjectDN()); |
---|
| 929 | |
---|
| 930 | //Log.d("*** try: ca.toString()", ca.toString()); |
---|
| 931 | } finally { |
---|
| 932 | caInput.close(); |
---|
| 933 | } |
---|
| 934 | Log.d("***", "despues de Certificate ca"); |
---|
| 935 | // Create a KeyStore containing our trusted CAs |
---|
| 936 | String keyStoreType = KeyStore.getDefaultType(); |
---|
| 937 | KeyStore keyStore = KeyStore.getInstance(keyStoreType); |
---|
| 938 | keyStore.load(null, null); |
---|
| 939 | keyStore.setCertificateEntry("ca", ca); |
---|
| 940 | Log.d("***", "despues de crear KeyStore"); |
---|
| 941 | |
---|
| 942 | // Create a TrustManager that trusts the CAs in our KeyStore |
---|
| 943 | String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm(); |
---|
| 944 | TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm); |
---|
| 945 | tmf.init(keyStore); |
---|
| 946 | Log.d("***", "despues de crear TrustManager"); |
---|
| 947 | |
---|
| 948 | // Create an SSLContext that uses our TrustManager |
---|
| 949 | SSLContext context = SSLContext.getInstance("TLS"); |
---|
| 950 | context.init(null, tmf.getTrustManagers(), null); |
---|
| 951 | Log.d("***", "despues de crear SSLContext"); |
---|
| 952 | |
---|
| 953 | HttpsURLConnection httpsConnection = (HttpsURLConnection) url.openConnection(); |
---|
| 954 | Log.d("***", "despues de crear httpsConnection"); |
---|
| 955 | |
---|
| 956 | httpsConnection.setSSLSocketFactory(context.getSocketFactory()); |
---|
| 957 | Log.d("***", "despues de crear setSSLSocketFactory"); |
---|
| 958 | |
---|
| 959 | //input = new BufferedInputStream(httpsConnection.getInputStream()); |
---|
| 960 | input = httpsConnection.getInputStream(); |
---|
| 961 | |
---|
| 962 | Log.d("***", "despues de crear BufferedInputStream"); |
---|
| 963 | |
---|
| 964 | lenghtOfFile = httpsConnection.getContentLength(); |
---|
| 965 | Log.d("***", "se acepto la excepcion"); |
---|
| 966 | |
---|
| 967 | }else{ |
---|
| 968 | HttpsURLConnection httpsConnection = (HttpsURLConnection) url.openConnection(); |
---|
| 969 | input = new BufferedInputStream(httpsConnection.getInputStream()); |
---|
| 970 | lenghtOfFile = httpsConnection.getContentLength(); |
---|
| 971 | } |
---|
| 972 | } |
---|
[c95e4a6] | 973 | |
---|
| 974 | |
---|
[6638d33] | 975 | // Crear directorio DownloadedFiles donde se almacenan los archivos descargados |
---|
| 976 | // desde un servidor para verificar su firma |
---|
| 977 | downloadedDirFiles = Environment.getExternalStorageDirectory() + "/" + |
---|
| 978 | getResources().getString(R.string.app_name) + "/" + |
---|
| 979 | getResources().getString(R.string.downloaded_dir_files) + "/"; |
---|
| 980 | prepareDirectory(downloadedDirFiles); |
---|
| 981 | |
---|
| 982 | //int lenghtOfFile = conexion.getContentLength(); |
---|
| 983 | Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile); |
---|
| 984 | |
---|
| 985 | //InputStream input = new BufferedInputStream(url.openStream()); |
---|
| 986 | //InputStream input = new BufferedInputStream(conexion.getInputStream()); |
---|
| 987 | |
---|
| 988 | //OutputStream output = new FileOutputStream("/mnt/sdcard/TibisayMovil/ExtractedFiles/photo.jpg"); |
---|
| 989 | |
---|
| 990 | //OutputStream output = new FileOutputStream(new File(rootDir+"/midescarga/", "foto.jpg")); |
---|
| 991 | |
---|
| 992 | String urlString = url.toString(); |
---|
| 993 | String[] values = urlString.split("/"); |
---|
| 994 | |
---|
| 995 | //OutputStream output = new FileOutputStream(new File(downloadedDirFiles, "acta.2.bdoc")); |
---|
| 996 | OutputStream output = new FileOutputStream(new File(downloadedDirFiles, values[values.length-1])); |
---|
| 997 | |
---|
[c95e4a6] | 998 | byte data[] = new byte[1024]; |
---|
[6638d33] | 999 | |
---|
[c95e4a6] | 1000 | long total = 0; |
---|
[6638d33] | 1001 | |
---|
[c95e4a6] | 1002 | while ((count = input.read(data)) != -1) { |
---|
| 1003 | total += count; |
---|
| 1004 | publishProgress(""+(int)((total*100)/lenghtOfFile)); |
---|
[6638d33] | 1005 | output.write(data, 0, count); |
---|
[c95e4a6] | 1006 | } |
---|
[6638d33] | 1007 | |
---|
[c95e4a6] | 1008 | output.flush(); |
---|
[6638d33] | 1009 | |
---|
[c95e4a6] | 1010 | output.close(); |
---|
| 1011 | input.close(); |
---|
| 1012 | |
---|
[6638d33] | 1013 | resultArray.add("0"); |
---|
| 1014 | resultArray.add(output.toString()); |
---|
[c95e4a6] | 1015 | |
---|
| 1016 | } catch (Exception e) { |
---|
| 1017 | |
---|
| 1018 | Log.e("Error: ", e.getMessage()); |
---|
| 1019 | outputString = e.getMessage(); |
---|
| 1020 | |
---|
[6638d33] | 1021 | resultArray.add("1"); |
---|
| 1022 | resultArray.add(e.toString()); |
---|
[c95e4a6] | 1023 | |
---|
| 1024 | } |
---|
[6638d33] | 1025 | |
---|
[c95e4a6] | 1026 | //return null; |
---|
[6638d33] | 1027 | return resultArray; |
---|
[c95e4a6] | 1028 | |
---|
| 1029 | } |
---|
| 1030 | |
---|
| 1031 | protected void onProgressUpdate(String... progress) { |
---|
[6638d33] | 1032 | Log.d("ANDRO_ASYNC",progress[0]); |
---|
| 1033 | mProgressDialog.setProgress(Integer.parseInt(progress[0])); |
---|
| 1034 | } |
---|
| 1035 | |
---|
| 1036 | @Override |
---|
| 1037 | protected void onPostExecute(ArrayList<String> result) { |
---|
| 1038 | dismissDialog(DIALOG_DOWNLOAD_PROGRESS); |
---|
| 1039 | Toast.makeText(getApplicationContext(), "onPostExecute: "+ result.get(0), Toast.LENGTH_LONG).show(); |
---|
| 1040 | |
---|
| 1041 | // ocurrio una excepcion |
---|
| 1042 | if (result.get(0).equals("1")){ |
---|
| 1043 | |
---|
| 1044 | Toast.makeText(getApplicationContext(), "onPostExecute: dentro del if" , Toast.LENGTH_LONG).show(); |
---|
| 1045 | |
---|
| 1046 | // ocurrio un problema |
---|
| 1047 | AlertDialog.Builder builder = new AlertDialog.Builder(BDOCVerifyResultActivity.this); |
---|
| 1048 | |
---|
| 1049 | |
---|
| 1050 | builder.setMessage(result.get(1)).setTitle("Error al descargar el archivo"); |
---|
| 1051 | |
---|
| 1052 | builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { |
---|
| 1053 | public void onClick(DialogInterface dialog, int id) { |
---|
| 1054 | // User clicked OK button |
---|
| 1055 | |
---|
| 1056 | Log.d("***", "DownloadFileAsync().execute(urlhttps, true)"); |
---|
| 1057 | Toast.makeText(getApplicationContext(), "DownloadFileAsync().execute(urlhttps, true)", Toast.LENGTH_LONG).show(); |
---|
| 1058 | // pasar como segundo argmento que se acepta que el sertivor es desconocido |
---|
| 1059 | new DownloadFileAsync().execute(urlhttps, "true"); |
---|
| 1060 | } |
---|
| 1061 | }); |
---|
| 1062 | builder.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() { |
---|
| 1063 | public void onClick(DialogInterface dialog, int id) { |
---|
| 1064 | // User cancelled the dialog |
---|
| 1065 | } |
---|
| 1066 | }); |
---|
| 1067 | AlertDialog dialog = builder.create(); |
---|
| 1068 | dialog.show(); |
---|
| 1069 | |
---|
| 1070 | |
---|
| 1071 | |
---|
| 1072 | }else{ |
---|
| 1073 | // no ocurrió excepcion |
---|
| 1074 | Toast.makeText(getApplicationContext(), "Se descargo correctamente el archivo", Toast.LENGTH_LONG).show(); |
---|
[e68708b] | 1075 | |
---|
[6638d33] | 1076 | } |
---|
[e68708b] | 1077 | |
---|
[6638d33] | 1078 | } |
---|
[c95e4a6] | 1079 | |
---|
[e68708b] | 1080 | |
---|
| 1081 | } // fin de la clase DownloadFileAsync |
---|
[c95e4a6] | 1082 | |
---|
[e68708b] | 1083 | // funcion para verificar un documento pdf |
---|
| 1084 | private void doPdfVerification(String pdfToVerify) { |
---|
[c95e4a6] | 1085 | |
---|
[e68708b] | 1086 | Intent intent = new Intent(this, PDFVerifyResultActivity.class); |
---|
| 1087 | |
---|
| 1088 | intent.putExtra("pdfToVerify", pdfToVerify); |
---|
| 1089 | |
---|
| 1090 | startActivity(intent); |
---|
| 1091 | |
---|
| 1092 | finish(); |
---|
| 1093 | |
---|
| 1094 | } // fin de doPdfVerification(String fileToVerify) |
---|
| 1095 | |
---|
| 1096 | |
---|
[5147e01] | 1097 | } |
---|
| 1098 | |
---|
| 1099 | |
---|
| 1100 | |
---|
[064d990] | 1101 | |
---|