1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.inputmethod.latin.utils;
18
19import java.io.File;
20import java.io.FilenameFilter;
21
22/**
23 * A simple class to help with removing directories recursively.
24 */
25public class FileUtils {
26    public static boolean deleteRecursively(final File path) {
27        if (path.isDirectory()) {
28            final File[] files = path.listFiles();
29            if (files != null) {
30                for (final File child : files) {
31                    deleteRecursively(child);
32                }
33            }
34        }
35        return path.delete();
36    }
37
38    public static boolean deleteFilteredFiles(final File dir, final FilenameFilter fileNameFilter) {
39        if (!dir.isDirectory()) {
40            return false;
41        }
42        final File[] files = dir.listFiles(fileNameFilter);
43        if (files == null) {
44            return false;
45        }
46        boolean hasDeletedAllFiles = true;
47        for (final File file : files) {
48            if (!deleteRecursively(file)) {
49                hasDeletedAllFiles = false;
50            }
51        }
52        return hasDeletedAllFiles;
53    }
54}
55