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.text.SimpleDateFormat; |
---|
25 | import java.util.ArrayList; |
---|
26 | import java.util.Collections; |
---|
27 | import java.util.Date; |
---|
28 | import ve.gob.cenditel.tibisaymovil.R; |
---|
29 | import android.app.Activity; |
---|
30 | import android.app.AlertDialog; |
---|
31 | import android.content.DialogInterface; |
---|
32 | import android.content.Intent; |
---|
33 | import android.graphics.drawable.Drawable; |
---|
34 | import android.net.Uri; |
---|
35 | import android.os.Bundle; |
---|
36 | import android.util.Log; |
---|
37 | import android.view.LayoutInflater; |
---|
38 | import android.view.View; |
---|
39 | import android.view.ViewGroup; |
---|
40 | import android.view.View.OnClickListener; |
---|
41 | import android.view.Window; |
---|
42 | import android.webkit.MimeTypeMap; |
---|
43 | import android.widget.AdapterView; |
---|
44 | import android.widget.ArrayAdapter; |
---|
45 | import android.widget.BaseAdapter; |
---|
46 | import android.widget.ImageView; |
---|
47 | import android.widget.LinearLayout; |
---|
48 | import android.widget.ListView; |
---|
49 | import android.widget.RadioButton; |
---|
50 | import android.widget.TextView; |
---|
51 | import android.widget.AdapterView.OnItemClickListener; |
---|
52 | import android.widget.Toast; |
---|
53 | |
---|
54 | public class SelectImgHandwrittenSignatureActivity extends Activity implements OnItemClickListener, OnClickListener { |
---|
55 | |
---|
56 | private File cwd; |
---|
57 | private File selected; |
---|
58 | private FileBrowserView viewHolder; |
---|
59 | private FileListAdapter listAdapter; |
---|
60 | private String filterMImeType = "image/png"; |
---|
61 | private String file_to_sign; |
---|
62 | |
---|
63 | @Override |
---|
64 | protected void onCreate(Bundle savedInstanceState) { |
---|
65 | //Estilando la barra de titulo |
---|
66 | final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); |
---|
67 | |
---|
68 | super.onCreate(savedInstanceState); |
---|
69 | |
---|
70 | this.viewHolder = new FileBrowserView(); |
---|
71 | |
---|
72 | if (savedInstanceState != null) { |
---|
73 | if(savedInstanceState.getString("selected") != null) |
---|
74 | this.selected = new File(savedInstanceState.getString("selected")); |
---|
75 | |
---|
76 | if (this.selected != null) { |
---|
77 | |
---|
78 | SelectImgHandwrittenSignatureActivity.this.updateButton |
---|
79 | (SelectImgHandwrittenSignatureActivity.this.viewHolder.accept,true); |
---|
80 | |
---|
81 | } |
---|
82 | |
---|
83 | this.cwd = new File(savedInstanceState.getString("cwd")); |
---|
84 | this.viewHolder.fileList.setAdapter(this.listAdapter = new FileListAdapter(this.cwd.getAbsolutePath(), filterMImeType)); |
---|
85 | |
---|
86 | } else { |
---|
87 | this.selected = null; |
---|
88 | this.viewHolder.fileList.setAdapter(this.listAdapter = new FileListAdapter()); |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | boolean enabled = false; |
---|
93 | if (this.selected != null) |
---|
94 | enabled = this.viewHolder.accept.isEnabled(); |
---|
95 | |
---|
96 | SelectImgHandwrittenSignatureActivity.this.updateButton |
---|
97 | (SelectImgHandwrittenSignatureActivity.this.viewHolder.accept,enabled); |
---|
98 | |
---|
99 | //Estilando Barra de titulo |
---|
100 | if(customTitleSupported) |
---|
101 | getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar); |
---|
102 | |
---|
103 | file_to_sign = getIntent().getExtras().getString("FILE_TO_SIGN"); |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | /** |
---|
108 | * Provides the data to be shown in the file browser ListView. |
---|
109 | * |
---|
110 | * @author José M. Prieto (jmprieto@emergya.com) |
---|
111 | */ |
---|
112 | private class FileListAdapter extends BaseAdapter { |
---|
113 | |
---|
114 | private final ArrayList<File> directories; |
---|
115 | private final ArrayList<File> files; |
---|
116 | |
---|
117 | private FileListAdapter() { |
---|
118 | this("/",filterMImeType); |
---|
119 | } |
---|
120 | |
---|
121 | private FileListAdapter(String location) { |
---|
122 | this(location, ""); |
---|
123 | } |
---|
124 | |
---|
125 | private FileListAdapter(String location, String filterMimeType) { |
---|
126 | |
---|
127 | directories = new ArrayList<File>(); |
---|
128 | files = new ArrayList<File>(); |
---|
129 | |
---|
130 | //Obtiene etiqueta que se colocará antes del path que visualizará el usuario |
---|
131 | String toPathText = SelectImgHandwrittenSignatureActivity.this.getString(R.string.pathstring)+": "; |
---|
132 | //Coloca el texto de la etiqueta en la vista |
---|
133 | SelectImgHandwrittenSignatureActivity.this.viewHolder.pathString.setText(toPathText); |
---|
134 | //Coloca el texto con el path actual |
---|
135 | SelectImgHandwrittenSignatureActivity.this.viewHolder.path.setText(location); |
---|
136 | //Crea un objeto file cwd con la ubicacion dada en location |
---|
137 | SelectImgHandwrittenSignatureActivity.this.cwd = new File(location); |
---|
138 | //Obtiene el directorio padre del objeto file cwd |
---|
139 | File parent = SelectImgHandwrittenSignatureActivity.this.cwd.getParentFile(); |
---|
140 | //Si tiene un padre, lo agrega en la posición cero de la lista de directorios |
---|
141 | |
---|
142 | if (parent != null) { |
---|
143 | directories.add(0, parent); |
---|
144 | } |
---|
145 | |
---|
146 | //Crea un arreglo con las lista de archivos contenidos en el directorio cwd |
---|
147 | File[] ls = SelectImgHandwrittenSignatureActivity.this.cwd.listFiles(); |
---|
148 | if (ls != null) { |
---|
149 | for (File f : ls) { //recorre todos los archivos contenidos en el directorio |
---|
150 | |
---|
151 | if (FsUtils.isHiddenOrNotReadable(f)) { // Si son ocultos no hace nada |
---|
152 | continue; |
---|
153 | } |
---|
154 | // Si son directorios los agrega a la lista de directorios a partir de la posición 1 |
---|
155 | // En la posición 0 se encuentra el directorio padre |
---|
156 | if (f.isDirectory()) { |
---|
157 | directories.add(f); |
---|
158 | } else // De lo contrario lo agrega a la lista de archivos |
---|
159 | { |
---|
160 | //Valida tipo de archivo a mostrar |
---|
161 | Uri selectedUri = Uri.fromFile(f); |
---|
162 | String fileExtension = MimeTypeMap.getFileExtensionFromUrl(selectedUri.toString()); |
---|
163 | String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension); |
---|
164 | |
---|
165 | // Toast.makeText(FileToSignHandwrittenSignatureActivity.this, |
---|
166 | // "FileExtension: " + fileExtension + "\n" + |
---|
167 | // "MimeType: " + mimeType, |
---|
168 | // Toast.LENGTH_LONG).show(); |
---|
169 | |
---|
170 | //Filtra por mimeType |
---|
171 | if(filterMimeType.isEmpty() || filterMimeType == mimeType) |
---|
172 | files.add(f); |
---|
173 | |
---|
174 | } |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|
178 | Collections.sort(directories); // Ordena los directorios alfabeticamente |
---|
179 | Collections.sort(files); // Ordena los archivos alfabeticamente |
---|
180 | } |
---|
181 | |
---|
182 | /** |
---|
183 | * Retorna cantidad total de elementos que se listarán en el directorio. |
---|
184 | */ |
---|
185 | @Override |
---|
186 | public int getCount() { |
---|
187 | |
---|
188 | return directories.size() + files.size(); |
---|
189 | |
---|
190 | } |
---|
191 | |
---|
192 | /** |
---|
193 | * Dada una posición en el listado del directorio, retorna un archivo o directorio |
---|
194 | * según corresponda. |
---|
195 | */ |
---|
196 | @Override |
---|
197 | public File getItem(int position) { |
---|
198 | |
---|
199 | if (position < directories.size()) { |
---|
200 | return directories.get(position); |
---|
201 | } else { |
---|
202 | return files.get(position - directories.size()); |
---|
203 | } |
---|
204 | } |
---|
205 | |
---|
206 | /** |
---|
207 | * Retorna un código hash para el archivo, permite comparar si dos archivos son los mismos |
---|
208 | */ |
---|
209 | @Override |
---|
210 | public long getItemId(int position) { |
---|
211 | |
---|
212 | return getItem(position).hashCode(); |
---|
213 | } |
---|
214 | |
---|
215 | /** |
---|
216 | * Crea la visualización de cada item del fileBrowser |
---|
217 | */ |
---|
218 | @Override |
---|
219 | public View getView(int position, View convertView, ViewGroup parent) { |
---|
220 | |
---|
221 | //Crea la vista de cada fila del filebrowser a partir del layout |
---|
222 | if (convertView == null) { |
---|
223 | LayoutInflater inflater = LayoutInflater.from(SelectImgHandwrittenSignatureActivity.this); |
---|
224 | convertView = inflater.inflate(R.layout.file_to_sign_handwritten_signature_item, parent, false); |
---|
225 | } |
---|
226 | |
---|
227 | // Se enlaza a cada componente del layout |
---|
228 | ImageView image = (ImageView) convertView.findViewById(R.id.type_image); |
---|
229 | TextView fileName = (TextView) convertView.findViewById(R.id.filename_text); |
---|
230 | TextView modified = (TextView) convertView.findViewById(R.id.filename_modified); |
---|
231 | |
---|
232 | // Se obtiene el archivo ubicado en position |
---|
233 | File file = getItem(position); |
---|
234 | |
---|
235 | //RadioButton |
---|
236 | RadioButton radio = (RadioButton) convertView.findViewById(R.id.file_radio); |
---|
237 | radio.setFocusable(false); |
---|
238 | |
---|
239 | // Se asignan los iconos según el tipo de archivo y se oculta el radio en los directorios |
---|
240 | if (file.isDirectory()) { |
---|
241 | image.setImageResource(R.drawable.ic_carpeta); |
---|
242 | radio.setVisibility(View.INVISIBLE); |
---|
243 | radio.setChecked(false); |
---|
244 | } else { |
---|
245 | image.setImageResource(R.drawable.ic_archivo); |
---|
246 | radio.setVisibility(View.VISIBLE); |
---|
247 | |
---|
248 | if (SelectImgHandwrittenSignatureActivity.this.selected == null || |
---|
249 | SelectImgHandwrittenSignatureActivity.this.selected.hashCode() != file.hashCode()){ |
---|
250 | radio.setChecked(false); |
---|
251 | } else{ |
---|
252 | radio.setChecked(true); |
---|
253 | } |
---|
254 | } |
---|
255 | |
---|
256 | // Si es el directorio que hace referencia al padre le coloca como nombre ".." |
---|
257 | if (file.isDirectory() && position == 0 && ! "/".equals(SelectImgHandwrittenSignatureActivity.this.cwd.getAbsolutePath())) { |
---|
258 | fileName.setText(".."); |
---|
259 | } else { |
---|
260 | fileName.setText(file.getName()); |
---|
261 | } |
---|
262 | |
---|
263 | |
---|
264 | //Datos de modificación del archivo |
---|
265 | SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); |
---|
266 | Date date = new Date(file.lastModified()); |
---|
267 | |
---|
268 | String dateString = SelectImgHandwrittenSignatureActivity.this.getString(R.string.modified)+": "; |
---|
269 | if (file.lastModified()>0) |
---|
270 | modified.setText(dateString+sdf.format(date)); |
---|
271 | else |
---|
272 | modified.setText(dateString+"-"); |
---|
273 | |
---|
274 | return convertView; |
---|
275 | } |
---|
276 | |
---|
277 | /** |
---|
278 | * Controla la selección de cada item del fileBrowser |
---|
279 | */ |
---|
280 | public void select(ListView parent, int position) { |
---|
281 | |
---|
282 | File item = getItem(position); |
---|
283 | //Si es un directorio el seleccionado se hace un llamado del fileBrowser del directorio |
---|
284 | if (item.isDirectory()) { |
---|
285 | parent.setAdapter(SelectImgHandwrittenSignatureActivity.this.listAdapter = new FileListAdapter(item.getAbsolutePath(), filterMImeType)); |
---|
286 | } else { // Si es un archivo |
---|
287 | |
---|
288 | //Se agrega el archivo a la lista de seleccionados si no se encuentra en la misma |
---|
289 | if (SelectImgHandwrittenSignatureActivity.this.selected == null || |
---|
290 | SelectImgHandwrittenSignatureActivity.this.selected.hashCode() != item.hashCode()){ |
---|
291 | |
---|
292 | SelectImgHandwrittenSignatureActivity.this.selected = item; |
---|
293 | |
---|
294 | SelectImgHandwrittenSignatureActivity.this.updateButton(SelectImgHandwrittenSignatureActivity.this.viewHolder.accept,true); |
---|
295 | |
---|
296 | } |
---|
297 | else{ // De lo contrario se elimina de la lista de seleccionados |
---|
298 | |
---|
299 | SelectImgHandwrittenSignatureActivity.this.selected = null; |
---|
300 | SelectImgHandwrittenSignatureActivity.this.updateButton(SelectImgHandwrittenSignatureActivity.this.viewHolder.accept,false); |
---|
301 | |
---|
302 | } |
---|
303 | notifyDataSetChanged(); |
---|
304 | } |
---|
305 | } |
---|
306 | } |
---|
307 | |
---|
308 | |
---|
309 | |
---|
310 | |
---|
311 | @Override |
---|
312 | protected void onSaveInstanceState(Bundle outState) { |
---|
313 | super.onSaveInstanceState(outState); |
---|
314 | |
---|
315 | outState.putParcelable("intent", this.getIntent()); |
---|
316 | outState.putString("cwd", this.cwd.getAbsolutePath()); |
---|
317 | if(this.selected != null) |
---|
318 | outState.putString("selected", this.selected.getAbsolutePath()); |
---|
319 | |
---|
320 | } |
---|
321 | |
---|
322 | private void updateButton(View v, boolean bool) { |
---|
323 | try{ |
---|
324 | |
---|
325 | v.setEnabled(bool); |
---|
326 | if (v instanceof TextView){ |
---|
327 | Drawable icon = ((TextView)v).getCompoundDrawables()[1]; |
---|
328 | if (icon!=null) |
---|
329 | if (bool) |
---|
330 | icon.setAlpha(255); |
---|
331 | else |
---|
332 | icon.setAlpha(127); |
---|
333 | } |
---|
334 | if (v instanceof ImageView){ |
---|
335 | ImageView result = (ImageView) v; |
---|
336 | if (bool) |
---|
337 | result.setAlpha(255); |
---|
338 | else |
---|
339 | result.setAlpha(127); |
---|
340 | } |
---|
341 | |
---|
342 | }catch(NullPointerException e){} |
---|
343 | |
---|
344 | } |
---|
345 | |
---|
346 | private void updateButton(LinearLayout layout, boolean bool) { |
---|
347 | try{ |
---|
348 | layout.setEnabled(bool); |
---|
349 | for (int i=0; i<layout.getChildCount();i++){ |
---|
350 | this.updateButton(layout.getChildAt(i), bool); |
---|
351 | } |
---|
352 | }catch(NullPointerException e){} |
---|
353 | |
---|
354 | } |
---|
355 | |
---|
356 | |
---|
357 | |
---|
358 | /** |
---|
359 | * Holds references to view objects. |
---|
360 | * |
---|
361 | * @author José M. Prieto (jmprieto@emergya.com) |
---|
362 | */ |
---|
363 | private class FileBrowserView { |
---|
364 | |
---|
365 | public ListView fileList; |
---|
366 | public TextView path; |
---|
367 | public LinearLayout accept; |
---|
368 | public LinearLayout clear; |
---|
369 | public TextView pathString; |
---|
370 | |
---|
371 | public FileBrowserView() { |
---|
372 | |
---|
373 | setContentView(R.layout.activity_img_to_sign_handwritten_signature); |
---|
374 | this.path = (TextView) findViewById(R.id.path); |
---|
375 | this.pathString = (TextView) findViewById(R.id.pathstring); |
---|
376 | |
---|
377 | this.fileList = (ListView) findViewById(R.id.file_list); |
---|
378 | this.fileList.setOnItemClickListener(SelectImgHandwrittenSignatureActivity.this); |
---|
379 | this.fileList.setItemsCanFocus(true); |
---|
380 | |
---|
381 | |
---|
382 | this.clear = (LinearLayout) findViewById(R.id.button_clear_zone); |
---|
383 | this.clear.setOnClickListener(SelectImgHandwrittenSignatureActivity.this); |
---|
384 | |
---|
385 | this.accept = (LinearLayout) findViewById(R.id.button_accept_zone); |
---|
386 | this.accept.setOnClickListener(SelectImgHandwrittenSignatureActivity.this); |
---|
387 | |
---|
388 | } |
---|
389 | } |
---|
390 | |
---|
391 | @Override |
---|
392 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
---|
393 | |
---|
394 | |
---|
395 | if (parent.getId() == R.id.file_list) { // user selects a file |
---|
396 | this.listAdapter.select((ListView) parent, position); |
---|
397 | |
---|
398 | } else { // user de-selects a file |
---|
399 | //this.listAdapter.addIfSameDirectory(selectedAdapter.doUnselect((ListView) parent, position)); |
---|
400 | |
---|
401 | } |
---|
402 | //this.viewHolder.numSelected.setText(Integer.toString(this.selected.size())); |
---|
403 | } |
---|
404 | |
---|
405 | @Override |
---|
406 | public void onClick(View v) { |
---|
407 | |
---|
408 | switch (v.getId()) { |
---|
409 | |
---|
410 | |
---|
411 | case R.id.button_clear_zone: |
---|
412 | |
---|
413 | this.selected = null; |
---|
414 | this.listAdapter.notifyDataSetChanged(); |
---|
415 | |
---|
416 | //this.viewHolder.numSelected.setText(""+this.selected.size()); |
---|
417 | this.updateButton(this.viewHolder.accept, false); |
---|
418 | break; |
---|
419 | |
---|
420 | |
---|
421 | |
---|
422 | case R.id.button_accept_zone: |
---|
423 | |
---|
424 | Intent intent1 = new Intent(SelectImgHandwrittenSignatureActivity.this, SelectSignUbicationHandwrittenSignatureActivity.class); |
---|
425 | intent1.putExtra("FILE_TO_SIGN", file_to_sign); |
---|
426 | intent1.putExtra("IMAGE_CAPTURED", SelectImgHandwrittenSignatureActivity.this.selected.getAbsolutePath()); |
---|
427 | startActivity(intent1); |
---|
428 | |
---|
429 | break; |
---|
430 | |
---|
431 | } |
---|
432 | } |
---|
433 | |
---|
434 | |
---|
435 | |
---|
436 | |
---|
437 | } |
---|