switch to alsa.omap3 module
[android_pandora.git] / apps / oi-filemanager / FileManager / src / org / openintents / filemanager / util / FileUtils.java
1 /* \r
2  * Copyright (C) 2007-2008 OpenIntents.org\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *      http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 package org.openintents.filemanager.util;\r
18 \r
19 import java.io.File;\r
20 import java.lang.reflect.Method;\r
21 import java.util.Date;\r
22 import java.util.zip.ZipFile;\r
23 \r
24 import android.content.Context;\r
25 import android.net.Uri;\r
26 import android.provider.MediaStore.Audio;\r
27 import android.provider.MediaStore.Video;\r
28 import android.text.format.DateFormat;\r
29 import android.text.format.Formatter;\r
30 import android.util.Log;\r
31 \r
32 /**\r
33  * @version 2009-07-03\r
34  * \r
35  * @author Peli\r
36  *\r
37  */\r
38 public class FileUtils {\r
39         \r
40         /** TAG for log messages. */\r
41         static final String TAG = "FileUtils";\r
42         private static final int X_OK = 1;\r
43         \r
44         private static boolean libLoadSuccess;\r
45         \r
46         static {\r
47                 try {\r
48                         System.loadLibrary("access");\r
49                         libLoadSuccess = true;\r
50                 } catch(UnsatisfiedLinkError e) {\r
51                         libLoadSuccess = false;\r
52                         Log.d(TAG, "libaccess.so failed to load.");\r
53                 }\r
54         }\r
55 \r
56     /**\r
57      * use it to calculate file count in the directory recursively\r
58      */\r
59     private static int fileCount = 0;\r
60 \r
61         /**\r
62          * Whether the filename is a video file.\r
63          * \r
64          * @param filename\r
65          * @return\r
66          *//*\r
67         public static boolean isVideo(String filename) {\r
68                 String mimeType = getMimeType(filename);\r
69                 if (mimeType != null && mimeType.startsWith("video/")) {\r
70                         return true;\r
71                 } else {\r
72                         return false;\r
73                 }\r
74         }*/\r
75 \r
76         /**\r
77          * Whether the URI is a local one.\r
78          * \r
79          * @param uri\r
80          * @return\r
81          */\r
82         public static boolean isLocal(String uri) {\r
83                 if (uri != null && !uri.startsWith("http://")) {\r
84                         return true;\r
85                 }\r
86                 return false;\r
87         }\r
88 \r
89         /**\r
90          * Gets the extension of a file name, like ".png" or ".jpg".\r
91          * \r
92          * @param uri\r
93          * @return Extension including the dot("."); "" if there is no extension;\r
94          *         null if uri was null.\r
95          */\r
96         public static String getExtension(String uri) {\r
97                 if (uri == null) {\r
98                         return null;\r
99                 }\r
100 \r
101                 int dot = uri.lastIndexOf(".");\r
102                 if (dot >= 0) {\r
103                         return uri.substring(dot);\r
104                 } else {\r
105                         // No extension.\r
106                         return "";\r
107                 }\r
108         }\r
109 \r
110         /**\r
111          * Returns true if uri is a media uri.\r
112          * \r
113          * @param uri\r
114          * @return\r
115          */\r
116         public static boolean isMediaUri(String uri) {\r
117                 if (uri.startsWith(Audio.Media.INTERNAL_CONTENT_URI.toString())\r
118                                 || uri.startsWith(Audio.Media.EXTERNAL_CONTENT_URI.toString())\r
119                                 || uri.startsWith(Video.Media.INTERNAL_CONTENT_URI.toString())\r
120                                 || uri.startsWith(Video.Media.EXTERNAL_CONTENT_URI.toString())) {\r
121                         return true;\r
122                 } else {\r
123                         return false;\r
124                 }\r
125         }\r
126         \r
127         /**\r
128          * Convert File into Uri.\r
129          * @param file\r
130          * @return uri\r
131          */\r
132         public static Uri getUri(File file) {\r
133                 if (file != null) {\r
134                         return Uri.fromFile(file);\r
135                 }\r
136                 return null;\r
137         }\r
138         \r
139         /**\r
140          * Convert Uri into File.\r
141          * @param uri\r
142          * @return file\r
143          */\r
144         public static File getFile(Uri uri) {\r
145                 if (uri != null) {\r
146                         String filepath = uri.getPath();\r
147                         if (filepath != null) {\r
148                                 return new File(filepath);\r
149                         }\r
150                 }\r
151                 return null;\r
152         }\r
153         \r
154         /**\r
155          * Returns the path only (without file name).\r
156          * @param file\r
157          * @return\r
158          */\r
159         public static File getPathWithoutFilename(File file) {\r
160                  if (file != null) {\r
161                          if (file.isDirectory()) {\r
162                                  // no file to be split off. Return everything\r
163                                  return file;\r
164                          } else {\r
165                                  String filename = file.getName();\r
166                                  String filepath = file.getAbsolutePath();\r
167           \r
168                                  // Construct path without file name.\r
169                                  String pathwithoutname = filepath.substring(0, filepath.length() - filename.length());\r
170                                  if (pathwithoutname.endsWith("/")) {\r
171                                          pathwithoutname = pathwithoutname.substring(0, pathwithoutname.length() - 1);\r
172                                  }\r
173                                  return new File(pathwithoutname);\r
174                          }\r
175                  }\r
176                  return null;\r
177         }\r
178 \r
179         /**\r
180          * Constructs a file from a path and file name.\r
181          * \r
182          * @param curdir\r
183          * @param file\r
184          * @return\r
185          */\r
186         public static File getFile(String curdir, String file) {\r
187                 String separator = "/";\r
188                   if (curdir.endsWith("/")) {\r
189                           separator = "";\r
190                   }\r
191                    File clickedFile = new File(curdir + separator\r
192                                        + file);\r
193                 return clickedFile;\r
194         }\r
195         \r
196         public static File getFile(File curdir, String file) {\r
197                 return getFile(curdir.getAbsolutePath(), file);\r
198         }\r
199         \r
200         public static String formatSize(Context context, long sizeInBytes) {\r
201                 return Formatter.formatFileSize(context, sizeInBytes);\r
202         }\r
203         \r
204         public static String formatDate(Context context, long dateTime) {\r
205                 return DateFormat.getDateFormat(context).format(new Date(dateTime));\r
206         }\r
207 \r
208     public static int getFileCount(File file){\r
209         fileCount = 0;\r
210         calculateFileCount(file);\r
211         return fileCount;\r
212     }\r
213 \r
214     /**\r
215      * @param f  - file which need be checked\r
216      * @return if is archive - returns true othewise\r
217      */\r
218     public static boolean checkIfZipArchive(File f){\r
219         try {\r
220             new ZipFile(f);\r
221             return true;\r
222         } catch (Exception e){\r
223             return false;\r
224         }\r
225     }\r
226 \r
227     private static void calculateFileCount(File file){\r
228         if (!file.isDirectory()){\r
229             fileCount++;\r
230             return;\r
231         }\r
232         if (file.list() == null){\r
233             return;\r
234         }\r
235         for (String fileName: file.list()){\r
236             File f = new File(file.getAbsolutePath()+File.separator+fileName);\r
237             calculateFileCount(f);\r
238         }\r
239     }    \r
240         \r
241         /**\r
242          * Native helper method, returns whether the current process has execute privilages.\r
243          * @param a File\r
244          * @return returns TRUE if the current process has execute privilages.\r
245          */\r
246         public static boolean canExecute(File mContextFile) {\r
247                 try {\r
248                         // File.canExecute() was introduced in API 9.  If it doesn't exist, then\r
249                         // this will throw an exception and the NDK version will be used.\r
250                         Method m = File.class.getMethod("canExecute", new Class[] {} );\r
251                         Boolean result=(Boolean)m.invoke(mContextFile);\r
252                         return result;\r
253                 } catch (Exception e) {\r
254                         if(libLoadSuccess){\r
255                                 return access(mContextFile.getPath(), X_OK);\r
256                         } else {\r
257                                 return false;\r
258                         }\r
259                 }\r
260         }\r
261         \r
262         // Native interface to unistd.h's access(*char, int) method.\r
263         public static native boolean access(String path, int mode);\r
264 }\r