• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.nfc.cardemulation.util;
17 
18 import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
19 
20 import android.util.Log;
21 
22 import java.io.File;
23 import java.io.IOException;
24 import java.nio.file.Files;
25 
26 public class NfcFileUtils {
27     private static final String TAG = "NfcFileUtils";
28 
29     /**
30      * Check if there are any files in the provided directory.
31      * @return true if there are no files, false otherwise.
32      */
isEmptyDir(File dir)33     public static boolean isEmptyDir(File dir) {
34         final File[] files = dir.listFiles();
35         return files == null || files.length == 0;
36     }
37 
38     /**
39      * Try our best to migrate all files from source to target.
40      *
41      * @return the number of files moved, or -1 if there was trouble.
42      */
moveFiles(File sourceDir, File targetDir)43     public static int moveFiles(File sourceDir, File targetDir) {
44         final File[] sourceFiles = sourceDir.listFiles();
45         if (sourceFiles == null) return -1;
46         int res = 0;
47         for (File sourceFile : sourceFiles) {
48             final File targetFile = new File(targetDir, sourceFile.getName());
49             Log.d(TAG, "moveFiles: Migrating " + sourceFile + " to " + targetFile);
50             try {
51                 Files.move(sourceFile.toPath(), targetFile.toPath(), REPLACE_EXISTING);
52                 if (res != -1) {
53                     res++;
54                 }
55             } catch (IOException e) {
56                 Log.w(TAG, "moveFiles: Failed to migrate " + sourceFile + ": " + e);
57                 res = -1;
58             }
59         }
60         return res;
61     }
62 }
63