[8379cd8] | 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 | |
---|
[ae1bcd1] | 22 | package ve.gob.cenditel.tibisaymovil; |
---|
| 23 | |
---|
| 24 | import java.io.File; |
---|
| 25 | import java.text.SimpleDateFormat; |
---|
| 26 | import java.util.ArrayList; |
---|
| 27 | import java.util.Collections; |
---|
| 28 | import java.util.Date; |
---|
| 29 | |
---|
| 30 | import ve.gob.cenditel.tibisaymovil.R; |
---|
| 31 | import android.app.Activity; |
---|
| 32 | import android.app.AlertDialog; |
---|
| 33 | import android.content.DialogInterface; |
---|
| 34 | import android.content.Intent; |
---|
| 35 | import android.graphics.drawable.Drawable; |
---|
| 36 | import android.net.Uri; |
---|
| 37 | import android.os.Bundle; |
---|
| 38 | import android.os.Environment; |
---|
| 39 | import android.util.Log; |
---|
| 40 | import android.view.LayoutInflater; |
---|
| 41 | import android.view.View; |
---|
| 42 | import android.view.ViewGroup; |
---|
| 43 | import android.view.View.OnClickListener; |
---|
| 44 | import android.view.Window; |
---|
| 45 | import android.webkit.MimeTypeMap; |
---|
| 46 | import android.widget.AdapterView; |
---|
| 47 | import android.widget.ArrayAdapter; |
---|
| 48 | import android.widget.BaseAdapter; |
---|
| 49 | import android.widget.ImageView; |
---|
| 50 | import android.widget.LinearLayout; |
---|
| 51 | import android.widget.ListView; |
---|
| 52 | import android.widget.RadioButton; |
---|
| 53 | import android.widget.TextView; |
---|
| 54 | import android.widget.AdapterView.OnItemClickListener; |
---|
| 55 | import android.widget.Toast; |
---|
| 56 | |
---|
| 57 | public class FileToEncryptActivity extends Activity implements OnItemClickListener, OnClickListener { |
---|
| 58 | |
---|
| 59 | private File cwd; |
---|
| 60 | private File selected; |
---|
| 61 | private FileBrowserView viewHolder; |
---|
| 62 | private FileListAdapter listAdapter; |
---|
| 63 | private String filterMImeType = "application/*"; |
---|
| 64 | |
---|
| 65 | // cadena que mantiene la ruta del archivo a compartir |
---|
| 66 | private String fileToEncrypt = null; |
---|
| 67 | |
---|
| 68 | @Override |
---|
| 69 | protected void onCreate(Bundle savedInstanceState) { |
---|
| 70 | //Estilando la barra de titulo |
---|
| 71 | final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); |
---|
| 72 | |
---|
| 73 | super.onCreate(savedInstanceState); |
---|
| 74 | |
---|
| 75 | this.viewHolder = new FileBrowserView(); |
---|
| 76 | |
---|
| 77 | if (savedInstanceState != null) { |
---|
| 78 | if(savedInstanceState.getString("selected") != null) |
---|
| 79 | this.selected = new File(savedInstanceState.getString("selected")); |
---|
| 80 | |
---|
| 81 | if (this.selected != null) { |
---|
| 82 | |
---|
| 83 | FileToEncryptActivity.this.updateButton |
---|
| 84 | (FileToEncryptActivity.this.viewHolder.accept,true); |
---|
| 85 | |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | this.cwd = new File(savedInstanceState.getString("cwd")); |
---|
| 89 | this.viewHolder.fileList.setAdapter(this.listAdapter = new FileListAdapter(this.cwd.getAbsolutePath(), filterMImeType)); |
---|
| 90 | |
---|
| 91 | } else { |
---|
| 92 | this.selected = null; |
---|
| 93 | this.viewHolder.fileList.setAdapter(this.listAdapter = new FileListAdapter()); |
---|
| 94 | /* |
---|
| 95 | // obtener la ruta donde se debe parar el listAdapter |
---|
| 96 | |
---|
| 97 | Bundle bundle = getIntent().getExtras(); |
---|
| 98 | String path = bundle.getString("path"); |
---|
| 99 | |
---|
| 100 | if (path.equals("archivoFirmado")){ |
---|
| 101 | |
---|
| 102 | String signedDirFiles = Environment.getExternalStorageDirectory() + "/" + |
---|
| 103 | getResources().getString(R.string.app_name) + "/" + |
---|
| 104 | getResources().getString(R.string.external_dir_files) + "/"; |
---|
| 105 | |
---|
| 106 | this.viewHolder.fileList.setAdapter(this.listAdapter = new FileListAdapter(signedDirFiles, filterMImeType)); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | if (path.equals("bdocDescargado")){ |
---|
| 110 | String downloadedDirFiles = Environment.getExternalStorageDirectory() + "/" + |
---|
| 111 | getResources().getString(R.string.app_name) + "/" + |
---|
| 112 | getResources().getString(R.string.downloaded_dir_files) + "/"; |
---|
| 113 | this.viewHolder.fileList.setAdapter(this.listAdapter = new FileListAdapter(downloadedDirFiles, filterMImeType)); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | if (path.equals("extraidoDeBdoc")){ |
---|
| 117 | String extractedDirFiles = Environment.getExternalStorageDirectory() + "/" + |
---|
| 118 | getResources().getString(R.string.app_name) + "/" + |
---|
| 119 | getResources().getString(R.string.extracted_dir_files) + "/"; |
---|
| 120 | this.viewHolder.fileList.setAdapter(this.listAdapter = new FileListAdapter(extractedDirFiles, filterMImeType)); |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | */ |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | |
---|
| 127 | boolean enabled = false; |
---|
| 128 | if (this.selected != null) |
---|
| 129 | enabled = this.viewHolder.accept.isEnabled(); |
---|
| 130 | |
---|
| 131 | FileToEncryptActivity.this.updateButton |
---|
| 132 | (FileToEncryptActivity.this.viewHolder.accept,enabled); |
---|
| 133 | |
---|
| 134 | //Estilando Barra de titulo |
---|
| 135 | if(customTitleSupported) |
---|
| 136 | getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | |
---|
| 140 | /** |
---|
| 141 | * Provides the data to be shown in the file browser ListView. |
---|
| 142 | * |
---|
| 143 | * @author José M. Prieto (jmprieto@emergya.com) |
---|
| 144 | */ |
---|
| 145 | private class FileListAdapter extends BaseAdapter { |
---|
| 146 | |
---|
| 147 | private final ArrayList<File> directories; |
---|
| 148 | private final ArrayList<File> files; |
---|
| 149 | |
---|
| 150 | private FileListAdapter() { |
---|
| 151 | this("/",filterMImeType); |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | private FileListAdapter(String location) { |
---|
| 155 | this(location, ""); |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | private FileListAdapter(String location, String filterMimeType) { |
---|
| 159 | |
---|
| 160 | directories = new ArrayList<File>(); |
---|
| 161 | files = new ArrayList<File>(); |
---|
| 162 | |
---|
| 163 | //Obtiene etiqueta que se colocará antes del path que visualizará el usuario |
---|
| 164 | String toPathText = FileToEncryptActivity.this.getString(R.string.pathstring)+": "; |
---|
| 165 | //Coloca el texto de la etiqueta en la vista |
---|
| 166 | FileToEncryptActivity.this.viewHolder.pathString.setText(toPathText); |
---|
| 167 | //Coloca el texto con el path actual |
---|
| 168 | FileToEncryptActivity.this.viewHolder.path.setText(location); |
---|
| 169 | //Crea un objeto file cwd con la ubicacion dada en location |
---|
| 170 | FileToEncryptActivity.this.cwd = new File(location); |
---|
| 171 | //Obtiene el directorio padre del objeto file cwd |
---|
| 172 | File parent = FileToEncryptActivity.this.cwd.getParentFile(); |
---|
| 173 | //Si tiene un padre, lo agrega en la posición cero de la lista de directorios |
---|
| 174 | |
---|
| 175 | if (parent != null) { |
---|
| 176 | directories.add(0, parent); |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | //Crea un arreglo con las lista de archivos contenidos en el directorio cwd |
---|
| 180 | File[] ls = FileToEncryptActivity.this.cwd.listFiles(); |
---|
| 181 | if (ls != null) { |
---|
| 182 | for (File f : ls) { //recorre todos los archivos contenidos en el directorio |
---|
| 183 | |
---|
| 184 | if (FsUtils.isHiddenOrNotReadable(f)) { // Si son ocultos no hace nada |
---|
| 185 | continue; |
---|
| 186 | } |
---|
| 187 | // Si son directorios los agrega a la lista de directorios a partir de la posición 1 |
---|
| 188 | // En la posición 0 se encuentra el directorio padre |
---|
| 189 | if (f.isDirectory()) { |
---|
| 190 | directories.add(f); |
---|
| 191 | } else // De lo contrario lo agrega a la lista de archivos |
---|
| 192 | { |
---|
| 193 | //Valida tipo de archivo a mostrar |
---|
| 194 | Uri selectedUri = Uri.fromFile(f); |
---|
| 195 | String fileExtension = MimeTypeMap.getFileExtensionFromUrl(selectedUri.toString()); |
---|
| 196 | String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension); |
---|
| 197 | |
---|
| 198 | // Toast.makeText(FileToEncryptActivity.this, |
---|
| 199 | // "FileExtension: " + fileExtension + "\n" + |
---|
| 200 | // "MimeType: " + mimeType, |
---|
| 201 | // Toast.LENGTH_LONG).show(); |
---|
| 202 | |
---|
| 203 | |
---|
| 204 | // en principio no filtrar ningun archivo |
---|
| 205 | files.add(f); |
---|
| 206 | |
---|
| 207 | } |
---|
| 208 | } |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | Collections.sort(directories); // Ordena los directorios alfabeticamente |
---|
| 212 | Collections.sort(files); // Ordena los archivos alfabeticamente |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | /** |
---|
| 216 | * Retorna cantidad total de elementos que se listarán en el directorio. |
---|
| 217 | */ |
---|
| 218 | @Override |
---|
| 219 | public int getCount() { |
---|
| 220 | |
---|
| 221 | return directories.size() + files.size(); |
---|
| 222 | |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | /** |
---|
| 226 | * Dada una posición en el listado del directorio, retorna un archivo o directorio |
---|
| 227 | * según corresponda. |
---|
| 228 | */ |
---|
| 229 | @Override |
---|
| 230 | public File getItem(int position) { |
---|
| 231 | |
---|
| 232 | if (position < directories.size()) { |
---|
| 233 | return directories.get(position); |
---|
| 234 | } else { |
---|
| 235 | return files.get(position - directories.size()); |
---|
| 236 | } |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | /** |
---|
| 240 | * Retorna un código hash para el archivo, permite comparar si dos archivos son los mismos |
---|
| 241 | */ |
---|
| 242 | @Override |
---|
| 243 | public long getItemId(int position) { |
---|
| 244 | |
---|
| 245 | return getItem(position).hashCode(); |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | /** |
---|
| 249 | * Crea la visualización de cada item del fileBrowser |
---|
| 250 | */ |
---|
| 251 | @Override |
---|
| 252 | public View getView(int position, View convertView, ViewGroup parent) { |
---|
| 253 | |
---|
| 254 | //Crea la vista de cada fila del filebrowser a partir del layout |
---|
| 255 | if (convertView == null) { |
---|
| 256 | LayoutInflater inflater = LayoutInflater.from(FileToEncryptActivity.this); |
---|
| 257 | convertView = inflater.inflate(R.layout.file_to_verify_bdoc_signature_item, parent, false); |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | // Se enlaza a cada componente del layout |
---|
| 261 | ImageView image = (ImageView) convertView.findViewById(R.id.type_image); |
---|
| 262 | TextView fileName = (TextView) convertView.findViewById(R.id.filename_text); |
---|
| 263 | TextView modified = (TextView) convertView.findViewById(R.id.filename_modified); |
---|
| 264 | |
---|
| 265 | // Se obtiene el archivo ubicado en position |
---|
| 266 | File file = getItem(position); |
---|
| 267 | |
---|
| 268 | //RadioButton |
---|
| 269 | RadioButton radio = (RadioButton) convertView.findViewById(R.id.file_radio); |
---|
| 270 | radio.setFocusable(false); |
---|
| 271 | |
---|
| 272 | // Se asignan los iconos según el tipo de archivo y se oculta el radio en los directorios |
---|
| 273 | if (file.isDirectory()) { |
---|
[58c7f6a] | 274 | image.setImageResource(R.drawable.ic_carpeta_verde); |
---|
[ae1bcd1] | 275 | radio.setVisibility(View.INVISIBLE); |
---|
| 276 | radio.setChecked(false); |
---|
| 277 | } else { |
---|
[58c7f6a] | 278 | image.setImageResource(R.drawable.ic_archivo_verde); |
---|
[ae1bcd1] | 279 | radio.setVisibility(View.VISIBLE); |
---|
| 280 | |
---|
| 281 | if (FileToEncryptActivity.this.selected == null || |
---|
| 282 | FileToEncryptActivity.this.selected.hashCode() != file.hashCode()){ |
---|
| 283 | radio.setChecked(false); |
---|
| 284 | } else{ |
---|
| 285 | radio.setChecked(true); |
---|
| 286 | } |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | // Si es el directorio que hace referencia al padre le coloca como nombre ".." |
---|
| 290 | if (file.isDirectory() && position == 0 && ! "/".equals(FileToEncryptActivity.this.cwd.getAbsolutePath())) { |
---|
| 291 | fileName.setText(".."); |
---|
| 292 | } else { |
---|
| 293 | fileName.setText(file.getName()); |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | |
---|
| 297 | //Datos de modificación del archivo |
---|
| 298 | SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); |
---|
| 299 | Date date = new Date(file.lastModified()); |
---|
| 300 | |
---|
| 301 | String dateString = FileToEncryptActivity.this.getString(R.string.modified)+": "; |
---|
| 302 | if (file.lastModified()>0) |
---|
| 303 | modified.setText(dateString+sdf.format(date)); |
---|
| 304 | else |
---|
| 305 | modified.setText(dateString+"-"); |
---|
| 306 | |
---|
| 307 | return convertView; |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | /** |
---|
| 311 | * Controla la selección de cada item del fileBrowser |
---|
| 312 | */ |
---|
| 313 | public void select(ListView parent, int position) { |
---|
| 314 | |
---|
| 315 | File item = getItem(position); |
---|
| 316 | //Si es un directorio el seleccionado se hace un llamado del fileBrowser del directorio |
---|
| 317 | if (item.isDirectory()) { |
---|
| 318 | parent.setAdapter(FileToEncryptActivity.this.listAdapter = new FileListAdapter(item.getAbsolutePath(), filterMImeType)); |
---|
| 319 | } else { // Si es un archivo |
---|
| 320 | |
---|
| 321 | //Se agrega el archivo a la lista de seleccionados si no se encuentra en la misma |
---|
| 322 | if (FileToEncryptActivity.this.selected == null || |
---|
| 323 | FileToEncryptActivity.this.selected.hashCode() != item.hashCode()){ |
---|
| 324 | |
---|
| 325 | FileToEncryptActivity.this.selected = item; |
---|
| 326 | |
---|
| 327 | FileToEncryptActivity.this.updateButton(FileToEncryptActivity.this.viewHolder.accept,true); |
---|
| 328 | |
---|
| 329 | } |
---|
| 330 | else{ // De lo contrario se elimina de la lista de seleccionados |
---|
| 331 | |
---|
| 332 | FileToEncryptActivity.this.selected = null; |
---|
| 333 | FileToEncryptActivity.this.updateButton(FileToEncryptActivity.this.viewHolder.accept,false); |
---|
| 334 | |
---|
| 335 | } |
---|
| 336 | notifyDataSetChanged(); |
---|
| 337 | } |
---|
| 338 | } |
---|
| 339 | } |
---|
| 340 | |
---|
| 341 | |
---|
| 342 | |
---|
| 343 | |
---|
| 344 | @Override |
---|
| 345 | protected void onSaveInstanceState(Bundle outState) { |
---|
| 346 | super.onSaveInstanceState(outState); |
---|
| 347 | |
---|
| 348 | outState.putParcelable("intent", this.getIntent()); |
---|
| 349 | outState.putString("cwd", this.cwd.getAbsolutePath()); |
---|
| 350 | if(this.selected != null) |
---|
| 351 | outState.putString("selected", this.selected.getAbsolutePath()); |
---|
| 352 | |
---|
| 353 | } |
---|
| 354 | |
---|
| 355 | private void updateButton(View v, boolean bool) { |
---|
| 356 | try{ |
---|
| 357 | |
---|
| 358 | v.setEnabled(bool); |
---|
| 359 | if (v instanceof TextView){ |
---|
| 360 | Drawable icon = ((TextView)v).getCompoundDrawables()[1]; |
---|
| 361 | if (icon!=null) |
---|
| 362 | if (bool) |
---|
| 363 | icon.setAlpha(255); |
---|
| 364 | else |
---|
| 365 | icon.setAlpha(127); |
---|
| 366 | } |
---|
| 367 | if (v instanceof ImageView){ |
---|
| 368 | ImageView result = (ImageView) v; |
---|
| 369 | if (bool) |
---|
| 370 | result.setAlpha(255); |
---|
| 371 | else |
---|
| 372 | result.setAlpha(127); |
---|
| 373 | } |
---|
| 374 | |
---|
| 375 | }catch(NullPointerException e){} |
---|
| 376 | |
---|
| 377 | } |
---|
| 378 | |
---|
| 379 | private void updateButton(LinearLayout layout, boolean bool) { |
---|
| 380 | try{ |
---|
| 381 | layout.setEnabled(bool); |
---|
| 382 | for (int i=0; i<layout.getChildCount();i++){ |
---|
| 383 | this.updateButton(layout.getChildAt(i), bool); |
---|
| 384 | } |
---|
| 385 | }catch(NullPointerException e){} |
---|
| 386 | |
---|
| 387 | } |
---|
| 388 | |
---|
| 389 | |
---|
| 390 | |
---|
| 391 | /** |
---|
| 392 | * Holds references to view objects. |
---|
| 393 | * |
---|
| 394 | * @author José M. Prieto (jmprieto@emergya.com) |
---|
| 395 | */ |
---|
| 396 | private class FileBrowserView { |
---|
| 397 | |
---|
| 398 | public ListView fileList; |
---|
| 399 | public TextView path; |
---|
| 400 | public LinearLayout accept; |
---|
| 401 | public LinearLayout clear; |
---|
| 402 | public TextView pathString; |
---|
| 403 | |
---|
| 404 | public FileBrowserView() { |
---|
| 405 | |
---|
| 406 | setContentView(R.layout.activity_file_to_encrypt); |
---|
| 407 | this.path = (TextView) findViewById(R.id.path); |
---|
| 408 | this.pathString = (TextView) findViewById(R.id.pathstring); |
---|
| 409 | |
---|
| 410 | this.fileList = (ListView) findViewById(R.id.file_list); |
---|
| 411 | this.fileList.setOnItemClickListener(FileToEncryptActivity.this); |
---|
| 412 | this.fileList.setItemsCanFocus(true); |
---|
| 413 | |
---|
| 414 | |
---|
| 415 | this.clear = (LinearLayout) findViewById(R.id.button_clear_zone); |
---|
| 416 | this.clear.setOnClickListener(FileToEncryptActivity.this); |
---|
| 417 | |
---|
| 418 | this.accept = (LinearLayout) findViewById(R.id.button_accept_zone); |
---|
| 419 | this.accept.setOnClickListener(FileToEncryptActivity.this); |
---|
| 420 | |
---|
| 421 | } |
---|
| 422 | } |
---|
| 423 | |
---|
| 424 | @Override |
---|
| 425 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
---|
| 426 | |
---|
| 427 | |
---|
| 428 | if (parent.getId() == R.id.file_list) { // user selects a file |
---|
| 429 | this.listAdapter.select((ListView) parent, position); |
---|
| 430 | |
---|
| 431 | } else { // user de-selects a file |
---|
| 432 | //this.listAdapter.addIfSameDirectory(selectedAdapter.doUnselect((ListView) parent, position)); |
---|
| 433 | |
---|
| 434 | } |
---|
| 435 | //this.viewHolder.numSelected.setText(Integer.toString(this.selected.size())); |
---|
| 436 | } |
---|
| 437 | |
---|
| 438 | @Override |
---|
| 439 | public void onClick(View v) { |
---|
| 440 | |
---|
| 441 | switch (v.getId()) { |
---|
| 442 | |
---|
| 443 | |
---|
| 444 | case R.id.button_clear_zone: |
---|
| 445 | |
---|
| 446 | this.selected = null; |
---|
| 447 | this.listAdapter.notifyDataSetChanged(); |
---|
| 448 | |
---|
| 449 | //this.viewHolder.numSelected.setText(""+this.selected.size()); |
---|
| 450 | this.updateButton(this.viewHolder.accept, false); |
---|
| 451 | break; |
---|
| 452 | |
---|
| 453 | |
---|
| 454 | |
---|
| 455 | case R.id.button_accept_zone: |
---|
| 456 | |
---|
| 457 | // lanzar intent para compartir el archivo seleccionado |
---|
| 458 | fileToEncrypt = FileToEncryptActivity.this.selected.getAbsolutePath(); |
---|
| 459 | |
---|
[ba6bc7c] | 460 | //Toast.makeText(getApplicationContext(), "FileToEncryptActivity: "+fileToEncrypt, Toast.LENGTH_SHORT).show(); |
---|
[ae1bcd1] | 461 | |
---|
| 462 | |
---|
| 463 | |
---|
| 464 | // Crear directorio DownloadedFiles donde se almacenan los archivos descargados |
---|
| 465 | // desde un servidor para verificar su firma |
---|
| 466 | //String downloadedDirFiles = Environment.getExternalStorageDirectory() + "/" + |
---|
| 467 | //getResources().getString(R.string.app_name) + "/" + |
---|
| 468 | //getResources().getString(R.string.downloaded_dir_files) + "/"; |
---|
| 469 | |
---|
| 470 | //String extractedDirFiles = Environment.getExternalStorageDirectory() + "/" + |
---|
| 471 | //getResources().getString(R.string.app_name) + "/" + |
---|
| 472 | //getResources().getString(R.string.extracted_dir_files) + "/"; |
---|
| 473 | |
---|
| 474 | //Toast.makeText(getApplicationContext(), "downloadedDirFiles: " + downloadedDirFiles, Toast.LENGTH_SHORT).show(); |
---|
| 475 | //Toast.makeText(getApplicationContext(), "extractedDirFiles: " + extractedDirFiles, Toast.LENGTH_SHORT).show(); |
---|
| 476 | |
---|
| 477 | //shareIt(); |
---|
| 478 | |
---|
| 479 | selectCertificateToEncrypt(fileToEncrypt); |
---|
| 480 | |
---|
| 481 | break; |
---|
| 482 | |
---|
| 483 | } |
---|
| 484 | } |
---|
| 485 | |
---|
| 486 | |
---|
| 487 | // funcion para compartir el archivo |
---|
| 488 | private void shareIt() { |
---|
| 489 | |
---|
| 490 | Intent shareIntent = new Intent(); |
---|
| 491 | shareIntent.setAction(Intent.ACTION_SEND); |
---|
| 492 | File file = new File(fileToEncrypt); |
---|
| 493 | |
---|
| 494 | |
---|
| 495 | Uri uri = Uri.fromFile(file); |
---|
| 496 | Log.i("DEBUG", file.getPath()); |
---|
| 497 | //Log.d("******", getMimeType(file.getPath())); |
---|
| 498 | //shareIntent.setDataAndType(uri, getMimeType(file.getPath())); |
---|
| 499 | shareIntent.putExtra(Intent.EXTRA_STREAM, uri); |
---|
| 500 | shareIntent.setType("application/*"); |
---|
| 501 | startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.share_it_using))); |
---|
| 502 | } |
---|
| 503 | |
---|
| 504 | |
---|
| 505 | /** |
---|
| 506 | * Selecciona el certificado del destinatario del directorio de certificados |
---|
| 507 | * para cifrado |
---|
| 508 | * @return void |
---|
| 509 | */ |
---|
| 510 | // funcion para desplegar el gestor de certificados de destinatarios para cifrar |
---|
| 511 | private void selectCertificateToEncrypt(String fileToEncrypt) { |
---|
| 512 | |
---|
| 513 | // desplegar la actividad de gestor de certificados de destinatarios |
---|
| 514 | |
---|
| 515 | // chequear disponibilidad de directorio de certificados |
---|
| 516 | if (!checkCertificatesDirectoryAvailability()){ |
---|
[ba6bc7c] | 517 | //Toast.makeText(getApplicationContext(), "FileToEncryptActivity: directorio no disponible", Toast.LENGTH_SHORT).show(); |
---|
[ae1bcd1] | 518 | |
---|
| 519 | finish(); |
---|
| 520 | return; |
---|
| 521 | }else{ |
---|
| 522 | // lanzar el activity SelectCeritificateToEncryptActivity |
---|
| 523 | Intent intent = new Intent(this, SelectCertificateToEncryptActivity.class); |
---|
| 524 | intent.putExtra("fileToEncrypt", fileToEncrypt); |
---|
| 525 | startActivity(intent); |
---|
| 526 | |
---|
| 527 | } |
---|
| 528 | |
---|
| 529 | |
---|
| 530 | |
---|
| 531 | |
---|
| 532 | |
---|
| 533 | } // fin de selectRecipientCertificate() |
---|
| 534 | |
---|
| 535 | /** |
---|
| 536 | * Chequea la disponibilidad del directorio de /TibisayMovil/CertificatesToEncrypt |
---|
| 537 | * @return boolean |
---|
| 538 | */ |
---|
| 539 | private boolean checkCertificatesDirectoryAvailability() { |
---|
| 540 | // verificar acceso al directorio /mnt/sdcard/TibisayMovil/CertificatesToEncrypt |
---|
| 541 | boolean mExternalStorageAvailable = false; |
---|
| 542 | boolean mExternalStorageWriteable = false; |
---|
| 543 | String state = Environment.getExternalStorageState(); |
---|
| 544 | |
---|
| 545 | String certificatesDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + |
---|
| 546 | getResources().getString(R.string.app_name) + "/" + |
---|
| 547 | getResources().getString(R.string.certificate_dir_files) + "/"; |
---|
| 548 | |
---|
| 549 | AlertDialog.Builder builder = new AlertDialog.Builder(FileToEncryptActivity.this); |
---|
| 550 | |
---|
| 551 | |
---|
| 552 | if (Environment.MEDIA_MOUNTED.equals(state)) { |
---|
| 553 | // We can read and write the media |
---|
| 554 | mExternalStorageAvailable = mExternalStorageWriteable = true; |
---|
[ba6bc7c] | 555 | //Toast.makeText(getApplicationContext(), "We can read and write the media", Toast.LENGTH_SHORT).show(); |
---|
[ae1bcd1] | 556 | |
---|
| 557 | // Crear directorio CertificatesToEncrypt donde se almacenan los certificados de |
---|
| 558 | // destinatarios para cifrado |
---|
| 559 | /* |
---|
| 560 | String certificatesDir = Environment.getExternalStorageDirectory() + "/" + |
---|
| 561 | getResources().getString(R.string.app_name) + "/" + |
---|
| 562 | getResources().getString(R.string.certificates_dir) + "/"; |
---|
| 563 | */ |
---|
| 564 | if (prepareDirectory(certificatesDir)){ |
---|
| 565 | return true; |
---|
| 566 | }else{ |
---|
| 567 | builder.setMessage("No existe el directorio "+certificatesDir+" para almacenar certificados.").setTitle("Error:"); |
---|
| 568 | |
---|
| 569 | } |
---|
| 570 | |
---|
| 571 | } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { |
---|
| 572 | // We can only read the media |
---|
| 573 | mExternalStorageAvailable = true; |
---|
| 574 | mExternalStorageWriteable = false; |
---|
[ba6bc7c] | 575 | //Toast.makeText(getApplicationContext(), "We can only read the media", Toast.LENGTH_SHORT).show(); |
---|
[ae1bcd1] | 576 | |
---|
| 577 | builder.setMessage("Directorio "+certificatesDir+ " montado de solo lectura. No se pueden almancenar certificados.").setTitle("Error:"); |
---|
| 578 | |
---|
| 579 | |
---|
| 580 | } else { |
---|
| 581 | // Something else is wrong. It may be one of many other states, but all we need |
---|
| 582 | // to know is we can neither read nor write |
---|
| 583 | mExternalStorageAvailable = mExternalStorageWriteable = false; |
---|
[ba6bc7c] | 584 | //Toast.makeText(getApplicationContext(), "we can neither read nor write", Toast.LENGTH_SHORT).show(); |
---|
[ae1bcd1] | 585 | |
---|
| 586 | builder.setMessage("Directorio "+certificatesDir+ " no está disponible. No se pueden almancenar certificados.").setTitle("Error:"); |
---|
| 587 | |
---|
| 588 | } |
---|
| 589 | |
---|
| 590 | |
---|
| 591 | builder.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() { |
---|
| 592 | public void onClick(DialogInterface dialog, int id) { |
---|
| 593 | // User cancelled the dialog |
---|
| 594 | FileToEncryptActivity.this.finish(); |
---|
| 595 | } |
---|
| 596 | }); |
---|
| 597 | AlertDialog dialog = builder.create(); |
---|
| 598 | dialog.show(); |
---|
| 599 | return false; |
---|
| 600 | } // fin de checkCertificatesDirectoryAvailability |
---|
| 601 | |
---|
| 602 | |
---|
| 603 | /** |
---|
| 604 | * Prepara directorio |
---|
| 605 | * @return boolean |
---|
| 606 | */ |
---|
| 607 | boolean prepareDirectory(String dir) |
---|
| 608 | { |
---|
| 609 | try |
---|
| 610 | { |
---|
| 611 | if (makedirs(dir)) |
---|
| 612 | { |
---|
| 613 | return true; |
---|
| 614 | } else { |
---|
| 615 | return false; |
---|
| 616 | } |
---|
| 617 | } catch (Exception e) |
---|
| 618 | { |
---|
| 619 | e.printStackTrace(); |
---|
[ba6bc7c] | 620 | //Toast.makeText(this, "Could not initiate File System.. Is Sdcard mounted properly?", Toast.LENGTH_LONG).show(); |
---|
| 621 | Toast.makeText(this, "No se pudo iniciar el sistema de archivos. ¿Está la SDCARD montada?", Toast.LENGTH_LONG).show(); |
---|
[ae1bcd1] | 622 | return false; |
---|
| 623 | } |
---|
| 624 | } |
---|
| 625 | |
---|
| 626 | /** |
---|
| 627 | * Crea directorio utilizando la variable tmpDir |
---|
| 628 | * @return boolean |
---|
| 629 | */ |
---|
| 630 | private boolean makedirs(String dir) |
---|
| 631 | { |
---|
| 632 | //File tempdir = new File(extractedDirFiles); |
---|
| 633 | File tempdir = new File(dir); |
---|
| 634 | if (!tempdir.exists()) |
---|
| 635 | tempdir.mkdirs(); |
---|
| 636 | return (tempdir.isDirectory()); |
---|
| 637 | } |
---|
| 638 | |
---|
| 639 | |
---|
| 640 | } |
---|
| 641 | |
---|