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