--- /dev/null
+/* \r
+ * Copyright (C) 2007-2008 OpenIntents.org\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package org.openintents.filemanager.util;\r
+\r
+import java.io.File;\r
+import java.lang.reflect.Method;\r
+import java.util.Date;\r
+import java.util.zip.ZipFile;\r
+\r
+import android.content.Context;\r
+import android.net.Uri;\r
+import android.provider.MediaStore.Audio;\r
+import android.provider.MediaStore.Video;\r
+import android.text.format.DateFormat;\r
+import android.text.format.Formatter;\r
+import android.util.Log;\r
+\r
+/**\r
+ * @version 2009-07-03\r
+ * \r
+ * @author Peli\r
+ *\r
+ */\r
+public class FileUtils {\r
+ \r
+ /** TAG for log messages. */\r
+ static final String TAG = "FileUtils";\r
+ private static final int X_OK = 1;\r
+ \r
+ private static boolean libLoadSuccess;\r
+ \r
+ static {\r
+ try {\r
+ System.loadLibrary("access");\r
+ libLoadSuccess = true;\r
+ } catch(UnsatisfiedLinkError e) {\r
+ libLoadSuccess = false;\r
+ Log.d(TAG, "libaccess.so failed to load.");\r
+ }\r
+ }\r
+\r
+ /**\r
+ * use it to calculate file count in the directory recursively\r
+ */\r
+ private static int fileCount = 0;\r
+\r
+ /**\r
+ * Whether the filename is a video file.\r
+ * \r
+ * @param filename\r
+ * @return\r
+ *//*\r
+ public static boolean isVideo(String filename) {\r
+ String mimeType = getMimeType(filename);\r
+ if (mimeType != null && mimeType.startsWith("video/")) {\r
+ return true;\r
+ } else {\r
+ return false;\r
+ }\r
+ }*/\r
+\r
+ /**\r
+ * Whether the URI is a local one.\r
+ * \r
+ * @param uri\r
+ * @return\r
+ */\r
+ public static boolean isLocal(String uri) {\r
+ if (uri != null && !uri.startsWith("http://")) {\r
+ return true;\r
+ }\r
+ return false;\r
+ }\r
+\r
+ /**\r
+ * Gets the extension of a file name, like ".png" or ".jpg".\r
+ * \r
+ * @param uri\r
+ * @return Extension including the dot("."); "" if there is no extension;\r
+ * null if uri was null.\r
+ */\r
+ public static String getExtension(String uri) {\r
+ if (uri == null) {\r
+ return null;\r
+ }\r
+\r
+ int dot = uri.lastIndexOf(".");\r
+ if (dot >= 0) {\r
+ return uri.substring(dot);\r
+ } else {\r
+ // No extension.\r
+ return "";\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Returns true if uri is a media uri.\r
+ * \r
+ * @param uri\r
+ * @return\r
+ */\r
+ public static boolean isMediaUri(String uri) {\r
+ if (uri.startsWith(Audio.Media.INTERNAL_CONTENT_URI.toString())\r
+ || uri.startsWith(Audio.Media.EXTERNAL_CONTENT_URI.toString())\r
+ || uri.startsWith(Video.Media.INTERNAL_CONTENT_URI.toString())\r
+ || uri.startsWith(Video.Media.EXTERNAL_CONTENT_URI.toString())) {\r
+ return true;\r
+ } else {\r
+ return false;\r
+ }\r
+ }\r
+ \r
+ /**\r
+ * Convert File into Uri.\r
+ * @param file\r
+ * @return uri\r
+ */\r
+ public static Uri getUri(File file) {\r
+ if (file != null) {\r
+ return Uri.fromFile(file);\r
+ }\r
+ return null;\r
+ }\r
+ \r
+ /**\r
+ * Convert Uri into File.\r
+ * @param uri\r
+ * @return file\r
+ */\r
+ public static File getFile(Uri uri) {\r
+ if (uri != null) {\r
+ String filepath = uri.getPath();\r
+ if (filepath != null) {\r
+ return new File(filepath);\r
+ }\r
+ }\r
+ return null;\r
+ }\r
+ \r
+ /**\r
+ * Returns the path only (without file name).\r
+ * @param file\r
+ * @return\r
+ */\r
+ public static File getPathWithoutFilename(File file) {\r
+ if (file != null) {\r
+ if (file.isDirectory()) {\r
+ // no file to be split off. Return everything\r
+ return file;\r
+ } else {\r
+ String filename = file.getName();\r
+ String filepath = file.getAbsolutePath();\r
+ \r
+ // Construct path without file name.\r
+ String pathwithoutname = filepath.substring(0, filepath.length() - filename.length());\r
+ if (pathwithoutname.endsWith("/")) {\r
+ pathwithoutname = pathwithoutname.substring(0, pathwithoutname.length() - 1);\r
+ }\r
+ return new File(pathwithoutname);\r
+ }\r
+ }\r
+ return null;\r
+ }\r
+\r
+ /**\r
+ * Constructs a file from a path and file name.\r
+ * \r
+ * @param curdir\r
+ * @param file\r
+ * @return\r
+ */\r
+ public static File getFile(String curdir, String file) {\r
+ String separator = "/";\r
+ if (curdir.endsWith("/")) {\r
+ separator = "";\r
+ }\r
+ File clickedFile = new File(curdir + separator\r
+ + file);\r
+ return clickedFile;\r
+ }\r
+ \r
+ public static File getFile(File curdir, String file) {\r
+ return getFile(curdir.getAbsolutePath(), file);\r
+ }\r
+ \r
+ public static String formatSize(Context context, long sizeInBytes) {\r
+ return Formatter.formatFileSize(context, sizeInBytes);\r
+ }\r
+ \r
+ public static String formatDate(Context context, long dateTime) {\r
+ return DateFormat.getDateFormat(context).format(new Date(dateTime));\r
+ }\r
+\r
+ public static int getFileCount(File file){\r
+ fileCount = 0;\r
+ calculateFileCount(file);\r
+ return fileCount;\r
+ }\r
+\r
+ /**\r
+ * @param f - file which need be checked\r
+ * @return if is archive - returns true othewise\r
+ */\r
+ public static boolean checkIfZipArchive(File f){\r
+ try {\r
+ new ZipFile(f);\r
+ return true;\r
+ } catch (Exception e){\r
+ return false;\r
+ }\r
+ }\r
+\r
+ private static void calculateFileCount(File file){\r
+ if (!file.isDirectory()){\r
+ fileCount++;\r
+ return;\r
+ }\r
+ if (file.list() == null){\r
+ return;\r
+ }\r
+ for (String fileName: file.list()){\r
+ File f = new File(file.getAbsolutePath()+File.separator+fileName);\r
+ calculateFileCount(f);\r
+ }\r
+ } \r
+ \r
+ /**\r
+ * Native helper method, returns whether the current process has execute privilages.\r
+ * @param a File\r
+ * @return returns TRUE if the current process has execute privilages.\r
+ */\r
+ public static boolean canExecute(File mContextFile) {\r
+ try {\r
+ // File.canExecute() was introduced in API 9. If it doesn't exist, then\r
+ // this will throw an exception and the NDK version will be used.\r
+ Method m = File.class.getMethod("canExecute", new Class[] {} );\r
+ Boolean result=(Boolean)m.invoke(mContextFile);\r
+ return result;\r
+ } catch (Exception e) {\r
+ if(libLoadSuccess){\r
+ return access(mContextFile.getPath(), X_OK);\r
+ } else {\r
+ return false;\r
+ }\r
+ }\r
+ }\r
+ \r
+ // Native interface to unistd.h's access(*char, int) method.\r
+ public static native boolean access(String path, int mode);\r
+}\r