diff --git a/README-CN.md b/README-CN.md
index 19f2605958..8b54bd5482 100644
--- a/README-CN.md
+++ b/README-CN.md
@@ -618,7 +618,7 @@ getEntries : 获取压缩文件中的文件对象
Gradle:
``` groovy
-compile 'com.blankj:utilcode:1.8.4'
+compile 'com.blankj:utilcode:1.8.5'
```
@@ -641,7 +641,7 @@ Utils.init(context);
[logo]: https://raw.githubusercontent.com/Blankj/AndroidUtilCode/master/art/logo.png
-[aucsvg]: https://img.shields.io/badge/AndroidUtilCode-v1.8.4-brightgreen.svg
+[aucsvg]: https://img.shields.io/badge/AndroidUtilCode-v1.8.5-brightgreen.svg
[auc]: https://github.com/Blankj/AndroidUtilCode
[apisvg]: https://img.shields.io/badge/API-14+-brightgreen.svg
diff --git a/README.md b/README.md
index 71b27961bf..5f1087a062 100644
--- a/README.md
+++ b/README.md
@@ -618,7 +618,7 @@ getEntries
Gradle:
``` groovy
-compile 'com.blankj:utilcode:1.8.4'
+compile 'com.blankj:utilcode:1.8.5'
```
@@ -641,7 +641,7 @@ Utils.init(context);
[logo]: https://raw.githubusercontent.com/Blankj/AndroidUtilCode/master/art/logo.png
-[aucsvg]: https://img.shields.io/badge/AndroidUtilCode-v1.8.4-brightgreen.svg
+[aucsvg]: https://img.shields.io/badge/AndroidUtilCode-v1.8.5-brightgreen.svg
[auc]: https://github.com/Blankj/AndroidUtilCode
[apisvg]: https://img.shields.io/badge/API-14+-brightgreen.svg
diff --git a/app/src/main/java/com/blankj/androidutilcode/core/snackbar/SnackbarActivity.java b/app/src/main/java/com/blankj/androidutilcode/core/snackbar/SnackbarActivity.java
index 4256571f59..37bd8ec178 100644
--- a/app/src/main/java/com/blankj/androidutilcode/core/snackbar/SnackbarActivity.java
+++ b/app/src/main/java/com/blankj/androidutilcode/core/snackbar/SnackbarActivity.java
@@ -26,7 +26,7 @@
*/
public class SnackbarActivity extends BaseBackActivity {
- private View snackBarRootView;
+ View snackBarRootView;
public static void start(Context context) {
Intent starter = new Intent(context, SnackbarActivity.class);
diff --git a/app/src/main/java/com/blankj/androidutilcode/core/toast/ToastActivity.java b/app/src/main/java/com/blankj/androidutilcode/core/toast/ToastActivity.java
index c69b9739e6..fdb8db8e9e 100644
--- a/app/src/main/java/com/blankj/androidutilcode/core/toast/ToastActivity.java
+++ b/app/src/main/java/com/blankj/androidutilcode/core/toast/ToastActivity.java
@@ -120,7 +120,7 @@ protected void onDestroy() {
}
private void resetToast() {
- ToastUtils.setMessageColor(0x12000000);
+ ToastUtils.setMessageColor(0xFFFFFFFF);
ToastUtils.setBgResource(-1);
ToastUtils.setView(null);
ToastUtils.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, getResources().getDimensionPixelSize(R.dimen.offset_64));
diff --git a/build.gradle b/build.gradle
index 9a1f063de9..767815f5e3 100644
--- a/build.gradle
+++ b/build.gradle
@@ -29,8 +29,8 @@ ext {
minSdkVersion = 14
targetSdkVersion = 25
- versionCode = 100800400
- versionName = '1.8.4'
+ versionCode = 100800500
+ versionName = '1.8.5'
// App dependencies
supportVersion = '25.3.1'
diff --git a/settings.gradle b/settings.gradle
index a624b0ea4d..7e181230c0 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -1 +1,3 @@
-include ':app', ':utilcode', ':subutil'
+include ':app',
+ ':utilcode',
+ ':subutil'
diff --git a/update_log.md b/update_log.md
index 0d253bd104..beb7930413 100644
--- a/update_log.md
+++ b/update_log.md
@@ -1,3 +1,4 @@
+* 17/08/30 修复ToastUtils弱引用带来的问题,修复CacheUtils异步问题,发布版本1.8.5
* 17/08/28 修复ToastUtils内存泄露,新增toast可根据系统字体显示不同字体,发布版本1.8.4
* 17/08/20 新增监听Activity生命周期,退出App,发布版本1.8.3
* 17/08/11 LogUtils的Builder改为Config,发布版本1.8.2
diff --git a/utilcode/build.gradle b/utilcode/build.gradle
index b59988837c..c9bcfd33d4 100644
--- a/utilcode/build.gradle
+++ b/utilcode/build.gradle
@@ -37,7 +37,7 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
release {
- minifyEnabled true
+ minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
@@ -63,4 +63,4 @@ dependencies {
testCompile "com.google.truth:truth:0.31"
}
//apply from: "https://raw.githubusercontent.com/xiaopansky/android-library-publish-to-jcenter/master/bintrayUpload.gradle"
-//gradlew bintrayUpload
+//gradle bintrayUpload
diff --git a/utilcode/src/main/java/com/blankj/utilcode/util/CacheUtils.java b/utilcode/src/main/java/com/blankj/utilcode/util/CacheUtils.java
index a1b294b4b9..8703b211f3 100644
--- a/utilcode/src/main/java/com/blankj/utilcode/util/CacheUtils.java
+++ b/utilcode/src/main/java/com/blankj/utilcode/util/CacheUtils.java
@@ -550,6 +550,7 @@ public Object getSerializable(@NonNull final String key, final Object defaultVal
/**
* 获取缓存大小
*
单位:字节
+ * 调用了Thread.join(),需异步调用,否则可能主线程会卡顿
*
* @return 缓存大小
*/
@@ -559,6 +560,7 @@ public long getCacheSize() {
/**
* 获取缓存个数
+ * 调用了Thread.join(),需异步调用,否则可能主线程会卡顿
*
* @return 缓存个数
*/
@@ -591,7 +593,8 @@ private class CacheManager {
private final long sizeLimit;
private final int countLimit;
private final Map lastUsageDates = Collections.synchronizedMap(new HashMap());
- private final File cacheDir;
+ private final File cacheDir;
+ private final Thread mThread;
private CacheManager(final File cacheDir, final long sizeLimit, final int countLimit) {
this.cacheDir = cacheDir;
@@ -599,11 +602,7 @@ private CacheManager(final File cacheDir, final long sizeLimit, final int countL
this.countLimit = countLimit;
cacheSize = new AtomicLong();
cacheCount = new AtomicInteger();
- calculateCacheSizeAndCacheCount();
- }
-
- private void calculateCacheSizeAndCacheCount() {
- new Thread(new Runnable() {
+ mThread = new Thread(new Runnable() {
@Override
public void run() {
int size = 0;
@@ -619,14 +618,25 @@ public void run() {
cacheCount.getAndAdd(count);
}
}
- }).start();
+ });
+ mThread.start();
}
private long getCacheSize() {
+ try {
+ mThread.join();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
return cacheSize.get();
}
private int getCacheCount() {
+ try {
+ mThread.join();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
return cacheCount.get();
}
diff --git a/utilcode/src/main/java/com/blankj/utilcode/util/SnackbarUtils.java b/utilcode/src/main/java/com/blankj/utilcode/util/SnackbarUtils.java
index 257bc7cb69..c4e993e2d6 100644
--- a/utilcode/src/main/java/com/blankj/utilcode/util/SnackbarUtils.java
+++ b/utilcode/src/main/java/com/blankj/utilcode/util/SnackbarUtils.java
@@ -28,7 +28,7 @@
*/
public final class SnackbarUtils {
- private static final int DEFAULT_COLOR = 0x12000000;
+ private static final int DEFAULT_COLOR = 0xFEFFFFFF;
public static final int LENGTH_INDEFINITE = -2;
diff --git a/utilcode/src/main/java/com/blankj/utilcode/util/SpanUtils.java b/utilcode/src/main/java/com/blankj/utilcode/util/SpanUtils.java
index ec6768cb22..fa8c4ee0c9 100644
--- a/utilcode/src/main/java/com/blankj/utilcode/util/SpanUtils.java
+++ b/utilcode/src/main/java/com/blankj/utilcode/util/SpanUtils.java
@@ -69,7 +69,7 @@
*/
public final class SpanUtils {
- private static final int DEFAULT_COLOR = 0x12000000;
+ private static final int DEFAULT_COLOR = 0xFEFFFFFF;
public static final int ALIGN_BOTTOM = 0;
public static final int ALIGN_BASELINE = 1;
diff --git a/utilcode/src/main/java/com/blankj/utilcode/util/ToastUtils.java b/utilcode/src/main/java/com/blankj/utilcode/util/ToastUtils.java
index ee687e07c8..b67c3db43a 100644
--- a/utilcode/src/main/java/com/blankj/utilcode/util/ToastUtils.java
+++ b/utilcode/src/main/java/com/blankj/utilcode/util/ToastUtils.java
@@ -9,10 +9,6 @@
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.support.v4.widget.TextViewCompat;
-import android.text.SpannableString;
-import android.text.Spanned;
-import android.text.style.ForegroundColorSpan;
-import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
@@ -31,11 +27,10 @@
*/
public final class ToastUtils {
- private static final String TAG = "ToastUtils";
- private static final int DEFAULT_COLOR = 0x12000000;
- private static final Handler sHandler = new Handler(Looper.getMainLooper());
- private static WeakReference sToastWeakReference;
- private static WeakReference sViewWeakReference;
+ private static final int DEFAULT_COLOR = 0xFEFFFFFF;
+ private static final Handler HANDLER = new Handler(Looper.getMainLooper());
+ private static Toast sToast;
+ private static WeakReference sViewWeakReference;
private static int gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
private static int xOffset = 0;
private static int yOffset = (int) (64 * Utils.getApp().getResources().getDisplayMetrics().density + 0.5);
@@ -89,9 +84,8 @@ public static View getView() {
if (view != null) {
return view;
}
- final Toast toast = getToastFromWR();
- if (toast != null) {
- return toast.getView();
+ if (sToast != null) {
+ return sToast.getView();
}
return null;
}
@@ -129,7 +123,7 @@ public static void setMessageColor(@ColorInt final int messageColor) {
* @param text 文本
*/
public static void showShortSafe(@NonNull final CharSequence text) {
- sHandler.post(new Runnable() {
+ HANDLER.post(new Runnable() {
@Override
public void run() {
show(text, Toast.LENGTH_SHORT);
@@ -143,7 +137,7 @@ public void run() {
* @param resId 资源Id
*/
public static void showShortSafe(@StringRes final int resId) {
- sHandler.post(new Runnable() {
+ HANDLER.post(new Runnable() {
@Override
public void run() {
show(resId, Toast.LENGTH_SHORT);
@@ -158,7 +152,7 @@ public void run() {
* @param args 参数
*/
public static void showShortSafe(@StringRes final int resId, final Object... args) {
- sHandler.post(new Runnable() {
+ HANDLER.post(new Runnable() {
@Override
public void run() {
show(resId, Toast.LENGTH_SHORT, args);
@@ -173,7 +167,7 @@ public void run() {
* @param args 参数
*/
public static void showShortSafe(final String format, final Object... args) {
- sHandler.post(new Runnable() {
+ HANDLER.post(new Runnable() {
@Override
public void run() {
show(format, Toast.LENGTH_SHORT, args);
@@ -187,7 +181,7 @@ public void run() {
* @param text 文本
*/
public static void showLongSafe(@NonNull final CharSequence text) {
- sHandler.post(new Runnable() {
+ HANDLER.post(new Runnable() {
@Override
public void run() {
show(text, Toast.LENGTH_LONG);
@@ -201,7 +195,7 @@ public void run() {
* @param resId 资源Id
*/
public static void showLongSafe(@StringRes final int resId) {
- sHandler.post(new Runnable() {
+ HANDLER.post(new Runnable() {
@Override
public void run() {
show(resId, Toast.LENGTH_LONG);
@@ -216,7 +210,7 @@ public void run() {
* @param args 参数
*/
public static void showLongSafe(@StringRes final int resId, final Object... args) {
- sHandler.post(new Runnable() {
+ HANDLER.post(new Runnable() {
@Override
public void run() {
show(resId, Toast.LENGTH_LONG, args);
@@ -231,7 +225,7 @@ public void run() {
* @param args 参数
*/
public static void showLongSafe(final String format, final Object... args) {
- sHandler.post(new Runnable() {
+ HANDLER.post(new Runnable() {
@Override
public void run() {
show(format, Toast.LENGTH_LONG, args);
@@ -319,7 +313,7 @@ public static void showLong(final String format, final Object... args) {
* 安全地显示短时自定义吐司
*/
public static void showCustomShortSafe(@LayoutRes final int layoutId) {
- sHandler.post(new Runnable() {
+ HANDLER.post(new Runnable() {
@Override
public void run() {
setView(layoutId);
@@ -332,7 +326,7 @@ public void run() {
* 安全地显示长时自定义吐司
*/
public static void showCustomLongSafe(@LayoutRes final int layoutId) {
- sHandler.post(new Runnable() {
+ HANDLER.post(new Runnable() {
@Override
public void run() {
setView(layoutId);
@@ -361,7 +355,7 @@ public static void showCustomLong(@LayoutRes final int layoutId) {
* 安全地显示短时自定义吐司
*/
public static void showCustomShortSafe(@NonNull final View view) {
- sHandler.post(new Runnable() {
+ HANDLER.post(new Runnable() {
@Override
public void run() {
setView(view);
@@ -374,7 +368,7 @@ public void run() {
* 安全地显示长时自定义吐司
*/
public static void showCustomLongSafe(@NonNull final View view) {
- sHandler.post(new Runnable() {
+ HANDLER.post(new Runnable() {
@Override
public void run() {
setView(view);
@@ -439,55 +433,36 @@ private static void show(final String format, final int duration, final Object..
*/
private static void show(final CharSequence text, final int duration) {
cancel();
- Toast toast;
final View view = getViewFromWR();
if (view != null) {
- toast = new Toast(Utils.getApp());
- toast.setView(view);
- toast.setDuration(duration);
+ sToast = new Toast(Utils.getApp());
+ sToast.setView(view);
+ sToast.setDuration(duration);
} else {
- if (messageColor != DEFAULT_COLOR) {
- SpannableString spannableString = new SpannableString(text);
- ForegroundColorSpan colorSpan = new ForegroundColorSpan(messageColor);
- spannableString.setSpan(colorSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- toast = Toast.makeText(Utils.getApp(), spannableString, duration);
- } else {
- toast = Toast.makeText(Utils.getApp(), text, duration);
- }
+ sToast = Toast.makeText(Utils.getApp(), text, duration);
// solve the font of toast
- TextViewCompat.setTextAppearance((TextView) toast.getView().findViewById(android.R.id.message), android.R.style.TextAppearance);
+ TextView tvMessage = (TextView) sToast.getView().findViewById(android.R.id.message);
+ TextViewCompat.setTextAppearance(tvMessage, android.R.style.TextAppearance);
+ tvMessage.setTextColor(messageColor);
}
- View toastView = toast.getView();
+ View toastView = sToast.getView();
if (bgResource != -1) {
toastView.setBackgroundResource(bgResource);
} else if (backgroundColor != DEFAULT_COLOR) {
toastView.setBackgroundColor(backgroundColor);
}
- toast.setGravity(gravity, xOffset, yOffset);
- sToastWeakReference = new WeakReference<>(toast);
- toast.show();
+ sToast.setGravity(gravity, xOffset, yOffset);
+ sToast.show();
}
/**
* 取消吐司显示
*/
public static void cancel() {
- Toast toast = getToastFromWR();
- if (toast != null) {
- toast.cancel();
- }
- sToastWeakReference = null;
- }
-
- private static Toast getToastFromWR() {
- if (sToastWeakReference != null) {
- final Toast toast = sToastWeakReference.get();
- if (toast != null) {
- return toast;
- }
+ if (sToast != null) {
+ sToast.cancel();
+ sToast = null;
}
- Log.e(TAG, "getToastFromWR: ", new NullPointerException("Toast is null"));
- return null;
}
private static View getViewFromWR() {
@@ -497,7 +472,6 @@ private static View getViewFromWR() {
return view;
}
}
- Log.e(TAG, "getViewFromWR: ", new NullPointerException("The custom view of toast is null"));
return null;
}
}
diff --git a/utilcode/src/main/java/com/blankj/utilcode/util/Utils.java b/utilcode/src/main/java/com/blankj/utilcode/util/Utils.java
index fe36607cfe..b194d71cd2 100644
--- a/utilcode/src/main/java/com/blankj/utilcode/util/Utils.java
+++ b/utilcode/src/main/java/com/blankj/utilcode/util/Utils.java
@@ -61,6 +61,7 @@ public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
@Override
public void onActivityDestroyed(Activity activity) {
sActivityList.remove(activity);
+ ToastUtils.cancel();
}
};
diff --git a/utilcode/src/test/java/com/blankj/utilcode/util/CacheUtilsTest.java b/utilcode/src/test/java/com/blankj/utilcode/util/CacheUtilsTest.java
index 8dad56b945..ae0a840614 100644
--- a/utilcode/src/test/java/com/blankj/utilcode/util/CacheUtilsTest.java
+++ b/utilcode/src/test/java/com/blankj/utilcode/util/CacheUtilsTest.java
@@ -319,7 +319,9 @@ public boolean equals(Object obj) {
}
static class SerializableTest implements Serializable {
- private static final long serialVersionUID = -8021039743766780051L;
+
+ private static final long serialVersionUID = -5806706668736895024L;
+
String author;
String className;