Skip to content

Commit

Permalink
see 08/30 log
Browse files Browse the repository at this point in the history
  • Loading branch information
Blankj committed Aug 30, 2017
1 parent c54c4f8 commit 7068e6c
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 78 deletions.
4 changes: 2 additions & 2 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ getEntries : 获取压缩文件中的文件对象

Gradle:
``` groovy
compile 'com.blankj:utilcode:1.8.4'
compile 'com.blankj:utilcode:1.8.5'
```


Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ getEntries

Gradle:
``` groovy
compile 'com.blankj:utilcode:1.8.4'
compile 'com.blankj:utilcode:1.8.5'
```


Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 3 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
include ':app', ':utilcode', ':subutil'
include ':app',
':utilcode',
':subutil'
1 change: 1 addition & 0 deletions update_log.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions utilcode/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
Expand All @@ -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
24 changes: 17 additions & 7 deletions utilcode/src/main/java/com/blankj/utilcode/util/CacheUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ public Object getSerializable(@NonNull final String key, final Object defaultVal
/**
* 获取缓存大小
* <p>单位:字节</p>
* <p>调用了Thread.join(),需异步调用,否则可能主线程会卡顿</p>
*
* @return 缓存大小
*/
Expand All @@ -559,6 +560,7 @@ public long getCacheSize() {

/**
* 获取缓存个数
* <p>调用了Thread.join(),需异步调用,否则可能主线程会卡顿</p>
*
* @return 缓存个数
*/
Expand Down Expand Up @@ -591,19 +593,16 @@ private class CacheManager {
private final long sizeLimit;
private final int countLimit;
private final Map<File, Long> lastUsageDates = Collections.synchronizedMap(new HashMap<File, Long>());
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;
this.sizeLimit = sizeLimit;
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;
Expand All @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
88 changes: 31 additions & 57 deletions utilcode/src/main/java/com/blankj/utilcode/util/ToastUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Toast> sToastWeakReference;
private static WeakReference<View> sViewWeakReference;
private static final int DEFAULT_COLOR = 0xFEFFFFFF;
private static final Handler HANDLER = new Handler(Looper.getMainLooper());
private static Toast sToast;
private static WeakReference<View> 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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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() {
Expand All @@ -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;
}
}
Loading

0 comments on commit 7068e6c

Please sign in to comment.