From e8d82752c02bd714b8dfb3e6175f8cb967be6fb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E6=99=93=E5=8B=87?= Date: Tue, 23 May 2017 18:45:56 +0800 Subject: [PATCH] v0.0.6 --- tiny/build.gradle | 2 +- tiny/src/main/java/com/zxy/tiny/Tiny.java | 19 +++++++++- .../zxy/tiny/common/ApplicationLoader.java | 37 +++++++++++++++++++ .../main/java/com/zxy/tiny/core/FileKit.java | 22 +++++++++++ 4 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 tiny/src/main/java/com/zxy/tiny/common/ApplicationLoader.java diff --git a/tiny/build.gradle b/tiny/build.gradle index f626f4a..46baeb8 100644 --- a/tiny/build.gradle +++ b/tiny/build.gradle @@ -31,7 +31,7 @@ dependencies { ext { upload_group_id = 'com.zxy.android' - upload_version = '0.0.5' + upload_version = '0.0.6' site_url = 'https://github.com/Sunzxyong/Tiny' git_url = 'https://github.com/Sunzxyong/Tiny.git' diff --git a/tiny/src/main/java/com/zxy/tiny/Tiny.java b/tiny/src/main/java/com/zxy/tiny/Tiny.java index e086ebd..c05e971 100644 --- a/tiny/src/main/java/com/zxy/tiny/Tiny.java +++ b/tiny/src/main/java/com/zxy/tiny/Tiny.java @@ -5,6 +5,7 @@ import android.net.Uri; import android.text.TextUtils; +import com.zxy.tiny.common.ApplicationLoader; import com.zxy.tiny.common.TinyException; import com.zxy.tiny.core.CompressEngine; import com.zxy.tiny.core.CompressKit; @@ -43,6 +44,12 @@ public Tiny debug(boolean isDebug) { return this; } + /** + * Initialization is not must. + * + * @param application + */ + @Deprecated public void init(Application application) { if (application == null) throw new TinyException.IllegalArgumentException("application can not be null!"); @@ -55,7 +62,7 @@ public boolean isDebug() { public Application getApplication() { if (mApplication == null) - throw new TinyException.UnsupportedOperationException("must initialize the 'Tiny' framework before use!"); + mApplication = ApplicationLoader.get(); return mApplication; } @@ -107,6 +114,16 @@ public synchronized CompressEngine source(int[] resIds) { return new CompressEngine().source(resIds); } + public synchronized boolean clearCompressDirectory() { + File dir = FileKit.getDefaultFileCompressDirectory(); + try { + return FileKit.clearDirectory(dir); + } catch (Exception e) { + //for android 6.0+,permission request + } + return false; + } + public static class BitmapCompressOptions { /** diff --git a/tiny/src/main/java/com/zxy/tiny/common/ApplicationLoader.java b/tiny/src/main/java/com/zxy/tiny/common/ApplicationLoader.java new file mode 100644 index 0000000..a714f62 --- /dev/null +++ b/tiny/src/main/java/com/zxy/tiny/common/ApplicationLoader.java @@ -0,0 +1,37 @@ +package com.zxy.tiny.common; + +import android.app.Application; + +import java.lang.reflect.Method; + +/** + * Created by zhengxiaoyong on 2017/5/23. + */ +public class ApplicationLoader { + + private static Application sApplication; + + public static Application get() { + return sApplication != null ? sApplication : getApplication(); + } + + private static Application getApplication() { + Application application = null; + Method method; + try { + method = Class.forName("android.app.AppGlobals").getDeclaredMethod("getInitialApplication"); + method.setAccessible(true); + application = (Application) method.invoke(null); + } catch (Exception e) { + try { + method = Class.forName("android.app.ActivityThread").getDeclaredMethod("currentApplication"); + method.setAccessible(true); + application = (Application) method.invoke(null); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + return application; + } + +} diff --git a/tiny/src/main/java/com/zxy/tiny/core/FileKit.java b/tiny/src/main/java/com/zxy/tiny/core/FileKit.java index 0859f7b..9347c70 100644 --- a/tiny/src/main/java/com/zxy/tiny/core/FileKit.java +++ b/tiny/src/main/java/com/zxy/tiny/core/FileKit.java @@ -4,6 +4,7 @@ import android.text.TextUtils; import com.zxy.tiny.Tiny; +import com.zxy.tiny.common.Logger; import java.io.File; import java.io.IOException; @@ -91,4 +92,25 @@ public static File[] wrap(String[] filePaths) { } return files; } + + public static boolean clearDirectory(File dir) { + if (dir == null || !dir.isDirectory() || !dir.exists()) + return false; + File[] files = dir.listFiles(); + int length = files.length; + for (int i = 0; i < length; i++) { + File file = files[i]; + if (file == null) + continue; + if (file.isFile() && file.exists()) { + boolean result = file.delete(); + Logger.e(file.getName() + (result ? " delete success!" : " delete failed!")); + continue; + } + if (file.isDirectory() && file.exists()) { + clearDirectory(file); + } + } + return true; + } }