Skip to content

Commit 4aec11e

Browse files
committed
see 08/11 log
1 parent ea43b05 commit 4aec11e

File tree

13 files changed

+429
-137
lines changed

13 files changed

+429
-137
lines changed

md/update_log.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
### 更新Log
2+
#### 16/08/11 新增SDCardUtils,UnitUtils,单元测试慢慢完善中
23
#### 16/08/09 目录排版更新,新增Download,Proguard和License。
34
#### 16/08/08 新增Shell工具类,已传jcenter()遇到好多坑,javaDoc惹的祸,注释一定要规范
45
#### 16/08/07 新增6.0获取Mac地址方法,新增对HTML转义,新增编码解码工具类,新增SP工具类

utilcode/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ android {
2424
dependencies {
2525
testCompile 'junit:junit:4.12'
2626
testCompile 'org.robolectric:robolectric:3.1.2'
27+
testCompile 'com.google.truth:truth:0.29'
2728
}
2829
apply from: "https://raw.githubusercontent.com/xiaopansky/android-library-publish-to-jcenter/master/bintrayUpload.gradle"

utilcode/src/main/java/com/blankj/utilcode/utils/DeviceUtils.java

-19
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,4 @@ public static String getModel() {
8383
}
8484
return model;
8585
}
86-
87-
/**
88-
* 获取设备SD卡是否可用
89-
*
90-
* @return true : 可用<br>false : 不可用
91-
*/
92-
public static boolean isSDCardEnable() {
93-
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
94-
}
95-
96-
/**
97-
* 获取设备SD卡路径
98-
* <p>一般是/storage/emulated/0/</p>
99-
*
100-
* @return SD卡路径
101-
*/
102-
public static String getSDCardPath() {
103-
return Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
104-
}
10586
}

utilcode/src/main/java/com/blankj/utilcode/utils/EncodeUtils.java

+17-16
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,25 @@ private EncodeUtils() {
1919
}
2020

2121
/**
22-
* 以UTF-8编码字符串
23-
* <p>若想自己指定字符集,可以使用encode(String string, String charset)方法</p>
22+
* URL编码
23+
* <p>若想自己指定字符集,可以使用{@link #urlEncode(String string, String charset)}方法</p>
2424
*
2525
* @param string 要编码的字符
2626
* @return 编码为UTF-8的字符串
2727
*/
28-
public static String encodeUTF8(String string) {
29-
return encode(string, "UTF-8");
28+
public static String urlEncode(String string) {
29+
return urlEncode(string, "UTF-8");
3030
}
3131

3232
/**
33-
* 字符编码
33+
* URL编码
3434
* <p>若系统不支持指定的编码字符集,则直接将string原样返回</p>
3535
*
3636
* @param string 要编码的字符
3737
* @param charset 字符集
3838
* @return 编码为字符集的字符串
3939
*/
40-
public static String encode(String string, String charset) {
40+
public static String urlEncode(String string, String charset) {
4141
try {
4242
return URLEncoder.encode(string, charset);
4343
} catch (UnsupportedEncodingException e) {
@@ -46,29 +46,30 @@ public static String encode(String string, String charset) {
4646
}
4747

4848
/**
49-
* 以UTF-8解码字符串
50-
* <p>若想自己指定字符集,可以使用# {decode(String string, String charset)}方法</p>
49+
* URL解码
50+
* <p>若想自己指定字符集,可以使用 {@link #urlDecode(String string, String charset)}方法</p>
5151
*
52-
* @param string 要解码的字符
53-
* @return 解码为UTF-8的字符串
52+
* @param string 要解码的字符串
53+
* @return URL解码后的字符串
5454
*/
55-
public static String decodeUTF8(String string) {
56-
return decode(string, "UTF-8");
55+
public static String urlDecode(String string) {
56+
return urlDecode(string, "UTF-8");
5757
}
5858

5959
/**
60-
* 字符解码
60+
* URL解码
6161
* <p>若系统不支持指定的解码字符集,则直接将string原样返回</p>
6262
*
63-
* @param string 要解码的字符
63+
* @param string 要解码的字符串
6464
* @param charset 字符集
65-
* @return 解码为字符集的字符串
65+
* @return URL解码为指定字符集的字符串
6666
*/
67-
public static String decode(String string, String charset) {
67+
public static String urlDecode(String string, String charset) {
6868
try {
6969
return URLDecoder.decode(string, charset);
7070
} catch (UnsupportedEncodingException e) {
7171
return string;
7272
}
7373
}
74+
7475
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.blankj.utilcode.utils;
2+
3+
/**
4+
* <pre>
5+
* author: Blankj
6+
* blog : http://blankj.com
7+
* time : 2016/8/11
8+
* desc : 文件相关的工具类
9+
* </pre>
10+
*/
11+
public class FileUtils {
12+
13+
private FileUtils() {
14+
throw new UnsupportedOperationException("u can't fuck me...");
15+
}
16+
17+
18+
}

utilcode/src/main/java/com/blankj/utilcode/utils/RegularUtils.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@ private RegularUtils() {
2424
private static final String REGEX_MOBILE_SIMPLE = "^[1]\\d{10}$";
2525
/**
2626
* 验证手机号(精确)
27-
* <p/>
28-
* <p>移动:134(0-8)、135、136、137、138、139、147、150、151、152、157、158、159、178、182、183、184、187、188
29-
* <p>联通:130、131、132、145、155、156、175、176、185、186
30-
* <p>电信:133、153、173、177、180、181、189
31-
* <p>全球星:1349
32-
* <p>虚拟运营商:170
27+
* <p>移动:134(0-8)、135、136、137、138、139、147、150、151、152、157、158、159、178、182、183、184、187、188</p>
28+
* <p>联通:130、131、132、145、155、156、175、176、185、186</p>
29+
* <p>电信:133、153、173、177、180、181、189</p>
30+
* <p>全球星:1349</p>
31+
* <p>虚拟运营商:170</p>
3332
*/
3433
private static final String REGEX_MOBILE_EXACT = "^((13[0-9])|(14[5,7])|(15[0-3,5-8])|(17[0,3,5-8])|(18[0-9])|(147))\\d{8}$";
3534
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
package com.blankj.utilcode.utils;
2+
3+
import android.os.Environment;
4+
import android.os.StatFs;
5+
6+
import java.io.File;
7+
import java.io.FileInputStream;
8+
import java.io.FileOutputStream;
9+
10+
/**
11+
* <pre>
12+
* author: Blankj
13+
* blog : http://blankj.com
14+
* time : 2016/8/11
15+
* desc : SD卡相关的工具类
16+
* </pre>
17+
*/
18+
public class SDCardUtils {
19+
20+
private SDCardUtils() {
21+
throw new UnsupportedOperationException("u can't fuck me...");
22+
}
23+
24+
/**
25+
* 获取设备SD卡是否可用
26+
*
27+
* @return true : 可用<br>false : 不可用
28+
*/
29+
public static boolean isSDCardEnable() {
30+
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
31+
}
32+
33+
/**
34+
* 获取设备SD卡路径
35+
* <p>一般是/storage/emulated/0/</p>
36+
*
37+
* @return SD卡路径
38+
*/
39+
public static String getSDCardPath() {
40+
return Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
41+
}
42+
43+
/**
44+
* 获取SD卡的剩余容量 单位byte
45+
*
46+
* @return
47+
*/
48+
public static long getSDCardAllSize() {
49+
if (isSDCardEnable()) {
50+
StatFs stat = new StatFs(getSDCardPath());
51+
// 获取空闲的数据块的数量
52+
long availableBlocks = (long) stat.getAvailableBlocks() - 4;
53+
// 获取单个数据块的大小(byte)
54+
long freeBlocks = stat.getAvailableBlocks();
55+
return freeBlocks * availableBlocks;
56+
}
57+
return 0;
58+
}
59+
60+
/**
61+
* 获取指定路径所在空间的剩余可用容量字节数,单位byte
62+
*
63+
* @param filePath
64+
* @return 容量字节 SDCard可用空间,内部存储可用空间
65+
*/
66+
public static long getFreeBytes(String filePath) {
67+
// 如果是sd卡的下的路径,则获取sd卡可用容量
68+
if (filePath.startsWith(getSDCardPath())) {
69+
filePath = getSDCardPath();
70+
} else {// 如果是内部存储的路径,则获取内存存储的可用容量
71+
filePath = Environment.getDataDirectory().getAbsolutePath();
72+
}
73+
StatFs stat = new StatFs(filePath);
74+
long availableBlocks = (long) stat.getAvailableBlocks() - 4;
75+
return stat.getBlockSize() * availableBlocks;
76+
}
77+
78+
/**
79+
* 获取系统存储路径
80+
*
81+
* @return
82+
*/
83+
public static String getRootDirectoryPath() {
84+
return Environment.getRootDirectory().getAbsolutePath();
85+
}
86+
87+
/**
88+
* Check if the file is exists
89+
*
90+
* @param filePath
91+
* @param fileName
92+
* @return
93+
*/
94+
public static boolean isFileExistsInSDCard(String filePath, String fileName) {
95+
boolean flag = false;
96+
if (isSDCardEnable()) {
97+
File file = new File(filePath, fileName);
98+
if (file.exists()) {
99+
flag = true;
100+
}
101+
}
102+
return flag;
103+
}
104+
105+
/**
106+
* Write file to SD card
107+
* @param filePath
108+
* @param filename
109+
* @param content
110+
* @return
111+
* @throws Exception
112+
*/
113+
public static boolean saveFileToSDCard(String filePath, String filename, String content)
114+
throws Exception {
115+
boolean flag = false;
116+
if (isSDCardEnable()) {
117+
File dir = new File(filePath);
118+
if (!dir.exists()) {
119+
dir.mkdir();
120+
}
121+
File file = new File(filePath, filename);
122+
FileOutputStream outStream = new FileOutputStream(file);
123+
outStream.write(content.getBytes());
124+
outStream.close();
125+
flag = true;
126+
}
127+
return flag;
128+
}
129+
130+
/**
131+
* Read file as stream from SD card
132+
*
133+
* @param fileName
134+
* String PATH =
135+
* Environment.getExternalStorageDirectory().getAbsolutePath() +
136+
* "/dirName";
137+
* @return
138+
*/
139+
public static byte[] readFileFromSDCard(String filePath, String fileName) {
140+
byte[] buffer = null;
141+
try {
142+
if (isSDCardEnable()) {
143+
String filePaht = filePath + "/" + fileName;
144+
FileInputStream fin = new FileInputStream(filePaht);
145+
int length = fin.available();
146+
buffer = new byte[length];
147+
fin.read(buffer);
148+
fin.close();
149+
}
150+
} catch (Exception e) {
151+
e.printStackTrace();
152+
}
153+
return buffer;
154+
}
155+
156+
/**
157+
* Delete file
158+
*
159+
* @param filePath
160+
* @param fileName
161+
* filePath =
162+
* android.os.Environment.getExternalStorageDirectory().getPath()
163+
* @return
164+
*/
165+
public static boolean deleteSDFile(String filePath, String fileName) {
166+
File file = new File(filePath + "/" + fileName);
167+
return !(!file.exists() || file.isDirectory()) && file.delete();
168+
}
169+
}

utilcode/src/main/java/com/blankj/utilcode/utils/SPUtils.java

+2
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,6 @@ public static boolean getBoolean(Context context, String key, boolean defaultVal
207207
private static SharedPreferences getSP(Context context) {
208208
return context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
209209
}
210+
211+
210212
}

0 commit comments

Comments
 (0)