1 |
/**
|
2 |
* ZANavi, Zoff Android Navigation system.
|
3 |
* Copyright (C) 2011 Zoff <zoff@zoff.cc>
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* version 2 as published by the Free Software Foundation.
|
8 |
*
|
9 |
* This program is distributed in the hope that it will be useful,
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12 |
* GNU General Public License for more details.
|
13 |
*
|
14 |
* You should have received a copy of the GNU General Public License
|
15 |
* along with this program; if not, write to the
|
16 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
17 |
* Boston, MA 02110-1301, USA.
|
18 |
*/
|
19 |
|
20 |
package com.zoffcc.applications.zanavi;
|
21 |
|
22 |
import android.os.StatFs;
|
23 |
|
24 |
/**
|
25 |
* This class is designed to get available space in external storage of android.
|
26 |
* It contains methods which provide you the available space in different units e.g
|
27 |
* bytes, KB, MB, GB. OR you can get the number of available blocks on external storage.
|
28 |
*
|
29 |
*/
|
30 |
public class NavitAvailableSpaceHandler
|
31 |
{
|
32 |
//********
|
33 |
// Variables
|
34 |
/**
|
35 |
* Number of bytes in one KB = 2<sup>10</sup>
|
36 |
*/
|
37 |
public final static long SIZE_KB = 1024L;
|
38 |
|
39 |
/**
|
40 |
* Number of bytes in one MB = 2<sup>20</sup>
|
41 |
*/
|
42 |
public final static long SIZE_MB = SIZE_KB * SIZE_KB;
|
43 |
|
44 |
/**
|
45 |
* Number of bytes in one GB = 2<sup>30</sup>
|
46 |
*/
|
47 |
public final static long SIZE_GB = SIZE_KB * SIZE_KB * SIZE_KB;
|
48 |
|
49 |
//********
|
50 |
// Methods
|
51 |
|
52 |
/**
|
53 |
* @return Number of bytes available on specific dir
|
54 |
*/
|
55 |
public static long getExternalAvailableSpaceInBytes(String directory)
|
56 |
{
|
57 |
long availableSpace = -1L;
|
58 |
try
|
59 |
{
|
60 |
StatFs stat = new StatFs(directory);
|
61 |
availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();
|
62 |
}
|
63 |
catch (Exception e)
|
64 |
{
|
65 |
e.printStackTrace();
|
66 |
}
|
67 |
|
68 |
return availableSpace;
|
69 |
}
|
70 |
|
71 |
/**
|
72 |
* @return Number of kilo bytes available on external storage
|
73 |
*/
|
74 |
public static long getExternalAvailableSpaceInKB(String directory)
|
75 |
{
|
76 |
return getExternalAvailableSpaceInBytes(directory) / SIZE_KB;
|
77 |
}
|
78 |
|
79 |
/**
|
80 |
* @return Number of Mega bytes available on external storage
|
81 |
*/
|
82 |
public static long getExternalAvailableSpaceInMB(String directory)
|
83 |
{
|
84 |
return getExternalAvailableSpaceInBytes(directory) / SIZE_MB;
|
85 |
}
|
86 |
|
87 |
/**
|
88 |
* @return gega bytes of bytes available on external storage
|
89 |
*/
|
90 |
public static long getExternalAvailableSpaceInGB(String directory)
|
91 |
{
|
92 |
return getExternalAvailableSpaceInBytes(directory) / SIZE_GB;
|
93 |
}
|
94 |
|
95 |
/**
|
96 |
* @return Total number of available blocks on external storage
|
97 |
*/
|
98 |
public static long getExternalStorageAvailableBlocks(String directory)
|
99 |
{
|
100 |
long availableBlocks = -1L;
|
101 |
try
|
102 |
{
|
103 |
StatFs stat = new StatFs(directory);
|
104 |
availableBlocks = stat.getAvailableBlocks();
|
105 |
}
|
106 |
catch (Exception e)
|
107 |
{
|
108 |
e.printStackTrace();
|
109 |
}
|
110 |
|
111 |
return availableBlocks;
|
112 |
}
|
113 |
}
|