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