├── .gitignore
├── README.md
├── YHUtils
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── xyz
│ │ └── yhsj
│ │ └── yhutils
│ │ ├── form
│ │ └── FormUtils.java
│ │ ├── io
│ │ ├── AssetUtils.java
│ │ ├── FileUtil.java
│ │ ├── IOUtils.java
│ │ ├── ImageUtils.java
│ │ ├── ResouceUtil.java
│ │ └── SDCardUtils.java
│ │ ├── keyboard
│ │ └── KeyBoardUtils.java
│ │ ├── logutils
│ │ ├── LogType.java
│ │ ├── LogUtils.java
│ │ ├── Logger.java
│ │ ├── Printer.java
│ │ └── utils
│ │ │ ├── ArrayUtil.java
│ │ │ └── SystemUtil.java
│ │ ├── notification
│ │ └── NotificationUtils.java
│ │ ├── phone
│ │ ├── AppDataUtils.java
│ │ ├── AppUtils.java
│ │ ├── CpuUtils.java
│ │ ├── DeviceUtils.java
│ │ ├── LocationUtils.java
│ │ ├── NetWorkUtils.java
│ │ ├── NetWorkUtilsBroadcast.java
│ │ ├── NetWorkUtils_Broadcast.java
│ │ ├── ScreenUtils.java
│ │ └── ShellUtils.java
│ │ ├── secarity
│ │ ├── AESUtils.java
│ │ ├── Base64Utils.java
│ │ ├── DES3Utils.java
│ │ ├── DESUtils.java
│ │ ├── HexUtils.java
│ │ ├── MD5Utils.java
│ │ └── RSAUtils.java
│ │ ├── sp
│ │ └── SharePreferenceUtil.java
│ │ ├── string
│ │ ├── DateUtils.java
│ │ ├── DensityUtils.java
│ │ └── StringUtils.java
│ │ └── tools
│ │ └── ClassUtils.java
│ └── res
│ └── values
│ └── strings.xml
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── xyz
│ │ └── yhsj
│ │ └── yhutilsDemo
│ │ └── MainActivity.java
│ └── res
│ ├── layout
│ ├── activity_main.xml
│ └── content_main.xml
│ ├── menu
│ └── menu_main.xml
│ ├── mipmap-hdpi
│ └── ic_launcher.png
│ ├── mipmap-mdpi
│ └── ic_launcher.png
│ ├── mipmap-xhdpi
│ └── ic_launcher.png
│ ├── mipmap-xxhdpi
│ └── ic_launcher.png
│ ├── mipmap-xxxhdpi
│ └── ic_launcher.png
│ ├── values-v21
│ └── styles.xml
│ ├── values-w820dp
│ └── dimens.xml
│ └── values
│ ├── colors.xml
│ ├── dimens.xml
│ ├── strings.xml
│ └── styles.xml
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | ### Android template
2 | # Built application files
3 | *.apk
4 | *.ap_
5 |
6 | # Files for the Dalvik VM
7 | *.dex
8 |
9 | # Java class files
10 | *.class
11 |
12 | # Generated files
13 | bin/
14 | gen/
15 |
16 | # Gradle files
17 | .gradle/
18 | build/
19 |
20 | # Local configuration file (sdk path, etc)
21 | local.properties
22 |
23 | # Proguard folder generated by Eclipse
24 | proguard/
25 |
26 | # Log Files
27 | *.log
28 |
29 | # Android Studio Navigation editor temp files
30 | .navigation/
31 | ### Example user template template
32 | ### Example user template
33 |
34 | # IntelliJ project files
35 | .idea
36 | *.iml
37 | out
38 | gen
39 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # YHUtls
2 | 常用工具库,包含一些实用工具
3 |
4 |
5 |
--------------------------------------------------------------------------------
/YHUtils/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/YHUtils/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 23
5 | buildToolsVersion "23.0.2"
6 |
7 | defaultConfig {
8 | minSdkVersion 14
9 | targetSdkVersion 23
10 | versionCode 1
11 | versionName "1.0"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | compile fileTree(dir: 'libs', include: ['*.jar'])
23 | compile 'com.android.support:appcompat-v7:23.1.1'
24 | }
25 |
--------------------------------------------------------------------------------
/YHUtils/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in D:\Develop\android_SDK/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/YHUtils/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/YHUtils/src/main/java/xyz/yhsj/yhutils/form/FormUtils.java:
--------------------------------------------------------------------------------
1 | package xyz.yhsj.yhutils.form;
2 |
3 | import org.json.JSONArray;
4 | import org.json.JSONObject;
5 |
6 | import android.annotation.SuppressLint;
7 | import android.inputmethodservice.ExtractEditText;
8 | import android.view.View;
9 | import android.view.ViewGroup;
10 | import android.widget.AbsoluteLayout;
11 | import android.widget.AutoCompleteTextView;
12 | import android.widget.CheckBox;
13 | import android.widget.EditText;
14 | import android.widget.FrameLayout;
15 | import android.widget.LinearLayout;
16 | import android.widget.MultiAutoCompleteTextView;
17 | import android.widget.RadioButton;
18 | import android.widget.RadioGroup;
19 | import android.widget.RelativeLayout;
20 | import android.widget.TableLayout;
21 |
22 | /**
23 | * 表单工具类,获得数据或者设置数据
24 | *
25 | * @author 修改 by 永恒瞬间(power by zftlive)
26 | * @version 1.0
27 | */
28 | @SuppressWarnings("deprecation")
29 | public class FormUtils {
30 | /**
31 | * 获取表单的数据(根据Tag获取控件:键-值,返回json格式)
32 | *
33 | * @param root
34 | * 当前表单容器
35 | * @param data
36 | * 当前表单数据
37 | * @return 表单数据(CheckBox多选选项JSONArray拼接)
38 | */
39 |
40 | @SuppressLint("NewApi")
41 | public static JSONObject getFormInfo(ViewGroup root, JSONObject data)
42 | throws Exception {
43 | if (root.getChildCount() > 0) {
44 | for (int i = 0; i < root.getChildCount(); i++) {
45 |
46 | View view = root.getChildAt(i);
47 | // 容器级别控件需要进行递归
48 | if (view instanceof LinearLayout) {
49 | getFormInfo((LinearLayout) view, data);
50 | } else if (view instanceof RelativeLayout) {
51 | getFormInfo((RelativeLayout) view, data);
52 | } else if (view instanceof FrameLayout) {
53 | getFormInfo((FrameLayout) view, data);
54 | } else if (view instanceof AbsoluteLayout) {
55 | getFormInfo((AbsoluteLayout) view, data);
56 | } else if (view instanceof RadioGroup) {
57 | getFormInfo((RadioGroup) view, data);
58 | } else if (view instanceof TableLayout) {
59 | getFormInfo((TableLayout) view, data);
60 | }
61 |
62 | // 非容器级别控件不用递归
63 | /**
64 | * EditText.class
65 | */
66 | else if (view instanceof EditText) {
67 |
68 | if (view.getTag() != null) {
69 | data.put((String) view.getTag(), ((EditText) view)
70 | .getText().toString());
71 | }
72 |
73 | } else if (view instanceof AutoCompleteTextView) {
74 |
75 | if (view.getTag() != null) {
76 | data.put((String) view.getTag(),
77 | ((AutoCompleteTextView) view).getText()
78 | .toString());
79 | }
80 |
81 | } else if (view instanceof MultiAutoCompleteTextView) {
82 |
83 | if (view.getTag() != null) {
84 | data.put((String) view.getTag(),
85 | ((MultiAutoCompleteTextView) view).getText()
86 | .toString());
87 | }
88 |
89 | } else if (view instanceof ExtractEditText) {
90 |
91 | if (view.getTag() != null) {
92 | data.put((String) view.getTag(),
93 | ((ExtractEditText) view).getText().toString());
94 | }
95 | }
96 |
97 | /**
98 | * RadioButton.class
99 | */
100 | else if (view.getClass().getName()
101 | .equals(RadioButton.class.getName())) {
102 |
103 | if (view.getTag() != null) {
104 | if (((RadioButton) view).isChecked()) {
105 | data.put((String) view.getTag(),
106 | ((RadioButton) view).getText().toString());
107 | }
108 | }
109 | }
110 |
111 | /**
112 | * CheckBox.class(需要拼装选中复选框)
113 | */
114 | else if (view.getClass().getName()
115 | .equals(CheckBox.class.getName())) {
116 |
117 | if (view.getTag() != null) {
118 |
119 | if (((CheckBox) view).isChecked()) {
120 |
121 | if (!data.isNull((String) view.getTag())) {
122 |
123 | JSONArray value = data.getJSONArray(view
124 | .getTag().toString());
125 | value.put(((CheckBox) view).getText()
126 | .toString());
127 | data.put((String) view.getTag(), value);
128 | } else {
129 | JSONArray arr = new JSONArray();
130 | arr.put(((CheckBox) view).getText().toString());
131 | data.put((String) view.getTag(), arr);
132 |
133 | }
134 | }
135 | }
136 |
137 | }
138 | /**
139 | * Spinner.class
140 | */
141 | else if (view.getClass().getName()
142 | .equals(android.widget.Spinner.class.getName())) {
143 |
144 | if (view.getTag() != null) {
145 | data.put((String) view.getTag(),
146 | ((android.widget.Spinner) view)
147 | .getSelectedItem().toString());
148 | }
149 | }
150 | }
151 | }
152 |
153 | return data;
154 | }
155 |
156 |
157 |
158 | }
159 |
--------------------------------------------------------------------------------
/YHUtils/src/main/java/xyz/yhsj/yhutils/io/AssetUtils.java:
--------------------------------------------------------------------------------
1 | package xyz.yhsj.yhutils.io;
2 |
3 | import java.io.IOException;
4 | import java.io.InputStream;
5 |
6 | import android.content.Context;
7 | import android.content.res.AssetManager;
8 | import android.graphics.drawable.Drawable;
9 |
10 | /**
11 | * Asset 工具类
12 | */
13 | @SuppressWarnings("deprecation")
14 | public class AssetUtils {
15 |
16 | /**
17 | * 打开Asset下的文件
18 | *
19 | * @param fileName 文件名
20 | * @return
21 | */
22 | public static InputStream openAssetFile(Context context, String fileName) {
23 | AssetManager am = context.getAssets();
24 | InputStream is = null;
25 | try {
26 | is = am.open(fileName);
27 | } catch (IOException e) {
28 | e.printStackTrace();
29 | }
30 | return is;
31 | }
32 |
33 | /**
34 | * 从assets 文件夹中读取文本数据
35 | */
36 | public static String getTextFromAssets(final Context context,
37 | String fileName) {
38 | String result = "";
39 | try {
40 | InputStream in = context.getResources().getAssets().open(fileName);
41 | // 获取文件的字节数
42 | int lenght = in.available();
43 | // 创建byte数组
44 | byte[] buffer = new byte[lenght];
45 | // 将文件中的数据读到byte数组中
46 | in.read(buffer);
47 | result = new String(buffer, "UTF-8");
48 | in.close();
49 | } catch (Exception e) {
50 | e.printStackTrace();
51 | }
52 | return result;
53 | }
54 |
55 | /**
56 | * 从assets 文件夹中读取图片
57 | */
58 | public static Drawable loadImageFromAsserts(final Context ctx,
59 | String fileName) {
60 | try {
61 | InputStream is = ctx.getResources().getAssets().open(fileName);
62 | return Drawable.createFromStream(is, null);
63 | } catch (IOException e) {
64 | if (e != null) {
65 | e.printStackTrace();
66 | }
67 | } catch (OutOfMemoryError e) {
68 | if (e != null) {
69 | e.printStackTrace();
70 | }
71 | } catch (Exception e) {
72 | if (e != null) {
73 | e.printStackTrace();
74 | }
75 | }
76 | return null;
77 | }
78 |
79 | }
80 |
--------------------------------------------------------------------------------
/YHUtils/src/main/java/xyz/yhsj/yhutils/io/FileUtil.java:
--------------------------------------------------------------------------------
1 | package xyz.yhsj.yhutils.io;
2 |
3 | import java.text.DecimalFormat;
4 |
5 | /**
6 | * Created by wangkuan on 15/10/13.
7 | */
8 | public class FileUtil {
9 |
10 | /**
11 | * 获取文件的大小
12 | *
13 | * @param fileS
14 | * @return
15 | */
16 | public static String FormetFileSize(long fileS) {//转换文件大小
17 | DecimalFormat df = new DecimalFormat("#.00");
18 | String fileSizeString = "0K";
19 | if (fileS < 1024) {
20 | fileSizeString = df.format((double) fileS) + "B";
21 | } else if (fileS < 1048576) {
22 | fileSizeString = df.format((double) fileS / 1024) + "K";
23 | } else if (fileS < 1073741824) {
24 | fileSizeString = df.format((double) fileS / 1048576) + "M";
25 | } else {
26 | fileSizeString = df.format((double) fileS / 1073741824) + "G";
27 | }
28 | return fileSizeString;
29 | }
30 |
31 | public static String FormatFileSizeByMB(long fileS) {//转换文件大小
32 |
33 |
34 | return String.format("%.2fM", (double) fileS / 1048576);
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/YHUtils/src/main/java/xyz/yhsj/yhutils/io/IOUtils.java:
--------------------------------------------------------------------------------
1 | package xyz.yhsj.yhutils.io;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.BufferedWriter;
5 | import java.io.ByteArrayOutputStream;
6 | import java.io.FileInputStream;
7 | import java.io.FileNotFoundException;
8 | import java.io.FileOutputStream;
9 | import java.io.IOException;
10 | import java.io.InputStream;
11 | import java.io.InputStreamReader;
12 | import java.io.OutputStreamWriter;
13 |
14 | import android.content.Context;
15 |
16 | /**
17 | * IO流 工具类
18 | * 很简单,仅支持文本操作
19 | *
20 | */
21 | public class IOUtils {
22 |
23 | /**
24 | * 文本的写入操作
25 | *
26 | * @param filePath
27 | * 文件路径。一定要加上文件名字
28 | * 例如:../a/a.txt
29 | * @param content
30 | * 写入内容
31 | * @param append
32 | * 是否追加
33 | */
34 | public static void write(String filePath, String content, boolean append) {
35 | BufferedWriter bufw = null;
36 | try {
37 | bufw = new BufferedWriter(new OutputStreamWriter(
38 | new FileOutputStream(filePath, append)));
39 | bufw.write(content);
40 |
41 | } catch (Exception e1) {
42 | e1.printStackTrace();
43 | } finally {
44 | if (bufw != null) {
45 | try {
46 | bufw.close();
47 | } catch (IOException e) {
48 | e.printStackTrace();
49 | }
50 | }
51 | }
52 | }
53 |
54 | /**
55 | * 文本的读取操作
56 | *
57 | * @param path
58 | * 文件路径,一定要加上文件名字
59 | * 例如:../a/a.txt
60 | * @return
61 | */
62 | public static String read(String path) {
63 | BufferedReader bufr = null;
64 | try {
65 | bufr = new BufferedReader(new InputStreamReader(
66 | new FileInputStream(path)));
67 | StringBuffer sb = new StringBuffer();
68 | String str = null;
69 | while ((str = bufr.readLine()) != null) {
70 | sb.append(str);
71 | }
72 | return sb.toString();
73 | } catch (Exception e) {
74 | e.printStackTrace();
75 | } finally {
76 | if (bufr != null) {
77 | try {
78 | bufr.close();
79 | } catch (IOException e) {
80 | e.printStackTrace();
81 | }
82 | }
83 | }
84 | return null;
85 | }
86 |
87 | /**
88 | * 文本的读取操作
89 | *
90 | * @param is 输入流
91 | * @return
92 | */
93 | public static String read(InputStream is) {
94 | BufferedReader bufr = null;
95 | try {
96 | bufr = new BufferedReader(new InputStreamReader(is));
97 | StringBuffer sb = new StringBuffer();
98 | String str = null;
99 | while ((str = bufr.readLine()) != null) {
100 | sb.append(str);
101 | }
102 | return sb.toString();
103 | } catch (Exception e) {
104 | e.printStackTrace();
105 | } finally {
106 | if (bufr != null) {
107 | try {
108 | bufr.close();
109 | } catch (IOException e) {
110 | e.printStackTrace();
111 | }
112 | }
113 | }
114 | return null;
115 | }
116 |
117 |
118 | /**
119 | * @param context 上下文
120 | * @param fileName 文件名
121 | * @return 字节数组
122 | */
123 | public static byte[] readBytes(Context context, String fileName) {
124 | FileInputStream fin = null;
125 | byte[] buffer = null;
126 | try {
127 | fin = context.openFileInput(fileName);
128 | int length = fin.available();
129 | buffer = new byte[length];
130 | fin.read(buffer);
131 | } catch (FileNotFoundException e) {
132 | e.printStackTrace();
133 | } catch (IOException e) {
134 | e.printStackTrace();
135 | } finally {
136 | try {
137 | if (fin != null) {
138 | fin.close();
139 | fin = null;
140 | }
141 | } catch (IOException e) {
142 | e.printStackTrace();
143 | }
144 | }
145 | return buffer;
146 | }
147 |
148 | /**
149 | * 快速读取程序应用包下的文件内容
150 | *
151 | * @param context
152 | * 上下文
153 | * @param filename
154 | * 文件名称
155 | * @return 文件内容
156 | * @throws IOException
157 | */
158 | public static String read(Context context, String filename)
159 | throws IOException {
160 | FileInputStream inStream = context.openFileInput(filename);
161 | ByteArrayOutputStream outStream = new ByteArrayOutputStream();
162 | byte[] buffer = new byte[1024];
163 | int len = 0;
164 | while ((len = inStream.read(buffer)) != -1) {
165 | outStream.write(buffer, 0, len);
166 | }
167 | byte[] data = outStream.toByteArray();
168 | return new String(data);
169 | }
170 |
171 |
172 | }
173 |
--------------------------------------------------------------------------------
/YHUtils/src/main/java/xyz/yhsj/yhutils/io/ImageUtils.java:
--------------------------------------------------------------------------------
1 | package xyz.yhsj.yhutils.io;
2 |
3 | import java.io.File;
4 | import java.io.FileOutputStream;
5 | import java.io.IOException;
6 |
7 | import android.content.Context;
8 | import android.graphics.Bitmap;
9 | import android.graphics.drawable.BitmapDrawable;
10 | import android.graphics.drawable.ColorDrawable;
11 | import android.graphics.drawable.Drawable;
12 | import android.graphics.drawable.TransitionDrawable;
13 | import android.support.annotation.DrawableRes;
14 | import android.widget.ImageView;
15 |
16 | public class ImageUtils {
17 |
18 | /**
19 | * 渐变显示图片
20 | *
21 | * @param context
22 | * @param imageView
23 | * @param bitmap
24 | */
25 | @SuppressWarnings("deprecation")
26 | public static void setImageBitmap(Context context, ImageView imageView,
27 | Bitmap bitmap) {
28 | // Use TransitionDrawable to fade in.
29 | final TransitionDrawable td = new TransitionDrawable(new Drawable[]{
30 | new ColorDrawable(context.getResources().getColor(android.R.color.transparent)),
31 | new BitmapDrawable(context.getResources(), bitmap)});
32 | // noinspection deprecation
33 | // imageView.setBackgroundDrawable(imageView.getDrawable());
34 | imageView.setImageDrawable(td);
35 | td.startTransition(200);
36 | }
37 |
38 | /**
39 | * 保存图片
40 | *
41 | * @param filePath 文件路径+文件名
42 | * @param bitmap 文件内容
43 | * @throws IOException
44 | */
45 | public static void saveAsJPEG(Bitmap bitmap, String filePath)
46 | throws IOException {
47 | FileOutputStream fos = null;
48 |
49 | try {
50 | File file = new File(filePath);
51 | if (!file.getParentFile().exists()) {
52 | file.getParentFile().mkdirs();
53 | }
54 | fos = new FileOutputStream(file);
55 | bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
56 | fos.flush();
57 | } finally {
58 | if (fos != null) {
59 | fos.close();
60 | }
61 | }
62 | }
63 |
64 | /**
65 | * 保存图片
66 | *
67 | * @param filePath 文件路径+文件名
68 | * @param bitmap 文件内容
69 | * @throws IOException
70 | */
71 | public static void saveAsPNG(Bitmap bitmap, String filePath)
72 | throws IOException {
73 | FileOutputStream fos = null;
74 |
75 | try {
76 | File file = new File(filePath);
77 | if (!file.getParentFile().exists()) {
78 | file.getParentFile().mkdirs();
79 | }
80 | fos = new FileOutputStream(file);
81 | bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
82 | fos.flush();
83 | } finally {
84 | if (fos != null) {
85 | fos.close();
86 | }
87 | }
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/YHUtils/src/main/java/xyz/yhsj/yhutils/io/ResouceUtil.java:
--------------------------------------------------------------------------------
1 | package xyz.yhsj.yhutils.io;
2 |
3 | import android.content.Context;
4 | import android.content.pm.ApplicationInfo;
5 | import android.content.pm.PackageManager;
6 | import android.content.res.AssetManager;
7 | import android.graphics.drawable.Drawable;
8 |
9 | import java.io.IOException;
10 | import java.io.InputStream;
11 |
12 | import xyz.yhsj.yhutils.string.StringUtils;
13 |
14 |
15 | /**
16 | * Asset 工具类
17 | */
18 | @SuppressWarnings("deprecation")
19 | public class ResouceUtil {
20 |
21 | /**
22 | * 打开Asset下的文件
23 | *
24 | * @param fileName 文件名
25 | * @return
26 | */
27 | public static InputStream openAssetFile(Context context, String fileName) {
28 | AssetManager am = context.getAssets();
29 | InputStream is = null;
30 | try {
31 | is = am.open(fileName);
32 | } catch (IOException e) {
33 | e.printStackTrace();
34 | }
35 | return is;
36 | }
37 |
38 | /**
39 | * 从assets 文件夹中读取文本数据
40 | */
41 | public static String getStringFromAssets(final Context context,
42 | String fileName) {
43 | String result = "";
44 | try {
45 | InputStream in = context.getResources().getAssets().open(fileName);
46 | // 获取文件的字节数
47 | int lenght = in.available();
48 | // 创建byte数组
49 | byte[] buffer = new byte[lenght];
50 | // 将文件中的数据读到byte数组中
51 | in.read(buffer);
52 | result = new String(buffer, "UTF-8");
53 | in.close();
54 | } catch (Exception e) {
55 | e.printStackTrace();
56 | }
57 | return result;
58 | }
59 |
60 | /**
61 | * 从assets 文件夹中读取图片
62 | */
63 | public static Drawable loadImageFromAsserts(final Context ctx,
64 | String fileName) {
65 | try {
66 | InputStream is = ctx.getResources().getAssets().open(fileName);
67 | return Drawable.createFromStream(is, null);
68 | } catch (IOException e) {
69 | if (e != null) {
70 | e.printStackTrace();
71 | }
72 | } catch (OutOfMemoryError e) {
73 | if (e != null) {
74 | e.printStackTrace();
75 | }
76 | } catch (Exception e) {
77 | if (e != null) {
78 | e.printStackTrace();
79 | }
80 | }
81 | return null;
82 | }
83 |
84 |
85 | /**
86 | * 从assert文件夹下读取文件到字节数组
87 | *
88 | * @param context 上下文
89 | * @param fileName 文件名
90 | * @return 文件字节数组
91 | */
92 | public static byte[] readBytesFromAssert(Context context, String fileName) {
93 | InputStream is = null;
94 | byte[] buffer = null;
95 | try {
96 | is = context.getAssets().open(fileName);
97 | int size = is.available();
98 | buffer = new byte[size];
99 | is.read(buffer);
100 | } catch (IOException e) {
101 | e.printStackTrace();
102 | } finally {
103 | if (is != null) {
104 | try {
105 | is.close();
106 | is = null;
107 | } catch (IOException e) {
108 | e.printStackTrace();
109 | }
110 | }
111 | }
112 |
113 | return buffer;
114 | }
115 |
116 | /**
117 | * 从raw文件夹下读取文件到字节数组
118 | *
119 | * @param context 上下文
120 | * @param rawId raw资源id
121 | * @return 文件字节数组
122 | */
123 | public static byte[] readBytesFromRaw(Context context, int rawId) {
124 | InputStream is = null;
125 | byte[] buffer = null;
126 | try {
127 | is = context.getResources().openRawResource(rawId);
128 | int size = is.available();
129 | buffer = new byte[size];
130 | is.read(buffer);
131 | } catch (IOException e) {
132 | e.printStackTrace();
133 | } finally {
134 | if (is != null) {
135 | try {
136 | is.close();
137 | is = null;
138 | } catch (IOException e) {
139 | e.printStackTrace();
140 | }
141 | }
142 | }
143 | return buffer;
144 | }
145 |
146 | /**
147 | * 读取raw目录的文件内容
148 | *
149 | * @param context 内容上下文
150 | * @param rawFileId raw文件名id
151 | * @return
152 | */
153 | public static String readRawValue(Context context, int rawFileId) {
154 | String result = "";
155 | try {
156 | InputStream is = context.getResources().openRawResource(rawFileId);
157 | int len = is.available();
158 | byte[] buffer = new byte[len];
159 | is.read(buffer);
160 | result = new String(buffer, "UTF-8");
161 | is.close();
162 | } catch (Exception e) {
163 | e.printStackTrace();
164 | }
165 | return result;
166 | }
167 |
168 | /**
169 | * 获取 layout 布局文件
170 | *
171 | * @param context Context
172 | * @param resName layout xml 的文件名
173 | * @return layout
174 | */
175 | public static int getLayoutId(Context context, String resName) {
176 | return context.getResources().getIdentifier(resName, "layout",
177 | context.getPackageName());
178 | }
179 |
180 | /**
181 | * 获取 string 值
182 | *
183 | * @param context Context
184 | * @param resName string name的名称
185 | * @return string
186 | */
187 | public static int getStringId(Context context, String resName) {
188 | return context.getResources().getIdentifier(resName, "string",
189 | context.getPackageName());
190 | }
191 |
192 | /**
193 | * 获取 drawable
194 | *
195 | * @param context Context
196 | * @param resName drawable 的名称
197 | * @return drawable
198 | */
199 | public static int getDrawableId(Context context, String resName) {
200 | return context.getResources().getIdentifier(resName,
201 | "drawable", context.getPackageName());
202 | }
203 |
204 | /**
205 | * 获取 mipmap
206 | *
207 | * @param context
208 | * @param resName
209 | * @return
210 | */
211 | public static int getMipmapId(Context context, String resName) {
212 | return context.getResources().getIdentifier(resName,
213 | "mipmap", context.getPackageName());
214 | }
215 |
216 |
217 | /**
218 | * 获取 style
219 | *
220 | * @param context Context
221 | * @param resName style的名称
222 | * @return style
223 | */
224 | public static int getStyleId(Context context, String resName) {
225 | return context.getResources().getIdentifier(resName, "style",
226 | context.getPackageName());
227 | }
228 |
229 | /**
230 | * 获取 styleable
231 | *
232 | * @param context Context
233 | * @param resName styleable 的名称
234 | * @return styleable
235 | */
236 | public static Object getStyleableId(Context context, String resName) {
237 | return context.getResources().getIdentifier(resName, "styleable",
238 | context.getPackageName());
239 | }
240 |
241 |
242 | /**
243 | * 获取 anim
244 | *
245 | * @param context Context
246 | * @param resName anim xml 文件名称
247 | * @return anim
248 | */
249 | public static int getAnimId(Context context, String resName) {
250 | return context.getResources().getIdentifier(resName, "anim",
251 | context.getPackageName());
252 | }
253 |
254 | /**
255 | * 获取 id
256 | *
257 | * @param context Context
258 | * @param resName id 的名称
259 | * @return
260 | */
261 | public static int getId(Context context, String resName) {
262 | return context.getResources().getIdentifier(resName, "id",
263 | context.getPackageName());
264 | }
265 |
266 | /**
267 | * color
268 | *
269 | * @param context Context
270 | * @param resName color 名称
271 | * @return
272 | */
273 | public static int getColorId(Context context, String resName) {
274 | return context.getResources().getIdentifier(resName, "color",
275 | context.getPackageName());
276 | }
277 |
278 |
279 | /**
280 | * 获取Manifest Meta Data
281 | *
282 | * @param context
283 | * @param metaKey
284 | * @return
285 | */
286 | public static String getMetaData(Context context, String metaKey) {
287 | String name = context.getPackageName();
288 | ApplicationInfo appInfo;
289 | String msg = "";
290 | try {
291 | appInfo = context.getPackageManager().getApplicationInfo(name,
292 | PackageManager.GET_META_DATA);
293 | msg = appInfo.metaData.getString(metaKey);
294 | } catch (PackageManager.NameNotFoundException e) {
295 | e.printStackTrace();
296 | }
297 |
298 | if (StringUtils.isEmpty(msg)) {
299 | return "";
300 | }
301 |
302 | return msg;
303 | }
304 |
305 | /**
306 | * 获得渠道号
307 | *
308 | * @param context
309 | * @param channelKey
310 | * @return
311 | */
312 | public static String getChannelNo(Context context, String channelKey) {
313 | return getMetaData(context, channelKey);
314 | }
315 | }
316 |
--------------------------------------------------------------------------------
/YHUtils/src/main/java/xyz/yhsj/yhutils/io/SDCardUtils.java:
--------------------------------------------------------------------------------
1 | package xyz.yhsj.yhutils.io;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.File;
5 | import java.io.FileNotFoundException;
6 | import java.io.IOException;
7 | import java.io.InputStream;
8 | import java.io.InputStreamReader;
9 | import java.util.ArrayList;
10 |
11 | import xyz.yhsj.yhutils.logutils.LogUtils;
12 |
13 | import android.content.Context;
14 | import android.os.Environment;
15 | import android.os.StatFs;
16 |
17 |
18 | /**
19 | * 内存卡 工具类
20 | */
21 | public class SDCardUtils {
22 |
23 | /**
24 | * 判断SDCard是否可用
25 | */
26 | public static boolean isSDCardEnable() {
27 | boolean result = Environment.getExternalStorageState().equals(
28 | Environment.MEDIA_MOUNTED);
29 | if (result)
30 | LogUtils.i("当前内存卡有效");
31 | else
32 | LogUtils.i("当前内存卡无效");
33 |
34 | return result;
35 |
36 | }
37 |
38 |
39 | /**
40 | * 获取SD卡路径
41 | */
42 | public static ArrayList getSDCardPathEx() {
43 | ArrayList list = new ArrayList();
44 | try {
45 | Runtime runtime = Runtime.getRuntime();
46 | Process proc = runtime.exec("mount");
47 | InputStream is = proc.getInputStream();
48 | InputStreamReader isr = new InputStreamReader(is);
49 | String line;
50 | BufferedReader br = new BufferedReader(isr);
51 | while ((line = br.readLine()) != null) {
52 | if (line.contains("secure")) {
53 | continue;
54 | }
55 | if (line.contains("asec")) {
56 | continue;
57 | }
58 |
59 | if (line.contains("fat")) {
60 | String columns[] = line.split(" ");
61 | if (columns.length > 1) {
62 | list.add("*" + columns[1]);
63 | }
64 | } else if (line.contains("fuse")) {
65 | String columns[] = line.split(" ");
66 | if (columns.length > 1) {
67 | list.add(columns[1]);
68 | }
69 | }
70 | }
71 | } catch (FileNotFoundException e) {
72 | e.printStackTrace();
73 | } catch (IOException e) {
74 | e.printStackTrace();
75 | }
76 | return list;
77 | }
78 |
79 | /**
80 | * 获取外部存储设备的总空间大小
81 | */
82 | public static long getSDCardTotalSize(Context context) {
83 | if (isSDCardEnable()) {
84 | // 得到一个外部存储设备的目录/通过getPath得到路径
85 | File path = Environment.getExternalStorageDirectory();
86 | // 文件系统的帮助类,传入一个路径可以得到路径的信息
87 | StatFs stat = new StatFs(path.getPath());
88 | // 得到该存储空间每一块存储空间的大小
89 | long blockSize = stat.getBlockSize();
90 | // 得到空间总个数
91 | long totalBlocks = stat.getBlockCount();
92 |
93 | // 得到总空间大小
94 | long totalSize = totalBlocks * blockSize;
95 |
96 | // String totalStr = Formatter.formatFileSize(context, totalSize);
97 |
98 | return totalSize;
99 | }
100 | return 0;
101 | }
102 |
103 | /**
104 | * 获取外部存储设备的剩余空间大小
105 | */
106 | public static long getSDCardAvailSize(Context context) {
107 | if (isSDCardEnable()) {
108 | // 得到一个外部存储设备的目录/通过getPath得到路径
109 | File path = Environment.getExternalStorageDirectory();
110 | // 文件系统的帮助类,传入一个路径可以得到路径的信息
111 | StatFs stat = new StatFs(path.getPath());
112 | // 得到该存储空间每一块存储空间的大小
113 | long blockSize = stat.getBlockSize();
114 | // 得到可用的空间个数
115 | long availableBlocks = stat.getAvailableBlocks();
116 | // 得到可用空间大小
117 | long availSize = availableBlocks * blockSize;
118 | // String availStr = Formatter.formatFileSize(context, availSize);
119 | return availSize;
120 | }
121 | return 0;
122 | }
123 |
124 | /**
125 | * 获取内部存储设备的总空间大小
126 | *
127 | * @param context
128 | * @return
129 | */
130 | public static long getRomSpaceTotalSize(Context context) {
131 | if (isSDCardEnable()) {
132 | // 得到一个内部存储设备的目录/通过getPath得到路径
133 | File path = Environment.getDataDirectory();
134 | // 文件系统的帮助类,传入一个路径可以得到路径的信息
135 | StatFs stat = new StatFs(path.getPath());
136 | // 得到该存储空间每一块存储空间的大小
137 | long blockSize = stat.getBlockSize();
138 | // 得到空间总个数
139 | long totalBlocks = stat.getBlockCount();
140 | // 得到总空间大小
141 | long totalSize = totalBlocks * blockSize;
142 |
143 | // String totalStr = Formatter.formatFileSize(context, totalSize);
144 |
145 | return totalSize;
146 | }
147 | return 0;
148 | }
149 |
150 | /**
151 | * 获取内部存储设备的剩余空间大小
152 | *
153 | * @param context
154 | * @return
155 | */
156 | public static long getRomSpaceAvailSize(Context context) {
157 | if (isSDCardEnable()) {
158 | // 得到一个内部存储设备的目录/通过getPath得到路径
159 | File path = Environment.getDataDirectory();
160 | // 文件系统的帮助类,传入一个路径可以得到路径的信息
161 | StatFs stat = new StatFs(path.getPath());
162 | // 得到该存储空间每一块存储空间的大小
163 | long blockSize = stat.getBlockSize();
164 | // 得到可用的空间个数
165 | long availableBlocks = stat.getAvailableBlocks();
166 | // 得到可用空间大小
167 | long availSize = availableBlocks * blockSize;
168 |
169 | // String availStr = Formatter.formatFileSize(context, availSize);
170 | return availSize;
171 | }
172 | return 0;
173 | }
174 |
175 | /**
176 | * 获取系统存储路径
177 | *
178 | * @return
179 | */
180 | public static String getRootDirectoryPath() {
181 | String path = Environment.getRootDirectory().getAbsolutePath();
182 | LogUtils.i("当前存储路径:" + path);
183 | return path;
184 | }
185 |
186 | /**
187 | * 判断SDCard是否已满
188 | *
189 | * @return
190 | */
191 | @SuppressWarnings({"deprecation", "unused"})
192 | public static boolean isSDCardSizeOverflow() {
193 | boolean result = false;
194 | // 取得SDCard当前的状态
195 | String sDcString = Environment.getExternalStorageState();
196 |
197 | if (sDcString.equals(Environment.MEDIA_MOUNTED)) {
198 |
199 | // 取得sdcard文件路径
200 | File pathFile = Environment
201 | .getExternalStorageDirectory();
202 | StatFs statfs = new StatFs(pathFile.getPath());
203 |
204 | // 获取SDCard上BLOCK总数
205 | long nTotalBlocks = statfs.getBlockCount();
206 |
207 | // 获取SDCard上每个block的SIZE
208 | long nBlocSize = statfs.getBlockSize();
209 |
210 | // 获取可供程序使用的Block的数量
211 | long nAvailaBlock = statfs.getAvailableBlocks();
212 |
213 | // 获取剩下的所有Block的数量(包括预留的一般程序无法使用的块)
214 | long nFreeBlock = statfs.getFreeBlocks();
215 |
216 | // 计算SDCard 总容量大小MB
217 | long nSDTotalSize = nTotalBlocks * nBlocSize / 1024 / 1024;
218 |
219 | // 计算 SDCard 剩余大小MB
220 | long nSDFreeSize = nAvailaBlock * nBlocSize / 1024 / 1024;
221 | if (nSDFreeSize <= 1) {
222 | result = true;
223 | }
224 | }// end of if
225 | // end of func
226 | return result;
227 | }
228 |
229 | }
230 |
--------------------------------------------------------------------------------
/YHUtils/src/main/java/xyz/yhsj/yhutils/keyboard/KeyBoardUtils.java:
--------------------------------------------------------------------------------
1 | package xyz.yhsj.yhutils.keyboard;
2 |
3 | import android.content.Context;
4 | import android.view.inputmethod.InputMethodManager;
5 | import android.widget.EditText;
6 |
7 | /**
8 | * 软键盘工具类
9 | */
10 | public class KeyBoardUtils {
11 | /**
12 | * 打开软键盘
13 | *
14 | * @param mEditText 输入框(好像关系不大)
15 | * @param mContext 上下文
16 | */
17 | public static void openKeybord(EditText mEditText, Context mContext) {
18 | InputMethodManager imm = (InputMethodManager) mContext
19 | .getSystemService(Context.INPUT_METHOD_SERVICE);
20 | imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN);
21 | imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
22 | InputMethodManager.HIDE_IMPLICIT_ONLY);
23 | }
24 |
25 | /**
26 | * 关闭软键盘
27 | *
28 | * @param mEditText 输入框(好像关系不大)
29 | * @param mContext 上下文
30 | */
31 | public static void closeKeybord(EditText mEditText, Context mContext) {
32 | InputMethodManager imm = (InputMethodManager) mContext
33 | .getSystemService(Context.INPUT_METHOD_SERVICE);
34 | imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/YHUtils/src/main/java/xyz/yhsj/yhutils/logutils/LogType.java:
--------------------------------------------------------------------------------
1 | package xyz.yhsj.yhutils.logutils;
2 |
3 | /**
4 | * Created by pengwei08 on 2015/7/20.
5 | * 日志类型
6 | */
7 | public enum LogType {
8 | Verbose, Debug, Info, Warn, Error, Wtf
9 | }
10 |
--------------------------------------------------------------------------------
/YHUtils/src/main/java/xyz/yhsj/yhutils/logutils/LogUtils.java:
--------------------------------------------------------------------------------
1 | package xyz.yhsj.yhutils.logutils;
2 |
3 | import xyz.yhsj.yhutils.logutils.utils.SystemUtil;
4 |
5 | /**
6 | * Created by pengwei08 on 2015/7/16. 日志管理器
7 | */
8 | public final class LogUtils {
9 |
10 | private static Logger logger;
11 | static {
12 | logger = new Logger();
13 | }
14 |
15 | /** 允许输出日志 */
16 | public static boolean configAllowLog = true;
17 |
18 | /** 配置日志Tag前缀 */
19 | public static String configTagPrefix = "";
20 |
21 | /**
22 | * verbose输出
23 | *
24 | * @param msg
25 | * @param args
26 | */
27 | public static void v(String msg, Object... args) {
28 | logger.v(SystemUtil.getStackTrace(), msg, args);
29 | }
30 |
31 | public static void v(Object object) {
32 | logger.v(SystemUtil.getStackTrace(), object);
33 | }
34 |
35 | /**
36 | * debug输出
37 | *
38 | * @param msg
39 | * @param args
40 | */
41 | public static void d(String msg, Object... args) {
42 | logger.d(SystemUtil.getStackTrace(), msg, args);
43 | }
44 |
45 | public static void d(Object object) {
46 | logger.d(SystemUtil.getStackTrace(), object);
47 | }
48 |
49 | /**
50 | * info输出
51 | *
52 | * @param msg
53 | * @param args
54 | */
55 | public static void i(String msg, Object... args) {
56 | logger.i(SystemUtil.getStackTrace(), msg, args);
57 | }
58 |
59 | public static void i(Object object) {
60 | logger.i(SystemUtil.getStackTrace(), object);
61 | }
62 |
63 | /**
64 | * warn输出
65 | *
66 | * @param msg
67 | * @param args
68 | */
69 | public static void w(String msg, Object... args) {
70 | logger.w(SystemUtil.getStackTrace(), msg, args);
71 | }
72 |
73 | public static void w(Object object) {
74 | logger.w(SystemUtil.getStackTrace(), object);
75 | }
76 |
77 | /**
78 | * error输出
79 | *
80 | * @param msg
81 | * @param args
82 | */
83 | public static void e(String msg, Object... args) {
84 | logger.e(SystemUtil.getStackTrace(), msg, args);
85 | }
86 |
87 | public static void e(Object object) {
88 | logger.e(SystemUtil.getStackTrace(), object);
89 | }
90 |
91 | /**
92 | * assert输出
93 | *
94 | * @param msg
95 | * @param args
96 | */
97 | public static void wtf(String msg, Object... args) {
98 | logger.wtf(SystemUtil.getStackTrace(), msg, args);
99 | }
100 |
101 | public static void wtf(Object object) {
102 | logger.wtf(SystemUtil.getStackTrace(), object);
103 | }
104 |
105 | /**
106 | * 打印json
107 | *
108 | * @param json
109 | */
110 | public static void json(String json) {
111 | logger.json(SystemUtil.getStackTrace(), json);
112 | }
113 |
114 | }
115 |
--------------------------------------------------------------------------------
/YHUtils/src/main/java/xyz/yhsj/yhutils/logutils/Logger.java:
--------------------------------------------------------------------------------
1 | package xyz.yhsj.yhutils.logutils;
2 |
3 | import static xyz.yhsj.yhutils.logutils.LogUtils.configTagPrefix;
4 |
5 | import java.util.Collection;
6 | import java.util.Iterator;
7 | import java.util.Map;
8 | import java.util.MissingFormatArgumentException;
9 | import java.util.Set;
10 |
11 | import org.json.JSONArray;
12 | import org.json.JSONException;
13 | import org.json.JSONObject;
14 |
15 | import xyz.yhsj.yhutils.logutils.utils.ArrayUtil;
16 | import xyz.yhsj.yhutils.logutils.utils.SystemUtil;
17 | import android.app.AlertDialog;
18 | import android.content.ClipboardManager;
19 | import android.content.Context;
20 | import android.content.DialogInterface;
21 | import android.text.TextUtils;
22 | import android.util.Log;
23 | import android.util.Pair;
24 | import android.widget.Toast;
25 |
26 | /**
27 | * Created by pengwei08 on 2015/7/20.
28 | */
29 | public final class Logger implements Printer {
30 |
31 | protected Logger() {
32 | }
33 |
34 | private AlertDialog dialog;
35 |
36 | /**
37 | * 打印字符串
38 | *
39 | * @param type
40 | * @param element
41 | * @param msg
42 | * @param args
43 | */
44 | private void logString(LogType type, StackTraceElement element, String msg,
45 | Object... args) {
46 | if (!LogUtils.configAllowLog) {
47 | return;
48 | }
49 | String tag = generateTag(element);
50 | msg = String.format(msg, args);
51 | switch (type) {
52 | case Verbose:
53 | Log.v(tag, msg);
54 | break;
55 | case Debug:
56 | Log.d(tag, msg);
57 | break;
58 | case Info:
59 | Log.i(tag, msg);
60 | break;
61 | case Warn:
62 | Log.w(tag, msg);
63 | break;
64 | case Error:
65 | Log.e(tag, msg);
66 | break;
67 | case Wtf:
68 | Log.wtf(tag, msg);
69 | break;
70 | default:
71 | break;
72 | }
73 | }
74 |
75 | /**
76 | * 打印对象
77 | *
78 | * @param type
79 | * @param element
80 | * @param object
81 | */
82 | private void logObject(LogType type, StackTraceElement element,
83 | Object object) {
84 | if (object != null) {
85 | final String simpleName = object.getClass().getSimpleName();
86 | if (object instanceof Throwable) {
87 | String tag = generateTag(element);
88 | String msg = object.toString();
89 | Exception exception = (Exception) object;
90 | switch (type) {
91 | case Verbose:
92 | Log.v(tag, msg, exception);
93 | break;
94 | case Debug:
95 | Log.d(tag, msg, exception);
96 | break;
97 | case Info:
98 | Log.i(tag, msg, exception);
99 | break;
100 | case Warn:
101 | Log.w(tag, msg, exception);
102 | break;
103 | case Error:
104 | Log.e(tag, msg, exception);
105 | break;
106 | case Wtf:
107 | Log.wtf(tag, msg, exception);
108 | break;
109 | default:
110 | break;
111 | }
112 | } else if (object instanceof String) {
113 | try {
114 | logString(type, element, (String) object);
115 | } catch (MissingFormatArgumentException e) {
116 | e(element, e);
117 | }
118 | } else if (object.getClass().isArray()) {
119 | String msg = "Temporarily not support more than two dimensional Array!";
120 | int dim = ArrayUtil.getArrayDimension(object);
121 | switch (dim) {
122 | case 1:
123 | Pair pair = ArrayUtil.arrayToString(object);
124 | msg = simpleName.replace("[]", "[" + pair.first + "] {\n");
125 | msg += pair.second + "\n";
126 | break;
127 | case 2:
128 | Pair pair1 = ArrayUtil.arrayToObject(object);
129 | Pair pair2 = (Pair) pair1.first;
130 | msg = simpleName.replace("[][]", "[" + pair2.first + "]["
131 | + pair2.second + "] {\n");
132 | msg += pair1.second + "\n";
133 | break;
134 | default:
135 | break;
136 | }
137 | logString(type, element, msg + "}");
138 | } else if (object instanceof Collection) {
139 | Collection collection = (Collection) object;
140 | String msg = "%s size = %d [\n";
141 | msg = String.format(msg, simpleName, collection.size());
142 | if (!collection.isEmpty()) {
143 | Iterator