arnaucube
arnaucube/openEventsPlatformAppPublic Self-hosted repository
openEventsPlatformApp/plugins/cordova-plugin-file/src/android/DirectoryManager.java
plugins/cordova-plugin-file/src/android/DirectoryManager.javaRaw
  1/*
  2       Licensed to the Apache Software Foundation (ASF) under one
  3       or more contributor license agreements.  See the NOTICE file
  4       distributed with this work for additional information
  5       regarding copyright ownership.  The ASF licenses this file
  6       to you under the Apache License, Version 2.0 (the
  7       "License"); you may not use this file except in compliance
  8       with the License.  You may obtain a copy of the License at
  9
 10         http://www.apache.org/licenses/LICENSE-2.0
 11
 12       Unless required by applicable law or agreed to in writing,
 13       software distributed under the License is distributed on an
 14       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15       KIND, either express or implied.  See the License for the
 16       specific language governing permissions and limitations
 17       under the License.
 18*/
 19package org.apache.cordova.file;
 20
 21import android.os.Environment;
 22import android.os.StatFs;
 23
 24import java.io.File;
 25
 26/**
 27 * This class provides file directory utilities.
 28 * All file operations are performed on the SD card.
 29 *
 30 * It is used by the FileUtils class.
 31 */
 32public class DirectoryManager {
 33
 34    @SuppressWarnings("unused")
 35    private static final String LOG_TAG = "DirectoryManager";
 36
 37    /**
 38     * Determine if a file or directory exists.
 39     * @param name				The name of the file to check.
 40     * @return					T=exists, F=not found
 41     */
 42    public static boolean testFileExists(String name) {
 43        boolean status;
 44
 45        // If SD card exists
 46        if ((testSaveLocationExists()) && (!name.equals(""))) {
 47            File path = Environment.getExternalStorageDirectory();
 48            File newPath = constructFilePaths(path.toString(), name);
 49            status = newPath.exists();
 50        }
 51        // If no SD card
 52        else {
 53            status = false;
 54        }
 55        return status;
 56    }
 57
 58    /**
 59     * Get the free space in external storage
 60     *
 61     * @return 		Size in KB or -1 if not available
 62     */
 63    public static long getFreeExternalStorageSpace() {
 64        String status = Environment.getExternalStorageState();
 65        long freeSpaceInBytes = 0;
 66
 67        // Check if external storage exists
 68        if (status.equals(Environment.MEDIA_MOUNTED)) {
 69            freeSpaceInBytes = getFreeSpaceInBytes(Environment.getExternalStorageDirectory().getPath());
 70        } else {
 71            // If no external storage then return -1
 72            return -1;
 73        }
 74
 75        return freeSpaceInBytes / 1024;
 76    }
 77
 78    /**
 79     * Given a path return the number of free bytes in the filesystem containing the path.
 80     *
 81     * @param path to the file system
 82     * @return free space in bytes
 83     */
 84    public static long getFreeSpaceInBytes(String path) {
 85        try {
 86            StatFs stat = new StatFs(path);
 87            long blockSize = stat.getBlockSize();
 88            long availableBlocks = stat.getAvailableBlocks();
 89            return availableBlocks * blockSize;
 90        } catch (IllegalArgumentException e) {
 91            // The path was invalid. Just return 0 free bytes.
 92            return 0;
 93        }
 94    }
 95
 96    /**
 97     * Determine if SD card exists.
 98     *
 99     * @return				T=exists, F=not found
100     */
101    public static boolean testSaveLocationExists() {
102        String sDCardStatus = Environment.getExternalStorageState();
103        boolean status;
104
105        // If SD card is mounted
106        if (sDCardStatus.equals(Environment.MEDIA_MOUNTED)) {
107            status = true;
108        }
109
110        // If no SD card
111        else {
112            status = false;
113        }
114        return status;
115    }
116
117    /**
118     * Create a new file object from two file paths.
119     *
120     * @param file1			Base file path
121     * @param file2			Remaining file path
122     * @return				File object
123     */
124    private static File constructFilePaths (String file1, String file2) {
125        File newPath;
126        if (file2.startsWith(file1)) {
127            newPath = new File(file2);
128        }
129        else {
130            newPath = new File(file1 + "/" + file2);
131        }
132        return newPath;
133    }
134}