[4528953] | 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.FileNotFoundException; |
---|
| 7 | import java.io.IOException; |
---|
| 8 | import java.io.InputStream; |
---|
| 9 | import java.security.cert.Certificate; |
---|
| 10 | import java.security.cert.CertificateException; |
---|
| 11 | import java.security.cert.CertificateFactory; |
---|
| 12 | import java.security.cert.X509Certificate; |
---|
| 13 | import java.util.ArrayList; |
---|
| 14 | import java.util.HashMap; |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | |
---|
| 18 | import ee.sk.digidoc.DigiDocException; |
---|
| 19 | import ee.sk.digidoc.SignedDoc; |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | import android.os.Bundle; |
---|
| 24 | import android.os.Environment; |
---|
| 25 | import android.app.Activity; |
---|
| 26 | import android.app.AlertDialog; |
---|
| 27 | import android.content.Context; |
---|
| 28 | import android.content.DialogInterface; |
---|
| 29 | import android.content.Intent; |
---|
| 30 | import android.util.Log; |
---|
| 31 | import android.view.LayoutInflater; |
---|
| 32 | import android.view.Menu; |
---|
| 33 | import android.view.View; |
---|
| 34 | import android.view.ViewGroup; |
---|
| 35 | import android.view.Window; |
---|
| 36 | import android.view.View.OnClickListener; |
---|
| 37 | import android.widget.AdapterView; |
---|
| 38 | import android.widget.BaseAdapter; |
---|
| 39 | import android.widget.ImageView; |
---|
| 40 | import android.widget.LinearLayout; |
---|
| 41 | import android.widget.ListView; |
---|
| 42 | import android.widget.RadioButton; |
---|
| 43 | import android.widget.TextView; |
---|
| 44 | import android.widget.Toast; |
---|
| 45 | import android.widget.AdapterView.OnItemClickListener; |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | |
---|
| 50 | // activity para gestionar los certificados de destinatarios disponibles para cifrar |
---|
| 51 | // un archivo |
---|
| 52 | public class EncryptionCertificatesActivity extends Activity { |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | // layout del boton agregar certificado |
---|
| 56 | private LinearLayout button_add; |
---|
| 57 | |
---|
| 58 | // layout del boton eliminar certificado |
---|
| 59 | private LinearLayout button_remove; |
---|
| 60 | |
---|
| 61 | // ListView que mantiene la lista de certificados |
---|
| 62 | private ListView certificate_list; |
---|
| 63 | |
---|
| 64 | // ruta del directorio que almancena los certificados de destinatarios |
---|
| 65 | String certificatesDir; |
---|
| 66 | |
---|
| 67 | // id de certificado seleccionado |
---|
| 68 | int certificateSelectedIndex = -1; |
---|
| 69 | |
---|
| 70 | // lista de rutas absolutas de archivos de certificados |
---|
| 71 | HashMap<String, String> certificatesPathHashMap = null; |
---|
| 72 | |
---|
| 73 | @Override |
---|
| 74 | protected void onCreate(Bundle savedInstanceState) { |
---|
| 75 | |
---|
| 76 | //Estilando la barra de titulo |
---|
| 77 | final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); |
---|
| 78 | |
---|
| 79 | super.onCreate(savedInstanceState); |
---|
| 80 | setContentView(R.layout.activity_encryption_certificates); |
---|
| 81 | |
---|
| 82 | //Estilando Barra de titulo |
---|
| 83 | if(customTitleSupported) |
---|
| 84 | getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar); |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | |
---|
| 88 | |
---|
| 89 | button_remove = (LinearLayout) this.findViewById(R.id.button_remove_certificate_zone); |
---|
| 90 | button_add = (LinearLayout) this.findViewById(R.id.button_add_certificate_zone); |
---|
| 91 | |
---|
| 92 | final TextView labelRemoveCertificate = (TextView) this.findViewById(R.id.label_certificate_to_remove); |
---|
| 93 | labelRemoveCertificate.setVisibility(View.GONE); |
---|
| 94 | |
---|
| 95 | final LinearLayout layoutCertificateToRemove = (LinearLayout) this.findViewById(R.id.layout_certificate_to_remove); |
---|
| 96 | layoutCertificateToRemove.setVisibility(View.GONE); |
---|
| 97 | |
---|
| 98 | final TextView certificateToRemove = (TextView) this.findViewById(R.id.filename_text); |
---|
| 99 | |
---|
| 100 | |
---|
| 101 | // lista de certificados |
---|
| 102 | final ArrayList<RecipientCertificate> certificates = new ArrayList<RecipientCertificate>(); |
---|
| 103 | |
---|
| 104 | // chequear disponibilidad de directorio de certificados |
---|
| 105 | if (!checkCertificatesDirectoryAvailability()){ |
---|
| 106 | return; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | // Leer el contenido del directorio de certificados |
---|
| 110 | // Crea un arreglo con las lista de archivos contenidos en el directorio cwd |
---|
| 111 | File f = new File("/mnt/sdcard/TibisayMovil/CertificatesToEncrypt"); |
---|
| 112 | File[] ls = f.listFiles(); |
---|
| 113 | |
---|
| 114 | if (ls != null) { |
---|
| 115 | //Toast.makeText(getApplicationContext(), "numero de archivos: " +Integer.toString(ls.length), Toast.LENGTH_SHORT).show(); |
---|
| 116 | |
---|
| 117 | certificatesPathHashMap = new HashMap<String, String>(); |
---|
| 118 | |
---|
| 119 | String recipient = null; |
---|
| 120 | // iterar sobre todos los archivos de certificados |
---|
| 121 | // en el KeyStore |
---|
| 122 | for (File iFile : ls) { |
---|
| 123 | |
---|
| 124 | try { |
---|
| 125 | X509Certificate recvCert = SignedDoc.readCertificate(new File(iFile.getAbsolutePath())); |
---|
| 126 | |
---|
| 127 | recipient = SignedDoc.getCommonName(recvCert.getSubjectDN().getName()); |
---|
| 128 | //Toast.makeText(getApplicationContext(), "Destinatario: "+ recipient,Toast.LENGTH_SHORT).show(); |
---|
| 129 | |
---|
| 130 | certificates.add(new RecipientCertificate(recipient)); |
---|
| 131 | |
---|
| 132 | certificatesPathHashMap.put(recipient, iFile.getAbsolutePath()); |
---|
| 133 | |
---|
| 134 | } catch (DigiDocException e) { |
---|
| 135 | Toast.makeText(getApplicationContext(), "DigiDocException: "+ e.getMessage(), Toast.LENGTH_SHORT).show(); |
---|
| 136 | } |
---|
| 137 | }// fin de la iteracion sobre iFile |
---|
| 138 | |
---|
| 139 | |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | |
---|
| 143 | // se crea el adaptador para mostrar los certificados de destinatarios |
---|
| 144 | final RecipientCertificateAdapter certificateAdapter = new RecipientCertificateAdapter(EncryptionCertificatesActivity.this, certificates); |
---|
| 145 | final ListView certificateListView = (ListView) findViewById(R.id.certificate_list); |
---|
| 146 | certificateListView.setAdapter(certificateAdapter); |
---|
| 147 | certificateListView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() { |
---|
| 148 | @Override |
---|
| 149 | public void onItemClick(AdapterView<?> parent, View view, |
---|
| 150 | int position, long id) { |
---|
| 151 | |
---|
| 152 | Object o = certificateListView.getItemAtPosition(position); |
---|
| 153 | RecipientCertificate c = (RecipientCertificate) o; |
---|
| 154 | |
---|
| 155 | certificateSelectedIndex = position; |
---|
| 156 | |
---|
| 157 | |
---|
| 158 | labelRemoveCertificate.setVisibility(View.VISIBLE); |
---|
| 159 | layoutCertificateToRemove.setVisibility(View.VISIBLE); |
---|
| 160 | |
---|
| 161 | certificateToRemove.setText(c.getName()); |
---|
| 162 | |
---|
| 163 | |
---|
| 164 | Toast.makeText(getApplicationContext(), "click sobre certificado"+ c.getName(), Toast.LENGTH_SHORT).show(); |
---|
| 165 | |
---|
| 166 | } |
---|
| 167 | }); |
---|
| 168 | |
---|
| 169 | |
---|
| 170 | |
---|
| 171 | |
---|
| 172 | |
---|
| 173 | |
---|
| 174 | |
---|
| 175 | |
---|
| 176 | |
---|
| 177 | |
---|
| 178 | |
---|
| 179 | |
---|
| 180 | |
---|
| 181 | |
---|
| 182 | // habilita el click sobre la acción para agregar un certificado nuevo |
---|
| 183 | button_add.setOnClickListener(new OnClickListener() |
---|
| 184 | { |
---|
| 185 | public void onClick(View v) |
---|
| 186 | { |
---|
| 187 | |
---|
| 188 | //Intent oIntent = new Intent(BDOCVerifyResultActivity.this, TibisayMovilActivity.class); |
---|
| 189 | //oIntent.setAction(Intent.ACTION_VIEW); |
---|
| 190 | //oIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); |
---|
| 191 | //startActivity(oIntent); |
---|
| 192 | |
---|
| 193 | //finish(); |
---|
| 194 | Toast.makeText(getApplicationContext(), "agregar un certificado", Toast.LENGTH_SHORT).show(); |
---|
| 195 | } |
---|
| 196 | }); |
---|
| 197 | |
---|
| 198 | // habilita el click sobre la acción eliminar el certificado seleccionado |
---|
| 199 | button_remove.setOnClickListener(new OnClickListener() |
---|
| 200 | { |
---|
| 201 | public void onClick(View v) |
---|
| 202 | { |
---|
| 203 | //shareIt(); |
---|
| 204 | |
---|
| 205 | Toast.makeText(getApplicationContext(), "eliminar un certificado", Toast.LENGTH_SHORT).show(); |
---|
| 206 | |
---|
| 207 | if (certificateSelectedIndex == -1){ |
---|
| 208 | // 1. Instantiate an AlertDialog.Builder with its constructor |
---|
| 209 | AlertDialog.Builder builder = new AlertDialog.Builder(EncryptionCertificatesActivity.this); |
---|
| 210 | |
---|
| 211 | // 2. Chain together various setter methods to set the dialog characteristics |
---|
| 212 | builder.setMessage("Debe seleccionar un certificado para eliminarlo.") |
---|
| 213 | .setTitle("Información:"); |
---|
| 214 | |
---|
| 215 | builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { |
---|
| 216 | public void onClick(DialogInterface dialog, int id) { |
---|
| 217 | // User clicked OK button |
---|
| 218 | Toast.makeText(getApplicationContext(), "User clicked OK button", Toast.LENGTH_LONG).show(); |
---|
| 219 | } |
---|
| 220 | }); |
---|
| 221 | |
---|
| 222 | // 3. Get the AlertDialog from create() |
---|
| 223 | AlertDialog dialog = builder.create(); |
---|
| 224 | dialog.show(); |
---|
| 225 | }else{ |
---|
| 226 | // 1. Instantiate an AlertDialog.Builder with its constructor |
---|
| 227 | AlertDialog.Builder builder = new AlertDialog.Builder(EncryptionCertificatesActivity.this); |
---|
| 228 | |
---|
| 229 | // 2. Chain together various setter methods to set the dialog characteristics |
---|
| 230 | builder.setMessage("¿Está seguro de eliminar el certificado?.") |
---|
| 231 | .setTitle("Información:"); |
---|
| 232 | |
---|
| 233 | builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { |
---|
| 234 | public void onClick(DialogInterface dialog, int id) { |
---|
| 235 | // User clicked OK button |
---|
| 236 | Toast.makeText(getApplicationContext(), "User clicked OK button", Toast.LENGTH_LONG).show(); |
---|
| 237 | |
---|
| 238 | // funcion para eliminar el certificado |
---|
| 239 | // ... |
---|
| 240 | String pathToErase = certificatesPathHashMap.get(certificateToRemove.getText()); |
---|
| 241 | |
---|
| 242 | File file = new File(pathToErase); |
---|
| 243 | boolean deleted = file.delete(); |
---|
| 244 | |
---|
| 245 | //actualizar la lista de certificados en ListView |
---|
| 246 | certificateAdapter.notifyDataSetChanged(); |
---|
| 247 | |
---|
| 248 | EncryptionCertificatesActivity.this.recreate(); |
---|
| 249 | |
---|
| 250 | Toast.makeText(getApplicationContext(), "**archivo a borrar: "+pathToErase, Toast.LENGTH_LONG).show(); |
---|
| 251 | |
---|
| 252 | |
---|
| 253 | labelRemoveCertificate.setVisibility(View.GONE); |
---|
| 254 | layoutCertificateToRemove.setVisibility(View.GONE); |
---|
| 255 | } |
---|
| 256 | }); |
---|
| 257 | |
---|
| 258 | builder.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() { |
---|
| 259 | public void onClick(DialogInterface dialog, int id) { |
---|
| 260 | // User cancelled the dialog |
---|
| 261 | Toast.makeText(getApplicationContext(), "User clicked Cancel button", Toast.LENGTH_LONG).show(); |
---|
| 262 | |
---|
| 263 | labelRemoveCertificate.setVisibility(View.GONE); |
---|
| 264 | layoutCertificateToRemove.setVisibility(View.GONE); |
---|
| 265 | } |
---|
| 266 | }); |
---|
| 267 | // 3. Get the AlertDialog from create() |
---|
| 268 | AlertDialog dialog = builder.create(); |
---|
| 269 | dialog.show(); |
---|
| 270 | } |
---|
| 271 | } |
---|
| 272 | }); |
---|
| 273 | |
---|
| 274 | |
---|
| 275 | |
---|
| 276 | } // fin de funcion onCreate() |
---|
| 277 | |
---|
| 278 | @Override |
---|
| 279 | public boolean onCreateOptionsMenu(Menu menu) { |
---|
| 280 | // Inflate the menu; this adds items to the action bar if it is present. |
---|
| 281 | getMenuInflater().inflate(R.menu.encryption_certificates, menu); |
---|
| 282 | return true; |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | |
---|
| 286 | |
---|
| 287 | // clase que abstrae un certificado de destinatario |
---|
| 288 | // se utiliza para mostrar en la interfaz grafica cada certificado |
---|
| 289 | // de destinatario almancenado por la aplicacion |
---|
| 290 | public class RecipientCertificate { |
---|
| 291 | private String mName; |
---|
| 292 | |
---|
| 293 | public RecipientCertificate(String name){ |
---|
| 294 | mName = name; |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | public void setName(String name) { |
---|
| 298 | this.mName = name; |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | public String getName() { |
---|
| 302 | return mName; |
---|
| 303 | } |
---|
| 304 | } // fin de clase RecipientCertificate |
---|
| 305 | |
---|
| 306 | // clase que abstrae el adaptador para llenar el ListView de documentos que |
---|
| 307 | // se encuentran dentro del contenedor BDOC |
---|
| 308 | private class RecipientCertificateAdapter extends BaseAdapter { |
---|
| 309 | |
---|
| 310 | private ArrayList<RecipientCertificate> mRecipientCertificateList; |
---|
| 311 | private LayoutInflater lInflater; |
---|
| 312 | |
---|
| 313 | private RecipientCertificateAdapter(Context context, ArrayList<RecipientCertificate> certificates) { |
---|
| 314 | this.lInflater = LayoutInflater.from(context); |
---|
| 315 | this.mRecipientCertificateList = certificates; |
---|
| 316 | |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | |
---|
| 320 | @Override |
---|
| 321 | public int getCount() { |
---|
| 322 | return mRecipientCertificateList.size(); |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | @Override |
---|
| 326 | public Object getItem(int position) { |
---|
| 327 | return mRecipientCertificateList.get(position); |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | @Override |
---|
| 331 | public long getItemId(int position) { |
---|
| 332 | return position; |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | @Override |
---|
| 336 | public View getView(int position, View convertView, ViewGroup parent) { |
---|
| 337 | ViewHolder container = null; |
---|
| 338 | |
---|
| 339 | if (convertView == null){ |
---|
| 340 | container = new ViewHolder(); |
---|
| 341 | convertView = lInflater.inflate(R.layout.recipient_certificate_info, null); |
---|
| 342 | |
---|
| 343 | container.filename_text = (TextView) convertView.findViewById(R.id.filename_text); |
---|
| 344 | //container.filename_modified = (TextView) convertView.findViewById(R.id.filename_modified); |
---|
| 345 | container.type_image = (ImageView) convertView.findViewById(R.id.type_image); |
---|
| 346 | convertView.setTag(container); |
---|
| 347 | }else{ |
---|
| 348 | container = (ViewHolder) convertView.getTag(); |
---|
| 349 | } |
---|
| 350 | |
---|
| 351 | RecipientCertificate certificate = (RecipientCertificate) getItem(position); |
---|
| 352 | container.filename_text.setText(certificate.getName()); |
---|
| 353 | //container.filename_modified.setText(certificate.getName()); |
---|
| 354 | container.type_image.setImageResource(R.drawable.ic_archivo); |
---|
| 355 | |
---|
| 356 | return convertView; |
---|
| 357 | } |
---|
| 358 | |
---|
| 359 | |
---|
| 360 | class ViewHolder{ |
---|
| 361 | TextView filename_text; |
---|
| 362 | //TextView filename_modified; |
---|
| 363 | ImageView type_image; |
---|
| 364 | } |
---|
| 365 | |
---|
| 366 | } // fin de la clase RecipientCertificateAdapter |
---|
| 367 | |
---|
| 368 | |
---|
| 369 | |
---|
| 370 | |
---|
| 371 | /** |
---|
| 372 | * Prepara directorio |
---|
| 373 | * @return boolean |
---|
| 374 | */ |
---|
| 375 | boolean prepareDirectory(String dir) |
---|
| 376 | { |
---|
| 377 | try |
---|
| 378 | { |
---|
| 379 | if (makedirs(dir)) |
---|
| 380 | { |
---|
| 381 | return true; |
---|
| 382 | } else { |
---|
| 383 | return false; |
---|
| 384 | } |
---|
| 385 | } catch (Exception e) |
---|
| 386 | { |
---|
| 387 | e.printStackTrace(); |
---|
| 388 | Toast.makeText(this, "Could not initiate File System.. Is Sdcard mounted properly?", Toast.LENGTH_LONG).show(); |
---|
| 389 | return false; |
---|
| 390 | } |
---|
| 391 | } |
---|
| 392 | |
---|
| 393 | /** |
---|
| 394 | * Crea directorio utilizando la variable tmpDir |
---|
| 395 | * @return boolean |
---|
| 396 | */ |
---|
| 397 | private boolean makedirs(String dir) |
---|
| 398 | { |
---|
| 399 | //File tempdir = new File(extractedDirFiles); |
---|
| 400 | File tempdir = new File(dir); |
---|
| 401 | if (!tempdir.exists()) |
---|
| 402 | tempdir.mkdirs(); |
---|
| 403 | return (tempdir.isDirectory()); |
---|
| 404 | } |
---|
| 405 | |
---|
| 406 | /** |
---|
| 407 | * Chequea la disponibilidad del directorio de /TibisayMovil/CertificatesToEncrypt |
---|
| 408 | * @return boolean |
---|
| 409 | */ |
---|
| 410 | private boolean checkCertificatesDirectoryAvailability() { |
---|
| 411 | // verificar acceso al directorio /mnt/sdcard/TibisayMovil/CertificatesToEncrypt |
---|
| 412 | boolean mExternalStorageAvailable = false; |
---|
| 413 | boolean mExternalStorageWriteable = false; |
---|
| 414 | String state = Environment.getExternalStorageState(); |
---|
| 415 | |
---|
| 416 | AlertDialog.Builder builder = new AlertDialog.Builder(EncryptionCertificatesActivity.this); |
---|
| 417 | |
---|
| 418 | |
---|
| 419 | if (Environment.MEDIA_MOUNTED.equals(state)) { |
---|
| 420 | // We can read and write the media |
---|
| 421 | mExternalStorageAvailable = mExternalStorageWriteable = true; |
---|
| 422 | Toast.makeText(getApplicationContext(), "We can read and write the media", Toast.LENGTH_SHORT).show(); |
---|
| 423 | |
---|
| 424 | // Crear directorio CertificatesToEncrypt donde se almacenan los certificados de |
---|
| 425 | // destinatarios para cifrado |
---|
| 426 | String certificatesDir = Environment.getExternalStorageDirectory() + "/" + |
---|
| 427 | getResources().getString(R.string.app_name) + "/" + |
---|
| 428 | getResources().getString(R.string.certificates_dir) + "/"; |
---|
| 429 | if (prepareDirectory(certificatesDir)){ |
---|
| 430 | return true; |
---|
| 431 | }else{ |
---|
| 432 | builder.setMessage("No existe el directorio "+certificatesDir+" para almacenar certificados.").setTitle("Error:"); |
---|
| 433 | |
---|
| 434 | } |
---|
| 435 | |
---|
| 436 | } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { |
---|
| 437 | // We can only read the media |
---|
| 438 | mExternalStorageAvailable = true; |
---|
| 439 | mExternalStorageWriteable = false; |
---|
| 440 | Toast.makeText(getApplicationContext(), "We can only read the media", Toast.LENGTH_SHORT).show(); |
---|
| 441 | |
---|
| 442 | builder.setMessage("Directorio "+certificatesDir+ " montado de solo lectura. No se pueden almancenar certificados.").setTitle("Error:"); |
---|
| 443 | |
---|
| 444 | |
---|
| 445 | } else { |
---|
| 446 | // Something else is wrong. It may be one of many other states, but all we need |
---|
| 447 | // to know is we can neither read nor write |
---|
| 448 | mExternalStorageAvailable = mExternalStorageWriteable = false; |
---|
| 449 | Toast.makeText(getApplicationContext(), "we can neither read nor write", Toast.LENGTH_SHORT).show(); |
---|
| 450 | |
---|
| 451 | builder.setMessage("Directorio "+certificatesDir+ " no está disponible. No se pueden almancenar certificados.").setTitle("Error:"); |
---|
| 452 | |
---|
| 453 | } |
---|
| 454 | |
---|
| 455 | |
---|
| 456 | builder.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() { |
---|
| 457 | public void onClick(DialogInterface dialog, int id) { |
---|
| 458 | // User cancelled the dialog |
---|
| 459 | EncryptionCertificatesActivity.this.finish(); |
---|
| 460 | } |
---|
| 461 | }); |
---|
| 462 | AlertDialog dialog = builder.create(); |
---|
| 463 | dialog.show(); |
---|
| 464 | return false; |
---|
| 465 | } |
---|
| 466 | |
---|
| 467 | |
---|
| 468 | |
---|
| 469 | |
---|
| 470 | } // fin de la activity |
---|