diff --git a/README.md b/README.md index b067e48..fd8f175 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ allprojects { ``` dependencies { - compile 'com.github.AllenCoder.SuperUtils:apputils:1.0.3' + compile 'com.github.AllenCoder.SuperUtils:apputils:1.0.4' } ``` @@ -51,6 +51,9 @@ dependencies { | TouchEventUtil | Android Touch事件打印辅助工具类 | [TouchEventUtil][19] | | | WeakRefHander | 弱引用 handler 防止内存泄露 | [WeakRefHander][23] | | | SecretUtils | 3DES 加密/解密 | [SecretUtils][24] | | +| ToastUtils | Toast工具类(需要Utils.init(context)) | [ToastUtils][25] | | +| IOUtil | IOUtil (文件操作工具) | [IOUtil][26] | | + ### 2.Android 数据库处理工具类 @@ -62,7 +65,7 @@ dependencies { ``` dependencies { - compile 'com.github.AllenCoder.SuperUtils:dbutils:1.0.3' + compile 'com.github.AllenCoder.SuperUtils:dbutils:1.0.4' } ``` @@ -76,7 +79,7 @@ dependencies { ``` dependencies { - compile 'com.github.AllenCoder.SuperUtils:mediautil:1.0.3' + compile 'com.github.AllenCoder.SuperUtils:mediautil:1.0.4' } ``` @@ -124,4 +127,6 @@ dependencies { [21]: https://github.com/AllenCoder/SuperUtils/blob/master/mediautil/src/main/java/com/allen/mediautil/ImageTakerHelper.java [22]: https://github.com/AllenCoder/SuperUtils/blob/master/mediautil/src/main/java/com/allen/mediautil/Utils.java [23]: https://github.com/AllenCoder/SuperUtils/blob/master/mediautil/src/main/java/com/allen/mediautil/WeakRefHander.java - [24]: https://github.com/AllenCoder/SuperUtils/blob/master/mediautil/src/main/java/com/allen/mediautil/SecretUtils.java \ No newline at end of file + [24]: https://github.com/AllenCoder/SuperUtils/blob/master/mediautil/src/main/java/com/allen/mediautil/SecretUtils.java + [25]: https://github.com/AllenCoder/SuperUtils/blob/master/mediautil/src/main/java/com/allen/mediautil/ToastUtils.java + [26]: https://github.com/AllenCoder/SuperUtils/blob/master/mediautil/src/main/java/com/allen/mediautil/IOUtil.java \ No newline at end of file diff --git a/apputils/src/main/java/com/allen/apputils/IOUtil.java b/apputils/src/main/java/com/allen/apputils/IOUtil.java new file mode 100644 index 0000000..f6aa5bc --- /dev/null +++ b/apputils/src/main/java/com/allen/apputils/IOUtil.java @@ -0,0 +1,155 @@ +/* + * Copyright 2017 [AllenCoderr] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.allen.apputils; + +import android.database.Cursor; +import android.text.TextUtils; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.ByteArrayOutputStream; +import java.io.Closeable; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Reader; +import java.io.Writer; + +public class IOUtil { + + private IOUtil() { + } + + public static void closeQuietly(Closeable closeable) { + if (closeable != null) { + try { + closeable.close(); + } catch (Throwable ignored) { + MLog.d(ignored.getMessage(), ignored); + } + } + } + + public static void closeQuietly(Cursor cursor) { + if (cursor != null) { + try { + cursor.close(); + } catch (Throwable ignored) { + MLog.d(ignored.getMessage(), ignored); + } + } + } + + public static byte[] readBytes(InputStream in) throws IOException { + if (!(in instanceof BufferedInputStream)) { + in = new BufferedInputStream(in); + } + ByteArrayOutputStream out = null; + try { + out = new ByteArrayOutputStream(); + byte[] buf = new byte[1024]; + int len; + while ((len = in.read(buf)) != -1) { + out.write(buf, 0, len); + } + return out.toByteArray(); + } finally { + closeQuietly(out); + } + } + + public static byte[] readBytes(InputStream in, long skip, int size) throws IOException { + byte[] result = null; + if (skip > 0) { + long skipped = 0; + while (skip > 0 && (skipped = in.skip(skip)) > 0) { + skip -= skipped; + } + } + result = new byte[size]; + for (int i = 0; i < size; i++) { + result[i] = (byte) in.read(); + } + return result; + } + + public static String readStr(InputStream in) throws IOException { + return readStr(in, "UTF-8"); + } + + public static String readStr(InputStream in, String charset) throws IOException { + if (TextUtils.isEmpty(charset)) charset = "UTF-8"; + + if (!(in instanceof BufferedInputStream)) { + in = new BufferedInputStream(in); + } + Reader reader = new InputStreamReader(in, charset); + StringBuilder sb = new StringBuilder(); + char[] buf = new char[1024]; + int len; + while ((len = reader.read(buf)) >= 0) { + sb.append(buf, 0, len); + } + return sb.toString(); + } + + public static void writeStr(OutputStream out, String str) throws IOException { + writeStr(out, str, "UTF-8"); + } + + public static void writeStr(OutputStream out, String str, String charset) throws IOException { + if (TextUtils.isEmpty(charset)) charset = "UTF-8"; + + Writer writer = new OutputStreamWriter(out, charset); + writer.write(str); + writer.flush(); + } + + public static void copy(InputStream in, OutputStream out) throws IOException { + if (!(in instanceof BufferedInputStream)) { + in = new BufferedInputStream(in); + } + if (!(out instanceof BufferedOutputStream)) { + out = new BufferedOutputStream(out); + } + int len = 0; + byte[] buffer = new byte[1024]; + while ((len = in.read(buffer)) != -1) { + out.write(buffer, 0, len); + } + out.flush(); + } + + public static boolean deleteFileOrDir(File path) { + if (path == null || !path.exists()) { + return true; + } + if (path.isFile()) { + return path.delete(); + } + File[] files = path.listFiles(); + if (files != null) { + for (File file : files) { + deleteFileOrDir(file); + } + } + return path.delete(); + } +} diff --git a/apputils/src/main/java/com/allen/apputils/ToastUtils.java b/apputils/src/main/java/com/allen/apputils/ToastUtils.java new file mode 100644 index 0000000..c207142 --- /dev/null +++ b/apputils/src/main/java/com/allen/apputils/ToastUtils.java @@ -0,0 +1,295 @@ +/* + * Copyright 2017 [AllenCoderr] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.allen.apputils; + +import android.os.Handler; +import android.os.Looper; +import android.support.annotation.StringRes; +import android.widget.Toast; + +public class ToastUtils { + + private ToastUtils() { + throw new UnsupportedOperationException("u can't instantiate me..."); + } + + private static Toast sToast; + private static Handler sHandler = new Handler(Looper.getMainLooper()); + private static boolean isJumpWhenMore; + + /** + * 吐司初始化 + * + * @param isJumpWhenMore 当连续弹出吐司时,是要弹出新吐司还是只修改文本内容 + *

{@code true}: 弹出新吐司
{@code false}: 只修改文本内容

+ *

如果为{@code false}的话可用来做显示任意时长的吐司

+ */ + public static void init(boolean isJumpWhenMore) { + ToastUtils.isJumpWhenMore = isJumpWhenMore; + } + + /** + * 安全地显示短时吐司 + * + * @param text 文本 + */ + public static void showShortToastSafe(final CharSequence text) { + sHandler.post(new Runnable() { + @Override + public void run() { + showToast(text, Toast.LENGTH_SHORT); + } + }); + } + + /** + * 安全地显示短时吐司 + * + * @param resId 资源Id + */ + public static void showShortToastSafe(final @StringRes int resId) { + sHandler.post(new Runnable() { + @Override + public void run() { + showToast(resId, Toast.LENGTH_SHORT); + } + }); + } + + /** + * 安全地显示短时吐司 + * + * @param resId 资源Id + * @param args 参数 + */ + public static void showShortToastSafe(final @StringRes int resId, final Object... args) { + sHandler.post(new Runnable() { + @Override + public void run() { + showToast(resId, Toast.LENGTH_SHORT, args); + } + }); + } + + /** + * 安全地显示短时吐司 + * + * @param format 格式 + * @param args 参数 + */ + public static void showShortToastSafe(final String format, final Object... args) { + sHandler.post(new Runnable() { + @Override + public void run() { + showToast(format, Toast.LENGTH_SHORT, args); + } + }); + } + + /** + * 安全地显示长时吐司 + * + * @param text 文本 + */ + public static void showLongToastSafe(final CharSequence text) { + sHandler.post(new Runnable() { + @Override + public void run() { + showToast(text, Toast.LENGTH_LONG); + } + }); + } + + /** + * 安全地显示长时吐司 + * + * @param resId 资源Id + */ + public static void showLongToastSafe(final @StringRes int resId) { + sHandler.post(new Runnable() { + @Override + public void run() { + showToast(resId, Toast.LENGTH_LONG); + } + }); + } + + /** + * 安全地显示长时吐司 + * + * @param resId 资源Id + * @param args 参数 + */ + public static void showLongToastSafe(final @StringRes int resId, final Object... args) { + sHandler.post(new Runnable() { + @Override + public void run() { + showToast(resId, Toast.LENGTH_LONG, args); + } + }); + } + + /** + * 安全地显示长时吐司 + * + * @param format 格式 + * @param args 参数 + */ + public static void showLongToastSafe(final String format, final Object... args) { + sHandler.post(new Runnable() { + @Override + public void run() { + showToast(format, Toast.LENGTH_LONG, args); + } + }); + } + + /** + * 显示短时吐司 + * + * @param text 文本 + */ + public static void showShortToast(CharSequence text) { + showToast(text, Toast.LENGTH_SHORT); + } + + /** + * 显示短时吐司 + * + * @param resId 资源Id + */ + public static void showShortToast(@StringRes int resId) { + showToast(resId, Toast.LENGTH_SHORT); + } + + /** + * 显示短时吐司 + * + * @param resId 资源Id + * @param args 参数 + */ + public static void showShortToast(@StringRes int resId, Object... args) { + showToast(resId, Toast.LENGTH_SHORT, args); + } + + /** + * 显示短时吐司 + * + * @param format 格式 + * @param args 参数 + */ + public static void showShortToast(String format, Object... args) { + showToast(format, Toast.LENGTH_SHORT, args); + } + + /** + * 显示长时吐司 + * + * @param text 文本 + */ + public static void showLongToast(CharSequence text) { + showToast(text, Toast.LENGTH_LONG); + } + + /** + * 显示长时吐司 + * + * @param resId 资源Id + */ + public static void showLongToast(@StringRes int resId) { + showToast(resId, Toast.LENGTH_LONG); + } + + /** + * 显示长时吐司 + * + * @param resId 资源Id + * @param args 参数 + */ + public static void showLongToast(@StringRes int resId, Object... args) { + showToast(resId, Toast.LENGTH_LONG, args); + } + + /** + * 显示长时吐司 + * + * @param format 格式 + * @param args 参数 + */ + public static void showLongToast(String format, Object... args) { + showToast(format, Toast.LENGTH_LONG, args); + } + + /** + * 显示吐司 + * + * @param resId 资源Id + * @param duration 显示时长 + */ + private static void showToast(@StringRes int resId, int duration) { + showToast(Utils.getContext().getResources().getText(resId).toString(), duration); + } + + /** + * 显示吐司 + * + * @param resId 资源Id + * @param duration 显示时长 + * @param args 参数 + */ + private static void showToast(@StringRes int resId, int duration, Object... args) { + showToast(String.format(Utils.getContext().getResources().getString(resId), args), duration); + } + + /** + * 显示吐司 + * + * @param format 格式 + * @param duration 显示时长 + * @param args 参数 + */ + private static void showToast(String format, int duration, Object... args) { + showToast(String.format(format, args), duration); + } + + /** + * 显示吐司 + * + * @param text 文本 + * @param duration 显示时长 + */ + private static void showToast(CharSequence text, int duration) { + if (isJumpWhenMore) cancelToast(); + if (sToast == null) { + sToast = Toast.makeText(Utils.getContext(), text, duration); + } else { + sToast.setText(text); + sToast.setDuration(duration); + } + sToast.show(); + } + + /** + * 取消吐司显示 + */ + public static void cancelToast() { + if (sToast != null) { + sToast.cancel(); + sToast = null; + } + } +} diff --git a/apputils/src/main/java/com/allen/apputils/Utils.java b/apputils/src/main/java/com/allen/apputils/Utils.java new file mode 100644 index 0000000..08e7c5e --- /dev/null +++ b/apputils/src/main/java/com/allen/apputils/Utils.java @@ -0,0 +1,55 @@ +/* + * Copyright 2017 [AllenCoderr] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.allen.apputils; + +import android.content.Context; + +/** + *
+ *     author: Blankj
+ *     blog  : http://blankj.com
+ *     time  : 16/12/08
+ *     desc  : Utils初始化相关
+ * 
+ */ +public class Utils { + + private static Context context; + + private Utils() { + throw new UnsupportedOperationException("u can't instantiate me..."); + } + + /** + * 初始化工具类 + * + * @param context 上下文 + */ + public static void init(Context context) { + Utils.context = context.getApplicationContext(); + } + + /** + * 获取ApplicationContext + * + * @return ApplicationContext + */ + public static Context getContext() { + if (context != null) return context; + throw new NullPointerException("u should init first"); + } +}