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