/* * This file is part of Popcorn Time. * * Popcorn Time is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Popcorn Time is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Popcorn Time. If not, see <http://www.gnu.org/licenses/>. */ package pct.droid.base.utils; import android.content.Context; import android.os.Environment; import android.os.StatFs; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; public class StorageUtils { public static final String SD_CARD = "sdCard"; public static final String EXTERNAL_SD_CARD = "externalSdCard"; /** * @return {@code true} if external storage is available and writable. {@code false} otherwise. */ public static boolean isExternalStorageAvailable() { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { return true; } return false; } /** * @return A map of all storage locations available */ public static Map<String, File> getAllStorageLocations() { Map<String, File> map = new HashMap<String, File>(10); List<String> mMounts = new ArrayList<String>(10); List<String> mVold = new ArrayList<String>(10); mMounts.add("/mnt/sdcard"); mVold.add("/mnt/sdcard"); try { File mountFile = new File("/proc/mounts"); if (mountFile.exists()) { Scanner scanner = new Scanner(mountFile); while (scanner.hasNext()) { String line = scanner.nextLine(); if (line.startsWith("/dev/block/vold/")) { String[] lineElements = line.split(" "); String element = lineElements[1]; // don't add the default mount path // it's already in the list. if (!element.equals("/mnt/sdcard")) mMounts.add(element); } } } } catch (Exception e) { e.printStackTrace(); } try { File voldFile = new File("/system/etc/vold.fstab"); if (voldFile.exists()) { Scanner scanner = new Scanner(voldFile); while (scanner.hasNext()) { String line = scanner.nextLine(); if (line.startsWith("dev_mount")) { String[] lineElements = line.split(" "); String element = lineElements[2]; if (element.contains(":")) element = element.substring(0, element.indexOf(":")); if (!element.equals("/mnt/sdcard")) mVold.add(element); } } } } catch (Exception e) { e.printStackTrace(); } for (int i = 0; i < mMounts.size(); i++) { String mount = mMounts.get(i); if (!mVold.contains(mount)) mMounts.remove(i--); } mVold.clear(); List<String> mountHash = new ArrayList<String>(10); for (String mount : mMounts) { File root = new File(mount); if (root.exists() && root.isDirectory() && root.canWrite()) { File[] list = root.listFiles(); String hash = "["; if (list != null) { for (File f : list) { hash += f.getName().hashCode() + ":" + f.length() + ", "; } } hash += "]"; if (!mountHash.contains(hash)) { String key = SD_CARD + "_" + map.size(); if (map.size() == 0) { key = SD_CARD; } else if (map.size() == 1) { key = EXTERNAL_SD_CARD; } mountHash.add(hash); map.put(key, root); } } } mMounts.clear(); if (map.isEmpty()) { map.put(SD_CARD, Environment.getExternalStorageDirectory()); } return map; } /** * @return Available internal memory */ public static int getAvailableInternalMemorySize() { File path = Environment.getDataDirectory(); StatFs stat = new StatFs(path.getPath()); int blockSize = stat.getBlockSize(); int availableBlocks = stat.getAvailableBlocks(); return availableBlocks * blockSize; } /** * @return Total internal memory */ public static int getTotalInternalMemorySize() { File path = Environment.getDataDirectory(); StatFs stat = new StatFs(path.getPath()); int blockSize = stat.getBlockSize(); int totalBlocks = stat.getBlockCount(); return totalBlocks * blockSize; } /** * @return Available external memory */ public static int getAvailableExternalMemorySize() { if (isExternalStorageAvailable()) { File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getPath()); int blockSize = stat.getBlockSize(); int availableBlocks = stat.getAvailableBlocks(); return availableBlocks * blockSize; } else { return 0; } } /** * @return Total external memory */ public static int getTotalExternalMemorySize() { if (isExternalStorageAvailable()) { File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getPath()); int blockSize = stat.getBlockSize(); int totalBlocks = stat.getBlockCount(); return totalBlocks * blockSize; } else { return 0; } } /** * Get ideal cache directory based on available * * @return Ideal file location for caching */ public static File getIdealCacheDirectory(Context context) { File externalCacheDir = context.getExternalCacheDir(); if (getTotalExternalMemorySize() < getTotalInternalMemorySize() || externalCacheDir == null) { return context.getCacheDir(); } return externalCacheDir; } /** * Format size in string form * * @param size Size in bytes * @return Size in stinrg format with suffix */ public static String formatSize(int size) { String suffix = null; if (size >= 1024) { suffix = "KB"; size /= 1024; if (size >= 1024) { suffix = "MB"; size /= 1024; } } StringBuilder resultBuffer = new StringBuilder(Long.toString(size)); int commaOffset = resultBuffer.length() - 3; while (commaOffset > 0) { resultBuffer.insert(commaOffset, ','); commaOffset -= 3; } if (suffix != null) resultBuffer.append(suffix); return resultBuffer.toString(); } }