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