1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/util/FileUtils.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,84 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko.util; 1.9 + 1.10 +import android.util.Log; 1.11 + 1.12 +import java.io.File; 1.13 +import java.io.IOException; 1.14 +import java.io.FilenameFilter; 1.15 + 1.16 +import org.mozilla.gecko.mozglue.RobocopTarget; 1.17 + 1.18 +public class FileUtils { 1.19 + private static final String LOGTAG= "GeckoFileUtils"; 1.20 + /* 1.21 + * A basic Filter for checking a filename and age. 1.22 + **/ 1.23 + static public class NameAndAgeFilter implements FilenameFilter { 1.24 + final private String mName; 1.25 + final private double mMaxAge; 1.26 + 1.27 + public NameAndAgeFilter(String name, double age) { 1.28 + mName = name; 1.29 + mMaxAge = age; 1.30 + } 1.31 + 1.32 + @Override 1.33 + public boolean accept(File dir, String filename) { 1.34 + if (mName == null || mName.matches(filename)) { 1.35 + File f = new File(dir, filename); 1.36 + 1.37 + if (mMaxAge < 0 || System.currentTimeMillis() - f.lastModified() > mMaxAge) { 1.38 + return true; 1.39 + } 1.40 + } 1.41 + 1.42 + return false; 1.43 + } 1.44 + } 1.45 + 1.46 + @RobocopTarget 1.47 + public static void delTree(File dir, FilenameFilter filter, boolean recurse) { 1.48 + String[] files = null; 1.49 + 1.50 + if (filter != null) { 1.51 + files = dir.list(filter); 1.52 + } else { 1.53 + files = dir.list(); 1.54 + } 1.55 + 1.56 + if (files == null) { 1.57 + return; 1.58 + } 1.59 + 1.60 + for (String file : files) { 1.61 + File f = new File(dir, file); 1.62 + delete(f, recurse); 1.63 + } 1.64 + } 1.65 + 1.66 + public static boolean delete(File file) throws IOException { 1.67 + return delete(file, true); 1.68 + } 1.69 + 1.70 + public static boolean delete(File file, boolean recurse) { 1.71 + if (file.isDirectory() && recurse) { 1.72 + // If the quick delete failed and this is a dir, recursively delete the contents of the dir 1.73 + String files[] = file.list(); 1.74 + for (String temp : files) { 1.75 + File fileDelete = new File(file, temp); 1.76 + try { 1.77 + delete(fileDelete); 1.78 + } catch(IOException ex) { 1.79 + Log.i(LOGTAG, "Error deleting " + fileDelete.getPath(), ex); 1.80 + } 1.81 + } 1.82 + } 1.83 + 1.84 + // Even if this is a dir, it should now be empty and delete should work 1.85 + return file.delete(); 1.86 + } 1.87 +}