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