1 | /* |
---|
2 | Tibisay Movil |
---|
3 | |
---|
4 | Copyright (C) 2013 Antonio Araujo (aaraujo@cenditel.gob.ve), Jose Ruiz |
---|
5 | (jruiz@cenditel.gob.ve), Fundacion Centro Nacional de Desarrollo e |
---|
6 | Investigacion en Tecnologias Libres - CENDITEL. |
---|
7 | |
---|
8 | La Fundación CENDITEL concede permiso para usar, copiar, distribuir y/o |
---|
9 | modificar este programa, reconociendo el derecho que la humanidad posee al |
---|
10 | libre acceso al conocimiento, bajo los términos de la licencia de software |
---|
11 | GPL versión 2.0 de la Free Software Foundation. |
---|
12 | |
---|
13 | Este programa se distribuye con la esperanza de que sea util, pero SIN |
---|
14 | NINGUNA GARANTIA; tampoco las implicitas garantias de MERCANTILIDAD o |
---|
15 | ADECUACION A UN PROPOSITO PARTICULAR. |
---|
16 | |
---|
17 | Para mayor información sobre los términos de la licencia ver el archivo |
---|
18 | llamado "gpl-2.0.txt" en ingles. |
---|
19 | */ |
---|
20 | |
---|
21 | package ve.gob.cenditel.tibisaymovil; |
---|
22 | |
---|
23 | import java.io.File; |
---|
24 | import java.io.FileInputStream; |
---|
25 | import java.io.InputStream; |
---|
26 | import java.text.SimpleDateFormat; |
---|
27 | import java.util.ArrayList; |
---|
28 | import java.util.Collections; |
---|
29 | import java.util.Date; |
---|
30 | |
---|
31 | import javax.ws.rs.core.MediaType; |
---|
32 | |
---|
33 | import com.sun.jersey.multipart.file.DefaultMediaTypePredictor; |
---|
34 | |
---|
35 | import ve.gob.cenditel.tibisaymovil.R; |
---|
36 | import android.app.Activity; |
---|
37 | import android.app.AlertDialog; |
---|
38 | import android.content.Context; |
---|
39 | import android.content.ContextWrapper; |
---|
40 | import android.content.DialogInterface; |
---|
41 | import android.content.Intent; |
---|
42 | import android.content.SharedPreferences; |
---|
43 | import android.graphics.drawable.Drawable; |
---|
44 | import android.net.Uri; |
---|
45 | import android.os.Bundle; |
---|
46 | import android.preference.PreferenceManager; |
---|
47 | import android.util.Log; |
---|
48 | import android.view.LayoutInflater; |
---|
49 | import android.view.View; |
---|
50 | import android.view.ViewGroup; |
---|
51 | import android.view.View.OnClickListener; |
---|
52 | import android.view.Window; |
---|
53 | import android.webkit.MimeTypeMap; |
---|
54 | import android.widget.AdapterView; |
---|
55 | import android.widget.ArrayAdapter; |
---|
56 | import android.widget.BaseAdapter; |
---|
57 | import android.widget.ImageView; |
---|
58 | import android.widget.LinearLayout; |
---|
59 | import android.widget.ListView; |
---|
60 | import android.widget.RadioButton; |
---|
61 | import android.widget.TextView; |
---|
62 | import android.widget.AdapterView.OnItemClickListener; |
---|
63 | import android.widget.Toast; |
---|
64 | |
---|
65 | public class FileToSignHandwrittenSignatureActivity extends Activity implements OnItemClickListener, OnClickListener { |
---|
66 | |
---|
67 | private File cwd; |
---|
68 | private File selected; |
---|
69 | private FileBrowserView viewHolder; |
---|
70 | private FileListAdapter listAdapter; |
---|
71 | private String filterMImeType = "application/pdf"; |
---|
72 | |
---|
73 | |
---|
74 | @Override |
---|
75 | protected void onCreate(Bundle savedInstanceState) { |
---|
76 | //Estilando la barra de titulo |
---|
77 | final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); |
---|
78 | |
---|
79 | super.onCreate(savedInstanceState); |
---|
80 | |
---|
81 | this.viewHolder = new FileBrowserView(); |
---|
82 | |
---|
83 | if (savedInstanceState != null) { |
---|
84 | if(savedInstanceState.getString("selected") != null) |
---|
85 | this.selected = new File(savedInstanceState.getString("selected")); |
---|
86 | |
---|
87 | if (this.selected != null) { |
---|
88 | |
---|
89 | FileToSignHandwrittenSignatureActivity.this.updateButton |
---|
90 | (FileToSignHandwrittenSignatureActivity.this.viewHolder.accept,true); |
---|
91 | |
---|
92 | } |
---|
93 | |
---|
94 | this.cwd = new File(savedInstanceState.getString("cwd")); |
---|
95 | this.viewHolder.fileList.setAdapter(this.listAdapter = new FileListAdapter(this.cwd.getAbsolutePath(), filterMImeType)); |
---|
96 | |
---|
97 | } else { |
---|
98 | this.selected = null; |
---|
99 | this.viewHolder.fileList.setAdapter(this.listAdapter = new FileListAdapter()); |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | boolean enabled = false; |
---|
104 | if (this.selected != null) |
---|
105 | enabled = this.viewHolder.accept.isEnabled(); |
---|
106 | |
---|
107 | FileToSignHandwrittenSignatureActivity.this.updateButton |
---|
108 | (FileToSignHandwrittenSignatureActivity.this.viewHolder.accept,enabled); |
---|
109 | |
---|
110 | //Estilando Barra de titulo |
---|
111 | if(customTitleSupported) |
---|
112 | getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar); |
---|
113 | |
---|
114 | |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | /** |
---|
119 | * Provides the data to be shown in the file browser ListView. |
---|
120 | * |
---|
121 | * @author José M. Prieto (jmprieto@emergya.com) |
---|
122 | */ |
---|
123 | private class FileListAdapter extends BaseAdapter { |
---|
124 | |
---|
125 | private final ArrayList<File> directories; |
---|
126 | private final ArrayList<File> files; |
---|
127 | |
---|
128 | private FileListAdapter() { |
---|
129 | this("/",filterMImeType); |
---|
130 | } |
---|
131 | |
---|
132 | private FileListAdapter(String location) { |
---|
133 | this(location, ""); |
---|
134 | } |
---|
135 | |
---|
136 | private FileListAdapter(String location, String filterMimeType) { |
---|
137 | |
---|
138 | directories = new ArrayList<File>(); |
---|
139 | files = new ArrayList<File>(); |
---|
140 | |
---|
141 | //Obtiene etiqueta que se colocará antes del path que visualizará el usuario |
---|
142 | String toPathText = FileToSignHandwrittenSignatureActivity.this.getString(R.string.pathstring)+": "; |
---|
143 | //Coloca el texto de la etiqueta en la vista |
---|
144 | FileToSignHandwrittenSignatureActivity.this.viewHolder.pathString.setText(toPathText); |
---|
145 | //Coloca el texto con el path actual |
---|
146 | FileToSignHandwrittenSignatureActivity.this.viewHolder.path.setText(location); |
---|
147 | //Crea un objeto file cwd con la ubicacion dada en location |
---|
148 | FileToSignHandwrittenSignatureActivity.this.cwd = new File(location); |
---|
149 | //Obtiene el directorio padre del objeto file cwd |
---|
150 | File parent = FileToSignHandwrittenSignatureActivity.this.cwd.getParentFile(); |
---|
151 | //Si tiene un padre, lo agrega en la posición cero de la lista de directorios |
---|
152 | |
---|
153 | if (parent != null) { |
---|
154 | directories.add(0, parent); |
---|
155 | } |
---|
156 | |
---|
157 | //Crea un arreglo con las lista de archivos contenidos en el directorio cwd |
---|
158 | File[] ls = FileToSignHandwrittenSignatureActivity.this.cwd.listFiles(); |
---|
159 | if (ls != null) { |
---|
160 | for (File f : ls) { //recorre todos los archivos contenidos en el directorio |
---|
161 | |
---|
162 | if (FsUtils.isHiddenOrNotReadable(f)) { // Si son ocultos no hace nada |
---|
163 | continue; |
---|
164 | } |
---|
165 | // Si son directorios los agrega a la lista de directorios a partir de la posición 1 |
---|
166 | // En la posición 0 se encuentra el directorio padre |
---|
167 | if (f.isDirectory()) { |
---|
168 | directories.add(f); |
---|
169 | } else // De lo contrario lo agrega a la lista de archivos |
---|
170 | { |
---|
171 | //Valida tipo de archivo a mostrar |
---|
172 | Uri selectedUri = Uri.fromFile(f); |
---|
173 | String fileExtension = MimeTypeMap.getFileExtensionFromUrl(selectedUri.toString()); |
---|
174 | String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension); |
---|
175 | |
---|
176 | // Toast.makeText(FileToSignHandwrittenSignatureActivity.this, |
---|
177 | // "FileExtension: " + fileExtension + "\n" + |
---|
178 | // "MimeType: " + mimeType, |
---|
179 | // Toast.LENGTH_LONG).show(); |
---|
180 | |
---|
181 | //Filtra por mimeType |
---|
182 | if(filterMimeType.isEmpty() || filterMimeType == mimeType) |
---|
183 | files.add(f); |
---|
184 | |
---|
185 | } |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | Collections.sort(directories); // Ordena los directorios alfabeticamente |
---|
190 | Collections.sort(files); // Ordena los archivos alfabeticamente |
---|
191 | } |
---|
192 | |
---|
193 | /** |
---|
194 | * Retorna cantidad total de elementos que se listarán en el directorio. |
---|
195 | */ |
---|
196 | @Override |
---|
197 | public int getCount() { |
---|
198 | |
---|
199 | return directories.size() + files.size(); |
---|
200 | |
---|
201 | } |
---|
202 | |
---|
203 | /** |
---|
204 | * Dada una posición en el listado del directorio, retorna un archivo o directorio |
---|
205 | * según corresponda. |
---|
206 | */ |
---|
207 | @Override |
---|
208 | public File getItem(int position) { |
---|
209 | |
---|
210 | if (position < directories.size()) { |
---|
211 | return directories.get(position); |
---|
212 | } else { |
---|
213 | return files.get(position - directories.size()); |
---|
214 | } |
---|
215 | } |
---|
216 | |
---|
217 | /** |
---|
218 | * Retorna un código hash para el archivo, permite comparar si dos archivos son los mismos |
---|
219 | */ |
---|
220 | @Override |
---|
221 | public long getItemId(int position) { |
---|
222 | |
---|
223 | return getItem(position).hashCode(); |
---|
224 | } |
---|
225 | |
---|
226 | /** |
---|
227 | * Crea la visualización de cada item del fileBrowser |
---|
228 | */ |
---|
229 | @Override |
---|
230 | public View getView(int position, View convertView, ViewGroup parent) { |
---|
231 | |
---|
232 | //Crea la vista de cada fila del filebrowser a partir del layout |
---|
233 | if (convertView == null) { |
---|
234 | LayoutInflater inflater = LayoutInflater.from(FileToSignHandwrittenSignatureActivity.this); |
---|
235 | convertView = inflater.inflate(R.layout.file_to_sign_handwritten_signature_item, parent, false); |
---|
236 | } |
---|
237 | |
---|
238 | // Se enlaza a cada componente del layout |
---|
239 | ImageView image = (ImageView) convertView.findViewById(R.id.type_image); |
---|
240 | TextView fileName = (TextView) convertView.findViewById(R.id.filename_text); |
---|
241 | TextView modified = (TextView) convertView.findViewById(R.id.filename_modified); |
---|
242 | |
---|
243 | // Se obtiene el archivo ubicado en position |
---|
244 | File file = getItem(position); |
---|
245 | |
---|
246 | //RadioButton |
---|
247 | RadioButton radio = (RadioButton) convertView.findViewById(R.id.file_radio); |
---|
248 | radio.setFocusable(false); |
---|
249 | |
---|
250 | // Se asignan los iconos según el tipo de archivo y se oculta el radio en los directorios |
---|
251 | if (file.isDirectory()) { |
---|
252 | image.setImageResource(R.drawable.ic_carpeta_verde); |
---|
253 | radio.setVisibility(View.INVISIBLE); |
---|
254 | radio.setChecked(false); |
---|
255 | } else { |
---|
256 | image.setImageResource(R.drawable.ic_archivo_verde); |
---|
257 | radio.setVisibility(View.VISIBLE); |
---|
258 | |
---|
259 | if (FileToSignHandwrittenSignatureActivity.this.selected == null || |
---|
260 | FileToSignHandwrittenSignatureActivity.this.selected.hashCode() != file.hashCode()){ |
---|
261 | radio.setChecked(false); |
---|
262 | } else{ |
---|
263 | radio.setChecked(true); |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | // Si es el directorio que hace referencia al padre le coloca como nombre ".." |
---|
268 | if (file.isDirectory() && position == 0 && ! "/".equals(FileToSignHandwrittenSignatureActivity.this.cwd.getAbsolutePath())) { |
---|
269 | fileName.setText(".."); |
---|
270 | } else { |
---|
271 | fileName.setText(file.getName()); |
---|
272 | } |
---|
273 | |
---|
274 | |
---|
275 | //Datos de modificación del archivo |
---|
276 | SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); |
---|
277 | Date date = new Date(file.lastModified()); |
---|
278 | |
---|
279 | String dateString = FileToSignHandwrittenSignatureActivity.this.getString(R.string.modified)+": "; |
---|
280 | if (file.lastModified()>0) |
---|
281 | modified.setText(dateString+sdf.format(date)); |
---|
282 | else |
---|
283 | modified.setText(dateString+"-"); |
---|
284 | |
---|
285 | return convertView; |
---|
286 | } |
---|
287 | |
---|
288 | /** |
---|
289 | * Controla la selección de cada item del fileBrowser |
---|
290 | */ |
---|
291 | public void select(ListView parent, int position) { |
---|
292 | |
---|
293 | File item = getItem(position); |
---|
294 | //Si es un directorio el seleccionado se hace un llamado del fileBrowser del directorio |
---|
295 | if (item.isDirectory()) { |
---|
296 | parent.setAdapter(FileToSignHandwrittenSignatureActivity.this.listAdapter = new FileListAdapter(item.getAbsolutePath(), filterMImeType)); |
---|
297 | } else { // Si es un archivo |
---|
298 | |
---|
299 | //Se agrega el archivo a la lista de seleccionados si no se encuentra en la misma |
---|
300 | if (FileToSignHandwrittenSignatureActivity.this.selected == null || |
---|
301 | FileToSignHandwrittenSignatureActivity.this.selected.hashCode() != item.hashCode()){ |
---|
302 | |
---|
303 | FileToSignHandwrittenSignatureActivity.this.selected = item; |
---|
304 | |
---|
305 | FileToSignHandwrittenSignatureActivity.this.updateButton(FileToSignHandwrittenSignatureActivity.this.viewHolder.accept,true); |
---|
306 | |
---|
307 | } |
---|
308 | else{ // De lo contrario se elimina de la lista de seleccionados |
---|
309 | |
---|
310 | FileToSignHandwrittenSignatureActivity.this.selected = null; |
---|
311 | FileToSignHandwrittenSignatureActivity.this.updateButton(FileToSignHandwrittenSignatureActivity.this.viewHolder.accept,false); |
---|
312 | |
---|
313 | } |
---|
314 | notifyDataSetChanged(); |
---|
315 | } |
---|
316 | } |
---|
317 | } |
---|
318 | |
---|
319 | |
---|
320 | |
---|
321 | |
---|
322 | @Override |
---|
323 | protected void onSaveInstanceState(Bundle outState) { |
---|
324 | super.onSaveInstanceState(outState); |
---|
325 | |
---|
326 | outState.putParcelable("intent", this.getIntent()); |
---|
327 | outState.putString("cwd", this.cwd.getAbsolutePath()); |
---|
328 | if(this.selected != null) |
---|
329 | outState.putString("selected", this.selected.getAbsolutePath()); |
---|
330 | |
---|
331 | } |
---|
332 | |
---|
333 | private void updateButton(View v, boolean bool) { |
---|
334 | try{ |
---|
335 | |
---|
336 | v.setEnabled(bool); |
---|
337 | if (v instanceof TextView){ |
---|
338 | Drawable icon = ((TextView)v).getCompoundDrawables()[1]; |
---|
339 | if (icon!=null) |
---|
340 | if (bool) |
---|
341 | icon.setAlpha(255); |
---|
342 | else |
---|
343 | icon.setAlpha(127); |
---|
344 | } |
---|
345 | if (v instanceof ImageView){ |
---|
346 | ImageView result = (ImageView) v; |
---|
347 | if (bool) |
---|
348 | result.setAlpha(255); |
---|
349 | else |
---|
350 | result.setAlpha(127); |
---|
351 | } |
---|
352 | |
---|
353 | }catch(NullPointerException e){} |
---|
354 | |
---|
355 | } |
---|
356 | |
---|
357 | private void updateButton(LinearLayout layout, boolean bool) { |
---|
358 | try{ |
---|
359 | layout.setEnabled(bool); |
---|
360 | for (int i=0; i<layout.getChildCount();i++){ |
---|
361 | this.updateButton(layout.getChildAt(i), bool); |
---|
362 | } |
---|
363 | }catch(NullPointerException e){} |
---|
364 | |
---|
365 | } |
---|
366 | |
---|
367 | |
---|
368 | |
---|
369 | /** |
---|
370 | * Holds references to view objects. |
---|
371 | * |
---|
372 | * @author José M. Prieto (jmprieto@emergya.com) |
---|
373 | */ |
---|
374 | private class FileBrowserView { |
---|
375 | |
---|
376 | public ListView fileList; |
---|
377 | public TextView path; |
---|
378 | public LinearLayout accept; |
---|
379 | public LinearLayout clear; |
---|
380 | public TextView pathString; |
---|
381 | |
---|
382 | public FileBrowserView() { |
---|
383 | |
---|
384 | setContentView(R.layout.activity_file_to_sign_handwritten_signature); |
---|
385 | this.path = (TextView) findViewById(R.id.path); |
---|
386 | this.pathString = (TextView) findViewById(R.id.pathstring); |
---|
387 | |
---|
388 | this.fileList = (ListView) findViewById(R.id.file_list); |
---|
389 | this.fileList.setOnItemClickListener(FileToSignHandwrittenSignatureActivity.this); |
---|
390 | this.fileList.setItemsCanFocus(true); |
---|
391 | |
---|
392 | |
---|
393 | this.clear = (LinearLayout) findViewById(R.id.button_clear_zone); |
---|
394 | this.clear.setOnClickListener(FileToSignHandwrittenSignatureActivity.this); |
---|
395 | |
---|
396 | this.accept = (LinearLayout) findViewById(R.id.button_accept_zone); |
---|
397 | this.accept.setOnClickListener(FileToSignHandwrittenSignatureActivity.this); |
---|
398 | |
---|
399 | } |
---|
400 | } |
---|
401 | |
---|
402 | @Override |
---|
403 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
---|
404 | |
---|
405 | |
---|
406 | if (parent.getId() == R.id.file_list) { // user selects a file |
---|
407 | this.listAdapter.select((ListView) parent, position); |
---|
408 | |
---|
409 | } else { // user de-selects a file |
---|
410 | //this.listAdapter.addIfSameDirectory(selectedAdapter.doUnselect((ListView) parent, position)); |
---|
411 | |
---|
412 | } |
---|
413 | //this.viewHolder.numSelected.setText(Integer.toString(this.selected.size())); |
---|
414 | } |
---|
415 | |
---|
416 | @Override |
---|
417 | public void onClick(View v) { |
---|
418 | |
---|
419 | switch (v.getId()) { |
---|
420 | |
---|
421 | |
---|
422 | case R.id.button_clear_zone: |
---|
423 | |
---|
424 | this.selected = null; |
---|
425 | this.listAdapter.notifyDataSetChanged(); |
---|
426 | |
---|
427 | //this.viewHolder.numSelected.setText(""+this.selected.size()); |
---|
428 | this.updateButton(this.viewHolder.accept, false); |
---|
429 | break; |
---|
430 | |
---|
431 | |
---|
432 | |
---|
433 | case R.id.button_accept_zone: |
---|
434 | |
---|
435 | //Crear dialogo de opciones para firmar |
---|
436 | final AlertDialog image_dialog = selectImageOptionsDialog(); |
---|
437 | image_dialog.show(); |
---|
438 | |
---|
439 | break; |
---|
440 | |
---|
441 | } |
---|
442 | } |
---|
443 | |
---|
444 | |
---|
445 | //Crea dialogo de opciones para seleccion de imagen de firma |
---|
446 | public AlertDialog selectImageOptionsDialog() { |
---|
447 | |
---|
448 | SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); |
---|
449 | |
---|
450 | final String imagePath = prefs.getString(PreferencesActivity.imageKey, ""); |
---|
451 | final String[] option; |
---|
452 | //---- |
---|
453 | if (imagePath.equals("")) { |
---|
454 | option = new String[] {getString(R.string.firmar_man_capt_img), getString(R.string.firmar_man_exist_img)}; |
---|
455 | } |
---|
456 | else { |
---|
457 | option = new String[] {getString(R.string.firmar_man_capt_img), getString(R.string.firmar_man_exist_img), getString(R.string.firmar_man_prefer_img) }; |
---|
458 | } |
---|
459 | |
---|
460 | ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, |
---|
461 | android.R.layout.select_dialog_item, option); |
---|
462 | AlertDialog.Builder builder = new AlertDialog.Builder(this); |
---|
463 | |
---|
464 | builder.setTitle(getString(R.string.seleccione_opcion_firmar_man_img)); |
---|
465 | builder.setAdapter(adapter, new DialogInterface.OnClickListener() { |
---|
466 | |
---|
467 | public void onClick(DialogInterface dialog, int which) { |
---|
468 | |
---|
469 | switch (which) { |
---|
470 | case 0: // Capturar imágen |
---|
471 | Intent intent = new Intent(FileToSignHandwrittenSignatureActivity.this, CaptureImgHandwrittenSignatureActivity.class); |
---|
472 | intent.putExtra("FILE_TO_SIGN", FileToSignHandwrittenSignatureActivity.this.selected.getAbsolutePath()); |
---|
473 | Log.i("DEBUG", FileToSignHandwrittenSignatureActivity.this.selected.getAbsolutePath()); |
---|
474 | startActivity(intent); |
---|
475 | |
---|
476 | break; |
---|
477 | case 1: // Buscar en archivos |
---|
478 | Intent intent1 = new Intent(FileToSignHandwrittenSignatureActivity.this, SelectImgHandwrittenSignatureActivity.class); |
---|
479 | intent1.putExtra("FILE_TO_SIGN", FileToSignHandwrittenSignatureActivity.this.selected.getAbsolutePath()); |
---|
480 | startActivity(intent1); |
---|
481 | |
---|
482 | break; |
---|
483 | case 2: // Usar predeterminada |
---|
484 | Intent intent2 = new Intent(FileToSignHandwrittenSignatureActivity.this, SelectSignUbicationHandwrittenSignatureActivity.class); |
---|
485 | intent2.putExtra("FILE_TO_SIGN", FileToSignHandwrittenSignatureActivity.this.selected.getAbsolutePath()); |
---|
486 | intent2.putExtra("IMAGE_CAPTURED", imagePath); |
---|
487 | startActivity(intent2); |
---|
488 | |
---|
489 | break; |
---|
490 | |
---|
491 | default: |
---|
492 | break; |
---|
493 | } |
---|
494 | } |
---|
495 | |
---|
496 | }); |
---|
497 | |
---|
498 | return builder.create(); |
---|
499 | } |
---|
500 | |
---|
501 | } |
---|