Skip to content

Commit

Permalink
Bug Fixes and Cleanup
Browse files Browse the repository at this point in the history
Version 1.3
  • Loading branch information
tonyofrancis committed Apr 27, 2017
1 parent f291386 commit 125c90e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 97 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file added app/src/main/res/drawable/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions storagegrapher/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
115 changes: 22 additions & 93 deletions storagegrapher/src/main/java/com/tonyodev/storagegrapher/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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;
}

Expand All @@ -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.
Expand Down Expand Up @@ -388,7 +335,7 @@ public static long getSecondaryAppFilesDirBytes(Context context) {

File file = getSecondaryAppFilesDir(context);

if(file == null) {
if(file == null || !file.exists()) {
return 0;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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
* */
Expand All @@ -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);
Expand All @@ -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));
}

/**
Expand All @@ -535,33 +461,36 @@ public static long getAppDirBytes(Context context ){
* */
public static long getDirectorySize(File file) {

if(file == null) {
throw new NullPointerException("File cannot be null");
}

LinkedList<File> queue = new LinkedList<>();
long size = 0;
Queue<File> queue = new LinkedList<>();

queue.add(file);

while (!queue.isEmpty()) {

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;
}
}

0 comments on commit 125c90e

Please sign in to comment.