diff --git a/app/build.gradle b/app/build.gradle index 677a090..f33cafc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,7 +2,7 @@ apply plugin: 'com.android.application' android { compileSdkVersion 25 - buildToolsVersion "25.0.2" + buildToolsVersion "25.0.3" defaultConfig { applicationId "com.tonyodev.storagespace" minSdkVersion 16 diff --git a/app/src/main/res/drawable/screenshot.png b/app/src/main/res/drawable/screenshot.png new file mode 100644 index 0000000..5c754a2 Binary files /dev/null and b/app/src/main/res/drawable/screenshot.png differ diff --git a/storagegrapher/build.gradle b/storagegrapher/build.gradle index c5e2c2c..3e8b1e0 100644 --- a/storagegrapher/build.gradle +++ b/storagegrapher/build.gradle @@ -2,13 +2,13 @@ apply plugin: 'com.android.library' android { compileSdkVersion 25 - buildToolsVersion "25.0.2" + buildToolsVersion "25.0.3" defaultConfig { minSdkVersion 16 targetSdkVersion 25 - versionCode 3 - versionName "1.2" + versionCode 4 + versionName "1.3" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" diff --git a/storagegrapher/src/main/java/com/tonyodev/storagegrapher/Storage.java b/storagegrapher/src/main/java/com/tonyodev/storagegrapher/Storage.java index e7d78e4..de51980 100644 --- a/storagegrapher/src/main/java/com/tonyodev/storagegrapher/Storage.java +++ b/storagegrapher/src/main/java/com/tonyodev/storagegrapher/Storage.java @@ -11,9 +11,11 @@ import android.text.format.Formatter; import java.io.File; +import java.io.IOException; import java.util.Arrays; import java.util.LinkedList; import java.util.List; +import java.util.Queue; /** * Created by tonyofrancis on 4/21/17. @@ -24,33 +26,6 @@ public final class Storage { - /** - * Converts bytes to Gigabytes and returns a formatted string - * @param bytes bytes - *@return formatted string with conversion - */ - public static String bytesToGigabytesString(long bytes) { - return bytesToGigabytes(bytes) + " GB"; - } - - /** - * Converts bytes to Megabytes and returns a formatted string - * @param bytes bytes - *@return formatted string with conversion - */ - public static String bytesToMegabytesString(long bytes) { - return bytesToMegabytes(bytes) + " MB"; - } - - /** - * Converts bytes to Kilobytes and returns a formatted string - * @param bytes bytes - *@return formatted string with conversion - */ - public static String bytesToKilobytesString(long bytes) { - return bytesToKilobytes(bytes) + " KB"; - } - /** * Formats a content size to be in the form of bytes, kilobytes, megabytes, etc. * @param context context @@ -66,18 +41,6 @@ public static String getFormattedStorageAmount(Context context, long bytes) { return Formatter.formatFileSize(context,bytes); } - public static double bytesToGigabytes(long bytes) { - return (double)bytes / (double)1073741824; - } - - public static double bytesToMegabytes(long bytes) { - return (double)bytes / (double)1048576; - } - - public static double bytesToKilobytes(long bytes) { - return (double)bytes / (double)1024; - } - /** * @param volume Storage Volume * @return available bytes on a storage volume @@ -280,7 +243,7 @@ private static boolean isNewSdNameFormat(String name) { int c = (int) name.charAt(i); - if(c == (int)'-') { + if(c == (int)'-' && i == 4) { continue; } @@ -292,22 +255,6 @@ private static boolean isNewSdNameFormat(String name) { return true; } - /** - * @return File - Internal Storage Directory - * */ - @Nullable - public static File getInternalStorageDir() { - - File internalStorage = Environment.getExternalStorageDirectory(); - - if(internalStorage != null && Environment.isExternalStorageEmulated() - && !Environment.isExternalStorageRemovable()) { - return internalStorage; - } - - return getPrimaryStorageDir(); - } - /** * @return File - Application directory. Note: An application * can be install on an external Storage Volume. @@ -388,7 +335,7 @@ public static long getSecondaryAppFilesDirBytes(Context context) { File file = getSecondaryAppFilesDir(context); - if(file == null) { + if(file == null || !file.exists()) { return 0; } @@ -405,9 +352,9 @@ public static long getPrimaryAppFilesDirBytes(Context context) { throw new NullPointerException("Context cannot be null"); } - File file = getPrimaryStorageDir(); + File file = getPrimaryAppFilesDir(context); - if(file == null) { + if(file == null || !file.exists()) { return 0; } @@ -458,21 +405,6 @@ public static StorageVolume getStorageVolume(String path) { usedPercentage,freePercentage); } - /** - * @return Storage Volume for internal storage directory - * */ - @Nullable - public static StorageVolume getInternalStorageVolume() { - - File file = getInternalStorageDir(); - - if(file == null) { - return null; - } - - return getStorageVolume(file); - } - /** * @return Storage Volume for primary storage directory * */ @@ -496,7 +428,7 @@ public static StorageVolume getPrimaryStorageVolume() { public static StorageVolume getSecondaryStorageVolume(Context context) { if(context == null) { - return null; + throw new NullPointerException("Context cannot be null"); } File file = getSecondaryStorageDir(context); @@ -512,19 +444,13 @@ public static StorageVolume getSecondaryStorageVolume(Context context) { * @param context context * @return the size of the application directory in bytes * */ - public static long getAppDirBytes(Context context ){ + public static long getAppDirBytes(Context context){ if(context == null) { throw new NullPointerException("Context cannot be null"); } - File file = getAppDir(context); - - if(file == null) { - return 0; - } - - return getDirectorySize(file); + return getDirectorySize(getAppDir(context)); } /** @@ -535,12 +461,8 @@ public static long getAppDirBytes(Context context ){ * */ public static long getDirectorySize(File file) { - if(file == null) { - throw new NullPointerException("File cannot be null"); - } - - LinkedList queue = new LinkedList<>(); long size = 0; + Queue queue = new LinkedList<>(); queue.add(file); @@ -548,20 +470,27 @@ public static long getDirectorySize(File file) { File f = queue.remove(); - if(f != null) { + if(f != null && file.exists()) { size += f.length(); - if(file.isDirectory()) { + try { + + if(f.isDirectory() && f.getAbsolutePath().equals(f.getCanonicalPath())) { - File[] subFiles = f.listFiles(); + File[] subFiles = f.listFiles(); - if(subFiles != null) { - queue.addAll(Arrays.asList(subFiles)); + if(subFiles != null) { + queue.addAll(Arrays.asList(subFiles)); + } } + + }catch (IOException e ){ + e.printStackTrace(); } } } + return size; } }