2 * Copyright (C) 2007-2008 OpenIntents.org
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
17 package org.openintents.filemanager.util;
\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
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
33 * @version 2009-07-03
\r
38 public class FileUtils {
\r
40 /** TAG for log messages. */
\r
41 static final String TAG = "FileUtils";
\r
42 private static final int X_OK = 1;
\r
44 private static boolean libLoadSuccess;
\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
57 * use it to calculate file count in the directory recursively
\r
59 private static int fileCount = 0;
\r
62 * Whether the filename is a video file.
\r
67 public static boolean isVideo(String filename) {
\r
68 String mimeType = getMimeType(filename);
\r
69 if (mimeType != null && mimeType.startsWith("video/")) {
\r
77 * Whether the URI is a local one.
\r
82 public static boolean isLocal(String uri) {
\r
83 if (uri != null && !uri.startsWith("http://")) {
\r
90 * Gets the extension of a file name, like ".png" or ".jpg".
\r
93 * @return Extension including the dot("."); "" if there is no extension;
\r
94 * null if uri was null.
\r
96 public static String getExtension(String uri) {
\r
101 int dot = uri.lastIndexOf(".");
\r
103 return uri.substring(dot);
\r
111 * Returns true if uri is a media uri.
\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
128 * Convert File into Uri.
\r
132 public static Uri getUri(File file) {
\r
133 if (file != null) {
\r
134 return Uri.fromFile(file);
\r
140 * Convert Uri into File.
\r
144 public static File getFile(Uri uri) {
\r
146 String filepath = uri.getPath();
\r
147 if (filepath != null) {
\r
148 return new File(filepath);
\r
155 * Returns the path only (without file name).
\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
165 String filename = file.getName();
\r
166 String filepath = file.getAbsolutePath();
\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
173 return new File(pathwithoutname);
\r
180 * Constructs a file from a path and file name.
\r
186 public static File getFile(String curdir, String file) {
\r
187 String separator = "/";
\r
188 if (curdir.endsWith("/")) {
\r
191 File clickedFile = new File(curdir + separator
\r
193 return clickedFile;
\r
196 public static File getFile(File curdir, String file) {
\r
197 return getFile(curdir.getAbsolutePath(), file);
\r
200 public static String formatSize(Context context, long sizeInBytes) {
\r
201 return Formatter.formatFileSize(context, sizeInBytes);
\r
204 public static String formatDate(Context context, long dateTime) {
\r
205 return DateFormat.getDateFormat(context).format(new Date(dateTime));
\r
208 public static int getFileCount(File file){
\r
210 calculateFileCount(file);
\r
215 * @param f - file which need be checked
\r
216 * @return if is archive - returns true othewise
\r
218 public static boolean checkIfZipArchive(File f){
\r
222 } catch (Exception e){
\r
227 private static void calculateFileCount(File file){
\r
228 if (!file.isDirectory()){
\r
232 if (file.list() == null){
\r
235 for (String fileName: file.list()){
\r
236 File f = new File(file.getAbsolutePath()+File.separator+fileName);
\r
237 calculateFileCount(f);
\r
242 * Native helper method, returns whether the current process has execute privilages.
\r
244 * @return returns TRUE if the current process has execute privilages.
\r
246 public static boolean canExecute(File mContextFile) {
\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
253 } catch (Exception e) {
\r
254 if(libLoadSuccess){
\r
255 return access(mContextFile.getPath(), X_OK);
\r
262 // Native interface to unistd.h's access(*char, int) method.
\r
263 public static native boolean access(String path, int mode);
\r