├── .gitignore
├── .idea
├── .name
├── compiler.xml
├── copyright
│ └── profiles_settings.xml
├── encodings.xml
├── gradle.xml
├── misc.xml
├── modules.xml
├── runConfigurations.xml
└── vcs.xml
├── README.md
├── androidutils
├── .gitignore
├── build.gradle
├── libs
│ └── gson-1.6.jar
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── dsw
│ │ └── androidutils
│ │ └── ApplicationTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── dsw
│ │ │ └── androidutils
│ │ │ ├── AppUtil.java
│ │ │ ├── BitmapUtil.java
│ │ │ ├── CipherUtil.java
│ │ │ ├── CommonUtil.java
│ │ │ ├── DateUtil.java
│ │ │ ├── JsonUtil.java
│ │ │ ├── LogUtil.java
│ │ │ ├── MeasureUtil.java
│ │ │ ├── NetWorkUtil.java
│ │ │ ├── PreferencesUtil.java
│ │ │ ├── ReflectUtil.java
│ │ │ ├── SDCardUtil.java
│ │ │ ├── ScreenUtil.java
│ │ │ ├── ToastUtil.java
│ │ │ ├── XMLUtil.java
│ │ │ └── database
│ │ │ ├── AnnotationColumn.java
│ │ │ ├── AnnotationTable.java
│ │ │ ├── DataBaseHelper.java
│ │ │ └── SQLiteHelper.java
│ └── res
│ │ └── values
│ │ └── strings.xml
│ └── test
│ └── java
│ └── com
│ └── dsw
│ └── androidutils
│ └── ExampleUnitTest.java
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── utils
│ │ └── dsw
│ │ └── androidutils
│ │ └── ApplicationTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── utils
│ │ │ └── dsw
│ │ │ └── androidutils
│ │ │ ├── MainActivity.java
│ │ │ └── Student.java
│ └── res
│ │ ├── layout
│ │ └── activity_main.xml
│ │ ├── mipmap-hdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-mdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xhdpi
│ │ ├── ic_launcher.png
│ │ └── src.jpg
│ │ ├── mipmap-xxhdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xxxhdpi
│ │ └── ic_launcher.png
│ │ ├── values-w820dp
│ │ └── dimens.xml
│ │ └── values
│ │ ├── colors.xml
│ │ ├── dimens.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── com
│ └── utils
│ └── dsw
│ └── androidutils
│ └── ExampleUnitTest.java
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 |
--------------------------------------------------------------------------------
/.idea/.name:
--------------------------------------------------------------------------------
1 | AndroidUtilsApplication
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
19 |
20 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## Android开发常用工具类
2 | ### 1、AppUtil
3 | 该类包含常用的App工具类,涉及到版本号的获取、版本号的名称、应用程序图标等功能。
4 |
5 | * public static String getPacketName(Context context),获取包名
6 | * public static String getVersionName(Context context),获取VersionName(版本名称)
7 | * public static int getVersionCode(Context context),获取VersionCode(版本号)
8 | * public static List getInstalledPackages(Context context),获取所有安装的应用程序,不包含系统应用
9 | * public static Drawable getApplicationIcon(Context context),获取应用程序的icon图标
10 | * public static void installApk(Activity activity,String path),启动安装应用程序
11 |
12 | ### 2、BitmapUtil
13 | 该类包含Bitmap常用的工具类,涉及到Bitmap的获取、Bitmap与Drawable的相互转换。
14 |
15 | * public static Bitmap getBitmapFromResource(Context context,int id,int height,int width),根据资源id获取指定大小的Bitmap对象
16 | * public static Bitmap getBitmapFromFile(String path, int height, int width),根据文件路径获取指定大小的Bitmap对象
17 | * public static Bitmap getThumbnailsBitmap(Bitmap bitmap, int height, int width),获取指定大小的Bitmap对象
18 | * public static Drawable bitmapToDrawable(Context context, Bitmap bitmap),将Bitmap对象转换成Drawable对象
19 | * public static Bitmap drawableToBitmap(Drawable drawable),将Drawable对象转换成Bitmap对象
20 | * public static byte[] bitmapToByte(Bitmap bitmap),将Bitmap对象转换为byte[]数组
21 |
22 | ### 3、DateUtil
23 | 该类包含日期的常用处理,涉及到日期字符串与Date对象的各种格式相互转换、获取星期、年月份、天数等功能。
24 |
25 | * public static String getNowDate(DatePattern pattern),返回当前时间,格式2015-12-3 10:54:21
26 | * public static Date stringToDate(String dateString, DatePattern pattern),将一个日期字符串转换成Data对象
27 | * public static String dateToString(Date date, DatePattern pattern),将date转换成字符串
28 | * public static String getWeekOfDate(Date date),返回值为: "周日", "周一", "周二", "周三", "周四", "周五", "周六"
29 | * public static int getIndexWeekOfDate(Date date),获取指定日期对应周几的序列(周一:1 周二:2 周三:3 周四:4 周五:5 周六:6 周日:7)
30 | * public static int getNowMonth(),返回当前月份
31 | * public static int getNowDay(),获取当前月号
32 | * public static int getNowYear(),获取当前年份
33 | * public static int getNowDaysOfMonth(),获取本月份的天数
34 | * public static int daysOfMonth(int year,int month),获取指定月份的天数
35 |
36 | 补充内部类DatePattern:为我所定义的日期格式的枚举类。
37 |
38 | ### 4、JsonUtil
39 | 常用的Json工具类,包含Json转换成实体、实体转json字符串、list集合转换成json、数组转换成json
40 |
41 | * public static String objectToJson(T t),将一个对象转换成一个Json字符串
42 | * public static T jsonToObject(String jsonString, Class clazz),将Json字符串转换成对应对象
43 | * public static String listToJson(List list),将List集合转换为json字符串
44 | * public static String arrayToJson(T[] array),将数组转换成json字符串
45 | * public static T getJsonObjectValue(String json, String key, Class clazz),获取json字符串中的值
46 | * public static T getJsonObjectValue(JSONObject jsonObject, String key, Class clazz),获取jsonObject对象中的值
47 | * public static ContentValues jsonToContentValues(String json),json字符串转换为ContentValues
48 |
49 |
50 | ### 5、LogUtil
51 | Log日志工具类
52 |
53 | * public static void i(String tag,String msg),打印information日志
54 | * public static void i(String tag, String msg, Throwable throwable),打印information日志
55 | * public static void v(String tag, String msg),打印verbose日志
56 | * public static void v(String tag, String msg, Throwable throwable),打印verbose日志
57 | * public static void d(String tag, String msg),打印debug信息
58 | * public static void d(String tag, String msg, Throwable throwable),打印debug信息
59 | * public static void w(String tag, String msg),打印warn日志
60 | * public static void w(String tag, String msg, Throwable throwable),打印warn日志
61 | * public static void e(String tag, String msg),打印error日志
62 | * public static void e(String tag, String msg, Throwable throwable),throwable
63 |
64 | ### 6、MeasureUtil
65 | 常用的测量工具类
66 |
67 | * public static int getMeasuredHeight(View view),获取控件的测量高度
68 | * public static int getHeight(View view),控件的高度
69 | * public static int getMeasuredWidth(View view),获取控件的测量宽度
70 | * public static int getWidth(View view),获取控件的宽度
71 | * public static void setHeight(View view, int height) ,设置高度
72 | * public static void setWidth(View view, int width),设置View的宽度
73 | * public static void setListHeight(ListView listView),设置ListView的实际高度
74 | * public static void setGridViewHeight(Context context, GridView gv, int n, int m),设置GridView的高度
75 |
76 | ### 7、NetWorkUtil
77 | 网络工具类,包含网络的判断、跳转到设置页面
78 |
79 | * public static boolean isNetWorkEnable(Context context),判断当前是否有网络连接
80 | * public static boolean isWiFiConnected(Context context),判断当前网络是否为wifi
81 | * public static boolean isMobileDataEnable(Context context),判断MOBILE网络是否可用
82 | * public static boolean isWifiDataEnable(Context context),connectivityManager
83 | * public static void GoSetting(Activity activity),跳转到网络设置页面
84 |
85 |
86 | ### 8、PreferencesUtil
87 | SharedPreferences工具类,包含常用的数值获取和存储
88 |
89 | * public static boolean containsKey(Context context, String key),是否包含key
90 | * public static String getString(Context context, String key, String defValue),获取String
91 | * public static Set getStringSet(Context context, String key, Set defValues),获取Set 集合
92 | * public static int getInt(Context context, String key, int defValue),获取int值
93 | * public static float getFloat(Context context, String key, float defValue),获取float值
94 | * public static long getLong(Context context, String key, long defValue),获取Long类型值
95 | * public static boolean getBoolean(Context context, String key, boolean defValue),获取boolean类型的值
96 | * public static boolean putString(Context context, String key, String value),保存Stirng类型的值
97 | * public static boolean putStringSet(Context context, String key, Set value),保存Set集合的值
98 | * public static boolean putInt(Context context, String key, int value), 保存int类型的值
99 | * public static boolean putLong(Context context, String key, long value),保存long类型的值
100 | * public static boolean putFloat(Context context, String key, float value),保存float类型的值
101 | * public static boolean putBoolean(Context context, String key, boolean value),保存boolean类型的值
102 | * public static boolean removeKey(Context context, String key),删除关键字key
103 | * public static boolean clearValues(Context context),清除所有的关键字
104 |
105 | ### 9、ReflectUtil
106 | 反射工具类
107 |
108 | * public static void setFieldValue(T t,Field field, String fieldName, String value),设置字段值
109 | * public static Field getField(Class> clazz, String filedName),根据字段名称获取指定Field字段
110 | * public static Field getFieldByName(Field[] fields, String fieldName),根据字段名称获取指定的Field
111 | * public static boolean isFiledWithName(Field field, String fieldName),判断该字段是否为FieldName对应字段
112 |
113 |
114 | ### 10、ScreenUtil
115 | 屏幕工具类,涉及到屏幕宽度、高度、密度比、(像素、dp、sp)之间的转换等。
116 |
117 | * public static int getScreenWidth(Context context)、获取屏幕宽度,单位为px
118 | * public static int getScreenHeight(Context context),获取屏幕高度,单位为px
119 | * public static float getDensity(Context context),获取系统dp尺寸密度值
120 | * public static float getScaledDensity(Context context),获取系统字体sp密度值
121 | * public static int dp2px(Context context, int dpValue),dip转换为px大小
122 | * public static int px2dp(Context context, int pxValue),px转换为dp值
123 | * public static int sp2px(Context context, int spValue),sp转换为px
124 | * public static int px2sp(Context context, int pxValue),px转换为sp
125 | * public static int getStatusHeight(Context context),获得状态栏的高度
126 | * public static Bitmap snapShotWithStatusBar(Activity activity),获取当前屏幕截图,包含状态栏
127 | * public static Bitmap snapShotWithoutStatusBar(Activity activity),获取当前屏幕截图,不包含状态栏
128 | * public static DisplayMetrics getDisplayMetrics(Context context),获取DisplayMetrics对象
129 |
130 |
131 | ### 11、SDCardUtil
132 | SD卡工具类,包含SD卡状态、路径、容量大小
133 |
134 | * public static boolean isSDCardEnable(),判断SD卡是否可用
135 | * public static String getSDCradPath(),获取SD卡路径
136 | * public static File getSDCardFile(),获取SD卡路径
137 | * public static String getSDCardDownloadCachePath(),获取SD卡DownloadCache路径
138 | * public static File getSDCardDownloadCacheFile(),获取SD卡DownloadCache路径
139 | * public static String getSDCardRootPath(),获取系统存储路径
140 | * public static File getSDCardRootFile(),获取系统存储路径
141 | * public static String getDataFilePath(Context context),获取应用程序的/data/data目录
142 | * public static String getDataCachePath(Context context),/data/data/PackageName/cache的路径
143 | * public static long getSDCardSize() ,获取SD卡大小
144 | * public static long getSDCardAvailableSize(),获取SD卡大小
145 | * public long getRomTotalSize(),获得手机内存总大小
146 | * public long getRomAvailableSize(),获得手机可用内存
147 |
148 | ### 12、ToastUtil
149 | Toast工具类
150 |
151 | * public static void showLongToast(Context context, String msg),长时Toast
152 | * public static void showShortToast(Context context, String msg),短时Toast
153 |
154 | ### 13、XMLUtil
155 | XML文件工具类,包含:将xml文件解析成实体集合、获取xml标签值、将标签值解析成实体集合。这个工具类主要是讲XML的解析进行抽象出来,方便使用。
156 |
157 | * public static List xmlToObject(String xml, Class clazz, String tagEntity),XML文件解析成实体,不涉及到标签的属性值。
158 | * public static List attributeToObject(String xml, Class clazz, String tagName),获取xml字符串标签中的属性值
159 | * public static String getTagAttribute(String xml, String tagName, String attributeName),获取Xml文件中的属性值
160 |
161 |
162 | ### 14、DataBaseHelper
163 | 数据库工具类,包含数据库的创建、表的创建、增删改查。
164 |
165 | * public void createTables(List> tableClassList),创建数据表,该套逻辑是基于标注解和字段注解进行判断
166 | * public long insert(String tableName, ContentValues contentValues),向数据库插入数据
167 | * public void insertBySQL(String sqlInsert),通过SQL语句进行插入数据
168 | * public int update(String tableName, ContentValues contentValues,
169 | String whereClause, String [] whereArgs),更新数据信息
170 | * public void updateBySQL(String sqlUpdate),通过SQL语句进行数据库的更新
171 | * public int delete(String tableName, String whereClause, String [] whereArgs),删除数据
172 | * public void deleteBySQL(String sqlUpdate),通过SQL语句进行数据的删除
173 | * public Cursor queryCursor(String sql, String [] whereArgs),查询
174 | * public List queryList(String sql, String[] whereArgs, Class clazz ),查询数据库,返回集合。
175 | * public T queryFirst(String sql, String[] whereArgs, Class clazz),查询第一个记录
176 | * public void beginTranslaction(),开启事务
177 | * public void setTransactionSuccessful(),设置事务处理成功
178 | * public void endTransaction(),设置事务处理结束
179 | * public int getCountOfTable(String tableName),获取某个表中的记录条数
180 | * public int getCount(String sql,String[] whereArgs),根据SQL语句,获取查询结果的条数
181 | * public void dropTable(String tableName),删除表,从数据库中删除该表
182 | * public void clearTableData(String tableName),清除表中所有数据
183 |
184 |
185 | ## 操作案例:
186 | 
187 |
188 |
189 |
190 |
--------------------------------------------------------------------------------
/androidutils/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/androidutils/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 23
5 | buildToolsVersion "23.0.2"
6 |
7 | defaultConfig {
8 | minSdkVersion 11
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 | testCompile 'junit:junit:4.12'
24 | compile 'com.android.support:appcompat-v7:23.1.1'
25 | }
26 |
--------------------------------------------------------------------------------
/androidutils/libs/gson-1.6.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dengshiwei/AndroidUtils/6337fe85ee3546cbe3ef296670cdceb4327a6dc8/androidutils/libs/gson-1.6.jar
--------------------------------------------------------------------------------
/androidutils/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:\JavaSoftware\AndroidStudio\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 |
--------------------------------------------------------------------------------
/androidutils/src/androidTest/java/com/dsw/androidutils/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.dsw.androidutils;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/androidutils/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/androidutils/src/main/java/com/dsw/androidutils/AppUtil.java:
--------------------------------------------------------------------------------
1 | package com.dsw.androidutils;
2 |
3 | import java.io.File;
4 | import java.util.ArrayList;
5 | import java.util.List;
6 |
7 | import android.app.Activity;
8 | import android.content.Context;
9 | import android.content.Intent;
10 | import android.content.pm.ApplicationInfo;
11 | import android.content.pm.PackageInfo;
12 | import android.content.pm.PackageManager;
13 | import android.content.pm.PackageManager.NameNotFoundException;
14 | import android.graphics.drawable.Drawable;
15 | import android.net.Uri;
16 |
17 | /**
18 | * 常用App的工具类,包含版本号、版本名称、安装的应用程序ICON
19 | * @author Administrator
20 | *
21 | */
22 | public class AppUtil {
23 | private AppUtil(){};
24 |
25 | /**
26 | * 获取包名
27 | * @param context
28 | * @return
29 | */
30 | public static String getPacketName(Context context){
31 | return context.getPackageName();
32 | }
33 |
34 | /**
35 | * 获取VersionName(版本名称)
36 | * @param context
37 | * @return
38 | * 失败时返回""
39 | */
40 | public static String getVersionName(Context context){
41 | PackageManager packageManager = getPackageManager(context);
42 | try {
43 | PackageInfo packageInfo = packageManager.getPackageInfo(getPacketName(context), 0);
44 | return packageInfo.versionName;
45 | } catch (NameNotFoundException e) {
46 | e.printStackTrace();
47 | }
48 | return "";
49 | }
50 |
51 | /**
52 | * 获取VersionCode(版本号)
53 | * @param context
54 | * @return
55 | * 失败时返回-1
56 | */
57 | public static int getVersionCode(Context context){
58 | PackageManager packageManager = getPackageManager(context);
59 | try {
60 | PackageInfo packageInfo = packageManager.getPackageInfo(getPacketName(context), 0);
61 | return packageInfo.versionCode;
62 | } catch (NameNotFoundException e) {
63 | e.printStackTrace();
64 | }
65 | return -1;
66 | }
67 |
68 | /**
69 | * 获取所有安装的应用程序,不包含系统应用
70 | * @param context
71 | * @return
72 | */
73 | public static List getInstalledPackages(Context context){
74 | PackageManager packageManager = getPackageManager(context);
75 | List packageInfos = packageManager.getInstalledPackages(0);
76 | List packageInfoList = new ArrayList();
77 | for(int i=0; i < packageInfos.size();i++){
78 | if((packageInfos.get(i).applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0){//系统应用
79 | packageInfoList.add(packageInfos.get(i));
80 | }
81 | }
82 | return packageInfoList;
83 | }
84 |
85 | /**
86 | * 获取应用程序的icon图标
87 | * @param context
88 | * @return
89 | * 当包名错误时,返回null
90 | */
91 | public static Drawable getApplicationIcon(Context context){
92 | PackageManager packageManager = getPackageManager(context);
93 | try {
94 | PackageInfo packageInfo = packageManager.getPackageInfo(getPacketName(context), 0);
95 | return packageInfo.applicationInfo.loadIcon(packageManager);
96 | } catch (NameNotFoundException e) {
97 | e.printStackTrace();
98 | }
99 | return null;
100 | }
101 |
102 | /**
103 | * 启动安装应用程序
104 | * @param activity
105 | * @param path 应用程序路径
106 | */
107 | public static void installApk(Activity activity,String path){
108 | Intent intent = new Intent(Intent.ACTION_VIEW);
109 | intent.setDataAndType(Uri.fromFile(new File(path)),
110 | "application/vnd.android.package-archive");
111 | activity.startActivity(intent);
112 | }
113 |
114 | /**
115 | * 获取PackageManager对象
116 | * @param context
117 | * @return
118 | */
119 | private static PackageManager getPackageManager(Context context){
120 | return context.getPackageManager();
121 | }
122 | }
123 |
--------------------------------------------------------------------------------
/androidutils/src/main/java/com/dsw/androidutils/BitmapUtil.java:
--------------------------------------------------------------------------------
1 | package com.dsw.androidutils;
2 |
3 | import java.io.ByteArrayOutputStream;
4 |
5 | import android.content.Context;
6 | import android.graphics.Bitmap;
7 | import android.graphics.BitmapFactory;
8 | import android.graphics.Bitmap.CompressFormat;
9 | import android.graphics.BitmapFactory.Options;
10 | import android.graphics.Canvas;
11 | import android.graphics.PixelFormat;
12 | import android.graphics.drawable.BitmapDrawable;
13 | import android.graphics.drawable.Drawable;
14 | import android.media.ThumbnailUtils;
15 | import android.text.TextUtils;
16 |
17 | /**
18 | * Bitmap工具类,获取Bitmap对象
19 | * @author Administrator
20 | *
21 | */
22 | public class BitmapUtil {
23 |
24 | private BitmapUtil(){};
25 |
26 | /**
27 | * 根据资源id获取指定大小的Bitmap对象
28 | * @param context 应用程序上下文
29 | * @param id 资源id
30 | * @param height 高度
31 | * @param width 宽度
32 | * @return
33 | */
34 | public static Bitmap getBitmapFromResource(Context context,int id,int height,int width){
35 | Options options = new Options();
36 | options.inJustDecodeBounds = true;
37 | BitmapFactory.decodeResource(context.getResources(), id, options);
38 | options.inSampleSize = calculateSampleSize(height, width, options);
39 | options.inJustDecodeBounds = false;
40 | Bitmap bitmap;
41 | bitmap = BitmapFactory.decodeResource(context.getResources(), id, options);
42 | return bitmap;
43 | }
44 |
45 | /**
46 | * 根据文件路径获取指定大小的Bitmap对象
47 | * @param path 文件路径
48 | * @param height 高度
49 | * @param width 宽度
50 | * @return
51 | */
52 | public static Bitmap getBitmapFromFile(String path, int height, int width){
53 | if(TextUtils.isEmpty(path)){
54 | throw new IllegalArgumentException("Params is null, please check your path:" + path);
55 | }
56 | Options options = new Options();
57 | options.inJustDecodeBounds = true;
58 | BitmapFactory.decodeFile(path, options);
59 | options.inSampleSize = calculateSampleSize(height, width, options);
60 | options.inJustDecodeBounds = false;
61 | Bitmap bitmap;
62 | bitmap = BitmapFactory.decodeFile(path, options);
63 | return bitmap;
64 | }
65 |
66 | /**
67 | * 获取指定大小的Bitmap对象
68 | * @param bitmap Bitmap对象
69 | * @param height 高度
70 | * @param width 宽度
71 | * @return
72 | */
73 | public static Bitmap getThumbnailsBitmap(Bitmap bitmap, int height, int width){
74 | if(bitmap == null){
75 | throw new IllegalArgumentException("Bitmap is null, please check you param");
76 | }
77 | return ThumbnailUtils.extractThumbnail(bitmap, width, height);
78 | }
79 |
80 | /**
81 | * 将Bitmap对象转换成Drawable对象
82 | * @param context 应用程序上下文
83 | * @param bitmap Bitmap对象
84 | * @return 返回转换后的Drawable对象
85 | */
86 | public static Drawable bitmapToDrawable(Context context, Bitmap bitmap){
87 | if(context == null || bitmap == null){
88 | throw new IllegalArgumentException("Params illegal, please check you param");
89 | }
90 | Drawable drawable = new BitmapDrawable(context.getResources(), bitmap);
91 | return drawable;
92 | }
93 |
94 | /**
95 | * 将Drawable对象转换成Bitmap对象
96 | * @param drawable Drawable对象
97 | * @return 返回转换后的Bitmap对象
98 | */
99 | public static Bitmap drawableToBitmap(Drawable drawable) {
100 | if(drawable == null){
101 | throw new IllegalArgumentException("Drawable is null, please check you param");
102 | }
103 | Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight(),
104 | drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
105 | Canvas canvas = new Canvas(bitmap);
106 | drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
107 | drawable.draw(canvas);
108 | return bitmap;
109 | }
110 |
111 | /**
112 | * 将Bitmap对象转换为byte[]数组
113 | * @param bitmap Bitmap对象
114 | * @return 返回转换后的数组
115 | */
116 | public static byte[] bitmapToByte(Bitmap bitmap){
117 | if(bitmap == null){
118 | throw new IllegalArgumentException("Bitmap is null, please check you param");
119 | }
120 | ByteArrayOutputStream baos = new ByteArrayOutputStream();
121 | bitmap.compress(CompressFormat.PNG, 100, baos);
122 | return baos.toByteArray();
123 | }
124 |
125 | /**
126 | * 计算所需图片的缩放比例
127 | * @param height 高度
128 | * @param width 宽度
129 | * @param options options选项
130 | * @return
131 | */
132 | private static int calculateSampleSize(int height, int width, Options options) {
133 | int realHeight = options.outHeight;
134 | int realWidth = options.outWidth;
135 | int heigthScale = realHeight / height;
136 | int widthScale = realWidth / width;
137 | if(widthScale > heigthScale){
138 | return widthScale;
139 | }else{
140 | return heigthScale;
141 | }
142 | }
143 | }
144 |
--------------------------------------------------------------------------------
/androidutils/src/main/java/com/dsw/androidutils/CipherUtil.java:
--------------------------------------------------------------------------------
1 | package com.dsw.androidutils;
2 |
3 |
4 | /**
5 | * 常用的加密工具类
6 | * @author Administrator
7 | *
8 | */
9 | public class CipherUtil {
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/androidutils/src/main/java/com/dsw/androidutils/CommonUtil.java:
--------------------------------------------------------------------------------
1 | package com.dsw.androidutils;
2 |
3 | import java.lang.reflect.Field;
4 | import java.lang.reflect.Type;
5 |
6 | import android.content.ContentValues;
7 |
8 | import com.dsw.androidutils.database.AnnotationColumn;
9 |
10 | public class CommonUtil {
11 |
12 | /**
13 | * 将对象转换成ContentValues对象
14 | * @param object 对象
15 | * @return 返回转换后的ContentValues对象
16 | */
17 | public static ContentValues objectToContentValues(Object object){
18 | if(object == null){
19 | throw new IllegalArgumentException("please check your argument");
20 | }
21 | ContentValues contentValues = new ContentValues();
22 | try{
23 | Field[] fields = object.getClass().getDeclaredFields();
24 | for(Field field : fields){
25 | String fieldName = field.getName();
26 | Type type = field.getType();
27 | field.setAccessible(true);
28 | if(type.equals(String.class)){
29 | contentValues.put(fieldName, field.get(object).toString());
30 | }else if(type.equals(Integer.class) || type.equals(Integer.TYPE)){
31 | contentValues.put(fieldName, field.getInt(object));
32 | }else if(type.equals(Float.class) || type.equals(Float.TYPE)){
33 | contentValues.put(fieldName, field.getFloat(object));
34 | }else if(type.equals(Long.class) || type.equals(Long.TYPE)){
35 | contentValues.put(fieldName, field.getLong(object));
36 | }else if(type.equals(Double.class) || type.equals(Double.TYPE)){
37 | contentValues.put(fieldName, field.getDouble(object));
38 | }else if(type.equals(Boolean.class) || type.equals(Boolean.TYPE)){
39 | contentValues.put(fieldName, field.getBoolean(object));
40 | }
41 | }
42 | } catch (IllegalAccessException | IllegalArgumentException e) {
43 | e.printStackTrace();
44 | }
45 | return contentValues;
46 | }
47 |
48 | /**
49 | * 将对象中注解为转换成ContentValues对象
50 | * @param object Object对象
51 | * @return
52 | */
53 | public static ContentValues objAnnotToContValues(Object object){
54 | if(object == null){
55 | throw new IllegalArgumentException("please check your argument");
56 | }
57 | ContentValues contentValues = new ContentValues();
58 | try{
59 | Field[] fields = object.getClass().getDeclaredFields();
60 | for(Field field : fields){
61 | if(!field.isAnnotationPresent(AnnotationColumn.class))continue;
62 | String fieldName = field.getName();
63 | Type type = field.getType();
64 | field.setAccessible(true);
65 | if(type.equals(String.class)){
66 | contentValues.put(fieldName, field.get(object).toString());
67 | }else if(type.equals(Integer.class) || type.equals(Integer.TYPE)){
68 | contentValues.put(fieldName, field.getInt(object));
69 | }else if(type.equals(Float.class) || type.equals(Float.TYPE)){
70 | contentValues.put(fieldName, field.getFloat(object));
71 | }else if(type.equals(Long.class) || type.equals(Long.TYPE)){
72 | contentValues.put(fieldName, field.getLong(object));
73 | }else if(type.equals(Double.class) || type.equals(Double.TYPE)){
74 | contentValues.put(fieldName, field.getDouble(object));
75 | }else if(type.equals(Boolean.class) || type.equals(Boolean.TYPE)){
76 | contentValues.put(fieldName, field.getBoolean(object));
77 | }
78 | }
79 | } catch (IllegalAccessException | IllegalArgumentException e) {
80 | e.printStackTrace();
81 | }
82 | return contentValues;
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/androidutils/src/main/java/com/dsw/androidutils/DateUtil.java:
--------------------------------------------------------------------------------
1 | package com.dsw.androidutils;
2 |
3 | import java.text.ParseException;
4 | import java.text.SimpleDateFormat;
5 | import java.util.Calendar;
6 | import java.util.Date;
7 | import java.util.Locale;
8 |
9 | /**
10 | * 日期工具类,包含日期的各种格式的获取,获取当前年、月、日,获取月份的天数
11 | * @author Administrator
12 | *
13 | */
14 | public class DateUtil {
15 | private DateUtil(){};
16 |
17 | /**
18 | * 枚举日期格式
19 | * @author Administrator
20 | *
21 | */
22 | public enum DatePattern{
23 | /**
24 | * 格式:"yyyy-MM-dd HH:mm:ss"
25 | */
26 | ALL_TIME{public String getValue(){return "yyyy-MM-dd HH:mm:ss";}},
27 | /**
28 | * 格式:"yyyy-MM"
29 | */
30 | ONLY_MONTH{public String getValue(){return "yyyy-MM";}},
31 | /**
32 | * 格式:"yyyy-MM-dd"
33 | */
34 | ONLY_DAY{public String getValue(){return "yyyy-MM-dd";}},
35 | /**
36 | * 格式:"yyyy-MM-dd HH"
37 | */
38 | ONLY_HOUR{public String getValue(){return "yyyy-MM-dd HH";}},
39 | /**
40 | * 格式:"yyyy-MM-dd HH:mm"
41 | */
42 | ONLY_MINUTE{public String getValue(){return "yyyy-MM-dd HH:mm";}},
43 | /**
44 | * 格式:"MM-dd"
45 | */
46 | ONLY_MONTH_DAY{public String getValue(){return "MM-dd";}},
47 | /**
48 | * 格式:"MM-dd HH:mm"
49 | */
50 | ONLY_MONTH_SEC{public String getValue(){return "MM-dd HH:mm";}},
51 | /**
52 | * 格式:"HH:mm:ss"
53 | */
54 | ONLY_TIME{public String getValue(){return "HH:mm:ss";}},
55 | /**
56 | * 格式:"HH:mm"
57 | */
58 | ONLY_HOUR_MINUTE{public String getValue(){return "HH:mm";}};
59 | public abstract String getValue();
60 | }
61 | /**
62 | * 获取当前时间
63 | * @return 返回当前时间,格式2015-12-3 10:54:21
64 | */
65 | public static String getNowDate(DatePattern pattern){
66 | String dateString = null;
67 | Calendar calendar = Calendar.getInstance();
68 | Date dateNow = calendar.getTime();
69 | SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern.getValue(),Locale.CHINA);
70 | dateString = simpleDateFormat.format(dateNow);
71 | return dateString;
72 | }
73 |
74 | /**
75 | * 将一个日期字符串转换成Data对象
76 | * @param dateString 日期字符串
77 | * @param pattern 转换格式
78 | * @return 返回转换后的日期对象
79 | */
80 | public static Date stringToDate(String dateString, DatePattern pattern){
81 | Date date = null;
82 | SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern.getValue(),Locale.CHINA);
83 | try {
84 | date = simpleDateFormat.parse(dateString);
85 | } catch (ParseException e) {
86 | e.printStackTrace();
87 | }
88 | return date;
89 | }
90 |
91 | /**
92 | * 将date转换成字符串
93 | * @param date 日期
94 | * @param pattern 日期的目标格式
95 | * @return
96 | */
97 | public static String dateToString(Date date, DatePattern pattern){
98 | String string = "";
99 | SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern.getValue(), Locale.CHINA);
100 | string = simpleDateFormat.format(date);
101 | return string;
102 | }
103 |
104 | /**
105 | * 获取指定日期周几
106 | * @param date 指定日期
107 | * @return
108 | * 返回值为: "周日", "周一", "周二", "周三", "周四", "周五", "周六"
109 | */
110 | public static String getWeekOfDate(Date date){
111 | String[] weekDays = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
112 | Calendar calendar = Calendar.getInstance();
113 | calendar.setTime(date);
114 | int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
115 | if (week < 0)
116 | week = 0;
117 | return weekDays[week];
118 | }
119 |
120 | /**
121 | * 获取指定日期对应周几的序列
122 | * @param date 指定日期
123 | * @return 周一:1 周二:2 周三:3 周四:4 周五:5 周六:6 周日:7
124 | */
125 | public static int getIndexWeekOfDate(Date date){
126 | Calendar calendar = Calendar.getInstance();
127 | calendar.setTime(date);
128 | int index = calendar.get(Calendar.DAY_OF_WEEK);
129 | if(index == 1){
130 | return 7;
131 | }else{
132 | return --index;
133 | }
134 | }
135 |
136 | /**
137 | * 返回当前月份
138 | * @return
139 | */
140 | public static int getNowMonth(){
141 | Calendar calendar = Calendar.getInstance();
142 | return calendar.get(Calendar.MONTH) + 1;
143 | }
144 |
145 | /**
146 | * 获取当前月号
147 | * @return
148 | */
149 | public static int getNowDay(){
150 | Calendar calendar = Calendar.getInstance();
151 | return calendar.get(Calendar.DATE);
152 | }
153 |
154 | /**
155 | * 获取当前年份
156 | * @return
157 | */
158 | public static int getNowYear(){
159 | Calendar calendar = Calendar.getInstance();
160 | return calendar.get(Calendar.YEAR);
161 | }
162 |
163 | /**
164 | * 获取本月份的天数
165 | * @param year
166 | * @param month
167 | * @return
168 | */
169 | public static int getNowDaysOfMonth(){
170 | Calendar calendar = Calendar.getInstance();
171 | return daysOfMonth(calendar.get(Calendar.YEAR),calendar.get(Calendar.DATE) + 1);
172 | }
173 |
174 | /**
175 | * 获取指定月份的天数
176 | * @param year 年份
177 | * @param month 月份
178 | * @return 对应天数
179 | */
180 | public static int daysOfMonth(int year,int month){
181 | switch(month){
182 | case 1:
183 | case 3:
184 | case 5:
185 | case 7:
186 | case 8:
187 | case 10:
188 | case 12:
189 | return 31;
190 | case 4:
191 | case 6:
192 | case 9:
193 | case 11:
194 | return 30;
195 | case 2:
196 | if((year % 4 ==0 && year % 100 == 0) || year % 400 == 0){
197 | return 29;
198 | }else{
199 | return 28;
200 | }
201 | default:
202 | return -1;
203 | }
204 | }
205 | }
206 |
--------------------------------------------------------------------------------
/androidutils/src/main/java/com/dsw/androidutils/JsonUtil.java:
--------------------------------------------------------------------------------
1 | package com.dsw.androidutils;
2 |
3 | import java.util.Iterator;
4 | import java.util.List;
5 |
6 | import org.json.JSONArray;
7 | import org.json.JSONException;
8 | import org.json.JSONObject;
9 |
10 | import android.content.ContentValues;
11 |
12 | import com.google.gson.Gson;
13 |
14 | /**
15 | * 常用的Json工具类,包含Json转换成实体、实体转json字符串、list集合转换成json、数组转换成json
16 | * @author Administrator
17 | *
18 | */
19 | public class JsonUtil {
20 | private JsonUtil(){};
21 |
22 | private static Gson gson = new Gson();
23 |
24 | /**
25 | * 将一个对象转换成一个Json字符串
26 | * @param t
27 | * @return
28 | */
29 | public static String objectToJson(T t){
30 | if(t instanceof String){
31 | return t.toString();
32 | }else{
33 | return gson.toJson(t);
34 | }
35 | }
36 |
37 | /**
38 | * 将Json字符串转换成对应对象
39 | * @param jsonString Json字符串
40 | * @param clazz 对应字节码文件.class
41 | * @return
42 | */
43 | @SuppressWarnings("unchecked")
44 | public static T jsonToObject(String jsonString, Class clazz){
45 | if(clazz == String.class){
46 | return (T) jsonString;
47 | }else{
48 | return (T) gson.fromJson(jsonString, clazz);
49 | }
50 | }
51 |
52 | /**
53 | * 将List集合转换为json字符串
54 | * @param list List集合
55 | * @return
56 | */
57 | public static String listToJson(List list){
58 | JSONArray jsonArray = new JSONArray();
59 | JSONObject jsonObject = null;
60 | try {
61 | for(int i=0;i String arrayToJson(T[] array){
81 | JSONArray jsonArray = new JSONArray();
82 | JSONObject jsonObject = null;
83 | try {
84 | for(int i=0;i T getJsonObjectValue(String json, String key, Class clazz){
107 | try {
108 | return getJsonObjectValue(new JSONObject(json), key, clazz);
109 | } catch (JSONException e) {
110 | e.printStackTrace();
111 | }
112 | return null;
113 | }
114 |
115 | /**
116 | * 获取jsonObject对象中的值
117 | * @param jsonObject jsonObject对象
118 | * @param key 键值
119 | * @param clazz 所取数据类型,例如:Integer.class,String.class,Double.class,JSONObject.class
120 | * @return 存在则返回正确值,不存在返回null
121 | */
122 | @SuppressWarnings("unchecked")
123 | public static T getJsonObjectValue(JSONObject jsonObject, String key, Class clazz){
124 | T t = null;
125 | try {
126 | if(clazz == Integer.class){
127 | t = (T) Integer.valueOf(jsonObject.getInt(key));
128 | }else if(clazz == Boolean.class){
129 | t = (T) Boolean.valueOf(jsonObject.getBoolean(key));
130 | }else if(clazz == String.class){
131 | t = (T) String.valueOf(jsonObject.getString(key));
132 | }else if(clazz == Double.class){
133 | t = (T) Double.valueOf(jsonObject.getDouble(key));
134 | }else if(clazz == JSONObject.class){
135 | t = (T) jsonObject.getJSONObject(key);
136 | }else if(clazz == JSONArray.class){
137 | t = (T) jsonObject.getJSONArray(key);
138 | }else if(clazz == Long.class){
139 | t = (T) Long.valueOf(jsonObject.getLong(key));
140 | }
141 | } catch (JSONException e) {
142 | e.printStackTrace();
143 | }
144 | return t;
145 | }
146 |
147 | /**
148 | * json字符串转换为ContentValues
149 | * @param json json字符串
150 | * @return
151 | */
152 | @SuppressWarnings("rawtypes")
153 | public static ContentValues jsonToContentValues(String json){
154 | ContentValues contentValues = new ContentValues();
155 | try {
156 | JSONObject jsonObject = new JSONObject(json);
157 | Iterator iterator = jsonObject.keys();
158 | String key;
159 | Object value;
160 | while(iterator.hasNext()){
161 | key = iterator.next().toString();
162 | value = jsonObject.get(key);
163 | String valueString = value.toString();
164 | if(value instanceof String){
165 | contentValues.put(key, valueString);
166 | }else if(value instanceof Integer){
167 | contentValues.put(key, Integer.valueOf(valueString));
168 | }else if(value instanceof Long){
169 | contentValues.put(key, Long.valueOf(valueString));
170 | }else if(value instanceof Double){
171 | contentValues.put(key, Double.valueOf(valueString));
172 | }else if(value instanceof Float){
173 | contentValues.put(key, Float.valueOf(valueString));
174 | }else if(value instanceof Boolean){
175 | contentValues.put(key, Boolean.valueOf(valueString));
176 | }
177 | }
178 | } catch (JSONException e) {
179 | e.printStackTrace();
180 | throw new Error("Json字符串不合法:" + json);
181 | }
182 |
183 | return contentValues;
184 | }
185 | }
186 |
--------------------------------------------------------------------------------
/androidutils/src/main/java/com/dsw/androidutils/LogUtil.java:
--------------------------------------------------------------------------------
1 | package com.dsw.androidutils;
2 |
3 | import android.util.Log;
4 | /**
5 | * Log日志工具类
6 | * @author Administrator
7 | *
8 | */
9 | public class LogUtil {
10 |
11 | private LogUtil(){};
12 |
13 | /**
14 | * 打印information日志
15 | * @param tag
16 | * 标签
17 | * @param msg
18 | * 日志信息
19 | */
20 | public static void i(String tag,String msg){
21 | Log.i(tag, msg);
22 | }
23 |
24 | /**
25 | * 打印information日志
26 | * @param tag 标签
27 | * @param msg 日志信息
28 | * @param throwable 异常
29 | */
30 | public static void i(String tag, String msg, Throwable throwable){
31 | Log.i(tag,msg,throwable);
32 | }
33 |
34 | /**
35 | * 打印verbose日志
36 | * @param tag 标签
37 | * @param msg 日志信息
38 | */
39 | public static void v(String tag, String msg){
40 | Log.v(tag, msg);
41 | }
42 |
43 | /**
44 | * 打印verbose日志
45 | * @param tag 标签
46 | * @param msg 日志信息
47 | * @param throwable 异常
48 | */
49 | public static void v(String tag, String msg, Throwable throwable){
50 | Log.v(tag, msg, throwable);
51 | }
52 |
53 | /**
54 | * 打印debug信息
55 | * @param tag 标签信息
56 | * @param msg 日志信息
57 | */
58 | public static void d(String tag, String msg){
59 | Log.d(tag, msg);
60 | }
61 |
62 | /**
63 | * 打印debug日志
64 | * @param tag 标签信息
65 | * @param msg 日志信息
66 | * @param throwable 异常
67 | */
68 | public static void d(String tag, String msg, Throwable throwable){
69 | Log.d(tag, msg, throwable);
70 | }
71 |
72 | /**
73 | * 打印warn日志
74 | * @param tag 标签信息
75 | * @param msg 日志信息
76 | */
77 | public static void w(String tag, String msg){
78 | Log.w(tag, msg);
79 | }
80 |
81 | /**
82 | * 打印warn日志
83 | * @param tag 标签信息
84 | * @param msg 日志信息
85 | * @param throwable 异常
86 | */
87 | public static void w(String tag, String msg, Throwable throwable){
88 | Log.w(tag, msg, throwable);
89 | }
90 |
91 | /**
92 | * 打印error日志
93 | * @param tag 标签
94 | * @param msg 日志信息
95 | */
96 | public static void e(String tag, String msg){
97 | Log.e(tag, msg);
98 | }
99 |
100 | /**
101 | * 打印error日志
102 | * @param tag 标签
103 | * @param msg 日志信息
104 | * @param throwable 异常
105 | */
106 | public static void e(String tag, String msg, Throwable throwable){
107 | Log.e(tag, msg, throwable);
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/androidutils/src/main/java/com/dsw/androidutils/MeasureUtil.java:
--------------------------------------------------------------------------------
1 | package com.dsw.androidutils;
2 |
3 | import android.annotation.SuppressLint;
4 | import android.content.Context;
5 | import android.view.View;
6 | import android.view.ViewGroup;
7 | import android.widget.ActionMenuView;
8 | import android.widget.Adapter;
9 | import android.widget.GridView;
10 | import android.widget.LinearLayout;
11 | import android.widget.ListView;
12 |
13 | /**
14 | * 常用的测量功能
15 | * @author Administrator
16 | *
17 | */
18 | public class MeasureUtil {
19 |
20 | private MeasureUtil(){};
21 |
22 | /**
23 | * 获取控件的测量高度
24 | * @param view 控件
25 | * @return 返回测量高度(MeasuredHeight)
26 | */
27 | public static int getMeasuredHeight(View view) {
28 | if(view == null){
29 | throw new IllegalArgumentException("view is null");
30 | }
31 |
32 | view.measure(0, 0);
33 | return view.getMeasuredHeight();
34 | }
35 |
36 | /**
37 | * 控件的高度
38 | * @param view 控件View
39 | * @return 返回控件的高度
40 | */
41 | public static int getHeight(View view){
42 | if(view == null){
43 | throw new IllegalArgumentException("view is null");
44 | }
45 |
46 | view.measure(0, 0);
47 | return view.getHeight();
48 | }
49 |
50 | /**
51 | * 获取控件的测量宽度
52 | * @param view 控件
53 | * @return 返回控件的测量宽度
54 | */
55 | public static int getMeasuredWidth(View view){
56 | if(view == null){
57 | throw new IllegalArgumentException("view is null");
58 | }
59 |
60 | view.measure(0, 0);
61 | return view.getMeasuredWidth();
62 | }
63 |
64 | /**
65 | * 获取控件的宽度
66 | * @param view 控件
67 | * @return 返回控件的宽度
68 | */
69 | public static int getWidth(View view){
70 | if(view == null){
71 | throw new IllegalArgumentException("view is null");
72 | }
73 |
74 | view.measure(0, 0);
75 | return view.getMeasuredWidth();
76 | }
77 |
78 | /**
79 | * 设置高度
80 | * @param view 控件
81 | * @param height 高度
82 | */
83 | public static void setHeight(View view, int height) {
84 | if(view == null || view.getLayoutParams() == null){
85 | throw new IllegalArgumentException("View LayoutParams is null");
86 | }
87 |
88 | ViewGroup.LayoutParams params = view.getLayoutParams();
89 | params.height = height;
90 | view.setLayoutParams(params);
91 | }
92 | /**
93 | * 设置View的宽度
94 | * @param view view
95 | * @param width 宽度
96 | */
97 | public static void setWidth(View view, int width){
98 | if(view == null || view.getLayoutParams() == null){
99 | throw new IllegalArgumentException("View LayoutParams is null");
100 | }
101 |
102 | ViewGroup.LayoutParams params = view.getLayoutParams();
103 | params.width = width;
104 | view.setLayoutParams(params);
105 | }
106 |
107 | /**
108 | * 设置ListView的实际高度
109 | * @param listView ListView控件
110 | */
111 | public static void setListHeight(ListView listView) {
112 | if(listView == null){
113 | throw new IllegalArgumentException("ListView is null");
114 | }
115 |
116 | Adapter adapter = listView.getAdapter();
117 | if (adapter == null) {
118 | return;
119 | }
120 | int totalHeight = 0;
121 | int size = adapter.getCount();
122 | for (int i = 0; i < size; i++) {
123 | View listItem = adapter.getView(i, null, listView);
124 | listItem.measure(0, 0);
125 | totalHeight += listItem.getMeasuredHeight();
126 | }
127 | ViewGroup.LayoutParams params = listView.getLayoutParams();
128 | params.height = totalHeight + (listView.getDividerHeight() * (size - 1));
129 | LogUtil.d("MeasureUtil", "listview-height--" + params.height);
130 | listView.setLayoutParams(params);
131 | }
132 |
133 |
134 | /**
135 | * 设置GridView的高度,
136 | * @param context 应用程序上下文
137 | * @param gv GridView控件
138 | * @param n 行数
139 | * @param m 列数
140 | */
141 | @SuppressLint("NewApi")
142 | public static void setGridViewHeight(Context context, GridView gv, int n, int m) {
143 | if(gv == null){
144 | throw new IllegalArgumentException("GridView is null");
145 | }
146 |
147 | Adapter adapter = gv.getAdapter();
148 | if (adapter == null) {
149 | return;
150 | }
151 | int totalHeight = 0;
152 | int s = adapter.getCount();
153 | for (int i = 0; i < s; i++) {
154 | if (i % n == 0) {
155 | View listItem = adapter.getView(i, null, gv);
156 | listItem.measure(0, 0);
157 | totalHeight +=
158 | listItem.getMeasuredHeight() + ScreenUtil.dp2px(context, m);
159 | }
160 | }
161 | ViewGroup.LayoutParams params = gv.getLayoutParams();
162 | params.height = totalHeight + gv.getPaddingTop() + gv.getPaddingBottom() + 2;
163 | LogUtil.d("MeasureUtil", "gridview-height--" + params.height);
164 | gv.setLayoutParams(params);
165 | }
166 |
167 | }
168 |
--------------------------------------------------------------------------------
/androidutils/src/main/java/com/dsw/androidutils/NetWorkUtil.java:
--------------------------------------------------------------------------------
1 | package com.dsw.androidutils;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.net.ConnectivityManager;
7 | import android.net.NetworkInfo;
8 | import android.provider.Settings;
9 |
10 | /**
11 | * 网络工具类,包含网络的判断、跳转到设置页面
12 | * @author Administrator
13 | *
14 | */
15 | public class NetWorkUtil {
16 |
17 | /**
18 | * 判断当前是否有网络连接
19 | * @param context
20 | * @return 有网络返回true;无网络返回false
21 | */
22 | public static boolean isNetWorkEnable(Context context){
23 | ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
24 | NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
25 | if(networkInfo != null && networkInfo.isConnected()){
26 | if(networkInfo.getState() == NetworkInfo.State.CONNECTED){
27 | return true;
28 | }
29 | }
30 | return false;
31 | }
32 |
33 | /**
34 | * 判断当前网络是否为wifi
35 | * @param context
36 | * @return 如果为wifi返回true;否则返回false
37 | */
38 | public static boolean isWiFiConnected(Context context){
39 | ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
40 | NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
41 | return networkInfo.getType() == ConnectivityManager.TYPE_WIFI ? true :false;
42 | }
43 |
44 | /**
45 | * 判断MOBILE网络是否可用
46 | * @param context
47 | * @return
48 | * @throws Exception
49 | */
50 | public static boolean isMobileDataEnable(Context context){
51 | ConnectivityManager connectivityManager = (ConnectivityManager) context
52 | .getSystemService(Context.CONNECTIVITY_SERVICE);
53 | boolean isMobileDataEnable = false;
54 | isMobileDataEnable = connectivityManager.getNetworkInfo(
55 | ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
56 |
57 | return isMobileDataEnable;
58 | }
59 |
60 | /**
61 | * 判断wifi 是否可用
62 | * @param context
63 | * @return
64 | * @throws Exception
65 | */
66 | public static boolean isWifiDataEnable(Context context){
67 | ConnectivityManager connectivityManager = (ConnectivityManager) context
68 | .getSystemService(Context.CONNECTIVITY_SERVICE);
69 | boolean isWifiDataEnable = false;
70 | isWifiDataEnable = connectivityManager.getNetworkInfo(
71 | ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
72 | return isWifiDataEnable;
73 | }
74 |
75 | /**
76 | * 跳转到网络设置页面
77 | * @param activity
78 | */
79 | public static void GoSetting(Activity activity){
80 | Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
81 | activity.startActivity(intent);
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/androidutils/src/main/java/com/dsw/androidutils/PreferencesUtil.java:
--------------------------------------------------------------------------------
1 | package com.dsw.androidutils;
2 |
3 | import java.util.Set;
4 |
5 | import android.content.Context;
6 | import android.content.SharedPreferences;
7 | import android.content.SharedPreferences.Editor;
8 |
9 | /**
10 | * SharedPreferences工具类,包含常用的数值获取和存储
11 | * @author Administrator
12 | *
13 | */
14 | public class PreferencesUtil {
15 | private PreferencesUtil(){};
16 | /**
17 | * 默认的SharePreference名称
18 | */
19 | private static final String SHARED_NAME = "SharedPreferences";
20 |
21 | /**
22 | * 是否包含key
23 | * @param context 应用程序上下文
24 | * @param key key关键字
25 | * @return 包含返回true;反之返回false
26 | */
27 | public static boolean containsKey(Context context, String key){
28 | SharedPreferences sp = getSharedPreferences(context);
29 | return sp.contains(key);
30 | }
31 |
32 | /**
33 | * 获取String
34 | * @param context 应用程序上下文
35 | * @param key key关键字
36 | * @param defValue 默认值
37 | * @return 返回获取的String值
38 | */
39 | public static String getString(Context context, String key, String defValue){
40 | SharedPreferences sp = getSharedPreferences(context);
41 | return sp.getString(key, defValue);
42 | }
43 |
44 | /**
45 | * 获取Set 集合
46 | * @param context 应用程序上下文
47 | * @param key key关键字
48 | * @param defValues 默认值
49 | * @return 返回Set值
50 | */
51 | public static Set getStringSet(Context context, String key, Set defValues){
52 | SharedPreferences sp = getSharedPreferences(context);
53 | return sp.getStringSet(key, defValues);
54 | }
55 |
56 | /**
57 | * 获取int值
58 | * @param context 应用程序上下文
59 | * @param key key关键字
60 | * @param defValue 默认值
61 | * @return 返回int值,
62 | */
63 | public static int getInt(Context context, String key, int defValue){
64 | SharedPreferences sp = getSharedPreferences(context);
65 | return sp.getInt(key, defValue);
66 | }
67 |
68 | /**
69 | * 获取float值
70 | * @param context 应用程序上下文
71 | * @param key key关键字
72 | * @param defValue 默认值
73 | * @return 返回float对应值
74 | */
75 | public static float getFloat(Context context, String key, float defValue){
76 | SharedPreferences sp = getSharedPreferences(context);
77 | return sp.getFloat(key, defValue);
78 | }
79 |
80 | /**
81 | * 获取Long类型值
82 | * @param context 应用程序上下文
83 | * @param key key关键字
84 | * @param defValue 默认值
85 | * @return 返回对应的long类型的值
86 | */
87 | public static long getLong(Context context, String key, long defValue){
88 | SharedPreferences sp = getSharedPreferences(context);
89 | return sp.getLong(key, defValue);
90 | }
91 |
92 | /**
93 | * 获取boolean类型的值
94 | * @param context 应用程序上下文
95 | * @param key key关键字
96 | * @param defValue 默认值
97 | * @return 返回boolean类型的值
98 | */
99 | public static boolean getBoolean(Context context, String key, boolean defValue){
100 | SharedPreferences sp = getSharedPreferences(context);
101 | return sp.getBoolean(key, defValue);
102 | }
103 |
104 | /**
105 | * 保存Stirng类型的值
106 | * @param context 应用程序上下文
107 | * @param key key关键字
108 | * @param value 对应的值
109 | * @return 成功返回true,失败返回false
110 | */
111 | public static boolean putString(Context context, String key, String value){
112 | return getEditor(context).putString(key, value).commit();
113 | }
114 |
115 | /**
116 | * 保存Set集合的值
117 | * @param context 应用程序上下文
118 | * @param key key关键字
119 | * @param value 对应值
120 | * @return 成功返回true,失败返回false
121 | */
122 | public static boolean putStringSet(Context context, String key, Set value){
123 | return getEditor(context).putStringSet(key, value).commit();
124 | }
125 |
126 | /**
127 | * 保存int类型的值
128 | * @param context 应用程序上下文
129 | * @param key key关键字
130 | * @param value 对应值
131 | * @return 成功返回true,失败返回false
132 | */
133 | public static boolean putInt(Context context, String key, int value){
134 | return getEditor(context).putInt(key, value).commit();
135 | }
136 |
137 | /**
138 | * 保存long类型的值
139 | * @param context 应用程序上下文
140 | * @param key key关键字
141 | * @param value 对应值
142 | * @return 成功返回true,失败返回false
143 | */
144 | public static boolean putLong(Context context, String key, long value){
145 | return getEditor(context).putLong(key, value).commit();
146 | }
147 |
148 | /**
149 | * 保存float类型的值
150 | * @param context 应用程序上下文
151 | * @param key key关键字
152 | * @param value 对应值
153 | * @return 成功返回true,失败返回false
154 | */
155 | public static boolean putFloat(Context context, String key, float value){
156 | return getEditor(context).putFloat(key, value).commit();
157 | }
158 |
159 | /**
160 | * 保存boolean类型的值
161 | * @param context 应用程序上下文
162 | * @param key key关键字
163 | * @param value 对应值
164 | * @return 成功返回true,失败返回false
165 | */
166 | public static boolean putBoolean(Context context, String key, boolean value){
167 | return getEditor(context).putBoolean(key, value).commit();
168 | }
169 |
170 | /**
171 | * 删除关键字key
172 | * @param context 应用程序上下文
173 | * @param key 关键字key
174 | * @return 成功返回true,失败返回false
175 | */
176 | public static boolean removeKey(Context context, String key){
177 | return getEditor(context).remove(key).commit();
178 | }
179 |
180 | /**
181 | * 清除所有的关键字
182 | * @param context 应用程序上下文
183 | * @return 成功返回true,失败返回false
184 | */
185 | public static boolean clearValues(Context context){
186 | return getEditor(context).clear().commit();
187 | }
188 |
189 |
190 | /**
191 | * 获取SharedPreferences对象
192 | * @param context 应用程序上下文
193 | * @return 返回SharedPreferences对象
194 | */
195 | private static SharedPreferences getSharedPreferences(Context context){
196 | return context.getSharedPreferences(SHARED_NAME, Context.MODE_PRIVATE);
197 | }
198 |
199 | /**
200 | * 获取Editor对象
201 | * @param context 应用程序上下文
202 | * @return 返回Editor对象
203 | */
204 | private static Editor getEditor(Context context){
205 | return getSharedPreferences(context).edit();
206 | }
207 | }
208 |
--------------------------------------------------------------------------------
/androidutils/src/main/java/com/dsw/androidutils/ReflectUtil.java:
--------------------------------------------------------------------------------
1 | package com.dsw.androidutils;
2 |
3 | import java.lang.reflect.Field;
4 | import java.lang.reflect.InvocationTargetException;
5 | import java.lang.reflect.Method;
6 | import java.lang.reflect.Modifier;
7 | import java.lang.reflect.Type;
8 |
9 | import android.text.TextUtils;
10 |
11 | /**
12 | * 反射工具类
13 | * @author Administrator
14 | *
15 | */
16 | public class ReflectUtil {
17 | private ReflectUtil(){};
18 |
19 | /**
20 | * 设置字段值
21 | * @param t 对应实体
22 | * @param field 字段
23 | * @param fieldName 字段名称
24 | * @param value 字段值
25 | */
26 | public static void setFieldValue(T t,Field field, String fieldName, String value){
27 | String name = field.getName();
28 | //判断该字段是否和目标字段相同
29 | if(!fieldName.equals(name))return;
30 | //获取字段的类型
31 | Type type = field.getType();
32 | //获取字段的修饰符号码
33 | int typeCode = field.getModifiers();
34 | //获取字段类型的名称
35 | String typeName = type.toString();
36 | try{
37 | switch(typeName){
38 | case "class java.lang.String":
39 | if(Modifier.isPublic(typeCode)){
40 | field.set(t, value);
41 | }else{
42 | Method method = t.getClass().getMethod("set" + getMethodName(fieldName),String.class);
43 | method.invoke(t, value);
44 | }
45 | break;
46 | case "double":
47 | if(Modifier.isPublic(typeCode)){
48 | field.setDouble(t, Double.valueOf(value));
49 | }else{
50 | Method method = t.getClass().getMethod("set" + getMethodName(fieldName),double.class);
51 | method.invoke(t, Double.valueOf(value));
52 | }
53 | break;
54 | case "int":
55 | if(Modifier.isPublic(typeCode)){
56 | field.setInt(t, Integer.valueOf(value));
57 | }else{
58 | Method method = t.getClass().getMethod("set" + getMethodName(fieldName),int.class);
59 | method.invoke(t, Integer.valueOf(value));
60 | }
61 | break;
62 | case "float":
63 | if(Modifier.isPublic(typeCode)){
64 | field.setFloat(t, Float.valueOf(value));
65 | }else{
66 | Method method = t.getClass().getMethod("set" + getMethodName(fieldName), float.class);
67 | method.invoke(t, Float.valueOf(value));
68 | }
69 | break;
70 | }
71 | }catch(NoSuchMethodException ex){
72 | ex.printStackTrace();
73 | } catch (NumberFormatException e) {
74 | e.printStackTrace();
75 | } catch (IllegalAccessException e) {
76 | e.printStackTrace();
77 | } catch (IllegalArgumentException e) {
78 | e.printStackTrace();
79 | } catch (InvocationTargetException e) {
80 | e.printStackTrace();
81 | } catch (Exception e) {
82 | e.printStackTrace();
83 | }
84 | }
85 |
86 | /**
87 | * 把字段名称第一个字母换成大写
88 | * @param fildeName 字段名称
89 | * @return
90 | * @throws Exception 异常处理
91 | */
92 | private static String getMethodName(String fildeName) throws Exception{
93 | byte[] items = fildeName.getBytes();
94 | items[0] = (byte) ((char) items[0] - 'a' + 'A');
95 | return new String(items);
96 | }
97 |
98 | /**
99 | * 根据字段名称获取指定Field字段
100 | * @param clazz 实体的字节码文件
101 | * @param filedName 字段的名称
102 | * @return 返回对应的字符按Field或者返回null
103 | */
104 | public static Field getField(Class> clazz, String filedName){
105 | if(clazz == null || TextUtils.isEmpty(filedName)){
106 | throw new IllegalArgumentException("params is illegal");
107 | }
108 | Field[] fields = clazz.getDeclaredFields();
109 | return getFieldByName(fields, filedName);
110 | }
111 |
112 | /**
113 | * 根据字段名称获取指定的Field
114 | * @param fields 字段集合
115 | * @param fieldName 字段名称
116 | * @return 返回对应的Field字段或者返回null
117 | */
118 | public static Field getFieldByName(Field[] fields, String fieldName){
119 | if(fields == null || fields.length ==0 || TextUtils.isEmpty(fieldName)){
120 | throw new IllegalArgumentException("params is illegal");
121 | }
122 | for(Field field : fields){
123 | String name = field.getName();
124 | //判断该字段是否和目标字段相同
125 | if(fieldName.equals(name)){
126 | return field;
127 | }
128 | }
129 | return null;
130 | }
131 |
132 | /**
133 | * 判断该字段是否为FieldName对应字段
134 | * @param field Field字段
135 | * @param fieldName 目标字段
136 | * @return 是,返回true;否,返回false
137 | */
138 | public static boolean isFiledWithName(Field field, String fieldName){
139 | if(field == null || TextUtils.isEmpty(fieldName)){
140 | throw new IllegalArgumentException("params is illegal");
141 | }
142 | if(fieldName.equals(field.getName())){
143 | return true;
144 | }
145 | return false;
146 | }
147 | }
148 |
--------------------------------------------------------------------------------
/androidutils/src/main/java/com/dsw/androidutils/SDCardUtil.java:
--------------------------------------------------------------------------------
1 | package com.dsw.androidutils;
2 |
3 | import java.io.File;
4 |
5 | import android.annotation.SuppressLint;
6 | import android.content.Context;
7 | import android.os.Environment;
8 | import android.os.StatFs;
9 |
10 | /**
11 | * SD卡工具类,包含SD卡状态、路径、容量大小
12 | * @author Administrator
13 | *
14 | */
15 | public class SDCardUtil {
16 |
17 | private SDCardUtil(){};
18 |
19 | /**
20 | * 判断SD卡是否可用
21 | * @return
22 | * ture:可用;false:不可用
23 | */
24 | public static boolean isSDCardEnable(){
25 | return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
26 | }
27 |
28 | /**
29 | * 获取SD卡路径
30 | * @return
31 | * SD卡存在返回正常路径;SD卡不存在返回""
32 | */
33 | public static String getSDCradPath(){
34 | if(isSDCardEnable()){
35 | return Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
36 | }else{
37 | return "";
38 | }
39 | }
40 |
41 | /**
42 | * 获取SD卡路径
43 | * @return
44 | * SD卡存在返回正常路径;SD卡不存在返回null
45 | */
46 | public static File getSDCardFile(){
47 | if(isSDCardEnable()){
48 | return Environment.getExternalStorageDirectory();
49 | }else{
50 | return null;
51 | }
52 | }
53 |
54 | /**
55 | * 获取SD卡DownloadCache路径
56 | * @return
57 | * SD卡存在返回正常路径;SD卡不存在返回""
58 | */
59 | public static String getSDCardDownloadCachePath(){
60 | if(isSDCardEnable()){
61 | return Environment.getDownloadCacheDirectory().getAbsolutePath() + File.separator;
62 | }else{
63 | return "";
64 | }
65 | }
66 |
67 | /**
68 | * 获取SD卡DownloadCache路径
69 | * @return
70 | * SD卡存在返回正常路径;SD卡不存在返回null
71 | */
72 | public static File getSDCardDownloadCacheFile(){
73 | if(isSDCardEnable()){
74 | return Environment.getDownloadCacheDirectory();
75 | }else{
76 | return null;
77 | }
78 | }
79 |
80 | /**
81 | * 获取系统存储路径
82 | * @return
83 | * SD卡存在返回正常路径;SD卡不存在返回""
84 | */
85 | public static String getSDCardRootPath(){
86 | if(isSDCardEnable()){
87 | return Environment.getRootDirectory().getAbsolutePath() + File.separator;
88 | }else{
89 | return "";
90 | }
91 | }
92 |
93 | /**
94 | * 获取系统存储路径
95 | * @return
96 | * SD卡存在返回正常路径;SD卡不存在返回null
97 | */
98 | public static File getSDCardRootFile(){
99 | if(isSDCardEnable()){
100 | return Environment.getRootDirectory();
101 | }else{
102 | return null;
103 | }
104 | }
105 |
106 | /**
107 | * 获取应用程序的/data/data目录
108 | * @param context
109 | * @return
110 | */
111 | public static String getDataFilePath(Context context){
112 | return context.getFilesDir().getAbsolutePath() + File.separator;
113 | }
114 |
115 | /**
116 | * /data/data/PackageName/cache的路径
117 | * @param context
118 | * @return
119 | */
120 | public static String getDataCachePath(Context context){
121 | return context.getCacheDir().getAbsolutePath() + File.separator;
122 | }
123 |
124 | /**
125 | * 获取SD卡大小
126 | * @return
127 | * SD卡存在返回大小;SD卡不存在返回-1
128 | */
129 | @SuppressLint("NewApi")
130 | public static long getSDCardSize(){
131 | if(isSDCardEnable()){
132 | StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator);
133 | if(android.os.Build.VERSION.SDK_INT < 18){
134 | int blockSize = statFs.getBlockSize();
135 | int blockCount = statFs.getBlockCount();
136 | return blockCount * blockSize;
137 | }else{
138 | long blockSize = statFs.getBlockSizeLong();
139 | long blockCount = statFs.getBlockCountLong();
140 | return blockCount * blockSize;
141 | }
142 | }
143 | return -1;
144 | }
145 |
146 | /**
147 | * 获取SD卡大小
148 | * @return
149 | * SD卡存在返回大小;SD卡不存在返回-1
150 | */
151 | @SuppressLint("NewApi")
152 | public static long getSDCardAvailableSize(){
153 | if(isSDCardEnable()){
154 | StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator);
155 | if(android.os.Build.VERSION.SDK_INT < 18){
156 | int blockSize = statFs.getBlockSize();
157 | int blockCount = statFs.getAvailableBlocks();
158 | return blockCount * blockSize;
159 | }else{
160 | long blockSize = statFs.getBlockSizeLong();
161 | long blockCount = statFs.getAvailableBlocksLong();
162 | return blockCount * blockSize;
163 | }
164 | }
165 | return -1;
166 | }
167 |
168 | /**
169 | * 获得手机内存总大小
170 | * @return
171 | */
172 | @SuppressLint("NewApi")
173 | public long getRomTotalSize() {
174 | File path = Environment.getDataDirectory();
175 | StatFs statFs = new StatFs(path.getPath());
176 | if(android.os.Build.VERSION.SDK_INT < 18){
177 | int blockSize = statFs.getBlockSize();
178 | int blockCount = statFs.getAvailableBlocks();
179 | return blockCount * blockSize;
180 | }else{
181 | long blockSize = statFs.getBlockSizeLong();
182 | long blockCount = statFs.getAvailableBlocksLong();
183 | return blockCount * blockSize;
184 | }
185 | }
186 |
187 | /**
188 | * 获得手机可用内存
189 | * @return
190 | */
191 | @SuppressLint("NewApi")
192 | public long getRomAvailableSize() {
193 | File path = Environment.getDataDirectory();
194 | StatFs statFs = new StatFs(path.getPath());
195 | if(android.os.Build.VERSION.SDK_INT < 18){
196 | int blockSize = statFs.getBlockSize();
197 | int blockCount = statFs.getAvailableBlocks();
198 | return blockCount * blockSize;
199 | }else{
200 | long blockSize = statFs.getBlockSizeLong();
201 | long blockCount = statFs.getAvailableBlocksLong();
202 | return blockCount * blockSize;
203 | }
204 | }
205 | }
206 |
--------------------------------------------------------------------------------
/androidutils/src/main/java/com/dsw/androidutils/ScreenUtil.java:
--------------------------------------------------------------------------------
1 | package com.dsw.androidutils;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.graphics.Bitmap;
6 | import android.graphics.Rect;
7 | import android.util.DisplayMetrics;
8 | import android.view.View;
9 | import android.view.WindowManager;
10 |
11 | /**
12 | * 屏幕工具类,涉及到屏幕宽度、高度、密度比、(像素、dp、sp)之间的转换等。
13 | * @author Administrator
14 | *
15 | */
16 | public class ScreenUtil {
17 | private ScreenUtil(){};
18 |
19 | /**
20 | * 获取屏幕宽度,单位为px
21 | * @param context 应用程序上下文
22 | * @return
23 | * 屏幕宽度,单位px
24 | */
25 | public static int getScreenWidth(Context context){
26 | return getDisplayMetrics(context).widthPixels;
27 | }
28 |
29 | /**
30 | * 获取屏幕高度,单位为px
31 | * @param context 应用程序上下文
32 | * @return
33 | * 屏幕高度,单位px
34 | */
35 | public static int getScreenHeight(Context context){
36 | return getDisplayMetrics(context).heightPixels;
37 | }
38 |
39 | /**
40 | * 获取系统dp尺寸密度值
41 | * @param context 应用程序上下文
42 | * @return
43 | */
44 | public static float getDensity(Context context){
45 | return getDisplayMetrics(context).density;
46 | }
47 |
48 | /**
49 | * 获取系统字体sp密度值
50 | * @param context 应用程序上下文
51 | * @return
52 | */
53 | public static float getScaledDensity(Context context){
54 | return getDisplayMetrics(context).scaledDensity;
55 | }
56 |
57 | /**
58 | * dip转换为px大小
59 | * @param context 应用程序上下文
60 | * @param dpValue dp值
61 | * @return 转换后的px值
62 | */
63 | public static int dp2px(Context context, int dpValue){
64 | return (int) (dpValue * getDensity(context) + 0.5f);
65 | }
66 |
67 | /**
68 | * px转换为dp值
69 | * @param context 应用程序上下文
70 | * @param pxValue px值
71 | * @return 转换后的dp值
72 | */
73 | public static int px2dp(Context context, int pxValue){
74 | return (int) (pxValue / getDensity(context) + 0.5f);
75 | }
76 |
77 | /**
78 | * sp转换为px
79 | * @param context 应用程序上下文
80 | * @param spValue sp值
81 | * @return 转换后的px值
82 | */
83 | public static int sp2px(Context context, int spValue){
84 | return (int) (spValue * getScaledDensity(context) + 0.5f);
85 | }
86 |
87 | /**
88 | * px转换为sp
89 | * @param context 应用程序上下文
90 | * @param pxValue px值
91 | * @return 转换后的sp值
92 | */
93 | public static int px2sp(Context context, int pxValue){
94 | return (int) (pxValue / getScaledDensity(context) + 0.5f);
95 | }
96 |
97 |
98 | /**
99 | * 获得状态栏的高度
100 | *
101 | * @param context
102 | * @return
103 | */
104 | public static int getStatusHeight(Context context){
105 | int statusHeight = -1;
106 | try{
107 | Class> clazz = Class.forName("com.android.internal.R$dimen");
108 | Object object = clazz.newInstance();
109 | int height = Integer.parseInt(clazz.getField("status_bar_height")
110 | .get(object).toString());
111 | statusHeight = context.getResources().getDimensionPixelSize(height);
112 | } catch (Exception e){
113 | e.printStackTrace();
114 | }
115 | return statusHeight;
116 | }
117 |
118 | /**
119 | * 获取当前屏幕截图,包含状态栏
120 | * @param activity
121 | * @return
122 | */
123 | public static Bitmap snapShotWithStatusBar(Activity activity){
124 | View decorView = activity.getWindow().getDecorView();
125 | decorView.setDrawingCacheEnabled(true);
126 | decorView.buildDrawingCache();
127 | Bitmap bmp = decorView.getDrawingCache();
128 | int width = getScreenWidth(activity);
129 | int height = getScreenHeight(activity);
130 | Bitmap bitMap = null;
131 | bitMap = Bitmap.createBitmap(bmp, 0, 0, width, height);
132 | decorView.destroyDrawingCache();
133 | return bitMap;
134 | }
135 |
136 | /**
137 | * 获取当前屏幕截图,不包含状态栏
138 | * @param activity
139 | * @return
140 | */
141 | public static Bitmap snapShotWithoutStatusBar(Activity activity){
142 | View decorView = activity.getWindow().getDecorView();
143 | decorView.setDrawingCacheEnabled(true);
144 | decorView.buildDrawingCache();
145 | Bitmap bmp = decorView.getDrawingCache();
146 | Rect frame = new Rect();
147 | activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
148 | int statusBarHeight = frame.top;
149 |
150 | int width = getScreenWidth(activity);
151 | int height = getScreenHeight(activity);
152 | Bitmap bitMap = null;
153 | bitMap = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height
154 | - statusBarHeight);
155 | decorView.destroyDrawingCache();
156 | return bitMap;
157 | }
158 |
159 | /**
160 | * 获取DisplayMetrics对象
161 | * @param context 应用程序上下文
162 | * @return
163 | */
164 | public static DisplayMetrics getDisplayMetrics(Context context){
165 | WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
166 | DisplayMetrics displayMetrics = new DisplayMetrics();
167 | windowManager.getDefaultDisplay().getMetrics(displayMetrics);
168 | return displayMetrics;
169 | }
170 | }
171 |
--------------------------------------------------------------------------------
/androidutils/src/main/java/com/dsw/androidutils/ToastUtil.java:
--------------------------------------------------------------------------------
1 | package com.dsw.androidutils;
2 |
3 | import android.content.Context;
4 | import android.widget.Toast;
5 |
6 | /**
7 | * Toast工具类
8 | * @author Administrator
9 | *
10 | */
11 | public class ToastUtil {
12 |
13 | private ToastUtil(){};
14 |
15 | /**
16 | * 长时Toast
17 | * @param context
18 | * @param msg
19 | */
20 | public static void showLongToast(Context context, String msg){
21 | Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
22 | }
23 |
24 | /**
25 | * 短时Toast
26 | * @param context
27 | * @param msg
28 | */
29 | public static void showShortToast(Context context, String msg){
30 | Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/androidutils/src/main/java/com/dsw/androidutils/XMLUtil.java:
--------------------------------------------------------------------------------
1 | package com.dsw.androidutils;
2 |
3 | import java.io.ByteArrayInputStream;
4 | import java.io.IOException;
5 | import java.io.InputStream;
6 | import java.lang.reflect.Field;
7 | import java.lang.reflect.InvocationTargetException;
8 | import java.lang.reflect.Method;
9 | import java.lang.reflect.Modifier;
10 | import java.lang.reflect.Type;
11 | import java.util.ArrayList;
12 | import java.util.List;
13 |
14 | import org.xmlpull.v1.XmlPullParser;
15 | import org.xmlpull.v1.XmlPullParserException;
16 |
17 | import android.text.TextUtils;
18 | import android.util.Xml;
19 |
20 | /**
21 | * XML文件工具类,包含:将xml文件解析成实体集合、获取xml标签值、将标签值解析成实体集合
22 | * @author Administrator
23 | *
24 | */
25 | public class XMLUtil {
26 | private XMLUtil(){};
27 |
28 | /*-
29 | * XML文件解析成实体,不涉及到标签的属性值。
30 | * @param xml xml字符串文件
31 | * @param clazz 对应实体的class文件
32 | * @param tagEntity
33 | * 开始解析实体的标签,例如下面的实例中就是student
34 | * < person >
35 | * < student >
36 | * < name >Lucy< /name >
37 | * < age >21< /age >
38 | * < /student >
39 | * < /person >
40 | * @return 返回解析的对应实体文件
41 | */
42 | public static List xmlToObject(String xml, Class clazz, String tagEntity){
43 | List list = null;
44 | XmlPullParser xmlPullParser = Xml.newPullParser();
45 | InputStream inputStream = new ByteArrayInputStream(xml.getBytes());
46 | try {
47 | xmlPullParser.setInput(inputStream, "utf-8");
48 | Field[] fields = clazz.getDeclaredFields();
49 | int type = xmlPullParser.getEventType();
50 | String lastTag = "";
51 | T t = null;
52 | while(type != XmlPullParser.END_DOCUMENT){
53 | switch(type){
54 | case XmlPullParser.START_DOCUMENT:
55 | list = new ArrayList();
56 | break;
57 | case XmlPullParser.START_TAG:
58 | String tagName = xmlPullParser.getName();
59 | if(tagEntity.equals(tagName)){
60 | t = clazz.newInstance();
61 | lastTag = tagEntity;
62 | }else if(tagEntity.equals(lastTag)){
63 | String textValue = xmlPullParser.nextText();
64 | String fieldName = xmlPullParser.getName();
65 | for(Field field : fields){
66 | ReflectUtil.setFieldValue(t,field,fieldName,textValue);
67 | }
68 | }
69 | break;
70 | case XmlPullParser.END_TAG:
71 | tagName = xmlPullParser.getName();
72 | if(tagEntity.equals(tagName)){
73 | list.add(t);
74 | lastTag = "";
75 | }
76 | break;
77 | case XmlPullParser.END_DOCUMENT:
78 | break;
79 | }
80 | type = xmlPullParser.next();
81 | }
82 | } catch (XmlPullParserException e) {
83 | e.printStackTrace();
84 | } catch (IOException e) {
85 | e.printStackTrace();
86 | } catch (InstantiationException e) {
87 | e.printStackTrace();
88 | } catch (IllegalAccessException e) {
89 | e.printStackTrace();
90 | } catch (Exception e) {
91 | e.printStackTrace();
92 | }
93 | return list;
94 | }
95 |
96 | /**
97 | * 获取xml字符串标签中的属性值
98 | * @param xml xml字符串
99 | * @param clazz 转换成对应的实体
100 | * @param tagName 实体对应xml字符串的起始标签,如下面实例中的person标签
101 | * < person name="Lucy" age="12">
102 | * < student >
103 | * < name >Lucy< /name >
104 | * < age >21< /age >
105 | * < /student >
106 | * < /person >
107 | * @return 返回属性值组成的List对象集合。
108 | */
109 | public static List attributeToObject(String xml, Class clazz, String tagName){
110 | if(TextUtils.isEmpty(tagName))return null;
111 | List list = null;
112 | XmlPullParser xmlPullParser = Xml.newPullParser();
113 | InputStream inputStream = new ByteArrayInputStream(xml.getBytes());
114 | try {
115 | xmlPullParser.setInput(inputStream, "utf-8");
116 | int type = xmlPullParser.getEventType();
117 | T t = null;
118 | while(type != XmlPullParser.END_DOCUMENT){
119 | switch(type){
120 | case XmlPullParser.START_DOCUMENT:
121 | list = new ArrayList();
122 | break;
123 | case XmlPullParser.START_TAG:
124 | if(tagName.equals(xmlPullParser.getName())){
125 | t = clazz.newInstance();
126 | Field[] fields = clazz.getDeclaredFields();
127 | for(Field field : fields){
128 | String fieldName = field.getName();
129 | for(int index = 0;index < xmlPullParser.getAttributeCount();index++){
130 | if(fieldName.equals(xmlPullParser.getAttributeName(index))){
131 | ReflectUtil.setFieldValue(t,field,fieldName,xmlPullParser.getAttributeValue(index));
132 | }
133 | }
134 | }
135 | }
136 | break;
137 | case XmlPullParser.END_TAG:
138 | if(tagName.equals(xmlPullParser.getName())){
139 | list.add(t);
140 | }
141 | break;
142 | case XmlPullParser.END_DOCUMENT:
143 | break;
144 | }
145 | type = xmlPullParser.next();
146 | }
147 | }catch(Exception ex){
148 | ex.printStackTrace();
149 | }
150 | return list;
151 |
152 | }
153 |
154 | /**
155 | * 获取Xml文件中的属性值
156 | * @param xml xml文件字符串
157 | * @param tagName 标签名称
158 | * @param attributeName 属性名称
159 | * @return 返回获取的值,或者null
160 | */
161 | public static String getTagAttribute(String xml, String tagName, String attributeName){
162 | if(TextUtils.isEmpty(tagName) || TextUtils.isEmpty(attributeName)){
163 | throw new IllegalArgumentException("请填写标签名称或属性名称");
164 | }
165 | XmlPullParser xmlPullParser = Xml.newPullParser();
166 | InputStream inputStream = new ByteArrayInputStream(xml.getBytes());
167 | try {
168 | xmlPullParser.setInput(inputStream, "utf-8");
169 | int type = xmlPullParser.getEventType();
170 | while(type != XmlPullParser.END_DOCUMENT){
171 | switch(type){
172 | case XmlPullParser.START_TAG:
173 | if(tagName.equals(xmlPullParser.getName())){
174 | for(int i=0; i < xmlPullParser.getAttributeCount();i++){
175 | if(attributeName.equals(xmlPullParser.getAttributeName(i))){
176 | return xmlPullParser.getAttributeValue(i);
177 | }
178 | }
179 | }
180 | break;
181 | }
182 | type = xmlPullParser.next();
183 | }
184 | } catch (XmlPullParserException e) {
185 | e.printStackTrace();
186 | } catch (IOException e) {
187 | e.printStackTrace();
188 | }
189 | return null;
190 | }
191 | }
192 |
--------------------------------------------------------------------------------
/androidutils/src/main/java/com/dsw/androidutils/database/AnnotationColumn.java:
--------------------------------------------------------------------------------
1 | package com.dsw.androidutils.database;
2 |
3 | import java.lang.annotation.ElementType;
4 | import java.lang.annotation.Retention;
5 | import java.lang.annotation.RetentionPolicy;
6 | import java.lang.annotation.Target;
7 |
8 | @Target(ElementType.FIELD)
9 | @Retention(RetentionPolicy.RUNTIME)
10 | public @interface AnnotationColumn {
11 | /**
12 | * 字段名称
13 | * @return
14 | */
15 | String name() default "";
16 | /**
17 | * 字段类型
18 | * @return
19 | */
20 | String type();
21 | /**
22 | * 是否为空
23 | * @return
24 | */
25 | boolean isNull() default false;
26 | /**
27 | * 是否为主键
28 | * @return
29 | */
30 | boolean isPrimaryKey() default false;
31 | }
32 |
--------------------------------------------------------------------------------
/androidutils/src/main/java/com/dsw/androidutils/database/AnnotationTable.java:
--------------------------------------------------------------------------------
1 | package com.dsw.androidutils.database;
2 |
3 | import java.lang.annotation.ElementType;
4 | import java.lang.annotation.Retention;
5 | import java.lang.annotation.RetentionPolicy;
6 | import java.lang.annotation.Target;
7 |
8 | @Target(ElementType.TYPE)
9 | @Retention(RetentionPolicy.RUNTIME)
10 | public @interface AnnotationTable {
11 | //表的名称
12 | String name() default "";
13 | }
14 |
--------------------------------------------------------------------------------
/androidutils/src/main/java/com/dsw/androidutils/database/DataBaseHelper.java:
--------------------------------------------------------------------------------
1 | package com.dsw.androidutils.database;
2 |
3 | import java.lang.reflect.Field;
4 | import java.util.ArrayList;
5 | import java.util.List;
6 |
7 | import android.content.ContentValues;
8 | import android.content.Context;
9 | import android.database.Cursor;
10 | import android.database.SQLException;
11 | import android.database.sqlite.SQLiteDatabase;
12 | import android.text.TextUtils;
13 | import android.util.Log;
14 |
15 | import com.dsw.androidutils.LogUtil;
16 | import com.dsw.androidutils.ReflectUtil;
17 |
18 |
19 | /**
20 | * 数据库工具类,包含数据库的创建、表的创建、增删改查
21 | * @author Administrator
22 | *
23 | */
24 | public class DataBaseHelper {
25 | private static final String TAG = DataBaseHelper.class.getSimpleName();
26 | private SQLiteHelper mSQlLiteHelper;
27 | private SQLiteDatabase mSqLiteDatabaseReadable,mSqLiteDatabaseWritable;
28 |
29 | /**
30 | * 在构造函数中创建数据库,方便下次使用
31 | * @param context 应用程序上下文
32 | * @param dataBaseName 数据库名称
33 | * @param version 数据库版本
34 | */
35 | public DataBaseHelper(Context context,String dataBaseName, int version){
36 | if(context == null || TextUtils.isEmpty(dataBaseName) || version < 1){
37 | throw new IllegalArgumentException("please check your params,some params is null | empty or version code <1");
38 | }
39 | mSQlLiteHelper = new SQLiteHelper(context, dataBaseName, null, version);
40 | mSqLiteDatabaseReadable = mSQlLiteHelper.getReadableDatabase();
41 | mSqLiteDatabaseWritable = mSQlLiteHelper.getWritableDatabase();
42 | };
43 |
44 | public SQLiteDatabase getmSqLiteDatabaseReadable() {
45 | return mSqLiteDatabaseReadable;
46 | }
47 |
48 | public SQLiteDatabase getmSqLiteDatabaseWritable() {
49 | return mSqLiteDatabaseWritable;
50 | }
51 |
52 | public SQLiteHelper getmSQlLiteHelper() {
53 | return mSQlLiteHelper;
54 | }
55 |
56 | /**
57 | * 创建数据表,该套逻辑是基于标注解和字段注解进行判断,由于在开发过程中,我们需要创建实体,
58 | * 所以只需要在对应的字段上加上注解就可以了。
59 | * @param tableClassList 需要创建表的实体类集合
60 | */
61 | public void createTables(List> tableClassList){
62 | if(tableClassList == null || tableClassList.size() == 0){
63 | throw new IllegalArgumentException("please check your params,some params is null or empty");
64 | }
65 |
66 | for(Class> clazz : tableClassList){
67 | StringBuilder sbCreateSQL = new StringBuilder();
68 | sbCreateSQL.append("CREATE TABLE IF NOT EXISTS ");
69 | if(clazz.isAnnotationPresent(AnnotationTable.class)){//判断是否为AnotationTable注解类型
70 | //获取AnnotationTable注解
71 | AnnotationTable tableAnno = clazz.getAnnotation(AnnotationTable.class);
72 | //获取AnnotatbleTable注解的属性值,即表名
73 | String tableName = tableAnno.name();
74 | sbCreateSQL.append(tableName + "(");
75 | //通过反射获取Person实体的成员变量,判断哪些是注解要被创建成Person表中的字段
76 | Field[] fields = clazz.getDeclaredFields();
77 | for(int i=0;i List queryList(String sql, String[] whereArgs, Class clazz ){
187 |
188 | Cursor cursor = queryCursor(sql, whereArgs);
189 | return cursorToList(cursor, clazz);
190 | }
191 |
192 | /**
193 | * 查询第一个记录,queryFirst(sqLiteHelper,"SELECT * FROM table WHERE name=?", new String[]("android"), table.class);
194 | * @param sql 查询SQL语句
195 | * @param whereArgs 查询参数
196 | * @param clazz 实体Class
197 | * @return 返回对应实体
198 | */
199 | public T queryFirst(String sql, String[] whereArgs, Class clazz){
200 | if(TextUtils.isEmpty(sql) || clazz == null){
201 | throw new IllegalArgumentException("please check your params,some params is null or empty");
202 | }
203 | Cursor cursor = queryCursor(sql, whereArgs);
204 | List list = cursorToList(cursor, clazz);
205 | if(list != null && list.size() >0)return list.get(0);
206 | return null;
207 | }
208 |
209 | /**
210 | * 开启事务
211 | */
212 | public void beginTranslaction(){
213 | mSqLiteDatabaseReadable.beginTransaction();
214 | mSqLiteDatabaseWritable.beginTransaction();
215 | }
216 |
217 | /**
218 | * 设置事务处理成功
219 | */
220 | public void setTransactionSuccessful(){
221 | mSqLiteDatabaseReadable.setTransactionSuccessful();
222 | mSqLiteDatabaseWritable.setTransactionSuccessful();
223 | }
224 |
225 | /**
226 | * 设置事务处理结束
227 | */
228 | public void endTransaction(){
229 | mSqLiteDatabaseReadable.endTransaction();
230 | mSqLiteDatabaseWritable.endTransaction();
231 | }
232 |
233 | /**
234 | * 获取某个表中的记录条数
235 | * @param tableName 表名称
236 | * @return 返回查询的记录条数,查询异常,则返回0
237 | */
238 | public int getCountOfTable(String tableName){
239 | if(TextUtils.isEmpty(tableName)){
240 | throw new IllegalArgumentException("please check you param,param is null or empty");
241 | }
242 |
243 | StringBuilder sbSQL = new StringBuilder();
244 | sbSQL.append("SELECT COUNT(*) FROM ")
245 | .append(tableName);
246 | Cursor cursor = queryCursor(sbSQL.toString(), null);
247 | return cursor == null ? 0: cursor.getCount();
248 | }
249 |
250 | /**
251 | * 根据SQL语句,获取查询结果的条数
252 | * @param sql SQL语句
253 | * @param whereArgs 对应参数的值
254 | * @return 返回查询的记录条数,查询异常,则返回0
255 | */
256 | public int getCount(String sql,String[] whereArgs){
257 | Cursor cursor = queryCursor(sql, whereArgs);
258 | return cursor == null ? 0: cursor.getCount();
259 | }
260 |
261 | /**
262 | * 删除表,从数据库中删除该表
263 | * @param tableName 表名
264 | */
265 | public void dropTable(String tableName){
266 | if(TextUtils.isEmpty(tableName)){
267 | throw new IllegalArgumentException("please check you param,param is null or empty");
268 | }
269 |
270 | String sqlDrop = "DROP TABLE " + tableName;
271 | executeSQL(sqlDrop);
272 | }
273 |
274 | /**
275 | * 清除表中所有数据
276 | * @param tableName 表名
277 | */
278 | public void clearTableData(String tableName){
279 | if(TextUtils.isEmpty(tableName)){
280 | throw new IllegalArgumentException("please check you param,param is null or empty");
281 | }
282 |
283 | String sqlDrop = "DELETE FROM " + tableName;
284 | executeSQL(sqlDrop);
285 | }
286 |
287 |
288 |
289 |
290 | /**
291 | * 执行SQL语句
292 | * @param sql SQL语句
293 | */
294 | private void executeSQL(String sql){
295 | if(TextUtils.isEmpty(sql)){
296 | throw new IllegalArgumentException("please check your param,param is null or empty");
297 | }
298 |
299 | try{
300 | mSqLiteDatabaseWritable.execSQL(sql);
301 | }catch(SQLException sqlException){
302 | LogUtil.e(TAG, sql + "\n" + "is not illegal.");
303 | throw new IllegalArgumentException("please check your SQL:" + sql);
304 | }
305 | }
306 |
307 | /**
308 | * 将Cursor转换成对应的结果集
309 | * @param cursor 游标
310 | * @param clazz Class文件
311 | * @return 返回集合
312 | */
313 | private static List cursorToList(Cursor cursor,Class clazz){
314 | if(cursor == null)return null;
315 | List list = new ArrayList();
316 | Field[] fields = clazz.getDeclaredFields();
317 | T t;
318 | try {
319 | while(cursor.moveToNext()){
320 | //每一行为一个实体,新建一个实体
321 | t = clazz.newInstance();
322 | String[] columnNames = cursor.getColumnNames();
323 | for(String columnName : columnNames){
324 | String fieldValue = cursor.getString(cursor.getColumnIndex(columnName));
325 | for(Field field : fields){
326 | if(ReflectUtil.isFiledWithName(field, columnName)){
327 | ReflectUtil.setFieldValue(t, field, columnName, fieldValue);
328 | break;
329 | }
330 | }
331 | }
332 | list.add(t);
333 | }
334 | return list;
335 | } catch (InstantiationException e) {
336 | e.printStackTrace();
337 | } catch (IllegalAccessException e) {
338 | e.printStackTrace();
339 | }finally{
340 | cursor.close();
341 | }
342 | return null;
343 | }
344 | }
345 |
--------------------------------------------------------------------------------
/androidutils/src/main/java/com/dsw/androidutils/database/SQLiteHelper.java:
--------------------------------------------------------------------------------
1 | package com.dsw.androidutils.database;
2 |
3 | import android.content.Context;
4 | import android.database.sqlite.SQLiteDatabase;
5 | import android.database.sqlite.SQLiteDatabase.CursorFactory;
6 | import android.database.sqlite.SQLiteOpenHelper;
7 |
8 | public class SQLiteHelper extends SQLiteOpenHelper {
9 |
10 | /**
11 | * 构造函数,用于创建数据库
12 | * @param context 应用程序上下文
13 | * @param name 数据库名称
14 | * @param factory
15 | * @param version 版本号
16 | */
17 | public SQLiteHelper(Context context, String name, CursorFactory factory,
18 | int version) {
19 | super(context, name, factory, version);
20 | }
21 |
22 | @Override
23 | public void onCreate(SQLiteDatabase db) {
24 |
25 | }
26 |
27 | @Override
28 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
29 |
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/androidutils/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | AndroidUtils
3 |
4 |
--------------------------------------------------------------------------------
/androidutils/src/test/java/com/dsw/androidutils/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.dsw.androidutils;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 23
5 | buildToolsVersion '23.0.2'
6 |
7 | defaultConfig {
8 | applicationId "com.utils.dsw.androidutils"
9 | minSdkVersion 11
10 | targetSdkVersion 23
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(include: ['*.jar'], dir: 'libs')
24 | testCompile 'junit:junit:4.12'
25 | compile 'com.android.support:appcompat-v7:23.1.1'
26 | compile project(':androidutils')
27 | }
28 |
--------------------------------------------------------------------------------
/app/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:\JavaSoftware\AndroidStudio\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 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/utils/dsw/androidutils/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.utils.dsw.androidutils;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/app/src/main/java/com/utils/dsw/androidutils/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.utils.dsw.androidutils;
2 |
3 | import android.app.Activity;
4 | import android.graphics.Color;
5 | import android.support.v7.app.AppCompatActivity;
6 | import android.os.Bundle;
7 | import android.view.Gravity;
8 | import android.view.View;
9 | import android.view.ViewGroup;
10 | import android.widget.AdapterView;
11 | import android.widget.ArrayAdapter;
12 | import android.widget.BaseAdapter;
13 | import android.widget.ImageView;
14 | import android.widget.LinearLayout;
15 | import android.widget.ListView;
16 | import android.widget.TextView;
17 |
18 | import com.dsw.androidutils.AppUtil;
19 | import com.dsw.androidutils.BitmapUtil;
20 | import com.dsw.androidutils.DateUtil;
21 | import com.dsw.androidutils.JsonUtil;
22 | import com.dsw.androidutils.MeasureUtil;
23 | import com.dsw.androidutils.NetWorkUtil;
24 | import com.dsw.androidutils.SDCardUtil;
25 | import com.dsw.androidutils.ScreenUtil;
26 | import com.dsw.androidutils.ToastUtil;
27 | import com.dsw.androidutils.XMLUtil;
28 |
29 | import java.util.ArrayList;
30 | import java.util.List;
31 |
32 | public class MainActivity extends Activity {
33 | private MainActivity _this;
34 | private ListView listView;
35 | private MyAdapter adapter;
36 | private LinearLayout linearLayout;
37 | @Override
38 | protected void onCreate(Bundle savedInstanceState) {
39 | super.onCreate(savedInstanceState);
40 | setContentView(R.layout.activity_main);
41 | _this = this;
42 | listView = (ListView) findViewById(R.id.list_View);
43 | linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
44 | initDateAndListener();
45 | }
46 |
47 | private void initDateAndListener(){
48 | List list = new ArrayList();
49 | list.add("AppUtil实例"); list.add("BitmapUtil实例"); list.add("DateUtil实例"); list.add("JsonUtil实例");
50 | list.add("MeasureUtil实例"); list.add("NetWorkUtil实例"); list.add("PreferencesUtil实例"); list.add("ScreenUtil实例");
51 | list.add("SDCardUtil实例"); list.add("XMLUtil实例"); list.add("DataBaseHelper实例"); list.add("ReflectUtil实例");
52 | adapter = new MyAdapter(list);
53 | listView.setAdapter(adapter);
54 | listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
55 | @Override
56 | public void onItemClick(AdapterView> parent, View view, int position, long id) {
57 | doActionItemClick(adapter.getItem(position).toString());
58 | }
59 | });
60 | }
61 |
62 | class MyAdapter extends BaseAdapter{
63 | List list;
64 | public MyAdapter(List list){
65 | this.list = list;
66 | }
67 |
68 | @Override
69 | public int getCount() {
70 | return list.size();
71 | }
72 |
73 | @Override
74 | public Object getItem(int position) {
75 | return list.get(position);
76 | }
77 |
78 | @Override
79 | public long getItemId(int position) {
80 | return position;
81 | }
82 |
83 | @Override
84 | public View getView(int position, View convertView, ViewGroup parent) {
85 | TextView tv = new TextView(_this);
86 | tv.setText(list.get(position));
87 | tv.setTextColor(Color.parseColor("#000000"));
88 | tv.setTextSize(17);
89 | tv.setGravity(Gravity.CENTER_VERTICAL);
90 | tv.setPadding(5,5,5,5);
91 | return tv;
92 | }
93 | }
94 |
95 | private void doActionItemClick(String name){
96 | linearLayout.removeAllViews();
97 | switch (name){
98 | case "AppUtil实例":
99 | TextView textView = new TextView(_this);
100 | StringBuffer stringBuffer = new StringBuffer();
101 | stringBuffer.append(AppUtil.getPacketName(_this) + "\n")
102 | .append(AppUtil.getVersionCode(_this) + "\n")
103 | .append(AppUtil.getVersionName(_this) + "\n");
104 | textView.setText(stringBuffer.toString());
105 | ImageView imageView = new ImageView(_this);
106 | imageView.setBackgroundDrawable(AppUtil.getApplicationIcon(_this));
107 | linearLayout.addView(textView);
108 | linearLayout.addView(imageView);
109 | break;
110 | case "BitmapUtil实例":
111 | ImageView image = new ImageView(_this);
112 | image.setLayoutParams(new ViewGroup.LayoutParams(200,200));
113 | MeasureUtil.setHeight(image,ScreenUtil.px2dp(_this, 100));
114 | MeasureUtil.setWidth(image, ScreenUtil.px2dp(_this, 100));
115 | image.setImageBitmap(BitmapUtil.getBitmapFromResource(_this, R.mipmap.src, ScreenUtil.px2dp(_this, 100), ScreenUtil.px2dp(_this, 100)));
116 | linearLayout.addView(image);
117 | break;
118 | case "DateUtil实例":
119 | TextView textViewDate = new TextView(_this);
120 | textViewDate.setText(DateUtil.getNowDate(DateUtil.DatePattern.ALL_TIME));
121 | linearLayout.addView(textViewDate);
122 | break;
123 | case "JsonUtil实例":
124 | Student student = new Student();
125 | student.setAge(21);
126 | student.setName("soul");
127 | TextView textViewJson = new TextView(_this);
128 | textViewJson.setText(JsonUtil.objectToJson(student));
129 | linearLayout.addView(textViewJson);
130 | break;
131 | case "MeasureUtil实例":
132 | break;
133 | case "NetWorkUtil实例":
134 | TextView textViewNet = new TextView(_this);
135 | textViewNet.setText(new StringBuilder().append("isMobileDataEnable:" + NetWorkUtil.isMobileDataEnable(_this) + "\n")
136 | .append("isNetWorkEnable:" + NetWorkUtil.isNetWorkEnable(_this) + "\n")
137 | .append("isWiFiConnected:" + NetWorkUtil.isWiFiConnected(_this) + "\n").toString());
138 | linearLayout.addView(textViewNet);
139 | break;
140 | case "PreferencesUtil实例":
141 | break;
142 | case "ScreenUtil实例":
143 | break;
144 | case "SDCardUtil实例":
145 | TextView textViewCard = new TextView(_this);
146 | textViewCard.setText(new StringBuilder().append("getDataCachePath:" + SDCardUtil.getDataCachePath(_this) + "\n")
147 | .append("getDataFilePath:" + SDCardUtil.getDataFilePath(_this) + "\n")
148 | .append("getSDCardDownloadCachePath:" + SDCardUtil.getSDCardDownloadCachePath() + "\n")
149 | .append("getSDCardRootPath:" + SDCardUtil.getSDCardRootPath() + "\n")
150 | .append("getSDCradPath:" + SDCardUtil.getSDCradPath() + "\n")
151 | .append("getSDCardAvailableSize:" + SDCardUtil.getSDCardAvailableSize() + "\n")
152 | .append("getSDCardSize:" + SDCardUtil.getSDCardSize() + "\n")
153 | .append("isSDCardEnable:" + SDCardUtil.isSDCardEnable() + "\n").toString());
154 | linearLayout.addView(textViewCard);
155 | break;
156 | case "XMLUtil实例":
157 | String strRequest = "";
158 | strRequest = strRequest + "";
159 | strRequest = strRequest + "";
160 | strRequest = strRequest + "";
161 | strRequest = strRequest + "zhangsan21";
162 | strRequest = strRequest + "lisi52";
163 | strRequest = strRequest + "wangwu22";
164 | strRequest = strRequest + "";
165 | strRequest = strRequest + "";
166 | TextView textViewSrcXml = new TextView(_this);
167 | textViewSrcXml.setText(strRequest);
168 | linearLayout.addView(textViewSrcXml);
169 | TextView tv_attribute = new TextView(_this);
170 | tv_attribute.setText("body的name值:" + XMLUtil.getTagAttribute(strRequest,"body","name"));
171 | linearLayout.addView(tv_attribute);
172 | break;
173 | case "DataBaseHelper实例":
174 | break;
175 | case "ReflectUtil实例":
176 | break;
177 | }
178 | }
179 | }
180 |
--------------------------------------------------------------------------------
/app/src/main/java/com/utils/dsw/androidutils/Student.java:
--------------------------------------------------------------------------------
1 | package com.utils.dsw.androidutils;
2 |
3 |
4 | import com.dsw.androidutils.database.AnnotationColumn;
5 | import com.dsw.androidutils.database.AnnotationTable;
6 |
7 | @AnnotationTable(name="Student")
8 | public class Student {
9 | @AnnotationColumn(name="name",isNull=false,isPrimaryKey=false,type="String")
10 | private String name;
11 | @AnnotationColumn(name="age",isNull=false,isPrimaryKey=false,type="int")
12 | private int age;
13 | private int id;
14 | public String getName() {
15 | return name;
16 | }
17 | public void setName(String name) {
18 | this.name = name;
19 | }
20 | public int getAge() {
21 | return age;
22 | }
23 | public void setAge(int age) {
24 | this.age = age;
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
13 |
14 |
22 |
27 |
28 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dengshiwei/AndroidUtils/6337fe85ee3546cbe3ef296670cdceb4327a6dc8/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dengshiwei/AndroidUtils/6337fe85ee3546cbe3ef296670cdceb4327a6dc8/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dengshiwei/AndroidUtils/6337fe85ee3546cbe3ef296670cdceb4327a6dc8/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/src.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dengshiwei/AndroidUtils/6337fe85ee3546cbe3ef296670cdceb4327a6dc8/app/src/main/res/mipmap-xhdpi/src.jpg
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dengshiwei/AndroidUtils/6337fe85ee3546cbe3ef296670cdceb4327a6dc8/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dengshiwei/AndroidUtils/6337fe85ee3546cbe3ef296670cdceb4327a6dc8/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | AndroidUtilsApplication
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/utils/dsw/androidutils/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.utils.dsw.androidutils;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | }
7 | dependencies {
8 | classpath 'com.android.tools.build:gradle:1.5.0'
9 |
10 | // NOTE: Do not place your application dependencies here; they belong
11 | // in the individual module build.gradle files
12 | }
13 | }
14 |
15 | allprojects {
16 | repositories {
17 | jcenter()
18 | }
19 | }
20 |
21 | task clean(type: Delete) {
22 | delete rootProject.buildDir
23 | }
24 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dengshiwei/AndroidUtils/6337fe85ee3546cbe3ef296670cdceb4327a6dc8/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Oct 21 11:34:03 PDT 2015
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':androidutils'
2 |
--------------------------------------------------------------------------------