Skip to content

Commit

Permalink
新增部分工具类
Browse files Browse the repository at this point in the history
调整网络异常的提示语
  • Loading branch information
LJYcoder committed Jul 20, 2018
1 parent 2dd96ba commit 932177a
Show file tree
Hide file tree
Showing 13 changed files with 405 additions and 41 deletions.
4 changes: 4 additions & 0 deletions VERSION.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
## 版本信息
- v1.0.12、1.0.13 (2018/7/20)
- 新增工具类:ConfigUtil(设备配置相关工具类)、FontTypeUtil(修改应用字体工具类)、KeyboardUtil(软键盘工具类)
- 调整网络请求异常的提示信息

- v1.0.10、1.0.11 (2018/6/6)
- 图片加载模块的LoadOption新增边框颜色、边框粗细选项(目前仅适用于圆形模式)
- 网络模块支持根据最新设置的config刷新管理者(DevRing.httpManager().refreshInstance();)
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/com/dev/base/mvp/model/UploadModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ public File getUploadFile(Activity activity, int reqCode, Intent intent) {
e.printStackTrace();
}
}
//少部分机型cursor会为空,则采用以下方式获取路径。
else {
filePath = photoUri.getPath();
}
break;

case ImageUtil.REQ_PHOTO_CAMERA:
Expand Down Expand Up @@ -146,7 +150,7 @@ public File getUploadFile(Activity activity, int reqCode, Intent intent) {
@Override
public void deleteTempFile(Activity activity) {
//将临时保存的图片文件删除
FileUtil.deleteFile(FileUtil.getDirectory(FileUtil.getExternalCacheDir(activity), "upload_image"));
FileUtil.deleteFile(FileUtil.getDirectory(FileUtil.getExternalCacheDir(activity), "upload_image"), false);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ protected void initView(Bundle savedInstanceState) {

//如果经过了配置变化而重建(如横竖屏切换),且tag为photo的DialogFragment不为空,则不使用新建的DialogFragment。
//不做此操作的话而使用新建的DialogFragment的话,会导致“弹出菜单栏---> 配置变化(如横竖屏切换)---> 点击菜单项触发dissmiss() ---> 空指针异常”
//java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.FragmentTransaction android.app.FragmentManager.beginTransaction()' on a null object reference
//因为新建的DialogFragment还未通过show方法进行FragmentTransaction的add、commit操作。
if (savedInstanceState != null && getFragmentManager().findFragmentByTag("photo") != null) {
mPhotoDialogFragment = (PhotoDialogFragment) getFragmentManager().findFragmentByTag("photo");
}
Expand Down
4 changes: 2 additions & 2 deletions devring/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 23
versionCode 12
versionName "1.0.11"
versionCode 14
versionName "1.0.13"

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ public class ThrowableHandler {
*/
public static HttpThrowable handleThrowable(Throwable throwable) {
if (throwable instanceof HttpException) {
return new HttpThrowable(HttpThrowable.HTTP_ERROR, "网络(协议)错误", throwable);
return new HttpThrowable(HttpThrowable.HTTP_ERROR, "网络(协议)异常", throwable);
} else if (throwable instanceof JsonParseException || throwable instanceof JSONException || throwable instanceof ParseException) {
return new HttpThrowable(HttpThrowable.PARSE_ERROR, "解析错误", throwable);
return new HttpThrowable(HttpThrowable.PARSE_ERROR, "数据解析异常", throwable);
} else if (throwable instanceof UnknownHostException) {
return new HttpThrowable(HttpThrowable.NO_NET_ERROR, "DNS解析错误(无网络)", throwable);
return new HttpThrowable(HttpThrowable.NO_NET_ERROR, "网络连接失败,请稍后重试", throwable);
} else if (throwable instanceof SocketTimeoutException) {
return new HttpThrowable(HttpThrowable.TIME_OUT_ERROR, "连接超时错误", throwable);
return new HttpThrowable(HttpThrowable.TIME_OUT_ERROR, "连接超时", throwable);
} else if (throwable instanceof ConnectException) {
return new HttpThrowable(HttpThrowable.CONNECT_ERROR, "连接错误", throwable);
return new HttpThrowable(HttpThrowable.CONNECT_ERROR, "连接异常", throwable);
} else if (throwable instanceof javax.net.ssl.SSLHandshakeException) {
return new HttpThrowable(HttpThrowable.SSL_ERROR, "证书验证错误", throwable);
return new HttpThrowable(HttpThrowable.SSL_ERROR, "证书验证失败", throwable);
} else {
return new HttpThrowable(HttpThrowable.UNKNOWN, throwable.getMessage(), throwable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
Expand Down Expand Up @@ -235,7 +234,7 @@ private RequestBuilder load(RequestBuilder requestBuilder, LoadOption loadOption
grayscaleTransformation = new GrayscaleTransformation();
}

MultiTransformation multiTransformation = getMultiTransformation(new CenterCrop(), circleTransformation, roundedCornersTransformation, blurTransformation, grayscaleTransformation);
MultiTransformation multiTransformation = getMultiTransformation(circleTransformation, roundedCornersTransformation, blurTransformation, grayscaleTransformation);
if (multiTransformation != null) requestOptions.transform(multiTransformation);
}
return requestBuilder.apply(requestOptions);
Expand Down
22 changes: 2 additions & 20 deletions devring/src/main/java/com/ljy/devring/util/ColorBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.ColorInt;
Expand Down Expand Up @@ -318,7 +317,7 @@ private int limitDepthOrAlpha(int depthOrAlpha) {
private View createStatusBarView(Context context, @ColorInt int color) {
View statusBarView = new View(context);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams
(FrameLayout.LayoutParams.MATCH_PARENT, getStatusBarHeight(context));
(FrameLayout.LayoutParams.MATCH_PARENT, ConfigUtil.getStatusBarHeight(context));
params.gravity = Gravity.TOP;
statusBarView.setLayoutParams(params);
statusBarView.setBackgroundColor(color);
Expand All @@ -328,7 +327,7 @@ private View createStatusBarView(Context context, @ColorInt int color) {
private View createNavBarView(Context context, @ColorInt int color) {
View navBarView = new View(context);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams
(FrameLayout.LayoutParams.MATCH_PARENT, getNavigationHeight(context));
(FrameLayout.LayoutParams.MATCH_PARENT, ConfigUtil.getNavigationBarHeight(context));
params.gravity = Gravity.BOTTOM;
navBarView.setLayoutParams(params);
navBarView.setBackgroundColor(color);
Expand Down Expand Up @@ -385,23 +384,6 @@ private void setRootView(Activity activity, boolean fit) {
}
}



private int getStatusBarHeight(Context context) {
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
return resources.getDimensionPixelSize(resourceId);
}


private int getNavigationHeight(Context context) {
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
return resources.getDimensionPixelSize(resourceId);
}



public static class ColorBuilder {

@ColorInt
Expand Down
98 changes: 98 additions & 0 deletions devring/src/main/java/com/ljy/devring/util/ConfigUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.ljy.devring.util;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.res.Resources;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import java.lang.reflect.Method;

/**
* author: ljy
* date: 2018/7/20
* description:
*/

public class ConfigUtil {

/**
* 获取屏幕宽高,会加上虚拟按键栏的长度
*
* @param context
* @return
*/
public static int[] getScreenSize(Context context) {
int[] size = new int[2];
if (Build.VERSION.SDK_INT >= 17) {
DisplayMetrics dm = new DisplayMetrics();
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getRealMetrics(dm);
size[0] = dm.widthPixels; // 屏幕宽
size[1] = dm.heightPixels; // 屏幕高
} else {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
@SuppressWarnings("rawtypes") Class c;
try {
c = Class.forName("android.view.Display");
@SuppressWarnings("unchecked") Method method = c.getMethod("getRealMetrics", DisplayMetrics.class);
method.invoke(display, dm);
} catch (Exception e) {
e.printStackTrace();
}
size[0] = dm.widthPixels; // 屏幕宽
size[1] = dm.heightPixels; // 屏幕高
}
return size;
}

/**
* 获取屏幕的长边(即竖屏下的高,横屏下的宽)
*/
public static int getScreenLongSide(Context context) {
int[] screenSize = getScreenSize(context);
return Math.max(screenSize[0], screenSize[1]);
}

/**
* 获取屏幕的短边(即竖屏下的宽,横屏下的高)
*/
public static int getScreenShortSide(Context context) {
int[] screenSize = getScreenSize(context);
return Math.min(screenSize[0], screenSize[1]);
}

/**
* 获取状态栏高度
*/
public static int getStatusBarHeight(Context context) {
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
return resources.getDimensionPixelSize(resourceId);
}

/**
* 获取底部导航栏高度
*/
public static int getNavigationBarHeight(Context context) {
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
return resources.getDimensionPixelSize(resourceId);
}

/**
* 判断当前应用是否是debug模式
*/
public static boolean isApkInDebug(Context context) {
try {
ApplicationInfo info = context.getApplicationInfo();
return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
} catch (Exception e) {
return false;
}
}
}
20 changes: 12 additions & 8 deletions devring/src/main/java/com/ljy/devring/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,13 @@ public static long calculateFileSize(File file) {
}

/**
* 删除文件/文件夹
* 如果是文件夹,则会删除其下的文件以及它本身
* 删除文件夹中的所有文件
*
* @param file file
* @param file 指定的文件夹
* @param isDeleteSelf 是否删除文件夹本身
* @return true代表成功删除
*/
public static boolean deleteFile(File file) {
public static boolean deleteFile(File file, boolean isDeleteSelf) {
if (file == null) {
return true;
}
Expand All @@ -216,7 +216,7 @@ public static boolean deleteFile(File file) {
if (null != files) {
for (File subFile : files) {
if (subFile.isDirectory()) {
if (!deleteFile(subFile)) {
if (!deleteFile(subFile,true)) {
result = false;
}
} else {
Expand All @@ -227,8 +227,11 @@ public static boolean deleteFile(File file) {
}
}
}
if (!file.delete()) {
result = false;

if (isDeleteSelf) {
if (!file.delete()) {
result = false;
}
}

return result;
Expand Down Expand Up @@ -299,7 +302,7 @@ public static String getDataDirectory() {
return Environment.getDataDirectory().getAbsolutePath();
}

//返回"/storage/emulated/0"目录
//返回"/storage/emulated/0"目录(需要外部存储权限)
public static String getExternalStorageDirectory() {
return Environment.getExternalStorageDirectory().getAbsolutePath();
}
Expand All @@ -315,6 +318,7 @@ public static String getDownloadCacheDirectory() {
}

/**
* (需要外部存储权限)
* @param type 所放的文件的类型,传入的参数是Environment类中的DIRECTORY_XXX静态变量
* @return 返回"/storage/emulated/0/xxx"目录
* 例如传入Environment.DIRECTORY_ALARMS则返回"/storage/emulated/0/Alarms"
Expand Down
Loading

0 comments on commit 932177a

Please sign in to comment.