/* Tibisay Movil Copyright (C) 2013 Antonio Araujo (aaraujo@cenditel.gob.ve), Jose Ruiz (jruiz@cenditel.gob.ve), Fundacion Centro Nacional de Desarrollo e Investigacion en Tecnologias Libres - CENDITEL. La Fundación CENDITEL concede permiso para usar, copiar, distribuir y/o modificar este programa, reconociendo el derecho que la humanidad posee al libre acceso al conocimiento, bajo los términos de la licencia de software GPL versión 2.0 de la Free Software Foundation. Este programa se distribuye con la esperanza de que sea util, pero SIN NINGUNA GARANTIA; tampoco las implicitas garantias de MERCANTILIDAD o ADECUACION A UN PROPOSITO PARTICULAR. Para mayor información sobre los términos de la licencia ver el archivo llamado "gpl-2.0.txt" en ingles. */ package ve.gob.cenditel.tibisaymovil; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.channels.FileChannel; import java.util.Collection; import android.content.Context; import android.net.Uri; import android.os.Environment; import android.os.StatFs; import android.text.format.DateFormat; import android.util.Log; /** * File system related utilities. * * @author José M. Prieto (jmprieto@emergya.com) */ public class FsUtils { private static final String TAG = "FsUtils"; /** * Save raw data to a File. * * @param data the data to be saved * @param targetFilename name of the file to be created * @throws FileAlreadyExistsException if the file already exists * @throws FileNotFoundException if the file cannot be open for writing * @throws IOException if there is an I/O error */ public static Uri saveToFile(byte[] data, String targetFilename) throws FileAlreadyExistsException, FileNotFoundException, IOException { File file = createNewFile(targetFilename); OutputStream stream = new FileOutputStream(file); stream.write(data); stream.close(); return Uri.fromFile(file); } public static Uri saveToFile(InputStream data, String targetFilename) throws FileAlreadyExistsException, FileNotFoundException, IOException { File file = createNewFile(targetFilename); OutputStream stream = new FileOutputStream(file); byte[] buf = new byte[8192]; int n; while ((n = data.read(buf)) > 0) { stream.write(buf, 0, n); } data.close(); stream.close(); return Uri.fromFile(file); } public static void copyTo(File file, String directory) throws IOException { String target = directory + File.separator + file.getName(); FileChannel source = null; FileChannel destination = null; try { destination = new FileOutputStream(createNewFile(target)).getChannel(); source = new FileInputStream(file).getChannel(); destination.transferFrom(source, 0, source.size()); } finally { if (source != null) { source.close(); } if (destination != null) { destination.close(); } } } private static File createNewFile(String targetFilename) throws FileNotFoundException { File file = new File(targetFilename); String base = null; String extension = null; if (file.exists()) { int n; if ((n = targetFilename.lastIndexOf(".")) > targetFilename.lastIndexOf(File.separator)) { base = targetFilename.substring(0, n); extension = targetFilename.substring(n); } else { base = targetFilename; extension = ""; } } for (int i = 1; file.exists(); i++) { file = new File(base + "-" + i + extension); } return file; } public static void checkOutOfSpace(Exception e, boolean external) throws OutOfSpaceError { String path; if (external) { path = Environment.getExternalStorageDirectory().getAbsolutePath(); } else { path = Environment.getDataDirectory().getAbsolutePath(); } StatFs stat = new StatFs(path); int available = stat.getAvailableBlocks() * stat.getBlockSize(); if (available < 8192) { throw new OutOfSpaceError(e); } } public static String createOutputDirectory(Context context) { File root = new File(Environment.getExternalStorageDirectory(), context.getResources().getString(R.string.app_name) + File.separator + context.getResources().getString(R.string.signature_directory)); root.mkdirs(); try { new File(root.getAbsolutePath() + File.separator + ".nomedia").createNewFile(); } catch (IOException e) { Log.e(TAG, "Error creating .nomedia file", e); } CharSequence datePart = DateFormat.format("yyyy-MM-dd_kkmmss", System.currentTimeMillis()); String output = root.getAbsolutePath() + File.separator + datePart; if (new File(output).mkdirs()) { return output; } else { return null; } } public static boolean isPDF(String inputFilename) { return inputFilename.toLowerCase().endsWith(".pdf"); } public static boolean containsPDF(Collection uris) { for (Uri uri : uris) { if (isPDF(uri.getPath())) { return true; } } return false; } public static boolean isHiddenOrNotReadable(File f) { String name = f.getName(); boolean notReadable = !f.canRead(); boolean hidden = name.startsWith(".") && !name.equals(".") && !name.equals(".."); return notReadable || hidden; } }